@minatojs/sql-utils
Advanced tools
Comparing version 4.2.1 to 4.2.2
import { Dict } from 'cosmokit'; | ||
import { Eval, Field, Model, Modifier, Query, Selection } from '@minatojs/core'; | ||
export declare function escapeId(value: string): string; | ||
export declare function isBracketed(value: string): boolean; | ||
export type QueryOperators = { | ||
@@ -23,5 +24,5 @@ [K in keyof Query.FieldExpr]?: (key: string, value: NonNullable<Query.FieldExpr[K]>) => string; | ||
group?: boolean; | ||
tables?: Dict<Model>; | ||
} | ||
export declare class Builder { | ||
tables?: Dict<Model<any>> | undefined; | ||
protected escapeMap: {}; | ||
@@ -38,3 +39,3 @@ protected escapeRegExp?: RegExp; | ||
private readonly _timezone; | ||
constructor(tables?: Dict<Model<any>> | undefined); | ||
constructor(tables?: Dict<Model>); | ||
protected unescapeId(value: string): string; | ||
@@ -65,3 +66,3 @@ protected createNullQuery(key: string, value: boolean): string; | ||
suffix(modifier: Modifier): string; | ||
get(sel: Selection.Immutable, inline?: boolean, group?: boolean): any; | ||
get(sel: Selection.Immutable, inline?: boolean, group?: boolean, addref?: boolean): any; | ||
define<S, T>(converter: Transformer<S, T>): void; | ||
@@ -68,0 +69,0 @@ dump(model: Model, obj: any): any; |
@@ -25,3 +25,4 @@ "use strict"; | ||
Builder: () => Builder, | ||
escapeId: () => escapeId | ||
escapeId: () => escapeId, | ||
isBracketed: () => isBracketed | ||
}); | ||
@@ -35,5 +36,20 @@ module.exports = __toCommonJS(src_exports); | ||
__name(escapeId, "escapeId"); | ||
function isBracketed(value) { | ||
return value.startsWith("(") && value.endsWith(")"); | ||
} | ||
__name(isBracketed, "isBracketed"); | ||
var _Builder = class _Builder { | ||
escapeMap = {}; | ||
escapeRegExp; | ||
types = {}; | ||
createEqualQuery = this.comparator("="); | ||
queryOperators; | ||
evalOperators; | ||
state = {}; | ||
$true = "1"; | ||
$false = "0"; | ||
modifiedTable; | ||
_timezone = `+${(/* @__PURE__ */ new Date()).getTimezoneOffset() / -60}:00`.replace("+-", "-"); | ||
constructor(tables) { | ||
this.tables = tables; | ||
this.state.tables = tables; | ||
this.queryOperators = { | ||
@@ -147,13 +163,2 @@ // logical | ||
} | ||
escapeMap = {}; | ||
escapeRegExp; | ||
types = {}; | ||
createEqualQuery = this.comparator("="); | ||
queryOperators; | ||
evalOperators; | ||
state = {}; | ||
$true = "1"; | ||
$false = "0"; | ||
modifiedTable; | ||
_timezone = `+${(/* @__PURE__ */ new Date()).getTimezoneOffset() / -60}:00`.replace("+-", "-"); | ||
unescapeId(value) { | ||
@@ -332,3 +337,3 @@ return value.slice(1, value.length - 1); | ||
const [table, key] = args; | ||
const fields = ((_b = (_a = this.tables) == null ? void 0 : _a[table]) == null ? void 0 : _b.fields) || {}; | ||
const fields = ((_b = (_a = this.state.tables) == null ? void 0 : _a[table]) == null ? void 0 : _b.fields) || {}; | ||
const fkey = Object.keys(fields).find((field) => key === field || key.startsWith(field + ".")); | ||
@@ -344,3 +349,3 @@ if (fkey && ((_c = fields[fkey]) == null ? void 0 : _c.expr)) { | ||
} | ||
const prefix = this.modifiedTable ? `${this.escapeId((_h = (_g = (_f = this.tables) == null ? void 0 : _f[table]) == null ? void 0 : _g.name) != null ? _h : this.modifiedTable)}.` : !this.tables || table === "_" || key in fields || Object.keys(this.tables).length === 1 && table in this.tables ? "" : `${this.escapeId(table)}.`; | ||
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)}.`; | ||
return this.transformKey(key, fields, prefix, `${table}.${key}`); | ||
@@ -375,3 +380,3 @@ } | ||
} | ||
get(sel, inline = false, group = false) { | ||
get(sel, inline = false, group = false, addref = true) { | ||
var _a; | ||
@@ -398,10 +403,14 @@ const { args, table, query, ref, model } = sel; | ||
const sqlTypes2 = {}; | ||
prefix = Object.entries(table).map(([key, table2]) => { | ||
const t = `${this.get(table2, true)} AS ${this.escapeId(key)}`; | ||
const joins = Object.entries(table).map(([key, table2]) => { | ||
const thisState = this.state; | ||
this.state = { tables: table2.tables }; | ||
const t = `${this.get(table2, true, false, false)} AS ${this.escapeId(key)}`; | ||
for (const [fieldKey, fieldType] of Object.entries(this.state.sqlTypes)) { | ||
sqlTypes2[`${key}.${fieldKey}`] = fieldType; | ||
} | ||
this.state = thisState; | ||
return t; | ||
}).join(" JOIN "); | ||
}); | ||
this.state.sqlTypes = sqlTypes2; | ||
prefix = " " + joins[0] + joins.slice(1, -1).map((join) => ` JOIN ${join} ON ${this.$true}`).join(" ") + ` JOIN ` + joins.at(-1); | ||
const filter2 = this.parseEval(args[0].having); | ||
@@ -427,5 +436,5 @@ prefix += ` ON ${filter2}`; | ||
if (inline && !args[0].fields && !suffix) { | ||
return prefix.startsWith("(") && prefix.endsWith(")") ? `${prefix} ${ref}` : prefix; | ||
return addref && isBracketed(prefix) ? `${prefix} ${ref}` : prefix; | ||
} | ||
if (!prefix.includes(" ") || prefix.startsWith("(")) { | ||
if (!prefix.includes(" ") || isBracketed(prefix)) { | ||
suffix = ` ${ref}` + suffix; | ||
@@ -511,4 +520,5 @@ } | ||
Builder, | ||
escapeId | ||
escapeId, | ||
isBracketed | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@minatojs/sql-utils", | ||
"version": "4.2.1", | ||
"version": "4.2.2", | ||
"description": "SQL Utilities for Minato", | ||
@@ -33,7 +33,7 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"@minatojs/core": "^2.8.0" | ||
"@minatojs/core": "^2.8.1" | ||
}, | ||
"dependencies": { | ||
"cosmokit": "^1.5.1" | ||
"cosmokit": "^1.5.2" | ||
} | ||
} |
@@ -8,2 +8,6 @@ import { Dict, isNullable } from 'cosmokit' | ||
export function isBracketed(value: string) { | ||
return value.startsWith('(') && value.endsWith(')') | ||
} | ||
export type QueryOperators = { | ||
@@ -31,2 +35,3 @@ [K in keyof Query.FieldExpr]?: (key: string, value: NonNullable<Query.FieldExpr[K]>) => string | ||
group?: boolean | ||
tables?: Dict<Model> | ||
} | ||
@@ -48,3 +53,5 @@ | ||
constructor(public tables?: Dict<Model>) { | ||
constructor(tables?: Dict<Model>) { | ||
this.state.tables = tables | ||
this.queryOperators = { | ||
@@ -368,3 +375,3 @@ // logical | ||
const [table, key] = args | ||
const fields = this.tables?.[table]?.fields || {} | ||
const fields = this.state.tables?.[table]?.fields || {} | ||
const fkey = Object.keys(fields).find(field => key === field || key.startsWith(field + '.')) | ||
@@ -380,5 +387,7 @@ if (fkey && fields[fkey]?.expr) { | ||
} | ||
const prefix = this.modifiedTable ? `${this.escapeId(this.tables?.[table]?.name ?? this.modifiedTable)}.` : (!this.tables || table === '_' || key in fields | ||
const prefix = this.modifiedTable ? `${this.escapeId(this.state.tables?.[table]?.name ?? this.modifiedTable)}.` | ||
: (!this.state.tables || table === '_' || key in fields | ||
// the only table must be the main table | ||
|| (Object.keys(this.tables).length === 1 && table in this.tables) ? '' : `${this.escapeId(table)}.`) | ||
|| (Object.keys(this.state.tables).length === 1 && table in this.state.tables) ? '' : `${this.escapeId(table)}.`) | ||
return this.transformKey(key, fields, prefix, `${table}.${key}`) | ||
@@ -413,5 +422,4 @@ } | ||
get(sel: Selection.Immutable, inline = false, group = false) { | ||
get(sel: Selection.Immutable, inline = false, group = false, addref = true) { | ||
const { args, table, query, ref, model } = sel | ||
// get prefix | ||
@@ -433,10 +441,15 @@ let prefix: string | undefined | ||
const sqlTypes: Dict<SQLType> = {} | ||
prefix = Object.entries(table).map(([key, table]) => { | ||
const t = `${this.get(table, true)} AS ${this.escapeId(key)}` | ||
const joins: string[] = Object.entries(table).map(([key, table]) => { | ||
const thisState = this.state | ||
this.state = { tables: table.tables } | ||
const t = `${this.get(table, true, false, false)} AS ${this.escapeId(key)}` | ||
for (const [fieldKey, fieldType] of Object.entries(this.state.sqlTypes!)) { | ||
sqlTypes[`${key}.${fieldKey}`] = fieldType | ||
} | ||
this.state = thisState | ||
return t | ||
}).join(' JOIN ') | ||
}) | ||
this.state.sqlTypes = sqlTypes | ||
// the leading space is to prevent from being parsed as bracketed and added ref | ||
prefix = ' ' + joins[0] + joins.slice(1, -1).map(join => ` JOIN ${join} ON ${this.$true}`).join(' ') + ` JOIN ` + joins.at(-1) | ||
const filter = this.parseEval(args[0].having) | ||
@@ -470,6 +483,6 @@ prefix += ` ON ${filter}` | ||
if (inline && !args[0].fields && !suffix) { | ||
return (prefix.startsWith('(') && prefix.endsWith(')')) ? `${prefix} ${ref}` : prefix | ||
return (addref && isBracketed(prefix)) ? `${prefix} ${ref}` : prefix | ||
} | ||
if (!prefix.includes(' ') || prefix.startsWith('(')) { | ||
if (!prefix.includes(' ') || isBracketed(prefix)) { | ||
suffix = ` ${ref}` + suffix | ||
@@ -476,0 +489,0 @@ } |
Sorry, the diff of this file is not supported yet
60533
1078
Updatedcosmokit@^1.5.2