root/trunk/midgard/apis/python/py_midgard_query_builder.c

Revision 16509, 9.2 kB (checked in by piotras, 6 months ago)

Collector's and QB's type objects moved to their c files and accessible with public function.
It should fix qb's and collector's common symbols problem.

Line 
1 /*
2  * Copyright (C) 2007 Piotr Pokora <piotrek.pokora@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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,                                 /* ob_size */
307     "query_builder",                   /* tp_name */
308     sizeof(PyGObject),          /* tp_basicsize */
309     0,                                 /* tp_itemsize */
310     /* methods */
311     (destructor)0,        /* tp_dealloc */
312     (printfunc)0,                      /* tp_print */
313     (getattrfunc)0,       /* tp_getattr */
314     (setattrfunc)0,       /* tp_setattr */
315     (cmpfunc)0,           /* tp_compare */
316     (reprfunc)0,             /* tp_repr */
317     (PyNumberMethods*)0,     /* tp_as_number */
318     (PySequenceMethods*)0, /* tp_as_sequence */
319     (PyMappingMethods*)0,   /* tp_as_mapping */
320     (hashfunc)0,             /* tp_hash */
321     (ternaryfunc)0,          /* tp_call */
322     (reprfunc)0,              /* tp_str */
323     (getattrofunc)0,     /* tp_getattro */
324     (setattrofunc)0,     /* tp_setattro */
325     (PyBufferProcs*)0,  /* tp_as_buffer */
326     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,                      /* tp_flags */
327     NULL,                        /* Documentation string */
328     (traverseproc)0,     /* tp_traverse */
329     (inquiry)0,             /* tp_clear */
330     (richcmpfunc)0,   /* tp_richcompare */
331     offsetof(PyGObject, weakreflist),             /* tp_weaklistoffset */
332     (getiterfunc)0,          /* tp_iter */
333     (iternextfunc)0,     /* tp_iternext */
334     pymidgard_qb_methods, /* tp_methods */
335     (struct PyMemberDef*)0,              /* tp_members */
336     (struct PyGetSetDef*)0,  /* tp_getset */
337     NULL,                              /* tp_base */
338     NULL,                              /* tp_dict */
339     (descrgetfunc)0,    /* tp_descr_get */
340     (descrsetfunc)0,    /* tp_descr_set */
341     offsetof(PyGObject, inst_dict),                 /* tp_dictoffset */
342     (initproc)__query_builder_constructor,             /* tp_init */
343     (allocfunc)0,           /* tp_alloc */
344     (newfunc)0,               /* tp_new */
345     (freefunc)0,             /* tp_free */
346     (inquiry)0              /* tp_is_gc */
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 }
Note: See TracBrowser for help on using the browser.