Skip to content

bpo-44165: optimise sqlite3 statement preparation by passing string size #26206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ pysqlite_connection_commit_impl(pysqlite_Connection *self)
if (!sqlite3_get_autocommit(self->db)) {

Py_BEGIN_ALLOW_THREADS
rc = sqlite3_prepare_v2(self->db, "COMMIT", -1, &statement, NULL);
rc = sqlite3_prepare_v2(self->db, "COMMIT", 7, &statement, NULL);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
_pysqlite_seterror(self->db);
Expand Down Expand Up @@ -478,7 +478,7 @@ pysqlite_connection_rollback_impl(pysqlite_Connection *self)
pysqlite_do_all_statements(self, ACTION_RESET, 1);

Py_BEGIN_ALLOW_THREADS
rc = sqlite3_prepare_v2(self->db, "ROLLBACK", -1, &statement, NULL);
rc = sqlite3_prepare_v2(self->db, "ROLLBACK", 9, &statement, NULL);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
_pysqlite_seterror(self->db);
Expand Down
20 changes: 16 additions & 4 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
const char* script_cstr;
sqlite3_stmt* statement;
int rc;
Py_ssize_t sql_len;
PyObject* result;

if (!check_cursor(self)) {
Expand All @@ -690,10 +691,17 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
self->reset = 0;

if (PyUnicode_Check(script_obj)) {
script_cstr = PyUnicode_AsUTF8(script_obj);
script_cstr = PyUnicode_AsUTF8AndSize(script_obj, &sql_len);
if (!script_cstr) {
return NULL;
}

int max_length = sqlite3_limit(self->connection->db,
SQLITE_LIMIT_LENGTH, -1);
if (sql_len >= max_length) {
PyErr_SetString(pysqlite_DataError, "query string is too large");
return NULL;
}
} else {
PyErr_SetString(PyExc_ValueError, "script argument must be unicode.");
return NULL;
Expand All @@ -707,12 +715,14 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
Py_DECREF(result);

while (1) {
const char *tail;

Py_BEGIN_ALLOW_THREADS
rc = sqlite3_prepare_v2(self->connection->db,
script_cstr,
-1,
(int)sql_len + 1,
&statement,
&script_cstr);
&tail);
Py_END_ALLOW_THREADS
if (rc != SQLITE_OK) {
_pysqlite_seterror(self->connection->db);
Expand Down Expand Up @@ -740,9 +750,11 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
goto error;
}

if (*script_cstr == (char)0) {
if (*tail == (char)0) {
break;
}
sql_len -= (tail - script_cstr);
script_cstr = tail;
}

error:
Expand Down
8 changes: 7 additions & 1 deletion Modules/_sqlite/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
rc = PYSQLITE_SQL_WRONG_TYPE;
return rc;
}

int max_length = sqlite3_limit(connection->db, SQLITE_LIMIT_LENGTH, -1);
if (sql_cstr_len >= max_length) {
PyErr_SetString(pysqlite_DataError, "query string is too large");
return PYSQLITE_TOO_MUCH_SQL;
}
if (strlen(sql_cstr) != (size_t)sql_cstr_len) {
PyErr_SetString(PyExc_ValueError, "the query contains a null character");
return PYSQLITE_SQL_WRONG_TYPE;
Expand Down Expand Up @@ -96,7 +102,7 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_prepare_v2(connection->db,
sql_cstr,
-1,
(int)sql_cstr_len + 1,
&self->st,
&tail);
Py_END_ALLOW_THREADS
Expand Down