@riao/dbal 2.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/builder/sql-builder.d.ts +1 -9
- package/builder/sql-builder.js +5 -43
- package/builder/sql-builder.js.map +1 -1
- package/builder/statement-builder.d.ts +1 -0
- package/builder/statement-builder.js +5 -0
- package/builder/statement-builder.js.map +1 -1
- package/comparison/comparison-aliases.d.ts +10 -0
- package/comparison/comparison-aliases.js +41 -0
- package/comparison/comparison-aliases.js.map +1 -0
- package/comparison/comparison-token.d.ts +17 -0
- package/comparison/comparison-token.js +16 -0
- package/comparison/comparison-token.js.map +1 -0
- package/comparison/comparison.d.ts +13 -0
- package/comparison/comparison.js +72 -0
- package/comparison/comparison.js.map +1 -0
- package/comparison/index.d.ts +4 -0
- package/comparison/index.js +25 -0
- package/comparison/index.js.map +1 -0
- package/comparison/is-comparison-token.d.ts +2 -0
- package/comparison/is-comparison-token.js +9 -0
- package/comparison/is-comparison-token.js.map +1 -0
- package/ddl/ddl-builder.d.ts +3 -0
- package/ddl/ddl-builder.js +10 -3
- package/ddl/ddl-builder.js.map +1 -1
- package/ddl/ddl-repository.js +15 -14
- package/ddl/ddl-repository.js.map +1 -1
- package/dml/delete.d.ts +2 -2
- package/dml/group-by.d.ts +4 -0
- package/dml/group-by.js +3 -0
- package/dml/group-by.js.map +1 -0
- package/dml/index.d.ts +2 -1
- package/dml/index.js +2 -1
- package/dml/index.js.map +1 -1
- package/dml/insert.d.ts +6 -4
- package/dml/join.d.ts +3 -3
- package/dml/query-builder.d.ts +37 -21
- package/dml/query-builder.js +197 -111
- package/dml/query-builder.js.map +1 -1
- package/dml/query-repository.js +12 -7
- package/dml/query-repository.js.map +1 -1
- package/dml/select.d.ts +7 -6
- package/dml/subquery.d.ts +5 -0
- package/dml/subquery.js +11 -0
- package/dml/subquery.js.map +1 -0
- package/dml/update.d.ts +2 -2
- package/expression/expression-token.d.ts +9 -0
- package/expression/expression-token.js +11 -0
- package/expression/expression-token.js.map +1 -0
- package/expression/expression.d.ts +6 -0
- package/expression/expression.js +3 -0
- package/expression/expression.js.map +1 -0
- package/expression/identifier.d.ts +7 -0
- package/expression/identifier.js +16 -0
- package/expression/identifier.js.map +1 -0
- package/expression/index.d.ts +5 -0
- package/{conditions → expression}/index.js +7 -6
- package/expression/index.js.map +1 -0
- package/expression/is-expression-token.d.ts +2 -0
- package/expression/is-expression-token.js +8 -0
- package/expression/is-expression-token.js.map +1 -0
- package/expression/key-val-expression.d.ts +7 -0
- package/expression/key-val-expression.js +3 -0
- package/expression/key-val-expression.js.map +1 -0
- package/expression/literal.d.ts +2 -0
- package/{dml/where.js → expression/literal.js} +1 -1
- package/expression/literal.js.map +1 -0
- package/expression/logical.d.ts +18 -0
- package/expression/logical.js +31 -0
- package/expression/logical.js.map +1 -0
- package/expression/simple-expression.d.ts +5 -0
- package/expression/simple-expression.js +3 -0
- package/expression/simple-expression.js.map +1 -0
- package/functions/function-token.d.ts +6 -4
- package/functions/function-token.js.map +1 -1
- package/functions/functions.js +5 -2
- package/functions/functions.js.map +1 -1
- package/functions/is-database-function.d.ts +3 -2
- package/functions/is-database-function.js +4 -3
- package/functions/is-database-function.js.map +1 -1
- package/index.d.ts +2 -1
- package/index.js +2 -1
- package/index.js.map +1 -1
- package/package.json +1 -1
- package/repository/repository.d.ts +12 -1
- package/repository/repository.js +34 -1
- package/repository/repository.js.map +1 -1
- package/seed/auto-seed.js +4 -4
- package/seed/auto-seed.js.map +1 -1
- package/tokens/index.d.ts +1 -1
- package/tokens/index.js +3 -15
- package/tokens/index.js.map +1 -1
- package/conditions/condition-aliases.d.ts +0 -12
- package/conditions/condition-aliases.js +0 -43
- package/conditions/condition-aliases.js.map +0 -1
- package/conditions/condition-token.d.ts +0 -18
- package/conditions/condition-token.js +0 -16
- package/conditions/condition-token.js.map +0 -1
- package/conditions/conditions.d.ts +0 -14
- package/conditions/conditions.js +0 -68
- package/conditions/conditions.js.map +0 -1
- package/conditions/index.d.ts +0 -4
- package/conditions/index.js.map +0 -1
- package/conditions/is-condition.d.ts +0 -1
- package/conditions/is-condition.js +0 -8
- package/conditions/is-condition.js.map +0 -1
- package/dml/where.d.ts +0 -8
- package/dml/where.js.map +0 -1
- package/tokens/column-name-token.d.ts +0 -4
- package/tokens/column-name-token.js +0 -8
- package/tokens/column-name-token.js.map +0 -1
package/dml/group-by.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"group-by.js","sourceRoot":"","sources":["../../../src/dml/group-by.ts"],"names":[],"mappings":""}
|
package/dml/index.d.ts
CHANGED
|
@@ -3,7 +3,8 @@ export { QueryRepository, QueryRepositoryOptions } from './query-repository';
|
|
|
3
3
|
export * from './delete';
|
|
4
4
|
export * from './insert';
|
|
5
5
|
export * from './join';
|
|
6
|
+
export * from './group-by';
|
|
6
7
|
export * from './order-by';
|
|
7
8
|
export * from './select';
|
|
9
|
+
export * from './subquery';
|
|
8
10
|
export * from './update';
|
|
9
|
-
export * from './where';
|
package/dml/index.js
CHANGED
|
@@ -22,8 +22,9 @@ Object.defineProperty(exports, "QueryRepository", { enumerable: true, get: funct
|
|
|
22
22
|
__exportStar(require("./delete"), exports);
|
|
23
23
|
__exportStar(require("./insert"), exports);
|
|
24
24
|
__exportStar(require("./join"), exports);
|
|
25
|
+
__exportStar(require("./group-by"), exports);
|
|
25
26
|
__exportStar(require("./order-by"), exports);
|
|
26
27
|
__exportStar(require("./select"), exports);
|
|
28
|
+
__exportStar(require("./subquery"), exports);
|
|
27
29
|
__exportStar(require("./update"), exports);
|
|
28
|
-
__exportStar(require("./where"), exports);
|
|
29
30
|
//# sourceMappingURL=index.js.map
|
package/dml/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/dml/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,iDAAuD;AAA9C,qHAAA,oBAAoB,OAAA;AAC7B,uDAA6E;AAApE,mHAAA,eAAe,OAAA;AAExB,2CAAyB;AACzB,2CAAyB;AACzB,yCAAuB;AACvB,6CAA2B;AAC3B,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/dml/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,iDAAuD;AAA9C,qHAAA,oBAAoB,OAAA;AAC7B,uDAA6E;AAApE,mHAAA,eAAe,OAAA;AAExB,2CAAyB;AACzB,2CAAyB;AACzB,yCAAuB;AACvB,6CAA2B;AAC3B,6CAA2B;AAC3B,2CAAyB;AACzB,6CAA2B;AAC3B,2CAAyB"}
|
package/dml/insert.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { DatabaseRecord } from '../record';
|
|
2
|
-
export interface
|
|
2
|
+
export interface BaseInsertOptions<T extends DatabaseRecord = DatabaseRecord> {
|
|
3
3
|
table?: string;
|
|
4
|
-
records: Partial<T> | Partial<T>[];
|
|
5
4
|
ifNotExists?: boolean;
|
|
6
5
|
onDuplicateKeyUpdate?: Partial<T>;
|
|
7
6
|
primaryKey?: string;
|
|
8
7
|
}
|
|
9
|
-
export interface
|
|
10
|
-
records: Partial<T
|
|
8
|
+
export interface InsertOptions<T extends DatabaseRecord = DatabaseRecord> extends BaseInsertOptions<T> {
|
|
9
|
+
records: Partial<T> | Partial<T>[];
|
|
10
|
+
}
|
|
11
|
+
export interface InsertOneOptions<T extends DatabaseRecord = DatabaseRecord> extends BaseInsertOptions<T> {
|
|
12
|
+
record: Partial<T>;
|
|
11
13
|
ignoreReturnId?: boolean;
|
|
12
14
|
}
|
package/dml/join.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Expression } from '../expression';
|
|
2
2
|
export interface BaseJoin {
|
|
3
3
|
table: string;
|
|
4
4
|
alias?: string;
|
|
5
5
|
}
|
|
6
6
|
export interface InnerCrossJoin extends BaseJoin {
|
|
7
7
|
type?: 'INNER' | 'CROSS';
|
|
8
|
-
on?:
|
|
8
|
+
on?: Expression;
|
|
9
9
|
}
|
|
10
10
|
export interface LeftRightJoin extends BaseJoin {
|
|
11
11
|
type?: 'LEFT' | 'RIGHT';
|
|
12
12
|
outer?: boolean;
|
|
13
|
-
on:
|
|
13
|
+
on: Expression;
|
|
14
14
|
}
|
|
15
15
|
export type Join = InnerCrossJoin | LeftRightJoin;
|
package/dml/query-builder.d.ts
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import { InsertOptions } from './insert';
|
|
2
|
-
import { Where } from './where';
|
|
3
2
|
import { SelectColumn, SelectQuery } from './select';
|
|
4
3
|
import { UpdateOptions } from './update';
|
|
5
4
|
import { DeleteOptions } from './delete';
|
|
6
5
|
import { StatementBuilder } from '../builder/statement-builder';
|
|
6
|
+
import { GroupBy } from './group-by';
|
|
7
7
|
import { OrderBy } from './order-by';
|
|
8
8
|
import { Join } from './join';
|
|
9
|
-
import {
|
|
9
|
+
import { ComparisonToken } from '../comparison';
|
|
10
|
+
import { Expression, LogicalToken } from '../expression';
|
|
11
|
+
import { DatabaseFunction } from '../functions/function-token';
|
|
12
|
+
import { Subquery } from './subquery';
|
|
13
|
+
import { KeyValExpression } from '../expression/key-val-expression';
|
|
10
14
|
export declare class DatabaseQueryBuilder extends StatementBuilder {
|
|
15
|
+
expression(expr: Expression | LogicalToken): void;
|
|
16
|
+
subquery(subquery: Subquery): void;
|
|
17
|
+
keyValueExpression(kv: KeyValExpression): this;
|
|
11
18
|
selectColumn(column: SelectColumn): this;
|
|
12
19
|
selectColumnList(columns?: SelectColumn[]): this;
|
|
13
20
|
selectFrom(from: string): this;
|
|
@@ -15,42 +22,51 @@ export declare class DatabaseQueryBuilder extends StatementBuilder {
|
|
|
15
22
|
selectStatement(): this;
|
|
16
23
|
distinctStatement(): this;
|
|
17
24
|
selectTop(limit: number): this;
|
|
18
|
-
|
|
19
|
-
like(value:
|
|
20
|
-
lt(value:
|
|
21
|
-
lte(value:
|
|
22
|
-
gt(value:
|
|
23
|
-
gte(value:
|
|
24
|
-
notEqual(value:
|
|
25
|
-
and(): void;
|
|
26
|
-
or(): void;
|
|
25
|
+
equal(value: Expression): void;
|
|
26
|
+
like(value: Expression): void;
|
|
27
|
+
lt(value: Expression): void;
|
|
28
|
+
lte(value: Expression): void;
|
|
29
|
+
gt(value: Expression): void;
|
|
30
|
+
gte(value: Expression): void;
|
|
31
|
+
notEqual(value: Expression): void;
|
|
27
32
|
isNull(): void;
|
|
28
|
-
inArray(values:
|
|
29
|
-
between(a:
|
|
33
|
+
inArray(values: Expression[]): void;
|
|
34
|
+
between(a: Expression, b: Expression): void;
|
|
30
35
|
notNull(): void;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
36
|
+
buildComparisonToken(condition: ComparisonToken): this;
|
|
37
|
+
and(): void;
|
|
38
|
+
or(): void;
|
|
39
|
+
not(value: Expression): void;
|
|
40
|
+
logical(token: LogicalToken): void;
|
|
41
|
+
where(where: Expression): this;
|
|
35
42
|
limit(nRecords: number): this;
|
|
43
|
+
groupBy(by: GroupBy): this;
|
|
36
44
|
orderBy(by: OrderBy): void;
|
|
37
45
|
select(query: SelectQuery): this;
|
|
38
46
|
join(join: Join): void;
|
|
39
47
|
insertIntoStatement(table: string): this;
|
|
40
|
-
insertColumnNames(record: Record<string,
|
|
41
|
-
insertOnDuplicateKeyUpdate(assignment: Record<string,
|
|
48
|
+
insertColumnNames(record: Record<string, Expression>): this;
|
|
49
|
+
insertOnDuplicateKeyUpdate(assignment: Record<string, Expression>): this;
|
|
42
50
|
insertIfNotExists(): this;
|
|
43
51
|
insertOutput(primaryKey: string): this;
|
|
44
52
|
insertReturning(primaryKey: string): this;
|
|
45
53
|
insert(options: InsertOptions): this;
|
|
46
54
|
updateStatement(table: string): this;
|
|
47
55
|
updateKeyValues(values: {
|
|
48
|
-
[key: string]:
|
|
56
|
+
[key: string]: Expression;
|
|
49
57
|
}): this;
|
|
50
58
|
updateSetStatement(values: {
|
|
51
|
-
[key: string]:
|
|
59
|
+
[key: string]: Expression;
|
|
52
60
|
}): this;
|
|
53
61
|
update(options: UpdateOptions): this;
|
|
54
62
|
deleteStatement(table: string): this;
|
|
55
63
|
delete(options: DeleteOptions): this;
|
|
64
|
+
/**
|
|
65
|
+
* Append a database function the query
|
|
66
|
+
*
|
|
67
|
+
* @param fn Database function token
|
|
68
|
+
*/
|
|
69
|
+
databaseFunction(fn: DatabaseFunction): this;
|
|
70
|
+
count(fn: DatabaseFunction): this;
|
|
71
|
+
currentTimestamp(fn: DatabaseFunction): this;
|
|
56
72
|
}
|
package/dml/query-builder.js
CHANGED
|
@@ -2,11 +2,93 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DatabaseQueryBuilder = void 0;
|
|
4
4
|
const statement_builder_1 = require("../builder/statement-builder");
|
|
5
|
-
const
|
|
6
|
-
const conditions_1 = require("../conditions");
|
|
5
|
+
const comparison_1 = require("../comparison");
|
|
7
6
|
const tokens_1 = require("../tokens");
|
|
8
7
|
const functions_1 = require("../functions");
|
|
8
|
+
const expression_1 = require("../expression");
|
|
9
|
+
const function_token_1 = require("../functions/function-token");
|
|
10
|
+
const subquery_1 = require("./subquery");
|
|
9
11
|
class DatabaseQueryBuilder extends statement_builder_1.StatementBuilder {
|
|
12
|
+
// ------------------------------------------------------------------------
|
|
13
|
+
// Expression
|
|
14
|
+
// ------------------------------------------------------------------------
|
|
15
|
+
expression(expr) {
|
|
16
|
+
if (Array.isArray(expr)) {
|
|
17
|
+
for (const e of expr) {
|
|
18
|
+
this.expression(e);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
else if ((0, expression_1.isExpressionToken)(expr)) {
|
|
22
|
+
const token = expr;
|
|
23
|
+
if ((0, comparison_1.isComparisonToken)(token)) {
|
|
24
|
+
this.buildComparisonToken(token);
|
|
25
|
+
}
|
|
26
|
+
else if ((0, expression_1.isLogicalToken)(token)) {
|
|
27
|
+
this.logical(token);
|
|
28
|
+
}
|
|
29
|
+
else if ((0, expression_1.isIdentifierToken)(token)) {
|
|
30
|
+
this.sql.columnName(token.name);
|
|
31
|
+
}
|
|
32
|
+
else if ((0, functions_1.isDatabaseFunction)(token)) {
|
|
33
|
+
this.databaseFunction(token);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else if (expr && typeof expr === 'object') {
|
|
37
|
+
if (expr instanceof Buffer || expr instanceof Date) {
|
|
38
|
+
this.sql.placeholder(expr);
|
|
39
|
+
}
|
|
40
|
+
else if (expr instanceof subquery_1.Subquery) {
|
|
41
|
+
this.subquery(expr);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
this.keyValueExpression(expr);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
this.sql.placeholder(expr);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
subquery(subquery) {
|
|
52
|
+
this.sql.openParens();
|
|
53
|
+
this.select(subquery.query);
|
|
54
|
+
this.sql.closeParens();
|
|
55
|
+
this.sql.trimEnd(' ');
|
|
56
|
+
}
|
|
57
|
+
keyValueExpression(kv) {
|
|
58
|
+
if (!Object.keys(kv).length) {
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
this.sql.openParens();
|
|
62
|
+
for (const key in kv) {
|
|
63
|
+
const value = kv[key];
|
|
64
|
+
this.sql.columnName(key);
|
|
65
|
+
this.sql.space();
|
|
66
|
+
if (value === null) {
|
|
67
|
+
this.isNull();
|
|
68
|
+
}
|
|
69
|
+
else if ((0, expression_1.isExpressionToken)(value)) {
|
|
70
|
+
const token = value;
|
|
71
|
+
if ((0, comparison_1.isComparisonToken)(token)) {
|
|
72
|
+
this.expression(value);
|
|
73
|
+
}
|
|
74
|
+
else if ((0, expression_1.isLogicalToken)(token)) {
|
|
75
|
+
this.expression(value);
|
|
76
|
+
}
|
|
77
|
+
else if ((0, expression_1.isIdentifierToken)(token)) {
|
|
78
|
+
this.equal(value);
|
|
79
|
+
}
|
|
80
|
+
else if ((0, functions_1.isDatabaseFunction)(token)) {
|
|
81
|
+
this.equal(value);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
this.equal(value);
|
|
86
|
+
}
|
|
87
|
+
this.and();
|
|
88
|
+
}
|
|
89
|
+
this.sql.trimEnd(this.sql.operators.and);
|
|
90
|
+
this.sql.closeParens();
|
|
91
|
+
}
|
|
10
92
|
// ------------------------------------------------------------------------
|
|
11
93
|
// Select
|
|
12
94
|
// ------------------------------------------------------------------------
|
|
@@ -19,15 +101,7 @@ class DatabaseQueryBuilder extends statement_builder_1.StatementBuilder {
|
|
|
19
101
|
this.sql.columnName(column.column);
|
|
20
102
|
}
|
|
21
103
|
else if ('query' in column) {
|
|
22
|
-
|
|
23
|
-
this.sql.databaseFunction(column.query);
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
this.sql.openParens();
|
|
27
|
-
this.select(column.query);
|
|
28
|
-
this.sql.closeParens();
|
|
29
|
-
this.sql.trimEnd(' ');
|
|
30
|
-
}
|
|
104
|
+
this.expression(column.query);
|
|
31
105
|
}
|
|
32
106
|
if (column.as) {
|
|
33
107
|
this.sql.append(' AS ');
|
|
@@ -75,41 +149,35 @@ class DatabaseQueryBuilder extends statement_builder_1.StatementBuilder {
|
|
|
75
149
|
return this;
|
|
76
150
|
}
|
|
77
151
|
// ------------------------------------------------------------------------
|
|
78
|
-
//
|
|
152
|
+
// Comparisons
|
|
79
153
|
// ------------------------------------------------------------------------
|
|
80
|
-
|
|
154
|
+
equal(value) {
|
|
81
155
|
this.sql.append(this.sql.operators.equals + ' ');
|
|
82
|
-
this.
|
|
156
|
+
this.expression(value);
|
|
83
157
|
}
|
|
84
158
|
like(value) {
|
|
85
159
|
this.sql.append(this.sql.operators.like + ' ');
|
|
86
|
-
this.
|
|
160
|
+
this.expression(value);
|
|
87
161
|
}
|
|
88
162
|
lt(value) {
|
|
89
163
|
this.sql.append(this.sql.operators.lt + ' ');
|
|
90
|
-
this.
|
|
164
|
+
this.expression(value);
|
|
91
165
|
}
|
|
92
166
|
lte(value) {
|
|
93
167
|
this.sql.append(this.sql.operators.lte + ' ');
|
|
94
|
-
this.
|
|
168
|
+
this.expression(value);
|
|
95
169
|
}
|
|
96
170
|
gt(value) {
|
|
97
171
|
this.sql.append(this.sql.operators.gt + ' ');
|
|
98
|
-
this.
|
|
172
|
+
this.expression(value);
|
|
99
173
|
}
|
|
100
174
|
gte(value) {
|
|
101
175
|
this.sql.append(this.sql.operators.gte + ' ');
|
|
102
|
-
this.
|
|
176
|
+
this.expression(value);
|
|
103
177
|
}
|
|
104
178
|
notEqual(value) {
|
|
105
179
|
this.sql.append(this.sql.operators.notEqual + ' ');
|
|
106
|
-
this.
|
|
107
|
-
}
|
|
108
|
-
and() {
|
|
109
|
-
this.sql.append(this.sql.operators.and + ' ');
|
|
110
|
-
}
|
|
111
|
-
or() {
|
|
112
|
-
this.sql.append(this.sql.operators.or + ' ');
|
|
180
|
+
this.expression(value);
|
|
113
181
|
}
|
|
114
182
|
isNull() {
|
|
115
183
|
this.sql.append(this.sql.operators.is + ' ' + this.sql.operators.null + ' ');
|
|
@@ -118,7 +186,7 @@ class DatabaseQueryBuilder extends statement_builder_1.StatementBuilder {
|
|
|
118
186
|
this.sql.append(this.sql.operators.in + ' ');
|
|
119
187
|
this.sql.openParens();
|
|
120
188
|
for (const value of values) {
|
|
121
|
-
this.
|
|
189
|
+
this.expression(value);
|
|
122
190
|
this.sql.append(', ');
|
|
123
191
|
}
|
|
124
192
|
this.sql.trimEnd(', ');
|
|
@@ -127,126 +195,114 @@ class DatabaseQueryBuilder extends statement_builder_1.StatementBuilder {
|
|
|
127
195
|
between(a, b) {
|
|
128
196
|
this.sql.append(this.sql.operators.between + ' ');
|
|
129
197
|
this.sql.openParens();
|
|
130
|
-
this.
|
|
198
|
+
this.expression(a);
|
|
131
199
|
this.sql.append(', ');
|
|
132
|
-
this.
|
|
200
|
+
this.expression(b);
|
|
133
201
|
this.sql.closeParens();
|
|
134
202
|
}
|
|
135
203
|
notNull() {
|
|
136
204
|
this.sql.append('NOT NULL ');
|
|
137
205
|
}
|
|
138
|
-
|
|
139
|
-
if (
|
|
140
|
-
this.
|
|
141
|
-
this.notNull();
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
const isConditionToken = (0, conditions_1.isCondition)(value);
|
|
145
|
-
const conditionType = isConditionToken
|
|
146
|
-
? value === null || value === void 0 ? void 0 : value.riao_condition
|
|
147
|
-
: null;
|
|
148
|
-
if (isConditionToken &&
|
|
149
|
-
(conditionType === condition_token_1.ConditionTokenType.LIKE ||
|
|
150
|
-
conditionType === condition_token_1.ConditionTokenType.IN_ARRAY)) {
|
|
151
|
-
this.sql.append('NOT ');
|
|
152
|
-
this.buildConditionToken(value);
|
|
153
|
-
}
|
|
154
|
-
else if (isConditionToken) {
|
|
155
|
-
throw new Error('Cannot use not() with ' + conditionType);
|
|
156
|
-
}
|
|
157
|
-
else if (typeof value === 'object' || Array.isArray(value)) {
|
|
158
|
-
this.sql.append('!');
|
|
159
|
-
this.whereClause(value);
|
|
160
|
-
}
|
|
161
|
-
else {
|
|
162
|
-
this.notEqual(value);
|
|
206
|
+
buildComparisonToken(condition) {
|
|
207
|
+
if (condition.op === comparison_1.ComparisonOperator.EQUALS) {
|
|
208
|
+
this.equal(condition.value);
|
|
163
209
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
if (condition.riao_condition === condition_token_1.ConditionTokenType.EQUALS) {
|
|
167
|
-
this.equals(condition.value);
|
|
210
|
+
else if (condition.op === comparison_1.ComparisonOperator.NOT_EQUAL) {
|
|
211
|
+
this.notEqual(condition.value);
|
|
168
212
|
}
|
|
169
|
-
else if (condition.
|
|
213
|
+
else if (condition.op === comparison_1.ComparisonOperator.LIKE) {
|
|
170
214
|
this.like(condition.value);
|
|
171
215
|
}
|
|
172
|
-
else if (condition.
|
|
216
|
+
else if (condition.op === comparison_1.ComparisonOperator.LT) {
|
|
173
217
|
this.lt(condition.value);
|
|
174
218
|
}
|
|
175
|
-
else if (condition.
|
|
219
|
+
else if (condition.op === comparison_1.ComparisonOperator.LTE) {
|
|
176
220
|
this.lte(condition.value);
|
|
177
221
|
}
|
|
178
|
-
else if (condition.
|
|
222
|
+
else if (condition.op === comparison_1.ComparisonOperator.GT) {
|
|
179
223
|
this.gt(condition.value);
|
|
180
224
|
}
|
|
181
|
-
else if (condition.
|
|
225
|
+
else if (condition.op === comparison_1.ComparisonOperator.GTE) {
|
|
182
226
|
this.gte(condition.value);
|
|
183
227
|
}
|
|
184
|
-
else if (condition.
|
|
228
|
+
else if (condition.op === comparison_1.ComparisonOperator.IN_ARRAY) {
|
|
185
229
|
this.inArray(condition.value);
|
|
186
230
|
}
|
|
187
|
-
else if (condition.
|
|
231
|
+
else if (condition.op === comparison_1.ComparisonOperator.BETWEEN) {
|
|
188
232
|
this.between(condition.value.a, condition.value.b);
|
|
189
233
|
}
|
|
190
|
-
else if (condition.riao_condition === condition_token_1.ConditionTokenType.NOT) {
|
|
191
|
-
this.not(condition.value);
|
|
192
|
-
}
|
|
193
234
|
return this;
|
|
194
235
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
236
|
+
// ------------------------------------------------------------------------
|
|
237
|
+
// Logical
|
|
238
|
+
// ------------------------------------------------------------------------
|
|
239
|
+
and() {
|
|
240
|
+
this.sql.append(this.sql.operators.and + ' ');
|
|
241
|
+
}
|
|
242
|
+
or() {
|
|
243
|
+
this.sql.append(this.sql.operators.or + ' ');
|
|
244
|
+
}
|
|
245
|
+
not(value) {
|
|
246
|
+
if (value === null) {
|
|
247
|
+
this.sql.append(this.sql.operators.is + ' ');
|
|
248
|
+
this.notNull();
|
|
249
|
+
return;
|
|
202
250
|
}
|
|
203
|
-
|
|
204
|
-
|
|
251
|
+
const isComparison = (0, expression_1.isExpressionToken)(value) &&
|
|
252
|
+
(0, comparison_1.isComparisonToken)(value);
|
|
253
|
+
const conditionType = isComparison
|
|
254
|
+
? value === null || value === void 0 ? void 0 : value.op
|
|
255
|
+
: null;
|
|
256
|
+
if (isComparison &&
|
|
257
|
+
(conditionType === comparison_1.ComparisonOperator.LIKE ||
|
|
258
|
+
conditionType === comparison_1.ComparisonOperator.IN_ARRAY)) {
|
|
259
|
+
this.sql.append('NOT ');
|
|
260
|
+
this.buildComparisonToken(value);
|
|
205
261
|
}
|
|
206
|
-
else if (
|
|
207
|
-
|
|
208
|
-
for (const key in where) {
|
|
209
|
-
const value = where[key];
|
|
210
|
-
if (value === null) {
|
|
211
|
-
this.sql.columnName(key);
|
|
212
|
-
this.sql.space();
|
|
213
|
-
this.isNull();
|
|
214
|
-
}
|
|
215
|
-
else if ((0, conditions_1.isCondition)(value)) {
|
|
216
|
-
this.sql.columnName(key);
|
|
217
|
-
this.sql.space();
|
|
218
|
-
this.buildConditionToken(value);
|
|
219
|
-
}
|
|
220
|
-
else {
|
|
221
|
-
this.sql.columnName(key);
|
|
222
|
-
this.sql.space();
|
|
223
|
-
this.equals(value);
|
|
224
|
-
}
|
|
225
|
-
this.and();
|
|
226
|
-
}
|
|
227
|
-
this.sql.trimEnd(this.sql.operators.and);
|
|
228
|
-
this.sql.closeParens();
|
|
262
|
+
else if (isComparison) {
|
|
263
|
+
throw new Error('Cannot use not() with ' + conditionType);
|
|
229
264
|
}
|
|
230
|
-
else if (
|
|
265
|
+
else if (typeof value === 'object' || Array.isArray(value)) {
|
|
266
|
+
this.sql.append('!');
|
|
267
|
+
this.expression(value);
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
this.notEqual(value);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
logical(token) {
|
|
274
|
+
if (token.op === expression_1.LogicalOperator.AND) {
|
|
231
275
|
this.and();
|
|
232
276
|
}
|
|
233
|
-
else if (
|
|
277
|
+
else if (token.op === expression_1.LogicalOperator.OR) {
|
|
234
278
|
this.or();
|
|
235
279
|
}
|
|
236
|
-
|
|
280
|
+
else if (token.op === expression_1.LogicalOperator.NOT) {
|
|
281
|
+
this.not(token.expr);
|
|
282
|
+
}
|
|
237
283
|
}
|
|
284
|
+
// ------------------------------------------------------------------------
|
|
285
|
+
// Where
|
|
286
|
+
// ------------------------------------------------------------------------
|
|
238
287
|
where(where) {
|
|
239
|
-
if (typeof where === 'object' && !Object.keys(where).length) {
|
|
240
|
-
return this;
|
|
241
|
-
}
|
|
242
288
|
this.sql.append('WHERE ');
|
|
243
|
-
this.
|
|
289
|
+
this.expression(where);
|
|
244
290
|
return this;
|
|
245
291
|
}
|
|
246
292
|
limit(nRecords) {
|
|
247
293
|
this.sql.append('LIMIT ' + nRecords + ' ');
|
|
248
294
|
return this;
|
|
249
295
|
}
|
|
296
|
+
groupBy(by) {
|
|
297
|
+
this.sql.append('GROUP BY ');
|
|
298
|
+
for (const key of by) {
|
|
299
|
+
this.sql.columnName(key);
|
|
300
|
+
this.sql.append(', ');
|
|
301
|
+
}
|
|
302
|
+
this.sql.trimEnd(', ');
|
|
303
|
+
this.sql.space();
|
|
304
|
+
return this;
|
|
305
|
+
}
|
|
250
306
|
orderBy(by) {
|
|
251
307
|
this.sql.append('ORDER BY ');
|
|
252
308
|
for (const key in by) {
|
|
@@ -257,7 +313,7 @@ class DatabaseQueryBuilder extends statement_builder_1.StatementBuilder {
|
|
|
257
313
|
this.sql.space();
|
|
258
314
|
}
|
|
259
315
|
select(query) {
|
|
260
|
-
var _a;
|
|
316
|
+
var _a, _b;
|
|
261
317
|
this.selectStatement();
|
|
262
318
|
if (query.distinct) {
|
|
263
319
|
this.distinctStatement();
|
|
@@ -278,6 +334,9 @@ class DatabaseQueryBuilder extends statement_builder_1.StatementBuilder {
|
|
|
278
334
|
if (query.where) {
|
|
279
335
|
this.where(query.where);
|
|
280
336
|
}
|
|
337
|
+
if ((_b = query.groupBy) === null || _b === void 0 ? void 0 : _b.length) {
|
|
338
|
+
this.groupBy(query.groupBy);
|
|
339
|
+
}
|
|
281
340
|
if (query.orderBy) {
|
|
282
341
|
this.orderBy(query.orderBy);
|
|
283
342
|
}
|
|
@@ -299,7 +358,7 @@ class DatabaseQueryBuilder extends statement_builder_1.StatementBuilder {
|
|
|
299
358
|
}
|
|
300
359
|
if (join.on) {
|
|
301
360
|
this.sql.append('ON ');
|
|
302
|
-
this.
|
|
361
|
+
this.expression(join.on);
|
|
303
362
|
}
|
|
304
363
|
}
|
|
305
364
|
// ------------------------------------------------------------------------
|
|
@@ -367,7 +426,7 @@ class DatabaseQueryBuilder extends statement_builder_1.StatementBuilder {
|
|
|
367
426
|
const record = insertions[i];
|
|
368
427
|
this.sql.openParens();
|
|
369
428
|
for (const key in record) {
|
|
370
|
-
this.
|
|
429
|
+
this.expression(record[key]);
|
|
371
430
|
this.sql = this.sql.trimEnd();
|
|
372
431
|
this.sql.append(', ');
|
|
373
432
|
}
|
|
@@ -404,7 +463,7 @@ class DatabaseQueryBuilder extends statement_builder_1.StatementBuilder {
|
|
|
404
463
|
for (const key of keys) {
|
|
405
464
|
this.sql.columnName(key);
|
|
406
465
|
this.sql.append(' = ');
|
|
407
|
-
this.
|
|
466
|
+
this.expression(values[key]);
|
|
408
467
|
this.sql = this.sql.trimEnd();
|
|
409
468
|
this.sql.append(', ');
|
|
410
469
|
}
|
|
@@ -449,6 +508,33 @@ class DatabaseQueryBuilder extends statement_builder_1.StatementBuilder {
|
|
|
449
508
|
}
|
|
450
509
|
return this;
|
|
451
510
|
}
|
|
511
|
+
// ------------------------------------------------------------------------
|
|
512
|
+
// Database Functions
|
|
513
|
+
// ------------------------------------------------------------------------
|
|
514
|
+
/**
|
|
515
|
+
* Append a database function the query
|
|
516
|
+
*
|
|
517
|
+
* @param fn Database function token
|
|
518
|
+
*/
|
|
519
|
+
databaseFunction(fn) {
|
|
520
|
+
switch (fn.fn) {
|
|
521
|
+
case function_token_1.DatabaseFunctionKeys.COUNT:
|
|
522
|
+
this.count(fn);
|
|
523
|
+
break;
|
|
524
|
+
case function_token_1.DatabaseFunctionKeys.CURRENT_TIMESTAMP:
|
|
525
|
+
this.currentTimestamp(fn);
|
|
526
|
+
break;
|
|
527
|
+
}
|
|
528
|
+
return this;
|
|
529
|
+
}
|
|
530
|
+
count(fn) {
|
|
531
|
+
this.sql.append('COUNT(*)');
|
|
532
|
+
return this;
|
|
533
|
+
}
|
|
534
|
+
currentTimestamp(fn) {
|
|
535
|
+
this.sql.append('CURRENT_TIMESTAMP');
|
|
536
|
+
return this;
|
|
537
|
+
}
|
|
452
538
|
}
|
|
453
539
|
exports.DatabaseQueryBuilder = DatabaseQueryBuilder;
|
|
454
540
|
//# sourceMappingURL=query-builder.js.map
|