Visitar URL original
bpo-9303: Migrate sqlite3 module to _v2 API to enhance performance by palaviv · Pull Request #359 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add optional use of sqlite3_open_v2, sqlite3_close_v2 and sqlite3_pre…
…pare_v2
  • Loading branch information
palaviv committed Feb 28, 2017
commit c92e6047bde01bcb846ad1fcf4126d3001e30f4b
15 changes: 10 additions & 5 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject
return -1;
}
Py_BEGIN_ALLOW_THREADS
#ifdef HAVE_SQLITE3_OPEN_V2
rc = sqlite3_open_v2(database, &self->db,

@berkerpeksag berkerpeksag Feb 28, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that there is an opportunity to simplify this whole SQLITE_OPEN_URI and HAVE_SQLITE3_OPEN_V2 dance.

(The use of Py_BEGIN_ALLOW_THREADS is a bit weird in line 111 and line 120.)

SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nitpick: FOO|BAR is more common than FOO | BAR in sqlite3 code base.

#else
rc = sqlite3_open(database, &self->db);
#endif
#endif
Py_END_ALLOW_THREADS

Expand Down Expand Up @@ -241,7 +246,7 @@ void pysqlite_connection_dealloc(pysqlite_Connection* self)
/* Clean up if user has not called .close() explicitly. */
if (self->db) {
Py_BEGIN_ALLOW_THREADS
sqlite3_close(self->db);
SQLITE3_CLOSE(self->db);
Py_END_ALLOW_THREADS
}

Expand Down Expand Up @@ -334,7 +339,7 @@ PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)

if (self->db) {
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_close(self->db);
rc = SQLITE3_CLOSE(self->db);
Py_END_ALLOW_THREADS

if (rc != SQLITE_OK) {
Expand Down Expand Up @@ -375,7 +380,7 @@ PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
sqlite3_stmt* statement;

Py_BEGIN_ALLOW_THREADS
rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
rc = SQLITE3_PREPARE(self->db, self->begin_statement, -1, &statement, &tail);
Py_END_ALLOW_THREADS

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

Py_BEGIN_ALLOW_THREADS
rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
rc = SQLITE3_PREPARE(self->db, "COMMIT", -1, &statement, &tail);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
_pysqlite_seterror(self->db, NULL);
Expand Down Expand Up @@ -460,7 +465,7 @@ PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args
pysqlite_do_all_statements(self, ACTION_RESET, 1);

Py_BEGIN_ALLOW_THREADS
rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
rc = SQLITE3_PREPARE(self->db, "ROLLBACK", -1, &statement, &tail);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
_pysqlite_seterror(self->db, NULL);
Expand Down
2 changes: 1 addition & 1 deletion Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)

while (1) {
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_prepare(self->connection->db,
rc = SQLITE3_PREPARE(self->connection->db,
script_cstr,
-1,
&statement,
Expand Down
4 changes: 2 additions & 2 deletions Modules/_sqlite/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
}

Py_BEGIN_ALLOW_THREADS
rc = sqlite3_prepare(connection->db,
rc = SQLITE3_PREPARE(connection->db,
sql_cstr,
-1,
&self->st,
Expand Down Expand Up @@ -334,7 +334,7 @@ int pysqlite_statement_recompile(pysqlite_Statement* self, PyObject* params)
}

Py_BEGIN_ALLOW_THREADS
rc = sqlite3_prepare(self->db,
rc = SQLITE3_PREPARE(self->db,
sql_cstr,
-1,
&new_st,
Expand Down
12 changes: 12 additions & 0 deletions Modules/_sqlite/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,16 @@ int _pysqlite_seterror(sqlite3* db, sqlite3_stmt* st);
PyObject * _pysqlite_long_from_int64(sqlite_int64 value);
sqlite_int64 _pysqlite_long_as_int64(PyObject * value);

#ifdef HAVE_SQLITE3_PREPARE_V2
#define SQLITE3_PREPARE sqlite3_prepare_v2
#else
#define SQLITE3_PREPARE sqlite3_prepare
#endif

#ifdef HAVE_SQLITE3_CLOSE_V2
#define SQLITE3_CLOSE sqlite3_close_v2
#else
#define SQLITE3_CLOSE sqlite3_close
#endif

#endif
8 changes: 8 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2854,6 +2854,14 @@ AC_ARG_ENABLE(loadable-sqlite-extensions,

AC_MSG_RESULT($enable_loadable_sqlite_extensions)

# Check for support of sqlite3 _v2 interface
AC_CHECK_LIB(sqlite3, sqlite3_prepare_v2,
AC_DEFINE(HAVE_SQLITE3_PREPARE_V2, 1, [Define if the sqlite3 library has sqlite3_prepare_v2]))
AC_CHECK_LIB(sqlite3, sqlite3_open_v2,
AC_DEFINE(HAVE_SQLITE3_OPEN_V2, 1, [Define if the sqlite3 library has sqlite3_open_v2]))
AC_CHECK_LIB(sqlite3, sqlite3_close_v2,
AC_DEFINE(HAVE_SQLITE3_CLOSE_V2, 1, [Define if the sqlite3 library has sqlite3_close_v2]))

# Check for --with-tcltk-includes=path and --with-tcltk-libs=path
AC_SUBST(TCLTK_INCLUDES)
AC_SUBST(TCLTK_LIBS)
Expand Down
9 changes: 9 additions & 0 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,15 @@
/* Define to 1 if you have the <spawn.h> header file. */
#undef HAVE_SPAWN_H

/* Define if the sqlite3 library has sqlite3_close_v2 */
#undef HAVE_SQLITE3_CLOSE_V2

/* Define if the sqlite3 library has sqlite3_open_v2 */
#undef HAVE_SQLITE3_OPEN_V2

/* Define if the sqlite3 library has sqlite3_prepare_v2 */
#undef HAVE_SQLITE3_PREPARE_V2

/* Define if your compiler provides ssize_t */
#undef HAVE_SSIZE_T

Expand Down