vsql-python 1.2.0__tar.gz → 1.2.4__tar.gz
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_python-1.2.0 → vsql_python-1.2.4}/PKG-INFO +20 -1
- {vsql_python-1.2.0 → vsql_python-1.2.4}/README.md +19 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/src/vsql/__init__.py +5 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/src/vsql/_connection.py +40 -13
- {vsql_python-1.2.0 → vsql_python-1.2.4}/src/vsql/_convert.py +7 -2
- {vsql_python-1.2.0 → vsql_python-1.2.4}/src/vsql/_errors.py +46 -3
- {vsql_python-1.2.0 → vsql_python-1.2.4}/src/vsql/_libvsql.c +140 -11
- {vsql_python-1.2.0 → vsql_python-1.2.4}/src/vsql_python.egg-info/PKG-INFO +20 -1
- {vsql_python-1.2.0 → vsql_python-1.2.4}/src/vsql_python.egg-info/scm_version.json +2 -2
- {vsql_python-1.2.0 → vsql_python-1.2.4}/tests/test_vsql.py +69 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/.github/workflows/release.yml +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/.github/workflows/test.yml +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/.gitignore +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/LICENSE +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/MANIFEST.in +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/example/main.py +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/internal/amalgamation/LICENSE +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/internal/amalgamation/LICENSE-V +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/internal/amalgamation/libvsql.c +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/internal/amalgamation/libvsql.h +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/libvsql_flags.json +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/pyproject.toml +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/scripts/update-amalgamation.sh +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/setup.cfg +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/setup.py +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/src/vsql_python.egg-info/SOURCES.txt +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/src/vsql_python.egg-info/dependency_links.txt +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/src/vsql_python.egg-info/scm_file_list.json +0 -0
- {vsql_python-1.2.0 → vsql_python-1.2.4}/src/vsql_python.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: vsql-python
|
|
3
|
-
Version: 1.2.
|
|
3
|
+
Version: 1.2.4
|
|
4
4
|
Summary: A DB-API 2.0 (PEP 249) driver for vsql, a single file or in-memory SQL database.
|
|
5
5
|
Author-email: Elliot Chance <elliotchance@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -184,6 +184,25 @@ except vsql.IntegrityError as e:
|
|
|
184
184
|
| `XX` internal error | `InternalError` |
|
|
185
185
|
| anything else | `DatabaseError` |
|
|
186
186
|
|
|
187
|
+
`e.diagnostics` is a tuple of `(sqlstate, message)` for every condition that
|
|
188
|
+
vsql raised, starting with the error itself.
|
|
189
|
+
|
|
190
|
+
### Warnings
|
|
191
|
+
|
|
192
|
+
A statement that completes with a warning (an SQLSTATE of class `01`) issues a
|
|
193
|
+
`vsql.Warning` with `warnings.warn`, so it's printed once by default, and can be
|
|
194
|
+
filtered or turned into an error like any other warning:
|
|
195
|
+
|
|
196
|
+
```python
|
|
197
|
+
cur.execute("SELECT COUNT(x) FROM foo")
|
|
198
|
+
# Warning: null value eliminated in set function (SQLSTATE 01003)
|
|
199
|
+
|
|
200
|
+
warnings.simplefilter("error", vsql.Warning) # raise them instead
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
`vsql.Warning` is a subclass of Python's `Warning` (which PEP 249 requires to
|
|
204
|
+
be an `Exception`), with the `sqlstate` and `message`.
|
|
205
|
+
|
|
187
206
|
## Versions
|
|
188
207
|
|
|
189
208
|
`vsql.vsql_version` is the version of vsql that's compiled into the package
|
|
@@ -165,6 +165,25 @@ except vsql.IntegrityError as e:
|
|
|
165
165
|
| `XX` internal error | `InternalError` |
|
|
166
166
|
| anything else | `DatabaseError` |
|
|
167
167
|
|
|
168
|
+
`e.diagnostics` is a tuple of `(sqlstate, message)` for every condition that
|
|
169
|
+
vsql raised, starting with the error itself.
|
|
170
|
+
|
|
171
|
+
### Warnings
|
|
172
|
+
|
|
173
|
+
A statement that completes with a warning (an SQLSTATE of class `01`) issues a
|
|
174
|
+
`vsql.Warning` with `warnings.warn`, so it's printed once by default, and can be
|
|
175
|
+
filtered or turned into an error like any other warning:
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
cur.execute("SELECT COUNT(x) FROM foo")
|
|
179
|
+
# Warning: null value eliminated in set function (SQLSTATE 01003)
|
|
180
|
+
|
|
181
|
+
warnings.simplefilter("error", vsql.Warning) # raise them instead
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
`vsql.Warning` is a subclass of Python's `Warning` (which PEP 249 requires to
|
|
185
|
+
be an `Exception`), with the `sqlstate` and `message`.
|
|
186
|
+
|
|
168
187
|
## Versions
|
|
169
188
|
|
|
170
189
|
`vsql.vsql_version` is the version of vsql that's compiled into the package
|
|
@@ -73,6 +73,9 @@ ProgrammingError for 42P01, an undefined table), with the SQLSTATE as its
|
|
|
73
73
|
sqlstate attribute. The object it's about (such as constraint_name or
|
|
74
74
|
table_name) is in the other attributes, see Error.
|
|
75
75
|
|
|
76
|
+
A statement that completes with a warning (such as 01003, null value
|
|
77
|
+
eliminated in set function) issues a Warning with warnings.warn.
|
|
78
|
+
|
|
76
79
|
Version
|
|
77
80
|
|
|
78
81
|
vsql_version is the version of vsql (such as "1.7.58"), vsql_version_info is
|
|
@@ -99,6 +102,7 @@ from ._errors import (
|
|
|
99
102
|
ProgrammingError,
|
|
100
103
|
Warning,
|
|
101
104
|
error,
|
|
105
|
+
warn as _warn,
|
|
102
106
|
)
|
|
103
107
|
|
|
104
108
|
apilevel = "2.0"
|
|
@@ -109,6 +113,7 @@ threadsafety = 1
|
|
|
109
113
|
paramstyle = "qmark"
|
|
110
114
|
|
|
111
115
|
_libvsql.set_error_factory(error)
|
|
116
|
+
_libvsql.set_warning_handler(_warn)
|
|
112
117
|
|
|
113
118
|
|
|
114
119
|
def connect(database, autocommit=False, isolation_level="READ COMMITTED", read_only=False):
|
|
@@ -3,7 +3,7 @@ import weakref
|
|
|
3
3
|
|
|
4
4
|
from . import _convert
|
|
5
5
|
from . import _libvsql as lib
|
|
6
|
-
from ._errors import InterfaceError, ProgrammingError
|
|
6
|
+
from ._errors import DatabaseError, InterfaceError, ProgrammingError
|
|
7
7
|
|
|
8
8
|
_isolation_levels = {
|
|
9
9
|
"READ UNCOMMITTED": lib.SQL_TRANSACTION_READ_UNCOMMITTED,
|
|
@@ -103,6 +103,17 @@ class Connection:
|
|
|
103
103
|
lib.start_tran(self._dbc, self._access_mode, self._isolation_level)
|
|
104
104
|
self._in_transaction = True
|
|
105
105
|
|
|
106
|
+
def _check_transaction(self, handle_type, handle):
|
|
107
|
+
"""Checks whether the transaction is still active after an error. vsql
|
|
108
|
+
rolls back the whole transaction for some errors (such as a 40001
|
|
109
|
+
serialization failure), which closes the cursors, and then the next
|
|
110
|
+
statement starts a new transaction."""
|
|
111
|
+
|
|
112
|
+
if self._in_transaction and not lib.transaction_active(handle_type, handle):
|
|
113
|
+
self._in_transaction = False
|
|
114
|
+
for cursor in list(self._cursors):
|
|
115
|
+
cursor._open = False
|
|
116
|
+
|
|
106
117
|
def _end(self, commit):
|
|
107
118
|
self._check()
|
|
108
119
|
if not self._in_transaction:
|
|
@@ -111,8 +122,12 @@ class Connection:
|
|
|
111
122
|
# The cursors of the transaction are closed by it.
|
|
112
123
|
for cursor in list(self._cursors):
|
|
113
124
|
cursor._close_results()
|
|
114
|
-
|
|
115
|
-
|
|
125
|
+
try:
|
|
126
|
+
lib.end_tran(self._dbc, commit)
|
|
127
|
+
finally:
|
|
128
|
+
# A commit that fails may leave the transaction active, or roll
|
|
129
|
+
# it back.
|
|
130
|
+
self._in_transaction = lib.transaction_active(lib.SQL_HANDLE_DBC, self._dbc)
|
|
116
131
|
|
|
117
132
|
def commit(self):
|
|
118
133
|
"""Commits the transaction, if there is one."""
|
|
@@ -138,7 +153,11 @@ class Connection:
|
|
|
138
153
|
self._check()
|
|
139
154
|
for cursor in list(self._cursors):
|
|
140
155
|
cursor._close_results()
|
|
141
|
-
|
|
156
|
+
try:
|
|
157
|
+
lib.end_savepoint(self._dbc, name, True)
|
|
158
|
+
except DatabaseError:
|
|
159
|
+
self._check_transaction(lib.SQL_HANDLE_DBC, self._dbc)
|
|
160
|
+
raise
|
|
142
161
|
|
|
143
162
|
def cursor(self):
|
|
144
163
|
self._check()
|
|
@@ -242,15 +261,19 @@ class Cursor:
|
|
|
242
261
|
if self._stmt is None:
|
|
243
262
|
self._stmt = lib.alloc_handle(lib.SQL_HANDLE_STMT, self.connection._dbc)
|
|
244
263
|
|
|
245
|
-
|
|
246
|
-
if operation
|
|
264
|
+
try:
|
|
265
|
+
if params or operation == self._prepared:
|
|
266
|
+
if operation != self._prepared:
|
|
267
|
+
self._prepared = None
|
|
268
|
+
lib.prepare(self._stmt, operation)
|
|
269
|
+
self._prepared = operation
|
|
270
|
+
lib.execute(self._stmt, params)
|
|
271
|
+
else:
|
|
247
272
|
self._prepared = None
|
|
248
|
-
lib.
|
|
249
|
-
|
|
250
|
-
lib.
|
|
251
|
-
|
|
252
|
-
self._prepared = None
|
|
253
|
-
lib.exec_direct(self._stmt, operation)
|
|
273
|
+
lib.exec_direct(self._stmt, operation)
|
|
274
|
+
except DatabaseError:
|
|
275
|
+
self.connection._check_transaction(lib.SQL_HANDLE_STMT, self._stmt)
|
|
276
|
+
raise
|
|
254
277
|
|
|
255
278
|
n = lib.num_result_cols(self._stmt)
|
|
256
279
|
if n == 0:
|
|
@@ -298,7 +321,11 @@ class Cursor:
|
|
|
298
321
|
raise ProgrammingError("the last statement wasn't a query")
|
|
299
322
|
return None
|
|
300
323
|
|
|
301
|
-
|
|
324
|
+
try:
|
|
325
|
+
row = lib.fetch_row(self._stmt, self._kinds)
|
|
326
|
+
except DatabaseError:
|
|
327
|
+
self.connection._check_transaction(lib.SQL_HANDLE_STMT, self._stmt)
|
|
328
|
+
raise
|
|
302
329
|
if row is None:
|
|
303
330
|
self._close_results()
|
|
304
331
|
return None
|
|
@@ -108,8 +108,13 @@ def to_param(value):
|
|
|
108
108
|
return (lib.SQL_DOUBLE, lib.SQL_DOUBLE, 0, 0, value)
|
|
109
109
|
|
|
110
110
|
if isinstance(value, str):
|
|
111
|
-
# A CHARACTER VARYING as long as the string (which can't be 0).
|
|
112
|
-
|
|
111
|
+
# A CHARACTER VARYING as long as the string (which can't be 0). A lone
|
|
112
|
+
# surrogate (such as from surrogateescape) can't be UTF-8.
|
|
113
|
+
try:
|
|
114
|
+
encoded = value.encode()
|
|
115
|
+
except UnicodeEncodeError as e:
|
|
116
|
+
raise DataError(f"{value!r} isn't valid Unicode: {e.reason}") from None
|
|
117
|
+
return (lib.SQL_CHAR, lib.SQL_VARCHAR, max(len(value), 1), 0, encoded)
|
|
113
118
|
|
|
114
119
|
if isinstance(value, decimal.Decimal):
|
|
115
120
|
if not value.is_finite():
|
|
@@ -3,9 +3,46 @@
|
|
|
3
3
|
# sqlstate attribute, and the other fields of its diagnostic record (such as
|
|
4
4
|
# the constraint that was violated) as the other attributes.
|
|
5
5
|
|
|
6
|
+
import builtins
|
|
7
|
+
import os
|
|
8
|
+
import sys
|
|
9
|
+
import warnings
|
|
6
10
|
|
|
7
|
-
|
|
8
|
-
|
|
11
|
+
_package = os.path.dirname(__file__)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Warning(builtins.Warning): # noqa: A001 (PEP 249 requires this name)
|
|
15
|
+
"""A warning from vsql (an SQLSTATE of class 01), such as 01003 (null value
|
|
16
|
+
eliminated in set function). It's issued with warnings.warn (so it's
|
|
17
|
+
printed once by default, and can be filtered or made an error), rather
|
|
18
|
+
than raised, since the statement still completed.
|
|
19
|
+
|
|
20
|
+
sqlstate is the SQLSTATE, and message is the message.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def __init__(self, message, sqlstate=None):
|
|
24
|
+
super().__init__(message)
|
|
25
|
+
self.message = message
|
|
26
|
+
self.sqlstate = sqlstate
|
|
27
|
+
|
|
28
|
+
def __str__(self):
|
|
29
|
+
if self.sqlstate is None:
|
|
30
|
+
return self.message
|
|
31
|
+
|
|
32
|
+
return f"{self.message} (SQLSTATE {self.sqlstate})"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def warn(sqlstate, message):
|
|
36
|
+
"""Issues a Warning for a diagnostic record of a statement that completed
|
|
37
|
+
with a warning. It's for the code that called the cursor (or connection)
|
|
38
|
+
method."""
|
|
39
|
+
|
|
40
|
+
# The first frame that isn't in this package (skip_file_prefixes is new in
|
|
41
|
+
# Python 3.12).
|
|
42
|
+
frame, level = sys._getframe(1), 2
|
|
43
|
+
while frame is not None and frame.f_code.co_filename.startswith(_package):
|
|
44
|
+
frame, level = frame.f_back, level + 1
|
|
45
|
+
warnings.warn(Warning(message, sqlstate), stacklevel=level)
|
|
9
46
|
|
|
10
47
|
|
|
11
48
|
class Error(Exception):
|
|
@@ -26,14 +63,19 @@ class Error(Exception):
|
|
|
26
63
|
the column (such as one that doesn't exist)
|
|
27
64
|
cursor_name
|
|
28
65
|
the cursor (for an invalid cursor state)
|
|
66
|
+
|
|
67
|
+
diagnostics is a tuple of (sqlstate, message) for each condition that vsql
|
|
68
|
+
raised, starting with this one (or () if the error isn't from vsql).
|
|
29
69
|
"""
|
|
30
70
|
|
|
31
71
|
constraint_catalog = constraint_schema = constraint_name = None
|
|
32
72
|
catalog_name = schema_name = table_name = column_name = cursor_name = None
|
|
73
|
+
diagnostics = ()
|
|
33
74
|
|
|
34
75
|
def __init__(self, message, sqlstate=None, *, constraint_catalog=None,
|
|
35
76
|
constraint_schema=None, constraint_name=None, catalog_name=None,
|
|
36
|
-
schema_name=None, table_name=None, column_name=None, cursor_name=None
|
|
77
|
+
schema_name=None, table_name=None, column_name=None, cursor_name=None,
|
|
78
|
+
diagnostics=()):
|
|
37
79
|
super().__init__(message)
|
|
38
80
|
self.message = message
|
|
39
81
|
self.sqlstate = sqlstate
|
|
@@ -45,6 +87,7 @@ class Error(Exception):
|
|
|
45
87
|
self.table_name = table_name
|
|
46
88
|
self.column_name = column_name
|
|
47
89
|
self.cursor_name = cursor_name
|
|
90
|
+
self.diagnostics = tuple(diagnostics)
|
|
48
91
|
|
|
49
92
|
def __str__(self):
|
|
50
93
|
if self.sqlstate is None:
|
|
@@ -9,7 +9,10 @@
|
|
|
9
9
|
//
|
|
10
10
|
// When a routine fails, the exception is made by the function given to
|
|
11
11
|
// set_error_factory, from the SQLSTATE, message and other fields (such as the
|
|
12
|
-
// constraint that was violated) of the first diagnostic record
|
|
12
|
+
// constraint that was violated) of the first diagnostic record, and every
|
|
13
|
+
// record (diagnostics). When a statement completes with a warning
|
|
14
|
+
// (SQL_SUCCESS_WITH_INFO), the function given to set_warning_handler is called
|
|
15
|
+
// with the SQLSTATE and message of each record.
|
|
13
16
|
|
|
14
17
|
#define PY_SSIZE_T_CLEAN
|
|
15
18
|
#include <Python.h>
|
|
@@ -28,6 +31,81 @@ static inline PyObject *Py_NewRef(PyObject *o) {
|
|
|
28
31
|
#endif
|
|
29
32
|
|
|
30
33
|
static PyObject *error_factory = NULL;
|
|
34
|
+
static PyObject *warning_handler = NULL;
|
|
35
|
+
|
|
36
|
+
// read_records returns a list of (sqlstate, message) for each diagnostic
|
|
37
|
+
// record of the last routine called with the handle, or NULL with an
|
|
38
|
+
// exception.
|
|
39
|
+
static PyObject *read_records(SQLSMALLINT handle_type, SQLINTEGER handle) {
|
|
40
|
+
PyObject *records = PyList_New(0);
|
|
41
|
+
if (records == NULL) {
|
|
42
|
+
return NULL;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for (SQLSMALLINT i = 1;; i++) {
|
|
46
|
+
SQLCHAR sqlstate[6];
|
|
47
|
+
SQLSMALLINT size = 512, length = 0;
|
|
48
|
+
SQLCHAR *message = NULL;
|
|
49
|
+
SQLRETURN rc;
|
|
50
|
+
for (;;) {
|
|
51
|
+
message = PyMem_Malloc(size);
|
|
52
|
+
if (message == NULL) {
|
|
53
|
+
Py_DECREF(records);
|
|
54
|
+
return PyErr_NoMemory();
|
|
55
|
+
}
|
|
56
|
+
Py_BEGIN_ALLOW_THREADS
|
|
57
|
+
rc = SQLGetDiagRec(handle_type, handle, i, sqlstate, NULL, message, size, &length);
|
|
58
|
+
Py_END_ALLOW_THREADS
|
|
59
|
+
// The message was truncated.
|
|
60
|
+
if ((rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) && length >= size) {
|
|
61
|
+
size = length + 1;
|
|
62
|
+
PyMem_Free(message);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
|
|
68
|
+
PyMem_Free(message);
|
|
69
|
+
return records;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
PyObject *record = Py_BuildValue("(sN)", (const char *)sqlstate,
|
|
73
|
+
PyUnicode_DecodeUTF8((const char *)message,
|
|
74
|
+
(Py_ssize_t)length, "replace"));
|
|
75
|
+
PyMem_Free(message);
|
|
76
|
+
if (record == NULL || PyList_Append(records, record) < 0) {
|
|
77
|
+
Py_XDECREF(record);
|
|
78
|
+
Py_DECREF(records);
|
|
79
|
+
return NULL;
|
|
80
|
+
}
|
|
81
|
+
Py_DECREF(record);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// warn calls the warning handler for each diagnostic record if the routine
|
|
86
|
+
// completed with a warning. It returns -1 if the handler raised an exception
|
|
87
|
+
// (such as when warnings are errors), otherwise 0.
|
|
88
|
+
static int warn(SQLRETURN rc, SQLSMALLINT handle_type, SQLINTEGER handle) {
|
|
89
|
+
if (rc != SQL_SUCCESS_WITH_INFO || warning_handler == NULL) {
|
|
90
|
+
return 0;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
PyObject *records = read_records(handle_type, handle);
|
|
94
|
+
if (records == NULL) {
|
|
95
|
+
return -1;
|
|
96
|
+
}
|
|
97
|
+
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(records); i++) {
|
|
98
|
+
PyObject *result = PyObject_CallObject(warning_handler, PyList_GET_ITEM(records, i));
|
|
99
|
+
if (result == NULL) {
|
|
100
|
+
Py_DECREF(records);
|
|
101
|
+
return -1;
|
|
102
|
+
}
|
|
103
|
+
Py_DECREF(result);
|
|
104
|
+
}
|
|
105
|
+
Py_DECREF(records);
|
|
106
|
+
|
|
107
|
+
return 0;
|
|
108
|
+
}
|
|
31
109
|
|
|
32
110
|
// raise_error raises the exception for an SQLSTATE and message, with the other
|
|
33
111
|
// fields of its diagnostic record (a dict, or NULL).
|
|
@@ -150,10 +228,16 @@ static int check(SQLRETURN rc, SQLSMALLINT handle_type, SQLINTEGER handle) {
|
|
|
150
228
|
}
|
|
151
229
|
|
|
152
230
|
PyObject *fields = get_diag_fields(handle_type, handle);
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
231
|
+
PyObject *records = fields == NULL ? NULL : read_records(handle_type, handle);
|
|
232
|
+
if (records != NULL) {
|
|
233
|
+
PyObject *diagnostics = PyList_AsTuple(records);
|
|
234
|
+
if (diagnostics != NULL && PyDict_SetItemString(fields, "diagnostics", diagnostics) == 0) {
|
|
235
|
+
raise_error_fields((const char *)sqlstate, (const char *)message, length, fields);
|
|
236
|
+
}
|
|
237
|
+
Py_XDECREF(diagnostics);
|
|
238
|
+
Py_DECREF(records);
|
|
156
239
|
}
|
|
240
|
+
Py_XDECREF(fields);
|
|
157
241
|
free(message);
|
|
158
242
|
return -1;
|
|
159
243
|
}
|
|
@@ -172,6 +256,25 @@ static int check(SQLRETURN rc, SQLSMALLINT handle_type, SQLINTEGER handle) {
|
|
|
172
256
|
} \
|
|
173
257
|
} while (0)
|
|
174
258
|
|
|
259
|
+
// CALL_WARN is CALL for a routine that can complete with a warning, which is
|
|
260
|
+
// given to the warning handler.
|
|
261
|
+
#define CALL_WARN(handle_type, handle, routine) \
|
|
262
|
+
do { \
|
|
263
|
+
SQLRETURN rc_; \
|
|
264
|
+
Py_BEGIN_ALLOW_THREADS \
|
|
265
|
+
rc_ = (routine); \
|
|
266
|
+
Py_END_ALLOW_THREADS \
|
|
267
|
+
if (check(rc_, (handle_type), (handle)) < 0 || \
|
|
268
|
+
warn(rc_, (handle_type), (handle)) < 0) { \
|
|
269
|
+
return NULL; \
|
|
270
|
+
} \
|
|
271
|
+
} while (0)
|
|
272
|
+
|
|
273
|
+
static PyObject *set_warning_handler(PyObject *self, PyObject *handler) {
|
|
274
|
+
Py_XSETREF(warning_handler, Py_NewRef(handler));
|
|
275
|
+
Py_RETURN_NONE;
|
|
276
|
+
}
|
|
277
|
+
|
|
175
278
|
static PyObject *set_error_factory(PyObject *self, PyObject *factory) {
|
|
176
279
|
Py_XSETREF(error_factory, Py_NewRef(factory));
|
|
177
280
|
Py_RETURN_NONE;
|
|
@@ -277,7 +380,8 @@ static PyObject *end_tran(PyObject *self, PyObject *args) {
|
|
|
277
380
|
return NULL;
|
|
278
381
|
}
|
|
279
382
|
|
|
280
|
-
|
|
383
|
+
CALL_WARN(SQL_HANDLE_DBC, dbc,
|
|
384
|
+
SQLEndTran(SQL_HANDLE_DBC, dbc, commit ? SQL_COMMIT : SQL_ROLLBACK));
|
|
281
385
|
Py_RETURN_NONE;
|
|
282
386
|
}
|
|
283
387
|
|
|
@@ -323,6 +427,22 @@ static PyObject *end_savepoint(PyObject *self, PyObject *args) {
|
|
|
323
427
|
Py_RETURN_NONE;
|
|
324
428
|
}
|
|
325
429
|
|
|
430
|
+
// transaction_active(handle_type, handle) -> bool is whether a transaction was
|
|
431
|
+
// active after the last routine called with the handle
|
|
432
|
+
// (SQL_DIAG_TRANSACTION_ACTIVE).
|
|
433
|
+
static PyObject *transaction_active(PyObject *self, PyObject *args) {
|
|
434
|
+
int handle_type;
|
|
435
|
+
SQLINTEGER handle, active = 0;
|
|
436
|
+
if (!PyArg_ParseTuple(args, "ii", &handle_type, &handle)) {
|
|
437
|
+
return NULL;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
CALL(handle_type, handle,
|
|
441
|
+
SQLGetDiagField((SQLSMALLINT)handle_type, handle, 0, SQL_DIAG_TRANSACTION_ACTIVE, &active,
|
|
442
|
+
0, NULL));
|
|
443
|
+
return PyBool_FromLong(active);
|
|
444
|
+
}
|
|
445
|
+
|
|
326
446
|
// exec_direct(stmt, query)
|
|
327
447
|
static PyObject *exec_direct(PyObject *self, PyObject *args) {
|
|
328
448
|
SQLINTEGER stmt;
|
|
@@ -332,7 +452,7 @@ static PyObject *exec_direct(PyObject *self, PyObject *args) {
|
|
|
332
452
|
return NULL;
|
|
333
453
|
}
|
|
334
454
|
|
|
335
|
-
|
|
455
|
+
CALL_WARN(SQL_HANDLE_STMT, stmt, SQLExecDirect(stmt, (SQLCHAR *)query, (SQLINTEGER)length));
|
|
336
456
|
Py_RETURN_NONE;
|
|
337
457
|
}
|
|
338
458
|
|
|
@@ -443,7 +563,7 @@ static PyObject *execute(PyObject *self, PyObject *args) {
|
|
|
443
563
|
Py_BEGIN_ALLOW_THREADS
|
|
444
564
|
rc = SQLExecute(stmt);
|
|
445
565
|
Py_END_ALLOW_THREADS
|
|
446
|
-
if (check(rc, SQL_HANDLE_STMT, stmt) == 0) {
|
|
566
|
+
if (check(rc, SQL_HANDLE_STMT, stmt) == 0 && warn(rc, SQL_HANDLE_STMT, stmt) == 0) {
|
|
447
567
|
result = Py_NewRef(Py_None);
|
|
448
568
|
}
|
|
449
569
|
|
|
@@ -570,10 +690,11 @@ static PyObject *get_string(SQLINTEGER stmt, SQLSMALLINT n) {
|
|
|
570
690
|
Py_RETURN_NONE;
|
|
571
691
|
}
|
|
572
692
|
|
|
573
|
-
// The rest of the string (ind) didn't fit, so the buffer
|
|
574
|
-
// terminated piece of it.
|
|
693
|
+
// The rest of the string (ind) didn't fit, so the buffer is full with a
|
|
694
|
+
// null terminated piece of it. The piece is the whole buffer, since the
|
|
695
|
+
// string can have a NUL in it.
|
|
575
696
|
if (rc == SQL_SUCCESS_WITH_INFO && ind >= size - (SQLINTEGER)length) {
|
|
576
|
-
size_t piece =
|
|
697
|
+
size_t piece = (size_t)size - length - 1;
|
|
577
698
|
size_t new_size = length + piece + (size_t)ind + 1;
|
|
578
699
|
char *grown = PyMem_Malloc(new_size);
|
|
579
700
|
if (grown == NULL) {
|
|
@@ -590,6 +711,12 @@ static PyObject *get_string(SQLINTEGER stmt, SQLSMALLINT n) {
|
|
|
590
711
|
|
|
591
712
|
PyObject *s = PyUnicode_DecodeUTF8(buf, (Py_ssize_t)(length + ind), NULL);
|
|
592
713
|
PyMem_Free(large);
|
|
714
|
+
// vsql's strings are always UTF-8, but it should still be a DataError
|
|
715
|
+
// rather than a UnicodeDecodeError if one isn't.
|
|
716
|
+
if (s == NULL && PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
|
|
717
|
+
PyErr_Clear();
|
|
718
|
+
raise_error("22021", "character not in repertoire: the value isn't UTF-8", -1);
|
|
719
|
+
}
|
|
593
720
|
return s;
|
|
594
721
|
}
|
|
595
722
|
}
|
|
@@ -658,7 +785,7 @@ static PyObject *fetch_row(PyObject *self, PyObject *args) {
|
|
|
658
785
|
if (rc == SQL_NO_DATA) {
|
|
659
786
|
Py_RETURN_NONE;
|
|
660
787
|
}
|
|
661
|
-
if (check(rc, SQL_HANDLE_STMT, stmt) < 0) {
|
|
788
|
+
if (check(rc, SQL_HANDLE_STMT, stmt) < 0 || warn(rc, SQL_HANDLE_STMT, stmt) < 0) {
|
|
662
789
|
return NULL;
|
|
663
790
|
}
|
|
664
791
|
|
|
@@ -721,6 +848,7 @@ static PyObject *fetch_row(PyObject *self, PyObject *args) {
|
|
|
721
848
|
|
|
722
849
|
static PyMethodDef methods[] = {
|
|
723
850
|
{"set_error_factory", set_error_factory, METH_O, NULL},
|
|
851
|
+
{"set_warning_handler", set_warning_handler, METH_O, NULL},
|
|
724
852
|
{"alloc_handle", alloc_handle, METH_VARARGS, NULL},
|
|
725
853
|
{"free_handle", free_handle, METH_VARARGS, NULL},
|
|
726
854
|
{"connect", connect, METH_VARARGS, NULL},
|
|
@@ -729,6 +857,7 @@ static PyMethodDef methods[] = {
|
|
|
729
857
|
{"start_tran", start_tran, METH_VARARGS, NULL},
|
|
730
858
|
{"end_tran", end_tran, METH_VARARGS, NULL},
|
|
731
859
|
{"end_savepoint", end_savepoint, METH_VARARGS, NULL},
|
|
860
|
+
{"transaction_active", transaction_active, METH_VARARGS, NULL},
|
|
732
861
|
{"get_info", get_info, METH_VARARGS, NULL},
|
|
733
862
|
{"exec_direct", exec_direct, METH_VARARGS, NULL},
|
|
734
863
|
{"prepare", prepare, METH_VARARGS, NULL},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: vsql-python
|
|
3
|
-
Version: 1.2.
|
|
3
|
+
Version: 1.2.4
|
|
4
4
|
Summary: A DB-API 2.0 (PEP 249) driver for vsql, a single file or in-memory SQL database.
|
|
5
5
|
Author-email: Elliot Chance <elliotchance@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -184,6 +184,25 @@ except vsql.IntegrityError as e:
|
|
|
184
184
|
| `XX` internal error | `InternalError` |
|
|
185
185
|
| anything else | `DatabaseError` |
|
|
186
186
|
|
|
187
|
+
`e.diagnostics` is a tuple of `(sqlstate, message)` for every condition that
|
|
188
|
+
vsql raised, starting with the error itself.
|
|
189
|
+
|
|
190
|
+
### Warnings
|
|
191
|
+
|
|
192
|
+
A statement that completes with a warning (an SQLSTATE of class `01`) issues a
|
|
193
|
+
`vsql.Warning` with `warnings.warn`, so it's printed once by default, and can be
|
|
194
|
+
filtered or turned into an error like any other warning:
|
|
195
|
+
|
|
196
|
+
```python
|
|
197
|
+
cur.execute("SELECT COUNT(x) FROM foo")
|
|
198
|
+
# Warning: null value eliminated in set function (SQLSTATE 01003)
|
|
199
|
+
|
|
200
|
+
warnings.simplefilter("error", vsql.Warning) # raise them instead
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
`vsql.Warning` is a subclass of Python's `Warning` (which PEP 249 requires to
|
|
204
|
+
be an `Exception`), with the `sqlstate` and `message`.
|
|
205
|
+
|
|
187
206
|
## Versions
|
|
188
207
|
|
|
189
208
|
`vsql.vsql_version` is the version of vsql that's compiled into the package
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import datetime
|
|
2
2
|
import decimal
|
|
3
3
|
import threading
|
|
4
|
+
import warnings
|
|
4
5
|
|
|
5
6
|
import pytest
|
|
6
7
|
|
|
@@ -245,6 +246,7 @@ def test_parameters_in_where(conn):
|
|
|
245
246
|
([vsql.YearMonthInterval(1)], vsql.ProgrammingError, "07006"),
|
|
246
247
|
([object()], vsql.ProgrammingError, "unsupported type"),
|
|
247
248
|
([decimal.Decimal("NaN")], vsql.DataError, "finite"),
|
|
249
|
+
(["\ud800"], vsql.DataError, "parameter 1: .* isn't valid Unicode"),
|
|
248
250
|
([], vsql.ProgrammingError, "dynamic parameter"),
|
|
249
251
|
],
|
|
250
252
|
)
|
|
@@ -299,6 +301,11 @@ def test_long_strings(conn):
|
|
|
299
301
|
assert scalar(conn, "SELECT s FROM foo") == want
|
|
300
302
|
|
|
301
303
|
|
|
304
|
+
# A NUL is part of the string, even in a piece that isn't the last.
|
|
305
|
+
for want in ["a\x00b", "a\x00b" + "c" * 300, "c" * 300 + "\x00" + "é" * 300]:
|
|
306
|
+
assert scalar(conn, "SELECT CAST(? AS VARCHAR(1000))", [want]) == want
|
|
307
|
+
|
|
308
|
+
|
|
302
309
|
@pytest.mark.parametrize(
|
|
303
310
|
"query, error, sqlstate",
|
|
304
311
|
[
|
|
@@ -355,6 +362,31 @@ def test_error_fields(conn):
|
|
|
355
362
|
assert (e.sqlstate, e.table_name, e.cursor_name) == (None, None, None)
|
|
356
363
|
|
|
357
364
|
|
|
365
|
+
|
|
366
|
+
def test_warnings(conn):
|
|
367
|
+
conn.execute("CREATE TABLE foo (x INTEGER)")
|
|
368
|
+
conn.execute("INSERT INTO foo (x) VALUES (1), (NULL)")
|
|
369
|
+
with pytest.warns(vsql.Warning) as record:
|
|
370
|
+
assert scalar(conn, "SELECT COUNT(x) FROM foo") == 1
|
|
371
|
+
assert [(w.message.sqlstate, w.message.message) for w in record] == [
|
|
372
|
+
("01003", "null value eliminated in set function")]
|
|
373
|
+
assert record[0].filename == __file__
|
|
374
|
+
assert issubclass(vsql.Warning, Exception)
|
|
375
|
+
|
|
376
|
+
# A warning can be an error.
|
|
377
|
+
with warnings.catch_warnings():
|
|
378
|
+
warnings.simplefilter("error", vsql.Warning)
|
|
379
|
+
with pytest.raises(vsql.Warning) as e:
|
|
380
|
+
conn.execute("VALUES CAST('abc' AS VARCHAR(2))")
|
|
381
|
+
assert e.value.sqlstate == "01004"
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def test_diagnostics(conn):
|
|
385
|
+
with pytest.raises(vsql.ProgrammingError) as e:
|
|
386
|
+
conn.execute("SELECT * FROM nope")
|
|
387
|
+
assert e.value.diagnostics == (("42P01", e.value.message),)
|
|
388
|
+
assert vsql.InterfaceError("x").diagnostics == ()
|
|
389
|
+
|
|
358
390
|
def test_error_classes():
|
|
359
391
|
assert isinstance(vsql.error("42P01", "x"), vsql.ProgrammingError)
|
|
360
392
|
assert isinstance(vsql.error("23502", "x"), vsql.IntegrityError)
|
|
@@ -520,6 +552,43 @@ def test_transactions(path):
|
|
|
520
552
|
assert count() == 4
|
|
521
553
|
|
|
522
554
|
|
|
555
|
+
|
|
556
|
+
def test_transaction_rolled_back_by_vsql(path):
|
|
557
|
+
"""A 40001 rolls back the whole transaction, so the next statement starts a
|
|
558
|
+
new one (which rollback undoes). Other errors only undo the statement."""
|
|
559
|
+
|
|
560
|
+
with vsql.connect(path) as conn:
|
|
561
|
+
conn.execute("CREATE TABLE t (x INTEGER PRIMARY KEY)")
|
|
562
|
+
conn.execute("INSERT INTO t (x) VALUES (1)")
|
|
563
|
+
|
|
564
|
+
a, b = vsql.connect(path), vsql.connect(path)
|
|
565
|
+
a.execute("UPDATE t SET x = 100 WHERE x = 1")
|
|
566
|
+
b.execute("INSERT INTO t (x) VALUES (50)")
|
|
567
|
+
cur = b.execute("SELECT x FROM t")
|
|
568
|
+
with pytest.raises(vsql.OperationalError) as e:
|
|
569
|
+
b.execute("UPDATE t SET x = 200 WHERE x = 1")
|
|
570
|
+
assert e.value.sqlstate == "40001"
|
|
571
|
+
assert not b.in_transaction
|
|
572
|
+
assert cur.fetchone() is None
|
|
573
|
+
a.commit()
|
|
574
|
+
|
|
575
|
+
b.execute("INSERT INTO t (x) VALUES (60)")
|
|
576
|
+
assert b.in_transaction
|
|
577
|
+
b.rollback()
|
|
578
|
+
assert not b.in_transaction
|
|
579
|
+
|
|
580
|
+
# A unique violation only undoes the statement.
|
|
581
|
+
b.execute("INSERT INTO t (x) VALUES (70)")
|
|
582
|
+
with pytest.raises(vsql.IntegrityError):
|
|
583
|
+
b.execute("INSERT INTO t (x) VALUES (70)")
|
|
584
|
+
assert b.in_transaction
|
|
585
|
+
b.commit()
|
|
586
|
+
a.close()
|
|
587
|
+
b.close()
|
|
588
|
+
|
|
589
|
+
with vsql.connect(path) as conn:
|
|
590
|
+
assert conn.execute("SELECT x FROM t ORDER BY x").fetchall() == [(70,), (100,)]
|
|
591
|
+
|
|
523
592
|
def test_savepoints(path):
|
|
524
593
|
conn = vsql.connect(path)
|
|
525
594
|
conn.execute("CREATE TABLE foo (x INTEGER)")
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|