| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
#ifdef HAVE_CONFIG_H |
|---|
| 20 |
#include "config.h" |
|---|
| 21 |
#endif |
|---|
| 22 |
|
|---|
| 23 |
#define NO_IMPORT_PYGOBJECT |
|---|
| 24 |
#include "py_midgard.h" |
|---|
| 25 |
|
|---|
| 26 |
PyTypeObject G_GNUC_INTERNAL Pymidgard_qb_Type; |
|---|
| 27 |
|
|---|
| 28 |
#define QB_DEBUG(__name) \ |
|---|
| 29 |
CHECK_MGD; \ |
|---|
| 30 |
CLASS_METHOD_DEBUG(Pymidgard_qb_Type.tp_name, __name); |
|---|
| 31 |
|
|---|
| 32 |
typedef struct { |
|---|
| 33 |
PyObject_HEAD |
|---|
| 34 |
MidgardConfig *mgdcfg; |
|---|
| 35 |
} PyMidgardQueryBuilder; |
|---|
| 36 |
|
|---|
| 37 |
static int |
|---|
| 38 |
__query_builder_constructor(PyGObject *self, PyObject *args, PyObject *kwargs) |
|---|
| 39 |
{ |
|---|
| 40 |
QB_DEBUG("__init__"); |
|---|
| 41 |
const gchar *classname; |
|---|
| 42 |
if(!PyArg_ParseTuple(args, "s", &classname)) |
|---|
| 43 |
return -1; |
|---|
| 44 |
|
|---|
| 45 |
MidgardConnection *mgd = |
|---|
| 46 |
_py_midgard_connection_singleton_get(); |
|---|
| 47 |
|
|---|
| 48 |
MidgardQueryBuilder *builder = |
|---|
| 49 |
midgard_query_builder_new(mgd, classname); |
|---|
| 50 |
|
|---|
| 51 |
if(!builder) |
|---|
| 52 |
return -1; |
|---|
| 53 |
|
|---|
| 54 |
self->obj = G_OBJECT(builder); |
|---|
| 55 |
|
|---|
| 56 |
return 0; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
static PyObject * |
|---|
| 60 |
pymidgard_qb_add_constraint(PyGObject *self, PyObject *args) |
|---|
| 61 |
{ |
|---|
| 62 |
QB_DEBUG("add_constraint"); |
|---|
| 63 |
const gchar *prop, *op; |
|---|
| 64 |
PyObject *pvalue; |
|---|
| 65 |
gboolean added; |
|---|
| 66 |
if(!PyArg_ParseTuple(args, "ssO", &prop, &op, &pvalue)) |
|---|
| 67 |
return NULL; |
|---|
| 68 |
|
|---|
| 69 |
MidgardQueryBuilder *builder = |
|---|
| 70 |
MIDGARD_QUERY_BUILDER(self->obj); |
|---|
| 71 |
|
|---|
| 72 |
GValue gval = gvalue_from_pyobject(pvalue); |
|---|
| 73 |
pyg_value_from_pyobject(&gval, pvalue); |
|---|
| 74 |
|
|---|
| 75 |
added = midgard_query_builder_add_constraint(builder, prop, op, &gval); |
|---|
| 76 |
|
|---|
| 77 |
if(added) |
|---|
| 78 |
Py_RETURN_TRUE; |
|---|
| 79 |
|
|---|
| 80 |
Py_RETURN_FALSE; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
static PyObject * |
|---|
| 84 |
pymidgard_qb_add_constraint_with_property(PyGObject *self, PyObject *args) |
|---|
| 85 |
{ |
|---|
| 86 |
QB_DEBUG("add_constraint_with_property"); |
|---|
| 87 |
const gchar *prop_a, *op, *prop_b; |
|---|
| 88 |
PyObject *pvalue; |
|---|
| 89 |
gboolean added; |
|---|
| 90 |
if(!PyArg_ParseTuple(args, "sss", &prop_a, &op, &prop_b)) |
|---|
| 91 |
return NULL; |
|---|
| 92 |
|
|---|
| 93 |
MidgardQueryBuilder *builder = |
|---|
| 94 |
MIDGARD_QUERY_BUILDER(self->obj); |
|---|
| 95 |
|
|---|
| 96 |
added = midgard_query_builder_add_constraint_with_property( |
|---|
| 97 |
builder, prop_a, op, prop_b); |
|---|
| 98 |
|
|---|
| 99 |
if(added) |
|---|
| 100 |
Py_RETURN_TRUE; |
|---|
| 101 |
|
|---|
| 102 |
Py_RETURN_FALSE; |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
static PyObject * |
|---|
| 106 |
pymidgard_qb_set_lang(PyGObject *self, PyObject *args) |
|---|
| 107 |
{ |
|---|
| 108 |
QB_DEBUG("set_lang"); |
|---|
| 109 |
guint lang; |
|---|
| 110 |
if(!PyArg_ParseTuple(args, "i", &lang)) |
|---|
| 111 |
return NULL; |
|---|
| 112 |
|
|---|
| 113 |
MidgardQueryBuilder *builder = |
|---|
| 114 |
MIDGARD_QUERY_BUILDER(self->obj); |
|---|
| 115 |
|
|---|
| 116 |
midgard_query_builder_set_lang(builder, lang); |
|---|
| 117 |
|
|---|
| 118 |
return Py_None; |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
static PyObject * |
|---|
| 122 |
pymidgard_qb_unset_languages(PyGObject *self, PyObject *args) |
|---|
| 123 |
{ |
|---|
| 124 |
QB_DEBUG("unset_languages"); |
|---|
| 125 |
if(!PyArg_ParseTuple(args, "")) |
|---|
| 126 |
return NULL; |
|---|
| 127 |
|
|---|
| 128 |
MidgardQueryBuilder *builder = |
|---|
| 129 |
MIDGARD_QUERY_BUILDER(self->obj); |
|---|
| 130 |
|
|---|
| 131 |
midgard_query_builder_unset_languages(builder); |
|---|
| 132 |
|
|---|
| 133 |
return Py_None; |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
static PyObject * |
|---|
| 137 |
pymidgard_qb_begin_group(PyGObject *self, PyObject *args) |
|---|
| 138 |
{ |
|---|
| 139 |
QB_DEBUG("begin_group"); |
|---|
| 140 |
const gchar *group_op; |
|---|
| 141 |
if(!PyArg_ParseTuple(args, "s", &group_op)) |
|---|
| 142 |
return NULL; |
|---|
| 143 |
|
|---|
| 144 |
MidgardQueryBuilder *builder = |
|---|
| 145 |
MIDGARD_QUERY_BUILDER(self->obj); |
|---|
| 146 |
|
|---|
| 147 |
if(midgard_query_builder_begin_group(builder, group_op)) |
|---|
| 148 |
Py_RETURN_TRUE; |
|---|
| 149 |
|
|---|
| 150 |
Py_RETURN_FALSE; |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
static PyObject * |
|---|
| 154 |
pymidgard_qb_end_group(PyGObject *self, PyObject *args) |
|---|
| 155 |
{ |
|---|
| 156 |
QB_DEBUG("end_group"); |
|---|
| 157 |
|
|---|
| 158 |
if(!PyArg_ParseTuple(args, "")) |
|---|
| 159 |
return NULL; |
|---|
| 160 |
|
|---|
| 161 |
MidgardQueryBuilder *builder = |
|---|
| 162 |
MIDGARD_QUERY_BUILDER(self->obj); |
|---|
| 163 |
|
|---|
| 164 |
if(midgard_query_builder_end_group(builder)) |
|---|
| 165 |
Py_RETURN_TRUE; |
|---|
| 166 |
|
|---|
| 167 |
Py_RETURN_FALSE; |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 170 |
static PyObject * |
|---|
| 171 |
pymidgard_qb_add_order(PyGObject *self, PyObject *args) |
|---|
| 172 |
{ |
|---|
| 173 |
QB_DEBUG("add_order"); |
|---|
| 174 |
|
|---|
| 175 |
const gchar *prop, *order; |
|---|
| 176 |
if(!PyArg_ParseTuple(args, "ss", &prop, &order)) |
|---|
| 177 |
return NULL; |
|---|
| 178 |
|
|---|
| 179 |
MidgardQueryBuilder *builder = |
|---|
| 180 |
MIDGARD_QUERY_BUILDER(self->obj); |
|---|
| 181 |
|
|---|
| 182 |
if(midgard_query_builder_add_order(builder, prop, order)) |
|---|
| 183 |
Py_RETURN_TRUE; |
|---|
| 184 |
|
|---|
| 185 |
Py_RETURN_FALSE; |
|---|
| 186 |
} |
|---|
| 187 |
|
|---|
| 188 |
static PyObject * |
|---|
| 189 |
pymidgard_qb_set_offset(PyGObject *self, PyObject *args) |
|---|
| 190 |
{ |
|---|
| 191 |
QB_DEBUG("set_offset"); |
|---|
| 192 |
|
|---|
| 193 |
guint offset; |
|---|
| 194 |
if(!PyArg_ParseTuple(args, "i", &offset)) |
|---|
| 195 |
return NULL; |
|---|
| 196 |
|
|---|
| 197 |
MidgardQueryBuilder *builder = |
|---|
| 198 |
MIDGARD_QUERY_BUILDER(self->obj); |
|---|
| 199 |
|
|---|
| 200 |
midgard_query_builder_set_offset(builder, offset); |
|---|
| 201 |
|
|---|
| 202 |
return Py_None; |
|---|
| 203 |
} |
|---|
| 204 |
|
|---|
| 205 |
static PyObject * |
|---|
| 206 |
pymidgard_qb_set_limit(PyGObject *self, PyObject *args) |
|---|
| 207 |
{ |
|---|
| 208 |
QB_DEBUG("set_limit"); |
|---|
| 209 |
|
|---|
| 210 |
long limit; |
|---|
| 211 |
if(!PyArg_ParseTuple(args, "l", &limit)) |
|---|
| 212 |
return NULL; |
|---|
| 213 |
|
|---|
| 214 |
MidgardQueryBuilder *builder = |
|---|
| 215 |
MIDGARD_QUERY_BUILDER(self->obj); |
|---|
| 216 |
|
|---|
| 217 |
midgard_query_builder_set_limit(builder, limit); |
|---|
| 218 |
|
|---|
| 219 |
return Py_None; |
|---|
| 220 |
} |
|---|
| 221 |
|
|---|
| 222 |
static PyObject * |
|---|
| 223 |
pymidgard_qb_count(PyGObject *self, PyObject *args) |
|---|
| 224 |
{ |
|---|
| 225 |
QB_DEBUG("count"); |
|---|
| 226 |
|
|---|
| 227 |
if(!PyArg_ParseTuple(args, "")) |
|---|
| 228 |
return NULL; |
|---|
| 229 |
|
|---|
| 230 |
MidgardQueryBuilder *builder = |
|---|
| 231 |
MIDGARD_QUERY_BUILDER(self->obj); |
|---|
| 232 |
|
|---|
| 233 |
guint counted = midgard_query_builder_count(builder); |
|---|
| 234 |
|
|---|
| 235 |
return Py_BuildValue("i", counted); |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
static PyObject * |
|---|
| 239 |
pymidgard_qb_include_deleted(PyGObject *self, PyObject *args) |
|---|
| 240 |
{ |
|---|
| 241 |
QB_DEBUG("include_deleted"); |
|---|
| 242 |
|
|---|
| 243 |
if(!PyArg_ParseTuple(args, "")) |
|---|
| 244 |
return NULL; |
|---|
| 245 |
|
|---|
| 246 |
MidgardQueryBuilder *builder = |
|---|
| 247 |
MIDGARD_QUERY_BUILDER(self->obj); |
|---|
| 248 |
|
|---|
| 249 |
midgard_query_builder_include_deleted(builder); |
|---|
| 250 |
|
|---|
| 251 |
return Py_None; |
|---|
| 252 |
} |
|---|
| 253 |
|
|---|
| 254 |
static PyObject * |
|---|
| 255 |
pymidgard_qb_execute(PyGObject *self, PyObject *args) |
|---|
| 256 |
{ |
|---|
| 257 |
QB_DEBUG("execute"); |
|---|
| 258 |
|
|---|
| 259 |
if(!PyArg_ParseTuple(args, "")) |
|---|
| 260 |
return NULL; |
|---|
| 261 |
|
|---|
| 262 |
MidgardQueryBuilder *builder = |
|---|
| 263 |
MIDGARD_QUERY_BUILDER(self->obj); |
|---|
| 264 |
|
|---|
| 265 |
guint i = 0; |
|---|
| 266 |
GObject **objects = |
|---|
| 267 |
midgard_query_builder_execute(builder, NULL); |
|---|
| 268 |
|
|---|
| 269 |
if(objects == NULL) { |
|---|
| 270 |
|
|---|
| 271 |
PyObject *list = PyTuple_New(i); |
|---|
| 272 |
return list; |
|---|
| 273 |
} |
|---|
| 274 |
|
|---|
| 275 |
while(objects[i] != NULL) |
|---|
| 276 |
i++; |
|---|
| 277 |
|
|---|
| 278 |
PyObject *list = PyTuple_New(i); |
|---|
| 279 |
|
|---|
| 280 |
OBJECTS2LIST(objects, list); |
|---|
| 281 |
|
|---|
| 282 |
g_free(objects); |
|---|
| 283 |
|
|---|
| 284 |
return list; |
|---|
| 285 |
} |
|---|
| 286 |
|
|---|
| 287 |
static PyMethodDef pymidgard_qb_methods[] = { |
|---|
| 288 |
{ "add_constraint", (PyCFunction)pymidgard_qb_add_constraint, METH_VARARGS }, |
|---|
| 289 |
{ "add_constraint_with_property", |
|---|
| 290 |
(PyCFunction)pymidgard_qb_add_constraint_with_property, METH_VARARGS }, |
|---|
| 291 |
{ "set_lang", (PyCFunction)pymidgard_qb_set_lang, METH_VARARGS }, |
|---|
| 292 |
{ "unset_languages", (PyCFunction)pymidgard_qb_unset_languages, METH_VARARGS }, |
|---|
| 293 |
{ "begin_group", (PyCFunction)pymidgard_qb_begin_group, METH_VARARGS }, |
|---|
| 294 |
{ "end_group", (PyCFunction)pymidgard_qb_end_group, METH_VARARGS }, |
|---|
| 295 |
{ "set_offset", (PyCFunction)pymidgard_qb_set_offset, METH_VARARGS }, |
|---|
| 296 |
{ "set_limit", (PyCFunction)pymidgard_qb_set_limit, METH_VARARGS }, |
|---|
| 297 |
{ "add_order", (PyCFunction)pymidgard_qb_add_order, METH_VARARGS }, |
|---|
| 298 |
{ "include_deleted", (PyCFunction)pymidgard_qb_include_deleted, METH_VARARGS }, |
|---|
| 299 |
{ "count", (PyCFunction)pymidgard_qb_count, METH_VARARGS }, |
|---|
| 300 |
{ "execute", (PyCFunction)pymidgard_qb_execute, METH_VARARGS }, |
|---|
| 301 |
{ NULL, NULL, 0 } |
|---|
| 302 |
}; |
|---|
| 303 |
|
|---|
| 304 |
PyTypeObject G_GNUC_INTERNAL Pymidgard_qb_Type = { |
|---|
| 305 |
PyObject_HEAD_INIT(NULL) |
|---|
| 306 |
0, |
|---|
| 307 |
"query_builder", |
|---|
| 308 |
sizeof(PyGObject), |
|---|
| 309 |
0, |
|---|
| 310 |
|
|---|
| 311 |
(destructor)0, |
|---|
| 312 |
(printfunc)0, |
|---|
| 313 |
(getattrfunc)0, |
|---|
| 314 |
(setattrfunc)0, |
|---|
| 315 |
(cmpfunc)0, |
|---|
| 316 |
(reprfunc)0, |
|---|
| 317 |
(PyNumberMethods*)0, |
|---|
| 318 |
(PySequenceMethods*)0, |
|---|
| 319 |
(PyMappingMethods*)0, |
|---|
| 320 |
(hashfunc)0, |
|---|
| 321 |
(ternaryfunc)0, |
|---|
| 322 |
(reprfunc)0, |
|---|
| 323 |
(getattrofunc)0, |
|---|
| 324 |
(setattrofunc)0, |
|---|
| 325 |
(PyBufferProcs*)0, |
|---|
| 326 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
|---|
| 327 |
NULL, |
|---|
| 328 |
(traverseproc)0, |
|---|
| 329 |
(inquiry)0, |
|---|
| 330 |
(richcmpfunc)0, |
|---|
| 331 |
offsetof(PyGObject, weakreflist), |
|---|
| 332 |
(getiterfunc)0, |
|---|
| 333 |
(iternextfunc)0, |
|---|
| 334 |
pymidgard_qb_methods, |
|---|
| 335 |
(struct PyMemberDef*)0, |
|---|
| 336 |
(struct PyGetSetDef*)0, |
|---|
| 337 |
NULL, |
|---|
| 338 |
NULL, |
|---|
| 339 |
(descrgetfunc)0, |
|---|
| 340 |
(descrsetfunc)0, |
|---|
| 341 |
offsetof(PyGObject, inst_dict), |
|---|
| 342 |
(initproc)__query_builder_constructor, |
|---|
| 343 |
(allocfunc)0, |
|---|
| 344 |
(newfunc)0, |
|---|
| 345 |
(freefunc)0, |
|---|
| 346 |
(inquiry)0 |
|---|
| 347 |
}; |
|---|
| 348 |
|
|---|
| 349 |
void py_midgard_query_builder_register_class( |
|---|
| 350 |
PyObject *d, gpointer pygobject_type) |
|---|
| 351 |
{ |
|---|
| 352 |
pygobject_register_class(d, |
|---|
| 353 |
"query_builder", |
|---|
| 354 |
MIDGARD_TYPE_QUERY_BUILDER, |
|---|
| 355 |
&Pymidgard_qb_Type, |
|---|
| 356 |
Py_BuildValue("(O)", pygobject_type)); |
|---|
| 357 |
|
|---|
| 358 |
pyg_set_object_has_new_constructor(MIDGARD_TYPE_QUERY_BUILDER); |
|---|
| 359 |
} |
|---|
| 360 |
|
|---|
| 361 |
PyTypeObject py_midgard_query_builder_get_type_object(void) |
|---|
| 362 |
{ |
|---|
| 363 |
return Pymidgard_qb_Type; |
|---|
| 364 |
} |
|---|