Skip to content
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

bpo-35398: Improve DDL statement detection for SQLite #10913

Closed
wants to merge 5 commits into from
Closed
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
26 changes: 26 additions & 0 deletions Modules/_sqlite/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
const char* sql_cstr;
Py_ssize_t sql_cstr_len;
const char* p;
unsigned char multi_line_comment;
unsigned char single_line_comment;

self->st = NULL;
self->in_use = 0;
Expand All @@ -76,7 +78,31 @@ int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* con
/* Determine if the statement is a DML statement.
SELECT is the only exception. See #9924. */
self->is_dml = 0;
single_line_comment = 0;
multi_line_comment = 0;
for (p = sql_cstr; *p != 0; p++) {
// skip leading comments
if (single_line_comment && (*p == '\n')) {
single_line_comment = 0;
continue;
} else if (multi_line_comment && strncmp(p, "*/", 2) == 0) {
multi_line_comment = 0;
p++;
continue;
}
if (single_line_comment || multi_line_comment) {
continue;
}
// detect leading comments
if (strncmp(p, "--", 2) == 0) {
single_line_comment = 1;
continue;
} else if (strncmp(p, "/*", 2) == 0) {
multi_line_comment = 1;
p++;
continue;
}
// skip leading whitespace
switch (*p) {
case ' ':
case '\r':
Expand Down