@fxjs/sql-query
Advanced tools
Comparing version 0.9.4 to 0.10.0-alpha.0
@@ -1,47 +0,83 @@ | ||
function between(a, b) { | ||
return createSpecialObject({ from: a, to: b }, 'between'); | ||
function between(a, b, opts) { | ||
return createSpecialObject({ | ||
from: a, to: b, | ||
asIdentifier: !!(opts === null || opts === void 0 ? void 0 : opts.asIdentifier) | ||
}, 'between'); | ||
} | ||
; | ||
function not_between(a, b) { | ||
return createSpecialObject({ from: a, to: b }, 'not_between'); | ||
function not_between(a, b, opts) { | ||
return createSpecialObject({ | ||
from: a, to: b, | ||
asIdentifier: !!(opts === null || opts === void 0 ? void 0 : opts.asIdentifier) | ||
}, 'not_between'); | ||
} | ||
; | ||
function like(expr) { | ||
return createSpecialObject({ expr: expr }, 'like'); | ||
function like(expr, opts) { | ||
return createSpecialObject({ | ||
expr: expr, | ||
asIdentifier: !!(opts === null || opts === void 0 ? void 0 : opts.asIdentifier) | ||
}, 'like'); | ||
} | ||
; | ||
function not_like(expr) { | ||
return createSpecialObject({ expr: expr }, 'not_like'); | ||
function not_like(expr, opts) { | ||
return createSpecialObject({ | ||
expr: expr, | ||
asIdentifier: !!(opts === null || opts === void 0 ? void 0 : opts.asIdentifier) | ||
}, 'not_like'); | ||
} | ||
; | ||
function eq(v) { | ||
return createSpecialObject({ val: v }, 'eq'); | ||
function eq(v, opts) { | ||
return createSpecialObject({ | ||
val: v, | ||
asIdentifier: !!(opts === null || opts === void 0 ? void 0 : opts.asIdentifier) | ||
}, 'eq'); | ||
} | ||
; | ||
function ne(v) { | ||
return createSpecialObject({ val: v }, 'ne'); | ||
function ne(v, opts) { | ||
return createSpecialObject({ | ||
val: v, | ||
asIdentifier: !!(opts === null || opts === void 0 ? void 0 : opts.asIdentifier) | ||
}, 'ne'); | ||
} | ||
; | ||
function gt(v) { | ||
return createSpecialObject({ val: v }, 'gt'); | ||
function gt(v, opts) { | ||
return createSpecialObject({ | ||
val: v, | ||
asIdentifier: !!(opts === null || opts === void 0 ? void 0 : opts.asIdentifier) | ||
}, 'gt'); | ||
} | ||
; | ||
function gte(v) { | ||
return createSpecialObject({ val: v }, 'gte'); | ||
function gte(v, opts) { | ||
return createSpecialObject({ | ||
val: v, | ||
asIdentifier: !!(opts === null || opts === void 0 ? void 0 : opts.asIdentifier) | ||
}, 'gte'); | ||
} | ||
; | ||
function lt(v) { | ||
return createSpecialObject({ val: v }, 'lt'); | ||
function lt(v, opts) { | ||
return createSpecialObject({ | ||
val: v, | ||
asIdentifier: !!(opts === null || opts === void 0 ? void 0 : opts.asIdentifier) | ||
}, 'lt'); | ||
} | ||
; | ||
function lte(v) { | ||
return createSpecialObject({ val: v }, 'lte'); | ||
function lte(v, opts) { | ||
return createSpecialObject({ | ||
val: v, | ||
asIdentifier: !!(opts === null || opts === void 0 ? void 0 : opts.asIdentifier) | ||
}, 'lte'); | ||
} | ||
; | ||
function _in(v) { | ||
return createSpecialObject({ val: v }, 'in'); | ||
function _in(v, opts) { | ||
return createSpecialObject({ | ||
val: v, | ||
asIdentifier: !!(opts === null || opts === void 0 ? void 0 : opts.asIdentifier) | ||
}, 'in'); | ||
} | ||
; | ||
function not_in(v) { | ||
return createSpecialObject({ val: v }, 'not_in'); | ||
function not_in(v, opts) { | ||
return createSpecialObject({ | ||
val: v, | ||
asIdentifier: !!(opts === null || opts === void 0 ? void 0 : opts.asIdentifier) | ||
}, 'not_in'); | ||
} | ||
@@ -48,0 +84,0 @@ ; |
@@ -0,1 +1,2 @@ | ||
import { ChainBuilderBase } from "./Helpers"; | ||
import { FxSqlQueryDialect } from "./Typo/Dialect"; | ||
@@ -10,4 +11,3 @@ import { FxSqlQueryColumns } from "./Typo/Field"; | ||
*/ | ||
export declare class CreateQuery implements FxSqlQueryChainBuilder.ChainBuilder__Create { | ||
private Dialect; | ||
export declare class CreateQuery extends ChainBuilderBase implements FxSqlQueryChainBuilder.ChainBuilder__Create { | ||
tableName: string; | ||
@@ -14,0 +14,0 @@ structure: FxSqlQueryColumns.FieldItemTypeMap; |
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CreateQuery = void 0; | ||
const Helpers_1 = require("./Helpers"); | ||
/** | ||
@@ -9,5 +10,5 @@ * Instantiate a new CREATE-type query builder | ||
*/ | ||
class CreateQuery { | ||
class CreateQuery extends Helpers_1.ChainBuilderBase { | ||
constructor(Dialect) { | ||
this.Dialect = Dialect; | ||
super(Dialect); | ||
this.tableName = null; | ||
@@ -14,0 +15,0 @@ this.structure = {}; |
@@ -9,4 +9,4 @@ /// <reference types="@fibjs/types" /> | ||
export declare function zeroPad(number: string | number, n?: number): string; | ||
export declare function get_table_alias(sql: FxSqlQuerySql.SqlQueryChainDescriptor, table: string): string; | ||
export declare function parseTableInputStr(table_name: FxSqlQuerySql.SqlTableInputType): FxSqlQuerySql.SqlTableTuple; | ||
export declare function get_table_alias(sql: FxSqlQuerySql.SqlQueryChainDescriptor, table: string | FxSqlQuerySql.SqlFromTableInput): string; | ||
export declare function parseTableInputStr(table_name: FxSqlQuerySql.SqlTableInputType, dialect?: FxSqlQueryDialect.DialectType): FxSqlQuerySql.SqlTableTuple; | ||
export declare function pickAliasFromFromDescriptor(fd: FxSqlQuerySql.QueryFromDescriptor): string; | ||
@@ -25,1 +25,16 @@ export declare function pickColumnAsFromSelectFieldsDescriptor(sitem: FxSqlQuerySql.SqlSelectFieldItemDescriptor): FxSqlQuerySql.SqlSelectFieldItemDescriptor['as']; | ||
}; | ||
export declare function maybeKnexSubQuery(input: any): input is import('@fxjs/knex').Knex.QueryBuilder; | ||
export declare function maybeKnexRaw(input: any): input is import('@fxjs/knex').Knex.Raw; | ||
export declare function maybeKnexRawOrQueryBuilder(input: any): input is import('@fxjs/knex').Knex.QueryBuilder | import('@fxjs/knex').Knex.Raw; | ||
export declare function isWrapperdSubQuerySelect(sql: any): sql is `(${string})`; | ||
/** @internal */ | ||
export declare function convertSqlFromTable(sql_from_item: FxSqlQuerySql.QueryFromDescriptor, knex: import('@fxjs/knex').Knex, isSubQuery?: boolean): string | import("@fxjs/knex").Knex.Raw<any>; | ||
export declare class ChainBuilderBase implements FxSqlQueryChainBuilder.ChainBuilder { | ||
protected Dialect: FxSqlQueryDialect.Dialect; | ||
readonly knex: FxSqlQuery.Class_Query['knex']; | ||
constructor(Dialect: FxSqlQueryDialect.Dialect); | ||
/** | ||
* @override | ||
*/ | ||
build(): string; | ||
} |
@@ -5,3 +5,3 @@ // Transforms: | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.cutOffOrderDirectionFromColumnFirstStr = exports.escapeValForKnex = exports.bufferToString = exports.ensureNumber = exports.ucfirst = exports.DialectTypes = exports.defaultTableAliasNameRule = exports.autoIncreatementTableIndex = exports.pickColumnAsFromSelectFieldsDescriptor = exports.pickAliasFromFromDescriptor = exports.parseTableInputStr = exports.get_table_alias = exports.zeroPad = exports.dateToString = exports.escapeQuery = void 0; | ||
exports.ChainBuilderBase = exports.convertSqlFromTable = exports.isWrapperdSubQuerySelect = exports.maybeKnexRawOrQueryBuilder = exports.maybeKnexRaw = exports.maybeKnexSubQuery = exports.cutOffOrderDirectionFromColumnFirstStr = exports.escapeValForKnex = exports.bufferToString = exports.ensureNumber = exports.ucfirst = exports.DialectTypes = exports.defaultTableAliasNameRule = exports.autoIncreatementTableIndex = exports.pickColumnAsFromSelectFieldsDescriptor = exports.pickAliasFromFromDescriptor = exports.parseTableInputStr = exports.get_table_alias = exports.zeroPad = exports.dateToString = exports.escapeQuery = void 0; | ||
// "name LIKE 'John' AND age > 23" | ||
@@ -67,22 +67,29 @@ function escapeQuery(Dialect, query, args) { | ||
} | ||
return table; | ||
return typeof table === 'string' ? table : ''; | ||
} | ||
exports.get_table_alias = get_table_alias; | ||
; | ||
// export function parse_table_alias ( | ||
// table: string, sql: FxSqlQuerySql.SqlQueryChainDescriptor | ||
// ): string { | ||
// let [_, table_alias] = parseTableInputStr(table) | ||
// if (table_alias) | ||
// return table_alias; | ||
// return get_table_alias(sql, table) | ||
// } | ||
function parseTableInputStr(table_name) { | ||
// TODO: add test about it | ||
function unwrapIdentifier(identifier = '', dialect) { | ||
switch (dialect) { | ||
case 'postgresql': | ||
return identifier.replace(/^"|"$/g, ''); | ||
case 'sqlite': | ||
case 'mysql': | ||
return identifier.replace(/^`|`$/g, ''); | ||
} | ||
return identifier; | ||
} | ||
function parseTableInputStr(table_name, dialect) { | ||
if (!table_name) | ||
throw `invalid input table_name!`; | ||
throw new Error(`[parseTableInputStr] invalid input table_name!`); | ||
let ta_tuple = ['', '']; | ||
if (typeof table_name === 'string') { | ||
table_name = table_name.trim(); | ||
if (table_name.indexOf(' as ') > 0) { | ||
ta_tuple = table_name.split(' as ').slice(0, 2); | ||
let idx = table_name.lastIndexOf(' as '); | ||
if (idx > 0) { | ||
ta_tuple = [ | ||
table_name.slice(0, idx).trim(), | ||
table_name.slice(idx + 4).trim() | ||
]; | ||
} | ||
@@ -93,5 +100,13 @@ else { | ||
} | ||
else if (maybeKnexRawOrQueryBuilder(table_name)) { | ||
ta_tuple = [table_name, '']; | ||
} | ||
else { | ||
ta_tuple = table_name.slice(0, 2); | ||
} | ||
if (dialect) { | ||
if (typeof ta_tuple[0] === 'string') | ||
ta_tuple[0] = unwrapIdentifier(ta_tuple[0], dialect); | ||
ta_tuple[1] = unwrapIdentifier(ta_tuple[1], dialect); | ||
} | ||
return ta_tuple; | ||
@@ -146,2 +161,5 @@ } | ||
function escapeValForKnex(val, Dialect, opts) { | ||
// no escape for `knex.raw(...)`, `knex.ref(...)` and subquery | ||
if (maybeKnexRawOrQueryBuilder(val)) | ||
return val; | ||
// never escapeVal those types with `Dialect.escapeVal`, knex would escape them automatically | ||
@@ -191,1 +209,50 @@ const _type = typeof val; | ||
exports.cutOffOrderDirectionFromColumnFirstStr = cutOffOrderDirectionFromColumnFirstStr; | ||
function maybeKnexSubQuery(input) { | ||
return typeof (input === null || input === void 0 ? void 0 : input.toQuery) === "function"; | ||
} | ||
exports.maybeKnexSubQuery = maybeKnexSubQuery; | ||
function maybeKnexRaw(input) { | ||
return typeof (input === null || input === void 0 ? void 0 : input.toQuery) === "function"; | ||
} | ||
exports.maybeKnexRaw = maybeKnexRaw; | ||
function maybeKnexRawOrQueryBuilder(input) { | ||
return typeof (input === null || input === void 0 ? void 0 : input.toQuery) === "function"; | ||
} | ||
exports.maybeKnexRawOrQueryBuilder = maybeKnexRawOrQueryBuilder; | ||
function isWrapperdSubQuerySelect(sql) { | ||
return typeof sql === 'string' && /^\(\s*SELECT\s.*\)$/i.test(sql); | ||
} | ||
exports.isWrapperdSubQuerySelect = isWrapperdSubQuerySelect; | ||
/** @internal */ | ||
function convertSqlFromTable(sql_from_item, knex, isSubQuery) { | ||
var _a, _b; | ||
if (isSubQuery === void 0) { isSubQuery = isWrapperdSubQuerySelect((_b = (_a = sql_from_item.table) === null || _a === void 0 ? void 0 : _a.trim) === null || _b === void 0 ? void 0 : _b.call(_a)); } | ||
// from: select subquery as table | ||
return maybeKnexRawOrQueryBuilder(sql_from_item.table) ? knex.raw(sql_from_item.table).wrap('(', ')') | ||
: isSubQuery ? knex.raw(sql_from_item.table.trim()).wrap('', '') | ||
: sql_from_item.table; | ||
} | ||
exports.convertSqlFromTable = convertSqlFromTable; | ||
class ChainBuilderBase { | ||
constructor(Dialect) { | ||
this.Dialect = Dialect; | ||
Object.defineProperty(this, 'Dialect', { | ||
get() { | ||
return Dialect; | ||
}, | ||
configurable: false | ||
}); | ||
Object.defineProperty(this, 'knex', { | ||
get() { | ||
return Dialect.knex; | ||
}, | ||
configurable: false | ||
}); | ||
} | ||
/** | ||
* @override | ||
*/ | ||
build() { return ''; } | ||
; | ||
} | ||
exports.ChainBuilderBase = ChainBuilderBase; |
@@ -0,6 +1,6 @@ | ||
import { ChainBuilderBase } from "./Helpers"; | ||
import { FxSqlQueryDialect } from "./Typo/Dialect"; | ||
import { FxSqlQuerySql } from "./Typo/Sql"; | ||
import { FxSqlQueryChainBuilder } from "./Typo/Query-ChainBuilder"; | ||
export declare class InsertQuery implements FxSqlQueryChainBuilder.ChainBuilder__Insert { | ||
private Dialect; | ||
export declare class InsertQuery extends ChainBuilderBase implements FxSqlQueryChainBuilder.ChainBuilder__Insert { | ||
private opts; | ||
@@ -7,0 +7,0 @@ private sql; |
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.InsertQuery = void 0; | ||
const Helpers_1 = require("./Helpers"); | ||
class InsertQuery { | ||
class InsertQuery extends Helpers_1.ChainBuilderBase { | ||
constructor(Dialect, opts) { | ||
this.Dialect = Dialect; | ||
super(Dialect); | ||
this.opts = opts; | ||
@@ -8,0 +8,0 @@ this.sql = {}; |
@@ -33,4 +33,5 @@ /// <reference lib="es2017" /> | ||
}); | ||
const knexInst = FKnex({ client: this.Dialect.type, useNullAsDefault: true }); | ||
Object.defineProperty(this.Dialect, 'knex', { | ||
value: FKnex({ client: this.Dialect.type, useNullAsDefault: true }), | ||
value: knexInst, | ||
writable: false, | ||
@@ -40,3 +41,3 @@ configurable: false | ||
Object.defineProperty(this, 'knex', { | ||
value: this.Dialect.knex, | ||
value: knexInst, | ||
writable: false, | ||
@@ -43,0 +44,0 @@ configurable: false |
@@ -0,1 +1,2 @@ | ||
import { ChainBuilderBase } from './Helpers'; | ||
import { FxSqlQuery } from './Typo/Query'; | ||
@@ -5,4 +6,3 @@ import { FxSqlQueryChainBuilder } from './Typo/Query-ChainBuilder'; | ||
import { FxSqlQuerySubQuery } from './Typo/SubQuery'; | ||
export declare class RemoveQuery implements FxSqlQueryChainBuilder.ChainBuilder__Remove { | ||
private Dialect; | ||
export declare class RemoveQuery extends ChainBuilderBase implements FxSqlQueryChainBuilder.ChainBuilder__Remove { | ||
private opts; | ||
@@ -9,0 +9,0 @@ private sql; |
@@ -6,5 +6,5 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Helpers_1 = require("./Helpers"); | ||
class RemoveQuery { | ||
class RemoveQuery extends Helpers_1.ChainBuilderBase { | ||
constructor(Dialect, opts) { | ||
this.Dialect = Dialect; | ||
super(Dialect); | ||
this.opts = opts; | ||
@@ -11,0 +11,0 @@ this.sql = { |
@@ -0,1 +1,2 @@ | ||
import Helpers = require('./Helpers'); | ||
import { FxSqlQueryDialect } from './Typo/Dialect'; | ||
@@ -7,3 +8,3 @@ import { FxSqlQueryChainBuilder } from './Typo/Query-ChainBuilder'; | ||
import { FxSqlQuerySubQuery } from "./Typo/SubQuery"; | ||
export declare class SelectQuery implements FxSqlQueryChainBuilder.ChainBuilder__Select { | ||
export declare class SelectQuery extends Helpers.ChainBuilderBase implements FxSqlQueryChainBuilder.ChainBuilder__Select { | ||
private opts; | ||
@@ -10,0 +11,0 @@ Dialect: FxSqlQueryDialect.Dialect; |
@@ -6,4 +6,5 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Where = require("./Where"); | ||
class SelectQuery { | ||
class SelectQuery extends Helpers.ChainBuilderBase { | ||
constructor(Dialect, opts) { | ||
super(Dialect); | ||
this.opts = opts; | ||
@@ -20,3 +21,2 @@ this.sql = { | ||
this.fun_stack = []; | ||
this.Dialect = Dialect; | ||
} | ||
@@ -100,3 +100,3 @@ get_aggregate_fun(fun) { | ||
from(table, from_id, to_table, to_id, from_opts) { | ||
const [table_name, table_alias] = Helpers.parseTableInputStr(table); | ||
const [table_name, table_alias] = Helpers.parseTableInputStr(table, this.Dialect.type); | ||
const from = { | ||
@@ -110,5 +110,9 @@ table: table_name, | ||
} | ||
let alias; | ||
const args = Array.prototype.slice.call(arguments); | ||
const last = args[args.length - 1]; | ||
if (args.length == 1) { // another table | ||
this.sql.from.push(from); | ||
return this; | ||
} | ||
let alias; | ||
if (typeof last == 'object' && !Array.isArray(last)) { | ||
@@ -122,6 +126,9 @@ from_opts = args.pop(); | ||
} | ||
else { // expect args.length === 4 | ||
else if (args.length === 4) { // expect args.length === 4 | ||
const [to_table_name, to_table_alias] = Helpers.parseTableInputStr(to_table); | ||
alias = to_table_alias || Helpers_1.get_table_alias(this.sql, to_table_name); | ||
} | ||
else { | ||
throw new Error('[SQL-QUERY] invalid arguments for .from()'); | ||
} | ||
from.joins = []; | ||
@@ -258,2 +265,3 @@ if (!from_id.length || !to_id.length) | ||
build() { | ||
var _a; | ||
const having = []; | ||
@@ -268,3 +276,4 @@ const sql_from = this.sql.from; | ||
sql_from[i].alias = Helpers.pickAliasFromFromDescriptor(sql_from[i]) || Helpers.defaultTableAliasNameRule(i + 1); | ||
tableAliasMap[`${sql_from[i].alias}`] = `${sql_from[i].table}`; | ||
const table = sql_from[i].table; | ||
tableAliasMap[`${sql_from[i].alias}`] = Helpers.maybeKnexRawOrQueryBuilder(table) ? table.toQuery() : `${table}`; | ||
} | ||
@@ -303,18 +312,25 @@ const single_query = from_len === 1; | ||
if (sql_from.length > 0) { | ||
if (sql_from.length > 2) { | ||
} | ||
const single_query = sql_from.length == 1 && !this.sql.where_exists; | ||
for (let i = 0, first_table = false; i < sql_from.length; i++) { | ||
const sql_from_item = sql_from[i]; | ||
if (single_query) { | ||
sqlBuilder.from(sql_from_item.table); | ||
if (single_query) { | ||
const isSubQuery = Helpers.isWrapperdSubQuerySelect(sql_from[0].table); | ||
const isKnexQuery = Helpers.maybeKnexRawOrQueryBuilder(sql_from[0].table); | ||
const table = Helpers.convertSqlFromTable(sql_from[0], this.knex, isSubQuery); | ||
if (!isSubQuery && !isKnexQuery) { | ||
sqlBuilder.from(table); | ||
} | ||
else { | ||
if (!first_table) | ||
sqlBuilder.from(`${sql_from_item.table} as ${Helpers.pickAliasFromFromDescriptor(sql_from_item)}`); | ||
else { | ||
const alias = Helpers.pickAliasFromFromDescriptor(sql_from[0]); | ||
sqlBuilder.from(alias ? { [alias]: table } : table); | ||
} | ||
} | ||
else { | ||
const fromAliasDict = {}; | ||
for (let i = 0; i < sql_from.length; i++) { | ||
const sql_from_item = sql_from[i]; | ||
const fallbackAlias = typeof sql_from_item.table == "string" ? sql_from_item.table : `t${i + 1}`; | ||
const alias = Helpers.pickAliasFromFromDescriptor(sql_from_item) || fallbackAlias; | ||
if ((_a = sql_from_item.joins) === null || _a === void 0 ? void 0 : _a.length) { // from describe joins | ||
const join_obj = {}; | ||
const table_str = Helpers.pickAliasFromFromDescriptor(sql_from_item) || sql_from_item.table; | ||
sql_from_item.joins.forEach(join_item => { | ||
join_obj[`${table_str}.${join_item[0]}`] = `${join_item[1]}.${join_item[2]}`; | ||
join_obj[`${alias}.${join_item[0]}`] = `${join_item[1]}.${join_item[2]}`; | ||
}); | ||
@@ -324,6 +340,7 @@ const joinOperator = filterJoinOperator(sql_from_item.opts); | ||
} | ||
else { | ||
fromAliasDict[alias] = Helpers.convertSqlFromTable(sql_from_item, this.knex); | ||
} | ||
} | ||
first_table = true; | ||
if (i > 0) { | ||
} | ||
sqlBuilder.from(fromAliasDict); | ||
} | ||
@@ -330,0 +347,0 @@ } |
@@ -57,6 +57,5 @@ export declare namespace FxSqlQueryComparator { | ||
type QueryComparatorObject<T = any> = { | ||
sql_comparator: { | ||
(): ComparatorNames; | ||
}; | ||
sql_comparator(): ComparatorNames; | ||
val?: T; | ||
asIdentifier?: boolean; | ||
expr?: QueryComparatorExprType; | ||
@@ -107,34 +106,37 @@ from?: string; | ||
interface between { | ||
(a: string, b: string): FxSqlQueryComparator.QueryComparatorObject; | ||
(a: string, b: string, options?: __Options): FxSqlQueryComparator.QueryComparatorObject; | ||
} | ||
interface not_between { | ||
(a: string, b: string): FxSqlQueryComparator.QueryComparatorObject; | ||
(a: string, b: string, options?: __Options): FxSqlQueryComparator.QueryComparatorObject; | ||
} | ||
interface like { | ||
(expr: FxSqlQueryComparator.QueryComparatorExprType): FxSqlQueryComparator.QueryComparatorObject; | ||
(expr: FxSqlQueryComparator.QueryComparatorExprType, options?: __Options): FxSqlQueryComparator.QueryComparatorObject; | ||
} | ||
interface not_like { | ||
(expr: FxSqlQueryComparator.QueryComparatorExprType): FxSqlQueryComparator.QueryComparatorObject; | ||
(expr: FxSqlQueryComparator.QueryComparatorExprType, options?: __Options): FxSqlQueryComparator.QueryComparatorObject; | ||
} | ||
interface eq { | ||
(v: any): FxSqlQueryComparator.QueryComparatorObject; | ||
(v: any, options?: __Options): FxSqlQueryComparator.QueryComparatorObject; | ||
} | ||
interface ne { | ||
(v: any): FxSqlQueryComparator.QueryComparatorObject; | ||
(v: any, options?: __Options): FxSqlQueryComparator.QueryComparatorObject; | ||
} | ||
interface gt { | ||
(v: any): FxSqlQueryComparator.QueryComparatorObject; | ||
(v: any, options?: __Options): FxSqlQueryComparator.QueryComparatorObject; | ||
} | ||
interface gte { | ||
(v: any): FxSqlQueryComparator.QueryComparatorObject; | ||
(v: any, options?: __Options): FxSqlQueryComparator.QueryComparatorObject; | ||
} | ||
interface lt { | ||
(v: any): FxSqlQueryComparator.QueryComparatorObject; | ||
(v: any, options?: __Options): FxSqlQueryComparator.QueryComparatorObject; | ||
} | ||
interface lte { | ||
(v: any): FxSqlQueryComparator.QueryComparatorObject; | ||
(v: any, options?: __Options): FxSqlQueryComparator.QueryComparatorObject; | ||
} | ||
interface not_in { | ||
(v: any): FxSqlQueryComparator.QueryComparatorObject; | ||
(v: any, options?: __Options): FxSqlQueryComparator.QueryComparatorObject; | ||
} | ||
type __Options = { | ||
asIdentifier?: boolean; | ||
}; | ||
} |
@@ -13,2 +13,3 @@ import { FxSqlQuery } from "./Query"; | ||
interface ChainBuilder { | ||
readonly knex: FxSqlQuery.Class_Query['knex']; | ||
build(): string; | ||
@@ -45,3 +46,3 @@ } | ||
where: (...whereConditions: (FxSqlQuerySubQuery.SubQueryBuildDescriptor['wheres'] | FxSqlQuerySubQuery.WhereExistsTuple_Flatten[0])[]) => this; | ||
whereExists: (table: string, table_link: string, link: FxSqlQuerySql.WhereExistsLinkTuple, cond: FxSqlQuerySubQuery.SubQueryBuildDescriptor['wheres']) => this; | ||
whereExists: (table: string, table_link: string | FxSqlQuerySql.SqlFromTableInput, link: FxSqlQuerySql.WhereExistsLinkTuple, cond: FxSqlQuerySubQuery.SubQueryBuildDescriptor['wheres']) => this; | ||
groupBy: (...args: FxSqlQuerySql.SqlGroupByType[]) => this; | ||
@@ -48,0 +49,0 @@ fun: (fun: string, column?: FxSqlQuerySql.SqlColumnType, _as?: string) => this; |
@@ -18,6 +18,5 @@ import { FxSqlAggregation } from "./Aggregation"; | ||
type SqlAssignmentTuple = [FxSqlQuerySql.SqlFragmentStr, [...SqlAssignmentValues]?]; | ||
type SqlTableRaw = string; | ||
type SqlTableAliasRaw = string; | ||
type SqlTableTuple = [string, string]; | ||
type SqlTableInputType = SqlTableRaw | SqlTableAliasRaw | SqlTableTuple; | ||
type SqlFromTableInput = import('@fxjs/knex').Knex.Raw | import('@fxjs/knex').Knex.QueryBuilder; | ||
type SqlTableTuple = [table: string | `(${string})` | SqlFromTableInput, alis: string]; | ||
type SqlTableInputType = string | SqlFromTableInput | SqlTableTuple; | ||
type WhereObj = { | ||
@@ -108,3 +107,3 @@ str: string; | ||
interface QueryFromDescriptor { | ||
table: string; | ||
table: string | import('@fxjs/knex').Knex.Raw | import('@fxjs/knex').Knex.QueryBuilder; | ||
alias: string; | ||
@@ -111,0 +110,0 @@ a?: string; |
@@ -6,4 +6,4 @@ import { FxSqlQuerySql } from "./Typo/Sql"; | ||
import { FxSqlQueryChainBuilder } from "./Typo/Query-ChainBuilder"; | ||
export declare class UpdateQuery implements FxSqlQueryChainBuilder.ChainBuilder__Update { | ||
private Dialect; | ||
import { ChainBuilderBase } from "./Helpers"; | ||
export declare class UpdateQuery extends ChainBuilderBase implements FxSqlQueryChainBuilder.ChainBuilder__Update { | ||
private opts; | ||
@@ -10,0 +10,0 @@ private sql; |
@@ -5,5 +5,6 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Where = require("./Where"); | ||
class UpdateQuery { | ||
const Helpers_1 = require("./Helpers"); | ||
class UpdateQuery extends Helpers_1.ChainBuilderBase { | ||
constructor(Dialect, opts) { | ||
this.Dialect = Dialect; | ||
super(Dialect); | ||
this.opts = opts; | ||
@@ -10,0 +11,0 @@ this.sql = { |
@@ -119,10 +119,10 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
} | ||
if (!isInStyleOperator(op, non_conj_where_conditem_value)) { | ||
innerOperator = innerOperator || selectWhereOperatorByCtx(nextPrefixedOpWord).nextOperator; | ||
knexQueryBuilder[innerOperator].call(knexQueryBuilder, normalizedKey, transformed_result_op, Helpers.escapeValForKnex(non_conj_where_conditem_value.val, Dialect, opts)); | ||
let val = non_conj_where_conditem_value.val; | ||
innerOperator = innerOperator || selectWhereOperatorByCtx(nextPrefixedOpWord).nextOperator; | ||
if (!isInStyleOperator(op, non_conj_where_conditem_value) | ||
&& val !== null | ||
&& non_conj_where_conditem_value.asIdentifier) { | ||
val = Dialect.knex.ref(val); | ||
} | ||
else { | ||
innerOperator = innerOperator || selectWhereOperatorByCtx(nextPrefixedOpWord).nextOperator; | ||
knexQueryBuilder[innerOperator].call(knexQueryBuilder, normalizedKey, transformed_result_op, Helpers.escapeValForKnex(non_conj_where_conditem_value.val, Dialect, opts)); | ||
} | ||
knexQueryBuilder[innerOperator].call(knexQueryBuilder, normalizedKey, transformed_result_op, Helpers.escapeValForKnex(val, Dialect, opts)); | ||
break; | ||
@@ -133,2 +133,6 @@ } | ||
if (isUnderscoreSqlInput(k, non_conj_where_conditem_value)) { | ||
if (!Array.isArray(non_conj_where_conditem_value) | ||
|| (non_conj_where_conditem_value[0] && non_conj_where_conditem_value[0].length < 1)) { | ||
throw new Error(`invalid value for ${k}, expected its 2 dimension array like __sql: [ ['expr with ? or ??', ['value', 'id']], ['SQL'], ... ]`); | ||
} | ||
for (let i = 0; i < non_conj_where_conditem_value.length; i++) { | ||
@@ -135,0 +139,0 @@ knexQueryBuilder.whereRaw(normalizeSqlConditions(Dialect, non_conj_where_conditem_value[i])); |
{ | ||
"name": "@fxjs/sql-query", | ||
"version": "0.9.4", | ||
"version": "0.10.0-alpha.0", | ||
"private": false, | ||
@@ -44,3 +44,3 @@ "description": "", | ||
}, | ||
"gitHead": "41f550dc30bda962c60bfc1b05428c3a7f819b5f" | ||
"gitHead": "9795ef20b3cd6fad2b513da45c7307457ea4650b" | ||
} |
105769
2654