mql2
Advanced tools
Comparing version 0.1.6 to 0.1.7
49
mql.js
@@ -7,3 +7,3 @@ import { | ||
import mysql from 'mysql'; | ||
import { plural } from 'pluralize'; | ||
import { plural, singular } from 'pluralize'; | ||
import load_ljoin from './ljoin.js' | ||
@@ -21,3 +21,3 @@ import { dump } from 'dumper.js'; | ||
const SymbolDefault = Symbol('DEFAULT'); | ||
const identity = i => i; | ||
const wrap_arr = a => Array.isArray(a) ? a : [a]; | ||
@@ -133,3 +133,2 @@ const mix = (arr1, arr2) => arr1.reduce((res, item, i) => { | ||
if (queries.find(is_injection)) return SymbolInjection; | ||
var query = reduce((res, query) => { | ||
@@ -280,2 +279,3 @@ if (!query) return res; | ||
option.rels = []; | ||
option.row_number = option.row_number || []; | ||
}), | ||
@@ -289,3 +289,3 @@ function setting([left, ...rest]) { | ||
if (me.rel_type == '-') { | ||
me.left_key = me.left_key || (me.is_poly ? 'id' : me.table.substr(0, me.table.length-1) + '_id'); | ||
me.left_key = me.left_key || (me.is_poly ? 'id' : singular(me.table) + '_id'); | ||
me.where_key = me.key || (me.is_poly ? 'attached_id' : 'id'); | ||
@@ -295,10 +295,10 @@ me.xjoin = tag(); | ||
me.left_key = me.left_key || 'id'; | ||
me.where_key = me.key || (me.is_poly ? 'attached_id' : left.table.substr(0, left.table.length-1) + '_id'); | ||
me.where_key = me.key || (me.is_poly ? 'attached_id' : singular(left.table) + '_id'); | ||
me.xjoin = tag(); | ||
} else if (me.rel_type == 'x') { | ||
me.left_key = me.left_key || 'id'; | ||
me.where_key = '_#_xtable_#_.' + (me.left_xkey || left.table.substr(0, left.table.length-1) + '_id'); | ||
me.where_key = '_#_xtable_#_.' + (me.left_xkey || singular(left.table) + '_id'); | ||
var xtable = me.xtable || (left.table + '_' + me.table); | ||
me.xjoin = SQL `INNER JOIN ${TB(xtable)} as ${escape_dq('_#_xtable_#_')} on ${EQ({ | ||
['_#_xtable_#_.' + (me.xkey || me.table.substr(0, me.table.length-1) + '_id')]: COLUMN(me.as + '.' + (me.key || 'id')) | ||
['_#_xtable_#_.' + (me.xkey || singular(me.table) + '_id')]: COLUMN(me.as + '.' + (me.key || 'id')) | ||
})}`; | ||
@@ -318,3 +318,2 @@ } | ||
FROM ${TB(me.table)} AS ${TB(me.as)} ${me.query}`; | ||
return go( | ||
@@ -330,19 +329,29 @@ [lefts, me], | ||
var colums = uniq(add_column(me).originals.concat(me.where_key + (me.rel_type == 'x' ? ` AS ${fold_key}` : ''))); | ||
const colums = uniq(add_column(me).originals.concat(me.as +'.'+me.where_key + (me.rel_type == 'x' ? ` AS ${fold_key}` : ''))); | ||
var in_vals = filter(a => a != null, pluck(me.left_key, lefts)); | ||
const rights = !in_vals.length ? [] : await QUERY ` | ||
const in_vals = filter(a => a != null, pluck(me.left_key, lefts)); | ||
const is_row_num = me.row_number.length == 2; | ||
const rights = (!in_vals.length ? [] : await is_row_num ? | ||
QUERY ` | ||
SELECT * | ||
FROM ( | ||
SELECT | ||
${COLUMN(...colums)}, | ||
ROW_NUMBER() OVER (PARTITION BY ${CL(me.where_key)} ORDER BY ${me.row_number[1]}) as "--row_number--" | ||
FROM ${TB(me.table)} AS ${TB(me.as)} | ||
${me.xjoin} | ||
WHERE ${IN(me.as +'.'+me.where_key, in_vals)} ${me.poly_type} ${tag(query)} | ||
) AS ${TB(me.as)} | ||
WHERE ${TB(me.as)}."--row_number--"<=${me.row_number[0]}` | ||
: | ||
QUERY ` | ||
SELECT ${COLUMN(...colums)} | ||
FROM ${TB(me.table)} AS ${TB(me.as)} | ||
${me.xjoin} | ||
WHERE | ||
${IN(me.where_key, in_vals)} | ||
${me.poly_type} | ||
${tag(query)}`; | ||
WHERE ${IN(me.where_key, in_vals)} ${me.poly_type} ${tag(query)}`); | ||
var [folder, default_value] = me.rel_type == '-' ? [index_by, () => ({})] : [group_by, () => []]; | ||
const [folder, default_value] = me.rel_type == '-' ? [index_by, () => ({})] : [group_by, () => []]; | ||
return go( | ||
rights, | ||
is_row_num ? map(r => delete r['--row_number--'] && r) : identity, | ||
folder(a => a[fold_key]), | ||
@@ -395,3 +404,3 @@ folded => each(function(left) { | ||
return { | ||
VALUES, IN, NOT_IN, EQ, SET, COLUMN, CL, TABLE, TB, SQL, MQL_DEBUG, | ||
VALUES, IN, NOT_IN, EQ, SET, COLUMN, CL, TABLE, TB, SQL, SQLS, MQL_DEBUG, | ||
QUERY, | ||
@@ -428,3 +437,3 @@ QUERY1, | ||
return { CONNECT, VALUES, IN, NOT_IN, EQ, SET, COLUMN, CL, TABLE, TB, SQL, MQL_DEBUG } | ||
return { CONNECT, VALUES, IN, NOT_IN, EQ, SET, COLUMN, CL, TABLE, TB, SQL, SQLS, MQL_DEBUG } | ||
} | ||
@@ -431,0 +440,0 @@ |
{ | ||
"name": "mql2", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "query builder", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
33030
588