vsql-python 1.2.2__tar.gz → 1.2.5__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.
Files changed (29) hide show
  1. {vsql_python-1.2.2 → vsql_python-1.2.5}/PKG-INFO +25 -4
  2. {vsql_python-1.2.2 → vsql_python-1.2.5}/README.md +24 -3
  3. {vsql_python-1.2.2 → vsql_python-1.2.5}/src/vsql/__init__.py +5 -0
  4. {vsql_python-1.2.2 → vsql_python-1.2.5}/src/vsql/_connection.py +6 -0
  5. {vsql_python-1.2.2 → vsql_python-1.2.5}/src/vsql/_convert.py +7 -2
  6. {vsql_python-1.2.2 → vsql_python-1.2.5}/src/vsql/_errors.py +46 -3
  7. {vsql_python-1.2.2 → vsql_python-1.2.5}/src/vsql/_libvsql.c +119 -8
  8. {vsql_python-1.2.2 → vsql_python-1.2.5}/src/vsql_python.egg-info/PKG-INFO +25 -4
  9. {vsql_python-1.2.2 → vsql_python-1.2.5}/src/vsql_python.egg-info/scm_version.json +2 -2
  10. {vsql_python-1.2.2 → vsql_python-1.2.5}/tests/test_vsql.py +37 -0
  11. {vsql_python-1.2.2 → vsql_python-1.2.5}/.github/workflows/release.yml +0 -0
  12. {vsql_python-1.2.2 → vsql_python-1.2.5}/.github/workflows/test.yml +0 -0
  13. {vsql_python-1.2.2 → vsql_python-1.2.5}/.gitignore +0 -0
  14. {vsql_python-1.2.2 → vsql_python-1.2.5}/LICENSE +0 -0
  15. {vsql_python-1.2.2 → vsql_python-1.2.5}/MANIFEST.in +0 -0
  16. {vsql_python-1.2.2 → vsql_python-1.2.5}/example/main.py +0 -0
  17. {vsql_python-1.2.2 → vsql_python-1.2.5}/internal/amalgamation/LICENSE +0 -0
  18. {vsql_python-1.2.2 → vsql_python-1.2.5}/internal/amalgamation/LICENSE-V +0 -0
  19. {vsql_python-1.2.2 → vsql_python-1.2.5}/internal/amalgamation/libvsql.c +0 -0
  20. {vsql_python-1.2.2 → vsql_python-1.2.5}/internal/amalgamation/libvsql.h +0 -0
  21. {vsql_python-1.2.2 → vsql_python-1.2.5}/libvsql_flags.json +0 -0
  22. {vsql_python-1.2.2 → vsql_python-1.2.5}/pyproject.toml +0 -0
  23. {vsql_python-1.2.2 → vsql_python-1.2.5}/scripts/update-amalgamation.sh +0 -0
  24. {vsql_python-1.2.2 → vsql_python-1.2.5}/setup.cfg +0 -0
  25. {vsql_python-1.2.2 → vsql_python-1.2.5}/setup.py +0 -0
  26. {vsql_python-1.2.2 → vsql_python-1.2.5}/src/vsql_python.egg-info/SOURCES.txt +0 -0
  27. {vsql_python-1.2.2 → vsql_python-1.2.5}/src/vsql_python.egg-info/dependency_links.txt +0 -0
  28. {vsql_python-1.2.2 → vsql_python-1.2.5}/src/vsql_python.egg-info/scm_file_list.json +0 -0
  29. {vsql_python-1.2.2 → vsql_python-1.2.5}/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.2
3
+ Version: 1.2.5
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
@@ -140,9 +140,11 @@ The `type_code` in `cursor.description` is the name of the type (such as
140
140
  `vsql.NUMBER`, `vsql.STRING`, `vsql.BINARY` or `vsql.DATETIME` (which includes
141
141
  the intervals). The `internal_size` is the length of a string (in octets, 4 for
142
142
  each character) or a binary string, the precision of a number, or the length of
143
- a datetime or interval. The `precision` and `scale` are those of a `DECIMAL`
144
- or `NUMERIC`, the precision of a `DECFLOAT` (16 or 34), or the leading field
145
- precision and the fractional seconds precision of an interval.
143
+ a datetime or interval (which is also its `display_size`). The `precision` and
144
+ `scale` are those of a `DECIMAL` or `NUMERIC`, the precision of a `DECFLOAT`
145
+ (16 or 34), or the leading field precision and the fractional seconds
146
+ precision of an interval. The `scale` of a `TIME` or `TIMESTAMP` is its
147
+ fractional seconds precision.
146
148
 
147
149
  ## Errors
148
150
 
@@ -184,6 +186,25 @@ except vsql.IntegrityError as e:
184
186
  | `XX` internal error | `InternalError` |
185
187
  | anything else | `DatabaseError` |
186
188
 
189
+ `e.diagnostics` is a tuple of `(sqlstate, message)` for every condition that
190
+ vsql raised, starting with the error itself.
191
+
192
+ ### Warnings
193
+
194
+ A statement that completes with a warning (an SQLSTATE of class `01`) issues a
195
+ `vsql.Warning` with `warnings.warn`, so it's printed once by default, and can be
196
+ filtered or turned into an error like any other warning:
197
+
198
+ ```python
199
+ cur.execute("SELECT COUNT(x) FROM foo")
200
+ # Warning: null value eliminated in set function (SQLSTATE 01003)
201
+
202
+ warnings.simplefilter("error", vsql.Warning) # raise them instead
203
+ ```
204
+
205
+ `vsql.Warning` is a subclass of Python's `Warning` (which PEP 249 requires to
206
+ be an `Exception`), with the `sqlstate` and `message`.
207
+
187
208
  ## Versions
188
209
 
189
210
  `vsql.vsql_version` is the version of vsql that's compiled into the package
@@ -121,9 +121,11 @@ The `type_code` in `cursor.description` is the name of the type (such as
121
121
  `vsql.NUMBER`, `vsql.STRING`, `vsql.BINARY` or `vsql.DATETIME` (which includes
122
122
  the intervals). The `internal_size` is the length of a string (in octets, 4 for
123
123
  each character) or a binary string, the precision of a number, or the length of
124
- a datetime or interval. The `precision` and `scale` are those of a `DECIMAL`
125
- or `NUMERIC`, the precision of a `DECFLOAT` (16 or 34), or the leading field
126
- precision and the fractional seconds precision of an interval.
124
+ a datetime or interval (which is also its `display_size`). The `precision` and
125
+ `scale` are those of a `DECIMAL` or `NUMERIC`, the precision of a `DECFLOAT`
126
+ (16 or 34), or the leading field precision and the fractional seconds
127
+ precision of an interval. The `scale` of a `TIME` or `TIMESTAMP` is its
128
+ fractional seconds precision.
127
129
 
128
130
  ## Errors
129
131
 
@@ -165,6 +167,25 @@ except vsql.IntegrityError as e:
165
167
  | `XX` internal error | `InternalError` |
166
168
  | anything else | `DatabaseError` |
167
169
 
170
+ `e.diagnostics` is a tuple of `(sqlstate, message)` for every condition that
171
+ vsql raised, starting with the error itself.
172
+
173
+ ### Warnings
174
+
175
+ A statement that completes with a warning (an SQLSTATE of class `01`) issues a
176
+ `vsql.Warning` with `warnings.warn`, so it's printed once by default, and can be
177
+ filtered or turned into an error like any other warning:
178
+
179
+ ```python
180
+ cur.execute("SELECT COUNT(x) FROM foo")
181
+ # Warning: null value eliminated in set function (SQLSTATE 01003)
182
+
183
+ warnings.simplefilter("error", vsql.Warning) # raise them instead
184
+ ```
185
+
186
+ `vsql.Warning` is a subclass of Python's `Warning` (which PEP 249 requires to
187
+ be an `Exception`), with the `sqlstate` and `message`.
188
+
168
189
  ## Versions
169
190
 
170
191
  `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):
@@ -414,6 +414,12 @@ def _column(name, type_name, size, digits, nullable, leading=None):
414
414
  precision, scale = size, digits
415
415
  elif type_name == "DECFLOAT":
416
416
  precision = size
417
+ elif type_name.startswith(("TIME", "DATE")):
418
+ # The size is the length of the datetime in positions, and the scale
419
+ # is the fractional seconds precision (a DATE has none).
420
+ display_size = size
421
+ if type_name != "DATE":
422
+ scale = digits
417
423
  elif leading is not None:
418
424
  # An interval's precision is the leading field precision, and its scale
419
425
  # is the fractional seconds precision (if it ends with SECOND). The
@@ -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
- return (lib.SQL_CHAR, lib.SQL_VARCHAR, max(len(value), 1), 0, value.encode())
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
- class Warning(Exception): # noqa: A001 (PEP 249 requires this name)
8
- """An important warning, such as data truncation."""
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
- if (fields != NULL) {
154
- raise_error_fields((const char *)sqlstate, (const char *)message, length, fields);
155
- Py_DECREF(fields);
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
- CALL(SQL_HANDLE_DBC, dbc, SQLEndTran(SQL_HANDLE_DBC, dbc, commit ? SQL_COMMIT : SQL_ROLLBACK));
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
- CALL(SQL_HANDLE_STMT, stmt, SQLExecDirect(stmt, (SQLCHAR *)query, (SQLINTEGER)length));
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.2
3
+ Version: 1.2.5
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
@@ -140,9 +140,11 @@ The `type_code` in `cursor.description` is the name of the type (such as
140
140
  `vsql.NUMBER`, `vsql.STRING`, `vsql.BINARY` or `vsql.DATETIME` (which includes
141
141
  the intervals). The `internal_size` is the length of a string (in octets, 4 for
142
142
  each character) or a binary string, the precision of a number, or the length of
143
- a datetime or interval. The `precision` and `scale` are those of a `DECIMAL`
144
- or `NUMERIC`, the precision of a `DECFLOAT` (16 or 34), or the leading field
145
- precision and the fractional seconds precision of an interval.
143
+ a datetime or interval (which is also its `display_size`). The `precision` and
144
+ `scale` are those of a `DECIMAL` or `NUMERIC`, the precision of a `DECFLOAT`
145
+ (16 or 34), or the leading field precision and the fractional seconds
146
+ precision of an interval. The `scale` of a `TIME` or `TIMESTAMP` is its
147
+ fractional seconds precision.
146
148
 
147
149
  ## Errors
148
150
 
@@ -184,6 +186,25 @@ except vsql.IntegrityError as e:
184
186
  | `XX` internal error | `InternalError` |
185
187
  | anything else | `DatabaseError` |
186
188
 
189
+ `e.diagnostics` is a tuple of `(sqlstate, message)` for every condition that
190
+ vsql raised, starting with the error itself.
191
+
192
+ ### Warnings
193
+
194
+ A statement that completes with a warning (an SQLSTATE of class `01`) issues a
195
+ `vsql.Warning` with `warnings.warn`, so it's printed once by default, and can be
196
+ filtered or turned into an error like any other warning:
197
+
198
+ ```python
199
+ cur.execute("SELECT COUNT(x) FROM foo")
200
+ # Warning: null value eliminated in set function (SQLSTATE 01003)
201
+
202
+ warnings.simplefilter("error", vsql.Warning) # raise them instead
203
+ ```
204
+
205
+ `vsql.Warning` is a subclass of Python's `Warning` (which PEP 249 requires to
206
+ be an `Exception`), with the `sqlstate` and `message`.
207
+
187
208
  ## Versions
188
209
 
189
210
  `vsql.vsql_version` is the version of vsql that's compiled into the package
@@ -1,7 +1,7 @@
1
1
  {
2
- "tag": "1.2.2",
2
+ "tag": "1.2.5",
3
3
  "distance": 0,
4
- "node": "ga1b26c506cb771dff61db933708f100dac62d6c8",
4
+ "node": "g5917873b5d9dffba5c0be595991adf4a0676e8d9",
5
5
  "dirty": false,
6
6
  "branch": "HEAD",
7
7
  "node_date": "2026-11-27"
@@ -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
 
@@ -146,6 +147,16 @@ def test_description(conn):
146
147
  assert y[1:6] == ("INTERVAL YEAR TO MONTH", 7, 7, 3, None)
147
148
  assert s[1:6] == ("INTERVAL DAY TO SECOND", 18, 18, 4, 3)
148
149
 
150
+ # The scale of a TIME or TIMESTAMP is the fractional seconds precision.
151
+ conn.execute("""CREATE TABLE baz (d DATE, t TIME, tz TIME(6) WITH TIME ZONE,
152
+ ts TIMESTAMP(3), tsz TIMESTAMP WITH TIME ZONE)""")
153
+ d, t, tz, ts, tsz = conn.execute("SELECT * FROM baz").description
154
+ assert d[1:6] == ("DATE", 10, 10, None, None)
155
+ assert t[1:6] == ("TIME", 8, 8, None, 0)
156
+ assert tz[1:6] == ("TIME WITH TIME ZONE", 21, 21, None, 6)
157
+ assert ts[1:6] == ("TIMESTAMP", 23, 23, None, 3)
158
+ assert tsz[1:6] == ("TIMESTAMP WITH TIME ZONE", 32, 32, None, 6)
159
+
149
160
  # A statement that isn't a query has no description.
150
161
  conn.execute("DELETE FROM foo")
151
162
  cur = conn.execute("DELETE FROM foo")
@@ -245,6 +256,7 @@ def test_parameters_in_where(conn):
245
256
  ([vsql.YearMonthInterval(1)], vsql.ProgrammingError, "07006"),
246
257
  ([object()], vsql.ProgrammingError, "unsupported type"),
247
258
  ([decimal.Decimal("NaN")], vsql.DataError, "finite"),
259
+ (["\ud800"], vsql.DataError, "parameter 1: .* isn't valid Unicode"),
248
260
  ([], vsql.ProgrammingError, "dynamic parameter"),
249
261
  ],
250
262
  )
@@ -360,6 +372,31 @@ def test_error_fields(conn):
360
372
  assert (e.sqlstate, e.table_name, e.cursor_name) == (None, None, None)
361
373
 
362
374
 
375
+
376
+ def test_warnings(conn):
377
+ conn.execute("CREATE TABLE foo (x INTEGER)")
378
+ conn.execute("INSERT INTO foo (x) VALUES (1), (NULL)")
379
+ with pytest.warns(vsql.Warning) as record:
380
+ assert scalar(conn, "SELECT COUNT(x) FROM foo") == 1
381
+ assert [(w.message.sqlstate, w.message.message) for w in record] == [
382
+ ("01003", "null value eliminated in set function")]
383
+ assert record[0].filename == __file__
384
+ assert issubclass(vsql.Warning, Exception)
385
+
386
+ # A warning can be an error.
387
+ with warnings.catch_warnings():
388
+ warnings.simplefilter("error", vsql.Warning)
389
+ with pytest.raises(vsql.Warning) as e:
390
+ conn.execute("VALUES CAST('abc' AS VARCHAR(2))")
391
+ assert e.value.sqlstate == "01004"
392
+
393
+
394
+ def test_diagnostics(conn):
395
+ with pytest.raises(vsql.ProgrammingError) as e:
396
+ conn.execute("SELECT * FROM nope")
397
+ assert e.value.diagnostics == (("42P01", e.value.message),)
398
+ assert vsql.InterfaceError("x").diagnostics == ()
399
+
363
400
  def test_error_classes():
364
401
  assert isinstance(vsql.error("42P01", "x"), vsql.ProgrammingError)
365
402
  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