* Split a column-list body on top-level commas (i.e. commas not inside * parentheses). Needed for `CHECK (a BETWEEN 0 AND 1)` style entries.
(body: string)
| 249 | * parentheses). Needed for `CHECK (a BETWEEN 0 AND 1)` style entries. |
| 250 | */ |
| 251 | function splitColumnList(body: string): string[] { |
| 252 | const out: string[] = []; |
| 253 | let depth = 0; |
| 254 | let inBacktick = false; |
| 255 | let inSingle = false; |
| 256 | let buf = ''; |
| 257 | for (let i = 0; i < body.length; i++) { |
| 258 | const ch = body[i]; |
| 259 | if (inSingle) { |
| 260 | buf += ch; |
| 261 | if (ch === "'" && body[i - 1] !== '\\') inSingle = false; |
| 262 | continue; |
| 263 | } |
| 264 | if (inBacktick) { |
| 265 | buf += ch; |
| 266 | if (ch === '`') inBacktick = false; |
| 267 | continue; |
| 268 | } |
| 269 | if (ch === "'") { |
| 270 | inSingle = true; |
| 271 | buf += ch; |
| 272 | continue; |
| 273 | } |
| 274 | if (ch === '`') { |
| 275 | inBacktick = true; |
| 276 | buf += ch; |
| 277 | continue; |
| 278 | } |
| 279 | if (ch === '(') { |
| 280 | depth++; |
| 281 | buf += ch; |
| 282 | continue; |
| 283 | } |
| 284 | if (ch === ')') { |
| 285 | depth--; |
| 286 | buf += ch; |
| 287 | continue; |
| 288 | } |
| 289 | if (ch === ',' && depth === 0) { |
| 290 | out.push(buf); |
| 291 | buf = ''; |
| 292 | continue; |
| 293 | } |
| 294 | buf += ch; |
| 295 | } |
| 296 | if (buf.trim().length > 0) out.push(buf); |
| 297 | return out; |
| 298 | } |
| 299 | |
| 300 | export function parseSchema(sql: string): ParsedSchema { |
| 301 | const statements = splitStatements(sql); |
no test coverage detected