The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * The dialects a SQL script can be split for. `sqlite` is split by |
| 3 | * `splitSqliteStatements` (in `SqlFile.ts`), which also understands trigger |
| 4 | * bodies. |
| 5 | */ |
| 6 | export type ScriptDialect = "postgres" | "mysql" | "googlesql"; |
| 7 | |
| 8 | /** One statement of a script, without its terminating `;`. */ |
| 9 | export interface ScannedStatement { |
| 10 | /** |
| 11 | * The statement's exact source slice, comments and surrounding whitespace |
| 12 | * included, so offsets into `code` index it directly. |
| 13 | */ |
| 14 | readonly sql: string; |
| 15 | /** The statement with comments removed: what to send to the database. */ |
| 16 | readonly text: string; |
| 17 | /** |
| 18 | * `sql` with comments blanked and every string, quoted identifier, and |
| 19 | * dollar-quoted body masked to `_` plus spaces. Offsets match `sql`, so a |
| 20 | * keyword found in `code` can be sliced out of `sql`. |
| 21 | */ |
| 22 | readonly code: string; |
| 23 | /** A comment or quoted span that never closed before the end of the script. */ |
| 24 | readonly unterminated?: "comment" | "quote"; |
| 25 | } |
| 26 | |
| 27 | interface Rules { |
| 28 | /** `#` starts a line comment. */ |
| 29 | readonly hashComments: boolean; |
| 30 | /** `--` is a comment only when followed by whitespace (MySQL). */ |
| 31 | readonly dashCommentNeedsSpace: boolean; |
| 32 | /** Block comments nest (PostgreSQL). */ |
| 33 | readonly nestedBlockComments: boolean; |
| 34 | /** `$tag$ … $tag$` bodies (PostgreSQL). */ |
| 35 | readonly dollarQuotes: boolean; |
| 36 | /** `'''…'''` and `"""…"""` (GoogleSQL). */ |
| 37 | readonly tripleQuotes: boolean; |
| 38 | /** Quote characters that open a string or quoted identifier. */ |
| 39 | readonly quotes: ReadonlyArray<string>; |
| 40 | /** Backslash escapes inside every quoted span. */ |
| 41 | readonly backslashEscapes: boolean; |
| 42 | } |
| 43 | |
| 44 | const RULES: Record<ScriptDialect, Rules> = { |
| 45 | postgres: { |
| 46 | hashComments: false, |
| 47 | dashCommentNeedsSpace: false, |
| 48 | nestedBlockComments: true, |
| 49 | dollarQuotes: true, |
| 50 | tripleQuotes: false, |
| 51 | quotes: ["'", '"'], |
| 52 | backslashEscapes: false, |
| 53 | }, |
| 54 | mysql: { |
| 55 | hashComments: true, |
| 56 | dashCommentNeedsSpace: true, |
| 57 | nestedBlockComments: false, |
| 58 | dollarQuotes: false, |
| 59 | tripleQuotes: false, |
| 60 | quotes: ["'", '"', "`"], |
| 61 | backslashEscapes: true, |
| 62 | }, |
| 63 | googlesql: { |
| 64 | hashComments: true, |
| 65 | dashCommentNeedsSpace: false, |
| 66 | nestedBlockComments: false, |
| 67 | dollarQuotes: false, |
| 68 | tripleQuotes: true, |
| 69 | quotes: ["'", '"', "`"], |
| 70 | backslashEscapes: true, |
| 71 | }, |
| 72 | }; |
| 73 | |
| 74 | /** Index just past a line comment starting at `i` (the newline is kept). */ |
| 75 | const endOfLine = (sql: string, i: number) => { |
| 76 | const end = sql.indexOf("\n", i); |
| 77 | return end < 0 ? sql.length : end; |
| 78 | }; |
| 79 | |
| 80 | interface Span { |
| 81 | readonly end: number; |
| 82 | readonly closed: boolean; |
| 83 | } |
| 84 | |
| 85 | /** The end of a block comment starting at `i`. */ |
| 86 | const blockComment = (sql: string, i: number, nested: boolean): Span => { |
| 87 | let depth = 1; |
| 88 | let j = i + 2; |
| 89 | while (j < sql.length && depth > 0) { |
| 90 | if (nested && sql.startsWith("/*", j)) { |
| 91 | depth++; |
| 92 | j += 2; |
| 93 | } else if (sql.startsWith("*/", j)) { |
| 94 | depth--; |
| 95 | j += 2; |
| 96 | } else j++; |
| 97 | } |
| 98 | return { end: j, closed: depth === 0 }; |
| 99 | }; |
| 100 | |
| 101 | /** |
| 102 | * The end of a quoted span starting at `i`. A doubled closing quote |
| 103 | * escapes itself in every dialect except GoogleSQL; backslashes escape when |
| 104 | * the dialect (or a PostgreSQL `E'…'` string) says so, never in a GoogleSQL |
| 105 | * raw string (`r'…'`). |
| 106 | */ |
| 107 | const quoted = ( |
| 108 | sql: string, |
| 109 | i: number, |
| 110 | close: string, |
| 111 | options: { backslashEscapes: boolean; doubledEscapes: boolean }, |
| 112 | ): Span => { |
| 113 | let j = i + close.length; |
| 114 | while (j < sql.length) { |
| 115 | if (options.backslashEscapes && sql[j] === "\\") { |
| 116 | j += 2; |
| 117 | continue; |
| 118 | } |
| 119 | if (sql.startsWith(close, j)) { |
| 120 | j += close.length; |
| 121 | if (options.doubledEscapes && close.length === 1 && sql[j] === close) { |
| 122 | j++; |
| 123 | continue; |
| 124 | } |
| 125 | return { end: j, closed: true }; |
| 126 | } |
| 127 | j++; |
| 128 | } |
| 129 | return { end: sql.length, closed: false }; |
| 130 | }; |
| 131 | |
| 132 | const isWordChar = (ch: string | undefined) => ch !== undefined && /[A-Za-z0-9_]/.test(ch); |
| 133 | |
| 134 | /** |
| 135 | * Split a SQL script into statements on top-level `;`. Semicolons inside |
| 136 | * strings, quoted identifiers, dollar-quoted bodies, and comments never |
| 137 | * split, and each dialect's quoting and comment rules apply. Empty |
| 138 | * statements are dropped. |
| 139 | * |
| 140 | * This is a lexer, not a parser: it does not understand statement bodies |
| 141 | * that contain their own `;` outside quotes (procedural blocks), which |
| 142 | * migrations for these engines don't use. |
| 143 | */ |
| 144 | export const scanSqlScript = ( |
| 145 | sql: string, |
| 146 | dialect: ScriptDialect, |
| 147 | ): ReadonlyArray<ScannedStatement> => { |
| 148 | const rules = RULES[dialect]; |
| 149 | const statements: ScannedStatement[] = []; |
| 150 | let start = 0; |
| 151 | let text = ""; |
| 152 | let code = ""; |
| 153 | let unterminated: ScannedStatement["unterminated"]; |
| 154 | const push = (end: number) => { |
| 155 | if (code.trim().length > 0) { |
| 156 | statements.push({ |
| 157 | sql: sql.slice(start, end), |
| 158 | text: text.trim(), |
| 159 | code, |
| 160 | ...(unterminated ? { unterminated } : {}), |
| 161 | }); |
| 162 | } |
| 163 | start = end + 1; |
| 164 | text = ""; |
| 165 | code = ""; |
| 166 | }; |
| 167 | const blank = (span: string) => span.replace(/[^\n]/g, " "); |
| 168 | const mask = (span: string) => `_${" ".repeat(Math.max(0, span.length - 1))}`; |
| 169 | |
| 170 | let i = 0; |
| 171 | while (i < sql.length) { |
| 172 | const ch = sql[i]!; |
| 173 | const next = sql[i + 1]; |
| 174 | |
| 175 | const dashComment = |
| 176 | ch === "-" && |
| 177 | next === "-" && |
| 178 | (!rules.dashCommentNeedsSpace || i + 2 >= sql.length || /\s/.test(sql[i + 2]!)); |
| 179 | if (dashComment || (rules.hashComments && ch === "#")) { |
| 180 | const end = endOfLine(sql, i); |
| 181 | code += blank(sql.slice(i, end)); |
| 182 | text += " "; |
| 183 | i = end; |
| 184 | continue; |
| 185 | } |
| 186 | if (ch === "/" && next === "*") { |
| 187 | const span = blockComment(sql, i, rules.nestedBlockComments); |
| 188 | if (!span.closed) unterminated = "comment"; |
| 189 | code += blank(sql.slice(i, span.end)); |
| 190 | text += " "; |
| 191 | i = span.end; |
| 192 | continue; |
| 193 | } |
| 194 | if (rules.dollarQuotes && ch === "$" && !isWordChar(sql[i - 1])) { |
| 195 | const tag = /^\$(?:[A-Za-z_][A-Za-z_0-9]*)?\$/.exec(sql.slice(i))?.[0]; |
| 196 | if (tag) { |
| 197 | const close = sql.indexOf(tag, i + tag.length); |
| 198 | if (close < 0) unterminated = "quote"; |
| 199 | const end = close < 0 ? sql.length : close + tag.length; |
| 200 | code += mask(sql.slice(i, end)); |
| 201 | text += sql.slice(i, end); |
| 202 | i = end; |
| 203 | continue; |
| 204 | } |
| 205 | } |
| 206 | if (rules.quotes.includes(ch)) { |
| 207 | // A prefix letter right before the quote: E'…' (PostgreSQL escapes), |
| 208 | // r'…' / rb'…' (GoogleSQL raw strings). |
| 209 | const prefix = |
| 210 | /(?:^|[^A-Za-z0-9_])([A-Za-z]{1,2})$/ |
| 211 | .exec(sql.slice(Math.max(0, i - 3), i))?.[1] |
| 212 | ?.toLowerCase() ?? ""; |
| 213 | const raw = dialect === "googlesql" && ["r", "rb", "br"].includes(prefix); |
| 214 | const escaped = dialect === "postgres" && prefix === "e" && ch === "'"; |
| 215 | const close = |
| 216 | rules.tripleQuotes && ch !== "`" && sql.startsWith(ch.repeat(3), i) ? ch.repeat(3) : ch; |
| 217 | const span = quoted(sql, i, close, { |
| 218 | backslashEscapes: !raw && (rules.backslashEscapes || escaped), |
| 219 | doubledEscapes: dialect !== "googlesql", |
| 220 | }); |
| 221 | if (!span.closed) unterminated = "quote"; |
| 222 | code += mask(sql.slice(i, span.end)); |
| 223 | text += sql.slice(i, span.end); |
| 224 | i = span.end; |
| 225 | continue; |
| 226 | } |
| 227 | if (ch === ";") { |
| 228 | push(i); |
| 229 | i++; |
| 230 | continue; |
| 231 | } |
| 232 | code += ch; |
| 233 | text += ch; |
| 234 | i++; |
| 235 | } |
| 236 | push(sql.length); |
| 237 | return statements; |
| 238 | }; |
| 239 | |
| 240 | /** Statements of a SQL script with comments removed; see {@link scanSqlScript}. */ |
| 241 | export const splitSqlScript = (sql: string, dialect: ScriptDialect): string[] => |
| 242 | scanSqlScript(sql, dialect).map((statement) => statement.text); |