|
| 1 | +/* |
| 2 | + * the PLyAutonomous class |
| 3 | + * |
| 4 | + * src/pl/plpython/plpy_atxobject.c |
| 5 | + */ |
| 6 | + |
| 7 | +#include"postgres.h" |
| 8 | + |
| 9 | +#include"access/xact.h" |
| 10 | +#include"executor/spi.h" |
| 11 | +#include"utils/memutils.h" |
| 12 | + |
| 13 | +#include"plpython.h" |
| 14 | + |
| 15 | +#include"plpy_atxobject.h" |
| 16 | + |
| 17 | +#include"plpy_elog.h" |
| 18 | + |
| 19 | + |
| 20 | +staticvoidPLy_atx_dealloc(PyObject*subxact); |
| 21 | +staticPyObject*PLy_atx_enter(PyObject*self,PyObject*unused); |
| 22 | +staticPyObject*PLy_atx_exit(PyObject*self,PyObject*args); |
| 23 | + |
| 24 | +staticcharPLy_atx_doc[]= { |
| 25 | +"PostgreSQL autonomous transaction context manager" |
| 26 | +}; |
| 27 | + |
| 28 | +staticPyMethodDefPLy_atx_methods[]= { |
| 29 | +{"__enter__",PLy_atx_enter,METH_VARARGS,NULL}, |
| 30 | +{"__exit__",PLy_atx_exit,METH_VARARGS,NULL}, |
| 31 | +/* user-friendly names for Python <2.6 */ |
| 32 | +{"enter",PLy_atx_enter,METH_VARARGS,NULL}, |
| 33 | +{"exit",PLy_atx_exit,METH_VARARGS,NULL}, |
| 34 | +{NULL,NULL,0,NULL} |
| 35 | +}; |
| 36 | + |
| 37 | +staticPyTypeObjectPLy_AtxType= { |
| 38 | +PyVarObject_HEAD_INIT(NULL,0) |
| 39 | +"PLyAtx",/* tp_name */ |
| 40 | +sizeof(PLyAtxObject),/* tp_size */ |
| 41 | +0,/* tp_itemsize */ |
| 42 | + |
| 43 | +/* |
| 44 | + * methods |
| 45 | + */ |
| 46 | +PLy_atx_dealloc,/* tp_dealloc */ |
| 47 | +0,/* tp_print */ |
| 48 | +0,/* tp_getattr */ |
| 49 | +0,/* tp_setattr */ |
| 50 | +0,/* tp_compare */ |
| 51 | +0,/* tp_repr */ |
| 52 | +0,/* tp_as_number */ |
| 53 | +0,/* tp_as_sequence */ |
| 54 | +0,/* tp_as_mapping */ |
| 55 | +0,/* tp_hash */ |
| 56 | +0,/* tp_call */ |
| 57 | +0,/* tp_str */ |
| 58 | +0,/* tp_getattro */ |
| 59 | +0,/* tp_setattro */ |
| 60 | +0,/* tp_as_buffer */ |
| 61 | +Py_TPFLAGS_DEFAULT |Py_TPFLAGS_BASETYPE,/* tp_flags */ |
| 62 | +PLy_atx_doc,/* tp_doc */ |
| 63 | +0,/* tp_traverse */ |
| 64 | +0,/* tp_clear */ |
| 65 | +0,/* tp_richcompare */ |
| 66 | +0,/* tp_weaklistoffset */ |
| 67 | +0,/* tp_iter */ |
| 68 | +0,/* tp_iternext */ |
| 69 | +PLy_atx_methods,/* tp_tpmethods */ |
| 70 | +}; |
| 71 | + |
| 72 | + |
| 73 | +void |
| 74 | +PLy_atx_init_type(void) |
| 75 | +{ |
| 76 | +if (PyType_Ready(&PLy_AtxType)<0) |
| 77 | +elog(ERROR,"could not initialize PLy_AtxType"); |
| 78 | +} |
| 79 | + |
| 80 | +/* s = plpy.atx() */ |
| 81 | +PyObject* |
| 82 | +PLy_atx_new(PyObject*self,PyObject*unused) |
| 83 | +{ |
| 84 | +PLyAtxObject*ob; |
| 85 | + |
| 86 | +ob=PyObject_New(PLyAtxObject,&PLy_AtxType); |
| 87 | + |
| 88 | +if (ob==NULL) |
| 89 | +returnNULL; |
| 90 | + |
| 91 | +return (PyObject*)ob; |
| 92 | +} |
| 93 | + |
| 94 | +/* Python requires a dealloc function to be defined */ |
| 95 | +staticvoid |
| 96 | +PLy_atx_dealloc(PyObject*atx) |
| 97 | +{ |
| 98 | +} |
| 99 | + |
| 100 | +/* |
| 101 | + * atx.__enter__() or atx.enter() |
| 102 | + * |
| 103 | + * Start an atx. |
| 104 | + */ |
| 105 | +staticPyObject* |
| 106 | +PLy_atx_enter(PyObject*self,PyObject*unused) |
| 107 | +{ |
| 108 | +PLyAtxObject*atx= (PLyAtxObject*)self; |
| 109 | + |
| 110 | +SuspendTransaction(); |
| 111 | + |
| 112 | +Py_INCREF(self); |
| 113 | +returnself; |
| 114 | +} |
| 115 | + |
| 116 | +/* |
| 117 | + * atx.__exit__(exc_type, exc, tb) or atx.exit(exc_type, exc, tb) |
| 118 | + * |
| 119 | + * Exit an autonomous transaction. exc_type is an exception type, exc |
| 120 | + * is the exception object, tb is the traceback. If exc_type is None, |
| 121 | + * commit the atx, if not abort it. |
| 122 | + * |
| 123 | + * The method signature is chosen to allow atx objects to |
| 124 | + * be used as context managers as described in |
| 125 | + * <http://www.python.org/dev/peps/pep-0343/>. |
| 126 | + */ |
| 127 | +staticPyObject* |
| 128 | +PLy_atx_exit(PyObject*self,PyObject*args) |
| 129 | +{ |
| 130 | +PyObject*type; |
| 131 | +PyObject*value; |
| 132 | +PyObject*traceback; |
| 133 | +PLyAtxObject*atx= (PLyAtxObject*)self; |
| 134 | + |
| 135 | +if (!PyArg_ParseTuple(args,"OOO",&type,&value,&traceback)) |
| 136 | +returnNULL; |
| 137 | + |
| 138 | +if (type!=Py_None) |
| 139 | +{ |
| 140 | +AbortCurrentTransaction(); |
| 141 | +} |
| 142 | +else |
| 143 | +{ |
| 144 | +CommitTransactionCommand(); |
| 145 | +} |
| 146 | + |
| 147 | +Py_INCREF(Py_None); |
| 148 | +returnPy_None; |
| 149 | +} |