void 1.12.0 → 1.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{account-cmd-D_0qJ9fx.mjs → account-cmd-BU1Fy_Bf.mjs} +4 -4
- package/dist/{application-analysis-Cx2S63z5.mjs → application-analysis-DVeN8yoi.mjs} +1 -1
- package/dist/{application-routing-BAXpnawu.mjs → application-routing-KKw4Y84h.mjs} +1 -1
- package/dist/{auth-Boz0lrb3.mjs → auth-A7cZ8aXA.mjs} +1 -1
- package/dist/{auth-DlLDymuc.mjs → auth-CqwJwItO.mjs} +1 -1
- package/dist/{auth-link-C97r9p2N.mjs → auth-link-Duu2NvN5.mjs} +4 -4
- package/dist/{auth-router-BmzEgzYx.mjs → auth-router-vgtOdb9j.mjs} +1 -1
- package/dist/{build-cmd-BaFsyZrz.mjs → build-cmd-B7OJ0U5R.mjs} +2 -2
- package/dist/build-id-4IV4hC1-.mjs +13 -0
- package/dist/{cache-xO711ATQ.mjs → cache-wSpi3aeM.mjs} +2 -2
- package/dist/{cancel-deploy-Di11ynsr.mjs → cancel-deploy-DMWzkAlb.mjs} +2 -2
- package/dist/{cf-access-FaIm95jA.mjs → cf-access-DHklo7Rg.mjs} +33 -19
- package/dist/{cf-build-output-Ca201_n1.mjs → cf-build-output-Cso7Vh-t.mjs} +4 -2
- package/dist/cf-build-output-Wpk_e5CI.mjs +1 -0
- package/dist/cli/cli.mjs +1 -1
- package/dist/cli/cloudflare-operation-process.mjs +1 -1
- package/dist/cli/db-execute-process.d.mts +2 -0
- package/dist/cli/db-execute-process.mjs +57 -0
- package/dist/{cli-main-DlW6lB8s.mjs → cli-main-BrChcpE3.mjs} +37 -344
- package/dist/{client-ImIe7Mjq.mjs → client-DE2m2ldh.mjs} +4 -4
- package/dist/{client-DkYzDEC0.mjs → client-eLZwqkPH.mjs} +1 -1
- package/dist/{cloudflare-operations-BF0PSErW.mjs → cloudflare-operations-BIRQkYwL.mjs} +14 -8
- package/dist/cloudflare-operations-ByjFGats.mjs +1 -0
- package/dist/{connect-DDRQGfFE.mjs → connect-D0V0o40j.mjs} +4 -4
- package/dist/{create-project-CY7sP8sI.mjs → create-project-8FdN6D2i.mjs} +2 -2
- package/dist/{create-project-Dro3K-az.mjs → create-project-CbuDH5Hv.mjs} +1 -1
- package/dist/{db-DmlaWVBu.mjs → db-BNPIjLkK.mjs} +1348 -69
- package/dist/db-args-BvMwAber.mjs +1 -0
- package/dist/db-args-CiQVg8SJ.mjs +321 -0
- package/dist/{delete-Uuq9ch6o.mjs → delete-WtLEfap2.mjs} +2 -2
- package/dist/{deploy-B9E7_rHC.mjs → deploy-BgSbagIP.mjs} +14 -14
- package/dist/{deploy-BB3rsZbP.mjs → deploy-Do6FWqon.mjs} +1 -1
- package/dist/{deploy-managed-static-IKZyuc1W.mjs → deploy-managed-static-BWBsxOww.mjs} +5 -5
- package/dist/{deploy-security-notice-C3LalelR.mjs → deploy-security-notice-IRGy6Edf.mjs} +2 -2
- package/dist/deploy.mjs +4 -4
- package/dist/{domain-B_xY1yZ5.mjs → domain-mweyYXSw.mjs} +3 -3
- package/dist/{email-BIC15L7a.mjs → email-DcQNP40O.mjs} +5 -5
- package/dist/{email-cloudflare-C447nOqq.mjs → email-cloudflare-DeYH3Ke7.mjs} +2 -2
- package/dist/{env-DJPZYHT5.mjs → env-DaAoYR2-.mjs} +3 -3
- package/dist/{gen-DiTZlDx8.mjs → gen-aOtHhZLD.mjs} +1 -1
- package/dist/gen-vhBDHs3q.mjs +1 -0
- package/dist/{github-cmd-DPmby8x-.mjs → github-cmd-BR19Ffdz.mjs} +2 -2
- package/dist/{help-9wfMyXHb.mjs → help-DvClCzqL.mjs} +9 -1
- package/dist/help-fKPzcTJv.mjs +1 -0
- package/dist/index.mjs +16 -206
- package/dist/{info-BWEWaOBo.mjs → info-P6MBiVJi.mjs} +2 -2
- package/dist/{init-BuTPVLPO.mjs → init-C8YpkGIP.mjs} +63 -18
- package/dist/installed-packages-tfmYd-cC.mjs +198 -0
- package/dist/{link-BiesPyT4.mjs → link-Dyz9IGcM.mjs} +3 -3
- package/dist/{list-CkonftPQ.mjs → list-VGYBMjmx.mjs} +4 -4
- package/dist/{login-BSOSah1j.mjs → login-CWFr-G2I.mjs} +3 -3
- package/dist/login-DECm3e9b.mjs +1 -0
- package/dist/{logs-HOBUnroa.mjs → logs-kS1wuR4b.mjs} +3 -3
- package/dist/module-references-DWQmO6TW.mjs +27 -0
- package/dist/{mysql-sql-CB52k8tQ.mjs → mysql-lock-D8xtnI-D.mjs} +1 -118
- package/dist/mysql-sql-BIE7bc4Z.mjs +124 -0
- package/dist/{operator-ai-logging-ZDqqhnIq.mjs → operator-ai-logging-DD0n6zc2.mjs} +3 -3
- package/dist/{operator-args-DCkWbgT-.mjs → operator-args-nvetCWuz.mjs} +1 -1
- package/dist/{operator-auth-BjoPLgja.mjs → operator-auth-DMvn-y9X.mjs} +1 -1
- package/dist/{operator-client-DzebIntv.mjs → operator-client-dFNnXfyB.mjs} +4 -3
- package/dist/operator-cmd-CVfK5U0B.mjs +1 -0
- package/dist/{operator-cmd-AZoeI9Pl.mjs → operator-cmd-a7yOf2tH.mjs} +13 -9
- package/dist/{operator-security-Bo26FP01.mjs → operator-security-D5MToU0p.mjs} +1 -1
- package/dist/pages/client.mjs +77 -9
- package/dist/pages/index.d.mts +6 -0
- package/dist/pages/index.mjs +276 -6
- package/dist/pages/protocol.mjs +1 -1
- package/dist/platform-args-CMwMoiLp.mjs +1 -0
- package/dist/{platform-args-lwCyQ6Xy.mjs → platform-args-CvZSuhYS.mjs} +1 -1
- package/dist/{platform-auth-config-DLRMPw1G.mjs → platform-auth-config-CpszX-Ne.mjs} +7 -7
- package/dist/{platform-auth-protection-HSmLmD3d.mjs → platform-auth-protection-DA6Tw8Og.mjs} +5 -5
- package/dist/{platform-auth-recovery-BXPhTlKa.mjs → platform-auth-recovery-Cztrb1JC.mjs} +4 -4
- package/dist/{platform-cmd-DJ-fOYhL.mjs → platform-cmd-m6Asj6nO.mjs} +6 -6
- package/dist/{platform-cmd-CqRjxFEU.mjs → platform-cmd-u1j88ggF.mjs} +1 -1
- package/dist/{platform-domain-CRsRJOws.mjs → platform-domain-DJSznNTA.mjs} +2 -2
- package/dist/{platform-lifecycle-bx_kpqo7.mjs → platform-lifecycle-BC-9p58d.mjs} +1 -1
- package/dist/{platform-lifecycle-fau3VKv6.mjs → platform-lifecycle-GFeXv3NC.mjs} +58 -11
- package/dist/{platform-management-BRGhD-oF.mjs → platform-management-BwHkz46V.mjs} +1 -1
- package/dist/{platform-management-CnKBdDx5.mjs → platform-management-DX1zINou.mjs} +4 -4
- package/dist/{platform-plans-config-2C3_5EDs.mjs → platform-plans-config-DiydtNKY.mjs} +3 -3
- package/dist/{platform-recovery-poxgOOAY.mjs → platform-recovery-CnzBbudV.mjs} +2 -2
- package/dist/{platform-setup-DYylapHD.mjs → platform-setup-Lph9i-rh.mjs} +1 -1
- package/dist/{plugin-inference-7O11_E4H.mjs → plugin-inference-COlq96WJ.mjs} +2 -27
- package/dist/prepare-DcwigRUS.mjs +1 -0
- package/dist/{prepare-DWjn1Lzd.mjs → prepare-WvijE289.mjs} +1 -1
- package/dist/{prepare-BbvlKreR.mjs → prepare-co1o1FFd.mjs} +1 -1
- package/dist/{project-cmd-DyUEFi2b.mjs → project-cmd-BWJTplpc.mjs} +18 -18
- package/dist/{project-security-DZEQgnsS.mjs → project-security-BV1xfj1K.mjs} +1 -1
- package/dist/{project-security--tYGFPGo.mjs → project-security-CY_aahvW.mjs} +2 -2
- package/dist/{project-team-BszTHgzC.mjs → project-team-BlIFXlmT.mjs} +2 -2
- package/dist/{project-token-DBDy4NdV.mjs → project-token-CMsB7I1-.mjs} +2 -2
- package/dist/{project-zero-trust-D9sUouij.mjs → project-zero-trust-BI6pdpx4.mjs} +2 -2
- package/dist/{protocol-CE3wP-H1.mjs → protocol-4rEnumif.mjs} +10 -3
- package/dist/{redirect-DD825Stf.mjs → redirect-Dyvuil0Q.mjs} +5 -2
- package/dist/{requests-CBokjiOi.mjs → requests-CfmJxNYg.mjs} +2 -2
- package/dist/{rollback-CEeHNFUn.mjs → rollback-DL5HuAsM.mjs} +3 -3
- package/dist/{runner-mysql-3XDKrn-g.mjs → runner-mysql-D549vGGm.mjs} +2 -1
- package/dist/{runner-mysql-r688x6Y9.mjs → runner-mysql-DOjbj-N1.mjs} +1 -1
- package/dist/runtime/db/migrations/mysql-handler.mjs +2 -1
- package/dist/runtime/routing/index.mjs +2 -2
- package/dist/{secret-Deqo3n_N.mjs → secret-C-WBaUSS.mjs} +3 -3
- package/docs/guide/jobs.md +1 -1
- package/docs/guide/pages-routing/actions-and-forms.md +36 -15
- package/docs/guide/pages-routing/errors.md +3 -1
- package/docs/guide/pages-routing/overview.md +20 -0
- package/docs/guide/platform/administration/zero-trust.md +1 -1
- package/docs/guide/platform/installation/maintenance.md +3 -3
- package/docs/guide/queues.md +1 -1
- package/docs/integrations/frameworks/sveltekit.md +1 -1
- package/docs/reference/api/pages.md +4 -0
- package/docs/reference/cli/database.md +42 -6
- package/docs/reference/cli/platform-security.md +1 -1
- package/docs/reference/cli/platform-zero-trust.md +4 -3
- package/docs/reference/cli/setup.md +4 -0
- package/package.json +7 -7
- package/dist/cf-build-output-Dy7N0hQN.mjs +0 -1
- package/dist/cloudflare-operations-dpRD-2Px.mjs +0 -1
- package/dist/gen-CQDiPi8m.mjs +0 -1
- package/dist/help-Dy3w4rPq.mjs +0 -1
- package/dist/login-DWCZeCX9.mjs +0 -1
- package/dist/operator-cmd-DEnHFmJF.mjs +0 -1
- package/dist/platform-args-DNbNZ_RR.mjs +0 -1
- package/dist/prepare-BmG2E6TI.mjs +0 -1
|
@@ -4,14 +4,15 @@ import { c as getDatabaseCasing, l as getDatabaseDialect, m as resolveBindingNam
|
|
|
4
4
|
import { t as resolveLocalD1Target } from "./local-d1-CYOYkVwO.mjs";
|
|
5
5
|
import { i as writeJournal, n as readJournal, r as stripSqlExt, t as collectMigrations } from "./collect-rNLybAnF.mjs";
|
|
6
6
|
import { i as validateMigrations, t as assertJournalCoherence } from "./validate-CtBWCzez.mjs";
|
|
7
|
-
import { r as detectFramework } from "./plugin-inference-
|
|
7
|
+
import { r as detectFramework } from "./plugin-inference-COlq96WJ.mjs";
|
|
8
8
|
import { n as readProjectPaths } from "./project-paths-2ENfCCih.mjs";
|
|
9
9
|
import { t as cfAccessHeaders } from "./cf-access-DmkKPcAI.mjs";
|
|
10
10
|
import { c as loadLocalEnv } from "./env-EjHqc7m9.mjs";
|
|
11
|
-
import { c as getToken, o as getProxyUrl } from "./auth-
|
|
12
|
-
import { n as ensurePlatformProxyAccess, r as isCloudflareAccessChallenge, t as ensurePlatformAccessToken } from "./cf-access-
|
|
13
|
-
import { n as PlatformApiError, r as PlatformClient } from "./client-
|
|
14
|
-
import {
|
|
11
|
+
import { c as getToken, o as getProxyUrl } from "./auth-CqwJwItO.mjs";
|
|
12
|
+
import { n as ensurePlatformProxyAccess, r as isCloudflareAccessChallenge, t as ensurePlatformAccessToken } from "./cf-access-DHklo7Rg.mjs";
|
|
13
|
+
import { n as PlatformApiError, r as PlatformClient } from "./client-DE2m2ldh.mjs";
|
|
14
|
+
import { t as closeMySqlQuoted } from "./mysql-sql-BIE7bc4Z.mjs";
|
|
15
|
+
import { n as formatTimestamp } from "./gen-aOtHhZLD.mjs";
|
|
15
16
|
import { t as Buffer } from "./node-buffer-D7kgjaDs.mjs";
|
|
16
17
|
import { c as formatCount, f as formatUrl, h as import_picocolors, o as dim, p as getErrorMessage, y as printSuccess } from "./output-BZLnu0z8.mjs";
|
|
17
18
|
import { n as readDeploymentPlatform, r as readProjectConfig } from "./project-dh9431bN.mjs";
|
|
@@ -19,7 +20,1207 @@ import { n as drizzleKitBin } from "./yarn-pnp-DgWbE7z5.mjs";
|
|
|
19
20
|
import { createRequire } from "node:module";
|
|
20
21
|
import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
21
22
|
import { pathToFileURL } from "node:url";
|
|
23
|
+
import { tokenizeSql } from "@void/deploy-core";
|
|
22
24
|
import { execFileSync } from "node:child_process";
|
|
25
|
+
// src/cli/db-execute-results.ts
|
|
26
|
+
const MIN_SAFE = BigInt(Number.MIN_SAFE_INTEGER);
|
|
27
|
+
const MAX_SAFE = BigInt(Number.MAX_SAFE_INTEGER);
|
|
28
|
+
/**
|
|
29
|
+
* Write values JSON cannot hold as-is without changing them. `this[key]` is the value before
|
|
30
|
+
* `toJSON()`, so a Buffer is still a Buffer here, at any depth (a `bytea[]` element too).
|
|
31
|
+
*/
|
|
32
|
+
function jsonValue(key, value) {
|
|
33
|
+
const raw = this[key];
|
|
34
|
+
if (raw instanceof Uint8Array) return Array.from(raw);
|
|
35
|
+
if (typeof raw === "bigint") return raw >= MIN_SAFE && raw <= MAX_SAFE ? Number(raw) : raw.toString();
|
|
36
|
+
if (typeof raw === "number" && !Number.isFinite(raw)) return String(raw);
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
/** Serialize results for stdout. */
|
|
40
|
+
function formatStatementResults(results) {
|
|
41
|
+
return JSON.stringify(results, jsonValue, 2);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Find each statement in SQLite SQL the way `sqlite3_complete()` does: a statement ends at a
|
|
45
|
+
* top-level `;`, except that `CREATE [TEMP] TRIGGER` runs until `; END ;`, since its body holds
|
|
46
|
+
* semicolons of its own. Ranges hold no statement when they are only whitespace and comments,
|
|
47
|
+
* and those are left out. The last range may lack its `;`; SQLite decides whether it is complete.
|
|
48
|
+
*/
|
|
49
|
+
function sqliteStatementRanges(sql) {
|
|
50
|
+
const START = 1;
|
|
51
|
+
const transitions = [
|
|
52
|
+
[
|
|
53
|
+
1,
|
|
54
|
+
2,
|
|
55
|
+
3,
|
|
56
|
+
4,
|
|
57
|
+
2,
|
|
58
|
+
2,
|
|
59
|
+
2
|
|
60
|
+
],
|
|
61
|
+
[
|
|
62
|
+
1,
|
|
63
|
+
2,
|
|
64
|
+
3,
|
|
65
|
+
4,
|
|
66
|
+
2,
|
|
67
|
+
2,
|
|
68
|
+
2
|
|
69
|
+
],
|
|
70
|
+
[
|
|
71
|
+
1,
|
|
72
|
+
2,
|
|
73
|
+
2,
|
|
74
|
+
2,
|
|
75
|
+
2,
|
|
76
|
+
2,
|
|
77
|
+
2
|
|
78
|
+
],
|
|
79
|
+
[
|
|
80
|
+
1,
|
|
81
|
+
3,
|
|
82
|
+
2,
|
|
83
|
+
4,
|
|
84
|
+
2,
|
|
85
|
+
2,
|
|
86
|
+
2
|
|
87
|
+
],
|
|
88
|
+
[
|
|
89
|
+
1,
|
|
90
|
+
2,
|
|
91
|
+
2,
|
|
92
|
+
2,
|
|
93
|
+
4,
|
|
94
|
+
5,
|
|
95
|
+
2
|
|
96
|
+
],
|
|
97
|
+
[
|
|
98
|
+
6,
|
|
99
|
+
5,
|
|
100
|
+
5,
|
|
101
|
+
5,
|
|
102
|
+
5,
|
|
103
|
+
5,
|
|
104
|
+
5
|
|
105
|
+
],
|
|
106
|
+
[
|
|
107
|
+
6,
|
|
108
|
+
5,
|
|
109
|
+
5,
|
|
110
|
+
5,
|
|
111
|
+
5,
|
|
112
|
+
5,
|
|
113
|
+
7
|
|
114
|
+
],
|
|
115
|
+
[
|
|
116
|
+
1,
|
|
117
|
+
5,
|
|
118
|
+
5,
|
|
119
|
+
5,
|
|
120
|
+
5,
|
|
121
|
+
5,
|
|
122
|
+
5
|
|
123
|
+
]
|
|
124
|
+
];
|
|
125
|
+
const keywords = /* @__PURE__ */ new Map([
|
|
126
|
+
["explain", 2],
|
|
127
|
+
["create", 3],
|
|
128
|
+
["temp", 4],
|
|
129
|
+
["temporary", 4],
|
|
130
|
+
["trigger", 5],
|
|
131
|
+
["end", 6]
|
|
132
|
+
]);
|
|
133
|
+
const ranges = [];
|
|
134
|
+
let state = 0;
|
|
135
|
+
let start = 0;
|
|
136
|
+
let hasStatement = false;
|
|
137
|
+
const advance = (token, index) => {
|
|
138
|
+
const next = transitions[state][token];
|
|
139
|
+
if (token === 0 && next === START) {
|
|
140
|
+
if (hasStatement) ranges.push({
|
|
141
|
+
start,
|
|
142
|
+
end: index + 1
|
|
143
|
+
});
|
|
144
|
+
start = index + 1;
|
|
145
|
+
hasStatement = false;
|
|
146
|
+
} else if (token !== 0) hasStatement = true;
|
|
147
|
+
state = next;
|
|
148
|
+
};
|
|
149
|
+
for (const segment of tokenizeSql(sql)) {
|
|
150
|
+
const bareOpen = segment.kind === "block-comment" && segment.end - segment.start === 2;
|
|
151
|
+
if (segment.kind === "line-comment" || segment.kind === "block-comment" && !bareOpen) continue;
|
|
152
|
+
if (segment.kind !== "sql") {
|
|
153
|
+
advance(1, segment.start);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
const text = sql.slice(segment.start, segment.end);
|
|
157
|
+
for (const match of text.matchAll(/;|[ \t\n\f\r\uFEFF]+|[\w$\u0080-]+|./gu)) {
|
|
158
|
+
const [token] = match;
|
|
159
|
+
if (token === ";") advance(0, segment.start + match.index);
|
|
160
|
+
else if (!/^[ \t\n\f\r\uFEFF]/u.test(token)) advance(keywords.get(token.toLowerCase()) ?? 1, segment.start + match.index);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (hasStatement) ranges.push({
|
|
164
|
+
start,
|
|
165
|
+
end: sql.length
|
|
166
|
+
});
|
|
167
|
+
return ranges;
|
|
168
|
+
}
|
|
169
|
+
/** Split SQLite SQL into its statements. See `sqliteStatementRanges`. */
|
|
170
|
+
function splitSqliteStatements(sql) {
|
|
171
|
+
return sqliteStatementRanges(sql).map(({ start, end }) => sql.slice(start, end));
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Run SQL statement by statement and keep each one's rows. Like `exec()`, every statement
|
|
175
|
+
* commits on its own unless the SQL opens a transaction, and a failing statement stops the run
|
|
176
|
+
* after the ones before it took effect. Integers are read exactly, as BigInt.
|
|
177
|
+
*/
|
|
178
|
+
function runSqliteStatements(db, sql) {
|
|
179
|
+
const counters = db.prepare("SELECT last_insert_rowid() AS last_row_id, total_changes() AS total").safeIntegers(true);
|
|
180
|
+
const readCounters = () => counters.get() ?? {
|
|
181
|
+
last_row_id: 0n,
|
|
182
|
+
total: 0n
|
|
183
|
+
};
|
|
184
|
+
const results = [];
|
|
185
|
+
for (const statementSql of splitSqliteStatements(sql)) {
|
|
186
|
+
const statement = db.prepare(statementSql).safeIntegers(true);
|
|
187
|
+
const before = readCounters().total;
|
|
188
|
+
const started = performance.now();
|
|
189
|
+
let rows = [];
|
|
190
|
+
if (statement.reader) {
|
|
191
|
+
const columns = statement.columns().map((column) => column.name);
|
|
192
|
+
rows = statement.raw(true).all().map((values) => Object.fromEntries(columns.map((name, index) => [name, values[index]])));
|
|
193
|
+
} else statement.run();
|
|
194
|
+
const duration = performance.now() - started;
|
|
195
|
+
const after = readCounters();
|
|
196
|
+
results.push({
|
|
197
|
+
results: rows,
|
|
198
|
+
success: true,
|
|
199
|
+
meta: {
|
|
200
|
+
changes: after.total - before,
|
|
201
|
+
last_row_id: after.last_row_id,
|
|
202
|
+
duration
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
return results;
|
|
207
|
+
}
|
|
208
|
+
/** Parse JSON keeping each number's digits, which `JSON.stringify` then writes back unchanged. */
|
|
209
|
+
function parseExactJson(text) {
|
|
210
|
+
return JSON.parse(text, (_key, value, context) => typeof value === "number" ? JSON.rawJSON(context.source ?? String(value)) : value);
|
|
211
|
+
}
|
|
212
|
+
function parseExactJsonElements(values) {
|
|
213
|
+
return values.map((value) => Array.isArray(value) ? parseExactJsonElements(value) : value === null ? null : parseExactJson(value));
|
|
214
|
+
}
|
|
215
|
+
const PG_TEXT_ARRAY = 1009;
|
|
216
|
+
const PG_NUMERIC_ARRAY = 1231;
|
|
217
|
+
const PG_JSON = 114;
|
|
218
|
+
const PG_JSONB = 3802;
|
|
219
|
+
const PG_JSON_ARRAY = 199;
|
|
220
|
+
const PG_JSONB_ARRAY = 3807;
|
|
221
|
+
const PG_DATE = 1082;
|
|
222
|
+
const PG_TIMESTAMP = 1114;
|
|
223
|
+
const PG_TIMESTAMPTZ = 1184;
|
|
224
|
+
const PG_DATE_ARRAY = 1182;
|
|
225
|
+
const PG_TIMESTAMP_ARRAY = 1115;
|
|
226
|
+
const PG_TIMESTAMPTZ_ARRAY = 1185;
|
|
227
|
+
/**
|
|
228
|
+
* Type parsers for `void db execute --json` on PostgreSQL. node-postgres reads `numeric[]`
|
|
229
|
+
* elements as numbers and JSON with `JSON.parse`, which round what a double cannot hold. Here a
|
|
230
|
+
* `numeric[]` element stays a string, like a `numeric`, and a JSON number keeps its digits.
|
|
231
|
+
* Dates and timestamps stay the text PostgreSQL sends: node-postgres reads a `date` or a
|
|
232
|
+
* `timestamp` in the machine's time zone and rounds to milliseconds, so the same row would
|
|
233
|
+
* print differently on each machine.
|
|
234
|
+
*/
|
|
235
|
+
function pgJsonTypes(types) {
|
|
236
|
+
const parseTextArray = types.getTypeParser(PG_TEXT_ARRAY, "text");
|
|
237
|
+
const parsers = /* @__PURE__ */ new Map([
|
|
238
|
+
[PG_NUMERIC_ARRAY, parseTextArray],
|
|
239
|
+
[PG_JSON, parseExactJson],
|
|
240
|
+
[PG_JSONB, parseExactJson],
|
|
241
|
+
[PG_JSON_ARRAY, (value) => parseExactJsonElements(parseTextArray(value))],
|
|
242
|
+
[PG_JSONB_ARRAY, (value) => parseExactJsonElements(parseTextArray(value))],
|
|
243
|
+
[PG_DATE, (value) => value],
|
|
244
|
+
[PG_TIMESTAMP, (value) => value],
|
|
245
|
+
[PG_TIMESTAMPTZ, (value) => value],
|
|
246
|
+
[PG_DATE_ARRAY, parseTextArray],
|
|
247
|
+
[PG_TIMESTAMP_ARRAY, parseTextArray],
|
|
248
|
+
[PG_TIMESTAMPTZ_ARRAY, parseTextArray]
|
|
249
|
+
]);
|
|
250
|
+
return { getTypeParser(oid, format = "text") {
|
|
251
|
+
return (format === "text" ? parsers.get(oid) : void 0) ?? types.getTypeParser(oid, format);
|
|
252
|
+
} };
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* mysql2's `typeCast` for `void db execute --json`. mysql2 reads JSON with `JSON.parse`, which
|
|
256
|
+
* rounds what a double cannot hold, so a JSON number keeps its digits here, as on PostgreSQL.
|
|
257
|
+
* MySQL sends JSON as binary, which is UTF-8 text; MariaDB sends it as text and marks it as JSON
|
|
258
|
+
* only in its extended metadata.
|
|
259
|
+
*/
|
|
260
|
+
function mySqlJsonTypeCast(field, next) {
|
|
261
|
+
const isJson = field.type === "JSON";
|
|
262
|
+
if (!isJson && field.extendedFormat !== "json") return next();
|
|
263
|
+
const text = isJson ? field.string("utf8") : field.string();
|
|
264
|
+
return text === null ? null : parseExactJson(text);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* node-postgres returns one result for one statement and an array for several. SQL without a
|
|
268
|
+
* statement returns an empty-query result with no command, which is left out.
|
|
269
|
+
*/
|
|
270
|
+
function pgStatementResults(result) {
|
|
271
|
+
return (Array.isArray(result) ? result : [result]).filter((item) => item.command !== null && item.command !== void 0).map((item) => ({
|
|
272
|
+
results: item.rows ?? [],
|
|
273
|
+
success: true,
|
|
274
|
+
meta: {
|
|
275
|
+
command: item.command,
|
|
276
|
+
rowCount: item.rowCount ?? null
|
|
277
|
+
}
|
|
278
|
+
}));
|
|
279
|
+
}
|
|
280
|
+
function mySqlCount(field) {
|
|
281
|
+
return typeof field === "number" || typeof field === "string" ? field : 0;
|
|
282
|
+
}
|
|
283
|
+
const UINT64_RANGE = 2n ** 64n;
|
|
284
|
+
/**
|
|
285
|
+
* The insert ID of a result header. MySQL sends it as an unsigned 64-bit integer, which mysql2
|
|
286
|
+
* reads as a signed one, so an ID of 2^63 or more, from a BIGINT UNSIGNED column, arrives
|
|
287
|
+
* negative.
|
|
288
|
+
*/
|
|
289
|
+
function mySqlInsertId(field) {
|
|
290
|
+
const id = mySqlCount(field);
|
|
291
|
+
return (typeof id === "number" ? id < 0 : id.startsWith("-")) ? (BigInt(id) + UINT64_RANGE).toString() : id;
|
|
292
|
+
}
|
|
293
|
+
function mySqlHeaderMeta(value) {
|
|
294
|
+
const header = typeof value === "object" && value !== null ? value : {};
|
|
295
|
+
return {
|
|
296
|
+
affectedRows: mySqlCount(header.affectedRows),
|
|
297
|
+
insertId: mySqlInsertId(header.insertId)
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
function mySqlStatementResult(value) {
|
|
301
|
+
return Array.isArray(value) ? {
|
|
302
|
+
results: value,
|
|
303
|
+
success: true,
|
|
304
|
+
meta: {}
|
|
305
|
+
} : {
|
|
306
|
+
results: [],
|
|
307
|
+
success: true,
|
|
308
|
+
meta: mySqlHeaderMeta(value)
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
/** Reads what `mySqlServerMode()` needs. It only matters for SQL that `needsMySqlServerMode` finds. */
|
|
312
|
+
const MYSQL_SERVER_MODE_QUERY = "SELECT @@SESSION.sql_mode AS sql_mode, VERSION() AS version";
|
|
313
|
+
/**
|
|
314
|
+
* Whether the SQL holds a backslash, a double quote, a `[` or an executable comment, which read
|
|
315
|
+
* differently per server. A double quote opens a string, or in `ANSI_QUOTES` mode a name, such
|
|
316
|
+
* as the name of a prepared statement.
|
|
317
|
+
*/
|
|
318
|
+
function needsMySqlServerMode(sql) {
|
|
319
|
+
return /[\\"[]|\/\*M?!/u.test(sql);
|
|
320
|
+
}
|
|
321
|
+
/** Read the row `MYSQL_SERVER_MODE_QUERY` returns. */
|
|
322
|
+
function mySqlServerMode(rows) {
|
|
323
|
+
const row = Array.isArray(rows) ? rows[0] : void 0;
|
|
324
|
+
const fields = typeof row === "object" && row !== null ? row : {};
|
|
325
|
+
const modes = typeof fields.sql_mode === "string" ? fields.sql_mode.split(",") : [];
|
|
326
|
+
const versionText = String(fields.version ?? "");
|
|
327
|
+
const version = /^(\d+)\.(\d+)\.(\d+)/.exec(versionText);
|
|
328
|
+
const mariaDb = /mariadb/iu.test(versionText);
|
|
329
|
+
return {
|
|
330
|
+
version: version ? Number(version[1]) * 1e4 + Number(version[2]) * 100 + Number(version[3]) : Number.POSITIVE_INFINITY,
|
|
331
|
+
backslashEscapes: !modes.includes("NO_BACKSLASH_ESCAPES"),
|
|
332
|
+
ansiQuotes: modes.includes("ANSI_QUOTES"),
|
|
333
|
+
brackets: mariaDb && modes.includes("MSSQL"),
|
|
334
|
+
mariaDb
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
/** Whether the character after `--` (`NaN` at the end of the input) makes it a comment. */
|
|
338
|
+
function startsMySqlDashComment(code) {
|
|
339
|
+
return Number.isNaN(code) || code <= 32 || code === 127;
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* The index after the comment whose text starts at `index`, or `undefined` when it does not
|
|
343
|
+
* close. A skipped version comment may hold one nested comment (`nesting` 1); any other comment
|
|
344
|
+
* ends at its first closing marker.
|
|
345
|
+
*/
|
|
346
|
+
function skipMySqlComment(sql, index, nesting) {
|
|
347
|
+
while (index < sql.length) {
|
|
348
|
+
const character = sql[index++];
|
|
349
|
+
if (nesting > 0 && character === "/" && sql[index] === "*") {
|
|
350
|
+
const end = skipMySqlComment(sql, index + 1, nesting - 1);
|
|
351
|
+
if (end === void 0) return;
|
|
352
|
+
index = end;
|
|
353
|
+
} else if (character === "*" && sql[index] === "/") return index + 1;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
const MYSQL_VERSION_COMMENT = /!(?:(\d{6})(?=\s)|(\d{5}))?/y;
|
|
357
|
+
const MARIADB_VERSION_COMMENT = /(M?)!(?:(\d{6})|(\d{5}))?/y;
|
|
358
|
+
/** The index after the marker of an executable comment the server runs at `index`, if any. */
|
|
359
|
+
function runsMySqlComment(sql, index, mode) {
|
|
360
|
+
const marker = mode.mariaDb ? MARIADB_VERSION_COMMENT : MYSQL_VERSION_COMMENT;
|
|
361
|
+
marker.lastIndex = index;
|
|
362
|
+
const match = marker.exec(sql);
|
|
363
|
+
if (!match) return;
|
|
364
|
+
const digits = mode.mariaDb ? match[2] ?? match[3] : match[1] ?? match[2];
|
|
365
|
+
if (digits === void 0) return marker.lastIndex;
|
|
366
|
+
const required = Number(digits);
|
|
367
|
+
const mySqlOnly = mode.mariaDb && match[1] !== "M" && required >= 50700 && required <= 99999;
|
|
368
|
+
return required <= mode.version && !mySqlOnly ? marker.lastIndex : void 0;
|
|
369
|
+
}
|
|
370
|
+
const MYSQL_DEFAULT_MODE = {
|
|
371
|
+
version: Number.POSITIVE_INFINITY,
|
|
372
|
+
backslashEscapes: true,
|
|
373
|
+
ansiQuotes: false,
|
|
374
|
+
brackets: false,
|
|
375
|
+
mariaDb: false
|
|
376
|
+
};
|
|
377
|
+
function newMySqlLexer() {
|
|
378
|
+
return {
|
|
379
|
+
index: 0,
|
|
380
|
+
start: 0,
|
|
381
|
+
inVersionComment: false,
|
|
382
|
+
modal: false,
|
|
383
|
+
rejected: false,
|
|
384
|
+
comment: false
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
/** The index after the `[name]` that opens at `open`, where `]]` stands for `]`. */
|
|
388
|
+
function closeMySqlBracket(sql, open) {
|
|
389
|
+
let index = open + 1;
|
|
390
|
+
for (;;) {
|
|
391
|
+
const close = sql.indexOf("]", index);
|
|
392
|
+
if (close === -1) return;
|
|
393
|
+
if (sql[close + 1] !== "]") return close + 1;
|
|
394
|
+
index = close + 2;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
const MYSQL_WORD = /[\w$\u0080-]+/uy;
|
|
398
|
+
/**
|
|
399
|
+
* The next word, semicolon or other character of MySQL SQL after any whitespace and comments,
|
|
400
|
+
* lowercased, or `undefined` at the end. A quoted string or name is one word, as written with
|
|
401
|
+
* its quotes. The SQL in an executable comment (`/*!`, and `/*M!` on MariaDB) counts when the
|
|
402
|
+
* server runs it.
|
|
403
|
+
*/
|
|
404
|
+
function readMySqlWord(sql, lexer, mode) {
|
|
405
|
+
while (lexer.index < sql.length) {
|
|
406
|
+
const index = lexer.index;
|
|
407
|
+
const character = sql[index];
|
|
408
|
+
const next = sql[index + 1];
|
|
409
|
+
lexer.start = index;
|
|
410
|
+
if (character === "'" || character === "\"" || character === "`") {
|
|
411
|
+
const name = character === "`" || character === "\"" && mode.ansiQuotes;
|
|
412
|
+
const end = closeMySqlQuoted(sql, index, character, mode.backslashEscapes && !name);
|
|
413
|
+
lexer.rejected ||= end === void 0;
|
|
414
|
+
lexer.index = end ?? sql.length;
|
|
415
|
+
const word = sql.slice(index, lexer.index);
|
|
416
|
+
lexer.modal ||= character !== "`" && word.includes("\\");
|
|
417
|
+
return word;
|
|
418
|
+
}
|
|
419
|
+
if (character === "[") {
|
|
420
|
+
lexer.modal ||= mode.mariaDb;
|
|
421
|
+
if (!mode.brackets) lexer.rejected = true;
|
|
422
|
+
else {
|
|
423
|
+
const end = closeMySqlBracket(sql, index);
|
|
424
|
+
lexer.rejected ||= end === void 0;
|
|
425
|
+
lexer.index = end ?? sql.length;
|
|
426
|
+
return sql.slice(index, lexer.index);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
if (character === "#" || character === "-" && next === "-" && startsMySqlDashComment(sql.charCodeAt(index + 2))) {
|
|
430
|
+
const newline = sql.indexOf("\n", index);
|
|
431
|
+
lexer.index = newline === -1 ? sql.length : newline;
|
|
432
|
+
lexer.comment = true;
|
|
433
|
+
} else if (lexer.inVersionComment && character === "*" && next === "/") {
|
|
434
|
+
lexer.inVersionComment = false;
|
|
435
|
+
lexer.index += 2;
|
|
436
|
+
} else if (character === "/" && next === "*") {
|
|
437
|
+
const executable = /^M?!/u.test(sql.slice(index + 2, index + 4));
|
|
438
|
+
const runsFrom = executable ? runsMySqlComment(sql, index + 2, mode) : void 0;
|
|
439
|
+
lexer.comment = true;
|
|
440
|
+
if (runsFrom !== void 0) {
|
|
441
|
+
lexer.inVersionComment = true;
|
|
442
|
+
lexer.index = runsFrom;
|
|
443
|
+
} else {
|
|
444
|
+
const end = skipMySqlComment(sql, index + 2, executable ? 1 : 0);
|
|
445
|
+
lexer.rejected ||= end === void 0;
|
|
446
|
+
lexer.index = end ?? sql.length;
|
|
447
|
+
}
|
|
448
|
+
} else if (/[ \t\n\v\f\r]/u.test(character)) lexer.index++;
|
|
449
|
+
else {
|
|
450
|
+
MYSQL_WORD.lastIndex = index;
|
|
451
|
+
const token = MYSQL_WORD.exec(sql)?.[0] ?? character;
|
|
452
|
+
lexer.index += token.length;
|
|
453
|
+
return token.toLowerCase();
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
const MYSQL_STORED_PROGRAMS = /* @__PURE__ */ new Set([
|
|
458
|
+
"procedure",
|
|
459
|
+
"function",
|
|
460
|
+
"trigger",
|
|
461
|
+
"event"
|
|
462
|
+
]);
|
|
463
|
+
const MYSQL_OTHER_OBJECTS = /* @__PURE__ */ new Set([
|
|
464
|
+
"database",
|
|
465
|
+
"fulltext",
|
|
466
|
+
"index",
|
|
467
|
+
"instance",
|
|
468
|
+
"logfile",
|
|
469
|
+
"resource",
|
|
470
|
+
"role",
|
|
471
|
+
"schema",
|
|
472
|
+
"sequence",
|
|
473
|
+
"server",
|
|
474
|
+
"spatial",
|
|
475
|
+
"table",
|
|
476
|
+
"tablespace",
|
|
477
|
+
"temporary",
|
|
478
|
+
"undo",
|
|
479
|
+
"unique",
|
|
480
|
+
"user",
|
|
481
|
+
"view"
|
|
482
|
+
]);
|
|
483
|
+
const MYSQL_CONTROL_BLOCKS = /* @__PURE__ */ new Set([
|
|
484
|
+
"if",
|
|
485
|
+
"loop",
|
|
486
|
+
"while",
|
|
487
|
+
"repeat",
|
|
488
|
+
"for"
|
|
489
|
+
]);
|
|
490
|
+
const MYSQL_PROCEDURE_CHARACTERISTICS = /* @__PURE__ */ new Set([
|
|
491
|
+
"comment",
|
|
492
|
+
"contains",
|
|
493
|
+
"data",
|
|
494
|
+
"definer",
|
|
495
|
+
"deterministic",
|
|
496
|
+
"invoker",
|
|
497
|
+
"language",
|
|
498
|
+
"modifies",
|
|
499
|
+
"no",
|
|
500
|
+
"not",
|
|
501
|
+
"reads",
|
|
502
|
+
"security",
|
|
503
|
+
"sql"
|
|
504
|
+
]);
|
|
505
|
+
const MYSQL_BODY_BLOCKS = /* @__PURE__ */ new Set([
|
|
506
|
+
"begin",
|
|
507
|
+
"case",
|
|
508
|
+
...MYSQL_CONTROL_BLOCKS
|
|
509
|
+
]);
|
|
510
|
+
const MYSQL_OPERAND_BEFORE = /* @__PURE__ */ new Set([
|
|
511
|
+
"and",
|
|
512
|
+
"between",
|
|
513
|
+
"binary",
|
|
514
|
+
"case",
|
|
515
|
+
"div",
|
|
516
|
+
"else",
|
|
517
|
+
"elseif",
|
|
518
|
+
"if",
|
|
519
|
+
"in",
|
|
520
|
+
"interval",
|
|
521
|
+
"is",
|
|
522
|
+
"like",
|
|
523
|
+
"mod",
|
|
524
|
+
"not",
|
|
525
|
+
"of",
|
|
526
|
+
"or",
|
|
527
|
+
"regexp",
|
|
528
|
+
"return",
|
|
529
|
+
"rlike",
|
|
530
|
+
"then",
|
|
531
|
+
"until",
|
|
532
|
+
"when",
|
|
533
|
+
"while",
|
|
534
|
+
"xor"
|
|
535
|
+
]);
|
|
536
|
+
/** Whether an operand follows `previous`, the word before it in an expression. */
|
|
537
|
+
function expectsMySqlOperand(previous) {
|
|
538
|
+
return previous === void 0 || MYSQL_OPERAND_BEFORE.has(previous) || previous.length === 1 && !/[\w$)?}\u0080-]/u.test(previous);
|
|
539
|
+
}
|
|
540
|
+
const MYSQL_HANDLER_ACTIONS = /* @__PURE__ */ new Set([
|
|
541
|
+
"continue",
|
|
542
|
+
"exit",
|
|
543
|
+
"undo"
|
|
544
|
+
]);
|
|
545
|
+
/** Where a handler's conditions are after `word`, or `'statement'` when its statement starts. */
|
|
546
|
+
function nextMySqlHandlerPart(part, word) {
|
|
547
|
+
switch (part) {
|
|
548
|
+
case "for": return word === "for" ? "condition" : void 0;
|
|
549
|
+
case "condition": return word === "sqlstate" ? "sqlstate" : word === "not" ? "last" : "after";
|
|
550
|
+
case "sqlstate": return word === "value" ? "last" : "after";
|
|
551
|
+
case "last": return "after";
|
|
552
|
+
default: return word === "," ? "condition" : "statement";
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Close the innermost block at an END. `next` is the word after it, which names the block for
|
|
557
|
+
* END IF, END LOOP, END WHILE, END REPEAT, END FOR and END CASE. Returns whether `next` belongs
|
|
558
|
+
* to the END.
|
|
559
|
+
*/
|
|
560
|
+
function closeMySqlBlock(blocks, next) {
|
|
561
|
+
const top = blocks.at(-1)?.kind;
|
|
562
|
+
if (top === "caseExpression") {
|
|
563
|
+
blocks.pop();
|
|
564
|
+
return next === "case";
|
|
565
|
+
}
|
|
566
|
+
if (next !== void 0 && MYSQL_CONTROL_BLOCKS.has(next)) {
|
|
567
|
+
if (top === next || next === "loop" && (top === "while" || top === "for")) blocks.pop();
|
|
568
|
+
return true;
|
|
569
|
+
}
|
|
570
|
+
if (next === "case") {
|
|
571
|
+
if (top === "case") blocks.pop();
|
|
572
|
+
return true;
|
|
573
|
+
}
|
|
574
|
+
for (let index = blocks.length - 1; index >= 0; index--) if (blocks[index].kind === "begin" || blocks[index].kind === "case") {
|
|
575
|
+
blocks.length = index;
|
|
576
|
+
break;
|
|
577
|
+
}
|
|
578
|
+
return false;
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Read one top-level statement of MySQL SQL the way a server in `mode` does, from where `lexer`
|
|
582
|
+
* is to its `;` or the end of the SQL.
|
|
583
|
+
*
|
|
584
|
+
* A statement ends at a semicolon outside any block. A stored program's body (`CREATE
|
|
585
|
+
* PROCEDURE`, `FUNCTION`, `TRIGGER` or `EVENT`, and `ALTER EVENT`) holds statements of its own:
|
|
586
|
+
* a BEGIN block, or an IF, CASE, LOOP, WHILE, REPEAT or MariaDB FOR statement, which may also
|
|
587
|
+
* stand alone as the body. MariaDB also runs these, and BEGIN NOT ATOMIC blocks, outside stored
|
|
588
|
+
* programs. Each opens a block only where a statement starts, a handler's statement among them,
|
|
589
|
+
* so the IF() function and DROP TABLE IF EXISTS do not, and BEGIN elsewhere starts a transaction
|
|
590
|
+
* or is a name. END closes a block where a statement starts too, and ends a CASE expression or
|
|
591
|
+
* a REPEAT's UNTIL condition after an operand; elsewhere it is a name, as `begin` and `end`
|
|
592
|
+
* columns are. A CASE expression nests anywhere. A word after a dot or right after an `@` is a
|
|
593
|
+
* name, such as the column in `t.case` or the variable `@case`, as is the account in
|
|
594
|
+
* `DEFINER = account`, but the dot in a number such as `1.` is its decimal point.
|
|
595
|
+
*/
|
|
596
|
+
function readMySqlStatement(sql, lexer, mode) {
|
|
597
|
+
lexer.modal = false;
|
|
598
|
+
lexer.rejected = false;
|
|
599
|
+
lexer.comment = false;
|
|
600
|
+
const words = [];
|
|
601
|
+
const blocks = [];
|
|
602
|
+
let program;
|
|
603
|
+
let otherObject = false;
|
|
604
|
+
let head = "name";
|
|
605
|
+
let parentheses = 0;
|
|
606
|
+
let returns = false;
|
|
607
|
+
let depth = 0;
|
|
608
|
+
let handler;
|
|
609
|
+
let startsNext = true;
|
|
610
|
+
let startedLast = false;
|
|
611
|
+
let namesBlock = false;
|
|
612
|
+
let wordEnd = -1;
|
|
613
|
+
let named = false;
|
|
614
|
+
let decimalPoint = false;
|
|
615
|
+
const result = (terminated) => ({
|
|
616
|
+
words,
|
|
617
|
+
end: lexer.index,
|
|
618
|
+
inVersionComment: lexer.inVersionComment,
|
|
619
|
+
terminated,
|
|
620
|
+
modal: lexer.modal,
|
|
621
|
+
rejected: lexer.rejected,
|
|
622
|
+
comment: lexer.comment
|
|
623
|
+
});
|
|
624
|
+
const ahead = (count) => {
|
|
625
|
+
const copy = { ...lexer };
|
|
626
|
+
return Array.from({ length: count }, () => readMySqlWord(sql, copy, mode));
|
|
627
|
+
};
|
|
628
|
+
for (let word = readMySqlWord(sql, lexer, mode); word !== void 0; word = readMySqlWord(sql, lexer, mode)) {
|
|
629
|
+
const touches = lexer.start === wordEnd;
|
|
630
|
+
wordEnd = lexer.index;
|
|
631
|
+
if (word === ";") {
|
|
632
|
+
if (blocks.length === 0) return result(true);
|
|
633
|
+
words.push(word);
|
|
634
|
+
startsNext = true;
|
|
635
|
+
startedLast = false;
|
|
636
|
+
namesBlock = false;
|
|
637
|
+
named = false;
|
|
638
|
+
decimalPoint = false;
|
|
639
|
+
handler = void 0;
|
|
640
|
+
continue;
|
|
641
|
+
}
|
|
642
|
+
const previous = words.at(-1);
|
|
643
|
+
const afterDecimalPoint = decimalPoint;
|
|
644
|
+
const operandBefore = !named && !afterDecimalPoint && expectsMySqlOperand(previous);
|
|
645
|
+
decimalPoint = word === "." && touches && /^\d+$/u.test(previous ?? "") && !named;
|
|
646
|
+
named = false;
|
|
647
|
+
words.push(word);
|
|
648
|
+
let starts = startsNext;
|
|
649
|
+
startsNext = false;
|
|
650
|
+
const afterLabel = word === ":" && startedLast;
|
|
651
|
+
startedLast = false;
|
|
652
|
+
const closing = namesBlock;
|
|
653
|
+
namesBlock = false;
|
|
654
|
+
if (word === "(") depth++;
|
|
655
|
+
else if (word === ")") depth--;
|
|
656
|
+
if (previous === "." && !afterDecimalPoint || previous === "@" && touches) {
|
|
657
|
+
named = true;
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
660
|
+
if ((words[0] === "create" || words[0] === "alter") && program === void 0 && !otherObject) {
|
|
661
|
+
if (previous === "=" && words.at(-3) === "definer") continue;
|
|
662
|
+
if (MYSQL_STORED_PROGRAMS.has(word)) {
|
|
663
|
+
program = word;
|
|
664
|
+
continue;
|
|
665
|
+
}
|
|
666
|
+
otherObject = MYSQL_OTHER_OBJECTS.has(word);
|
|
667
|
+
} else if (program !== void 0 && head !== "body") {
|
|
668
|
+
const routine = program === "procedure" || program === "function";
|
|
669
|
+
if (head === "name") {
|
|
670
|
+
if (word === "(" && routine) {
|
|
671
|
+
head = "parameters";
|
|
672
|
+
parentheses = 1;
|
|
673
|
+
} else if (program === "trigger" && word === "row" && previous === "each") head = "triggerOrder";
|
|
674
|
+
else if (program === "event" && word === "do" && ![
|
|
675
|
+
"event",
|
|
676
|
+
"exists",
|
|
677
|
+
"to"
|
|
678
|
+
].includes(previous ?? "") || routine && (word === "as" || word === "is")) {
|
|
679
|
+
head = "body";
|
|
680
|
+
startsNext = true;
|
|
681
|
+
}
|
|
682
|
+
} else if (head === "parameters") {
|
|
683
|
+
if (word === "(") parentheses++;
|
|
684
|
+
else if (word === ")" && --parentheses === 0) head = "characteristics";
|
|
685
|
+
} else if (head === "characteristics") {
|
|
686
|
+
if (word === ":" || word === "as" || word === "is") {
|
|
687
|
+
head = "body";
|
|
688
|
+
startsNext = true;
|
|
689
|
+
} else if (program === "procedure") {
|
|
690
|
+
if (!MYSQL_PROCEDURE_CHARACTERISTICS.has(word) && !/^['"]/u.test(word)) {
|
|
691
|
+
head = "body";
|
|
692
|
+
starts = true;
|
|
693
|
+
}
|
|
694
|
+
} else if (word === "return" || MYSQL_BODY_BLOCKS.has(word)) {
|
|
695
|
+
head = "body";
|
|
696
|
+
starts = true;
|
|
697
|
+
returns = word === "return";
|
|
698
|
+
}
|
|
699
|
+
} else if (head === "triggerOrder") {
|
|
700
|
+
if (word === "follows" || word === "precedes") head = "triggerOrderName";
|
|
701
|
+
else {
|
|
702
|
+
head = "body";
|
|
703
|
+
starts = true;
|
|
704
|
+
}
|
|
705
|
+
} else {
|
|
706
|
+
head = "body";
|
|
707
|
+
startsNext = true;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
if (handler !== void 0) {
|
|
711
|
+
const part = nextMySqlHandlerPart(handler, word);
|
|
712
|
+
handler = part === "statement" ? void 0 : part;
|
|
713
|
+
starts ||= part === "statement";
|
|
714
|
+
} else if (word === "handler" && blocks.length > 0 && MYSQL_HANDLER_ACTIONS.has(previous ?? "") && words.at(-3) === "declare") handler = "for";
|
|
715
|
+
const top = blocks.at(-1);
|
|
716
|
+
if (closing && (MYSQL_CONTROL_BLOCKS.has(word) || word === "case")) {} else if (starts && MYSQL_CONTROL_BLOCKS.has(word)) {
|
|
717
|
+
blocks.push({
|
|
718
|
+
kind: word,
|
|
719
|
+
depth,
|
|
720
|
+
past: false
|
|
721
|
+
});
|
|
722
|
+
startsNext = word === "loop" || word === "repeat";
|
|
723
|
+
} else if (word === "case") blocks.push({
|
|
724
|
+
kind: starts ? "case" : "caseExpression",
|
|
725
|
+
depth,
|
|
726
|
+
past: false
|
|
727
|
+
});
|
|
728
|
+
else if (word === "begin" && starts && (program !== void 0 || blocks.length > 0 || ahead(2).join(" ") === "not atomic")) {
|
|
729
|
+
blocks.push({
|
|
730
|
+
kind: "begin",
|
|
731
|
+
depth,
|
|
732
|
+
past: false
|
|
733
|
+
});
|
|
734
|
+
startsNext = true;
|
|
735
|
+
} else if (word === "end" && top !== void 0 && (starts || (top.kind === "caseExpression" || top.kind === "repeat" && top.past) && top.depth === depth && !operandBefore)) namesBlock = closeMySqlBlock(blocks, ahead(1)[0]);
|
|
736
|
+
else if (word === "until" && starts && top?.kind === "repeat") top.past = true;
|
|
737
|
+
else if (word === "do" && (top?.kind === "while" || top?.kind === "for") && !top.past && top.depth === depth && !operandBefore) {
|
|
738
|
+
top.past = true;
|
|
739
|
+
startsNext = true;
|
|
740
|
+
} else if ((word === "then" || word === "else") && top?.kind !== "caseExpression" || word === "loop" || afterLabel || word === "atomic" && previous === "not" && words.at(-3) === "begin" || returns && blocks.length === 0 && depth === 0 && (word === "as" || word === "is")) startsNext = true;
|
|
741
|
+
startedLast = starts;
|
|
742
|
+
}
|
|
743
|
+
return result(false);
|
|
744
|
+
}
|
|
745
|
+
const MYSQL_ANSI_QUOTES_MODES = /* @__PURE__ */ new Set([
|
|
746
|
+
"ANSI",
|
|
747
|
+
"ANSI_QUOTES",
|
|
748
|
+
"DB2",
|
|
749
|
+
"MAXDB",
|
|
750
|
+
"MSSQL",
|
|
751
|
+
"ORACLE",
|
|
752
|
+
"POSTGRESQL"
|
|
753
|
+
]);
|
|
754
|
+
const MYSQL_GLOBAL_SCOPES = /* @__PURE__ */ new Set([
|
|
755
|
+
"global",
|
|
756
|
+
"persist",
|
|
757
|
+
"persist_only"
|
|
758
|
+
]);
|
|
759
|
+
const MYSQL_SESSION_SCOPES = /* @__PURE__ */ new Set(["session", "local"]);
|
|
760
|
+
/** Whether a word is a quoted name. */
|
|
761
|
+
function isMySqlQuotedName(word, mode) {
|
|
762
|
+
const quote = word?.[0];
|
|
763
|
+
return quote === "`" || quote === "\"" && mode.ansiQuotes || quote === "[" && mode.brackets;
|
|
764
|
+
}
|
|
765
|
+
/** The name a word gives, unquoted and lowercased. */
|
|
766
|
+
function mySqlName(word, mode) {
|
|
767
|
+
if (isMySqlQuotedName(word, mode)) {
|
|
768
|
+
const close = word[0] === "[" ? "]" : word[0];
|
|
769
|
+
return word.slice(1, -1).replaceAll(`${close}${close}`, close).toLowerCase();
|
|
770
|
+
}
|
|
771
|
+
return word;
|
|
772
|
+
}
|
|
773
|
+
/** Whether a word is a quoted string, not a name. */
|
|
774
|
+
function isMySqlString(word, mode) {
|
|
775
|
+
const quote = word?.[0];
|
|
776
|
+
return word !== void 0 && (quote === "'" || quote === "\"" && !mode.ansiQuotes) && word.length >= 2 && word.endsWith(quote);
|
|
777
|
+
}
|
|
778
|
+
const MYSQL_ESCAPES = /* @__PURE__ */ new Map([
|
|
779
|
+
["0", "\0"],
|
|
780
|
+
["b", "\b"],
|
|
781
|
+
["n", "\n"],
|
|
782
|
+
["r", "\r"],
|
|
783
|
+
["t", " "],
|
|
784
|
+
["Z", ""],
|
|
785
|
+
["%", String.raw`\%`],
|
|
786
|
+
["_", String.raw`\_`]
|
|
787
|
+
]);
|
|
788
|
+
/** The text of a quoted string. */
|
|
789
|
+
function mySqlStringText(word, mode) {
|
|
790
|
+
const quote = word[0];
|
|
791
|
+
let text = "";
|
|
792
|
+
for (let index = 1; index < word.length - 1; index++) {
|
|
793
|
+
const character = word[index];
|
|
794
|
+
if (character === "\\" && mode.backslashEscapes) {
|
|
795
|
+
const escaped = word[++index];
|
|
796
|
+
text += MYSQL_ESCAPES.get(escaped) ?? escaped;
|
|
797
|
+
} else {
|
|
798
|
+
text += character;
|
|
799
|
+
if (character === quote) index++;
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
return text;
|
|
803
|
+
}
|
|
804
|
+
/** The quoting rules a quoted list of modes, such as `'ANSI,NO_BACKSLASH_ESCAPES'`, sets. */
|
|
805
|
+
function mySqlModeList(value, mode) {
|
|
806
|
+
const [word] = value;
|
|
807
|
+
if (value.length !== 1 || !isMySqlString(word, mode)) return "unclear";
|
|
808
|
+
const text = word.slice(1, -1);
|
|
809
|
+
if (!/^[A-Za-z_,]*$/u.test(text)) return "unclear";
|
|
810
|
+
const modes = text.toUpperCase().split(",");
|
|
811
|
+
return {
|
|
812
|
+
backslashEscapes: !modes.includes("NO_BACKSLASH_ESCAPES"),
|
|
813
|
+
ansiQuotes: modes.some((name) => MYSQL_ANSI_QUOTES_MODES.has(name)),
|
|
814
|
+
brackets: mode.mariaDb && modes.includes("MSSQL")
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
/** Split words at top-level commas, or at the first top-level word `at` when one is given. */
|
|
818
|
+
function splitMySqlWords(words, at = ",") {
|
|
819
|
+
let current = [];
|
|
820
|
+
const parts = [current];
|
|
821
|
+
let parentheses = 0;
|
|
822
|
+
for (const word of words) {
|
|
823
|
+
if (word === at && parentheses === 0 && (at === "," || parts.length === 1)) {
|
|
824
|
+
current = [];
|
|
825
|
+
parts.push(current);
|
|
826
|
+
continue;
|
|
827
|
+
}
|
|
828
|
+
if (word === "(") parentheses++;
|
|
829
|
+
else if (word === ")") parentheses--;
|
|
830
|
+
current.push(word);
|
|
831
|
+
}
|
|
832
|
+
return parts;
|
|
833
|
+
}
|
|
834
|
+
/**
|
|
835
|
+
* How a SET statement changes the session's quoting rules. `SET GLOBAL` leaves them alone, as
|
|
836
|
+
* does a SET of a user variable.
|
|
837
|
+
*/
|
|
838
|
+
function mySqlSetChange(words, mode) {
|
|
839
|
+
const assignments = splitMySqlWords(words.slice(1));
|
|
840
|
+
let change;
|
|
841
|
+
let global = false;
|
|
842
|
+
for (const assignment of assignments) {
|
|
843
|
+
let index = 0;
|
|
844
|
+
if (MYSQL_GLOBAL_SCOPES.has(assignment[index])) {
|
|
845
|
+
global = true;
|
|
846
|
+
index++;
|
|
847
|
+
} else if (MYSQL_SESSION_SCOPES.has(assignment[index])) {
|
|
848
|
+
global = false;
|
|
849
|
+
index++;
|
|
850
|
+
}
|
|
851
|
+
let targetsGlobal = global;
|
|
852
|
+
if (assignment[index] === "@") {
|
|
853
|
+
if (assignment[index + 1] !== "@") continue;
|
|
854
|
+
index += 2;
|
|
855
|
+
targetsGlobal = false;
|
|
856
|
+
if (assignment[index + 1] === ".") {
|
|
857
|
+
targetsGlobal = MYSQL_GLOBAL_SCOPES.has(assignment[index]);
|
|
858
|
+
index += 2;
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
if (targetsGlobal || mySqlName(assignment[index], mode) !== "sql_mode") continue;
|
|
862
|
+
index++;
|
|
863
|
+
if (assignment[index] === ":") index++;
|
|
864
|
+
change = assignment[index] === "=" ? mySqlModeList(assignment.slice(index + 1), mode) : "unclear";
|
|
865
|
+
}
|
|
866
|
+
return change;
|
|
867
|
+
}
|
|
868
|
+
const MYSQL_SINGLE = {
|
|
869
|
+
kind: "single",
|
|
870
|
+
change: void 0
|
|
871
|
+
};
|
|
872
|
+
const MYSQL_REJECTED = {
|
|
873
|
+
...MYSQL_SINGLE,
|
|
874
|
+
rejected: true
|
|
875
|
+
};
|
|
876
|
+
/**
|
|
877
|
+
* Whether the server rejects the text a PREPARE or EXECUTE IMMEDIATE is given: a quoted name
|
|
878
|
+
* there, such as a double-quoted text in `ANSI_QUOTES` mode, names no statement.
|
|
879
|
+
*/
|
|
880
|
+
function rejectsMySqlSource(source, mode) {
|
|
881
|
+
return source.length === 1 && isMySqlQuotedName(source[0], mode);
|
|
882
|
+
}
|
|
883
|
+
/** The statement in a PREPARE's or EXECUTE IMMEDIATE's text, when it is a quoted string. */
|
|
884
|
+
function mySqlPreparedStatement(source, mode) {
|
|
885
|
+
const [word] = source;
|
|
886
|
+
if (source.length !== 1 || !isMySqlString(word, mode)) return;
|
|
887
|
+
const { words } = readMySqlStatement(mySqlStringText(word, mode), newMySqlLexer(), mode);
|
|
888
|
+
const { kind, change } = mySqlStatementEffect(words, mode, /* @__PURE__ */ new Map());
|
|
889
|
+
return {
|
|
890
|
+
kind,
|
|
891
|
+
change
|
|
892
|
+
};
|
|
893
|
+
}
|
|
894
|
+
/**
|
|
895
|
+
* What a top-level statement does. A CALL, and a compound statement that MariaDB runs outside
|
|
896
|
+
* a stored program (a BEGIN NOT ATOMIC block, IF, CASE, LOOP, WHILE, REPEAT or FOR), return
|
|
897
|
+
* their results like a CALL. So does an EXECUTE, or MariaDB's EXECUTE IMMEDIATE, of a CALL.
|
|
898
|
+
* MariaDB's SET STATEMENT ... FOR returns what the statement after FOR does, and keeps what it
|
|
899
|
+
* changes unless it sets sql_mode itself, which it then restores. A stored program, and a
|
|
900
|
+
* compound statement, run with a sql_mode of their own and restore the session's.
|
|
901
|
+
*/
|
|
902
|
+
function mySqlStatementEffect(words, mode, prepared) {
|
|
903
|
+
const [first, second] = words;
|
|
904
|
+
if (first === "call" || first === "case" || MYSQL_CONTROL_BLOCKS.has(first) || first === "begin" && second === "not" && words[2] === "atomic") return {
|
|
905
|
+
kind: "call",
|
|
906
|
+
change: void 0
|
|
907
|
+
};
|
|
908
|
+
if (first === "execute") {
|
|
909
|
+
if (second === "immediate" && words.length > 2 && words[2] !== "using") {
|
|
910
|
+
const [source] = splitMySqlWords(words.slice(2), "using");
|
|
911
|
+
return {
|
|
912
|
+
...mySqlPreparedStatement(source, mode) ?? {
|
|
913
|
+
kind: "unknown",
|
|
914
|
+
change: "unclear"
|
|
915
|
+
},
|
|
916
|
+
rejected: rejectsMySqlSource(source, mode)
|
|
917
|
+
};
|
|
918
|
+
}
|
|
919
|
+
if (isMySqlString(second, mode)) return MYSQL_REJECTED;
|
|
920
|
+
const name = mySqlName(second, mode) ?? "";
|
|
921
|
+
return {
|
|
922
|
+
...prepared.get(name) ?? {
|
|
923
|
+
kind: "unknown",
|
|
924
|
+
change: "unclear"
|
|
925
|
+
},
|
|
926
|
+
executes: name
|
|
927
|
+
};
|
|
928
|
+
}
|
|
929
|
+
if (first === "prepare" && words[2] === "from") {
|
|
930
|
+
if (isMySqlString(second, mode)) return MYSQL_REJECTED;
|
|
931
|
+
const source = words.slice(3);
|
|
932
|
+
return {
|
|
933
|
+
...MYSQL_SINGLE,
|
|
934
|
+
prepares: {
|
|
935
|
+
name: mySqlName(second, mode),
|
|
936
|
+
statement: mySqlPreparedStatement(source, mode)
|
|
937
|
+
},
|
|
938
|
+
rejected: rejectsMySqlSource(source, mode)
|
|
939
|
+
};
|
|
940
|
+
}
|
|
941
|
+
if ((first === "deallocate" || first === "drop") && second === "prepare") {
|
|
942
|
+
if (isMySqlString(words[2], mode)) return MYSQL_REJECTED;
|
|
943
|
+
return {
|
|
944
|
+
...MYSQL_SINGLE,
|
|
945
|
+
prepares: {
|
|
946
|
+
name: mySqlName(words[2], mode),
|
|
947
|
+
statement: void 0,
|
|
948
|
+
deallocates: true
|
|
949
|
+
}
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
if (first === "set" && second === "statement") {
|
|
953
|
+
const [settings, statement] = splitMySqlWords(words.slice(2), "for");
|
|
954
|
+
if (statement === void 0) return MYSQL_SINGLE;
|
|
955
|
+
const effect = mySqlStatementEffect(statement, mode, prepared);
|
|
956
|
+
return settings.some((word) => mySqlName(word, mode) === "sql_mode") ? {
|
|
957
|
+
...effect,
|
|
958
|
+
change: void 0
|
|
959
|
+
} : effect;
|
|
960
|
+
}
|
|
961
|
+
if (first === "set") return {
|
|
962
|
+
kind: "single",
|
|
963
|
+
change: mySqlSetChange(words, mode)
|
|
964
|
+
};
|
|
965
|
+
return MYSQL_SINGLE;
|
|
966
|
+
}
|
|
967
|
+
/**
|
|
968
|
+
* Each quoting mode a server of `mode`'s version and family may be in. Only MariaDB reads
|
|
969
|
+
* square brackets as quotes, in its MSSQL mode.
|
|
970
|
+
*/
|
|
971
|
+
function mySqlQuotingModes(mode) {
|
|
972
|
+
return [true, false].flatMap((backslashEscapes) => [false, true].flatMap((ansiQuotes) => (mode.mariaDb ? [false, true] : [false]).map((brackets) => ({
|
|
973
|
+
...mode,
|
|
974
|
+
backslashEscapes,
|
|
975
|
+
ansiQuotes,
|
|
976
|
+
brackets
|
|
977
|
+
}))));
|
|
978
|
+
}
|
|
979
|
+
/** Whether two modes read quoted text the same. */
|
|
980
|
+
function sameMySqlQuoting(a, b) {
|
|
981
|
+
return a.backslashEscapes === b.backslashEscapes && a.ansiQuotes === b.ansiQuotes && a.brackets === b.brackets;
|
|
982
|
+
}
|
|
983
|
+
/** The modes a server in `mode` may be in after a statement that makes `change`. */
|
|
984
|
+
function nextMySqlModes(mode, change) {
|
|
985
|
+
if (change === "unclear") return mySqlQuotingModes(mode);
|
|
986
|
+
return [change === void 0 ? mode : {
|
|
987
|
+
...mode,
|
|
988
|
+
...change
|
|
989
|
+
}];
|
|
990
|
+
}
|
|
991
|
+
/** Record what a PREPARE or DEALLOCATE does, the same in every mode the server may be in. */
|
|
992
|
+
function applyMySqlPrepares(prepared, effects) {
|
|
993
|
+
if (effects[0].kind === "call") {
|
|
994
|
+
const { executes } = effects[0];
|
|
995
|
+
for (const name of prepared.keys()) if (name !== executes) prepared.delete(name);
|
|
996
|
+
return;
|
|
997
|
+
}
|
|
998
|
+
const [{ prepares }] = effects;
|
|
999
|
+
if (prepares === void 0) return;
|
|
1000
|
+
const agree = effects.every((effect) => JSON.stringify(effect.prepares) === JSON.stringify(prepares));
|
|
1001
|
+
for (const effect of effects) {
|
|
1002
|
+
const name = effect.prepares?.name;
|
|
1003
|
+
if (name !== void 0) prepared.delete(name);
|
|
1004
|
+
}
|
|
1005
|
+
if (agree && prepares.name !== void 0 && prepares.statement !== void 0) prepared.set(prepares.name, prepares.statement);
|
|
1006
|
+
}
|
|
1007
|
+
const MYSQL_UNKNOWN_EXECUTE = "db: `void db execute --json` cannot tell whether an EXECUTE in this MySQL SQL runs a CALL, which returns several results, so it cannot group them. It reads the statement that a PREPARE or EXECUTE IMMEDIATE in the same SQL takes as a quoted string, such as PREPARE s FROM 'CALL p()', but a procedure may prepare a statement again, so it forgets the others after a CALL. Prepare the statement again after the CALL, or run SQL that prepares a statement from a variable or an expression without --json.";
|
|
1008
|
+
/** The error for SQL whose statements this scan cannot tell apart, which says what it cannot do. */
|
|
1009
|
+
function unclearMySqlStatements(consequence, remedy) {
|
|
1010
|
+
return /* @__PURE__ */ new Error(`db: \`void db execute --json\` cannot tell where the statements of this MySQL SQL end, so it ${consequence}. A statement in it changes sql_mode in a way the command cannot read, such as SET sql_mode = @saved, and the mode decides how quoted text reads, such as what a backslash in a string means. Set sql_mode to a quoted list of modes, such as SET sql_mode = 'ANSI_QUOTES', or ${remedy}.`);
|
|
1011
|
+
}
|
|
1012
|
+
const MYSQL_UNCLEAR_CALL = () => unclearMySqlStatements("cannot group the results of a statement after it that returns several, such as a CALL", "run that statement in a separate command");
|
|
1013
|
+
/**
|
|
1014
|
+
* Find MySQL's statements (see `readMySqlStatement`) and tell which return their results like a
|
|
1015
|
+
* CALL (see `mySqlStatementEffect`), reading the SQL the way a server in `mode` does.
|
|
1016
|
+
*
|
|
1017
|
+
* The server reads each statement with the `sql_mode` the statements before it left, so a
|
|
1018
|
+
* `SET sql_mode` to a quoted list of modes changes how the SQL after it reads. After a statement
|
|
1019
|
+
* that changes it in a way this scan cannot read, the SQL is read in each quoting mode the
|
|
1020
|
+
* server may be in, leaving out a reading that leaves a quoted string open, or finds a `[` that
|
|
1021
|
+
* quotes no name, which the server rejects. Where they read different statements, the statements are `uncounted` when none of
|
|
1022
|
+
* them can return results like a CALL, and otherwise this throws before the SQL runs, since
|
|
1023
|
+
* their results could not be grouped. It also throws for an EXECUTE of a statement it cannot
|
|
1024
|
+
* see, which may be a CALL.
|
|
1025
|
+
*/
|
|
1026
|
+
function mySqlStatements(sql, initialMode = MYSQL_DEFAULT_MODE) {
|
|
1027
|
+
const lexer = newMySqlLexer();
|
|
1028
|
+
let modes = [initialMode];
|
|
1029
|
+
const prepared = /* @__PURE__ */ new Map();
|
|
1030
|
+
const calls = [];
|
|
1031
|
+
let cut;
|
|
1032
|
+
let trailingComment = false;
|
|
1033
|
+
let read;
|
|
1034
|
+
do {
|
|
1035
|
+
const from = { ...lexer };
|
|
1036
|
+
read = readMySqlStatement(sql, lexer, modes[0]);
|
|
1037
|
+
let readings = modes.map((mode, index) => ({
|
|
1038
|
+
mode,
|
|
1039
|
+
read: index === 0 || !read.modal ? read : readMySqlStatement(sql, { ...from }, mode)
|
|
1040
|
+
}));
|
|
1041
|
+
const accepted = readings.filter((reading) => !reading.read.rejected);
|
|
1042
|
+
if (accepted.length > 0) readings = accepted;
|
|
1043
|
+
const [{ read: chosen }] = readings;
|
|
1044
|
+
read = chosen;
|
|
1045
|
+
if (readings.some((reading) => !sameMySqlStatement(reading.read, chosen))) return divergedMySqlStatements(sql, from, readings.map((reading) => reading.mode), {
|
|
1046
|
+
calls,
|
|
1047
|
+
prepared
|
|
1048
|
+
});
|
|
1049
|
+
lexer.index = read.end;
|
|
1050
|
+
lexer.inVersionComment = read.inVersionComment;
|
|
1051
|
+
if (read.words.length === 0) {
|
|
1052
|
+
trailingComment ||= read.comment;
|
|
1053
|
+
cut = mySqlCut(cut, read);
|
|
1054
|
+
continue;
|
|
1055
|
+
}
|
|
1056
|
+
let effects = readings.map((reading) => mySqlStatementEffect(read.words, reading.mode, prepared));
|
|
1057
|
+
if (effects.some((effect) => !effect.rejected)) {
|
|
1058
|
+
readings = readings.filter((_, index) => !effects[index].rejected);
|
|
1059
|
+
effects = effects.filter((effect) => !effect.rejected);
|
|
1060
|
+
}
|
|
1061
|
+
if (effects.some((effect) => effect.kind === "unknown")) throw new Error(MYSQL_UNKNOWN_EXECUTE);
|
|
1062
|
+
if (effects.some((effect) => effect.kind !== effects[0].kind)) throw MYSQL_UNCLEAR_CALL();
|
|
1063
|
+
calls.push(effects[0].kind === "call");
|
|
1064
|
+
applyMySqlPrepares(prepared, effects);
|
|
1065
|
+
const next = readings.flatMap((reading, index) => nextMySqlModes(reading.mode, effects[index].change));
|
|
1066
|
+
modes = next.filter((mode, index) => next.findIndex((other) => sameMySqlQuoting(other, mode)) === index);
|
|
1067
|
+
cut = mySqlCut(void 0, read);
|
|
1068
|
+
trailingComment = false;
|
|
1069
|
+
} while (read.terminated);
|
|
1070
|
+
return {
|
|
1071
|
+
calls,
|
|
1072
|
+
uncounted: false,
|
|
1073
|
+
sql: trailingComment && cut !== void 0 ? sql.slice(0, cut) : sql
|
|
1074
|
+
};
|
|
1075
|
+
}
|
|
1076
|
+
/**
|
|
1077
|
+
* Where the SQL may be cut to leave out what follows a statement, after reading `read`: at the
|
|
1078
|
+
* end of a statement, unless an executable comment is still open there, and not where comments
|
|
1079
|
+
* that follow leave one open, which the server rejects.
|
|
1080
|
+
*/
|
|
1081
|
+
function mySqlCut(cut, read) {
|
|
1082
|
+
if (read.words.length > 0) return read.inVersionComment ? void 0 : read.end;
|
|
1083
|
+
return read.rejected ? void 0 : cut;
|
|
1084
|
+
}
|
|
1085
|
+
/** Whether two readings of a statement find the same words and end in the same place. */
|
|
1086
|
+
function sameMySqlStatement(a, b) {
|
|
1087
|
+
return a === b || a.end === b.end && a.inVersionComment === b.inVersionComment && a.words.length === b.words.length && a.words.every((word, index) => word === b.words[index]);
|
|
1088
|
+
}
|
|
1089
|
+
/**
|
|
1090
|
+
* Record a PREPARE that one way of reading diverged SQL finds, in the statements all the ways
|
|
1091
|
+
* share. Returns 1 when that forgets a statement some way may have read an EXECUTE with.
|
|
1092
|
+
*
|
|
1093
|
+
* A name keeps a statement while every way that prepares it prepares the same one as before,
|
|
1094
|
+
* or the first time. A way that does not prepare it then reads the statement prepared before,
|
|
1095
|
+
* which is the same, or none, and the server rejects its EXECUTE. Otherwise the name is
|
|
1096
|
+
* forgotten for every way. A DEALLOCATE changes nothing here: its own way then fails on the
|
|
1097
|
+
* server at any EXECUTE of the name, and the others still read the statement.
|
|
1098
|
+
*/
|
|
1099
|
+
function prepareDivergedMySql(effect, before, prepared, conflicts) {
|
|
1100
|
+
const { name, statement, deallocates } = effect.prepares ?? {};
|
|
1101
|
+
if (name === void 0 || deallocates || conflicts.has(name)) return 0;
|
|
1102
|
+
const same = (other) => other === void 0 || JSON.stringify(other) === JSON.stringify(statement);
|
|
1103
|
+
if (statement !== void 0 && same(prepared.get(name)) && same(before.get(name))) {
|
|
1104
|
+
prepared.set(name, statement);
|
|
1105
|
+
return 0;
|
|
1106
|
+
}
|
|
1107
|
+
conflicts.add(name);
|
|
1108
|
+
return prepared.delete(name) ? 1 : 0;
|
|
1109
|
+
}
|
|
1110
|
+
/**
|
|
1111
|
+
* The statements of SQL whose quoting modes read the statement at `from` differently. Every way
|
|
1112
|
+
* the server may read the rest is followed to the end. They are counted only where they hold
|
|
1113
|
+
* no statement that returns results like a CALL, and comments after the last statement are left
|
|
1114
|
+
* out only where every way agrees on where they start.
|
|
1115
|
+
*
|
|
1116
|
+
* The ways share their prepared statements (see `prepareDivergedMySql`), which tells less than
|
|
1117
|
+
* each way knows, never more, and keeps the walk short.
|
|
1118
|
+
*/
|
|
1119
|
+
function divergedMySqlStatements(sql, from, modes, before) {
|
|
1120
|
+
const prepared = new Map(before.prepared);
|
|
1121
|
+
const conflicts = /* @__PURE__ */ new Set();
|
|
1122
|
+
let forgotten = 0;
|
|
1123
|
+
const ends = /* @__PURE__ */ new Set();
|
|
1124
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1125
|
+
const readings = modes.map((mode) => ({
|
|
1126
|
+
lexer: { ...from },
|
|
1127
|
+
mode,
|
|
1128
|
+
cut: void 0
|
|
1129
|
+
}));
|
|
1130
|
+
for (let reading = readings.pop(); reading !== void 0; reading = readings.pop()) {
|
|
1131
|
+
const { lexer } = reading;
|
|
1132
|
+
const key = JSON.stringify([
|
|
1133
|
+
lexer.index,
|
|
1134
|
+
lexer.inVersionComment,
|
|
1135
|
+
reading.mode.backslashEscapes,
|
|
1136
|
+
reading.mode.ansiQuotes,
|
|
1137
|
+
reading.mode.brackets,
|
|
1138
|
+
reading.cut,
|
|
1139
|
+
forgotten
|
|
1140
|
+
]);
|
|
1141
|
+
if (seen.has(key)) continue;
|
|
1142
|
+
seen.add(key);
|
|
1143
|
+
let trailingComment = false;
|
|
1144
|
+
let finished = true;
|
|
1145
|
+
let read;
|
|
1146
|
+
do {
|
|
1147
|
+
read = readMySqlStatement(sql, lexer, reading.mode);
|
|
1148
|
+
if (read.rejected) {
|
|
1149
|
+
finished = false;
|
|
1150
|
+
break;
|
|
1151
|
+
}
|
|
1152
|
+
if (read.words.length === 0) {
|
|
1153
|
+
trailingComment ||= read.comment;
|
|
1154
|
+
continue;
|
|
1155
|
+
}
|
|
1156
|
+
const effect = mySqlStatementEffect(read.words, reading.mode, prepared);
|
|
1157
|
+
if (effect.rejected) {
|
|
1158
|
+
finished = false;
|
|
1159
|
+
break;
|
|
1160
|
+
}
|
|
1161
|
+
if (effect.kind === "unknown") throw new Error(MYSQL_UNKNOWN_EXECUTE);
|
|
1162
|
+
if (effect.kind === "call") throw MYSQL_UNCLEAR_CALL();
|
|
1163
|
+
forgotten += prepareDivergedMySql(effect, before.prepared, prepared, conflicts);
|
|
1164
|
+
reading.cut = mySqlCut(void 0, read);
|
|
1165
|
+
trailingComment = false;
|
|
1166
|
+
if (effect.change === "unclear") {
|
|
1167
|
+
for (const next of mySqlQuotingModes(reading.mode)) readings.push({
|
|
1168
|
+
lexer: { ...lexer },
|
|
1169
|
+
mode: next,
|
|
1170
|
+
cut: reading.cut
|
|
1171
|
+
});
|
|
1172
|
+
finished = false;
|
|
1173
|
+
break;
|
|
1174
|
+
}
|
|
1175
|
+
[reading.mode] = nextMySqlModes(reading.mode, effect.change);
|
|
1176
|
+
} while (read.terminated);
|
|
1177
|
+
if (finished) ends.add(trailingComment && reading.cut !== void 0 ? reading.cut : sql.length);
|
|
1178
|
+
}
|
|
1179
|
+
if (ends.size > 1) throw unclearMySqlStatements("cannot tell whether the comments after its last statement return a result of their own", "remove those comments");
|
|
1180
|
+
const [end = sql.length] = ends;
|
|
1181
|
+
return {
|
|
1182
|
+
calls: before.calls,
|
|
1183
|
+
uncounted: true,
|
|
1184
|
+
sql: sql.slice(0, end)
|
|
1185
|
+
};
|
|
1186
|
+
}
|
|
1187
|
+
/**
|
|
1188
|
+
* mysql2 returns `[rows, fields]`. With several result sets, `rows` holds one entry per result
|
|
1189
|
+
* (rows, or a result header for a write) and `fields` holds each one's column list, or
|
|
1190
|
+
* `undefined` for a header. A `CALL` returns each result set of the procedure and then its own
|
|
1191
|
+
* header, so those entries are grouped under the `CALL`, its rows in order. `statements` is what
|
|
1192
|
+
* `mySqlStatements` found in the SQL. SQL without a statement returns no entry, though the
|
|
1193
|
+
* server answers comments alone with a result header.
|
|
1194
|
+
*/
|
|
1195
|
+
function mySqlStatementResults(rows, fields, statements) {
|
|
1196
|
+
if (statements.calls.length === 0 && !statements.uncounted) return [];
|
|
1197
|
+
if (!(Array.isArray(rows) && Array.isArray(fields) && fields.some((entry) => entry === void 0 || Array.isArray(entry)))) return [mySqlStatementResult(rows)];
|
|
1198
|
+
const entries = rows;
|
|
1199
|
+
const grouped = [];
|
|
1200
|
+
let index = 0;
|
|
1201
|
+
let matched = true;
|
|
1202
|
+
for (const isCall of statements.calls) {
|
|
1203
|
+
if (!isCall) {
|
|
1204
|
+
matched &&= index < entries.length;
|
|
1205
|
+
grouped.push(mySqlStatementResult(entries[index++]));
|
|
1206
|
+
continue;
|
|
1207
|
+
}
|
|
1208
|
+
const sets = [];
|
|
1209
|
+
while (index < entries.length && Array.isArray(entries[index])) sets.push(entries[index++]);
|
|
1210
|
+
matched &&= index < entries.length;
|
|
1211
|
+
grouped.push({
|
|
1212
|
+
results: sets.flat(),
|
|
1213
|
+
success: true,
|
|
1214
|
+
meta: mySqlHeaderMeta(entries[index++])
|
|
1215
|
+
});
|
|
1216
|
+
}
|
|
1217
|
+
if (statements.uncounted) {
|
|
1218
|
+
matched &&= index < entries.length;
|
|
1219
|
+
grouped.push(...entries.slice(index).map(mySqlStatementResult));
|
|
1220
|
+
index = entries.length;
|
|
1221
|
+
}
|
|
1222
|
+
return matched && index === entries.length ? grouped : entries.map(mySqlStatementResult);
|
|
1223
|
+
}
|
|
23
1224
|
// src/cli/db-export-sequences.ts
|
|
24
1225
|
function quoteSqlIdentifier(value) {
|
|
25
1226
|
return `"${value.replaceAll("\"", "\"\"")}"`;
|
|
@@ -186,7 +1387,7 @@ async function withPgClient(root, fn) {
|
|
|
186
1387
|
}
|
|
187
1388
|
}
|
|
188
1389
|
async function withMySqlClient(root, fn, options = {}) {
|
|
189
|
-
const { toMySqlClient } = await import("./runner-mysql-
|
|
1390
|
+
const { toMySqlClient } = await import("./runner-mysql-DOjbj-N1.mjs");
|
|
190
1391
|
const { createConnection } = await import("mysql2/promise");
|
|
191
1392
|
const connection = await createConnection({
|
|
192
1393
|
uri: getLocalDatabaseUrl(root, "mysql"),
|
|
@@ -209,7 +1410,14 @@ function validatePendingMigrations(migrations, appliedNames) {
|
|
|
209
1410
|
if (pending.length === 0) return;
|
|
210
1411
|
validateMigrations(pending);
|
|
211
1412
|
}
|
|
212
|
-
async function runDbCommand(root, args) {
|
|
1413
|
+
async function runDbCommand(root, args, writeResults = (text) => process.stdout.write(text)) {
|
|
1414
|
+
if (args.action === "execute" && args.json) {
|
|
1415
|
+
const printResults = (results) => {
|
|
1416
|
+
writeResults(`${formatStatementResults(results)}\n`);
|
|
1417
|
+
};
|
|
1418
|
+
await runExecute(root, args, getDatabaseDialect(readConfig(root)), printResults);
|
|
1419
|
+
return;
|
|
1420
|
+
}
|
|
213
1421
|
if (!(args.action === "export" && !args.output)) console.log();
|
|
214
1422
|
const dialect = getDatabaseDialect(readConfig(root));
|
|
215
1423
|
if (args.action === "push") await runPush(root, dialect);
|
|
@@ -217,7 +1425,7 @@ async function runDbCommand(root, args) {
|
|
|
217
1425
|
else if (args.action === "status") await runStatus(root, dialect);
|
|
218
1426
|
else if (args.action === "reset") await runReset(root, dialect, args.yes);
|
|
219
1427
|
else if (args.action === "seed") await runSeed(root, args.file, dialect, args.yes);
|
|
220
|
-
else if (args.action === "execute") await runExecute(root, args
|
|
1428
|
+
else if (args.action === "execute") await runExecute(root, args, dialect, void 0);
|
|
221
1429
|
else if (args.action === "migrate") await runMigrate(root, dialect, args.remote);
|
|
222
1430
|
else if (args.action === "studio") await runStudio(root, dialect, args.remote);
|
|
223
1431
|
else if (args.action === "set-url") await runSetUrl(root, dialect);
|
|
@@ -327,7 +1535,7 @@ async function runPush(root, dialect) {
|
|
|
327
1535
|
printSuccess("Database schema pushed.");
|
|
328
1536
|
}
|
|
329
1537
|
async function runGenerate(root, dialect) {
|
|
330
|
-
const { prepareProject } = await import("./prepare-
|
|
1538
|
+
const { prepareProject } = await import("./prepare-DcwigRUS.mjs");
|
|
331
1539
|
const { PLACEHOLDER_DB_URL, writeDrizzleConfig } = await import("./config-DtjrUtPo.mjs");
|
|
332
1540
|
await prepareProject(root);
|
|
333
1541
|
const authSchemaPath = join(root, ".void", "better-auth-schema.ts");
|
|
@@ -377,7 +1585,7 @@ async function runStatus(root, dialect) {
|
|
|
377
1585
|
localError = error;
|
|
378
1586
|
}
|
|
379
1587
|
else if (dialect === "mysql") try {
|
|
380
|
-
const { readMySqlAppliedMigrations } = await import("./runner-mysql-
|
|
1588
|
+
const { readMySqlAppliedMigrations } = await import("./runner-mysql-DOjbj-N1.mjs");
|
|
381
1589
|
localApplied = await withMySqlClient(root, readMySqlAppliedMigrations);
|
|
382
1590
|
} catch (error) {
|
|
383
1591
|
localError = error;
|
|
@@ -393,12 +1601,12 @@ async function runStatus(root, dialect) {
|
|
|
393
1601
|
if (dialect !== "sqlite") {
|
|
394
1602
|
const databaseUrl = process.env.DATABASE_URL;
|
|
395
1603
|
if (!databaseUrl) throw new Error("DATABASE_URL is not available.");
|
|
396
|
-
remoteApplied = (dialect === "postgresql" ? await (await import("./runner-pg-DMJquR3d.mjs")).getPgAppliedMigrations(databaseUrl) : await (await import("./runner-mysql-
|
|
1604
|
+
remoteApplied = (dialect === "postgresql" ? await (await import("./runner-pg-DMJquR3d.mjs")).getPgAppliedMigrations(databaseUrl) : await (await import("./runner-mysql-DOjbj-N1.mjs")).getMySqlAppliedMigrations(databaseUrl)).map((name) => ({
|
|
397
1605
|
name,
|
|
398
1606
|
applied_at: ""
|
|
399
1607
|
}));
|
|
400
1608
|
} else {
|
|
401
|
-
const { readCloudflareD1Migrations, resolveCloudflareD1Context } = await import("./cloudflare-operations-
|
|
1609
|
+
const { readCloudflareD1Migrations, resolveCloudflareD1Context } = await import("./cloudflare-operations-ByjFGats.mjs");
|
|
402
1610
|
remoteApplied = (await readCloudflareD1Migrations(resolveCloudflareD1Context(root, configuredD1Binding(root)))).map((name) => ({
|
|
403
1611
|
name,
|
|
404
1612
|
applied_at: ""
|
|
@@ -566,7 +1774,7 @@ async function resetDatabase(root, dialect, yes) {
|
|
|
566
1774
|
return migrations.length;
|
|
567
1775
|
}
|
|
568
1776
|
if (dialect === "mysql") {
|
|
569
|
-
const { applyPendingMySqlMigrations, withMySqlMigrationLock } = await import("./runner-mysql-
|
|
1777
|
+
const { applyPendingMySqlMigrations, withMySqlMigrationLock } = await import("./runner-mysql-DOjbj-N1.mjs");
|
|
570
1778
|
await withMySqlClient(root, (client) => withMySqlMigrationLock(client, async () => {
|
|
571
1779
|
const tables = await client.query("SELECT table_name AS table_name, table_type AS table_type FROM information_schema.tables WHERE table_schema = DATABASE() AND table_type IN ('BASE TABLE', 'VIEW')");
|
|
572
1780
|
await client.query("SET FOREIGN_KEY_CHECKS = 0");
|
|
@@ -686,56 +1894,123 @@ function prepareSingleStatement(db, sql) {
|
|
|
686
1894
|
* Exits with a friendly message when the URL has never been stored (404);
|
|
687
1895
|
* other errors propagate so the caller's top-level handler can report them.
|
|
688
1896
|
*/
|
|
689
|
-
async function fetchRemoteDatabaseUrl(client, projectId) {
|
|
1897
|
+
async function fetchRemoteDatabaseUrl(client, projectId, messages = {}) {
|
|
690
1898
|
try {
|
|
691
1899
|
return (await client.getDatabaseUrl(projectId)).url;
|
|
692
1900
|
} catch (err) {
|
|
693
1901
|
if (err instanceof PlatformApiError && err.status === 404) {
|
|
694
|
-
log.error("This project's database URL isn't stored on the platform yet. Run `void db set-url` once to populate it, then retry.");
|
|
1902
|
+
log.error("This project's database URL isn't stored on the platform yet. Run `void db set-url` once to populate it, then retry.", messages);
|
|
695
1903
|
process.exit(1);
|
|
696
1904
|
}
|
|
697
1905
|
throw err;
|
|
698
1906
|
}
|
|
699
1907
|
}
|
|
700
|
-
|
|
1908
|
+
/**
|
|
1909
|
+
* Run SQL on a PostgreSQL or MySQL database. Both drivers return one result per statement, so
|
|
1910
|
+
* `--json` prints every statement with `printResults`; the table prints only a single
|
|
1911
|
+
* statement's rows.
|
|
1912
|
+
*/
|
|
1913
|
+
async function executeExternalSql(dialect, dbUrl, sql, printResults) {
|
|
1914
|
+
const json = printResults !== void 0;
|
|
701
1915
|
if (dialect === "postgresql") {
|
|
702
|
-
const { Client } = await import("pg");
|
|
703
|
-
const pgClient = new Client({
|
|
1916
|
+
const { Client, types } = await import("pg");
|
|
1917
|
+
const pgClient = new Client({
|
|
1918
|
+
connectionString: dbUrl,
|
|
1919
|
+
...json ? { types: pgJsonTypes(types) } : {}
|
|
1920
|
+
});
|
|
704
1921
|
await pgClient.connect();
|
|
705
1922
|
try {
|
|
706
1923
|
const result = await pgClient.query(sql);
|
|
707
|
-
if (
|
|
1924
|
+
if (json) printResults(pgStatementResults(result));
|
|
1925
|
+
else if (!Array.isArray(result) && result.rows.length > 0) printTable(result.rows);
|
|
708
1926
|
} finally {
|
|
709
1927
|
await pgClient.end();
|
|
710
1928
|
}
|
|
711
1929
|
return;
|
|
712
1930
|
}
|
|
713
1931
|
const { createConnection } = await import("mysql2/promise");
|
|
1932
|
+
const uri = json ? mySqlJsonUri(dbUrl) : dbUrl;
|
|
1933
|
+
let statements;
|
|
1934
|
+
if (json) {
|
|
1935
|
+
let mode;
|
|
1936
|
+
if (needsMySqlServerMode(sql)) {
|
|
1937
|
+
const modeClient = await createConnection({ uri });
|
|
1938
|
+
try {
|
|
1939
|
+
mode = mySqlServerMode((await modeClient.query(MYSQL_SERVER_MODE_QUERY))[0]);
|
|
1940
|
+
} finally {
|
|
1941
|
+
await modeClient.end();
|
|
1942
|
+
}
|
|
1943
|
+
}
|
|
1944
|
+
statements = mySqlStatements(sql, mode);
|
|
1945
|
+
}
|
|
714
1946
|
const mySqlClient = await createConnection({
|
|
715
|
-
uri
|
|
716
|
-
multipleStatements: true
|
|
1947
|
+
uri,
|
|
1948
|
+
multipleStatements: true,
|
|
1949
|
+
...json ? {
|
|
1950
|
+
supportBigNumbers: true,
|
|
1951
|
+
bigNumberStrings: false,
|
|
1952
|
+
dateStrings: true,
|
|
1953
|
+
typeCast: mySqlJsonTypeCast
|
|
1954
|
+
} : {}
|
|
717
1955
|
});
|
|
718
1956
|
try {
|
|
719
|
-
const [rows] = await mySqlClient.query(sql);
|
|
720
|
-
if (
|
|
1957
|
+
const [rows, fields] = await mySqlClient.query(statements?.sql ?? sql);
|
|
1958
|
+
if (json && statements) printResults(mySqlStatementResults(rows, fields, statements));
|
|
1959
|
+
else if (Array.isArray(rows) && rows.length > 0) printTable(rows);
|
|
721
1960
|
} finally {
|
|
722
1961
|
await mySqlClient.end();
|
|
723
1962
|
}
|
|
724
1963
|
}
|
|
1964
|
+
const MYSQL_JSON_URL_OPTIONS = [
|
|
1965
|
+
"debug",
|
|
1966
|
+
"bigNumberStrings",
|
|
1967
|
+
"dateStrings",
|
|
1968
|
+
"decimalNumbers",
|
|
1969
|
+
"jsonStrings",
|
|
1970
|
+
"nestTables",
|
|
1971
|
+
"rowsAsArray",
|
|
1972
|
+
"supportBigNumbers",
|
|
1973
|
+
"typeCast"
|
|
1974
|
+
];
|
|
1975
|
+
/**
|
|
1976
|
+
* The URL without options that would change --json output. mysql2 reads options from the URL,
|
|
1977
|
+
* and an option passed as false does not override one the URL sets.
|
|
1978
|
+
*/
|
|
1979
|
+
function mySqlJsonUri(dbUrl) {
|
|
1980
|
+
let url;
|
|
1981
|
+
try {
|
|
1982
|
+
url = new URL(dbUrl);
|
|
1983
|
+
} catch {
|
|
1984
|
+
return dbUrl;
|
|
1985
|
+
}
|
|
1986
|
+
const options = MYSQL_JSON_URL_OPTIONS.filter((option) => url.searchParams.has(option));
|
|
1987
|
+
if (options.length === 0) return dbUrl;
|
|
1988
|
+
for (const option of options) url.searchParams.delete(option);
|
|
1989
|
+
return url.toString();
|
|
1990
|
+
}
|
|
725
1991
|
/**
|
|
726
1992
|
* Prompt the user to confirm a destructive remote query when running in a TTY.
|
|
727
1993
|
* `always` asks even for read-only SQL, and `yes` (`--yes`) never asks. Returns true if
|
|
728
1994
|
* execution should proceed, false if it was cancelled.
|
|
729
1995
|
*/
|
|
730
|
-
async function confirmDestructiveRemote(sql, { always = false, yes = false } = {}) {
|
|
1996
|
+
async function confirmDestructiveRemote(sql, { always = false, yes = false, output } = {}) {
|
|
731
1997
|
if (yes || !always && isSafeStatement(sql)) return true;
|
|
732
1998
|
if (!process.stdin.isTTY) return true;
|
|
733
|
-
const ok = await confirm({
|
|
1999
|
+
const ok = await confirm({
|
|
2000
|
+
message: "This will run against your deployed database. Continue?",
|
|
2001
|
+
output
|
|
2002
|
+
});
|
|
734
2003
|
if (isCancel(ok) || !ok) return false;
|
|
735
2004
|
return true;
|
|
736
2005
|
}
|
|
737
2006
|
const D1_MAX_SQL_STATEMENT_BYTES = 1e5;
|
|
738
|
-
|
|
2007
|
+
/** Run `void db execute`. `printResults` prints the results with --json, and is undefined without. */
|
|
2008
|
+
async function runExecute(root, { command, file, remote, yes }, dialect, printResults) {
|
|
2009
|
+
const json = printResults !== void 0;
|
|
2010
|
+
const messages = json ? { output: process.stderr } : {};
|
|
2011
|
+
const finish = () => {
|
|
2012
|
+
if (!json) printSuccess("SQL executed successfully.");
|
|
2013
|
+
};
|
|
739
2014
|
const readSql = () => {
|
|
740
2015
|
if (file) return readFileSync(resolve(root, file), "utf8");
|
|
741
2016
|
if (command === void 0) throw new Error("db execute requires a SQL command or file.");
|
|
@@ -746,18 +2021,21 @@ async function runExecute(root, command, file, dialect, remote, yes) {
|
|
|
746
2021
|
const sql = readSql();
|
|
747
2022
|
if (directCloudflare) {
|
|
748
2023
|
const bulkImport = dialect === "sqlite" && file !== void 0 && Buffer.byteLength(sql) > D1_MAX_SQL_STATEMENT_BYTES;
|
|
2024
|
+
if (bulkImport && json) throw new Error(`db: ${file} is larger than 100 KB, so Void would run it as a D1 import, which returns no results. Run it without --json.`);
|
|
749
2025
|
if (bulkImport) log.warn(`${file} is larger than 100 KB, so Void runs it as a D1 import. The database cannot serve queries until the import finishes, and query rows are not printed.`);
|
|
750
2026
|
if (!await confirmDestructiveRemote(sql, {
|
|
751
2027
|
always: bulkImport,
|
|
752
|
-
yes
|
|
2028
|
+
yes,
|
|
2029
|
+
...messages
|
|
753
2030
|
})) {
|
|
754
|
-
log.info("Cancelled.");
|
|
2031
|
+
log.info("Cancelled.", messages);
|
|
755
2032
|
process.exit(0);
|
|
756
2033
|
}
|
|
757
2034
|
if (dialect === "sqlite") {
|
|
758
|
-
const { executeCloudflareD1Sql, importCloudflareD1Sql, resolveCloudflareD1Context } = await import("./cloudflare-operations-
|
|
2035
|
+
const { executeCloudflareD1Sql, executeCloudflareD1Statements, importCloudflareD1Sql, resolveCloudflareD1Context } = await import("./cloudflare-operations-ByjFGats.mjs");
|
|
759
2036
|
const context = resolveCloudflareD1Context(root, configuredD1Binding(root));
|
|
760
2037
|
if (bulkImport) await importCloudflareD1Sql(context, sql);
|
|
2038
|
+
else if (json) printResults(await executeCloudflareD1Statements(context, sql));
|
|
761
2039
|
else {
|
|
762
2040
|
const rows = await executeCloudflareD1Sql(context, sql);
|
|
763
2041
|
if (rows.length > 0) printTable(rows);
|
|
@@ -765,31 +2043,35 @@ async function runExecute(root, command, file, dialect, remote, yes) {
|
|
|
765
2043
|
} else {
|
|
766
2044
|
const dbUrl = process.env.DATABASE_URL;
|
|
767
2045
|
if (!dbUrl) {
|
|
768
|
-
log.error("DATABASE_URL is required in the shell for a direct Cloudflare remote database command.");
|
|
2046
|
+
log.error("DATABASE_URL is required in the shell for a direct Cloudflare remote database command.", messages);
|
|
769
2047
|
process.exit(1);
|
|
770
2048
|
}
|
|
771
|
-
await
|
|
2049
|
+
await executeExternalSql(dialect, dbUrl, sql, printResults);
|
|
772
2050
|
}
|
|
773
|
-
|
|
2051
|
+
finish();
|
|
774
2052
|
return;
|
|
775
2053
|
}
|
|
776
2054
|
const token = getToken(root);
|
|
777
2055
|
if (!token) {
|
|
778
|
-
log.error("Not logged in. Run `void account login` first.");
|
|
2056
|
+
log.error("Not logged in. Run `void account login` first.", messages);
|
|
779
2057
|
process.exit(1);
|
|
780
2058
|
}
|
|
781
2059
|
const projectConfig = readProjectConfig(root);
|
|
782
2060
|
if (!projectConfig) {
|
|
783
|
-
log.error("No project linked. Run `void deploy` first or link a project.");
|
|
2061
|
+
log.error("No project linked. Run `void deploy` first or link a project.", messages);
|
|
784
2062
|
process.exit(1);
|
|
785
2063
|
}
|
|
786
|
-
if (
|
|
787
|
-
|
|
2064
|
+
if (json && dialect === "sqlite" && splitSqliteStatements(sql).length > 1) throw new Error("db: `void db execute --remote --json` runs one statement at a time on a Void platform D1 database. Run each statement separately.");
|
|
2065
|
+
if (!await confirmDestructiveRemote(sql, {
|
|
2066
|
+
yes,
|
|
2067
|
+
...messages
|
|
2068
|
+
})) {
|
|
2069
|
+
log.info("Cancelled.", messages);
|
|
788
2070
|
process.exit(0);
|
|
789
2071
|
}
|
|
790
2072
|
if (dialect !== "sqlite") {
|
|
791
|
-
await
|
|
792
|
-
|
|
2073
|
+
await executeExternalSql(dialect, await fetchRemoteDatabaseUrl(new PlatformClient(token, { root }), projectConfig.projectId, messages), sql, printResults);
|
|
2074
|
+
finish();
|
|
793
2075
|
return;
|
|
794
2076
|
}
|
|
795
2077
|
const platformClient = new PlatformClient(token, { root });
|
|
@@ -834,50 +2116,47 @@ async function runExecute(root, command, file, dialect, remote, yes) {
|
|
|
834
2116
|
}
|
|
835
2117
|
if (!res.ok) {
|
|
836
2118
|
const text = await res.text().catch(() => res.statusText);
|
|
837
|
-
log.error(`db: Proxy request failed with ${res.status}: '${text}'
|
|
2119
|
+
log.error(`db: Proxy request failed with ${res.status}: '${text}'.`, messages);
|
|
838
2120
|
process.exit(1);
|
|
839
2121
|
}
|
|
840
|
-
const
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
if (result.rows && result.rows.length > 0) printTable(result.rows);
|
|
850
|
-
});
|
|
851
|
-
printSuccess("SQL executed successfully.");
|
|
2122
|
+
const data = await res.json();
|
|
2123
|
+
const rows = data.results ?? [];
|
|
2124
|
+
if (json) printResults([{
|
|
2125
|
+
results: rows,
|
|
2126
|
+
success: data.success !== false,
|
|
2127
|
+
meta: data.meta ?? {}
|
|
2128
|
+
}]);
|
|
2129
|
+
else if (rows.length > 0) printTable(rows);
|
|
2130
|
+
finish();
|
|
852
2131
|
return;
|
|
853
2132
|
}
|
|
854
|
-
if (dialect
|
|
2133
|
+
if (dialect !== "sqlite") {
|
|
855
2134
|
const sql = readSql();
|
|
856
|
-
await
|
|
857
|
-
|
|
858
|
-
if (rows.length > 0) printTable(rows);
|
|
859
|
-
});
|
|
860
|
-
printSuccess("SQL executed successfully.");
|
|
2135
|
+
await executeExternalSql(dialect, getLocalDatabaseUrl(root, dialect), sql, printResults);
|
|
2136
|
+
finish();
|
|
861
2137
|
return;
|
|
862
2138
|
}
|
|
863
2139
|
const { openSqliteDatabase } = await import("./runner-Bf94YVJK.mjs");
|
|
864
2140
|
const { databasePath: dbPath } = getLocalD1Target(root);
|
|
865
2141
|
if (!existsSync(dbPath)) {
|
|
866
|
-
log.error("No local database found. Start the dev server first.");
|
|
2142
|
+
log.error("No local database found. Start the dev server first.", messages);
|
|
867
2143
|
process.exit(1);
|
|
868
2144
|
}
|
|
869
2145
|
const sql = readSql();
|
|
870
2146
|
const db = await openSqliteDatabase(dbPath);
|
|
871
2147
|
try {
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
const
|
|
875
|
-
if (
|
|
876
|
-
|
|
2148
|
+
if (json) printResults(runSqliteStatements(db, sql));
|
|
2149
|
+
else {
|
|
2150
|
+
const statement = prepareSingleStatement(db, sql);
|
|
2151
|
+
if (statement?.reader) {
|
|
2152
|
+
const rows = statement.all();
|
|
2153
|
+
if (rows.length > 0) printTable(rows);
|
|
2154
|
+
} else db.exec(sql);
|
|
2155
|
+
}
|
|
877
2156
|
} finally {
|
|
878
2157
|
db.close();
|
|
879
2158
|
}
|
|
880
|
-
|
|
2159
|
+
finish();
|
|
881
2160
|
}
|
|
882
2161
|
function printTable(rows) {
|
|
883
2162
|
const cols = Object.keys(rows[0]);
|
|
@@ -927,7 +2206,7 @@ async function runMigrate(root, dialect, remote) {
|
|
|
927
2206
|
appliedNames = await getPgAppliedMigrations(databaseUrl);
|
|
928
2207
|
applyMigrations = () => runPgMigrations(migrationsDir, databaseUrl);
|
|
929
2208
|
} else {
|
|
930
|
-
const { getMySqlAppliedMigrations, runMySqlMigrations } = await import("./runner-mysql-
|
|
2209
|
+
const { getMySqlAppliedMigrations, runMySqlMigrations } = await import("./runner-mysql-DOjbj-N1.mjs");
|
|
931
2210
|
appliedNames = await getMySqlAppliedMigrations(databaseUrl);
|
|
932
2211
|
applyMigrations = () => runMySqlMigrations(migrationsDir, databaseUrl);
|
|
933
2212
|
}
|
|
@@ -952,7 +2231,7 @@ async function runMigrate(root, dialect, remote) {
|
|
|
952
2231
|
log.info(`No migration files found in ${paths.sourceRelative("db/migrations")}/`);
|
|
953
2232
|
return;
|
|
954
2233
|
}
|
|
955
|
-
const { applyCloudflareD1Migrations, readCloudflareD1Migrations, resolveCloudflareD1Context } = await import("./cloudflare-operations-
|
|
2234
|
+
const { applyCloudflareD1Migrations, readCloudflareD1Migrations, resolveCloudflareD1Context } = await import("./cloudflare-operations-ByjFGats.mjs");
|
|
956
2235
|
const binding = configuredD1Binding(root);
|
|
957
2236
|
const context = resolveCloudflareD1Context(root, binding);
|
|
958
2237
|
const remoteBefore = new Set(await readCloudflareD1Migrations(context));
|
|
@@ -972,7 +2251,7 @@ async function runMigrate(root, dialect, remote) {
|
|
|
972
2251
|
log.error(`db: D1 binding ${binding} has no "migrations_dir", so Cloudflare would apply ./migrations instead of ${dir}/. Add "migrations_dir": "./${dir}" to that D1 binding in your Cloudflare config, then run the command again.`);
|
|
973
2252
|
process.exit(1);
|
|
974
2253
|
}
|
|
975
|
-
const { checkWranglerMigrationApplySet, parseRootWranglerConfig } = await import("./deploy-
|
|
2254
|
+
const { checkWranglerMigrationApplySet, parseRootWranglerConfig } = await import("./deploy-Do6FWqon.mjs");
|
|
976
2255
|
const applySet = await checkWranglerMigrationApplySet(context.operation.configPath, parseRootWranglerConfig(context.operation.configPath), binding, context.migrationsDir, rawMigrations, paths.sourceRelative("db/migrations"));
|
|
977
2256
|
if (!applySet.ok) {
|
|
978
2257
|
log.error(applySet.reason);
|
|
@@ -1053,7 +2332,7 @@ async function runMigrate(root, dialect, remote) {
|
|
|
1053
2332
|
return;
|
|
1054
2333
|
}
|
|
1055
2334
|
if (dialect === "mysql") {
|
|
1056
|
-
const { applyMySqlMigrations, readMySqlAppliedMigrations } = await import("./runner-mysql-
|
|
2335
|
+
const { applyMySqlMigrations, readMySqlAppliedMigrations } = await import("./runner-mysql-DOjbj-N1.mjs");
|
|
1057
2336
|
const applied = await withMySqlClient(root, async (client) => {
|
|
1058
2337
|
const appliedNames = await readMySqlAppliedMigrations(client).catch(() => []);
|
|
1059
2338
|
try {
|
|
@@ -1099,7 +2378,7 @@ async function runRestore(root, dialect, { target, platform: explicitPlatform, y
|
|
|
1099
2378
|
log.error("db: `void db restore` restores the D1 database of a direct Cloudflare deploy. After a one-off `void deploy --platform cloudflare`, run it with `--platform cloudflare`.");
|
|
1100
2379
|
process.exit(1);
|
|
1101
2380
|
}
|
|
1102
|
-
const { readCloudflareD1Migrations, resolveCloudflareD1Context, restoreCloudflareD1Database } = await import("./cloudflare-operations-
|
|
2381
|
+
const { readCloudflareD1Migrations, resolveCloudflareD1Context, restoreCloudflareD1Database } = await import("./cloudflare-operations-ByjFGats.mjs");
|
|
1103
2382
|
const binding = configuredD1Binding(root);
|
|
1104
2383
|
const context = resolveCloudflareD1Context(root, binding);
|
|
1105
2384
|
const database = `D1 database ${context.migrateToken} (${binding})`;
|