@fxjs/sql-query
Advanced tools
Comparing version
@@ -30,3 +30,3 @@ /// <reference path="Sql.d.ts" /> | ||
escapeId: { | ||
(...els: FxSqlQuerySql.SqlEscapeArgIdType[]): string | ||
(...els: (FxSqlQuerySql.SqlEscapeArgIdType | {str: string, escapes: string[]})[]): string | ||
} | ||
@@ -33,0 +33,0 @@ escapeVal: { |
/// <reference path="Dialect.d.ts" /> | ||
declare namespace FxSqlQueryColumns { | ||
interface FieldItem {} | ||
type SelectInputArgType = string | FxSqlQuerySql.SqlSelectFieldsDescriptor | ||
interface FieldItemHash { | ||
[key: string]: FieldItem | ||
} | ||
interface FieldItemTypeMap { | ||
@@ -11,0 +7,0 @@ [key: string]: FxSqlQueryDialect.DialectFieldType |
declare namespace FxSqlQueryHelpler { | ||
type Arraiable<T> = T | T[] | ||
type BinaryTuple<T1, T2=T1> = [T1, T2] | ||
} |
/// <reference types="fibjs" /> | ||
/// <reference path="Helper.d.ts" /> | ||
/// <reference path="Aggregation.d.ts" /> | ||
/// <reference path="Comparators.d.ts" /> | ||
/// <reference path="Query.d.ts" /> | ||
/// <reference path="Query-Aggregation.d.ts" /> | ||
/// <reference path="Query-Comparators.d.ts" /> | ||
/// <reference path="Query-ChainBuilder.d.ts" /> | ||
@@ -13,6 +13,6 @@ /// <reference path="Sql.d.ts" /> | ||
declare module "@fxjs/sql-query" { | ||
export const comparators: FxSqlQuery.ComparatorHash | ||
export const comparators: FxSqlQueryComparator.ComparatorHash | ||
export const Text: FxSqlQuery.TypedQueryObjectWrapper<'text'> | ||
export type Query = FxSqlQuery.Class_Query | ||
} | ||
export const Query: typeof FxSqlQuery.Class_Query | ||
} |
@@ -8,5 +8,4 @@ /// <reference path="Helper.d.ts" /> | ||
declare namespace FxSqlQuery { | ||
type QueryWhereConditionPayloadUnit = FxSqlQuerySql.DetailedQueryWhereCondition | string | null | ||
type QueryWhereConditionPayloadUnit = FxSqlQuerySql.QueryWhereCondition | string | null | ||
interface ChainBuilderOptions extends QueryOptions {} | ||
@@ -20,3 +19,3 @@ | ||
offset(offset: number): this | ||
limit(limit: number): this | ||
limit(limit: number|string): this | ||
} | ||
@@ -33,12 +32,24 @@ | ||
{ | ||
select: (fields: FxSqlQueryColumns.FieldItemHash) => this | ||
where: (...whereConditions: FxSqlQuerySql.QueryWhereConditionHash[]) => this | ||
whereExists: (table: string, table_link: string, link: string, cond: FxSqlQuerySql.QueryWhereConditionHash) => this | ||
select: (fields: FxSqlQueryColumns.SelectInputArgType[]) => this | ||
/** | ||
* .where('table1', {t1_col1: 'v1'}, 'table2', {t1_col2: 'v2'}, ...) | ||
* .where({__sql: 'xxx'}) | ||
* .where({col1: 'v1', col1: 'v1'}) | ||
* .where(not: [ { col: 2 }, { col: 3 } ]) | ||
*/ | ||
where: (...whereConditions: (FxSqlQuerySubQuery.SubQueryBuildDescriptor['w'] | FxSqlQuerySubQuery.WhereExistsTuple_Flatten[0])[]) => this | ||
whereExists: ( | ||
table: string, | ||
table_link: string, | ||
link: FxSqlQuerySql.WhereExistsLinkTuple, | ||
cond: FxSqlQuerySubQuery.SubQueryBuildDescriptor['w'] | ||
) => this | ||
groupBy: (...args: FxSqlQuerySql.SqlGroupByType[]) => this | ||
fun: (fun: string, column?: FxSqlQuerySql.SqlColumnType, alias?: string) => this | ||
from: ( | ||
table: string, | ||
from_id: FxSqlQueryHelpler.Arraiable<string>, | ||
to_table: string, | ||
to_id: FxSqlQueryHelpler.Arraiable<string>, | ||
fromOpts?: FxSqlQuerySql.QueryFromDescriptorOpts | ||
from_id?: FxSqlQueryHelpler.Arraiable<string>, | ||
to_table?: string, | ||
to_id?: FxSqlQueryHelpler.Arraiable<string>, | ||
from_opts?: FxSqlQuerySql.QueryFromDescriptorOpts | ||
) => this | ||
@@ -50,3 +61,3 @@ | ||
into(table: string): this | ChainBuilder__Insert | ||
set(values: FxSqlQuerySql.ValuesToSet): this | ChainBuilder__Insert | ||
set(values: FxSqlQuerySql.DataToSet): this | ChainBuilder__Insert | ||
} | ||
@@ -59,4 +70,4 @@ | ||
fields: { | ||
(): FxSqlQueryColumns.FieldItemHash | ||
(fields: FxSqlQueryColumns.FieldItemHash): ChainBuilder__Create | ||
(): FxSqlQueryColumns.FieldItemTypeMap | ||
(fields: FxSqlQueryColumns.FieldItemTypeMap): ChainBuilder__Create | ||
} | ||
@@ -66,5 +77,5 @@ } | ||
interface ChainBuilder__Update extends ChainBuilder { | ||
where: (...whereConditions: FxSqlQuerySql.QueryWhereConditionHash[]) => ChainBuilder__Update | ||
where: (...whereConditions: FxSqlQuerySubQuery.SubQueryBuildDescriptor['w'][]) => ChainBuilder__Update | ||
into(table: string): this | ChainBuilder__Update | ||
set(values: FxSqlQuerySql.ValuesToSet): this | ChainBuilder__Update | ||
set(values: FxSqlQuerySql.DataToSet): this | ChainBuilder__Update | ||
} | ||
@@ -76,5 +87,5 @@ | ||
order(column: string, dir: QueryOrderDirection): this | ||
where: (...whereConditions: FxSqlQuerySql.QueryWhereConditionHash[]) => this | ||
where: (...whereConditions: FxSqlQuerySubQuery.SubQueryBuildDescriptor['w'][]) => this | ||
from(table: string): this | ||
} | ||
} |
/// <reference path="Dialect.d.ts" /> | ||
/// <reference path="SubQuery.d.ts" /> | ||
/// <reference path="Query-ChainBuilder.d.ts" /> | ||
@@ -29,3 +30,5 @@ | ||
interface Class_Query { | ||
export class Class_Query { | ||
constructor (_opts?: string | FxSqlQuery.QueryOptions) | ||
escape: FxSqlQueryDialect.Dialect['escape'] | ||
@@ -32,0 +35,0 @@ escapeId: FxSqlQueryDialect.Dialect['escapeId'] |
@@ -0,3 +1,8 @@ | ||
/// <reference path="Comparators.d.ts" /> | ||
declare namespace FxSqlQuerySql { | ||
type ValuesToSet = (string|number)[] | ||
// type ValueToSet = (string|number) | ||
type DataToSet = { | ||
[key: string]: any | ||
} | ||
@@ -18,8 +23,10 @@ type SqlResultStr = string | ||
} | ||
type WherePureEscapableSqlTuple = [SqlAssignmentTuple] | ||
interface QueryWhereCondition { | ||
sql_comparator: { | ||
(): FxSqlQuery.QueryComparatorType | ||
} | ||
// ['f1', 'f2'] ---> (`t1.f1` = `t2.f2`) | ||
type WhereExistsLinkTuple_L1 = FxSqlQueryHelpler.BinaryTuple<string> | ||
// [['f1', 'f2'], ['ff1', 'ff2']] ---> (`t1.f1` = `t2.f2`) AND (`t1.ff1` = `t2.ff2`) | ||
type WhereExistsLinkTuple_L2 = FxSqlQueryHelpler.BinaryTuple<FxSqlQueryHelpler.BinaryTuple<string>> | ||
type WhereExistsLinkTuple = WhereExistsLinkTuple_L1 | WhereExistsLinkTuple_L2 | ||
interface DetailedQueryWhereCondition extends FxSqlQueryComparator.QueryComparatorObject { | ||
// from table name | ||
@@ -29,3 +36,3 @@ from: string | ||
to: string | ||
expr: FxSqlQuery.QueryComparatorExprType | ||
expr: FxSqlQueryComparator.QueryComparatorExprType | ||
val: any | ||
@@ -35,23 +42,10 @@ where: WhereObj | ||
type ListOfQueryWhereConditionItemHash = QueryWhereConditionHash[] | ||
type NonSpecialQueryWhereConditionItem = QueryWhereCondition | WherePureEscapableSqlTuple | ||
interface NonSpecialQueryWhereConditionItemHash { | ||
[k: string]: NonSpecialQueryWhereConditionItem | ||
interface QueryWhereConjunctionHash { | ||
or?: FxSqlQueryComparator.Input[] | ||
and?: FxSqlQueryComparator.Input[] | ||
not_or?: FxSqlQueryComparator.Input[] | ||
not_and?: FxSqlQueryComparator.Input[] | ||
not?: FxSqlQueryComparator.Input[] | ||
} | ||
type ComplextQueryWhereConditionUnit = ListOfQueryWhereConditionItemHash | NonSpecialQueryWhereConditionItem | ||
interface QueryWhereConditionHash { | ||
// infact, those [k] must be `NonSpecialQueryWhereConditionItem`, such as part of NonSpecialQueryWhereConditionItemHash | ||
[k: string]: ComplextQueryWhereConditionUnit | ||
or: ListOfQueryWhereConditionItemHash | ||
and: ListOfQueryWhereConditionItemHash | ||
not_or: ListOfQueryWhereConditionItemHash | ||
not_and: ListOfQueryWhereConditionItemHash | ||
not: ListOfQueryWhereConditionItemHash | ||
__sql: WherePureEscapableSqlTuple | ||
} | ||
type QueryWhereConditionInputArg = QueryWhereConditionHash | string | ||
interface QueryWhereExtendItem { | ||
@@ -73,3 +67,4 @@ // table | ||
type SqlColumnType = SqlColumnDescriptor[] | string[] | string | '*' | ||
type NormalizedSimpleSqlColumnType = string | '*' | ||
type SqlColumnType = SqlColumnDescriptor[] | string[] | NormalizedSimpleSqlColumnType | ||
@@ -80,5 +75,7 @@ interface SqlSelectFieldsDescriptor { | ||
// column name | ||
c: SqlColumnType | ||
c?: SqlColumnType | ||
// table alias | ||
a?: string | ||
alias?: string | ||
// args to describe what columns to select | ||
a?: FxSqlQuerySql.SqlColumnType | ||
// fun_stack | ||
@@ -94,3 +91,3 @@ s?: FxSqlQuery.SupportedAggregationFunction[] | ||
interface SqlSelectFieldsGenerator { | ||
(dialect: FxSqlQueryDialect.Dialect): SqlSelectFieldsDescriptor | ||
(dialect: FxSqlQueryDialect.Dialect): string | ||
} | ||
@@ -116,11 +113,2 @@ type SqlSelectFieldsType = SqlSelectFieldsDescriptor | SqlSelectFieldsGenerator | ||
interface SqlWhereDescriptor { | ||
// table name | ||
t: string | ||
// where conditions | ||
w: QueryWhereConditionHash | ||
// exists query info | ||
e?: QueryWhereExtendItem | ||
} | ||
interface SqlOrderDescriptor { | ||
@@ -137,3 +125,3 @@ c: any | ||
type SqlQueryDescriptorWhereItem = SqlWhereDescriptor | string | ||
// type SqlQueryDescriptorWhereItem = SqlWhereDescriptor | string | ||
@@ -144,4 +132,4 @@ interface SqlQueryChainDescriptor { | ||
// values to set in UPDATE like command | ||
set?: ValuesToSet | ||
where?: SqlQueryDescriptorWhereItem[] | ||
set?: DataToSet | ||
where?: FxSqlQuerySubQuery.SubQueryBuildDescriptor[] | ||
order?: SqlOrderPayloadType[] | ||
@@ -148,0 +136,0 @@ offset?: number |
v0.0.3 / 2019-01-05 | ||
v0.1.0 / 2019-01-08 | ||
================== | ||
* build next version typo. | ||
* typo fix. | ||
* update typo. | ||
* typo fix. | ||
v0.0.3 / 2019-01-05 | ||
=================== | ||
* Release v0.0.3 | ||
* update typo. | ||
* update .npmignore. | ||
@@ -7,0 +16,0 @@ |
@@ -135,2 +135,15 @@ /// <reference path="../@types/index.d.ts" /> | ||
if (typeof whereConditions[i] == "string") { | ||
/** | ||
* deal with input like this: | ||
* [ | ||
"table1", | ||
{ | ||
"col": 1 | ||
}, | ||
"table2", | ||
{ | ||
"col": 2 | ||
} | ||
] | ||
*/ | ||
const cond_str = whereConditions[i]; | ||
@@ -195,3 +208,3 @@ if (whereItem !== null) { | ||
build() { | ||
var query = [], tmp = [], from, j, ord, str; | ||
var query = [], tmp = [], _from, ord, str; | ||
var having = []; | ||
@@ -317,6 +330,6 @@ if (this.fun_stack.length) { | ||
for (let i = 0; i < sql_from.length; i++) { | ||
from = sql_from[i]; | ||
_from = sql_from[i]; | ||
if (i > 0) { | ||
if (from.opts && from.opts.joinType) { | ||
query.push(from.opts.joinType.toUpperCase()); | ||
if (_from.opts && _from.opts.joinType) { | ||
query.push(_from.opts.joinType.toUpperCase()); | ||
} | ||
@@ -326,16 +339,16 @@ query.push("JOIN"); | ||
if (sql_from.length == 1 && !this.sql.where_exists) { | ||
query.push(this.Dialect.escapeId(from.t)); | ||
query.push(this.Dialect.escapeId(_from.t)); | ||
} | ||
else { | ||
query.push(this.Dialect.escapeId(from.t) + " " + this.Dialect.escapeId(from.a)); | ||
query.push(this.Dialect.escapeId(_from.t) + " " + this.Dialect.escapeId(_from.a)); | ||
} | ||
if (i > 0) { | ||
query.push("ON"); | ||
for (let ii = 0; ii < from.j.length; ii++) { | ||
for (let ii = 0; ii < _from.j.length; ii++) { | ||
if (ii > 0) { | ||
query.push("AND"); | ||
} | ||
query.push(this.Dialect.escapeId(from.a, from.j[ii][0]) + | ||
query.push(this.Dialect.escapeId(_from.a, _from.j[ii][0]) + | ||
" = " + | ||
this.Dialect.escapeId(from.j[ii][1], from.j[ii][2])); | ||
this.Dialect.escapeId(_from.j[ii][1], _from.j[ii][2])); | ||
} | ||
@@ -354,3 +367,3 @@ if (i < sql_from.length - 1) { | ||
query = query.concat(Where.build(this.Dialect, | ||
// at this time, this.sql.where has been normalized, it must be `FxSqlQuerySql.SqlWhereDescriptor[]` | ||
// at this time, this.sql.where has been normalized, it must be `FxSqlQuerySubQuery.SubQueryBuildDescriptor[]` | ||
this.sql.where, this.opts)); | ||
@@ -357,0 +370,0 @@ if (this.sql.group_by !== null) { |
/// <reference path="../@types/index.d.ts" /> | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Set = require("./Set"); | ||
var Where = require("./Where"); | ||
const UpdateSet = require("./Set"); | ||
const Where = require("./Where"); | ||
class UpdateQuery { | ||
@@ -34,3 +34,3 @@ constructor(Dialect, opts) { | ||
query.push(this.Dialect.escapeId(this.sql.table)); | ||
query = query.concat(Set.build(this.Dialect, this.sql.set, this.opts)); | ||
query = query.concat(UpdateSet.build(this.Dialect, this.sql.set, this.opts)); | ||
query = query.concat(Where.build(this.Dialect, this.sql.where, this.opts)); | ||
@@ -37,0 +37,0 @@ return query.join(" "); |
@@ -25,2 +25,3 @@ /// <reference path="../@types/index.d.ts" /> | ||
; | ||
const where_conjunctions = ["or", "and", "not_or", "not_and", "not"]; | ||
function buildOrGroup(Dialect, where, opts) { | ||
@@ -60,10 +61,11 @@ opts = opts || {}; | ||
} | ||
// not is an alias for not_and | ||
if (["or", "and", "not_or", "not_and", "not"].indexOf(k) >= 0) { | ||
// `not` is an alias for `not_and` | ||
if (where_conjunctions.indexOf(k) >= 0) { | ||
var q, subquery = []; | ||
var prefix = (k == "not" || k.indexOf("_") >= 0 ? "NOT " : false); | ||
op = (k == "not" ? "and" : (k.indexOf("_") >= 0 ? k.substr(4) : k)).toUpperCase(); | ||
const special_kv = where.w[k]; | ||
for (var j = 0; j < special_kv.length; j++) { | ||
q = buildOrGroup(Dialect, { t: where.t, w: special_kv[j] }, opts); | ||
const conj_cond_item = where.w[k]; | ||
for (var j = 0; j < conj_cond_item.length; j++) { | ||
const conj_c = conj_cond_item[j]; | ||
q = buildOrGroup(Dialect, { t: where.t, w: conj_c }, opts); | ||
if (q !== false) { | ||
@@ -79,4 +81,8 @@ subquery.push(q); | ||
const non_special_kv = where.w[k]; | ||
if (typeof non_special_kv.sql_comparator == "function") { | ||
op = non_special_kv.sql_comparator(); | ||
if ( | ||
/* non_special_kv could be string, it's international */ | ||
typeof non_special_kv.sql_comparator == "function") { | ||
const query_comparator_obj = non_special_kv; | ||
op = query_comparator_obj.sql_comparator(); | ||
const normalized_cond = query_comparator_obj; | ||
switch (op) { | ||
@@ -86,5 +92,5 @@ case "between": | ||
" BETWEEN " + | ||
Dialect.escapeVal(non_special_kv.from, opts.timezone) + | ||
Dialect.escapeVal(normalized_cond.from, opts.timezone) + | ||
" AND " + | ||
Dialect.escapeVal(non_special_kv.to, opts.timezone)); | ||
Dialect.escapeVal(normalized_cond.to, opts.timezone)); | ||
break; | ||
@@ -94,5 +100,5 @@ case "not_between": | ||
" NOT BETWEEN " + | ||
Dialect.escapeVal(non_special_kv.from, opts.timezone) + | ||
Dialect.escapeVal(normalized_cond.from, opts.timezone) + | ||
" AND " + | ||
Dialect.escapeVal(non_special_kv.to, opts.timezone)); | ||
Dialect.escapeVal(normalized_cond.to, opts.timezone)); | ||
break; | ||
@@ -102,3 +108,3 @@ case "like": | ||
" LIKE " + | ||
Dialect.escapeVal(non_special_kv.expr, opts.timezone)); | ||
Dialect.escapeVal(normalized_cond.expr, opts.timezone)); | ||
break; | ||
@@ -108,3 +114,3 @@ case "not_like": | ||
" NOT LIKE " + | ||
Dialect.escapeVal(non_special_kv.expr, opts.timezone)); | ||
Dialect.escapeVal(normalized_cond.expr, opts.timezone)); | ||
break; | ||
@@ -120,6 +126,6 @@ case "eq": | ||
case "eq": | ||
op = (non_special_kv.val === null ? "IS" : "="); | ||
op = (normalized_cond.val === null ? "IS" : "="); | ||
break; | ||
case "ne": | ||
op = (non_special_kv.val === null ? "IS NOT" : "<>"); | ||
op = (normalized_cond.val === null ? "IS NOT" : "<>"); | ||
break; | ||
@@ -144,16 +150,16 @@ case "gt": | ||
" " + op + " " + | ||
Dialect.escapeVal(non_special_kv.val, opts.timezone)); | ||
Dialect.escapeVal(normalized_cond.val, opts.timezone)); | ||
break; | ||
case "sql": | ||
if (typeof non_special_kv.where == "object") { | ||
var sql = non_special_kv.where.str.replace("?:column", buildComparisonKey(Dialect, where.t, k)); | ||
if (typeof normalized_cond.where == "object") { | ||
var sql = normalized_cond.where.str.replace("?:column", buildComparisonKey(Dialect, where.t, k)); | ||
sql = sql.replace(/\?:(id|value)/g, function (m) { | ||
if (non_special_kv.where.escapes.length === 0) { | ||
if (normalized_cond.where.escapes.length === 0) { | ||
return ''; | ||
} | ||
if (m == "?:id") { | ||
return Dialect.escapeId(non_special_kv.where.escapes.shift()); | ||
return Dialect.escapeId(normalized_cond.where.escapes.shift()); | ||
} | ||
// ?:value | ||
return Dialect.escapeVal(non_special_kv.where.escapes.shift(), opts.timezone); | ||
return Dialect.escapeVal(normalized_cond.where.escapes.shift(), opts.timezone); | ||
}); | ||
@@ -168,3 +174,3 @@ query.push(sql); | ||
const puresql_kv = where.w[k]; | ||
for (var a = 0; a < where.w[k].length; a++) { | ||
for (var a = 0; a < puresql_kv.length; a++) { | ||
query.push(normalizeSqlConditions(Dialect, puresql_kv[a])); | ||
@@ -171,0 +177,0 @@ } |
{ | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"name": "@fxjs/sql-query", | ||
@@ -19,3 +19,3 @@ "types": "@types/index.d.ts", | ||
"ci": "npm run build && npm run test", | ||
"prepublishOnly": "npm run build" | ||
"prepublishOnly": "npm run build" | ||
}, | ||
@@ -22,0 +22,0 @@ "main": "./lib/Query", |
92399
4.6%32
3.23%1916
5.45%