Skip to content
File

Blob: patches/sqlite/0003-sqlite-complete-early-exit.patch

2.1 KB
1--- sqlite-src-pristine/src/complete.c 2023-11-01 22:31:37
2+++ sqlite-src-modified/src/complete.c 2024-03-14 12:51:32
3@@ -99,14 +99,21 @@
4 **
5 ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
6 ** to recognize the end of a trigger can be omitted. All we have to do
7 ** is look for a semicolon that is not part of an string or comment.
8 */
9-int sqlite3_complete(const char *zSql){
10+int sqlite3_complete(const char *zSql) {
11+ return sqlite3_complete_length(zSql, 0) != 0;
12+}
13+
14+int sqlite3_complete_length(const char *zSql, int firstOnly){
15 u8 state = 0; /* Current state, using numbers defined in header comment */
16 u8 token; /* Value of the next token */
17 
18+ /* Store the original pointer for later index calculations */
19+ const char *initialZSql = zSql;
20+
21 #ifndef SQLITE_OMIT_TRIGGER
22 /* A complex statement machine used to detect the end of a CREATE TRIGGER
23 ** statement. This is the normal case.
24 */
25 static const u8 trans[8][8] = {
26@@ -254,12 +261,16 @@
27 break;
28 }
29 }
30 state = trans[state][token];
31 zSql++;
32+
33+ // Once we have found a single complete statement, return its length
34+ if (firstOnly && state==1) return zSql - initialZSql;
35 }
36- return state==1;
37+ // Otherwise only return the length if the entire input is valid
38+ return state==1 ? zSql - initialZSql : 0;
39 }
40 
41 #ifndef SQLITE_OMIT_UTF16
42 /*
43 ** This routine is the same as the sqlite3_complete() routine described
44diff -u5 -r sqlite-src-pristine/src/sqlite.h.in sqlite-src-modified/src/sqlite.h.in
45--- sqlite-src-pristine/src/sqlite.h.in 2023-11-01 22:31:37
46+++ sqlite-src-modified/src/sqlite.h.in 2024-03-12 12:48:37
47@@ -2775,10 +2775,14 @@
48 ** UTF-16 string in native byte order.
49 */
50 int sqlite3_complete(const char *sql);
51 int sqlite3_complete16(const void *sql);
52 
53+// workerd addition: measure the length of the first (or all) complete
54+// statements in the input.
55+int sqlite3_complete_length(const char *sql, int firstOnly);
56+
57 /*
58 ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
59 ** KEYWORDS: {busy-handler callback} {busy handler}
60 ** METHOD: sqlite3
61 **