vsql-python 1.2.0__tar.gz → 1.2.2__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.2}/PKG-INFO +1 -1
  2. {vsql_python-1.2.0 → vsql_python-1.2.2}/src/vsql/_connection.py +40 -13
  3. {vsql_python-1.2.0 → vsql_python-1.2.2}/src/vsql/_libvsql.c +21 -3
  4. {vsql_python-1.2.0 → vsql_python-1.2.2}/src/vsql_python.egg-info/PKG-INFO +1 -1
  5. {vsql_python-1.2.0 → vsql_python-1.2.2}/src/vsql_python.egg-info/scm_version.json +2 -2
  6. {vsql_python-1.2.0 → vsql_python-1.2.2}/tests/test_vsql.py +42 -0
  7. {vsql_python-1.2.0 → vsql_python-1.2.2}/.github/workflows/release.yml +0 -0
  8. {vsql_python-1.2.0 → vsql_python-1.2.2}/.github/workflows/test.yml +0 -0
  9. {vsql_python-1.2.0 → vsql_python-1.2.2}/.gitignore +0 -0
  10. {vsql_python-1.2.0 → vsql_python-1.2.2}/LICENSE +0 -0
  11. {vsql_python-1.2.0 → vsql_python-1.2.2}/MANIFEST.in +0 -0
  12. {vsql_python-1.2.0 → vsql_python-1.2.2}/README.md +0 -0
  13. {vsql_python-1.2.0 → vsql_python-1.2.2}/example/main.py +0 -0
  14. {vsql_python-1.2.0 → vsql_python-1.2.2}/internal/amalgamation/LICENSE +0 -0
  15. {vsql_python-1.2.0 → vsql_python-1.2.2}/internal/amalgamation/LICENSE-V +0 -0
  16. {vsql_python-1.2.0 → vsql_python-1.2.2}/internal/amalgamation/libvsql.c +0 -0
  17. {vsql_python-1.2.0 → vsql_python-1.2.2}/internal/amalgamation/libvsql.h +0 -0
  18. {vsql_python-1.2.0 → vsql_python-1.2.2}/libvsql_flags.json +0 -0
  19. {vsql_python-1.2.0 → vsql_python-1.2.2}/pyproject.toml +0 -0
  20. {vsql_python-1.2.0 → vsql_python-1.2.2}/scripts/update-amalgamation.sh +0 -0
  21. {vsql_python-1.2.0 → vsql_python-1.2.2}/setup.cfg +0 -0
  22. {vsql_python-1.2.0 → vsql_python-1.2.2}/setup.py +0 -0
  23. {vsql_python-1.2.0 → vsql_python-1.2.2}/src/vsql/__init__.py +0 -0
  24. {vsql_python-1.2.0 → vsql_python-1.2.2}/src/vsql/_convert.py +0 -0
  25. {vsql_python-1.2.0 → vsql_python-1.2.2}/src/vsql/_errors.py +0 -0
  26. {vsql_python-1.2.0 → vsql_python-1.2.2}/src/vsql_python.egg-info/SOURCES.txt +0 -0
  27. {vsql_python-1.2.0 → vsql_python-1.2.2}/src/vsql_python.egg-info/dependency_links.txt +0 -0
  28. {vsql_python-1.2.0 → vsql_python-1.2.2}/src/vsql_python.egg-info/scm_file_list.json +0 -0
  29. {vsql_python-1.2.0 → vsql_python-1.2.2}/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.2
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
@@ -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) {
@@ -729,6 +746,7 @@ static PyMethodDef methods[] = {
729
746
  {"start_tran", start_tran, METH_VARARGS, NULL},
730
747
  {"end_tran", end_tran, METH_VARARGS, NULL},
731
748
  {"end_savepoint", end_savepoint, METH_VARARGS, NULL},
749
+ {"transaction_active", transaction_active, METH_VARARGS, NULL},
732
750
  {"get_info", get_info, METH_VARARGS, NULL},
733
751
  {"exec_direct", exec_direct, METH_VARARGS, NULL},
734
752
  {"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.2
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.2",
3
3
  "distance": 0,
4
- "node": "g64f93fcf088912ead1fa9b18103c1f87bd234fd9",
4
+ "node": "ga1b26c506cb771dff61db933708f100dac62d6c8",
5
5
  "dirty": false,
6
6
  "branch": "HEAD",
7
7
  "node_date": "2026-11-27"
@@ -299,6 +299,11 @@ def test_long_strings(conn):
299
299
  assert scalar(conn, "SELECT s FROM foo") == want
300
300
 
301
301
 
302
+ # A NUL is part of the string, even in a piece that isn't the last.
303
+ for want in ["a\x00b", "a\x00b" + "c" * 300, "c" * 300 + "\x00" + "é" * 300]:
304
+ assert scalar(conn, "SELECT CAST(? AS VARCHAR(1000))", [want]) == want
305
+
306
+
302
307
  @pytest.mark.parametrize(
303
308
  "query, error, sqlstate",
304
309
  [
@@ -520,6 +525,43 @@ def test_transactions(path):
520
525
  assert count() == 4
521
526
 
522
527
 
528
+
529
+ def test_transaction_rolled_back_by_vsql(path):
530
+ """A 40001 rolls back the whole transaction, so the next statement starts a
531
+ new one (which rollback undoes). Other errors only undo the statement."""
532
+
533
+ with vsql.connect(path) as conn:
534
+ conn.execute("CREATE TABLE t (x INTEGER PRIMARY KEY)")
535
+ conn.execute("INSERT INTO t (x) VALUES (1)")
536
+
537
+ a, b = vsql.connect(path), vsql.connect(path)
538
+ a.execute("UPDATE t SET x = 100 WHERE x = 1")
539
+ b.execute("INSERT INTO t (x) VALUES (50)")
540
+ cur = b.execute("SELECT x FROM t")
541
+ with pytest.raises(vsql.OperationalError) as e:
542
+ b.execute("UPDATE t SET x = 200 WHERE x = 1")
543
+ assert e.value.sqlstate == "40001"
544
+ assert not b.in_transaction
545
+ assert cur.fetchone() is None
546
+ a.commit()
547
+
548
+ b.execute("INSERT INTO t (x) VALUES (60)")
549
+ assert b.in_transaction
550
+ b.rollback()
551
+ assert not b.in_transaction
552
+
553
+ # A unique violation only undoes the statement.
554
+ b.execute("INSERT INTO t (x) VALUES (70)")
555
+ with pytest.raises(vsql.IntegrityError):
556
+ b.execute("INSERT INTO t (x) VALUES (70)")
557
+ assert b.in_transaction
558
+ b.commit()
559
+ a.close()
560
+ b.close()
561
+
562
+ with vsql.connect(path) as conn:
563
+ assert conn.execute("SELECT x FROM t ORDER BY x").fetchall() == [(70,), (100,)]
564
+
523
565
  def test_savepoints(path):
524
566
  conn = vsql.connect(path)
525
567
  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