@stamhoofd/sql 2.46.0 → 2.48.0

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.
@@ -1,128 +1,128 @@
1
- import { isSQLExpression, joinSQLQuery, SQLExpression, SQLExpressionOptions, SQLQuery } from "./SQLExpression";
2
- import {Database} from "@simonbackx/simple-database"
1
+ import { Database } from '@simonbackx/simple-database';
2
+ import { joinSQLQuery, SQLExpression, SQLExpressionOptions, SQLQuery } from './SQLExpression';
3
3
 
4
- export type SQLScalarValue = string|number|boolean|Date;
5
- export type SQLDynamicExpression = SQLScalarValue|SQLScalarValue[]|null|SQLExpression
4
+ export type SQLScalarValue = string | number | boolean | Date;
5
+ export type SQLDynamicExpression = SQLScalarValue | SQLScalarValue[] | null | SQLExpression;
6
6
 
7
- export function scalarToSQLJSONExpression(s: SQLScalarValue|null): SQLExpression {
7
+ export function scalarToSQLExpression(s: SQLScalarValue | null): SQLExpression {
8
8
  if (s === null) {
9
- return new SQLJSONValue(null)
9
+ return new SQLNull();
10
10
  }
11
11
 
12
- if (s === true) {
13
- return new SQLJSONValue(true)
14
- }
15
-
16
- if (s === false) {
17
- return new SQLJSONValue(false)
18
- }
19
-
20
- return new SQLScalar(s)
21
- }
22
-
23
- export function scalarToSQLExpression(s: SQLScalarValue|null): SQLExpression {
24
- if (s === null) {
25
- return new SQLNull()
26
- }
27
-
28
- return new SQLScalar(s)
12
+ return new SQLScalar(s);
29
13
  }
30
14
 
31
15
  export function readDynamicSQLExpression(s: SQLDynamicExpression): SQLExpression {
32
16
  if (Array.isArray(s)) {
33
- return new SQLArray(s)
17
+ return new SQLArray(s);
34
18
  }
35
19
  if (s === null) {
36
- return new SQLNull()
20
+ return new SQLNull();
37
21
  }
38
22
 
39
23
  if (typeof s === 'object' && !(s instanceof Date)) {
40
24
  return s;
41
25
  }
42
26
 
43
- return new SQLScalar(s)
27
+ return new SQLScalar(s);
44
28
  }
45
29
  export class SQLDistinct implements SQLExpression {
46
- expression: SQLExpression
30
+ expression: SQLExpression;
47
31
 
48
32
  constructor(expression: SQLExpression) {
49
- this.expression = expression
33
+ this.expression = expression;
50
34
  }
51
35
 
52
36
  getSQL(options?: SQLExpressionOptions): SQLQuery {
53
37
  return joinSQLQuery([
54
38
  'DISTINCT',
55
39
  this.expression.getSQL(options),
56
- ])
40
+ ]);
57
41
  }
58
42
  }
59
43
  export class SQLCount implements SQLExpression {
60
- expression: SQLExpression|null
44
+ expression: SQLExpression | null;
61
45
 
62
- constructor(expression: SQLExpression|null = null) {
63
- this.expression = expression
46
+ constructor(expression: SQLExpression | null = null) {
47
+ this.expression = expression;
64
48
  }
65
49
 
66
50
  getSQL(options?: SQLExpressionOptions): SQLQuery {
67
51
  return joinSQLQuery([
68
52
  'COUNT(',
69
- this.expression ? this.expression.getSQL(options) : '*',
70
- ')'
71
- ])
53
+ this.expression ? this.expression.getSQL(options) : '*',
54
+ ')',
55
+ ]);
56
+ }
57
+ }
58
+
59
+ export class SQLLower implements SQLExpression {
60
+ expression: SQLExpression;
61
+
62
+ constructor(expression: SQLExpression) {
63
+ this.expression = expression;
64
+ }
65
+
66
+ getSQL(options?: SQLExpressionOptions): SQLQuery {
67
+ return joinSQLQuery([
68
+ 'LOWER(',
69
+ this.expression.getSQL(options),
70
+ ')',
71
+ ]);
72
72
  }
73
73
  }
74
74
 
75
75
  export class SQLPlusSign implements SQLExpression {
76
76
  getSQL(): SQLQuery {
77
- return '+'
77
+ return '+';
78
78
  }
79
79
  }
80
80
 
81
81
  export class SQLMultiplicationSign implements SQLExpression {
82
82
  getSQL(): SQLQuery {
83
- return '*'
83
+ return '*';
84
84
  }
85
85
  }
86
86
 
87
87
  export class SQLMinusSign implements SQLExpression {
88
88
  getSQL(): SQLQuery {
89
- return '-'
89
+ return '-';
90
90
  }
91
91
  }
92
92
 
93
93
  export class SQLCalculation implements SQLExpression {
94
- expressions: SQLExpression[]
94
+ expressions: SQLExpression[];
95
95
 
96
96
  constructor(...expressions: SQLExpression[]) {
97
- this.expressions = expressions
97
+ this.expressions = expressions;
98
98
  }
99
99
 
100
100
  getSQL(options?: SQLExpressionOptions): SQLQuery {
101
- return joinSQLQuery(this.expressions.map(e => e.getSQL(options)), ' ')
101
+ return joinSQLQuery(this.expressions.map(e => e.getSQL(options)), ' ');
102
102
  }
103
103
  }
104
104
 
105
105
  export class SQLSum implements SQLExpression {
106
- expression: SQLExpression
106
+ expression: SQLExpression;
107
107
 
108
108
  constructor(expression: SQLExpression) {
109
- this.expression = expression
109
+ this.expression = expression;
110
110
  }
111
111
 
112
112
  getSQL(options?: SQLExpressionOptions): SQLQuery {
113
113
  return joinSQLQuery([
114
114
  'SUM(',
115
- this.expression.getSQL(options),
116
- ')'
117
- ])
115
+ this.expression.getSQL(options),
116
+ ')',
117
+ ]);
118
118
  }
119
119
  }
120
120
  export class SQLSelectAs implements SQLExpression {
121
- expression: SQLExpression
122
- as: SQLAlias
121
+ expression: SQLExpression;
122
+ as: SQLAlias;
123
123
 
124
124
  constructor(expression: SQLExpression, as: SQLAlias) {
125
- this.expression = expression
125
+ this.expression = expression;
126
126
  this.as = as;
127
127
  }
128
128
 
@@ -130,17 +130,17 @@ export class SQLSelectAs implements SQLExpression {
130
130
  return joinSQLQuery([
131
131
  this.expression.getSQL(options),
132
132
  ' AS ',
133
- this.as.getSQL(options)
134
- ])
133
+ this.as.getSQL(options),
134
+ ]);
135
135
  }
136
136
  }
137
137
 
138
138
  export class SQLAssignment implements SQLExpression {
139
- key: SQLExpression
140
- value: SQLExpression
139
+ key: SQLExpression;
140
+ value: SQLExpression;
141
141
 
142
142
  constructor(key: SQLExpression, value: SQLExpression) {
143
- this.key = key
143
+ this.key = key;
144
144
  this.value = value;
145
145
  }
146
146
 
@@ -148,61 +148,58 @@ export class SQLAssignment implements SQLExpression {
148
148
  return joinSQLQuery([
149
149
  this.key.getSQL(options),
150
150
  ' = ',
151
- this.value.getSQL(options)
152
- ])
151
+ this.value.getSQL(options),
152
+ ]);
153
153
  }
154
154
  }
155
155
 
156
-
157
156
  export class SQLAlias implements SQLExpression {
158
157
  name: string;
159
158
 
160
159
  constructor(name: string) {
161
- this.name = name
160
+ this.name = name;
162
161
  }
163
162
 
164
163
  getSQL(options?: SQLExpressionOptions): SQLQuery {
165
- return Database.escapeId(this.name) ;
164
+ return Database.escapeId(this.name);
166
165
  }
167
166
  }
168
167
 
169
-
170
168
  export class SQLConcat implements SQLExpression {
171
169
  expressions: SQLExpression[];
172
170
 
173
171
  constructor(...expressions: SQLExpression[]) {
174
- this.expressions = expressions
172
+ this.expressions = expressions;
175
173
  }
176
174
 
177
175
  getSQL(options?: SQLExpressionOptions): SQLQuery {
178
176
  return joinSQLQuery([
179
177
  'CONCAT(',
180
178
  joinSQLQuery(this.expressions.map(e => e.getSQL(options)), ', '),
181
- ')'
182
- ])
179
+ ')',
180
+ ]);
183
181
  }
184
182
  }
185
183
 
186
-
187
184
  export class SQLAge implements SQLExpression {
188
185
  expression: SQLExpression;
189
186
 
190
187
  constructor(expression: SQLExpression) {
191
- this.expression = expression
188
+ this.expression = expression;
192
189
  }
193
190
 
194
191
  getSQL(options?: SQLExpressionOptions): SQLQuery {
195
192
  return joinSQLQuery([
196
193
  'TIMESTAMPDIFF(YEAR, ',
197
194
  this.expression.getSQL(options),
198
- ', CURDATE())'
199
- ])
195
+ ', CURDATE())',
196
+ ]);
200
197
  }
201
198
  }
202
199
 
203
200
  export class SQLCast implements SQLExpression {
204
- value: SQLExpression
205
- as = 'CHAR'
201
+ value: SQLExpression;
202
+ as = 'CHAR';
206
203
 
207
204
  constructor(value: SQLExpression, as = 'CHAR') {
208
205
  this.value = value;
@@ -212,23 +209,23 @@ export class SQLCast implements SQLExpression {
212
209
  getSQL(options?: SQLExpressionOptions): SQLQuery {
213
210
  return joinSQLQuery([
214
211
  'CAST( ',
215
- this.value.getSQL(options),
212
+ this.value.getSQL(options),
216
213
  ' AS ',
217
- this.as,
218
- ')'
219
- ])
214
+ this.as,
215
+ ')',
216
+ ]);
220
217
  }
221
218
  }
222
219
 
223
220
  export class SQLJSONValue implements SQLExpression {
224
- value: null|true|false;
221
+ value: null | true | false;
225
222
 
226
- constructor(value: null|true|false) {
223
+ constructor(value: null | true | false) {
227
224
  this.value = value;
228
225
  }
229
226
 
230
227
  getSQL(options?: SQLExpressionOptions): SQLQuery {
231
- return "CAST('"+JSON.stringify(this.value)+"' AS JSON)";
228
+ return "CAST('" + JSON.stringify(this.value) + "' AS JSON)";
232
229
  }
233
230
  }
234
231
 
@@ -248,22 +245,22 @@ export class SQLScalar implements SQLExpression {
248
245
  value: SQLScalarValue;
249
246
 
250
247
  constructor(value: SQLScalarValue) {
251
- this.value = value
248
+ this.value = value;
252
249
  }
253
250
 
254
251
  getSQL(options?: SQLExpressionOptions): SQLQuery {
255
252
  return {
256
253
  query: '?',
257
- params: [this.value]
258
- }
254
+ params: [this.value],
255
+ };
259
256
  }
260
257
  }
261
258
 
262
259
  export class SQLSafeValue implements SQLExpression {
263
- value: string|number;
260
+ value: string | number;
264
261
 
265
- constructor(value: string|number) {
266
- this.value = value
262
+ constructor(value: string | number) {
263
+ this.value = value;
267
264
  }
268
265
 
269
266
  getSQL(options?: SQLExpressionOptions): SQLQuery {
@@ -271,19 +268,18 @@ export class SQLSafeValue implements SQLExpression {
271
268
  }
272
269
  }
273
270
 
274
-
275
271
  export class SQLArray implements SQLExpression {
276
272
  value: SQLScalarValue[];
277
273
 
278
274
  constructor(value: SQLScalarValue[]) {
279
- this.value = value
275
+ this.value = value;
280
276
  }
281
277
 
282
278
  getSQL(options?: SQLExpressionOptions): SQLQuery {
283
279
  return {
284
280
  query: '(?)',
285
- params: [this.value]
286
- }
281
+ params: [this.value],
282
+ };
287
283
  }
288
284
  }
289
285
 
@@ -291,11 +287,11 @@ export class SQLWildcardSelectExpression implements SQLExpression {
291
287
  namespace?: string;
292
288
 
293
289
  constructor(namespace?: string) {
294
- this.namespace = namespace
290
+ this.namespace = namespace;
295
291
  }
296
292
 
297
293
  getSQL(options?: SQLExpressionOptions): SQLQuery {
298
- return Database.escapeId(this.namespace ?? options?.defaultNamespace ?? '') + '.*'
294
+ return Database.escapeId(this.namespace ?? options?.defaultNamespace ?? '') + '.*';
299
295
  }
300
296
  }
301
297
 
@@ -310,12 +306,12 @@ export class SQLColumnExpression implements SQLExpression {
310
306
  this.column = namespaceOrColumn;
311
307
  return;
312
308
  }
313
- this.namespace = namespaceOrColumn
314
- this.column = column
309
+ this.namespace = namespaceOrColumn;
310
+ this.column = column;
315
311
  }
316
312
 
317
313
  getSQL(options?: SQLExpressionOptions): SQLQuery {
318
- return Database.escapeId(this.namespace ?? options?.defaultNamespace ?? '') + '.' + Database.escapeId(this.column)
314
+ return Database.escapeId(this.namespace ?? options?.defaultNamespace ?? '') + '.' + Database.escapeId(this.column);
319
315
  }
320
316
  }
321
317
 
@@ -330,14 +326,14 @@ export class SQLTableExpression implements SQLExpression {
330
326
  this.table = namespaceOrTable;
331
327
  return;
332
328
  }
333
- this.namespace = namespaceOrTable
334
- this.table = table
329
+ this.namespace = namespaceOrTable;
330
+ this.table = table;
335
331
  }
336
332
 
337
333
  getSQL(options?: SQLExpressionOptions): SQLQuery {
338
334
  if (!this.namespace) {
339
- return Database.escapeId(this.table)
335
+ return Database.escapeId(this.table);
340
336
  }
341
- return Database.escapeId(this.table) + ' ' + Database.escapeId(this.namespace)
337
+ return Database.escapeId(this.table) + ' ' + Database.escapeId(this.namespace);
342
338
  }
343
339
  }
@@ -1,9 +1,25 @@
1
- import { SQLExpression, SQLExpressionOptions, SQLQuery, joinSQLQuery } from "./SQLExpression";
2
- import { SQLSafeValue } from "./SQLExpressions";
3
- import { SQLWhere } from "./SQLWhere";
1
+ import { SQLExpression, SQLExpressionOptions, SQLQuery, joinSQLQuery } from './SQLExpression';
2
+ import { SQLJSONValue, SQLSafeValue, SQLScalar, SQLScalarValue } from './SQLExpressions';
3
+ import { SQLWhere } from './SQLWhere';
4
+
5
+ export function scalarToSQLJSONExpression(s: SQLScalarValue | null): SQLExpression {
6
+ if (s === null) {
7
+ return new SQLJSONValue(null);
8
+ }
9
+
10
+ if (s === true) {
11
+ return new SQLJSONValue(true);
12
+ }
13
+
14
+ if (s === false) {
15
+ return new SQLJSONValue(false);
16
+ }
17
+
18
+ return new SQLScalar(s);
19
+ }
4
20
 
5
21
  export class SQLJsonUnquote implements SQLExpression {
6
- target: SQLExpression
22
+ target: SQLExpression;
7
23
 
8
24
  constructor(target: SQLExpression) {
9
25
  this.target = target;
@@ -12,9 +28,9 @@ export class SQLJsonUnquote implements SQLExpression {
12
28
  getSQL(options?: SQLExpressionOptions): SQLQuery {
13
29
  return joinSQLQuery([
14
30
  'JSON_UNQUOTE(',
15
- this.target.getSQL(options),
16
- ')'
17
- ])
31
+ this.target.getSQL(options),
32
+ ')',
33
+ ]);
18
34
  }
19
35
  }
20
36
 
@@ -22,8 +38,8 @@ export class SQLJsonUnquote implements SQLExpression {
22
38
  * Same as target->path, JSON_EXTRACT(target, path)
23
39
  */
24
40
  export class SQLJsonExtract implements SQLExpression {
25
- target: SQLExpression
26
- path: SQLExpression
41
+ target: SQLExpression;
42
+ path: SQLExpression;
27
43
 
28
44
  constructor(target: SQLExpression, path: SQLExpression) {
29
45
  this.target = target;
@@ -33,17 +49,17 @@ export class SQLJsonExtract implements SQLExpression {
33
49
  getSQL(options?: SQLExpressionOptions): SQLQuery {
34
50
  return joinSQLQuery([
35
51
  'JSON_EXTRACT(',
36
- this.target.getSQL(options),
37
- ',',
38
- this.path.getSQL(options),
39
- ')'
40
- ])
52
+ this.target.getSQL(options),
53
+ ',',
54
+ this.path.getSQL(options),
55
+ ')',
56
+ ]);
41
57
  }
42
58
  }
43
59
 
44
60
  export class SQLJsonLength implements SQLExpression {
45
- target: SQLExpression
46
- path?: SQLExpression
61
+ target: SQLExpression;
62
+ path?: SQLExpression;
47
63
 
48
64
  constructor(target: SQLExpression, path?: SQLExpression) {
49
65
  this.target = target;
@@ -53,25 +69,27 @@ export class SQLJsonLength implements SQLExpression {
53
69
  getSQL(options?: SQLExpressionOptions): SQLQuery {
54
70
  return joinSQLQuery([
55
71
  'JSON_LENGTH(',
56
- this.target.getSQL(options),
57
- ...(this.path ? [
58
- ',',
59
- this.path.getSQL(options),
60
- ] : []),
61
- ')'
62
- ])
72
+ this.target.getSQL(options),
73
+ ...(this.path
74
+ ? [
75
+ ',',
76
+ this.path.getSQL(options),
77
+ ]
78
+ : []),
79
+ ')',
80
+ ]);
63
81
  }
64
82
  }
65
83
  /**
66
84
  * JSON_SEARCH(json_doc, one_or_all, search_str[, escape_char[, path] ...])
67
85
  */
68
86
  export class SQLJsonSearch implements SQLExpression {
69
- target: SQLExpression
70
- oneOrAll: 'one'|'all'
87
+ target: SQLExpression;
88
+ oneOrAll: 'one' | 'all';
71
89
  searchStr: SQLExpression;
72
- path: SQLExpression|null;
90
+ path: SQLExpression | null;
73
91
 
74
- constructor(target: SQLExpression, oneOrAll: 'one'|'all', searchStr: SQLExpression, path: SQLExpression|null = null) {
92
+ constructor(target: SQLExpression, oneOrAll: 'one' | 'all', searchStr: SQLExpression, path: SQLExpression | null = null) {
75
93
  this.target = target;
76
94
  this.oneOrAll = oneOrAll;
77
95
  this.searchStr = searchStr;
@@ -81,17 +99,19 @@ export class SQLJsonSearch implements SQLExpression {
81
99
  getSQL(options?: SQLExpressionOptions): SQLQuery {
82
100
  return joinSQLQuery([
83
101
  'JSON_SEARCH(',
84
- this.target.getSQL(options),
85
- ',',
86
- new SQLSafeValue(this.oneOrAll).getSQL(options),
87
- ',',
88
- this.searchStr.getSQL(options),
89
- ...(this.path ? [
90
- ',',
91
- this.path.getSQL(options)
92
- ] : []),
93
- ')'
94
- ])
102
+ this.target.getSQL(options),
103
+ ',',
104
+ new SQLSafeValue(this.oneOrAll).getSQL(options),
105
+ ',',
106
+ this.searchStr.getSQL(options),
107
+ ...(this.path
108
+ ? [
109
+ ',',
110
+ this.path.getSQL(options),
111
+ ]
112
+ : []),
113
+ ')',
114
+ ]);
95
115
  }
96
116
  }
97
117
 
@@ -99,12 +119,12 @@ export class SQLJsonSearch implements SQLExpression {
99
119
  * JSON_CONTAINS(target, candidate[, path])
100
120
  */
101
121
  export class SQLJsonContains extends SQLWhere {
102
- target: SQLExpression
122
+ target: SQLExpression;
103
123
  candidate: SQLExpression;
104
- path: SQLExpression|null;
124
+ path: SQLExpression | null;
105
125
 
106
- constructor(target: SQLExpression, candidate: SQLExpression, path: SQLExpression|null = null) {
107
- super()
126
+ constructor(target: SQLExpression, candidate: SQLExpression, path: SQLExpression | null = null) {
127
+ super();
108
128
  this.target = target;
109
129
  this.candidate = candidate;
110
130
  this.path = path;
@@ -113,28 +133,29 @@ export class SQLJsonContains extends SQLWhere {
113
133
  getSQL(options?: SQLExpressionOptions): SQLQuery {
114
134
  return joinSQLQuery([
115
135
  'JSON_CONTAINS(',
116
- this.target.getSQL(options),
117
- ',',
118
- this.candidate.getSQL(options),
119
- ...(this.path ? [
120
- ',',
121
- this.path.getSQL(options)
122
- ] : []),
123
- ')'
124
- ])
136
+ this.target.getSQL(options),
137
+ ',',
138
+ this.candidate.getSQL(options),
139
+ ...(this.path
140
+ ? [
141
+ ',',
142
+ this.path.getSQL(options),
143
+ ]
144
+ : []),
145
+ ')',
146
+ ]);
125
147
  }
126
148
  }
127
149
 
128
-
129
150
  /**
130
151
  * JSON_CONTAINS(json_doc1, json_doc2)
131
152
  */
132
153
  export class SQLJsonOverlaps extends SQLWhere {
133
- jsonDoc1: SQLExpression
154
+ jsonDoc1: SQLExpression;
134
155
  jsonDoc2: SQLExpression;
135
156
 
136
157
  constructor(jsonDoc1: SQLExpression, jsonDoc2: SQLExpression) {
137
- super()
158
+ super();
138
159
  this.jsonDoc1 = jsonDoc1;
139
160
  this.jsonDoc2 = jsonDoc2;
140
161
  }
@@ -142,10 +163,10 @@ export class SQLJsonOverlaps extends SQLWhere {
142
163
  getSQL(options?: SQLExpressionOptions): SQLQuery {
143
164
  return joinSQLQuery([
144
165
  'JSON_OVERLAPS(',
145
- this.jsonDoc1.getSQL(options),
146
- ',',
147
- this.jsonDoc2.getSQL(options),
148
- ')'
149
- ])
166
+ this.jsonDoc1.getSQL(options),
167
+ ',',
168
+ this.jsonDoc2.getSQL(options),
169
+ ')',
170
+ ]);
150
171
  }
151
172
  }