@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.
Files changed (110) hide show
  1. package/builder/sql-builder.d.ts +1 -9
  2. package/builder/sql-builder.js +5 -43
  3. package/builder/sql-builder.js.map +1 -1
  4. package/builder/statement-builder.d.ts +1 -0
  5. package/builder/statement-builder.js +5 -0
  6. package/builder/statement-builder.js.map +1 -1
  7. package/comparison/comparison-aliases.d.ts +10 -0
  8. package/comparison/comparison-aliases.js +41 -0
  9. package/comparison/comparison-aliases.js.map +1 -0
  10. package/comparison/comparison-token.d.ts +17 -0
  11. package/comparison/comparison-token.js +16 -0
  12. package/comparison/comparison-token.js.map +1 -0
  13. package/comparison/comparison.d.ts +13 -0
  14. package/comparison/comparison.js +72 -0
  15. package/comparison/comparison.js.map +1 -0
  16. package/comparison/index.d.ts +4 -0
  17. package/comparison/index.js +25 -0
  18. package/comparison/index.js.map +1 -0
  19. package/comparison/is-comparison-token.d.ts +2 -0
  20. package/comparison/is-comparison-token.js +9 -0
  21. package/comparison/is-comparison-token.js.map +1 -0
  22. package/ddl/ddl-builder.d.ts +3 -0
  23. package/ddl/ddl-builder.js +10 -3
  24. package/ddl/ddl-builder.js.map +1 -1
  25. package/ddl/ddl-repository.js +15 -14
  26. package/ddl/ddl-repository.js.map +1 -1
  27. package/dml/delete.d.ts +2 -2
  28. package/dml/group-by.d.ts +4 -0
  29. package/dml/group-by.js +3 -0
  30. package/dml/group-by.js.map +1 -0
  31. package/dml/index.d.ts +2 -1
  32. package/dml/index.js +2 -1
  33. package/dml/index.js.map +1 -1
  34. package/dml/insert.d.ts +6 -4
  35. package/dml/join.d.ts +3 -3
  36. package/dml/query-builder.d.ts +37 -21
  37. package/dml/query-builder.js +197 -111
  38. package/dml/query-builder.js.map +1 -1
  39. package/dml/query-repository.js +12 -7
  40. package/dml/query-repository.js.map +1 -1
  41. package/dml/select.d.ts +7 -6
  42. package/dml/subquery.d.ts +5 -0
  43. package/dml/subquery.js +11 -0
  44. package/dml/subquery.js.map +1 -0
  45. package/dml/update.d.ts +2 -2
  46. package/expression/expression-token.d.ts +9 -0
  47. package/expression/expression-token.js +11 -0
  48. package/expression/expression-token.js.map +1 -0
  49. package/expression/expression.d.ts +6 -0
  50. package/expression/expression.js +3 -0
  51. package/expression/expression.js.map +1 -0
  52. package/expression/identifier.d.ts +7 -0
  53. package/expression/identifier.js +16 -0
  54. package/expression/identifier.js.map +1 -0
  55. package/expression/index.d.ts +5 -0
  56. package/{conditions → expression}/index.js +7 -6
  57. package/expression/index.js.map +1 -0
  58. package/expression/is-expression-token.d.ts +2 -0
  59. package/expression/is-expression-token.js +8 -0
  60. package/expression/is-expression-token.js.map +1 -0
  61. package/expression/key-val-expression.d.ts +7 -0
  62. package/expression/key-val-expression.js +3 -0
  63. package/expression/key-val-expression.js.map +1 -0
  64. package/expression/literal.d.ts +2 -0
  65. package/{dml/where.js → expression/literal.js} +1 -1
  66. package/expression/literal.js.map +1 -0
  67. package/expression/logical.d.ts +18 -0
  68. package/expression/logical.js +31 -0
  69. package/expression/logical.js.map +1 -0
  70. package/expression/simple-expression.d.ts +5 -0
  71. package/expression/simple-expression.js +3 -0
  72. package/expression/simple-expression.js.map +1 -0
  73. package/functions/function-token.d.ts +6 -4
  74. package/functions/function-token.js.map +1 -1
  75. package/functions/functions.js +5 -2
  76. package/functions/functions.js.map +1 -1
  77. package/functions/is-database-function.d.ts +3 -2
  78. package/functions/is-database-function.js +4 -3
  79. package/functions/is-database-function.js.map +1 -1
  80. package/index.d.ts +2 -1
  81. package/index.js +2 -1
  82. package/index.js.map +1 -1
  83. package/package.json +1 -1
  84. package/repository/repository.d.ts +12 -1
  85. package/repository/repository.js +34 -1
  86. package/repository/repository.js.map +1 -1
  87. package/seed/auto-seed.js +4 -4
  88. package/seed/auto-seed.js.map +1 -1
  89. package/tokens/index.d.ts +1 -1
  90. package/tokens/index.js +3 -15
  91. package/tokens/index.js.map +1 -1
  92. package/conditions/condition-aliases.d.ts +0 -12
  93. package/conditions/condition-aliases.js +0 -43
  94. package/conditions/condition-aliases.js.map +0 -1
  95. package/conditions/condition-token.d.ts +0 -18
  96. package/conditions/condition-token.js +0 -16
  97. package/conditions/condition-token.js.map +0 -1
  98. package/conditions/conditions.d.ts +0 -14
  99. package/conditions/conditions.js +0 -68
  100. package/conditions/conditions.js.map +0 -1
  101. package/conditions/index.d.ts +0 -4
  102. package/conditions/index.js.map +0 -1
  103. package/conditions/is-condition.d.ts +0 -1
  104. package/conditions/is-condition.js +0 -8
  105. package/conditions/is-condition.js.map +0 -1
  106. package/dml/where.d.ts +0 -8
  107. package/dml/where.js.map +0 -1
  108. package/tokens/column-name-token.d.ts +0 -4
  109. package/tokens/column-name-token.js +0 -8
  110. package/tokens/column-name-token.js.map +0 -1
@@ -0,0 +1,4 @@
1
+ import { DatabaseRecord } from '../record';
2
+ type AllowedKeys<T extends DatabaseRecord = DatabaseRecord> = keyof T & string;
3
+ export type GroupBy<T extends DatabaseRecord = DatabaseRecord> = AllowedKeys<T>[];
4
+ export {};
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=group-by.js.map
@@ -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,2CAAyB;AACzB,2CAAyB;AACzB,0CAAwB"}
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 InsertOptions<T extends DatabaseRecord = DatabaseRecord> {
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 InsertOneOptions<T extends DatabaseRecord = DatabaseRecord> extends InsertOptions<T> {
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 { Where } from './where';
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?: Where;
8
+ on?: Expression;
9
9
  }
10
10
  export interface LeftRightJoin extends BaseJoin {
11
11
  type?: 'LEFT' | 'RIGHT';
12
12
  outer?: boolean;
13
- on: Where;
13
+ on: Expression;
14
14
  }
15
15
  export type Join = InnerCrossJoin | LeftRightJoin;
@@ -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 { ConditionToken } from '../conditions/condition-token';
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
- equals(value: any): void;
19
- like(value: any): void;
20
- lt(value: any): void;
21
- lte(value: any): void;
22
- gt(value: any): void;
23
- gte(value: any): void;
24
- notEqual(value: any): void;
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: any[]): void;
29
- between(a: any, b: any): void;
33
+ inArray(values: Expression[]): void;
34
+ between(a: Expression, b: Expression): void;
30
35
  notNull(): void;
31
- not(value: any): void;
32
- buildConditionToken(condition: ConditionToken): this;
33
- whereClause(where: Where | Where[] | ConditionToken): this;
34
- where(where: Where | Where[]): this;
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, any>): this;
41
- insertOnDuplicateKeyUpdate(assignment: Record<string, any>): this;
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]: any;
56
+ [key: string]: Expression;
49
57
  }): this;
50
58
  updateSetStatement(values: {
51
- [key: string]: any;
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
  }
@@ -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 condition_token_1 = require("../conditions/condition-token");
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
- if ((0, functions_1.isDatabaseFunction)(column.query)) {
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
- // Conditions
152
+ // Comparisons
79
153
  // ------------------------------------------------------------------------
80
- equals(value) {
154
+ equal(value) {
81
155
  this.sql.append(this.sql.operators.equals + ' ');
82
- this.sql.placeholder(value);
156
+ this.expression(value);
83
157
  }
84
158
  like(value) {
85
159
  this.sql.append(this.sql.operators.like + ' ');
86
- this.sql.placeholder(value);
160
+ this.expression(value);
87
161
  }
88
162
  lt(value) {
89
163
  this.sql.append(this.sql.operators.lt + ' ');
90
- this.sql.placeholder(value);
164
+ this.expression(value);
91
165
  }
92
166
  lte(value) {
93
167
  this.sql.append(this.sql.operators.lte + ' ');
94
- this.sql.placeholder(value);
168
+ this.expression(value);
95
169
  }
96
170
  gt(value) {
97
171
  this.sql.append(this.sql.operators.gt + ' ');
98
- this.sql.placeholder(value);
172
+ this.expression(value);
99
173
  }
100
174
  gte(value) {
101
175
  this.sql.append(this.sql.operators.gte + ' ');
102
- this.sql.placeholder(value);
176
+ this.expression(value);
103
177
  }
104
178
  notEqual(value) {
105
179
  this.sql.append(this.sql.operators.notEqual + ' ');
106
- this.sql.placeholder(value);
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.sql.placeholder(value);
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.sql.placeholder(a);
198
+ this.expression(a);
131
199
  this.sql.append(', ');
132
- this.sql.placeholder(b);
200
+ this.expression(b);
133
201
  this.sql.closeParens();
134
202
  }
135
203
  notNull() {
136
204
  this.sql.append('NOT NULL ');
137
205
  }
138
- not(value) {
139
- if (value === null) {
140
- this.sql.append(this.sql.operators.is + ' ');
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
- buildConditionToken(condition) {
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.riao_condition === condition_token_1.ConditionTokenType.LIKE) {
213
+ else if (condition.op === comparison_1.ComparisonOperator.LIKE) {
170
214
  this.like(condition.value);
171
215
  }
172
- else if (condition.riao_condition === condition_token_1.ConditionTokenType.LT) {
216
+ else if (condition.op === comparison_1.ComparisonOperator.LT) {
173
217
  this.lt(condition.value);
174
218
  }
175
- else if (condition.riao_condition === condition_token_1.ConditionTokenType.LTE) {
219
+ else if (condition.op === comparison_1.ComparisonOperator.LTE) {
176
220
  this.lte(condition.value);
177
221
  }
178
- else if (condition.riao_condition === condition_token_1.ConditionTokenType.GT) {
222
+ else if (condition.op === comparison_1.ComparisonOperator.GT) {
179
223
  this.gt(condition.value);
180
224
  }
181
- else if (condition.riao_condition === condition_token_1.ConditionTokenType.GTE) {
225
+ else if (condition.op === comparison_1.ComparisonOperator.GTE) {
182
226
  this.gte(condition.value);
183
227
  }
184
- else if (condition.riao_condition === condition_token_1.ConditionTokenType.IN_ARRAY) {
228
+ else if (condition.op === comparison_1.ComparisonOperator.IN_ARRAY) {
185
229
  this.inArray(condition.value);
186
230
  }
187
- else if (condition.riao_condition === condition_token_1.ConditionTokenType.BETWEEN) {
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
- whereClause(where) {
196
- if (Array.isArray(where)) {
197
- this.sql.openParens();
198
- for (const w of where) {
199
- this.whereClause(w);
200
- }
201
- this.sql.closeParens();
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
- else if ((0, conditions_1.isCondition)(where)) {
204
- this.buildConditionToken(where);
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 (typeof where === 'object') {
207
- this.sql.openParens();
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 (where === 'and') {
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 (where === 'or') {
277
+ else if (token.op === expression_1.LogicalOperator.OR) {
234
278
  this.or();
235
279
  }
236
- return this;
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.whereClause(where);
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.whereClause(join.on);
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.sql.placeholder(record[key]);
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.sql.placeholder(values[key]);
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