Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge: type
Browse files Browse the repository at this point in the history
Hieuzest committed Nov 2, 2023
1 parent d12245a commit f11557b
Showing 2 changed files with 25 additions and 16 deletions.
34 changes: 19 additions & 15 deletions packages/mysql/src/index.ts
Original file line number Diff line number Diff line change
@@ -115,10 +115,10 @@ class MySQLBuilder extends Builder {
constructor(tables?: Dict<Model>, private compat: Compat = {}) {
super(tables)

this.evalOperators.$sum = (expr) => this.createAggr(expr, value => `ifnull(sum(${value}), 0)`, undefined, value => `ifnull(mj_sum(${value}), 0)`)
this.evalOperators.$avg = (expr) => this.createAggr(expr, value => `avg(${value})`, undefined, value => `mj_avg(${value})`)
this.evalOperators.$min = (expr) => this.createAggr(expr, value => `(0+min(${value}))`, undefined, value => `(0+mj_min(${value}))`)
this.evalOperators.$max = (expr) => this.createAggr(expr, value => `(0+max(${value}))`, undefined, value => `(0+mj_max(${value}))`)
this.evalOperators.$sum = (expr) => this.createAggr(expr, value => `ifnull(sum(${value}), 0)`, undefined, value => `ifnull(minato_cfunc_sum(${value}), 0)`)
this.evalOperators.$avg = (expr) => this.createAggr(expr, value => `avg(${value})`, undefined, value => `minato_cfunc_avg(${value})`)
this.evalOperators.$min = (expr) => this.createAggr(expr, value => `(0+min(${value}))`, undefined, value => `(0+minato_cfunc_min(${value}))`)
this.evalOperators.$max = (expr) => this.createAggr(expr, value => `(0+max(${value}))`, undefined, value => `(0+minato_cfunc_max(${value}))`)

this.define<string[], string>({
types: ['list'],
@@ -150,7 +150,7 @@ class MySQLBuilder extends Builder {
}

protected createAggr(expr: any, aggr: (value: string) => string, nonaggr?: (value: string) => string, compat?: (value: string) => string) {
if (!this.state.group && compat && (this.compat.mysql57 || this.compat.maria105)) {
if (!this.state.group && compat && (this.compat.mysql57 || this.compat.maria)) {
const value = compat(this.parseEval(expr, false))
this.state.sqlType = 'raw'
return value
@@ -181,7 +181,11 @@ class MySQLBuilder extends Builder {
let query: string
if (!(sel.args[0] as any).$) {
this.state.sqlType = 'raw'
query = `(SELECT ${output} AS value FROM ${inner} ${ref})`
if (inner.startsWith('(') && inner.endsWith(')')) {
query = `(SELECT ${output} AS value FROM ${inner} ${ref})`
} else {
query = `(SELECT ${output} AS value FROM ${inner})`
}
} else if (inner.startsWith('(') && inner.endsWith(')')) {
query = `(ifnull((SELECT ${this.groupArray(output)} AS value FROM ${inner} ${ref}), json_array()))`
} else {
@@ -314,7 +318,7 @@ export class MySQLDriver extends Driver {
// For json_table
this._compat.mysql57 = !!version.match(/5.7.\d+/)

if (this._compat.mysql57 || this._compat.maria105) {
if (this._compat.mysql57 || this._compat.maria) {
await this._setupCompatFunctions()
}

@@ -446,20 +450,20 @@ export class MySQLDriver extends Driver {

async _setupCompatFunctions() {
const log = () => logger.debug('Failed to setup compact functions')
await this.query(`DROP FUNCTION IF EXISTS mj_sum`).catch(log)
await this.query(`CREATE FUNCTION mj_sum (j JSON) RETURNS DOUBLE DETERMINISTIC BEGIN DECLARE n int; DECLARE i int; DECLARE r DOUBLE;
await this.query(`DROP FUNCTION IF EXISTS minato_cfunc_sum`).catch(log)
await this.query(`CREATE FUNCTION minato_cfunc_sum (j JSON) RETURNS DOUBLE DETERMINISTIC BEGIN DECLARE n int; DECLARE i int; DECLARE r DOUBLE;
DROP TEMPORARY TABLE IF EXISTS mtt; CREATE TEMPORARY TABLE mtt (value JSON); SELECT json_length(j) into n; set i = 0; WHILE i<n DO
INSERT INTO mtt VALUES(json_extract(j, concat('$[', i, ']'))); SET i=i+1; END WHILE; SELECT sum(value) INTO r FROM mtt; RETURN r; END`).catch(log)
await this.query(`DROP FUNCTION IF EXISTS mj_avg`).catch(log)
await this.query(`CREATE FUNCTION mj_avg (j JSON) RETURNS DOUBLE DETERMINISTIC BEGIN DECLARE n int; DECLARE i int; DECLARE r DOUBLE;
await this.query(`DROP FUNCTION IF EXISTS minato_cfunc_avg`).catch(log)
await this.query(`CREATE FUNCTION minato_cfunc_avg (j JSON) RETURNS DOUBLE DETERMINISTIC BEGIN DECLARE n int; DECLARE i int; DECLARE r DOUBLE;
DROP TEMPORARY TABLE IF EXISTS mtt; CREATE TEMPORARY TABLE mtt (value JSON); SELECT json_length(j) into n; set i = 0; WHILE i<n DO
INSERT INTO mtt VALUES(json_extract(j, concat('$[', i, ']'))); SET i=i+1; END WHILE; SELECT avg(value) INTO r FROM mtt; RETURN r; END`).catch(log)
await this.query(`DROP FUNCTION IF EXISTS mj_min`).catch(log)
await this.query(`CREATE FUNCTION mj_min (j JSON) RETURNS DOUBLE DETERMINISTIC BEGIN DECLARE n int; DECLARE i int; DECLARE r DOUBLE;
await this.query(`DROP FUNCTION IF EXISTS minato_cfunc_min`).catch(log)
await this.query(`CREATE FUNCTION minato_cfunc_min (j JSON) RETURNS DOUBLE DETERMINISTIC BEGIN DECLARE n int; DECLARE i int; DECLARE r DOUBLE;
DROP TEMPORARY TABLE IF EXISTS mtt; CREATE TEMPORARY TABLE mtt (value JSON); SELECT json_length(j) into n; set i = 0; WHILE i<n DO
INSERT INTO mtt VALUES(json_extract(j, concat('$[', i, ']'))); SET i=i+1; END WHILE; SELECT min(value) INTO r FROM mtt; RETURN r; END`).catch(log)
await this.query(`DROP FUNCTION IF EXISTS mj_max`).catch(log)
await this.query(`CREATE FUNCTION mj_max (j JSON) RETURNS DOUBLE DETERMINISTIC BEGIN DECLARE n int; DECLARE i int; DECLARE r DOUBLE;
await this.query(`DROP FUNCTION IF EXISTS minato_cfunc_max`).catch(log)
await this.query(`CREATE FUNCTION minato_cfunc_max (j JSON) RETURNS DOUBLE DETERMINISTIC BEGIN DECLARE n int; DECLARE i int; DECLARE r DOUBLE;
DROP TEMPORARY TABLE IF EXISTS mtt; CREATE TEMPORARY TABLE mtt (value JSON); SELECT json_length(j) into n; set i = 0; WHILE i<n DO
INSERT INTO mtt VALUES(json_extract(j, concat('$[', i, ']'))); SET i=i+1; END WHILE; SELECT max(value) INTO r FROM mtt; RETURN r; END`).catch(log)
}
7 changes: 6 additions & 1 deletion packages/sql-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -216,9 +216,14 @@ export class Builder {
const inner = this.get(table as Selection, true) as string
this.tables = thisTables
this.state = thisState
console.log(inner)
if (!(sel.args[0] as any).$) {
this.state.sqlType = 'raw'
return `(SELECT ${output} AS value FROM ${inner} ${ref})`
if (inner.startsWith('(') && inner.endsWith(')')) {
return `(SELECT ${output} AS value FROM ${inner} ${ref})`
} else {
return `(SELECT ${output} AS value FROM ${inner})`
}
} else if (inner.startsWith('(') && inner.endsWith(')')) {
return `(ifnull((SELECT ${this.groupArray(output)} AS value FROM ${inner} ${ref}), json_array()))`
} else {

0 comments on commit f11557b

Please sign in to comment.