|
|
@@ -0,0 +1,276 @@ |
| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
import { html, type Html } from "../html.ts"; |
| 13 |
import { fileSize } from "../ui.ts"; |
| 14 |
import type { Rendered, RenderInput } from "./renderers.ts"; |
| 15 |
|
| 16 |
export const ROW_CAP = 500; |
| 17 |
const COLUMN_CAP = 500; |
| 18 |
const LINE_CAP = 200; |
| 19 |
|
| 20 |
export interface SqlColumn { |
| 21 |
readonly name: string; |
| 22 |
readonly type: string; |
| 23 |
readonly notNull: boolean; |
| 24 |
readonly primaryKey: boolean; |
| 25 |
readonly default: string; |
| 26 |
} |
| 27 |
|
| 28 |
export type SqlStatement = |
| 29 |
| { readonly kind: "table"; readonly name: string; readonly columns: readonly SqlColumn[]; readonly extras: readonly string[] } |
| 30 |
| { readonly kind: "other"; readonly line: string }; |
| 31 |
|
| 32 |
export interface SqlDoc { |
| 33 |
readonly statements: readonly SqlStatement[]; |
| 34 |
|
| 35 |
readonly total: number; |
| 36 |
} |
| 37 |
|
| 38 |
const VERB = /^(create|alter|drop|insert|update|delete|pragma|begin|commit|rollback|with|select|replace|vacuum|attach|detach|reindex|analyze|grant|revoke|set|comment|truncate|explain|savepoint|release)\b/i; |
| 39 |
|
| 40 |
|
| 41 |
function split(source: string): string[] | null { |
| 42 |
const out: string[] = []; |
| 43 |
let cur = ""; |
| 44 |
let depth = 0; |
| 45 |
for (let i = 0; i < source.length; i++) { |
| 46 |
const ch = source[i]!; |
| 47 |
if (ch === "-" && source[i + 1] === "-") { |
| 48 |
const nl = source.indexOf("\n", i); |
| 49 |
i = nl === -1 ? source.length : nl - 1; |
| 50 |
} else if (ch === "/" && source[i + 1] === "*") { |
| 51 |
const end = source.indexOf("*/", i + 2); |
| 52 |
if (end === -1) return null; |
| 53 |
i = end + 1; |
| 54 |
cur += " "; |
| 55 |
} else if (ch === "'" || ch === '"' || ch === "`" || ch === "[") { |
| 56 |
const close = ch === "[" ? "]" : ch; |
| 57 |
let j = i + 1; |
| 58 |
for (; j < source.length; j++) { |
| 59 |
if (source[j] === close) { |
| 60 |
if (close !== "]" && source[j + 1] === close) j++; |
| 61 |
else break; |
| 62 |
} |
| 63 |
} |
| 64 |
if (j >= source.length) return null; |
| 65 |
cur += source.slice(i, j + 1); |
| 66 |
i = j; |
| 67 |
} else if (ch === ";" && depth === 0) { |
| 68 |
if (cur.trim() !== "") out.push(cur.trim()); |
| 69 |
cur = ""; |
| 70 |
} else { |
| 71 |
if (ch === "(") depth++; |
| 72 |
else if (ch === ")" && depth > 0) depth--; |
| 73 |
cur += ch; |
| 74 |
} |
| 75 |
} |
| 76 |
if (cur.trim() !== "") out.push(cur.trim()); |
| 77 |
return out; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
function parts(body: string): string[] { |
| 82 |
const out: string[] = []; |
| 83 |
let cur = ""; |
| 84 |
let depth = 0; |
| 85 |
for (let i = 0; i < body.length; i++) { |
| 86 |
const ch = body[i]!; |
| 87 |
if (ch === "'" || ch === '"' || ch === "`" || ch === "[") { |
| 88 |
const close = ch === "[" ? "]" : ch; |
| 89 |
let j = i + 1; |
| 90 |
while (j < body.length && !(body[j] === close && (close === "]" || body[j + 1] !== close))) j += body[j] === close ? 2 : 1; |
| 91 |
cur += body.slice(i, j + 1); |
| 92 |
i = j; |
| 93 |
} else if (ch === "," && depth === 0) { |
| 94 |
out.push(cur.trim()); |
| 95 |
cur = ""; |
| 96 |
} else { |
| 97 |
if (ch === "(") depth++; |
| 98 |
else if (ch === ")") depth--; |
| 99 |
cur += ch; |
| 100 |
} |
| 101 |
} |
| 102 |
if (cur.trim() !== "") out.push(cur.trim()); |
| 103 |
return out; |
| 104 |
} |
| 105 |
|
| 106 |
const unquote = (id: string): string => { |
| 107 |
const q = id[0]; |
| 108 |
if (q === "[" && id.endsWith("]")) return id.slice(1, -1); |
| 109 |
if ((q === '"' || q === "`" || q === "'") && id.length >= 2 && id.endsWith(q)) return id.slice(1, -1).replaceAll(q + q, q); |
| 110 |
return id; |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
const IDENT = /^(\[[^\]]*\]|"(?:[^"]|"")*"|`(?:[^`]|``)*`|[^\s(),]+)\s*/; |
| 115 |
|
| 116 |
|
| 117 |
function qualified(s: string): { name: string; rest: string } | null { |
| 118 |
let name = ""; |
| 119 |
let rest = s; |
| 120 |
for (let n = 0; n < 3; n++) { |
| 121 |
const m = IDENT.exec(rest); |
| 122 |
if (m === null) return null; |
| 123 |
name += (name === "" ? "" : ".") + unquote(m[1]!); |
| 124 |
rest = rest.slice(m[0].length); |
| 125 |
if (rest[0] === ".") rest = rest.slice(1); |
| 126 |
else break; |
| 127 |
} |
| 128 |
return { name, rest }; |
| 129 |
} |
| 130 |
|
| 131 |
const STOP = /^(not|null|primary|default|unique|references|check|constraint|collate|generated|autoincrement|auto_increment|as|on|comment)$/i; |
| 132 |
const TABLE_LEVEL = /^(constraint|primary|foreign|unique|check|key|index|fulltext|exclude|like)\b/i; |
| 133 |
|
| 134 |
|
| 135 |
function tokens(s: string): string[] { |
| 136 |
const out: string[] = []; |
| 137 |
let i = 0; |
| 138 |
while (i < s.length) { |
| 139 |
if (/\s/.test(s[i]!)) { |
| 140 |
i++; |
| 141 |
continue; |
| 142 |
} |
| 143 |
const start = i; |
| 144 |
const q = s[i]!; |
| 145 |
if (q === "'" || q === '"' || q === "`" || q === "[") { |
| 146 |
const close = q === "[" ? "]" : q; |
| 147 |
i++; |
| 148 |
while (i < s.length && !(s[i] === close && (close === "]" || s[i + 1] !== close))) i += s[i] === close ? 2 : 1; |
| 149 |
i++; |
| 150 |
} else if (q === "(") { |
| 151 |
let d = 0; |
| 152 |
for (; i < s.length; i++) { |
| 153 |
if (s[i] === "(") d++; |
| 154 |
else if (s[i] === ")" && --d === 0) { |
| 155 |
i++; |
| 156 |
break; |
| 157 |
} |
| 158 |
} |
| 159 |
} else { |
| 160 |
while (i < s.length && !/[\s(]/.test(s[i]!)) i++; |
| 161 |
if (s[i] === "(") { |
| 162 |
|
| 163 |
let d = 0; |
| 164 |
for (; i < s.length; i++) { |
| 165 |
if (s[i] === "(") d++; |
| 166 |
else if (s[i] === ")" && --d === 0) { |
| 167 |
i++; |
| 168 |
break; |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
out.push(s.slice(start, i)); |
| 174 |
} |
| 175 |
return out; |
| 176 |
} |
| 177 |
|
| 178 |
function column(def: string): SqlColumn | null { |
| 179 |
const t = tokens(def); |
| 180 |
if (t.length === 0) return null; |
| 181 |
const name = unquote(t[0]!); |
| 182 |
const type: string[] = []; |
| 183 |
let i = 1; |
| 184 |
while (i < t.length && !STOP.test(t[i]!)) type.push(t[i++]!); |
| 185 |
let notNull = false; |
| 186 |
let primaryKey = false; |
| 187 |
let dflt = ""; |
| 188 |
for (; i < t.length; i++) { |
| 189 |
const w = t[i]!.toLowerCase(); |
| 190 |
if (w === "not" && t[i + 1]?.toLowerCase() === "null") notNull = true; |
| 191 |
else if (w === "primary" && t[i + 1]?.toLowerCase() === "key") primaryKey = true; |
| 192 |
else if (w === "default" && t[i + 1] !== undefined) dflt = t[i + 1]!; |
| 193 |
} |
| 194 |
return { name, type: type.join(" "), notNull: notNull || primaryKey, primaryKey, default: dflt }; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
function pkColumns(def: string): string[] { |
| 199 |
const m = /^(?:constraint\s+\S+\s+)?primary\s+key\s*\(([^)]*)\)/i.exec(def); |
| 200 |
return m === null ? [] : parts(m[1]!).map((c) => unquote(c.trim().split(/\s+/)[0]!)); |
| 201 |
} |
| 202 |
|
| 203 |
function table(stmt: string): SqlStatement | null { |
| 204 |
const m = /^create\s+(?:(?:global\s+|local\s+)?(?:temp|temporary)\s+|virtual\s+|unlogged\s+)?table\s+(?:if\s+not\s+exists\s+)?/i.exec(stmt); |
| 205 |
if (m === null) return null; |
| 206 |
const q = qualified(stmt.slice(m[0].length)); |
| 207 |
if (q === null || q.rest[0] !== "(") return null; |
| 208 |
let depth = 0; |
| 209 |
let end = -1; |
| 210 |
for (let i = 0; i < q.rest.length && end === -1; i++) { |
| 211 |
const ch = q.rest[i]!; |
| 212 |
if (ch === "'" || ch === '"' || ch === "`" || ch === "[") { |
| 213 |
const close = ch === "[" ? "]" : ch; |
| 214 |
let j = i + 1; |
| 215 |
while (j < q.rest.length && !(q.rest[j] === close && (close === "]" || q.rest[j + 1] !== close))) j += q.rest[j] === close ? 2 : 1; |
| 216 |
i = j; |
| 217 |
} else if (ch === "(") depth++; |
| 218 |
else if (ch === ")" && --depth === 0) end = i; |
| 219 |
} |
| 220 |
if (end === -1) return null; |
| 221 |
const columns: SqlColumn[] = []; |
| 222 |
const extras: string[] = []; |
| 223 |
const keys: string[] = []; |
| 224 |
for (const def of parts(q.rest.slice(1, end))) { |
| 225 |
if (TABLE_LEVEL.test(def)) { |
| 226 |
keys.push(...pkColumns(def)); |
| 227 |
extras.push(def.replace(/\s+/g, " ")); |
| 228 |
} else { |
| 229 |
const c = column(def); |
| 230 |
if (c !== null) columns.push(c); |
| 231 |
} |
| 232 |
} |
| 233 |
if (columns.length === 0) return null; |
| 234 |
const done = columns.map((c) => (keys.includes(c.name) ? { ...c, primaryKey: true, notNull: true } : c)); |
| 235 |
return { kind: "table", name: q.name, columns: done, extras }; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
export function parseSql(source: string): SqlDoc | null { |
| 240 |
const stmts = split(source.replace(/^/, "")); |
| 241 |
if (stmts === null || !stmts.some((s) => VERB.test(s))) return null; |
| 242 |
const statements: SqlStatement[] = []; |
| 243 |
for (const s of stmts) { |
| 244 |
if (statements.length >= ROW_CAP) break; |
| 245 |
const t = table(s); |
| 246 |
if (t !== null) { |
| 247 |
statements.push(t.kind === "table" ? { ...t, columns: t.columns.slice(0, COLUMN_CAP) } : t); |
| 248 |
} else { |
| 249 |
const line = (s.split(/\r?\n/)[0] ?? "").trim(); |
| 250 |
statements.push({ kind: "other", line: line.length > LINE_CAP ? line.slice(0, LINE_CAP) + "…" : line }); |
| 251 |
} |
| 252 |
} |
| 253 |
return { statements, total: stmts.length }; |
| 254 |
} |
| 255 |
|
| 256 |
const cell = (v: string): string => (v === "" ? "—" : v); |
| 257 |
|
| 258 |
function block(s: SqlStatement): Html { |
| 259 |
if (s.kind === "other") return html`<table class="table" data-dense><tbody><tr><td><code>${s.line}</code></td></tr></tbody></table>`; |
| 260 |
const rows = s.columns.map( |
| 261 |
(c) => |
| 262 |
html`<tr><th scope="row"><code>${c.name}</code></th><td>${cell(c.type)}</td><td>${c.notNull ? "NOT NULL" : "—"}</td><td>${c.primaryKey ? "PRIMARY KEY" : "—"}</td><td>${cell(c.default)}</td></tr>`, |
| 263 |
); |
| 264 |
const extras = s.extras.length === 0 ? html`` : html`<p class="muted small">${s.extras.join(" · ")}</p>`; |
| 265 |
return html`<h3><code>${s.name}</code></h3><table class="table" data-dense aria-label="${s.name} columns"><thead><tr><th scope="col">Column</th><th scope="col">Type</th><th scope="col">Null</th><th scope="col">Key</th><th scope="col">Default</th></tr></thead><tbody>${rows}</tbody></table>${extras}`; |
| 266 |
} |
| 267 |
|
| 268 |
export function sqlView(input: RenderInput): Rendered { |
| 269 |
const doc = parseSql(new TextDecoder().decode(input.body)); |
| 270 |
if (doc === null) return { content: html``, lines: 0, fallback: "This file holds no SQL statements to draw." }; |
| 271 |
const more = doc.total - doc.statements.length; |
| 272 |
const foot = more > 0 ? html`<p class="muted small">${more.toLocaleString()} more not shown</p>` : html``; |
| 273 |
const content = html`<div class="sql-view">${doc.statements.map(block)}${foot}</div>`; |
| 274 |
const n = doc.total; |
| 275 |
return { content, lines: 0, summary: `${n.toLocaleString()} ${n === 1 ? "statement" : "statements"} · ${fileSize(input.size)}` }; |
| 276 |
} |