vsql-python 1.2.0__tar.gz → 1.2.3__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.0 → vsql_python-1.2.3}/PKG-INFO +1 -1
  2. {vsql_python-1.2.0 → vsql_python-1.2.3}/src/vsql/_connection.py +40 -13
  3. {vsql_python-1.2.0 → vsql_python-1.2.3}/src/vsql/_convert.py +7 -2
  4. {vsql_python-1.2.0 → vsql_python-1.2.3}/src/vsql/_libvsql.c +27 -3
  5. {vsql_python-1.2.0 → vsql_python-1.2.3}/src/vsql_python.egg-info/PKG-INFO +1 -1
  6. {vsql_python-1.2.0 → vsql_python-1.2.3}/src/vsql_python.egg-info/scm_version.json +2 -2
  7. {vsql_python-1.2.0 → vsql_python-1.2.3}/tests/test_vsql.py +43 -0
  8. {vsql_python-1.2.0 → vsql_python-1.2.3}/.github/workflows/release.yml +0 -0
  9. {vsql_python-1.2.0 → vsql_python-1.2.3}/.github/workflows/test.yml +0 -0
  10. {vsql_python-1.2.0 → vsql_python-1.2.3}/.gitignore +0 -0
  11. {vsql_python-1.2.0 → vsql_python-1.2.3}/LICENSE +0 -0
  12. {vsql_python-1.2.0 → vsql_python-1.2.3}/MANIFEST.in +0 -0
  13. {vsql_python-1.2.0 → vsql_python-1.2.3}/README.md +0 -0
  14. {vsql_python-1.2.0 → vsql_python-1.2.3}/example/main.py +0 -0
  15. {vsql_python-1.2.0 → vsql_python-1.2.3}/internal/amalgamation/LICENSE +0 -0
  16. {vsql_python-1.2.0 → vsql_python-1.2.3}/internal/amalgamation/LICENSE-V +0 -0
  17. {vsql_python-1.2.0 → vsql_python-1.2.3}/internal/amalgamation/libvsql.c +0 -0
  18. {vsql_python-1.2.0 → vsql_python-1.2.3}/internal/amalgamation/libvsql.h +0 -0
  19. {vsql_python-1.2.0 → vsql_python-1.2.3}/libvsql_flags.json +0 -0
  20. {vsql_python-1.2.0 → vsql_python-1.2.3}/pyproject.toml +0 -0
  21. {vsql_python-1.2.0 → vsql_python-1.2.3}/scripts/update-amalgamation.sh +0 -0
  22. {vsql_python-1.2.0 → vsql_python-1.2.3}/setup.cfg +0 -0
  23. {vsql_python-1.2.0 → vsql_python-1.2.3}/setup.py +0 -0
  24. {vsql_python-1.2.0 → vsql_python-1.2.3}/src/vsql/__init__.py +0 -0
  25. {vsql_python-1.2.0 → vsql_python-1.2.3}/src/vsql/_errors.py +0 -0
  26. {vsql_python-1.2.0 → vsql_python-1.2.3}/src/vsql_python.egg-info/SOURCES.txt +0 -0
  27. {vsql_python-1.2.0 → vsql_python-1.2.3}/src/vsql_python.egg-info/dependency_links.txt +0 -0
  28. {vsql_python-1.2.0 → vsql_python-1.2.3}/src/vsql_python.egg-info/scm_file_list.json +0 -0
  29. {vsql_python-1.2.0 → vsql_python-1.2.3}/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.0
3
+ Version: 1.2.3
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
@@ -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
- self._in_transaction = False
115
- lib.end_tran(self._dbc, commit)
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
- lib.end_savepoint(self._dbc, name, True)
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
- if params or operation == self._prepared:
246
- if operation != self._prepared:
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.prepare(self._stmt, operation)
249
- self._prepared = operation
250
- lib.execute(self._stmt, params)
251
- else:
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
- row = lib.fetch_row(self._stmt, self._kinds)
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
- 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():
@@ -323,6 +323,22 @@ static PyObject *end_savepoint(PyObject *self, PyObject *args) {
323
323
  Py_RETURN_NONE;
324
324
  }
325
325
 
326
+ // transaction_active(handle_type, handle) -> bool is whether a transaction was
327
+ // active after the last routine called with the handle
328
+ // (SQL_DIAG_TRANSACTION_ACTIVE).
329
+ static PyObject *transaction_active(PyObject *self, PyObject *args) {
330
+ int handle_type;
331
+ SQLINTEGER handle, active = 0;
332
+ if (!PyArg_ParseTuple(args, "ii", &handle_type, &handle)) {
333
+ return NULL;
334
+ }
335
+
336
+ CALL(handle_type, handle,
337
+ SQLGetDiagField((SQLSMALLINT)handle_type, handle, 0, SQL_DIAG_TRANSACTION_ACTIVE, &active,
338
+ 0, NULL));
339
+ return PyBool_FromLong(active);
340
+ }
341
+
326
342
  // exec_direct(stmt, query)
327
343
  static PyObject *exec_direct(PyObject *self, PyObject *args) {
328
344
  SQLINTEGER stmt;
@@ -570,10 +586,11 @@ static PyObject *get_string(SQLINTEGER stmt, SQLSMALLINT n) {
570
586
  Py_RETURN_NONE;
571
587
  }
572
588
 
573
- // The rest of the string (ind) didn't fit, so the buffer has a null
574
- // terminated piece of it.
589
+ // The rest of the string (ind) didn't fit, so the buffer is full with a
590
+ // null terminated piece of it. The piece is the whole buffer, since the
591
+ // string can have a NUL in it.
575
592
  if (rc == SQL_SUCCESS_WITH_INFO && ind >= size - (SQLINTEGER)length) {
576
- size_t piece = strlen(buf + length);
593
+ size_t piece = (size_t)size - length - 1;
577
594
  size_t new_size = length + piece + (size_t)ind + 1;
578
595
  char *grown = PyMem_Malloc(new_size);
579
596
  if (grown == NULL) {
@@ -590,6 +607,12 @@ static PyObject *get_string(SQLINTEGER stmt, SQLSMALLINT n) {
590
607
 
591
608
  PyObject *s = PyUnicode_DecodeUTF8(buf, (Py_ssize_t)(length + ind), NULL);
592
609
  PyMem_Free(large);
610
+ // vsql's strings are always UTF-8, but it should still be a DataError
611
+ // rather than a UnicodeDecodeError if one isn't.
612
+ if (s == NULL && PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
613
+ PyErr_Clear();
614
+ raise_error("22021", "character not in repertoire: the value isn't UTF-8", -1);
615
+ }
593
616
  return s;
594
617
  }
595
618
  }
@@ -729,6 +752,7 @@ static PyMethodDef methods[] = {
729
752
  {"start_tran", start_tran, METH_VARARGS, NULL},
730
753
  {"end_tran", end_tran, METH_VARARGS, NULL},
731
754
  {"end_savepoint", end_savepoint, METH_VARARGS, NULL},
755
+ {"transaction_active", transaction_active, METH_VARARGS, NULL},
732
756
  {"get_info", get_info, METH_VARARGS, NULL},
733
757
  {"exec_direct", exec_direct, METH_VARARGS, NULL},
734
758
  {"prepare", prepare, METH_VARARGS, NULL},
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: vsql-python
3
- Version: 1.2.0
3
+ Version: 1.2.3
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
@@ -1,7 +1,7 @@
1
1
  {
2
- "tag": "1.2.0",
2
+ "tag": "1.2.3",
3
3
  "distance": 0,
4
- "node": "g64f93fcf088912ead1fa9b18103c1f87bd234fd9",
4
+ "node": "gac273b770adeeaf3e7f3c57890169914c733b65d",
5
5
  "dirty": false,
6
6
  "branch": "HEAD",
7
7
  "node_date": "2026-11-27"
@@ -245,6 +245,7 @@ def test_parameters_in_where(conn):
245
245
  ([vsql.YearMonthInterval(1)], vsql.ProgrammingError, "07006"),
246
246
  ([object()], vsql.ProgrammingError, "unsupported type"),
247
247
  ([decimal.Decimal("NaN")], vsql.DataError, "finite"),
248
+ (["\ud800"], vsql.DataError, "parameter 1: .* isn't valid Unicode"),
248
249
  ([], vsql.ProgrammingError, "dynamic parameter"),
249
250
  ],
250
251
  )
@@ -299,6 +300,11 @@ def test_long_strings(conn):
299
300
  assert scalar(conn, "SELECT s FROM foo") == want
300
301
 
301
302
 
303
+ # A NUL is part of the string, even in a piece that isn't the last.
304
+ for want in ["a\x00b", "a\x00b" + "c" * 300, "c" * 300 + "\x00" + "é" * 300]:
305
+ assert scalar(conn, "SELECT CAST(? AS VARCHAR(1000))", [want]) == want
306
+
307
+
302
308
  @pytest.mark.parametrize(
303
309
  "query, error, sqlstate",
304
310
  [
@@ -520,6 +526,43 @@ def test_transactions(path):
520
526
  assert count() == 4
521
527
 
522
528
 
529
+
530
+ def test_transaction_rolled_back_by_vsql(path):
531
+ """A 40001 rolls back the whole transaction, so the next statement starts a
532
+ new one (which rollback undoes). Other errors only undo the statement."""
533
+
534
+ with vsql.connect(path) as conn:
535
+ conn.execute("CREATE TABLE t (x INTEGER PRIMARY KEY)")
536
+ conn.execute("INSERT INTO t (x) VALUES (1)")
537
+
538
+ a, b = vsql.connect(path), vsql.connect(path)
539
+ a.execute("UPDATE t SET x = 100 WHERE x = 1")
540
+ b.execute("INSERT INTO t (x) VALUES (50)")
541
+ cur = b.execute("SELECT x FROM t")
542
+ with pytest.raises(vsql.OperationalError) as e:
543
+ b.execute("UPDATE t SET x = 200 WHERE x = 1")
544
+ assert e.value.sqlstate == "40001"
545
+ assert not b.in_transaction
546
+ assert cur.fetchone() is None
547
+ a.commit()
548
+
549
+ b.execute("INSERT INTO t (x) VALUES (60)")
550
+ assert b.in_transaction
551
+ b.rollback()
552
+ assert not b.in_transaction
553
+
554
+ # A unique violation only undoes the statement.
555
+ b.execute("INSERT INTO t (x) VALUES (70)")
556
+ with pytest.raises(vsql.IntegrityError):
557
+ b.execute("INSERT INTO t (x) VALUES (70)")
558
+ assert b.in_transaction
559
+ b.commit()
560
+ a.close()
561
+ b.close()
562
+
563
+ with vsql.connect(path) as conn:
564
+ assert conn.execute("SELECT x FROM t ORDER BY x").fetchall() == [(70,), (100,)]
565
+
523
566
  def test_savepoints(path):
524
567
  conn = vsql.connect(path)
525
568
  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