@minatojs/sql-utils
Advanced tools
Comparing version 4.2.3 to 4.3.0
@@ -25,2 +25,5 @@ import { Dict } from 'cosmokit'; | ||
tables?: Dict<Model>; | ||
refFields?: Dict<string>; | ||
refTables?: Dict<Model>; | ||
wrappedSubquery?: boolean; | ||
} | ||
@@ -50,2 +53,3 @@ export declare class Builder { | ||
protected logicalNot(condition: string): string; | ||
protected parseSelection(sel: Selection): string; | ||
protected jsonLength(value: string): string; | ||
@@ -65,2 +69,3 @@ protected jsonContains(obj: string, value: string): string; | ||
parseEval(expr: any, unquote?: boolean): string; | ||
protected saveState(extra?: Partial<State>): () => void; | ||
suffix(modifier: Modifier): string; | ||
@@ -67,0 +72,0 @@ get(sel: Selection.Immutable, inline?: boolean, group?: boolean, addref?: boolean): any; |
@@ -159,3 +159,4 @@ "use strict"; | ||
$object: (fields) => this.groupObject(fields), | ||
$array: (expr) => this.groupArray(this.parseEval(expr, false)) | ||
$array: (expr) => this.groupArray(this.parseEval(expr, false)), | ||
$exec: (sel) => this.parseSelection(sel) | ||
}; | ||
@@ -218,2 +219,14 @@ } | ||
} | ||
parseSelection(sel) { | ||
const { args: [expr], ref, table, tables } = sel; | ||
const restore = this.saveState({ tables }); | ||
const inner = this.get(table, true, true); | ||
const output = this.parseEval(expr, false); | ||
restore(); | ||
if (!sel.args[0].$) { | ||
return `(SELECT ${output} AS value FROM ${inner} ${isBracketed(inner) ? ref : ""})`; | ||
} else { | ||
return `(ifnull((SELECT ${this.groupArray(output)} AS value FROM ${inner} ${isBracketed(inner) ? ref : ""}), json_array()))`; | ||
} | ||
} | ||
jsonLength(value) { | ||
@@ -332,3 +345,3 @@ return `json_length(${value})`; | ||
getRecursive(args) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h; | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o; | ||
if (typeof args === "string") { | ||
@@ -350,2 +363,15 @@ return this.getRecursive(["_", args]); | ||
const prefix = this.modifiedTable ? `${this.escapeId((_h = (_g = (_f = this.state.tables) == null ? void 0 : _f[table]) == null ? void 0 : _g.name) != null ? _h : this.modifiedTable)}.` : !this.state.tables || table === "_" || key in fields || Object.keys(this.state.tables).length === 1 && table in this.state.tables ? "" : `${this.escapeId(table)}.`; | ||
if (!(table in (this.state.tables || {})) && table in (this.state.refTables || {})) { | ||
const fields2 = ((_j = (_i = this.state.refTables) == null ? void 0 : _i[table]) == null ? void 0 : _j.fields) || {}; | ||
const res = ((_k = fields2[key]) == null ? void 0 : _k.expr) ? this.parseEvalExpr((_l = fields2[key]) == null ? void 0 : _l.expr) : this.transformKey(key, fields2, `${this.escapeId(table)}.`, `${table}.${key}`); | ||
if (this.state.wrappedSubquery) { | ||
if (res in ((_m = this.state.refFields) != null ? _m : {})) | ||
return this.state.refFields[res]; | ||
const key2 = `minato_tvar_${(0, import_core.randomId)()}`; | ||
((_o = (_n = this.state).refFields) != null ? _o : _n.refFields = {})[res] = key2; | ||
this.state.sqlType = "json"; | ||
return this.escapeId(key2); | ||
} else | ||
return res; | ||
} | ||
return this.transformKey(key, fields, prefix, `${table}.${key}`); | ||
@@ -360,2 +386,10 @@ } | ||
} | ||
saveState(extra = {}) { | ||
const thisState = this.state; | ||
this.state = { refTables: { ...this.state.refTables || {}, ...this.state.tables || {} }, ...extra }; | ||
return () => { | ||
thisState.sqlType = this.state.sqlType; | ||
this.state = thisState; | ||
}; | ||
} | ||
suffix(modifier) { | ||
@@ -404,4 +438,3 @@ const { limit, offset, sort, group, having } = modifier; | ||
const joins = Object.entries(table).map(([key, table2]) => { | ||
const thisState = this.state; | ||
this.state = { tables: table2.tables }; | ||
const restore = this.saveState({ tables: table2.tables }); | ||
const t = `${this.get(table2, true, false, false)} AS ${this.escapeId(key)}`; | ||
@@ -411,3 +444,3 @@ for (const [fieldKey, fieldType] of Object.entries(this.state.sqlTypes)) { | ||
} | ||
this.state = thisState; | ||
restore(); | ||
return t; | ||
@@ -414,0 +447,0 @@ }); |
{ | ||
"name": "@minatojs/sql-utils", | ||
"version": "4.2.3", | ||
"version": "4.3.0", | ||
"description": "SQL Utilities for Minato", | ||
@@ -33,3 +33,3 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"@minatojs/core": "^2.8.1" | ||
"@minatojs/core": "^2.9.0" | ||
}, | ||
@@ -36,0 +36,0 @@ "dependencies": { |
@@ -35,2 +35,5 @@ import { Dict, isNullable } from 'cosmokit' | ||
tables?: Dict<Model> | ||
refFields?: Dict<string> | ||
refTables?: Dict<Model> | ||
wrappedSubquery?: boolean | ||
} | ||
@@ -178,2 +181,4 @@ | ||
$array: (expr) => this.groupArray(this.parseEval(expr, false)), | ||
$exec: (sel) => this.parseSelection(sel as Selection), | ||
} | ||
@@ -241,2 +246,15 @@ } | ||
protected parseSelection(sel: Selection) { | ||
const { args: [expr], ref, table, tables } = sel | ||
const restore = this.saveState({ tables }) | ||
const inner = this.get(table as Selection, true, true) as string | ||
const output = this.parseEval(expr, false) | ||
restore() | ||
if (!(sel.args[0] as any).$) { | ||
return `(SELECT ${output} AS value FROM ${inner} ${isBracketed(inner) ? ref : ''})` | ||
} else { | ||
return `(ifnull((SELECT ${this.groupArray(output)} AS value FROM ${inner} ${isBracketed(inner) ? ref : ''}), json_array()))` | ||
} | ||
} | ||
protected jsonLength(value: string) { | ||
@@ -392,2 +410,16 @@ return `json_length(${value})` | ||
// field from outer selection | ||
if (!(table in (this.state.tables || {})) && (table in (this.state.refTables || {}))) { | ||
const fields = this.state.refTables?.[table]?.fields || {} | ||
const res = (fields[key]?.expr) ? this.parseEvalExpr(fields[key]?.expr) | ||
: this.transformKey(key, fields, `${this.escapeId(table)}.`, `${table}.${key}`) | ||
if (this.state.wrappedSubquery) { | ||
if (res in (this.state.refFields ?? {})) return this.state.refFields![res] | ||
const key = `minato_tvar_${randomId()}` | ||
;(this.state.refFields ??= {})[res] = key | ||
this.state.sqlType = 'json' | ||
return this.escapeId(key) | ||
} else return res | ||
} | ||
return this.transformKey(key, fields, prefix, `${table}.${key}`) | ||
@@ -404,2 +436,11 @@ } | ||
protected saveState(extra: Partial<State> = {}) { | ||
const thisState = this.state | ||
this.state = { refTables: { ...(this.state.refTables || {}), ...(this.state.tables || {}) }, ...extra } | ||
return () => { | ||
thisState.sqlType = this.state.sqlType | ||
this.state = thisState | ||
} | ||
} | ||
suffix(modifier: Modifier) { | ||
@@ -442,4 +483,3 @@ const { limit, offset, sort, group, having } = modifier | ||
const joins: string[] = Object.entries(table).map(([key, table]) => { | ||
const thisState = this.state | ||
this.state = { tables: table.tables } | ||
const restore = this.saveState({ tables: table.tables }) | ||
const t = `${this.get(table, true, false, false)} AS ${this.escapeId(key)}` | ||
@@ -449,3 +489,3 @@ for (const [fieldKey, fieldType] of Object.entries(this.state.sqlTypes!)) { | ||
} | ||
this.state = thisState | ||
restore() | ||
return t | ||
@@ -452,0 +492,0 @@ }) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
65682
1154