Skip to content

Commit c92e604

Browse files
committed
Add optional use of sqlite3_open_v2, sqlite3_close_v2 and sqlite3_prepare_v2
1 parent 6265842 commit c92e604

File tree

6 files changed

+42
-8
lines changed

6 files changed

+42
-8
lines changed

Modules/_sqlite/connection.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,12 @@ int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject
118118
return -1;
119119
}
120120
Py_BEGIN_ALLOW_THREADS
121+
#ifdef HAVE_SQLITE3_OPEN_V2
122+
rc = sqlite3_open_v2(database, &self->db,
123+
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
124+
#else
121125
rc = sqlite3_open(database, &self->db);
126+
#endif
122127
#endif
123128
Py_END_ALLOW_THREADS
124129

@@ -241,7 +246,7 @@ void pysqlite_connection_dealloc(pysqlite_Connection* self)
241246
/* Clean up if user has not called .close() explicitly. */
242247
if (self->db) {
243248
Py_BEGIN_ALLOW_THREADS
244-
sqlite3_close(self->db);
249+
SQLITE3_CLOSE(self->db);
245250
Py_END_ALLOW_THREADS
246251
}
247252

@@ -334,7 +339,7 @@ PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
334339

335340
if (self->db) {
336341
Py_BEGIN_ALLOW_THREADS
337-
rc = sqlite3_close(self->db);
342+
rc = SQLITE3_CLOSE(self->db);
338343
Py_END_ALLOW_THREADS
339344

340345
if (rc != SQLITE_OK) {
@@ -375,7 +380,7 @@ PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
375380
sqlite3_stmt* statement;
376381

377382
Py_BEGIN_ALLOW_THREADS
378-
rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
383+
rc = SQLITE3_PREPARE(self->db, self->begin_statement, -1, &statement, &tail);
379384
Py_END_ALLOW_THREADS
380385

381386
if (rc != SQLITE_OK) {
@@ -417,7 +422,7 @@ PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
417422
if (!sqlite3_get_autocommit(self->db)) {
418423

419424
Py_BEGIN_ALLOW_THREADS
420-
rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
425+
rc = SQLITE3_PREPARE(self->db, "COMMIT", -1, &statement, &tail);
421426
Py_END_ALLOW_THREADS
422427
if (rc != SQLITE_OK) {
423428
_pysqlite_seterror(self->db, NULL);
@@ -460,7 +465,7 @@ PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args
460465
pysqlite_do_all_statements(self, ACTION_RESET, 1);
461466

462467
Py_BEGIN_ALLOW_THREADS
463-
rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
468+
rc = SQLITE3_PREPARE(self->db, "ROLLBACK", -1, &statement, &tail);
464469
Py_END_ALLOW_THREADS
465470
if (rc != SQLITE_OK) {
466471
_pysqlite_seterror(self->db, NULL);

Modules/_sqlite/cursor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
706706

707707
while (1) {
708708
Py_BEGIN_ALLOW_THREADS
709-
rc = sqlite3_prepare(self->connection->db,
709+
rc = SQLITE3_PREPARE(self->connection->db,
710710
script_cstr,
711711
-1,
712712
&statement,

Modules/_sqlite/statement.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
9393
}
9494

9595
Py_BEGIN_ALLOW_THREADS
96-
rc = sqlite3_prepare(connection->db,
96+
rc = SQLITE3_PREPARE(connection->db,
9797
sql_cstr,
9898
-1,
9999
&self->st,
@@ -334,7 +334,7 @@ int pysqlite_statement_recompile(pysqlite_Statement* self, PyObject* params)
334334
}
335335

336336
Py_BEGIN_ALLOW_THREADS
337-
rc = sqlite3_prepare(self->db,
337+
rc = SQLITE3_PREPARE(self->db,
338338
sql_cstr,
339339
-1,
340340
&new_st,

Modules/_sqlite/util.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,16 @@ int _pysqlite_seterror(sqlite3* db, sqlite3_stmt* st);
3939
PyObject * _pysqlite_long_from_int64(sqlite_int64 value);
4040
sqlite_int64 _pysqlite_long_as_int64(PyObject * value);
4141

42+
#ifdef HAVE_SQLITE3_PREPARE_V2
43+
#define SQLITE3_PREPARE sqlite3_prepare_v2
44+
#else
45+
#define SQLITE3_PREPARE sqlite3_prepare
46+
#endif
47+
48+
#ifdef HAVE_SQLITE3_CLOSE_V2
49+
#define SQLITE3_CLOSE sqlite3_close_v2
50+
#else
51+
#define SQLITE3_CLOSE sqlite3_close
52+
#endif
53+
4254
#endif

configure.ac

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2854,6 +2854,14 @@ AC_ARG_ENABLE(loadable-sqlite-extensions,
28542854

28552855
AC_MSG_RESULT($enable_loadable_sqlite_extensions)
28562856

2857+
# Check for support of sqlite3 _v2 interface
2858+
AC_CHECK_LIB(sqlite3, sqlite3_prepare_v2,
2859+
AC_DEFINE(HAVE_SQLITE3_PREPARE_V2, 1, [Define if the sqlite3 library has sqlite3_prepare_v2]))
2860+
AC_CHECK_LIB(sqlite3, sqlite3_open_v2,
2861+
AC_DEFINE(HAVE_SQLITE3_OPEN_V2, 1, [Define if the sqlite3 library has sqlite3_open_v2]))
2862+
AC_CHECK_LIB(sqlite3, sqlite3_close_v2,
2863+
AC_DEFINE(HAVE_SQLITE3_CLOSE_V2, 1, [Define if the sqlite3 library has sqlite3_close_v2]))
2864+
28572865
# Check for --with-tcltk-includes=path and --with-tcltk-libs=path
28582866
AC_SUBST(TCLTK_INCLUDES)
28592867
AC_SUBST(TCLTK_LIBS)

pyconfig.h.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,15 @@
898898
/* Define to 1 if you have the <spawn.h> header file. */
899899
#undef HAVE_SPAWN_H
900900

901+
/* Define if the sqlite3 library has sqlite3_close_v2 */
902+
#undef HAVE_SQLITE3_CLOSE_V2
903+
904+
/* Define if the sqlite3 library has sqlite3_open_v2 */
905+
#undef HAVE_SQLITE3_OPEN_V2
906+
907+
/* Define if the sqlite3 library has sqlite3_prepare_v2 */
908+
#undef HAVE_SQLITE3_PREPARE_V2
909+
901910
/* Define if your compiler provides ssize_t */
902911
#undef HAVE_SSIZE_T
903912

0 commit comments

Comments
 (0)