Skip to content

Commit a677971

Browse files
author
Erlend Egeberg Aasland
authored
bpo-45041: Simplify sqlite3.Cursor.executescript() (pythonGH-28020)
1 parent 771a546 commit a677971

File tree

1 file changed

+25
-42
lines changed

1 file changed

+25
-42
lines changed

Modules/_sqlite/cursor.c

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -721,19 +721,13 @@ pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
721721
const char *sql_script)
722722
/*[clinic end generated code: output=8fd726dde1c65164 input=1ac0693dc8db02a8]*/
723723
{
724-
_Py_IDENTIFIER(commit);
725-
sqlite3_stmt* statement;
726-
int rc;
727-
size_t sql_len;
728-
PyObject* result;
729-
730724
if (!check_cursor(self)) {
731725
return NULL;
732726
}
733727

734728
self->reset = 0;
735729

736-
sql_len = strlen(sql_script);
730+
size_t sql_len = strlen(sql_script);
737731
int max_length = sqlite3_limit(self->connection->db,
738732
SQLITE_LIMIT_LENGTH, -1);
739733
if (sql_len >= (unsigned)max_length) {
@@ -742,47 +736,37 @@ pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
742736
return NULL;
743737
}
744738

745-
/* commit first */
746-
result = _PyObject_CallMethodIdNoArgs((PyObject *)self->connection, &PyId_commit);
747-
if (!result) {
748-
goto error;
749-
}
750-
Py_DECREF(result);
751-
752-
pysqlite_state *state = self->connection->state;
753-
while (1) {
754-
const char *tail;
739+
// Commit if needed
740+
sqlite3 *db = self->connection->db;
741+
if (!sqlite3_get_autocommit(db)) {
742+
int rc = SQLITE_OK;
755743

756744
Py_BEGIN_ALLOW_THREADS
757-
rc = sqlite3_prepare_v2(self->connection->db,
758-
sql_script,
759-
(int)sql_len + 1,
760-
&statement,
761-
&tail);
745+
rc = sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
762746
Py_END_ALLOW_THREADS
747+
763748
if (rc != SQLITE_OK) {
764-
_pysqlite_seterror(state, self->connection->db);
765749
goto error;
766750
}
751+
}
767752

768-
/* execute statement, and ignore results of SELECT statements */
769-
do {
770-
rc = pysqlite_step(statement);
771-
if (PyErr_Occurred()) {
772-
(void)sqlite3_finalize(statement);
773-
goto error;
774-
}
775-
} while (rc == SQLITE_ROW);
753+
while (1) {
754+
int rc;
755+
const char *tail;
776756

777-
if (rc != SQLITE_DONE) {
778-
(void)sqlite3_finalize(statement);
779-
_pysqlite_seterror(state, self->connection->db);
780-
goto error;
757+
Py_BEGIN_ALLOW_THREADS
758+
sqlite3_stmt *stmt;
759+
rc = sqlite3_prepare_v2(db, sql_script, (int)sql_len + 1, &stmt,
760+
&tail);
761+
if (rc == SQLITE_OK) {
762+
do {
763+
(void)sqlite3_step(stmt);
764+
} while (rc == SQLITE_ROW);
765+
rc = sqlite3_finalize(stmt);
781766
}
767+
Py_END_ALLOW_THREADS
782768

783-
rc = sqlite3_finalize(statement);
784769
if (rc != SQLITE_OK) {
785-
_pysqlite_seterror(state, self->connection->db);
786770
goto error;
787771
}
788772

@@ -793,12 +777,11 @@ pysqlite_cursor_executescript_impl(pysqlite_Cursor *self,
793777
sql_script = tail;
794778
}
795779

780+
return Py_NewRef((PyObject *)self);
781+
796782
error:
797-
if (PyErr_Occurred()) {
798-
return NULL;
799-
} else {
800-
return Py_NewRef((PyObject *)self);
801-
}
783+
_pysqlite_seterror(self->connection->state, db);
784+
return NULL;
802785
}
803786

804787
static PyObject *

0 commit comments

Comments
 (0)