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

Revision 23608, 5.3 kB (checked in by piotras, 5 months ago)

Set BASETYPE flags. Fix #1391

Line 
1 /*
2  * Copyright (C) 2009 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
25 #include "py_midgard.h"
26
27 PyTypeObject G_GNUC_INTERNAL Pymidgard_transaction_Type;
28
29 #define TRANSACTION_DEBUG(__name) \
30         CHECK_MGD; \
31         CLASS_METHOD_DEBUG(Pymidgard_transaction_Type.tp_name, __name);
32
33 #define __GET_TRANSACTION       \
34         MidgardTransaction *trns = MIDGARD_TRANSACTION(self->obj);      \
35         g_assert(trns != NULL);
36
37 static int
38 __transaction_constructor(PyGObject *self, PyObject *args, PyObject *kwargs)
39 {
40         TRANSACTION_DEBUG("__init__");
41
42         MidgardConnection *mgd =
43                 _py_midgard_connection_singleton_get();
44
45         MidgardTransaction *trns = midgard_transaction_new(mgd);
46        
47         if(!trns)
48                 return -1;
49        
50         self->obj = G_OBJECT(trns);
51        
52         return 0;
53 }
54
55 static PyObject *
56 pymidgard_transaction_begin(PyGObject *self, PyObject *args)
57 {
58         TRANSACTION_DEBUG("begin");
59
60         __GET_TRANSACTION;
61        
62         if (midgard_transaction_begin(trns))
63                 Py_RETURN_TRUE;
64
65         Py_RETURN_FALSE;
66 }
67
68 static PyObject *
69 pymidgard_transaction_commit(PyGObject *self, PyObject *args)
70 {
71         TRANSACTION_DEBUG("commit");
72
73         __GET_TRANSACTION;
74        
75         if (midgard_transaction_commit(trns))
76                 Py_RETURN_TRUE;
77
78         Py_RETURN_FALSE;
79 }
80
81 static PyObject *
82 pymidgard_transaction_rollback(PyGObject *self, PyObject *args)
83 {
84         TRANSACTION_DEBUG("rollback");
85
86         __GET_TRANSACTION;
87        
88         if (midgard_transaction_rollback(trns))
89                 Py_RETURN_TRUE;
90
91         Py_RETURN_FALSE;
92 }
93
94 static PyObject *
95 pymidgard_transaction_get_status(PyGObject *self, PyObject *args)
96 {
97         TRANSACTION_DEBUG("get_status");
98
99         __GET_TRANSACTION;
100        
101         if (midgard_transaction_get_status(trns))
102                 Py_RETURN_TRUE;
103
104         Py_RETURN_FALSE;
105 }
106
107 static PyObject *
108 pymidgard_transaction_get_name(PyGObject *self, PyObject *args)
109 {
110         TRANSACTION_DEBUG("get_name");
111        
112         __GET_TRANSACTION;
113         const gchar *msg = midgard_transaction_get_name(trns);
114
115         return Py_BuildValue("s", msg);
116 }
117
118 static PyMethodDef pymidgard_transaction_methods[] = {
119         { "begin", (PyCFunction)pymidgard_transaction_begin, METH_NOARGS},
120         { "commit", (PyCFunction)pymidgard_transaction_commit, METH_NOARGS},
121         { "rollback", (PyCFunction)pymidgard_transaction_rollback, METH_NOARGS},
122         { "get_status", (PyCFunction)pymidgard_transaction_get_status, METH_NOARGS},
123         { "get_name", (PyCFunction)pymidgard_transaction_get_name, METH_NOARGS},
124         { NULL, NULL, 0 }
125 };
126
127 PyTypeObject G_GNUC_INTERNAL Pymidgard_transaction_Type = {
128     PyObject_HEAD_INIT(NULL)
129     0,                                 /* ob_size */
130     "transaction",                   /* tp_name */
131     sizeof(PyGObject),          /* tp_basicsize */
132     0,                                 /* tp_itemsize */
133     /* methods */
134     (destructor)_py_midgard_gobject_destructor,        /* tp_dealloc */
135     (printfunc)0,                      /* tp_print */
136     (getattrfunc)0,       /* tp_getattr */
137     (setattrfunc)0,       /* tp_setattr */
138     (cmpfunc)0,           /* tp_compare */
139     (reprfunc)0,             /* tp_repr */
140     (PyNumberMethods*)0,     /* tp_as_number */
141     (PySequenceMethods*)0, /* tp_as_sequence */
142     (PyMappingMethods*)0,   /* tp_as_mapping */
143     (hashfunc)0,             /* tp_hash */
144     (ternaryfunc)0,          /* tp_call */
145     (reprfunc)0,              /* tp_str */
146     (getattrofunc)0,     /* tp_getattro */
147     (setattrofunc)0,     /* tp_setattro */
148     (PyBufferProcs*)0,  /* tp_as_buffer */
149     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,                      /* tp_flags */
150     NULL,                        /* Documentation string */
151     (traverseproc)0,     /* tp_traverse */
152     (inquiry)0,             /* tp_clear */
153     (richcmpfunc)0,   /* tp_richcompare */
154     offsetof(PyGObject, weakreflist),             /* tp_weaklistoffset */
155     (getiterfunc)0,          /* tp_iter */
156     (iternextfunc)0,     /* tp_iternext */
157     pymidgard_transaction_methods, /* tp_methods */
158     (struct PyMemberDef*)0,              /* tp_members */
159     (struct PyGetSetDef*)0,  /* tp_getset */
160     NULL,                              /* tp_base */
161     NULL,                              /* tp_dict */
162     (descrgetfunc)0,    /* tp_descr_get */
163     (descrsetfunc)0,    /* tp_descr_set */
164     offsetof(PyGObject, inst_dict),                 /* tp_dictoffset */
165     (initproc)__transaction_constructor,             /* tp_init */
166     (allocfunc)0,           /* tp_alloc */
167     (newfunc)0,               /* tp_new */
168     (freefunc)0,             /* tp_free */
169     (inquiry)0              /* tp_is_gc */
170 };
171
172 void py_midgard_transaction_register_class(
173                 PyObject *d, gpointer pygobject_type)
174 {
175         pygobject_register_class(d,
176                         "transaction",
177                         MIDGARD_TYPE_TRANSACTION,
178                         &Pymidgard_transaction_Type,
179                         Py_BuildValue("(O)", pygobject_type));
180 }
Note: See TracBrowser for help on using the browser.