vsql-python 1.0.0__cp314-cp314-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
vsql/_libvsql.c ADDED
@@ -0,0 +1,610 @@
1
+ // _libvsql.c is the vsql._libvsql extension module, the only code that calls
2
+ // libvsql (see libvsql.h), which is compiled into the module from the
3
+ // amalgamation by setup.py. Each function is a thin wrapper around a libvsql
4
+ // routine, with handles as ints. Everything else (such as converting values)
5
+ // is in Python.
6
+ //
7
+ // libvsql can be called from any thread, so the GIL is released while a
8
+ // routine runs.
9
+ //
10
+ // When a routine fails, the exception is made by the function given to
11
+ // set_error_factory, from the SQLSTATE and message of the first diagnostic
12
+ // record.
13
+
14
+ #define PY_SSIZE_T_CLEAN
15
+ #include <Python.h>
16
+
17
+ #include <stdlib.h>
18
+ #include <string.h>
19
+
20
+ #include "libvsql.h"
21
+
22
+ // Py_NewRef is new in Python 3.10.
23
+ #if PY_VERSION_HEX < 0x030A0000
24
+ static inline PyObject *Py_NewRef(PyObject *o) {
25
+ Py_INCREF(o);
26
+ return o;
27
+ }
28
+ #endif
29
+
30
+ static PyObject *error_factory = NULL;
31
+
32
+ // raise_error raises the exception for an SQLSTATE and message.
33
+ static void raise_error(const char *sqlstate, const char *message, Py_ssize_t length) {
34
+ if (error_factory == NULL) {
35
+ PyErr_Format(PyExc_RuntimeError, "vsql: %.*s (SQLSTATE %s)", (int)length, message,
36
+ sqlstate);
37
+ return;
38
+ }
39
+
40
+ PyObject *err = PyObject_CallFunction(error_factory, "ss#", sqlstate, message, length);
41
+ if (err != NULL) {
42
+ PyErr_SetObject((PyObject *)Py_TYPE(err), err);
43
+ Py_DECREF(err);
44
+ }
45
+ }
46
+
47
+ // check returns 0 for a return code that isn't an error (SQL_NO_DATA isn't),
48
+ // otherwise it raises the error from the diagnostics of the handle and returns
49
+ // -1.
50
+ static int check(SQLRETURN rc, SQLSMALLINT handle_type, SQLINTEGER handle) {
51
+ switch (rc) {
52
+ case SQL_SUCCESS:
53
+ case SQL_SUCCESS_WITH_INFO:
54
+ case SQL_NO_DATA:
55
+ return 0;
56
+ case SQL_INVALID_HANDLE:
57
+ raise_error("HY000", "invalid handle", -1);
58
+ return -1;
59
+ }
60
+
61
+ SQLCHAR sqlstate[6];
62
+ SQLSMALLINT size = 512, length = 0;
63
+ SQLCHAR *message = malloc(size);
64
+ for (;;) {
65
+ if (message == NULL) {
66
+ PyErr_NoMemory();
67
+ return -1;
68
+ }
69
+
70
+ Py_BEGIN_ALLOW_THREADS
71
+ rc = SQLGetDiagRec(handle_type, handle, 1, sqlstate, NULL, message, size, &length);
72
+ Py_END_ALLOW_THREADS
73
+ if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
74
+ free(message);
75
+ raise_error("HY000", "unknown error", -1);
76
+ return -1;
77
+ }
78
+
79
+ // The message was truncated.
80
+ if (length >= size) {
81
+ size = length + 1;
82
+ free(message);
83
+ message = malloc(size);
84
+ continue;
85
+ }
86
+
87
+ raise_error((const char *)sqlstate, (const char *)message, length);
88
+ free(message);
89
+ return -1;
90
+ }
91
+ }
92
+
93
+ // CALL runs a libvsql routine without the GIL, and returns NULL from the
94
+ // function if it fails.
95
+ #define CALL(handle_type, handle, routine) \
96
+ do { \
97
+ SQLRETURN rc_; \
98
+ Py_BEGIN_ALLOW_THREADS \
99
+ rc_ = (routine); \
100
+ Py_END_ALLOW_THREADS \
101
+ if (check(rc_, (handle_type), (handle)) < 0) { \
102
+ return NULL; \
103
+ } \
104
+ } while (0)
105
+
106
+ static PyObject *set_error_factory(PyObject *self, PyObject *factory) {
107
+ Py_XSETREF(error_factory, Py_NewRef(factory));
108
+ Py_RETURN_NONE;
109
+ }
110
+
111
+ // alloc_handle(handle_type, input_handle) -> handle
112
+ static PyObject *alloc_handle(PyObject *self, PyObject *args) {
113
+ int handle_type;
114
+ SQLINTEGER input, output = 0;
115
+ if (!PyArg_ParseTuple(args, "ii", &handle_type, &input)) {
116
+ return NULL;
117
+ }
118
+
119
+ SQLRETURN rc;
120
+ Py_BEGIN_ALLOW_THREADS
121
+ rc = SQLAllocHandle(handle_type, input, &output);
122
+ Py_END_ALLOW_THREADS
123
+
124
+ // An environment doesn't have an input handle with diagnostics.
125
+ if (handle_type == SQL_HANDLE_ENV) {
126
+ if (rc != SQL_SUCCESS) {
127
+ raise_error("HY001", "can't allocate an environment", -1);
128
+ return NULL;
129
+ }
130
+ } else if (check(rc, handle_type == SQL_HANDLE_DBC ? SQL_HANDLE_ENV : SQL_HANDLE_DBC,
131
+ input) < 0) {
132
+ return NULL;
133
+ }
134
+
135
+ return PyLong_FromLong(output);
136
+ }
137
+
138
+ // free_handle(handle_type, handle)
139
+ static PyObject *free_handle(PyObject *self, PyObject *args) {
140
+ int handle_type;
141
+ SQLINTEGER handle;
142
+ if (!PyArg_ParseTuple(args, "ii", &handle_type, &handle)) {
143
+ return NULL;
144
+ }
145
+
146
+ CALL(handle_type, handle, SQLFreeHandle(handle_type, handle));
147
+ Py_RETURN_NONE;
148
+ }
149
+
150
+ // connect(dbc, name)
151
+ static PyObject *connect(PyObject *self, PyObject *args) {
152
+ SQLINTEGER dbc;
153
+ const char *name;
154
+ Py_ssize_t length;
155
+ if (!PyArg_ParseTuple(args, "is#", &dbc, &name, &length)) {
156
+ return NULL;
157
+ }
158
+
159
+ CALL(SQL_HANDLE_DBC, dbc,
160
+ SQLConnect(dbc, (SQLCHAR *)name, (SQLSMALLINT)length, NULL, 0, NULL, 0));
161
+ Py_RETURN_NONE;
162
+ }
163
+
164
+ // disconnect(dbc)
165
+ static PyObject *disconnect(PyObject *self, PyObject *args) {
166
+ SQLINTEGER dbc;
167
+ if (!PyArg_ParseTuple(args, "i", &dbc)) {
168
+ return NULL;
169
+ }
170
+
171
+ CALL(SQL_HANDLE_DBC, dbc, SQLDisconnect(dbc));
172
+ Py_RETURN_NONE;
173
+ }
174
+
175
+ // set_autocommit(dbc, on)
176
+ static PyObject *set_autocommit(PyObject *self, PyObject *args) {
177
+ SQLINTEGER dbc;
178
+ int on;
179
+ if (!PyArg_ParseTuple(args, "ip", &dbc, &on)) {
180
+ return NULL;
181
+ }
182
+
183
+ // libvsql reads the value after SQLSetConnectAttr returns, so it can't be
184
+ // on the stack.
185
+ static SQLINTEGER autocommit_on = SQL_AUTOCOMMIT_ON, autocommit_off = SQL_AUTOCOMMIT_OFF;
186
+ CALL(SQL_HANDLE_DBC, dbc,
187
+ SQLSetConnectAttr(dbc, SQL_ATTR_AUTOCOMMIT,
188
+ on ? (SQLPOINTER)&autocommit_on : (SQLPOINTER)&autocommit_off, 0));
189
+ Py_RETURN_NONE;
190
+ }
191
+
192
+ // start_tran(dbc, access_mode, isolation_level)
193
+ static PyObject *start_tran(PyObject *self, PyObject *args) {
194
+ SQLINTEGER dbc, access_mode, isolation_level;
195
+ if (!PyArg_ParseTuple(args, "iii", &dbc, &access_mode, &isolation_level)) {
196
+ return NULL;
197
+ }
198
+
199
+ CALL(SQL_HANDLE_DBC, dbc, SQLStartTran(SQL_HANDLE_DBC, dbc, access_mode, isolation_level));
200
+ Py_RETURN_NONE;
201
+ }
202
+
203
+ // end_tran(dbc, commit)
204
+ static PyObject *end_tran(PyObject *self, PyObject *args) {
205
+ SQLINTEGER dbc;
206
+ int commit;
207
+ if (!PyArg_ParseTuple(args, "ip", &dbc, &commit)) {
208
+ return NULL;
209
+ }
210
+
211
+ CALL(SQL_HANDLE_DBC, dbc, SQLEndTran(SQL_HANDLE_DBC, dbc, commit ? SQL_COMMIT : SQL_ROLLBACK));
212
+ Py_RETURN_NONE;
213
+ }
214
+
215
+ // exec_direct(stmt, query)
216
+ static PyObject *exec_direct(PyObject *self, PyObject *args) {
217
+ SQLINTEGER stmt;
218
+ const char *query;
219
+ Py_ssize_t length;
220
+ if (!PyArg_ParseTuple(args, "is#", &stmt, &query, &length)) {
221
+ return NULL;
222
+ }
223
+
224
+ CALL(SQL_HANDLE_STMT, stmt, SQLExecDirect(stmt, (SQLCHAR *)query, (SQLINTEGER)length));
225
+ Py_RETURN_NONE;
226
+ }
227
+
228
+ // prepare(stmt, query)
229
+ static PyObject *prepare(PyObject *self, PyObject *args) {
230
+ SQLINTEGER stmt;
231
+ const char *query;
232
+ Py_ssize_t length;
233
+ if (!PyArg_ParseTuple(args, "is#", &stmt, &query, &length)) {
234
+ return NULL;
235
+ }
236
+
237
+ CALL(SQL_HANDLE_STMT, stmt, SQLPrepare(stmt, (SQLCHAR *)query, (SQLINTEGER)length));
238
+ Py_RETURN_NONE;
239
+ }
240
+
241
+ // A parameter's value, which must stay allocated until SQLExecute has read it.
242
+ typedef union {
243
+ SQLBIGINT i;
244
+ SQLDOUBLE d;
245
+ } number;
246
+
247
+ // execute(stmt, params) executes the prepared statement with the parameters,
248
+ // which are bound for this execution only. Each parameter is a tuple of
249
+ // (value_type, parameter_type, column_size, decimal_digits, value), where the
250
+ // value is None, an int (SQL_BIGINT), a float (SQL_DOUBLE) or UTF-8 bytes
251
+ // (SQL_CHAR).
252
+ static PyObject *execute(PyObject *self, PyObject *args) {
253
+ SQLINTEGER stmt;
254
+ PyObject *params;
255
+ if (!PyArg_ParseTuple(args, "iO!", &stmt, &PyTuple_Type, &params)) {
256
+ return NULL;
257
+ }
258
+
259
+ Py_ssize_t n = PyTuple_GET_SIZE(params);
260
+ number *numbers = PyMem_Calloc(n ? n : 1, sizeof(number));
261
+ SQLINTEGER *inds = PyMem_Calloc(n ? n : 1, sizeof(SQLINTEGER));
262
+ if (numbers == NULL || inds == NULL) {
263
+ PyMem_Free(numbers);
264
+ PyMem_Free(inds);
265
+ return PyErr_NoMemory();
266
+ }
267
+
268
+ PyObject *result = NULL;
269
+ SQLRETURN rc;
270
+ for (Py_ssize_t i = 0; i < n; i++) {
271
+ int value_type, parameter_type, decimal_digits;
272
+ SQLINTEGER column_size;
273
+ PyObject *value;
274
+ if (!PyArg_ParseTuple(PyTuple_GET_ITEM(params, i), "iiiiO", &value_type, &parameter_type,
275
+ &column_size, &decimal_digits, &value)) {
276
+ goto done;
277
+ }
278
+
279
+ SQLPOINTER pointer = NULL;
280
+ SQLINTEGER buffer_length = 0;
281
+ if (value == Py_None) {
282
+ inds[i] = SQL_NULL_DATA;
283
+ } else if (value_type == SQL_BIGINT) {
284
+ numbers[i].i = PyLong_AsLongLong(value);
285
+ if (numbers[i].i == -1 && PyErr_Occurred()) {
286
+ goto done;
287
+ }
288
+ pointer = &numbers[i].i;
289
+ buffer_length = sizeof(SQLBIGINT);
290
+ } else if (value_type == SQL_DOUBLE) {
291
+ numbers[i].d = PyFloat_AsDouble(value);
292
+ if (numbers[i].d == -1.0 && PyErr_Occurred()) {
293
+ goto done;
294
+ }
295
+ pointer = &numbers[i].d;
296
+ buffer_length = sizeof(SQLDOUBLE);
297
+ } else {
298
+ // The bytes are kept alive by params until SQLExecute returns.
299
+ char *s;
300
+ Py_ssize_t length;
301
+ if (PyBytes_AsStringAndSize(value, &s, &length) < 0) {
302
+ goto done;
303
+ }
304
+ pointer = s;
305
+ buffer_length = (SQLINTEGER)length + 1;
306
+ inds[i] = (SQLINTEGER)length;
307
+ }
308
+
309
+ Py_BEGIN_ALLOW_THREADS
310
+ rc = SQLBindParameter(stmt, (SQLSMALLINT)(i + 1), SQL_PARAM_MODE_IN, (SQLSMALLINT)value_type,
311
+ (SQLSMALLINT)parameter_type, column_size, (SQLSMALLINT)decimal_digits,
312
+ pointer, buffer_length, &inds[i]);
313
+ Py_END_ALLOW_THREADS
314
+ if (check(rc, SQL_HANDLE_STMT, stmt) < 0) {
315
+ goto done;
316
+ }
317
+ }
318
+
319
+ Py_BEGIN_ALLOW_THREADS
320
+ rc = SQLExecute(stmt);
321
+ Py_END_ALLOW_THREADS
322
+ if (check(rc, SQL_HANDLE_STMT, stmt) == 0) {
323
+ result = Py_NewRef(Py_None);
324
+ }
325
+
326
+ done:
327
+ // The values are freed, so they can't stay bound for the next execution.
328
+ if (n > 0) {
329
+ Py_BEGIN_ALLOW_THREADS
330
+ SQLFreeStmt(stmt, SQL_RESET_PARAMS);
331
+ Py_END_ALLOW_THREADS
332
+ }
333
+ PyMem_Free(numbers);
334
+ PyMem_Free(inds);
335
+
336
+ return result;
337
+ }
338
+
339
+ // row_count(stmt) -> int
340
+ static PyObject *row_count(PyObject *self, PyObject *args) {
341
+ SQLINTEGER stmt, n = 0;
342
+ if (!PyArg_ParseTuple(args, "i", &stmt)) {
343
+ return NULL;
344
+ }
345
+
346
+ CALL(SQL_HANDLE_STMT, stmt, SQLRowCount(stmt, &n));
347
+ return PyLong_FromLong(n);
348
+ }
349
+
350
+ // num_result_cols(stmt) -> int
351
+ static PyObject *num_result_cols(PyObject *self, PyObject *args) {
352
+ SQLINTEGER stmt;
353
+ SQLSMALLINT n = 0;
354
+ if (!PyArg_ParseTuple(args, "i", &stmt)) {
355
+ return NULL;
356
+ }
357
+
358
+ CALL(SQL_HANDLE_STMT, stmt, SQLNumResultCols(stmt, &n));
359
+ return PyLong_FromLong(n);
360
+ }
361
+
362
+ // describe_col(stmt, n) -> (name, data_type, column_size, decimal_digits,
363
+ // nullable)
364
+ static PyObject *describe_col(PyObject *self, PyObject *args) {
365
+ SQLINTEGER stmt;
366
+ int n;
367
+ if (!PyArg_ParseTuple(args, "ii", &stmt, &n)) {
368
+ return NULL;
369
+ }
370
+
371
+ SQLSMALLINT size = 129, length = 0, data_type = 0, decimal_digits = 0, nullable = 0;
372
+ SQLINTEGER column_size = 0;
373
+ for (;;) {
374
+ SQLCHAR *name = malloc(size);
375
+ if (name == NULL) {
376
+ return PyErr_NoMemory();
377
+ }
378
+
379
+ SQLRETURN rc;
380
+ Py_BEGIN_ALLOW_THREADS
381
+ rc = SQLDescribeCol(stmt, (SQLSMALLINT)n, name, size, &length, &data_type, &column_size,
382
+ &decimal_digits, &nullable);
383
+ Py_END_ALLOW_THREADS
384
+ if (check(rc, SQL_HANDLE_STMT, stmt) < 0) {
385
+ free(name);
386
+ return NULL;
387
+ }
388
+
389
+ // The name was truncated.
390
+ if (length >= size) {
391
+ size = length + 1;
392
+ free(name);
393
+ continue;
394
+ }
395
+
396
+ PyObject *result = Py_BuildValue("(s#iiii)", name, (Py_ssize_t)length, data_type,
397
+ column_size, decimal_digits, nullable);
398
+ free(name);
399
+ return result;
400
+ }
401
+ }
402
+
403
+ // close_cursor(stmt)
404
+ static PyObject *close_cursor(PyObject *self, PyObject *args) {
405
+ SQLINTEGER stmt;
406
+ if (!PyArg_ParseTuple(args, "i", &stmt)) {
407
+ return NULL;
408
+ }
409
+
410
+ CALL(SQL_HANDLE_STMT, stmt, SQLCloseCursor(stmt));
411
+ Py_RETURN_NONE;
412
+ }
413
+
414
+ // get_string reads a column as SQL_CHAR. A string that doesn't fit in the
415
+ // buffer is read in pieces, into a buffer that's the size of the rest of it.
416
+ static PyObject *get_string(SQLINTEGER stmt, SQLSMALLINT n) {
417
+ char small[256];
418
+ char *buf = small, *large = NULL;
419
+ SQLINTEGER size = sizeof(small), ind = 0;
420
+ size_t length = 0;
421
+ for (;;) {
422
+ SQLRETURN rc;
423
+ Py_BEGIN_ALLOW_THREADS
424
+ rc = SQLGetData(stmt, n, SQL_CHAR, buf + length, size - (SQLINTEGER)length, &ind);
425
+ Py_END_ALLOW_THREADS
426
+ if (check(rc, SQL_HANDLE_STMT, stmt) < 0) {
427
+ PyMem_Free(large);
428
+ return NULL;
429
+ }
430
+ if (ind == SQL_NULL_DATA) {
431
+ PyMem_Free(large);
432
+ Py_RETURN_NONE;
433
+ }
434
+
435
+ // The rest of the string (ind) didn't fit, so the buffer has a null
436
+ // terminated piece of it.
437
+ if (rc == SQL_SUCCESS_WITH_INFO && ind >= size - (SQLINTEGER)length) {
438
+ size_t piece = strlen(buf + length);
439
+ size_t new_size = length + piece + (size_t)ind + 1;
440
+ char *grown = PyMem_Malloc(new_size);
441
+ if (grown == NULL) {
442
+ PyMem_Free(large);
443
+ return PyErr_NoMemory();
444
+ }
445
+ memcpy(grown, buf, length + piece);
446
+ PyMem_Free(large);
447
+ buf = large = grown;
448
+ length += piece;
449
+ size = (SQLINTEGER)new_size;
450
+ continue;
451
+ }
452
+
453
+ PyObject *s = PyUnicode_DecodeUTF8(buf, (Py_ssize_t)(length + ind), NULL);
454
+ PyMem_Free(large);
455
+ return s;
456
+ }
457
+ }
458
+
459
+ // fetch_row(stmt, kinds) -> tuple or None fetches the next row, it's None
460
+ // after the last row. kinds has a character for how each column is read: 'i'
461
+ // (SQL_BIGINT), 'd' (SQL_DOUBLE), 'r' (SQL_REAL) or 's' (SQL_CHAR). A NULL is
462
+ // None.
463
+ static PyObject *fetch_row(PyObject *self, PyObject *args) {
464
+ SQLINTEGER stmt;
465
+ const char *kinds;
466
+ Py_ssize_t n;
467
+ if (!PyArg_ParseTuple(args, "iy#", &stmt, &kinds, &n)) {
468
+ return NULL;
469
+ }
470
+
471
+ SQLRETURN rc;
472
+ Py_BEGIN_ALLOW_THREADS
473
+ rc = SQLFetch(stmt);
474
+ Py_END_ALLOW_THREADS
475
+ if (rc == SQL_NO_DATA) {
476
+ Py_RETURN_NONE;
477
+ }
478
+ if (check(rc, SQL_HANDLE_STMT, stmt) < 0) {
479
+ return NULL;
480
+ }
481
+
482
+ PyObject *row = PyTuple_New(n);
483
+ if (row == NULL) {
484
+ return NULL;
485
+ }
486
+
487
+ for (Py_ssize_t i = 0; i < n; i++) {
488
+ SQLSMALLINT column = (SQLSMALLINT)(i + 1);
489
+ SQLINTEGER ind = 0;
490
+ PyObject *value = NULL;
491
+ switch (kinds[i]) {
492
+ case 'i': {
493
+ SQLBIGINT v = 0;
494
+ Py_BEGIN_ALLOW_THREADS
495
+ rc = SQLGetData(stmt, column, SQL_BIGINT, &v, sizeof(v), &ind);
496
+ Py_END_ALLOW_THREADS
497
+ if (check(rc, SQL_HANDLE_STMT, stmt) == 0) {
498
+ value = ind == SQL_NULL_DATA ? Py_NewRef(Py_None) : PyLong_FromLongLong(v);
499
+ }
500
+ break;
501
+ }
502
+ case 'd': {
503
+ SQLDOUBLE v = 0;
504
+ Py_BEGIN_ALLOW_THREADS
505
+ rc = SQLGetData(stmt, column, SQL_DOUBLE, &v, sizeof(v), &ind);
506
+ Py_END_ALLOW_THREADS
507
+ if (check(rc, SQL_HANDLE_STMT, stmt) == 0) {
508
+ value = ind == SQL_NULL_DATA ? Py_NewRef(Py_None) : PyFloat_FromDouble(v);
509
+ }
510
+ break;
511
+ }
512
+ case 'r': {
513
+ SQLREAL v = 0;
514
+ Py_BEGIN_ALLOW_THREADS
515
+ rc = SQLGetData(stmt, column, SQL_REAL, &v, sizeof(v), &ind);
516
+ Py_END_ALLOW_THREADS
517
+ if (check(rc, SQL_HANDLE_STMT, stmt) == 0) {
518
+ value = ind == SQL_NULL_DATA ? Py_NewRef(Py_None) : PyFloat_FromDouble(v);
519
+ }
520
+ break;
521
+ }
522
+ default:
523
+ value = get_string(stmt, column);
524
+ break;
525
+ }
526
+ if (value == NULL) {
527
+ Py_DECREF(row);
528
+ return NULL;
529
+ }
530
+ PyTuple_SET_ITEM(row, i, value);
531
+ }
532
+
533
+ return row;
534
+ }
535
+
536
+ static PyMethodDef methods[] = {
537
+ {"set_error_factory", set_error_factory, METH_O, NULL},
538
+ {"alloc_handle", alloc_handle, METH_VARARGS, NULL},
539
+ {"free_handle", free_handle, METH_VARARGS, NULL},
540
+ {"connect", connect, METH_VARARGS, NULL},
541
+ {"disconnect", disconnect, METH_VARARGS, NULL},
542
+ {"set_autocommit", set_autocommit, METH_VARARGS, NULL},
543
+ {"start_tran", start_tran, METH_VARARGS, NULL},
544
+ {"end_tran", end_tran, METH_VARARGS, NULL},
545
+ {"exec_direct", exec_direct, METH_VARARGS, NULL},
546
+ {"prepare", prepare, METH_VARARGS, NULL},
547
+ {"execute", execute, METH_VARARGS, NULL},
548
+ {"row_count", row_count, METH_VARARGS, NULL},
549
+ {"num_result_cols", num_result_cols, METH_VARARGS, NULL},
550
+ {"describe_col", describe_col, METH_VARARGS, NULL},
551
+ {"close_cursor", close_cursor, METH_VARARGS, NULL},
552
+ {"fetch_row", fetch_row, METH_VARARGS, NULL},
553
+ {NULL, NULL, 0, NULL},
554
+ };
555
+
556
+ static struct PyModuleDef module = {
557
+ PyModuleDef_HEAD_INIT, "vsql._libvsql", NULL, -1, methods,
558
+ };
559
+
560
+ // The constants that the Python code needs.
561
+ static const struct {
562
+ const char *name;
563
+ long value;
564
+ } constants[] = {
565
+ {"SQL_HANDLE_ENV", SQL_HANDLE_ENV},
566
+ {"SQL_HANDLE_DBC", SQL_HANDLE_DBC},
567
+ {"SQL_HANDLE_STMT", SQL_HANDLE_STMT},
568
+ {"SQL_NULL_HANDLE", SQL_NULL_HANDLE},
569
+ {"SQL_CHAR", SQL_CHAR},
570
+ {"SQL_NUMERIC", SQL_NUMERIC},
571
+ {"SQL_DECIMAL", SQL_DECIMAL},
572
+ {"SQL_INTEGER", SQL_INTEGER},
573
+ {"SQL_SMALLINT", SQL_SMALLINT},
574
+ {"SQL_FLOAT", SQL_FLOAT},
575
+ {"SQL_REAL", SQL_REAL},
576
+ {"SQL_DOUBLE", SQL_DOUBLE},
577
+ {"SQL_VARCHAR", SQL_VARCHAR},
578
+ {"SQL_BOOLEAN", SQL_BOOLEAN},
579
+ {"SQL_BIGINT", SQL_BIGINT},
580
+ {"SQL_TYPE_DATE", SQL_TYPE_DATE},
581
+ {"SQL_TYPE_TIME", SQL_TYPE_TIME},
582
+ {"SQL_TYPE_TIMESTAMP", SQL_TYPE_TIMESTAMP},
583
+ {"SQL_TYPE_TIME_WITH_TIMEZONE", SQL_TYPE_TIME_WITH_TIMEZONE},
584
+ {"SQL_TYPE_TIMESTAMP_WITH_TIMEZONE", SQL_TYPE_TIMESTAMP_WITH_TIMEZONE},
585
+ {"SQL_NO_NULLS", SQL_NO_NULLS},
586
+ {"SQL_NULLABLE", SQL_NULLABLE},
587
+ {"SQL_TRANSACTION_READ_ONLY", SQL_TRANSACTION_READ_ONLY},
588
+ {"SQL_TRANSACTION_READ_WRITE", SQL_TRANSACTION_READ_WRITE},
589
+ {"SQL_TRANSACTION_READ_UNCOMMITTED", SQL_TRANSACTION_READ_UNCOMMITTED},
590
+ {"SQL_TRANSACTION_READ_COMMITTED", SQL_TRANSACTION_READ_COMMITTED},
591
+ {"SQL_TRANSACTION_REPEATABLE_READ", SQL_TRANSACTION_REPEATABLE_READ},
592
+ {"SQL_TRANSACTION_SERIALIZABLE", SQL_TRANSACTION_SERIALIZABLE},
593
+ {NULL, 0},
594
+ };
595
+
596
+ PyMODINIT_FUNC PyInit__libvsql(void) {
597
+ PyObject *m = PyModule_Create(&module);
598
+ if (m == NULL) {
599
+ return NULL;
600
+ }
601
+
602
+ for (int i = 0; constants[i].name != NULL; i++) {
603
+ if (PyModule_AddIntConstant(m, constants[i].name, constants[i].value) < 0) {
604
+ Py_DECREF(m);
605
+ return NULL;
606
+ }
607
+ }
608
+
609
+ return m;
610
+ }
Binary file