@adep/runtime 0.2.3 → 0.2.4
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/index.js +321 -46
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2018,6 +2018,7 @@ function stripQuotes(raw) {
|
|
|
2018
2018
|
const t = raw.trim();
|
|
2019
2019
|
if (/^'.*'$/.test(t)) return t.slice(1, -1);
|
|
2020
2020
|
if (/^".*"$/.test(t)) return t.slice(1, -1);
|
|
2021
|
+
if (/^NULL$/i.test(t)) return null;
|
|
2021
2022
|
return t;
|
|
2022
2023
|
}
|
|
2023
2024
|
function parseCreateTable(ddl) {
|
|
@@ -2038,50 +2039,109 @@ function parseCreateTable(ddl) {
|
|
|
2038
2039
|
}
|
|
2039
2040
|
return { name, columns };
|
|
2040
2041
|
}
|
|
2041
|
-
function
|
|
2042
|
-
const
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
const inner = rhs.replace(/^\(|\)$/g, "");
|
|
2065
|
-
const items = splitListItems(inner);
|
|
2066
|
-
const list = items.map((item) => {
|
|
2067
|
-
if (item.trim() === "?") return cursor.take();
|
|
2068
|
-
return stripQuotes(item);
|
|
2069
|
-
});
|
|
2070
|
-
clauses.push({ column: col, operator: op, value: list, list: true });
|
|
2042
|
+
function splitTopLevel(raw, token) {
|
|
2043
|
+
const parts = [];
|
|
2044
|
+
let depth = 0;
|
|
2045
|
+
let inQuote = false;
|
|
2046
|
+
let buffer = "";
|
|
2047
|
+
const n = raw.length;
|
|
2048
|
+
const isTokenAt = (i) => {
|
|
2049
|
+
const word = raw.slice(i, i + token.length);
|
|
2050
|
+
if (word.toUpperCase() !== token) return false;
|
|
2051
|
+
const before = i === 0 ? "" : raw[i - 1] ?? "";
|
|
2052
|
+
const after = raw[i + token.length] ?? "";
|
|
2053
|
+
return !/[A-Za-z0-9_.]/.test(before) && !/[A-Za-z0-9_.]/.test(after);
|
|
2054
|
+
};
|
|
2055
|
+
for (let i = 0; i < n; i += 1) {
|
|
2056
|
+
const ch = raw[i] ?? "";
|
|
2057
|
+
if (ch === "'") {
|
|
2058
|
+
if (inQuote && raw[i + 1] === "'") {
|
|
2059
|
+
buffer += ch + (raw[i + 1] ?? "");
|
|
2060
|
+
i += 1;
|
|
2061
|
+
continue;
|
|
2062
|
+
}
|
|
2063
|
+
inQuote = !inQuote;
|
|
2064
|
+
buffer += ch;
|
|
2071
2065
|
continue;
|
|
2072
2066
|
}
|
|
2073
|
-
|
|
2074
|
-
if (
|
|
2075
|
-
|
|
2067
|
+
if (!inQuote && ch === "(") depth += 1;
|
|
2068
|
+
else if (!inQuote && ch === ")") depth -= 1;
|
|
2069
|
+
if (!inQuote && depth === 0 && isTokenAt(i)) {
|
|
2070
|
+
parts.push(buffer);
|
|
2071
|
+
buffer = "";
|
|
2072
|
+
i += token.length - 1;
|
|
2076
2073
|
} else {
|
|
2077
|
-
|
|
2074
|
+
buffer += ch;
|
|
2078
2075
|
}
|
|
2079
|
-
clauses.push({ column: col, operator: op, value, list: false });
|
|
2080
2076
|
}
|
|
2081
|
-
|
|
2077
|
+
parts.push(buffer);
|
|
2078
|
+
return parts;
|
|
2082
2079
|
}
|
|
2083
|
-
function
|
|
2084
|
-
|
|
2080
|
+
function unqualifyColumn(raw) {
|
|
2081
|
+
const t = raw.trim();
|
|
2082
|
+
const dot = t.lastIndexOf(".");
|
|
2083
|
+
if (dot > 0 && /^[A-Za-z_][A-Za-z0-9_]*\./.test(t)) return t.slice(dot + 1);
|
|
2084
|
+
return t;
|
|
2085
|
+
}
|
|
2086
|
+
function parseLeftJoins(tail) {
|
|
2087
|
+
const joins = [];
|
|
2088
|
+
const re = /LEFT\s+JOIN\s+((?:"[^"]+")|([A-Za-z_][A-Za-z0-9_]*))\s+([A-Za-z_][A-Za-z0-9_]*)\s+ON\s+([A-Za-z_][A-Za-z0-9_.]*)\s*=\s*([A-Za-z_][A-Za-z0-9_.]*)/gi;
|
|
2089
|
+
let m;
|
|
2090
|
+
while ((m = re.exec(tail)) !== null) {
|
|
2091
|
+
joins.push({
|
|
2092
|
+
table: ident(m[1] ?? ""),
|
|
2093
|
+
alias: m[3] ?? "",
|
|
2094
|
+
leftCol: unqualifyColumn(m[4] ?? ""),
|
|
2095
|
+
rightCol: unqualifyColumn(m[5] ?? "")
|
|
2096
|
+
});
|
|
2097
|
+
}
|
|
2098
|
+
return joins;
|
|
2099
|
+
}
|
|
2100
|
+
function parseWhereClauses(whereRaw, cursor) {
|
|
2101
|
+
const parseAndGroup = (andRaw) => {
|
|
2102
|
+
const clauses = [];
|
|
2103
|
+
for (const part of splitTopLevel(andRaw, "AND")) {
|
|
2104
|
+
const t = part.trim();
|
|
2105
|
+
if (t.length === 0) continue;
|
|
2106
|
+
if (/\bIS\s+NULL\b/i.test(t)) {
|
|
2107
|
+
clauses.push({ column: unqualifyColumn(ident(t.split(/\s+IS\s+NULL\b/i)[0] ?? "")), operator: "IS NULL", value: null, list: false });
|
|
2108
|
+
continue;
|
|
2109
|
+
}
|
|
2110
|
+
if (/\bIS\s+NOT\s+NULL\b/i.test(t)) {
|
|
2111
|
+
clauses.push({ column: unqualifyColumn(ident(t.split(/\s+IS\s+NOT\s+NULL\b/i)[0] ?? "")), operator: "IS NOT NULL", value: null, list: false });
|
|
2112
|
+
continue;
|
|
2113
|
+
}
|
|
2114
|
+
const opMatch = /^\s*((?:"[^"]+")|([A-Za-z_][A-Za-z0-9_.]*))\s*(=|!=|>=|<=|>|<|like|in|not\s+in)\s*(.+)$/i.exec(
|
|
2115
|
+
t
|
|
2116
|
+
);
|
|
2117
|
+
if (opMatch === null) continue;
|
|
2118
|
+
const col = unqualifyColumn(ident(opMatch[1] ?? ""));
|
|
2119
|
+
const op = (opMatch[3] ?? opMatch[2] ?? "").toLowerCase();
|
|
2120
|
+
const rhs = (opMatch[4] ?? "").trim();
|
|
2121
|
+
if (op === "in" || op === "not in") {
|
|
2122
|
+
const inner = rhs.replace(/^\(|\)$/g, "");
|
|
2123
|
+
const items = splitListItems(inner);
|
|
2124
|
+
const list = items.map((item) => {
|
|
2125
|
+
if (item.trim() === "?") return cursor.take();
|
|
2126
|
+
return stripQuotes(item);
|
|
2127
|
+
});
|
|
2128
|
+
clauses.push({ column: col, operator: op, value: list, list: true });
|
|
2129
|
+
continue;
|
|
2130
|
+
}
|
|
2131
|
+
let value;
|
|
2132
|
+
if (rhs === "?") {
|
|
2133
|
+
value = cursor.take();
|
|
2134
|
+
} else {
|
|
2135
|
+
value = stripQuotes(rhs);
|
|
2136
|
+
}
|
|
2137
|
+
clauses.push({ column: col, operator: op, value, list: false });
|
|
2138
|
+
}
|
|
2139
|
+
return clauses;
|
|
2140
|
+
};
|
|
2141
|
+
return splitTopLevel(whereRaw, "OR").map((group) => group.trim()).filter((group) => group.length > 0).map(parseAndGroup);
|
|
2142
|
+
}
|
|
2143
|
+
function matchWhere(groups, row) {
|
|
2144
|
+
return groups.some((clauses) => clauses.every((c) => matchValue(row[c.column], c)));
|
|
2085
2145
|
}
|
|
2086
2146
|
function splitListItems(inner) {
|
|
2087
2147
|
const items = [];
|
|
@@ -2101,6 +2161,168 @@ function splitListItems(inner) {
|
|
|
2101
2161
|
items.push(buffer);
|
|
2102
2162
|
return items;
|
|
2103
2163
|
}
|
|
2164
|
+
var ExcludedRef = class {
|
|
2165
|
+
constructor(column) {
|
|
2166
|
+
this.column = column;
|
|
2167
|
+
}
|
|
2168
|
+
};
|
|
2169
|
+
var ColumnRef = class {
|
|
2170
|
+
constructor(column) {
|
|
2171
|
+
this.column = column;
|
|
2172
|
+
}
|
|
2173
|
+
};
|
|
2174
|
+
var ExprRef = class {
|
|
2175
|
+
constructor(source) {
|
|
2176
|
+
this.source = source;
|
|
2177
|
+
}
|
|
2178
|
+
};
|
|
2179
|
+
function resolveSetValue(value, existing, row) {
|
|
2180
|
+
if (value instanceof ExcludedRef) return row[value.column];
|
|
2181
|
+
if (value instanceof ColumnRef) return existing[value.column];
|
|
2182
|
+
if (value instanceof ExprRef) return evalArithmetic(value.source, existing, row);
|
|
2183
|
+
return value;
|
|
2184
|
+
}
|
|
2185
|
+
function evalArithmetic(src, existing, row) {
|
|
2186
|
+
const num = (v) => {
|
|
2187
|
+
const n2 = Number(v);
|
|
2188
|
+
return Number.isFinite(n2) ? n2 : 0;
|
|
2189
|
+
};
|
|
2190
|
+
let i = 0;
|
|
2191
|
+
const n = src.length;
|
|
2192
|
+
const skip = () => {
|
|
2193
|
+
while (i < n && /\s/.test(src[i] ?? "")) i += 1;
|
|
2194
|
+
};
|
|
2195
|
+
const peek = () => {
|
|
2196
|
+
skip();
|
|
2197
|
+
return i < n ? src[i] ?? "" : null;
|
|
2198
|
+
};
|
|
2199
|
+
const parseIdent = () => {
|
|
2200
|
+
skip();
|
|
2201
|
+
const m = /^([A-Za-z_][A-Za-z0-9_]*)/.exec(src.slice(i));
|
|
2202
|
+
if (m === null) return null;
|
|
2203
|
+
i += (m[1] ?? "").length;
|
|
2204
|
+
skip();
|
|
2205
|
+
let col = ident(m[1] ?? "");
|
|
2206
|
+
let excluded = false;
|
|
2207
|
+
if (src[i] === ".") {
|
|
2208
|
+
const m2 = /^\.([A-Za-z_][A-Za-z0-9_]*)/.exec(src.slice(i));
|
|
2209
|
+
if (m2 !== null) {
|
|
2210
|
+
i += (m2[0] ?? "").length;
|
|
2211
|
+
if (col.toLowerCase() === "excluded") excluded = true;
|
|
2212
|
+
col = ident(m2[1] ?? "");
|
|
2213
|
+
}
|
|
2214
|
+
}
|
|
2215
|
+
return { col, excluded };
|
|
2216
|
+
};
|
|
2217
|
+
const parseFactor = () => {
|
|
2218
|
+
const ch = peek();
|
|
2219
|
+
if (ch === "(") {
|
|
2220
|
+
i += 1;
|
|
2221
|
+
const v = parseExpr();
|
|
2222
|
+
skip();
|
|
2223
|
+
if (src[i] === ")") i += 1;
|
|
2224
|
+
return v;
|
|
2225
|
+
}
|
|
2226
|
+
const numMatch = /^(\d+(?:\.\d+)?)/.exec(src.slice(i));
|
|
2227
|
+
if (numMatch !== null) {
|
|
2228
|
+
i += (numMatch[1] ?? "").length;
|
|
2229
|
+
return Number(numMatch[1]);
|
|
2230
|
+
}
|
|
2231
|
+
const id = parseIdent();
|
|
2232
|
+
if (id !== null) {
|
|
2233
|
+
const v = id.excluded ? row[id.col] : existing[id.col];
|
|
2234
|
+
return num(v);
|
|
2235
|
+
}
|
|
2236
|
+
return 0;
|
|
2237
|
+
};
|
|
2238
|
+
const parseTerm = () => {
|
|
2239
|
+
let v = parseFactor();
|
|
2240
|
+
for (; ; ) {
|
|
2241
|
+
const ch = peek();
|
|
2242
|
+
if (ch !== "*" && ch !== "/") return v;
|
|
2243
|
+
i += 1;
|
|
2244
|
+
const rhs = parseFactor();
|
|
2245
|
+
v = ch === "*" ? v * rhs : rhs === 0 ? v : v / rhs;
|
|
2246
|
+
}
|
|
2247
|
+
};
|
|
2248
|
+
const parseExpr = () => {
|
|
2249
|
+
let v = parseTerm();
|
|
2250
|
+
for (; ; ) {
|
|
2251
|
+
const ch = peek();
|
|
2252
|
+
if (ch !== "+" && ch !== "-") return v;
|
|
2253
|
+
i += 1;
|
|
2254
|
+
const rhs = parseTerm();
|
|
2255
|
+
v = ch === "+" ? v + rhs : v - rhs;
|
|
2256
|
+
}
|
|
2257
|
+
};
|
|
2258
|
+
return parseExpr();
|
|
2259
|
+
}
|
|
2260
|
+
function splitInsertTuples(body) {
|
|
2261
|
+
const tuples = [];
|
|
2262
|
+
let i = 0;
|
|
2263
|
+
const n = body.length;
|
|
2264
|
+
while (i < n) {
|
|
2265
|
+
while (i < n && (body[i] === " " || body[i] === " " || body[i] === "\n" || body[i] === "\r" || body[i] === ",")) {
|
|
2266
|
+
i += 1;
|
|
2267
|
+
}
|
|
2268
|
+
if (i >= n) break;
|
|
2269
|
+
if (body[i] !== "(") {
|
|
2270
|
+
return { tuples, conflict: body.slice(i).trim() };
|
|
2271
|
+
}
|
|
2272
|
+
let depth = 0;
|
|
2273
|
+
let j = i;
|
|
2274
|
+
for (; j < n; j += 1) {
|
|
2275
|
+
if (body[j] === "(") depth += 1;
|
|
2276
|
+
else if (body[j] === ")") {
|
|
2277
|
+
depth -= 1;
|
|
2278
|
+
if (depth === 0) break;
|
|
2279
|
+
}
|
|
2280
|
+
}
|
|
2281
|
+
if (j >= n) return { tuples, conflict: null };
|
|
2282
|
+
tuples.push(body.slice(i + 1, j));
|
|
2283
|
+
i = j + 1;
|
|
2284
|
+
}
|
|
2285
|
+
return { tuples, conflict: null };
|
|
2286
|
+
}
|
|
2287
|
+
function parseConflictClause(clause, cursor, fallbackCols) {
|
|
2288
|
+
const m = /^ON\s+CONFLICT(?:\s*\(([^)]*)\))?\s+DO\s+(UPDATE\s+SET\s+([\s\S]+)|NOTHING\s*)$/i.exec(
|
|
2289
|
+
clause.trim()
|
|
2290
|
+
);
|
|
2291
|
+
if (m === null) {
|
|
2292
|
+
throw new SimDbError("DB_UNSAFE_OP", `\u65E0\u6CD5\u89E3\u6790 ON CONFLICT \u5B50\u53E5\uFF1A${clause.trim().slice(0, 60)}\u2026`);
|
|
2293
|
+
}
|
|
2294
|
+
const colsRaw = m[1];
|
|
2295
|
+
const cols = colsRaw === void 0 ? fallbackCols : colsRaw.split(",").map(ident);
|
|
2296
|
+
if (cols.length === 0) throw new SimDbError("DB_UNSAFE_OP", "ON CONFLICT \u51B2\u7A81\u5217\u4E3A\u7A7A");
|
|
2297
|
+
const action = m[2] ?? "";
|
|
2298
|
+
if (/^NOTHING/i.test(action)) return { cols, sets: [] };
|
|
2299
|
+
const setRaw = m[3] ?? "";
|
|
2300
|
+
const sets = splitListItems(setRaw).map((part) => {
|
|
2301
|
+
const eq = /^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*([\s\S]+)$/i.exec(part.trim());
|
|
2302
|
+
if (eq === null) {
|
|
2303
|
+
throw new SimDbError("DB_UNSAFE_OP", `\u65E0\u6CD5\u89E3\u6790 SET \u8D4B\u503C\uFF1A${part.trim().slice(0, 60)}\u2026`);
|
|
2304
|
+
}
|
|
2305
|
+
const col = ident(eq[1] ?? "");
|
|
2306
|
+
const rhs = (eq[2] ?? "").trim();
|
|
2307
|
+
let value;
|
|
2308
|
+
if (rhs === "?") {
|
|
2309
|
+
value = cursor.take();
|
|
2310
|
+
} else {
|
|
2311
|
+
const ex = /^excluded\.([A-Za-z_][A-Za-z0-9_]*)$/i.exec(rhs);
|
|
2312
|
+
if (ex !== null) {
|
|
2313
|
+
value = new ExcludedRef(ident(ex[1] ?? ""));
|
|
2314
|
+
} else if (/^([A-Za-z_][A-Za-z0-9_]*)$/.test(rhs) && !/^NULL$/i.test(rhs)) {
|
|
2315
|
+
value = new ColumnRef(ident(rhs));
|
|
2316
|
+
} else if (/^'.*'$/.test(rhs) || /^".*"$/.test(rhs) || /^NULL$/i.test(rhs)) {
|
|
2317
|
+
value = stripQuotes(rhs);
|
|
2318
|
+
} else {
|
|
2319
|
+
value = new ExprRef(rhs);
|
|
2320
|
+
}
|
|
2321
|
+
}
|
|
2322
|
+
return { col, value };
|
|
2323
|
+
});
|
|
2324
|
+
return { cols, sets };
|
|
2325
|
+
}
|
|
2104
2326
|
function matchValue(value, clause) {
|
|
2105
2327
|
const op = clause.operator;
|
|
2106
2328
|
if (op === "IS NULL") return value === null || value === void 0;
|
|
@@ -2254,16 +2476,36 @@ var SimSqlEngine = class {
|
|
|
2254
2476
|
const valueBody = match[4].trim();
|
|
2255
2477
|
const cursor = new ParamCursor(params);
|
|
2256
2478
|
const table = this.ensureTable(tableName);
|
|
2479
|
+
const { tuples, conflict } = splitInsertTuples(valueBody);
|
|
2480
|
+
if (tuples.length === 0) throw new SimDbError("DB_UNSAFE_OP", "INSERT \u7F3A\u5C11 VALUES \u5143\u7EC4");
|
|
2481
|
+
const conflictSpec = conflict === null ? null : parseConflictClause(
|
|
2482
|
+
conflict,
|
|
2483
|
+
cursor,
|
|
2484
|
+
table.columns.filter((c) => c.primaryKey).map((c) => c.name)
|
|
2485
|
+
);
|
|
2257
2486
|
let inserted = 0;
|
|
2258
|
-
for (const
|
|
2259
|
-
const
|
|
2260
|
-
const values = splitListItems(inner).map(
|
|
2487
|
+
for (const tuple of tuples) {
|
|
2488
|
+
const values = splitListItems(tuple).map(
|
|
2261
2489
|
(item) => item.trim() === "?" ? cursor.take() : stripQuotes(item)
|
|
2262
2490
|
);
|
|
2263
2491
|
const row = {};
|
|
2264
2492
|
cols.forEach((col, i) => {
|
|
2265
2493
|
row[col] = values[i] ?? null;
|
|
2266
2494
|
});
|
|
2495
|
+
if (conflictSpec !== null) {
|
|
2496
|
+
const existing = table.rows.find(
|
|
2497
|
+
(r) => conflictSpec.cols.every((c) => r[c] === row[c])
|
|
2498
|
+
);
|
|
2499
|
+
if (existing !== void 0) {
|
|
2500
|
+
const oldRow = { ...existing };
|
|
2501
|
+
for (const set of conflictSpec.sets) {
|
|
2502
|
+
existing[set.col] = resolveSetValue(set.value, oldRow, row);
|
|
2503
|
+
}
|
|
2504
|
+
inserted += 1;
|
|
2505
|
+
void this.persist();
|
|
2506
|
+
continue;
|
|
2507
|
+
}
|
|
2508
|
+
}
|
|
2267
2509
|
this.applyAutoincrement(table, row);
|
|
2268
2510
|
table.rows.push(row);
|
|
2269
2511
|
inserted += 1;
|
|
@@ -2332,6 +2574,20 @@ var SimSqlEngine = class {
|
|
|
2332
2574
|
return project(this.catalog(), selectRaw);
|
|
2333
2575
|
}
|
|
2334
2576
|
const table = this.ensureTable(tableName);
|
|
2577
|
+
let rows = table.rows;
|
|
2578
|
+
const joins = parseLeftJoins(tail);
|
|
2579
|
+
if (joins.length > 0) {
|
|
2580
|
+
rows = table.rows.map((row) => {
|
|
2581
|
+
const merged = { ...row };
|
|
2582
|
+
for (const j of joins) {
|
|
2583
|
+
const joined = this.tables[j.table]?.rows.find((jr) => jr[j.leftCol] === merged[j.rightCol]);
|
|
2584
|
+
if (joined !== void 0) {
|
|
2585
|
+
for (const [k, v] of Object.entries(joined)) merged[`${j.alias}.${k}`] = v;
|
|
2586
|
+
}
|
|
2587
|
+
}
|
|
2588
|
+
return merged;
|
|
2589
|
+
});
|
|
2590
|
+
}
|
|
2335
2591
|
const whereMatch = /\bWHERE\b/i.exec(tail);
|
|
2336
2592
|
const orderMatch = /\bORDER\s+BY\b/i.exec(tail);
|
|
2337
2593
|
const limitMatch = /\bLIMIT\b/i.exec(tail);
|
|
@@ -2340,7 +2596,6 @@ var SimSqlEngine = class {
|
|
|
2340
2596
|
whereMatch.index + whereMatch[0].length,
|
|
2341
2597
|
indexAfter(whereMatch.index, [orderMatch, limitMatch, offsetMatch], tail)
|
|
2342
2598
|
);
|
|
2343
|
-
let rows = table.rows;
|
|
2344
2599
|
if (whereRaw.trim().length > 0) {
|
|
2345
2600
|
const clauses = parseWhereClauses(whereRaw, cursor);
|
|
2346
2601
|
rows = rows.filter((row) => matchWhere(clauses, row));
|
|
@@ -2350,9 +2605,9 @@ var SimSqlEngine = class {
|
|
|
2350
2605
|
orderMatch.index + orderMatch[0].length,
|
|
2351
2606
|
indexAfter(orderMatch.index, [limitMatch, offsetMatch], tail)
|
|
2352
2607
|
);
|
|
2353
|
-
const oc = /^\s*((?:"[^"]+")|([A-Za-z_][A-Za-z0-9_]*))\s+(asc|desc)/i.exec(orderRaw);
|
|
2608
|
+
const oc = /^\s*((?:"[^"]+")|([A-Za-z_][A-Za-z0-9_.]*))\s+(asc|desc)/i.exec(orderRaw);
|
|
2354
2609
|
if (oc !== null) {
|
|
2355
|
-
const col = ident(oc[1]);
|
|
2610
|
+
const col = unqualifyColumn(ident(oc[1]));
|
|
2356
2611
|
const dir = oc[3].toLowerCase();
|
|
2357
2612
|
rows = [...rows].toSorted((a, b) => {
|
|
2358
2613
|
const av = a[col];
|
|
@@ -2397,6 +2652,15 @@ function indexAfter(start, matches, tail) {
|
|
|
2397
2652
|
const end = candidates.length === 0 ? -1 : Math.min(...candidates);
|
|
2398
2653
|
return end === -1 ? tail.length : end;
|
|
2399
2654
|
}
|
|
2655
|
+
function parseSelectItem(raw) {
|
|
2656
|
+
const t = raw.trim();
|
|
2657
|
+
const as = /\s+AS\s+([A-Za-z_][A-Za-z0-9_]*)$/i.exec(t);
|
|
2658
|
+
let name = as === null ? t : t.slice(0, as.index).trim();
|
|
2659
|
+
if (unqualifyColumn(name) === "*") return { name: "", qualified: "", alias: "", star: true };
|
|
2660
|
+
const qualified = /^[A-Za-z_][A-Za-z0-9_]*\.[A-Za-z_][A-Za-z0-9_]*$/.test(name) ? ident(name) : "";
|
|
2661
|
+
const plain = unqualifyColumn(ident(name));
|
|
2662
|
+
return { name: plain, qualified, alias: as === null ? plain : ident(as[1] ?? ""), star: false };
|
|
2663
|
+
}
|
|
2400
2664
|
function project(rows, selectRaw) {
|
|
2401
2665
|
if (/^count\s*\(\s*\*/i.test(selectRaw) || /^count\s*\(\s*1\)/i.test(selectRaw)) {
|
|
2402
2666
|
const alias = selectRaw.match(/\bAS\s+([A-Za-z_][A-Za-z0-9_]*)/i)?.[1] ?? "n";
|
|
@@ -2407,10 +2671,21 @@ function project(rows, selectRaw) {
|
|
|
2407
2671
|
return [{ [constMatch[2]]: Number(constMatch[1]) }];
|
|
2408
2672
|
}
|
|
2409
2673
|
if (selectRaw.trim() === "*") return rows;
|
|
2410
|
-
const
|
|
2674
|
+
const items = selectRaw.split(",").map(parseSelectItem);
|
|
2675
|
+
const valueOf = (row, item) => item.qualified !== "" && row[item.qualified] !== void 0 ? row[item.qualified] : row[item.name];
|
|
2676
|
+
if (items.some((item) => item.star)) {
|
|
2677
|
+
return rows.map((row) => {
|
|
2678
|
+
const out = { ...row };
|
|
2679
|
+
for (const item of items) {
|
|
2680
|
+
if (item.star) continue;
|
|
2681
|
+
out[item.alias] = valueOf(row, item);
|
|
2682
|
+
}
|
|
2683
|
+
return out;
|
|
2684
|
+
});
|
|
2685
|
+
}
|
|
2411
2686
|
return rows.map((row) => {
|
|
2412
2687
|
const out = {};
|
|
2413
|
-
for (const
|
|
2688
|
+
for (const item of items) out[item.alias] = valueOf(row, item);
|
|
2414
2689
|
return out;
|
|
2415
2690
|
});
|
|
2416
2691
|
}
|