-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmgclientmodule.c
308 lines (287 loc) · 9.66 KB
/
mgclientmodule.c
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright (c) 2016-2020 Memgraph Ltd. [https://memgraph.com]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#define APILEVEL "2.0"
#define THREADSAFETY 1
// For simplicity, here we deviate from the DB-API spec.
#define PARAMSTYLE "cypher"
#include <mgclient.h>
#include "column.h"
#include "connection.h"
#include "cursor.h"
#include "types.h"
PyObject *Warning;
PyObject *Error;
PyObject *InterfaceError;
PyObject *DatabaseError;
PyObject *DataError;
PyObject *OperationalError;
PyObject *IntegrityError;
PyObject *InternalError;
PyObject *ProgrammingError;
PyObject *NotSupportedError;
PyDoc_STRVAR(Warning_doc, "Exception raised for important warnings.");
PyDoc_STRVAR(Error_doc, "Base class of all other error exceptions.");
PyDoc_STRVAR(
InterfaceError_doc,
"Exception raised for errors related to the database interface rather than "
"the database itself.");
PyDoc_STRVAR(DatabaseError_doc,
"Exception raised for errors related to the database.");
PyDoc_STRVAR(
DataError_doc,
"Exception raised for errors that are due to problems with the processed "
"data.");
PyDoc_STRVAR(
OperationalError_doc,
"Exception raised for errors related to the database's operation, not "
"necessarily under the control of the programmer (e.g. unexpected "
"disconnect, failed allocation).");
PyDoc_STRVAR(
IntegrityError_doc,
"Exception raised when the relational integrity of the database is "
"affected.");
PyDoc_STRVAR(
InternalError_doc,
"Exception raised when the database encounters an internal error.");
PyDoc_STRVAR(
ProgrammingError_doc,
"Exception raised for programming errors (e.g. syntax error, invalid "
"parameters)");
PyDoc_STRVAR(
NotSupportedError_doc,
"Exception raised in a case a method or database API was used which is not "
"supported by the database.");
static struct {
const char *name;
PyObject **exc;
PyObject **base;
const char *docstring;
} module_exceptions[] = {
{"mgclient.Warning", &Warning, &PyExc_Exception, Warning_doc},
{"mgclient.Error", &Error, &PyExc_Exception, Error_doc},
{"mgclient.InterfaceError", &InterfaceError, &Error, InterfaceError_doc},
{"mgclient.DatabaseError", &DatabaseError, &Error, DatabaseError_doc},
{"mgclient.DataError", &DataError, &DatabaseError, DataError_doc},
{"mgclient.OperationalError", &OperationalError, &DatabaseError,
OperationalError_doc},
{"mgclient.IntegrityError", &IntegrityError, &DatabaseError,
IntegrityError_doc},
{"mgclient.InternalError", &InternalError, &DatabaseError,
InternalError_doc},
{"mgclient.ProgrammingError", &ProgrammingError, &DatabaseError,
ProgrammingError_doc},
{"mgclient.NotSupportedError", &NotSupportedError, &DatabaseError,
NotSupportedError_doc},
{NULL, NULL, NULL, NULL}};
int add_module_exceptions(PyObject *module) {
for (size_t i = 0; module_exceptions[i].name; ++i) {
*module_exceptions[i].exc = NULL;
}
for (size_t i = 0; module_exceptions[i].name; ++i) {
PyObject *exc = PyErr_NewExceptionWithDoc(module_exceptions[i].name,
module_exceptions[i].docstring,
*module_exceptions[i].base, NULL);
if (!exc) {
goto cleanup;
}
*module_exceptions[i].exc = exc;
}
for (size_t i = 0; module_exceptions[i].name; ++i) {
const char *name = strrchr(module_exceptions[i].name, '.');
name = name ? name + 1 : module_exceptions[i].name;
if (PyModule_AddObject(module, name, *module_exceptions[i].exc) < 0) {
goto cleanup;
}
}
return 0;
cleanup:
for (size_t i = 0; module_exceptions[i].name; ++i) {
Py_XDECREF(*module_exceptions[i].exc);
}
return -1;
}
int add_module_constants(PyObject *module) {
if (PyModule_AddStringConstant(module, "apilevel", APILEVEL) < 0) {
return -1;
}
if (PyModule_AddIntConstant(module, "threadsafety", THREADSAFETY) < 0) {
return -1;
}
if (PyModule_AddStringConstant(module, "paramstyle", PARAMSTYLE) < 0) {
return -1;
}
if (PyModule_AddIntMacro(module, MG_SSLMODE_REQUIRE) < 0) {
return -1;
}
if (PyModule_AddIntMacro(module, MG_SSLMODE_DISABLE) < 0) {
return -1;
}
// Connection status constants.
if (PyModule_AddIntMacro(module, CONN_STATUS_READY) < 0) {
return -1;
}
if (PyModule_AddIntMacro(module, CONN_STATUS_BAD) < 0) {
return -1;
}
if (PyModule_AddIntMacro(module, CONN_STATUS_CLOSED) < 0) {
return -1;
}
if (PyModule_AddIntMacro(module, CONN_STATUS_IN_TRANSACTION) < 0) {
return -1;
}
if (PyModule_AddIntMacro(module, CONN_STATUS_EXECUTING) < 0) {
return -1;
}
return 0;
}
static struct {
char *name;
PyTypeObject *type;
} type_table[] = {{"Connection", &ConnectionType},
{"Cursor", &CursorType},
{"Column", &ColumnType},
{"Node", &NodeType},
{"Relationship", &RelationshipType},
{"Path", &PathType},
{NULL, NULL}};
static int add_module_types(PyObject *module) {
for (size_t i = 0; type_table[i].name; ++i) {
if (PyType_Ready(type_table[i].type) < 0) {
return -1;
}
if (PyModule_AddObject(module, type_table[i].name,
(PyObject *)type_table[i].type) < 0) {
return -1;
}
}
return 0;
}
static PyObject *mgclient_connect(PyObject *self, PyObject *args,
PyObject *kwargs) {
// Unused parameter.
(void)self;
return PyObject_Call((PyObject *)&ConnectionType, args, kwargs);
}
// clang-format off
PyDoc_STRVAR(mgclient_connect_doc,
"connect(host=None, address=None, port=None, username=None, password=None,\n\
client_name=None, sslmode=mgclient.MG_SSLMODE_DISABLE,\n\
sslcert=None, sslkey=None, trust_callback=None, lazy=False, database=None)\n\
--\n\
\n\
Makes a new connection to the database server and returns a\n\
:class:`Connection` object.\n\
\n\
Currently recognized parameters are:\n\
\n\
* :obj:`host`\n\
\n\
DNS resolvable name of host to connect to. Exactly one of host and\n\
address parameters must be specified.\n\
\n\
* :obj:`address`\n\
\n\
Numeric IP address of host to connect to. This should be in the\n\
standard IPv4 address format. You can also use IPv6 if your machine\n\
supports it. Exactly one of host and address parameters must be\n\
specified.\n\
\n\
* :obj:`port`\n\
\n\
Port number to connect to at the server host.\n\
\n\
* :obj:`username`\n\
\n\
Username to connect as.\n\
\n\
* :obj:`password`\n\
\n\
Password to be used if the server demands password authentication.\n\
\n\
* :obj:`client_name`\n\
\n\
Alternate name and version of the client to send to server. Default is\n\
set by the underlying mgclient library.\n\
\n\
* :obj:`sslmode`\n\
\n\
This option determines whether a secure connection will be negotiated\n\
with the server. There are 2 possible values:\n\
\n\
* :const:`mgclient.MG_SSLMODE_DISABLE`\n\
\n\
Only try a non-SSL connection (default).\n\
\n\
* :const:`mgclient.MG_SSLMODE_REQUIRE`\n\
\n\
Only try an SSL connection.\n\
\n\
* :obj:`sslcert`\n\
\n\
This parameter specifies the file name of the client SSL certificate.\n\
It is ignored in case an SSL connection is not made.\n\
\n\
* :obj:`sslkey`\n\
\n\
This parameter specifies the location of the secret key used for the\n\
client certificate. This parameter is ignored in case an SSL connection\n\
is not made.\n\
\n\
* :obj:`trust_callback`\n\
\n\
A callable taking four arguments.\n\
\n\
After performing the SSL handshake, :meth:`connect` will call this\n\
callable providing the hostname, IP address, public key type and\n\
fingerprint. If the function returns ``False`` SSL connection will\n\
immediately be terminated.\n\
\n\
This can be used to implement TOFU (trust on first use) mechanism.\n\
\n\
* :obj:`lazy`\n\
\n\
If this is set to ``True``, a lazy connection is made. Default is ``False``.\n\
\n\
* :obj:`database`\n\
\n\
If set, all queries executed will target the defined database. Default is ``None``.");
// clang-format on
static PyMethodDef mgclient_methods[] = {
{"connect", (PyCFunction)mgclient_connect, METH_VARARGS | METH_KEYWORDS,
mgclient_connect_doc},
{NULL, NULL, 0, NULL}};
static struct PyModuleDef mgclient_module = {.m_base = PyModuleDef_HEAD_INIT,
.m_name = "mgclient",
.m_doc = NULL,
.m_size = -1,
.m_methods = mgclient_methods,
.m_slots = NULL};
PyMODINIT_FUNC PyInit_mgclient(void) {
PyObject *m;
if (!(m = PyModule_Create(&mgclient_module))) {
return NULL;
}
if (add_module_exceptions(m) < 0) {
return NULL;
}
if (add_module_constants(m) < 0) {
return NULL;
}
if (add_module_types(m) < 0) {
return NULL;
}
return m;
}