@minatojs/driver-mysql
Advanced tools
Comparing version 2.7.6 to 2.8.0
@@ -29,2 +29,3 @@ import type { Pool, PoolConfig } from 'mysql'; | ||
}; | ||
prequeries: string[]; | ||
constructor(tables?: Dict<Model>, compat?: Compat); | ||
@@ -35,2 +36,3 @@ escape(value: any, field?: Field<any>): string; | ||
protected groupArray(value: string): string; | ||
protected parseSelection(sel: Selection): string; | ||
toUpdateExpr(item: any, key: string, field?: Field, upsert?: boolean): string; | ||
@@ -37,0 +39,0 @@ } |
@@ -167,2 +167,3 @@ "use strict"; | ||
}; | ||
prequeries = []; | ||
escape(value, field) { | ||
@@ -193,3 +194,3 @@ if (value instanceof Date) { | ||
groupArray(value) { | ||
if (!this.compat.maria105) | ||
if (!this.compat.maria) | ||
return super.groupArray(value); | ||
@@ -200,2 +201,32 @@ const res = this.state.sqlType === "json" ? `concat('[', group_concat(${value}), ']')` : `concat('[', group_concat(json_extract(json_object('v', ${value}), '$.v')), ']')`; | ||
} | ||
parseSelection(sel) { | ||
if (!this.compat.maria && !this.compat.mysql57) | ||
return super.parseSelection(sel); | ||
const { args: [expr], ref, table, tables } = sel; | ||
const restore = this.saveState({ wrappedSubquery: true, tables }); | ||
const inner = this.get(table, true, true); | ||
const output = this.parseEval(expr, false); | ||
const refFields = this.state.refFields; | ||
restore(); | ||
let query; | ||
if (!sel.args[0].$) { | ||
query = `(SELECT ${output} AS value FROM ${inner} ${(0, import_sql_utils.isBracketed)(inner) ? ref : ""})`; | ||
} else { | ||
query = `(ifnull((SELECT ${this.groupArray(output)} AS value FROM ${inner} ${(0, import_sql_utils.isBracketed)(inner) ? ref : ""}), json_array()))`; | ||
} | ||
if (Object.keys(refFields != null ? refFields : {}).length) { | ||
const funcname = `minato_tfunc_${(0, import_core.randomId)()}`; | ||
const decls = Object.values(refFields != null ? refFields : {}).map((x) => `${x} JSON`).join(","); | ||
const args = Object.keys(refFields != null ? refFields : {}).map((x) => { | ||
var _a, _b; | ||
return (_b = (_a = this.state.refFields) == null ? void 0 : _a[x]) != null ? _b : x; | ||
}).map((x) => this.jsonQuote(x, true)).join(","); | ||
query = this.state.sqlType === "json" ? `ifnull(${query}, json_array())` : this.jsonQuote(query); | ||
this.prequeries.push(`DROP FUNCTION IF EXISTS ${funcname}`); | ||
this.prequeries.push(`CREATE FUNCTION ${funcname} (${decls}) RETURNS JSON DETERMINISTIC RETURN ${query}`); | ||
this.state.sqlType = "json"; | ||
return `${funcname}(${args})`; | ||
} else | ||
return query; | ||
} | ||
toUpdateExpr(item, key, field, upsert) { | ||
@@ -512,4 +543,4 @@ const escaped = (0, import_sql_utils.escapeId)(key); | ||
return []; | ||
return this.queue(sql).then((data) => { | ||
return data.map((row) => builder.load(model, row)); | ||
return Promise.all([...builder.prequeries, sql].map((x) => this.queue(x))).then((data) => { | ||
return data.at(-1).map((row) => builder.load(model, row)); | ||
}); | ||
@@ -522,4 +553,6 @@ } | ||
const ref = (0, import_sql_utils.isBracketed)(inner) ? sel.ref : ""; | ||
const [data] = await this.queue(`SELECT ${output} AS value FROM ${inner} ${ref}`); | ||
return builder.load(data.value); | ||
const sql = `SELECT ${output} AS value FROM ${inner} ${ref}`; | ||
return Promise.all([...builder.prequeries, sql].map((x) => this.queue(x))).then((data) => { | ||
return builder.load(data.at(-1)[0].value); | ||
}); | ||
} | ||
@@ -526,0 +559,0 @@ async set(sel, data) { |
{ | ||
"name": "@minatojs/driver-mysql", | ||
"version": "2.7.6", | ||
"version": "2.8.0", | ||
"description": "MySQL Driver for Minato", | ||
@@ -29,10 +29,10 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"@minatojs/core": "^2.8.1" | ||
"@minatojs/core": "^2.9.0" | ||
}, | ||
"devDependencies": { | ||
"@minatojs/tests": "^1.8.6", | ||
"@minatojs/tests": "^1.9.0", | ||
"@types/mysql": "^2.15.24" | ||
}, | ||
"dependencies": { | ||
"@minatojs/sql-utils": "^4.2.3", | ||
"@minatojs/sql-utils": "^4.3.0", | ||
"@vlasky/mysql": "^2.18.6", | ||
@@ -39,0 +39,0 @@ "cosmokit": "^1.5.2", |
import { createPool, format } from '@vlasky/mysql' | ||
import type { OkPacket, Pool, PoolConfig, PoolConnection } from 'mysql' | ||
import { Dict, difference, makeArray, pick, Time } from 'cosmokit' | ||
import { Database, Driver, Eval, executeUpdate, Field, isEvalExpr, Model, RuntimeError, Selection } from '@minatojs/core' | ||
import { Database, Driver, Eval, executeUpdate, Field, isEvalExpr, Model, randomId, RuntimeError, Selection } from '@minatojs/core' | ||
import { Builder, escapeId, isBracketed } from '@minatojs/sql-utils' | ||
@@ -117,2 +117,4 @@ import Logger from 'reggol' | ||
prequeries: string[] = [] | ||
constructor(tables?: Dict<Model>, private compat: Compat = {}) { | ||
@@ -180,3 +182,3 @@ super(tables) | ||
protected groupArray(value: string) { | ||
if (!this.compat.maria105) return super.groupArray(value) | ||
if (!this.compat.maria) return super.groupArray(value) | ||
const res = this.state.sqlType === 'json' ? `concat('[', group_concat(${value}), ']')` | ||
@@ -188,2 +190,28 @@ : `concat('[', group_concat(json_extract(json_object('v', ${value}), '$.v')), ']')` | ||
protected parseSelection(sel: Selection) { | ||
if (!this.compat.maria && !this.compat.mysql57) return super.parseSelection(sel) | ||
const { args: [expr], ref, table, tables } = sel | ||
const restore = this.saveState({ wrappedSubquery: true, tables }) | ||
const inner = this.get(table as Selection, true, true) as string | ||
const output = this.parseEval(expr, false) | ||
const refFields = this.state.refFields | ||
restore() | ||
let query: string | ||
if (!(sel.args[0] as any).$) { | ||
query = `(SELECT ${output} AS value FROM ${inner} ${isBracketed(inner) ? ref : ''})` | ||
} else { | ||
query = `(ifnull((SELECT ${this.groupArray(output)} AS value FROM ${inner} ${isBracketed(inner) ? ref : ''}), json_array()))` | ||
} | ||
if (Object.keys(refFields ?? {}).length) { | ||
const funcname = `minato_tfunc_${randomId()}` | ||
const decls = Object.values(refFields ?? {}).map(x => `${x} JSON`).join(',') | ||
const args = Object.keys(refFields ?? {}).map(x => this.state.refFields?.[x] ?? x).map(x => this.jsonQuote(x, true)).join(',') | ||
query = this.state.sqlType === 'json' ? `ifnull(${query}, json_array())` : this.jsonQuote(query) | ||
this.prequeries.push(`DROP FUNCTION IF EXISTS ${funcname}`) | ||
this.prequeries.push(`CREATE FUNCTION ${funcname} (${decls}) RETURNS JSON DETERMINISTIC RETURN ${query}`) | ||
this.state.sqlType = 'json' | ||
return `${funcname}(${args})` | ||
} else return query | ||
} | ||
toUpdateExpr(item: any, key: string, field?: Field, upsert?: boolean) { | ||
@@ -534,4 +562,4 @@ const escaped = escapeId(key) | ||
if (!sql) return [] | ||
return this.queue(sql).then((data) => { | ||
return data.map((row) => builder.load(model, row)) | ||
return Promise.all([...builder.prequeries, sql].map(x => this.queue(x))).then((data) => { | ||
return data.at(-1).map((row) => builder.load(model, row)) | ||
}) | ||
@@ -545,4 +573,6 @@ } | ||
const ref = isBracketed(inner) ? sel.ref : '' | ||
const [data] = await this.queue(`SELECT ${output} AS value FROM ${inner} ${ref}`) | ||
return builder.load(data.value) | ||
const sql = `SELECT ${output} AS value FROM ${inner} ${ref}` | ||
return Promise.all([...builder.prequeries, sql].map(x => this.queue(x))).then((data) => { | ||
return builder.load(data.at(-1)[0].value) | ||
}) | ||
} | ||
@@ -549,0 +579,0 @@ |
Sorry, the diff of this file is not supported yet
76648
1382
Updated@minatojs/sql-utils@^4.3.0