vsql-python 1.2.2__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.2 → vsql_python-1.2.4}/PKG-INFO +20 -1
- {vsql_python-1.2.2 → vsql_python-1.2.4}/README.md +19 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/src/vsql/__init__.py +5 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/src/vsql/_convert.py +7 -2
- {vsql_python-1.2.2 → vsql_python-1.2.4}/src/vsql/_errors.py +46 -3
- {vsql_python-1.2.2 → vsql_python-1.2.4}/src/vsql/_libvsql.c +119 -8
- {vsql_python-1.2.2 → vsql_python-1.2.4}/src/vsql_python.egg-info/PKG-INFO +20 -1
- {vsql_python-1.2.2 → vsql_python-1.2.4}/src/vsql_python.egg-info/scm_version.json +2 -2
- {vsql_python-1.2.2 → vsql_python-1.2.4}/tests/test_vsql.py +27 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/.github/workflows/release.yml +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/.github/workflows/test.yml +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/.gitignore +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/LICENSE +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/MANIFEST.in +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/example/main.py +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/internal/amalgamation/LICENSE +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/internal/amalgamation/LICENSE-V +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/internal/amalgamation/libvsql.c +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/internal/amalgamation/libvsql.h +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/libvsql_flags.json +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/pyproject.toml +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/scripts/update-amalgamation.sh +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/setup.cfg +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/setup.py +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/src/vsql/_connection.py +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/src/vsql_python.egg-info/SOURCES.txt +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/src/vsql_python.egg-info/dependency_links.txt +0 -0
- {vsql_python-1.2.2 → vsql_python-1.2.4}/src/vsql_python.egg-info/scm_file_list.json +0 -0
- {vsql_python-1.2.2 → 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):
|
|
@@ -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
|
|
|
@@ -348,7 +452,7 @@ static PyObject *exec_direct(PyObject *self, PyObject *args) {
|
|
|
348
452
|
return NULL;
|
|
349
453
|
}
|
|
350
454
|
|
|
351
|
-
|
|
455
|
+
CALL_WARN(SQL_HANDLE_STMT, stmt, SQLExecDirect(stmt, (SQLCHAR *)query, (SQLINTEGER)length));
|
|
352
456
|
Py_RETURN_NONE;
|
|
353
457
|
}
|
|
354
458
|
|
|
@@ -459,7 +563,7 @@ static PyObject *execute(PyObject *self, PyObject *args) {
|
|
|
459
563
|
Py_BEGIN_ALLOW_THREADS
|
|
460
564
|
rc = SQLExecute(stmt);
|
|
461
565
|
Py_END_ALLOW_THREADS
|
|
462
|
-
if (check(rc, SQL_HANDLE_STMT, stmt) == 0) {
|
|
566
|
+
if (check(rc, SQL_HANDLE_STMT, stmt) == 0 && warn(rc, SQL_HANDLE_STMT, stmt) == 0) {
|
|
463
567
|
result = Py_NewRef(Py_None);
|
|
464
568
|
}
|
|
465
569
|
|
|
@@ -607,6 +711,12 @@ static PyObject *get_string(SQLINTEGER stmt, SQLSMALLINT n) {
|
|
|
607
711
|
|
|
608
712
|
PyObject *s = PyUnicode_DecodeUTF8(buf, (Py_ssize_t)(length + ind), NULL);
|
|
609
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
|
+
}
|
|
610
720
|
return s;
|
|
611
721
|
}
|
|
612
722
|
}
|
|
@@ -675,7 +785,7 @@ static PyObject *fetch_row(PyObject *self, PyObject *args) {
|
|
|
675
785
|
if (rc == SQL_NO_DATA) {
|
|
676
786
|
Py_RETURN_NONE;
|
|
677
787
|
}
|
|
678
|
-
if (check(rc, SQL_HANDLE_STMT, stmt) < 0) {
|
|
788
|
+
if (check(rc, SQL_HANDLE_STMT, stmt) < 0 || warn(rc, SQL_HANDLE_STMT, stmt) < 0) {
|
|
679
789
|
return NULL;
|
|
680
790
|
}
|
|
681
791
|
|
|
@@ -738,6 +848,7 @@ static PyObject *fetch_row(PyObject *self, PyObject *args) {
|
|
|
738
848
|
|
|
739
849
|
static PyMethodDef methods[] = {
|
|
740
850
|
{"set_error_factory", set_error_factory, METH_O, NULL},
|
|
851
|
+
{"set_warning_handler", set_warning_handler, METH_O, NULL},
|
|
741
852
|
{"alloc_handle", alloc_handle, METH_VARARGS, NULL},
|
|
742
853
|
{"free_handle", free_handle, METH_VARARGS, NULL},
|
|
743
854
|
{"connect", connect, 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
|
)
|
|
@@ -360,6 +362,31 @@ def test_error_fields(conn):
|
|
|
360
362
|
assert (e.sqlstate, e.table_name, e.cursor_name) == (None, None, None)
|
|
361
363
|
|
|
362
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
|
+
|
|
363
390
|
def test_error_classes():
|
|
364
391
|
assert isinstance(vsql.error("42P01", "x"), vsql.ProgrammingError)
|
|
365
392
|
assert isinstance(vsql.error("23502", "x"), vsql.IntegrityError)
|
|
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
|
|
File without changes
|