-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtriggerdata.c
More file actions
212 lines (185 loc) · 5.56 KB
/
triggerdata.c
File metadata and controls
212 lines (185 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Postgres.TriggerData
*/
#include <setjmp.h>
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <compile.h>
#include <structmember.h>
#include "postgres.h"
#include "commands/trigger.h"
#include "utils/rel.h"
#include "mb/pg_wchar.h"
#include "pypg/python.h"
#include "pypg/postgres.h"
#include "pypg/extension.h"
#include "pypg/pl.h"
#include "pypg/triggerdata.h"
#include "pypg/type/type.h"
static PyObj
trigdata_get_rel_nspname(PyObj td, void *arg)
{
PyObj tp;
tp = PyPgTriggerData_GetRelationType(td);
tp = PyPgTypeInfo(tp)->nspname_PyUnicode;
Py_INCREF(tp);
return(tp);
}
static PyObj
trigdata_get_typname(PyObj td, void *arg)
{
PyObj tp, rob;
tp = PyPgTriggerData_GetRelationType(td);
rob = PyPgTypeInfo(tp)->typname_PyUnicode;
Py_INCREF(rob);
return(rob);
}
static PyObj
trigdata_get_relation_id(PyObj td, void *arg)
{
PyObj tp, rob;
tp = PyPgTriggerData_GetRelationType(td);
rob = PyLong_FromOid(
PyPgTypeInfo(tp)->array.x_no.composite.typrelid);
return(rob);
}
static PyObj
trigdata_get_dbname(PyObj td, void *arg)
{
Py_INCREF(py_my_datname_str_ob);
return(py_my_datname_str_ob);
}
static PyGetSetDef PyPgTriggerData_GetSet[] = {
{"table_name", trigdata_get_typname,
NULL, PyDoc_STR("name of table object(event object)"), NULL},
{"table_schema", trigdata_get_rel_nspname,
NULL, PyDoc_STR("schema of table object(event object)"), NULL},
{"trigger_schema", trigdata_get_rel_nspname,
NULL, PyDoc_STR("schema of trigger object"), NULL},
{"relation_id", trigdata_get_relation_id, NULL, PyDoc_STR("Oid of the table(event object)"), NULL},
/* Constant */
{"table_catalog", trigdata_get_dbname, NULL, PyDoc_STR("catalog of table object(event object)"), NULL},
{"trigger_catalog", trigdata_get_dbname, NULL, PyDoc_STR("catalog of trigger object"), NULL},
{NULL,}
};
static PyMemberDef PyPgTriggerData_Members[] = {
{"type", T_OBJECT, offsetof(struct PyPgTriggerData, td_reltype), READONLY,
PyDoc_STR("relation type of table")},
{"trigger_name", T_OBJECT, offsetof(struct PyPgTriggerData, td_name), READONLY,
PyDoc_STR("name of trigger")},
{"manipulation", T_OBJECT, offsetof(struct PyPgTriggerData, td_manipulation), READONLY,
PyDoc_STR("event manipulation(INSERT or DELETE or UPDATE or TRUNCATE")},
{"orientation", T_OBJECT, offsetof(struct PyPgTriggerData, td_orientation), READONLY,
PyDoc_STR("action orientation(STATEMENT or ROW)")},
{"timing", T_OBJECT, offsetof(struct PyPgTriggerData, td_timing), READONLY,
PyDoc_STR("condition timing(BEFORE or AFTER)")},
{"args", T_OBJECT, offsetof(struct PyPgTriggerData, td_args), READONLY,
PyDoc_STR("args of trigger")},
{NULL}
};
static void
trigdata_dealloc(PyObj self)
{
PyPgTriggerData td = (PyPgTriggerData) self;
Py_XDECREF(td->td_reltype);
td->td_reltype = NULL;
Py_XDECREF(td->td_name);
td->td_name = NULL;
Py_XDECREF(td->td_args);
td->td_args = NULL;
/*
* The rest are borrowed from globals.
*/
}
const char PyPgTriggerData_Doc[] = "Postgres TriggerData interface";
PyTypeObject PyPgTriggerData_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"Postgres.TriggerData", /* tp_name */
sizeof(struct PyPgTriggerData), /* tp_basicsize */
0, /* tp_itemsize */
trigdata_dealloc, /* tp_dealloc */
NULL, /* tp_print */
NULL, /* tp_getattr */
NULL, /* tp_setattr */
NULL, /* tp_compare */
NULL, /* tp_repr */
NULL, /* tp_as_number */
NULL, /* tp_as_sequence */
NULL, /* tp_as_mapping */
NULL, /* tp_hash */
NULL, /* tp_call */
NULL, /* tp_str */
NULL, /* tp_getattro */
NULL, /* tp_setattro */
NULL, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
PyPgTriggerData_Doc, /* tp_doc */
NULL, /* tp_traverse */
NULL, /* tp_clear */
NULL, /* tp_richcompare */
0, /* tp_weaklistoffset */
NULL, /* tp_iter */
NULL, /* tp_iternext */
NULL, /* tp_methods */
PyPgTriggerData_Members, /* tp_members */
PyPgTriggerData_GetSet, /* tp_getset */
NULL, /* tp_base */
NULL, /* tp_dict */
NULL, /* tp_descr_get */
NULL, /* tp_descr_set */
0, /* tp_dictoffset */
NULL, /* tp_init */
NULL, /* tp_alloc */
NULL, /* tp_new */
};
PyObj
PyPgTriggerData_Initialize(PyObj self, TriggerData *td)
{
int i;
Trigger *tg = td->tg_trigger;
struct PyPgTriggerData *pytd = (PyPgTriggerData) self;
pytd->td_reltype = PyPgType_FromOid(td->tg_relation->rd_rel->reltype);
if (pytd->td_reltype == NULL)
goto fail;
/*
* Build out trigger arguments.
*/
pytd->td_args = PyTuple_New(tg->tgnargs);
for (i = 0; i < tg->tgnargs; ++i)
{
PyObj ob = PyUnicode_FromCString(tg->tgargs[i]);
if (ob == NULL)
goto fail;
PyTuple_SET_ITEM(pytd->td_args, i, ob);
}
pytd->td_name = PyUnicode_FromCString(tg->tgname);
if (pytd->td_name == NULL)
goto fail;
if (TRIGGER_FIRED_BEFORE(td->tg_event))
pytd->td_timing = BEFORE_str_ob;
else
pytd->td_timing = AFTER_str_ob;
if (TRIGGER_FIRED_FOR_ROW(td->tg_event))
pytd->td_orientation = ROW_str_ob;
else
pytd->td_orientation = STATEMENT_str_ob;
switch ((TriggerEvent) (td->tg_event) & TRIGGER_EVENT_OPMASK)
{
case TRIGGER_EVENT_INSERT:
pytd->td_manipulation = INSERT_str_ob;
break;
case TRIGGER_EVENT_DELETE:
pytd->td_manipulation = DELETE_str_ob;
break;
case TRIGGER_EVENT_UPDATE:
pytd->td_manipulation = UPDATE_str_ob;
break;
case TRIGGER_EVENT_TRUNCATE:
pytd->td_manipulation = TRUNCATE_str_ob;
break;
}
return(self);
fail:
Py_DECREF(self);
return(NULL);
}