@itwin/ecsql-common 4.2.0-dev.10

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.
@@ -0,0 +1,2393 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MemberFuncCallExpr = exports.CastExpr = exports.BinaryValueExpr = exports.SearchCaseExpr = exports.IIFExpr = exports.SetClauseExpr = exports.AssignmentExpr = exports.ECSqlOptionsClauseExpr = exports.UpdateStatementExpr = exports.ClassNameExpr = exports.TableValuedFuncExpr = exports.CteBlockRefExpr = exports.CteBlockExpr = exports.CteExpr = exports.BetweenExpr = exports.LikeExpr = exports.InExpr = exports.NotExpr = exports.IsOfTypeExpr = exports.IsNullExpr = exports.BinaryBooleanExpr = exports.SubqueryExpr = exports.SelectExpr = exports.LimitClauseExpr = exports.OrderByClauseExpr = exports.OrderBySpecExpr = exports.WhereClauseExp = exports.FromClauseExpr = exports.HavingClauseExpr = exports.GroupByClauseExpr = exports.SelectionClauseExpr = exports.SelectStatementExpr = exports.SubqueryRefExpr = exports.SubqueryTestExpr = exports.UsingRelationshipJoinExpr = exports.QualifiedJoinExpr = exports.InsertStatementExpr = exports.DeleteStatementExpr = exports.DerivedPropertyExpr = exports.ECSqlWriter = exports.ClassRefExpr = exports.ValueExpr = exports.BooleanExpr = exports.ComputedExpr = exports.StatementExpr = exports.Expr = exports.ExprFactory = exports.ExprType = exports.JoinType = exports.LiteralValueType = void 0;
4
+ exports.PropertyNameExpr = exports.LiteralExpr = exports.UnaryValueExpr = exports.ParameterExpr = exports.FuncCallExpr = void 0;
5
+ const core_bentley_1 = require("@itwin/core-bentley");
6
+ /**
7
+ * Id returned by native code
8
+ * @internal
9
+ */
10
+ var NativeExpIds;
11
+ (function (NativeExpIds) {
12
+ NativeExpIds["AllOrAny"] = "AllOrAnyExp";
13
+ NativeExpIds["Assignment"] = "AssignmentExp";
14
+ NativeExpIds["BetweenRangeValue"] = "BetweenRangeValueExp";
15
+ NativeExpIds["BinaryBoolean"] = "BinaryBooleanExp";
16
+ NativeExpIds["BinaryValue"] = "BinaryValueExp";
17
+ NativeExpIds["BooleanFactor"] = "BooleanFactorExp";
18
+ NativeExpIds["Cast"] = "CastExp";
19
+ NativeExpIds["ClassName"] = "ClassNameExp";
20
+ NativeExpIds["CommonTable"] = "CommonTableExp";
21
+ NativeExpIds["CommonTableBlock"] = "CommonTableBlockExp";
22
+ NativeExpIds["CommonTableBlockName"] = "CommonTableBlockNameExp";
23
+ NativeExpIds["CrossJoin"] = "CrossJoinExp";
24
+ NativeExpIds["DeleteStatement"] = "DeleteStatementExp";
25
+ NativeExpIds["DerivedProperty"] = "DerivedPropertyExp";
26
+ NativeExpIds["FunctionCall"] = "FunctionCallExp";
27
+ NativeExpIds["IIF"] = "IIFExp";
28
+ NativeExpIds["InsertStatement"] = "InsertStatementExp";
29
+ NativeExpIds["LikeRhsValue"] = "LikeRhsValueExp";
30
+ NativeExpIds["LimitOffset"] = "LimitOffsetExp";
31
+ NativeExpIds["LiteralValue"] = "LiteralValueExp";
32
+ NativeExpIds["MemberFunctionCall"] = "MemberFunctionCallExp";
33
+ NativeExpIds["NaturalJoin"] = "NaturalJoinExp";
34
+ NativeExpIds["Options"] = "OptionsExp";
35
+ NativeExpIds["OrderBySpec"] = "OrderBySpecExp";
36
+ NativeExpIds["Parameter"] = "ParameterExp";
37
+ NativeExpIds["PropertyName"] = "PropertyNameExp";
38
+ NativeExpIds["QualifiedJoin"] = "QualifiedJoinExp";
39
+ NativeExpIds["RowConstructor"] = "RowConstructor";
40
+ NativeExpIds["SearchCaseValue"] = "SearchCaseValueExp";
41
+ NativeExpIds["SelectStatement"] = "SelectStatementExp";
42
+ NativeExpIds["SingleSelectStatement"] = "SingleSelectStatementExp";
43
+ NativeExpIds["Subquery"] = "SubqueryExp";
44
+ NativeExpIds["SubqueryRef"] = "SubqueryRefExp";
45
+ NativeExpIds["SubqueryTest"] = "SubqueryTestExp";
46
+ NativeExpIds["TableValuedFunction"] = "TableValuedFunctionExp";
47
+ NativeExpIds["UnaryValue"] = "UnaryValueExp";
48
+ NativeExpIds["UpdateStatement"] = "UpdateStatementExp";
49
+ NativeExpIds["UsingRelationshipJoinExp"] = "UsingRelationshipJoinExp";
50
+ })(NativeExpIds || (NativeExpIds = {}));
51
+ /**
52
+ * Type of literal value.
53
+ * @alpha
54
+ */
55
+ var LiteralValueType;
56
+ (function (LiteralValueType) {
57
+ LiteralValueType["Null"] = "NULL";
58
+ LiteralValueType["String"] = "STRING";
59
+ LiteralValueType["Date"] = "DATE";
60
+ LiteralValueType["Time"] = "TIME";
61
+ LiteralValueType["Timestamp"] = "TIMESTAMP";
62
+ LiteralValueType["Raw"] = "RAW";
63
+ })(LiteralValueType = exports.LiteralValueType || (exports.LiteralValueType = {}));
64
+ /**
65
+ * Qualified JOIN type @see [[QualifiedJoinExpr]]
66
+ * @alpha
67
+ */
68
+ var JoinType;
69
+ (function (JoinType) {
70
+ JoinType["LeftOuter"] = "LEFT OUTER JOIN";
71
+ JoinType["RightOuter"] = "RIGHT OUTER JOIN";
72
+ JoinType["FullOuter"] = "FULL OUTER JOIN";
73
+ JoinType["Inner"] = "INNER JOIN";
74
+ })(JoinType = exports.JoinType || (exports.JoinType = {}));
75
+ /**
76
+ * ECSql expr type supported @see [[Expr]]
77
+ * @alpha
78
+ */
79
+ var ExprType;
80
+ (function (ExprType) {
81
+ ExprType["Literal"] = "Literal";
82
+ ExprType["Unary"] = "Unary";
83
+ ExprType["Parameter"] = "Parameter";
84
+ ExprType["Cast"] = "Cast";
85
+ ExprType["BinaryValue"] = "BinaryValue";
86
+ ExprType["SearchCase"] = "SearchCase";
87
+ ExprType["IIF"] = "IIF";
88
+ ExprType["FuncCall"] = "FuncCall";
89
+ ExprType["PropertyName"] = "PropertyName";
90
+ ExprType["Subquery"] = "Subquery";
91
+ ExprType["Between"] = "Between";
92
+ // Match = "Match",
93
+ ExprType["Like"] = "Like";
94
+ ExprType["In"] = "In";
95
+ ExprType["Not"] = "Not";
96
+ ExprType["IsOfType"] = "IsOfType";
97
+ ExprType["IsNull"] = "IsNull";
98
+ ExprType["BinaryBoolean"] = "BinaryBoolean";
99
+ ExprType["SubqueryTest"] = "SubqueryTest";
100
+ ExprType["UsingRelationshipJoin"] = "UsingRelationshipJoin";
101
+ ExprType["QualifiedJoin"] = "QualifiedJoin";
102
+ ExprType["SubqueryRef"] = "SubqueryRef";
103
+ ExprType["CteBlockRef"] = "CteBlockRef";
104
+ ExprType["ClassName"] = "ClassName";
105
+ ExprType["TableValuedFunc"] = "TableValuedFunc";
106
+ ExprType["DerivedProperty"] = "DerivedProperty";
107
+ ExprType["SetClause"] = "SetClause";
108
+ ExprType["Select"] = "Select";
109
+ ExprType["ECSqlOptionsClause"] = "ECSqlOptions";
110
+ ExprType["CteBlock"] = "CteBlock";
111
+ ExprType["MemberFuncCall"] = "MemberFuncCall";
112
+ ExprType["Cte"] = "Cte";
113
+ ExprType["UpdateStatement"] = "UpdateStatement";
114
+ ExprType["InsertStatement"] = "InsertStatement";
115
+ ExprType["DeleteStatement"] = "DeleteStatement";
116
+ ExprType["SelectStatement"] = "SelectStatement";
117
+ ExprType["SelectionClause"] = "SelectionClause";
118
+ ExprType["WhereClause"] = "WhereClause";
119
+ ExprType["GroupByClause"] = "GroupByClause";
120
+ ExprType["HavingClause"] = "HavingCluase";
121
+ ExprType["FromClause"] = "FromClause";
122
+ ExprType["OrderByClause"] = "OrderByClause";
123
+ ExprType["OrderBySpec"] = "OrderBySpec";
124
+ ExprType["LimitClause"] = "LimitClause";
125
+ ExprType["Assignment"] = "Assignment";
126
+ })(ExprType = exports.ExprType || (exports.ExprType = {}));
127
+ /**
128
+ * Allow to create statement expression tree from a ecsql
129
+ * @alpha
130
+ */
131
+ class ExprFactory {
132
+ constructor(provider) {
133
+ this.provider = provider;
134
+ }
135
+ async parseStatement(ecsql) {
136
+ return StatementExpr.deserialize(this.provider.parseECSql(ecsql));
137
+ }
138
+ }
139
+ exports.ExprFactory = ExprFactory;
140
+ /**
141
+ * Base class for all ECSql expressions.
142
+ * @alpha
143
+ */
144
+ class Expr {
145
+ constructor(expType) {
146
+ this.expType = expType;
147
+ }
148
+ /**
149
+ * Find instances of expressions matching the type in sub tree.
150
+ * @param type a subclass of Expr
151
+ * @returns
152
+ */
153
+ findInstancesOf(type) {
154
+ const listOfT = [];
155
+ this.traverse((expr) => {
156
+ const inst = expr.asInstanceOf(type);
157
+ if (inst)
158
+ listOfT.push(inst);
159
+ });
160
+ return listOfT;
161
+ }
162
+ /**
163
+ * Allow to traverse the expression tree depth first
164
+ * @param callback this will be called for each expression traverse from first to last.
165
+ */
166
+ traverse(callback) {
167
+ const list = [this];
168
+ let parent;
169
+ while (list.length > 0) {
170
+ const current = list.pop();
171
+ if (current) {
172
+ const rc = callback(current, parent);
173
+ if (typeof rc === "boolean") {
174
+ if (!rc)
175
+ return;
176
+ }
177
+ parent = current;
178
+ list.push(...current.children.reverse());
179
+ }
180
+ }
181
+ }
182
+ /**
183
+ * Test if class instance is of certain type
184
+ * @param type A class that extends from Expr
185
+ * @returns true if instances matches the type else return false.
186
+ */
187
+ isInstanceOf(type) { return (0, core_bentley_1.isInstanceOf)(this, type); }
188
+ asInstanceOf(type) { return (0, core_bentley_1.asInstanceOf)(this, type); }
189
+ /**
190
+ * Convert expression tree to ECSQL.
191
+ * @param args args to ecsql writer.
192
+ * @returns ECSQL string
193
+ */
194
+ toECSql(args) {
195
+ if (args) {
196
+ const customWriter = new ECSqlWriter(args);
197
+ this.writeTo(customWriter);
198
+ return customWriter.toString();
199
+ }
200
+ const defaultWriter = new ECSqlWriter();
201
+ this.writeTo(defaultWriter);
202
+ return defaultWriter.toString();
203
+ }
204
+ }
205
+ exports.Expr = Expr;
206
+ /**
207
+ * Base class for all ECSQL Statements. Here are list of subclasses @see [[CteExpr]], @see [[SelectStatement]], @see [[InsertStatement]], @see [UpdateStatement]] and @see [[DeleteStatement]]
208
+ * @alpha
209
+ */
210
+ class StatementExpr extends Expr {
211
+ static deserialize(node) {
212
+ if (node.id === NativeExpIds.CommonTable)
213
+ return CteExpr.deserialize(node);
214
+ if (node.id === NativeExpIds.InsertStatement)
215
+ return InsertStatementExpr.deserialize(node);
216
+ if (node.id === NativeExpIds.UpdateStatement)
217
+ return UpdateStatementExpr.deserialize(node);
218
+ if (node.id === NativeExpIds.DeleteStatement)
219
+ return DeleteStatementExpr.deserialize(node);
220
+ if (node.id === NativeExpIds.SelectStatement)
221
+ return SelectStatementExpr.deserialize(node);
222
+ throw new Error(`unknow node.id = ${node.id}`);
223
+ }
224
+ }
225
+ exports.StatementExpr = StatementExpr;
226
+ /**
227
+ * Base class for all computed expression like Value and Boolean.
228
+ * @alpha
229
+ */
230
+ class ComputedExpr extends Expr {
231
+ static deserialize(node) {
232
+ if (BooleanExpr.deserializableIds.includes(node.id)) {
233
+ return BooleanExpr.deserialize(node);
234
+ }
235
+ if (ValueExpr.deserializableIds.includes(node.id)) {
236
+ return ValueExpr.deserialize(node);
237
+ }
238
+ throw new Error(`unknow node.id = ${node.id}`);
239
+ }
240
+ }
241
+ exports.ComputedExpr = ComputedExpr;
242
+ /**
243
+ * Base class for a boolean expressions. It has following subclasses
244
+ * @see [[BooleanExpr]]
245
+ * @see [[ubqueryTestExpr]]
246
+ * @see [[BetweenExpr]]
247
+ * @see [[LikeExpr]]
248
+ * @see [[InExpr]]
249
+ * @see [[IsNullExpr]]
250
+ * @see [[IsOfTypeExpr]]
251
+ * @see [[NotExpr]]
252
+ * @see [[BinaryBooleanExpr]]
253
+ * @alpha
254
+ */
255
+ class BooleanExpr extends ComputedExpr {
256
+ static deserialize(node) {
257
+ if (node.id === NativeExpIds.BinaryBoolean) {
258
+ const op = node.op;
259
+ // if (MatchExpr.parseOp(op)[0]) {
260
+ // return MatchExpr.deserialize(node);
261
+ // }
262
+ if (BetweenExpr.parseOp(op)[0]) {
263
+ return BetweenExpr.deserialize(node);
264
+ }
265
+ if (LikeExpr.parseOp(op)[0]) {
266
+ return LikeExpr.deserialize(node);
267
+ }
268
+ if (InExpr.parseOp(op)[0]) {
269
+ return InExpr.deserialize(node);
270
+ }
271
+ if (IsNullExpr.parseOp(node)[0]) {
272
+ return IsNullExpr.deserialize(node);
273
+ }
274
+ if (IsOfTypeExpr.parseOp(node)[0]) {
275
+ return IsOfTypeExpr.deserialize(node);
276
+ }
277
+ return BinaryBooleanExpr.deserialize(node);
278
+ }
279
+ if (node.id === NativeExpIds.BooleanFactor) {
280
+ return NotExpr.deserialize(node);
281
+ }
282
+ if (node.id === NativeExpIds.SubqueryTest) {
283
+ return SubqueryTestExpr.deserialize(node);
284
+ }
285
+ if (node.id === NativeExpIds.BooleanFactor) {
286
+ return NotExpr.deserialize(node);
287
+ }
288
+ throw new Error(`Unknown type of native value exp ${node.id}`);
289
+ }
290
+ }
291
+ BooleanExpr.deserializableIds = [NativeExpIds.BinaryBoolean, NativeExpIds.BooleanFactor, NativeExpIds.SubqueryTest, NativeExpIds.AllOrAny, NativeExpIds.BooleanFactor];
292
+ exports.BooleanExpr = BooleanExpr;
293
+ /**
294
+ * Base class for all value expressions. Following is list of subclasses.
295
+ * @see [[SubqueryExpr]]
296
+ * @see [[ValueExpr]]
297
+ * @see [[UnaryValueExpr]]
298
+ * @see [[FuncCallExpr]]
299
+ * @see [[CastExpr]]
300
+ * @see [[BinaryValueExpr]]
301
+ * @see [[SearchCaseExpr]]
302
+ * @see [[IIFExpr]]
303
+ * @see [[LiteralExpr]]
304
+ * @see [[PropertyNameExpr]]
305
+ * @alpha
306
+ */
307
+ class ValueExpr extends ComputedExpr {
308
+ static deserialize(node) {
309
+ if (node.id === NativeExpIds.UnaryValue) {
310
+ return UnaryValueExpr.deserialize(node);
311
+ }
312
+ if (node.id === NativeExpIds.PropertyName) {
313
+ return PropertyNameExpr.deserialize(node);
314
+ }
315
+ if (node.id === NativeExpIds.Parameter) {
316
+ return ParameterExpr.deserialize(node);
317
+ }
318
+ if (node.id === NativeExpIds.FunctionCall) {
319
+ return FuncCallExpr.deserialize(node);
320
+ }
321
+ if (node.id === NativeExpIds.Cast) {
322
+ return CastExpr.deserialize(node);
323
+ }
324
+ if (node.id === NativeExpIds.Subquery) {
325
+ return SubqueryExpr.deserialize(node);
326
+ }
327
+ if (node.id === NativeExpIds.BinaryValue) {
328
+ return BinaryValueExpr.deserialize(node);
329
+ }
330
+ if (node.id === NativeExpIds.SearchCaseValue) {
331
+ return SearchCaseExpr.deserialize(node);
332
+ }
333
+ if (node.id === NativeExpIds.IIF) {
334
+ return IIFExpr.deserialize(node);
335
+ }
336
+ if (node.id === NativeExpIds.LiteralValue) {
337
+ return LiteralExpr.deserialize(node);
338
+ }
339
+ throw new Error(`Unknown type of native value exp ${node.id}`);
340
+ }
341
+ }
342
+ ValueExpr.deserializableIds = [
343
+ NativeExpIds.LiteralValue,
344
+ NativeExpIds.Parameter,
345
+ NativeExpIds.FunctionCall,
346
+ NativeExpIds.Cast,
347
+ NativeExpIds.BinaryValue,
348
+ NativeExpIds.SearchCaseValue,
349
+ NativeExpIds.IIF,
350
+ NativeExpIds.UnaryValue,
351
+ NativeExpIds.PropertyName,
352
+ NativeExpIds.Subquery,
353
+ ];
354
+ exports.ValueExpr = ValueExpr;
355
+ /**
356
+ * Base class for expressions that can be used in FROM clause of a SELECT. Following list of this subclasses.
357
+ * @see [[ClassRefExpr]]
358
+ * @see [[ClassNameExpr]]
359
+ * @see [[SubqueryRefExpr]]
360
+ * @see [[UsingRelationshipJoinExpr]]
361
+ * @see [[QualifiedJoinExpr]]
362
+ * @see [[CteBlockRefExpr]]
363
+ * @see [[TableValuedFuncExpr]]
364
+ * @alpha
365
+ */
366
+ class ClassRefExpr extends Expr {
367
+ static deserialize(node) {
368
+ if (node.id === NativeExpIds.ClassName) {
369
+ return ClassNameExpr.deserialize(node);
370
+ }
371
+ if (node.id === NativeExpIds.SubqueryRef) {
372
+ return SubqueryRefExpr.deserialize(node);
373
+ }
374
+ if (node.id === NativeExpIds.UsingRelationshipJoinExp) {
375
+ return UsingRelationshipJoinExpr.deserialize(node);
376
+ }
377
+ if (node.id === NativeExpIds.QualifiedJoin) {
378
+ return QualifiedJoinExpr.deserialize(node);
379
+ }
380
+ if (node.id === NativeExpIds.CommonTableBlockName) {
381
+ return CteBlockRefExpr.deserialize(node);
382
+ }
383
+ if (node.id === NativeExpIds.TableValuedFunction) {
384
+ return TableValuedFuncExpr.deserialize(node);
385
+ }
386
+ throw new Error(`Unknown type of native value exp ${node.id}`);
387
+ }
388
+ }
389
+ ClassRefExpr.deserializableIds = [
390
+ NativeExpIds.ClassName,
391
+ NativeExpIds.SubqueryRef,
392
+ NativeExpIds.UsingRelationshipJoinExp,
393
+ NativeExpIds.QualifiedJoin,
394
+ NativeExpIds.CommonTableBlockName,
395
+ ];
396
+ exports.ClassRefExpr = ClassRefExpr;
397
+ /**
398
+ * Write expression tree to string
399
+ * @alpha
400
+ */
401
+ class ECSqlWriter {
402
+ constructor(options = {
403
+ multiline: false,
404
+ spaceAfterComma: true,
405
+ spaceAroundBinOp: true,
406
+ eol: "\r\n",
407
+ keywordCasing: "UPPER",
408
+ indent: { size: 3, char: " " },
409
+ }) {
410
+ this.options = options;
411
+ this._tokens = [];
412
+ this._currentIndent = 0;
413
+ this._isNewLine = false;
414
+ }
415
+ indent() {
416
+ this._currentIndent++;
417
+ }
418
+ unindent() {
419
+ if (this._currentIndent > 0)
420
+ this._currentIndent--;
421
+ }
422
+ appendBinaryOp(val) {
423
+ if (this.options.spaceAroundBinOp)
424
+ return this.append(` ${val} `);
425
+ return this.append(val);
426
+ }
427
+ appendKeyword(val) {
428
+ if (this.options.keywordCasing === "UPPER")
429
+ return this.append(val);
430
+ return this.append(val.toLowerCase());
431
+ }
432
+ appendComma() {
433
+ this.append(",");
434
+ if (this.options.spaceAfterComma)
435
+ this.appendSpace();
436
+ }
437
+ appendQuoted(val) {
438
+ return this.append(`[${val}]`);
439
+ }
440
+ append(val) {
441
+ if (this._isNewLine) {
442
+ if (this._currentIndent > 0 && this.options.indent.size > 0 && this.options.indent.char.length === 1) {
443
+ this._tokens.push("".padEnd(this._currentIndent * this.options.indent.size, this.options.indent.char));
444
+ }
445
+ this._isNewLine = false;
446
+ }
447
+ this._tokens.push(val);
448
+ return this;
449
+ }
450
+ appendStringLiteral(val) {
451
+ return this.append(`'${val}'`);
452
+ }
453
+ appendLineOrSpace() {
454
+ if (this.options.multiline)
455
+ return this.appendLine();
456
+ return this.appendSpace();
457
+ }
458
+ appendSpace() {
459
+ return this.append(" ");
460
+ }
461
+ appendLine() {
462
+ if (this.options.multiline) {
463
+ this._isNewLine = true;
464
+ if (this._tokens.length > 0) {
465
+ this._tokens[this._tokens.length - 1] = this._tokens[this._tokens.length - 1].trimEnd();
466
+ }
467
+ this._tokens.push(this.options.eol);
468
+ }
469
+ return this;
470
+ }
471
+ clear() {
472
+ this._tokens = [];
473
+ return this;
474
+ }
475
+ squash() {
476
+ if (this._tokens.length > 1) {
477
+ this._tokens = [this.toString()];
478
+ }
479
+ return this;
480
+ }
481
+ toString() {
482
+ return this._tokens.join("");
483
+ }
484
+ appendExp(exp) {
485
+ exp.writeTo(this);
486
+ return this;
487
+ }
488
+ }
489
+ exports.ECSqlWriter = ECSqlWriter;
490
+ /**
491
+ * Use to describe selection clause terms in a SELECT statements @see [[SelectionClauseExpr]] @see [[SelectExpr]]
492
+ * @alpha
493
+ */
494
+ class DerivedPropertyExpr extends Expr {
495
+ constructor(computedExpr, alias) {
496
+ super(DerivedPropertyExpr.type);
497
+ this.computedExpr = computedExpr;
498
+ this.alias = alias;
499
+ }
500
+ get children() {
501
+ return [this.computedExpr];
502
+ }
503
+ static deserialize(node) {
504
+ if (node.id !== NativeExpIds.DerivedProperty) {
505
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.DerivedProperty'. ${JSON.stringify(node)}`);
506
+ }
507
+ return new DerivedPropertyExpr(ComputedExpr.deserialize(node.exp), node.alias ? node.alias : undefined);
508
+ }
509
+ writeTo(writer) {
510
+ writer.appendExp(this.computedExpr);
511
+ if (this.alias) {
512
+ writer.appendSpace();
513
+ writer.appendQuoted(this.alias);
514
+ }
515
+ }
516
+ }
517
+ DerivedPropertyExpr.type = ExprType.DerivedProperty;
518
+ exports.DerivedPropertyExpr = DerivedPropertyExpr;
519
+ /**
520
+ * Describes a ECSQL delete statement.
521
+ * @alpha
522
+ */
523
+ class DeleteStatementExpr extends StatementExpr {
524
+ constructor(className, where, options) {
525
+ super(DeleteStatementExpr.type);
526
+ this.className = className;
527
+ this.where = where;
528
+ this.options = options;
529
+ }
530
+ get children() {
531
+ const exprs = [this.className];
532
+ if (this.where)
533
+ exprs.push(this.where);
534
+ if (this.options)
535
+ exprs.push(this.options);
536
+ return exprs;
537
+ }
538
+ static deserialize(node) {
539
+ if (node.id !== NativeExpIds.DeleteStatement) {
540
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.DeleteStatement'. ${JSON.stringify(node)}`);
541
+ }
542
+ const className = ClassNameExpr.deserialize(node.class);
543
+ const where = node.where ? WhereClauseExp.deserialize(node.where) : undefined;
544
+ const options = node.options ? ECSqlOptionsClauseExpr.deserialize(node.options) : undefined;
545
+ return new DeleteStatementExpr(className, where, options);
546
+ }
547
+ writeTo(writer) {
548
+ writer.appendKeyword("DELETE");
549
+ writer.appendSpace();
550
+ writer.appendKeyword("FROM");
551
+ writer.appendSpace();
552
+ writer.appendExp(this.className);
553
+ if (this.where) {
554
+ writer.appendSpace();
555
+ writer.appendExp(this.where);
556
+ }
557
+ if (this.options) {
558
+ writer.appendSpace();
559
+ writer.appendExp(this.options);
560
+ }
561
+ }
562
+ }
563
+ DeleteStatementExpr.type = ExprType.DeleteStatement;
564
+ exports.DeleteStatementExpr = DeleteStatementExpr;
565
+ /**
566
+ * Describe a ECSQL Insert statement.
567
+ * @alpha
568
+ */
569
+ class InsertStatementExpr extends StatementExpr {
570
+ constructor(className, values, propertyNames) {
571
+ super(InsertStatementExpr.type);
572
+ this.className = className;
573
+ this.values = values;
574
+ this.propertyNames = propertyNames;
575
+ }
576
+ get children() {
577
+ const exprs = [this.className];
578
+ exprs.push(...this.values);
579
+ if (this.propertyNames)
580
+ exprs.push(...this.propertyNames);
581
+ return exprs;
582
+ }
583
+ static deserialize(node) {
584
+ if (node.id !== NativeExpIds.InsertStatement) {
585
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.InsertStatement'. ${JSON.stringify(node)}`);
586
+ }
587
+ const className = ClassNameExpr.deserialize(node.class);
588
+ if (className.polymorphicInfo) {
589
+ // Patch as INSERT are always ONLY but parser have issue accepting ONLY.
590
+ if (className.polymorphicInfo.allOrAny === "ONLY") {
591
+ className.polymorphicInfo = undefined;
592
+ }
593
+ }
594
+ const values = Array.from(node.values.map((v) => ValueExpr.deserialize(v)));
595
+ const properties = node.properties ? Array.from(node.properties.map((v) => PropertyNameExpr.deserialize(v))) : undefined;
596
+ return new InsertStatementExpr(className, values, properties);
597
+ }
598
+ writeTo(writer) {
599
+ writer.appendKeyword("INSERT");
600
+ writer.appendSpace();
601
+ writer.appendKeyword("INTO");
602
+ writer.appendSpace();
603
+ writer.appendExp(this.className);
604
+ writer.appendSpace();
605
+ if (this.propertyNames) {
606
+ writer.append("(");
607
+ this.propertyNames.forEach((v, i) => {
608
+ if (i > 0) {
609
+ writer.appendComma();
610
+ }
611
+ writer.appendExp(v);
612
+ });
613
+ writer.append(")");
614
+ }
615
+ writer.appendSpace();
616
+ writer.appendKeyword("VALUES");
617
+ writer.append("(");
618
+ this.values.forEach((v, i) => {
619
+ if (i > 0) {
620
+ writer.appendComma();
621
+ }
622
+ writer.appendExp(v);
623
+ });
624
+ writer.append(")");
625
+ }
626
+ }
627
+ InsertStatementExpr.type = ExprType.InsertStatement;
628
+ exports.InsertStatementExpr = InsertStatementExpr;
629
+ /**
630
+ * Describes a JOIN clause e.g. <classNameExpr> JOIN <classNameExpr> ON <joinspec>
631
+ * @alpha
632
+ */
633
+ class QualifiedJoinExpr extends ClassRefExpr {
634
+ constructor(joinType, from, to, spec) {
635
+ super(QualifiedJoinExpr.type);
636
+ this.joinType = joinType;
637
+ this.from = from;
638
+ this.to = to;
639
+ this.spec = spec;
640
+ }
641
+ get children() {
642
+ const exprs = [this.from, this.to];
643
+ if (this.spec instanceof BooleanExpr)
644
+ exprs.push(this.spec);
645
+ return exprs;
646
+ }
647
+ static deserialize(node) {
648
+ if (node.id !== NativeExpIds.QualifiedJoin) {
649
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.QualifiedJoin'. ${JSON.stringify(node)}`);
650
+ }
651
+ const type = node.type;
652
+ const from = ClassRefExpr.deserialize(node.from);
653
+ const to = ClassRefExpr.deserialize(node.to);
654
+ let spec;
655
+ if (Array.isArray(node.spec))
656
+ spec = node.spec;
657
+ else
658
+ spec = BooleanExpr.deserialize(node.spec);
659
+ return new QualifiedJoinExpr(type, from, to, spec);
660
+ }
661
+ writeTo(writer) {
662
+ writer.appendExp(this.from);
663
+ writer.appendSpace();
664
+ if (this.joinType === JoinType.LeftOuter) {
665
+ writer.appendKeyword("LEFT").appendSpace();
666
+ writer.appendKeyword("OUTER").appendSpace();
667
+ writer.appendKeyword("JOIN").appendSpace();
668
+ }
669
+ else if (this.joinType === JoinType.RightOuter) {
670
+ writer.appendKeyword("RIGHT").appendSpace();
671
+ writer.appendKeyword("OUTER").appendSpace();
672
+ writer.appendKeyword("JOIN").appendSpace();
673
+ }
674
+ else if (this.joinType === JoinType.FullOuter) {
675
+ writer.appendKeyword("FULL").appendSpace();
676
+ writer.appendKeyword("OUTER").appendSpace();
677
+ writer.appendKeyword("JOIN").appendSpace();
678
+ }
679
+ else if (this.joinType === JoinType.Inner) {
680
+ writer.appendKeyword("INNER").appendSpace();
681
+ writer.appendKeyword("JOIN").appendSpace();
682
+ }
683
+ else {
684
+ throw new Error(`not supported join type ${this.joinType}`);
685
+ }
686
+ writer.appendExp(this.to);
687
+ if (this.spec) {
688
+ writer.appendSpace();
689
+ if (this.spec instanceof BooleanExpr) {
690
+ writer.appendKeyword("ON");
691
+ writer.appendSpace();
692
+ writer.appendExp(this.spec);
693
+ }
694
+ else if (this.spec instanceof Array) {
695
+ writer.appendKeyword("USING");
696
+ this.spec.forEach((v, i) => {
697
+ if (i > 0) {
698
+ writer.appendComma();
699
+ writer.append(v);
700
+ }
701
+ });
702
+ }
703
+ else {
704
+ throw new Error("unknow join spec");
705
+ }
706
+ }
707
+ }
708
+ }
709
+ QualifiedJoinExpr.type = ExprType.QualifiedJoin;
710
+ exports.QualifiedJoinExpr = QualifiedJoinExpr;
711
+ /**
712
+ * Describe a JOIN USING clause.
713
+ * @alpha
714
+ */
715
+ class UsingRelationshipJoinExpr extends ClassRefExpr {
716
+ constructor(fromClassName, toClassName, toRelClassName, direction) {
717
+ super(UsingRelationshipJoinExpr.type);
718
+ this.fromClassName = fromClassName;
719
+ this.toClassName = toClassName;
720
+ this.toRelClassName = toRelClassName;
721
+ this.direction = direction;
722
+ }
723
+ get children() {
724
+ return [this.fromClassName, this.toClassName, this.toRelClassName];
725
+ }
726
+ static deserialize(node) {
727
+ if (node.id !== NativeExpIds.UsingRelationshipJoinExp) {
728
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.UsingRelationshipJoinExp'. ${JSON.stringify(node)}`);
729
+ }
730
+ const from = ClassRefExpr.deserialize(node.from);
731
+ const to = ClassNameExpr.deserialize(node.to);
732
+ const usingRel = ClassNameExpr.deserialize(node.using);
733
+ const direction = node.direction ? node.direction : undefined;
734
+ return new UsingRelationshipJoinExpr(from, to, usingRel, direction);
735
+ }
736
+ writeTo(writer) {
737
+ writer.appendExp(this.fromClassName);
738
+ writer.appendSpace();
739
+ writer.appendKeyword("JOIN");
740
+ writer.appendSpace();
741
+ writer.appendExp(this.toClassName);
742
+ writer.appendSpace();
743
+ writer.appendKeyword("USING");
744
+ writer.appendSpace();
745
+ writer.appendExp(this.toRelClassName);
746
+ if (this.direction) {
747
+ writer.appendSpace();
748
+ writer.appendKeyword(this.direction);
749
+ }
750
+ }
751
+ }
752
+ UsingRelationshipJoinExpr.type = ExprType.UsingRelationshipJoin;
753
+ exports.UsingRelationshipJoinExpr = UsingRelationshipJoinExpr;
754
+ /**
755
+ * Describe subquery result test e.g. EXISTS(<subquery>)
756
+ * @alpha
757
+ */
758
+ class SubqueryTestExpr extends BooleanExpr {
759
+ constructor(op, query) {
760
+ super(SubqueryTestExpr.type);
761
+ this.op = op;
762
+ this.query = query;
763
+ }
764
+ get children() {
765
+ return [this.query];
766
+ }
767
+ static deserialize(node) {
768
+ if (node.id !== NativeExpIds.SubqueryTest) {
769
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.SubqueryTest'. ${JSON.stringify(node)}`);
770
+ }
771
+ const query = SubqueryExpr.deserialize(node.query);
772
+ const op = node.op;
773
+ return new SubqueryTestExpr(op, query);
774
+ }
775
+ writeTo(writer) {
776
+ writer.appendKeyword(this.op);
777
+ writer.appendExp(this.query);
778
+ }
779
+ }
780
+ SubqueryTestExpr.type = ExprType.SubqueryTest;
781
+ exports.SubqueryTestExpr = SubqueryTestExpr;
782
+ /**
783
+ * Describe a subquery when used in FROM clause.
784
+ * @alpha
785
+ */
786
+ class SubqueryRefExpr extends ClassRefExpr {
787
+ constructor(query, polymorphicInfo, alias) {
788
+ super(SubqueryRefExpr.type);
789
+ this.query = query;
790
+ this.polymorphicInfo = polymorphicInfo;
791
+ this.alias = alias;
792
+ }
793
+ get children() {
794
+ return [this.query];
795
+ }
796
+ static deserialize(node) {
797
+ if (node.id !== NativeExpIds.SubqueryRef) {
798
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.SubqueryRef'. ${JSON.stringify(node)}`);
799
+ }
800
+ const query = SubqueryExpr.deserialize(node.query);
801
+ const polymorphicInfo = node.polymorphicInfo ? node.polymorphicInfo : undefined;
802
+ const alias = node.alias ? node.alias : undefined;
803
+ return new SubqueryRefExpr(query, polymorphicInfo, alias);
804
+ }
805
+ writeTo(writer) {
806
+ if (this.polymorphicInfo) {
807
+ if (this.polymorphicInfo.disqualify) {
808
+ writer.append(this.polymorphicInfo.disqualify);
809
+ }
810
+ writer.appendKeyword(this.polymorphicInfo.allOrAny);
811
+ writer.appendSpace();
812
+ }
813
+ writer.appendExp(this.query);
814
+ if (this.alias) {
815
+ writer.appendSpace();
816
+ writer.appendQuoted(this.alias);
817
+ }
818
+ }
819
+ }
820
+ SubqueryRefExpr.type = ExprType.SubqueryRef;
821
+ exports.SubqueryRefExpr = SubqueryRefExpr;
822
+ /**
823
+ * Describe a optionally compound SELECT statement.
824
+ * @alpha
825
+ */
826
+ class SelectStatementExpr extends StatementExpr {
827
+ constructor(singleSelect, nextSelect) {
828
+ super(SelectStatementExpr.type);
829
+ this.singleSelect = singleSelect;
830
+ this.nextSelect = nextSelect;
831
+ }
832
+ get children() {
833
+ const exprs = [this.singleSelect];
834
+ if (this.nextSelect)
835
+ exprs.push(this.nextSelect.select);
836
+ return exprs;
837
+ }
838
+ static deserialize(node) {
839
+ if (node.id !== NativeExpIds.SelectStatement) {
840
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.SelectStatement'. ${JSON.stringify(node)}`);
841
+ }
842
+ const singleSelect = SelectExpr.deserialize(node.select);
843
+ let nextSelect;
844
+ if (node.nextBlock) {
845
+ nextSelect = {
846
+ op: node.nextBlock.combineOp,
847
+ select: SelectStatementExpr.deserialize(node.nextBlock.select),
848
+ };
849
+ }
850
+ return new SelectStatementExpr(singleSelect, nextSelect);
851
+ }
852
+ writeTo(writer) {
853
+ writer.appendExp(this.singleSelect);
854
+ if (this.nextSelect) {
855
+ writer.appendSpace();
856
+ if (this.nextSelect.op === "UNION ALL") {
857
+ writer.appendKeyword("UNION");
858
+ writer.appendSpace();
859
+ writer.appendKeyword("ALL");
860
+ }
861
+ else {
862
+ writer.append(this.nextSelect.op);
863
+ }
864
+ writer.appendSpace();
865
+ writer.appendExp(this.nextSelect.select);
866
+ }
867
+ }
868
+ }
869
+ SelectStatementExpr.type = ExprType.SelectStatement;
870
+ exports.SelectStatementExpr = SelectStatementExpr;
871
+ /**
872
+ * Describe selection in a SELECT query
873
+ * @alpha
874
+ */
875
+ class SelectionClauseExpr extends Expr {
876
+ constructor(derivedPropertyList) {
877
+ super(SelectionClauseExpr.type);
878
+ this.derivedPropertyList = derivedPropertyList;
879
+ }
880
+ get children() {
881
+ return [...this.derivedPropertyList];
882
+ }
883
+ static deserialize(node) {
884
+ if (!Array.isArray(node)) {
885
+ throw new Error(`Expect node to be array of NativeECSqlParseNode[] ${JSON.stringify(node)}`);
886
+ }
887
+ return new SelectionClauseExpr(Array.from(node.map((v) => DerivedPropertyExpr.deserialize(v))));
888
+ }
889
+ writeTo(writer) {
890
+ this.derivedPropertyList.forEach((v, i) => {
891
+ if (i > 0) {
892
+ writer.appendComma();
893
+ }
894
+ writer.appendExp(v);
895
+ });
896
+ }
897
+ }
898
+ SelectionClauseExpr.type = ExprType.SelectionClause;
899
+ exports.SelectionClauseExpr = SelectionClauseExpr;
900
+ /**
901
+ * Describe a GROUP BY clause in a SELECT statement.
902
+ * @alpha
903
+ */
904
+ class GroupByClauseExpr extends Expr {
905
+ constructor(exprList) {
906
+ super(GroupByClauseExpr.type);
907
+ this.exprList = exprList;
908
+ }
909
+ get children() {
910
+ return [...this.exprList];
911
+ }
912
+ static deserialize(node) {
913
+ if (!Array.isArray(node)) {
914
+ throw new Error(`Expect node to be array of NativeECSqlParseNode[] ${JSON.stringify(node)}`);
915
+ }
916
+ return new GroupByClauseExpr(Array.from(node.map((v) => ValueExpr.deserialize(v))));
917
+ }
918
+ writeTo(writer) {
919
+ writer.appendKeyword("GROUP");
920
+ writer.appendSpace();
921
+ writer.appendKeyword("BY");
922
+ writer.appendSpace();
923
+ this.exprList.forEach((v, i) => {
924
+ if (i > 0) {
925
+ writer.appendComma();
926
+ }
927
+ writer.appendExp(v);
928
+ });
929
+ }
930
+ }
931
+ GroupByClauseExpr.type = ExprType.GroupByClause;
932
+ exports.GroupByClauseExpr = GroupByClauseExpr;
933
+ /**
934
+ * Describe a HAVING clause in a SELECT statement.
935
+ * @alpha
936
+ */
937
+ class HavingClauseExpr extends Expr {
938
+ constructor(filterExpr) {
939
+ super(HavingClauseExpr.type);
940
+ this.filterExpr = filterExpr;
941
+ }
942
+ get children() {
943
+ return [this.filterExpr];
944
+ }
945
+ static deserialize(node) {
946
+ if (typeof node !== "object") {
947
+ throw new Error(`Expect node to be array of NativeECSqlParseNode ${JSON.stringify(node)}`);
948
+ }
949
+ return new HavingClauseExpr(BooleanExpr.deserialize(node));
950
+ }
951
+ writeTo(writer) {
952
+ writer.appendKeyword("HAVING");
953
+ writer.appendSpace();
954
+ writer.appendExp(this.filterExpr);
955
+ }
956
+ }
957
+ HavingClauseExpr.type = ExprType.HavingClause;
958
+ exports.HavingClauseExpr = HavingClauseExpr;
959
+ /**
960
+ * Describe a FROM clause in a SELECT statement.
961
+ * @alpha
962
+ */
963
+ class FromClauseExpr extends Expr {
964
+ constructor(classRefs) {
965
+ super(FromClauseExpr.type);
966
+ this.classRefs = classRefs;
967
+ }
968
+ get children() {
969
+ return [...this.classRefs];
970
+ }
971
+ static deserialize(node) {
972
+ if (!Array.isArray(node)) {
973
+ throw new Error(`Expect node to be array of NativeECSqlParseNode[] ${JSON.stringify(node)}`);
974
+ }
975
+ return new FromClauseExpr(Array.from(node.map((v) => ClassRefExpr.deserialize(v))));
976
+ }
977
+ writeTo(writer) {
978
+ writer.appendKeyword("FROM");
979
+ writer.appendSpace();
980
+ this.classRefs.forEach((v, i) => {
981
+ if (i > 0) {
982
+ writer.appendComma();
983
+ }
984
+ writer.appendExp(v);
985
+ });
986
+ }
987
+ }
988
+ FromClauseExpr.type = ExprType.FromClause;
989
+ exports.FromClauseExpr = FromClauseExpr;
990
+ /**
991
+ * Describe a WHERE clause in a SELECT, UPDATE and DELETE statement.
992
+ * @alpha
993
+ */
994
+ class WhereClauseExp extends Expr {
995
+ constructor(filterExpr) {
996
+ super(WhereClauseExp.type);
997
+ this.filterExpr = filterExpr;
998
+ }
999
+ get children() {
1000
+ return [this.filterExpr];
1001
+ }
1002
+ static deserialize(node) {
1003
+ if (typeof node !== "object") {
1004
+ throw new Error(`Expect node to be array of NativeECSqlParseNode ${JSON.stringify(node)}`);
1005
+ }
1006
+ return new WhereClauseExp(BooleanExpr.deserialize(node));
1007
+ }
1008
+ writeTo(writer) {
1009
+ writer.appendKeyword("WHERE");
1010
+ writer.appendSpace();
1011
+ writer.appendExp(this.filterExpr);
1012
+ }
1013
+ }
1014
+ WhereClauseExp.type = ExprType.WhereClause;
1015
+ exports.WhereClauseExp = WhereClauseExp;
1016
+ /**
1017
+ * Describe a single sorted term in a ORDER BY clause of a SELECT statement.
1018
+ * @alpha
1019
+ */
1020
+ class OrderBySpecExpr extends Expr {
1021
+ constructor(term, sortDirection, nulls) {
1022
+ super(OrderBySpecExpr.type);
1023
+ this.term = term;
1024
+ this.sortDirection = sortDirection;
1025
+ this.nulls = nulls;
1026
+ }
1027
+ get children() {
1028
+ return [this.term];
1029
+ }
1030
+ static deserialize(node) {
1031
+ if (typeof node !== "object") {
1032
+ throw new Error(`Expect node to be array of NativeECSqlParseNode ${JSON.stringify(node)}`);
1033
+ }
1034
+ return new OrderBySpecExpr(ValueExpr.deserialize(node.exp), node.direction ? node.direction : undefined);
1035
+ }
1036
+ writeTo(writer) {
1037
+ writer.appendExp(this.term);
1038
+ if (this.sortDirection) {
1039
+ writer.appendSpace();
1040
+ writer.appendKeyword(this.sortDirection);
1041
+ }
1042
+ if (this.nulls) {
1043
+ writer.appendSpace();
1044
+ writer.appendKeyword("NULLS");
1045
+ writer.appendSpace();
1046
+ writer.appendKeyword(this.nulls);
1047
+ }
1048
+ }
1049
+ }
1050
+ OrderBySpecExpr.type = ExprType.OrderBySpec;
1051
+ exports.OrderBySpecExpr = OrderBySpecExpr;
1052
+ /**
1053
+ * Describe a ORDER BY clause in a SELECT statement.
1054
+ * @alpha
1055
+ */
1056
+ class OrderByClauseExpr extends Expr {
1057
+ constructor(terms) {
1058
+ super(OrderByClauseExpr.type);
1059
+ this.terms = terms;
1060
+ }
1061
+ get children() {
1062
+ return [...this.terms.map((v) => v.term)];
1063
+ }
1064
+ static deserialize(node) {
1065
+ if (!Array.isArray(node)) {
1066
+ throw new Error(`Expect node to be array of NativeECSqlParseNode[] ${JSON.stringify(node)}`);
1067
+ }
1068
+ return new OrderByClauseExpr(Array.from(node.map((v) => OrderBySpecExpr.deserialize(v))));
1069
+ }
1070
+ writeTo(writer) {
1071
+ writer.appendKeyword("ORDER");
1072
+ writer.appendSpace();
1073
+ writer.appendKeyword("BY");
1074
+ writer.appendSpace();
1075
+ this.terms.forEach((v, i) => {
1076
+ if (i > 0) {
1077
+ writer.appendComma();
1078
+ }
1079
+ writer.appendExp(v);
1080
+ });
1081
+ }
1082
+ }
1083
+ OrderByClauseExpr.type = ExprType.OrderByClause;
1084
+ exports.OrderByClauseExpr = OrderByClauseExpr;
1085
+ /**
1086
+ * Describe a LIMIT clause in a SELECT statement.
1087
+ * @alpha
1088
+ */
1089
+ class LimitClauseExpr extends Expr {
1090
+ constructor(limit, offset) {
1091
+ super(LimitClauseExpr.type);
1092
+ this.limit = limit;
1093
+ this.offset = offset;
1094
+ }
1095
+ get children() {
1096
+ const exprs = [this.limit];
1097
+ if (this.offset)
1098
+ exprs.push(this.offset);
1099
+ return exprs;
1100
+ }
1101
+ static deserialize(node) {
1102
+ if (typeof node !== "object") {
1103
+ throw new Error(`Expect node to be array of NativeECSqlParseNode ${JSON.stringify(node)}`);
1104
+ }
1105
+ return new LimitClauseExpr(ValueExpr.deserialize(node.exp), node.offset ? ValueExpr.deserialize(node.offset) : undefined);
1106
+ }
1107
+ writeTo(writer) {
1108
+ writer.appendKeyword("LIMIT");
1109
+ writer.appendSpace();
1110
+ writer.appendExp(this.limit);
1111
+ if (this.offset) {
1112
+ writer.appendSpace();
1113
+ writer.appendKeyword("OFFSET");
1114
+ writer.appendSpace();
1115
+ writer.appendExp(this.offset);
1116
+ }
1117
+ }
1118
+ }
1119
+ LimitClauseExpr.type = ExprType.LimitClause;
1120
+ exports.LimitClauseExpr = LimitClauseExpr;
1121
+ /**
1122
+ * Describe a single select statement.
1123
+ * @alpha
1124
+ */
1125
+ class SelectExpr extends Expr {
1126
+ constructor(selection, rowQuantifier, from, where, groupBy, having, orderBy, limit, options) {
1127
+ super(SelectExpr.type);
1128
+ this.selection = selection;
1129
+ this.rowQuantifier = rowQuantifier;
1130
+ this.from = from;
1131
+ this.where = where;
1132
+ this.groupBy = groupBy;
1133
+ this.having = having;
1134
+ this.orderBy = orderBy;
1135
+ this.limit = limit;
1136
+ this.options = options;
1137
+ }
1138
+ get children() {
1139
+ const exprs = [this.selection];
1140
+ if (this.from)
1141
+ exprs.push(this.from);
1142
+ if (this.where)
1143
+ exprs.push(this.where);
1144
+ if (this.groupBy)
1145
+ exprs.push(this.groupBy);
1146
+ if (this.having)
1147
+ exprs.push(this.having);
1148
+ if (this.orderBy)
1149
+ exprs.push(this.orderBy);
1150
+ if (this.limit)
1151
+ exprs.push(this.limit);
1152
+ if (this.options)
1153
+ exprs.push(this.options);
1154
+ return exprs;
1155
+ }
1156
+ static deserialize(node) {
1157
+ if (node.id !== NativeExpIds.SingleSelectStatement && node.id !== NativeExpIds.RowConstructor) {
1158
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.SingleSelectStatement/RowConstructor'. ${JSON.stringify(node)}`);
1159
+ }
1160
+ if (node.id === NativeExpIds.RowConstructor) {
1161
+ const values = SelectionClauseExpr.deserialize(node.values);
1162
+ return new SelectExpr(values);
1163
+ }
1164
+ const selection = SelectionClauseExpr.deserialize(node.selection);
1165
+ const from = node.from ? FromClauseExpr.deserialize(node.from) : undefined;
1166
+ const where = node.where ? WhereClauseExp.deserialize(node.where) : undefined;
1167
+ const groupBy = node.groupBy ? GroupByClauseExpr.deserialize(node.groupBy) : undefined;
1168
+ const having = node.having ? HavingClauseExpr.deserialize(node.having) : undefined;
1169
+ const orderBy = node.orderBy ? OrderByClauseExpr.deserialize(node.orderBy) : undefined;
1170
+ const options = node.options ? ECSqlOptionsClauseExpr.deserialize(node.options) : undefined;
1171
+ const limitSpec = node.limit ? LimitClauseExpr.deserialize(node.limit) : undefined;
1172
+ const rowQuantifier = node.selectionType ? node.selectionType : undefined;
1173
+ return new SelectExpr(selection, rowQuantifier, from, where, groupBy, having, orderBy, limitSpec, options);
1174
+ }
1175
+ writeTo(writer) {
1176
+ writer.appendKeyword("SELECT");
1177
+ writer.appendSpace();
1178
+ if (this.rowQuantifier) {
1179
+ writer.appendKeyword(this.rowQuantifier);
1180
+ writer.appendSpace();
1181
+ }
1182
+ writer.appendExp(this.selection);
1183
+ if (this.from) {
1184
+ writer.appendSpace();
1185
+ writer.appendExp(this.from);
1186
+ }
1187
+ if (this.where) {
1188
+ writer.appendSpace();
1189
+ writer.appendExp(this.where);
1190
+ }
1191
+ if (this.groupBy) {
1192
+ writer.appendSpace();
1193
+ writer.appendExp(this.groupBy);
1194
+ }
1195
+ if (this.having) {
1196
+ writer.appendSpace();
1197
+ writer.appendExp(this.having);
1198
+ }
1199
+ if (this.orderBy) {
1200
+ writer.appendSpace();
1201
+ writer.appendExp(this.orderBy);
1202
+ }
1203
+ if (this.limit) {
1204
+ writer.appendSpace();
1205
+ writer.appendExp(this.limit);
1206
+ }
1207
+ if (this.options) {
1208
+ writer.appendSpace();
1209
+ writer.appendExp(this.options);
1210
+ }
1211
+ }
1212
+ }
1213
+ SelectExpr.type = ExprType.Select;
1214
+ exports.SelectExpr = SelectExpr;
1215
+ /**
1216
+ * Describe a subquery when used as value. This kind of query expect to return one column and one one value.
1217
+ * @alpha
1218
+ */
1219
+ class SubqueryExpr extends ValueExpr {
1220
+ constructor(query) {
1221
+ super(SubqueryExpr.type);
1222
+ this.query = query;
1223
+ }
1224
+ get children() {
1225
+ return [this.query];
1226
+ }
1227
+ static deserialize(node) {
1228
+ if (node.id !== NativeExpIds.Subquery) {
1229
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.Subquery'. ${JSON.stringify(node)}`);
1230
+ }
1231
+ return new SubqueryExpr(SelectStatementExpr.deserialize(node.query));
1232
+ }
1233
+ writeTo(writer) {
1234
+ writer.append("(");
1235
+ writer.appendExp(this.query);
1236
+ writer.append(")");
1237
+ }
1238
+ }
1239
+ SubqueryExpr.type = ExprType.Subquery;
1240
+ exports.SubqueryExpr = SubqueryExpr;
1241
+ /**
1242
+ * Describe a binary boolean expression in ECSQL.
1243
+ * @alpha
1244
+ */
1245
+ class BinaryBooleanExpr extends BooleanExpr {
1246
+ constructor(op, lhsExpr, rhsExpr, not) {
1247
+ super(BinaryBooleanExpr.type);
1248
+ this.op = op;
1249
+ this.lhsExpr = lhsExpr;
1250
+ this.rhsExpr = rhsExpr;
1251
+ this.not = not;
1252
+ }
1253
+ get children() {
1254
+ return [this.lhsExpr, this.rhsExpr];
1255
+ }
1256
+ static deserialize(node) {
1257
+ if (node.id !== NativeExpIds.BinaryBoolean) {
1258
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.BinaryBoolean'. ${JSON.stringify(node)}`);
1259
+ }
1260
+ const op = node.op;
1261
+ return new BinaryBooleanExpr(op, ComputedExpr.deserialize(node.lhs), ComputedExpr.deserialize(node.rhs));
1262
+ }
1263
+ writeTo(writer) {
1264
+ writer.append("(");
1265
+ writer.appendExp(this.lhsExpr);
1266
+ writer.appendBinaryOp(this.op);
1267
+ writer.appendExp(this.rhsExpr);
1268
+ writer.append(")");
1269
+ }
1270
+ }
1271
+ BinaryBooleanExpr.type = ExprType.BinaryBoolean;
1272
+ exports.BinaryBooleanExpr = BinaryBooleanExpr;
1273
+ /**
1274
+ * Describe a <expr> IS NULL boolean expression
1275
+ * @alpha
1276
+ */
1277
+ class IsNullExpr extends BooleanExpr {
1278
+ constructor(operandExpr, not) {
1279
+ super(IsNullExpr.type);
1280
+ this.operandExpr = operandExpr;
1281
+ this.not = not;
1282
+ }
1283
+ get children() {
1284
+ return [this.operandExpr];
1285
+ }
1286
+ static parseOp(node) {
1287
+ const op = node.op;
1288
+ const isLiteral = node.rhs.op === NativeExpIds.LiteralValue;
1289
+ if (!isLiteral) {
1290
+ return [false, false];
1291
+ }
1292
+ const isNull = LiteralExpr.deserialize(node.rhs).rawValue === "NULL";
1293
+ return [op.startsWith("IS"), isNull];
1294
+ }
1295
+ static deserialize(node) {
1296
+ if (node.id !== NativeExpIds.BinaryBoolean) {
1297
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.BinaryBoolean'. ${JSON.stringify(node)}`);
1298
+ }
1299
+ const [isNullExp, isNull] = this.parseOp(node);
1300
+ if (!isNullExp) {
1301
+ throw new Error(`Parse node has 'node.op !== IS NULL'. ${JSON.stringify(node)}`);
1302
+ }
1303
+ const exp = ValueExpr.deserialize(node.lhs);
1304
+ return new IsNullExpr(exp, isNull ? "NOT" : undefined);
1305
+ }
1306
+ writeTo(writer) {
1307
+ writer.appendExp(this.operandExpr);
1308
+ writer.appendSpace();
1309
+ writer.appendKeyword("IS");
1310
+ writer.appendSpace();
1311
+ if (this.not) {
1312
+ writer.appendKeyword("NOT");
1313
+ writer.appendSpace();
1314
+ }
1315
+ writer.appendKeyword("NULL");
1316
+ }
1317
+ }
1318
+ IsNullExpr.type = ExprType.IsNull;
1319
+ exports.IsNullExpr = IsNullExpr;
1320
+ /**
1321
+ * Describe a <expr> IS (type1[, type2]) in ECSQL.
1322
+ * @alpha
1323
+ */
1324
+ class IsOfTypeExpr extends BooleanExpr {
1325
+ constructor(lhsExpr, typeNames, not) {
1326
+ super(IsOfTypeExpr.type);
1327
+ this.lhsExpr = lhsExpr;
1328
+ this.typeNames = typeNames;
1329
+ this.not = not;
1330
+ }
1331
+ get children() {
1332
+ return [this.lhsExpr, ...this.typeNames];
1333
+ }
1334
+ static parseOp(node) {
1335
+ const op = node.op;
1336
+ return [op.startsWith("IS") && Array.isArray(node.rhs), op.endsWith("NOT")];
1337
+ }
1338
+ static deserialize(node) {
1339
+ if (node.id !== NativeExpIds.BinaryBoolean) {
1340
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.BinaryBoolean'. ${JSON.stringify(node)}`);
1341
+ }
1342
+ const [isTypeOf, isNull] = this.parseOp(node);
1343
+ if (!isTypeOf) {
1344
+ throw new Error(`Parse node has 'node.op !== IS (type....)'. ${JSON.stringify(node)}`);
1345
+ }
1346
+ const exp = ValueExpr.deserialize(node.lhs);
1347
+ const classNames = Array.from(node.rhs.map((v) => ClassNameExpr.deserialize(v)));
1348
+ return new IsOfTypeExpr(exp, classNames, isNull ? "NOT" : undefined);
1349
+ }
1350
+ writeTo(writer) {
1351
+ writer.appendExp(this.lhsExpr);
1352
+ writer.appendSpace();
1353
+ writer.appendKeyword("IS");
1354
+ writer.appendSpace();
1355
+ if (this.not) {
1356
+ writer.appendKeyword("NOT");
1357
+ writer.appendSpace();
1358
+ }
1359
+ writer.append("(");
1360
+ this.typeNames.forEach((v, i) => {
1361
+ if (i > 0) {
1362
+ writer.appendComma();
1363
+ }
1364
+ writer.appendExp(v);
1365
+ });
1366
+ writer.append(")");
1367
+ }
1368
+ }
1369
+ IsOfTypeExpr.type = ExprType.IsOfType;
1370
+ exports.IsOfTypeExpr = IsOfTypeExpr;
1371
+ /**
1372
+ * Describe a NOT <expr> boolean expression
1373
+ * @alpha
1374
+ */
1375
+ class NotExpr extends BooleanExpr {
1376
+ constructor(operandExpr) {
1377
+ super(NotExpr.type);
1378
+ this.operandExpr = operandExpr;
1379
+ }
1380
+ get children() {
1381
+ return [this.operandExpr];
1382
+ }
1383
+ static deserialize(node) {
1384
+ if (node.id !== NativeExpIds.BooleanFactor) {
1385
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.BooleanFactor'. ${JSON.stringify(node)}`);
1386
+ }
1387
+ const exp = ComputedExpr.deserialize(node.exp);
1388
+ return new NotExpr(exp);
1389
+ }
1390
+ writeTo(writer) {
1391
+ writer.append("(");
1392
+ writer.appendKeyword("NOT");
1393
+ writer.appendSpace();
1394
+ writer.appendExp(this.operandExpr);
1395
+ writer.append(")");
1396
+ }
1397
+ }
1398
+ NotExpr.type = ExprType.Not;
1399
+ exports.NotExpr = NotExpr;
1400
+ /**
1401
+ * Describe a <expr> IN subquery|(val1[,val2...]) boolean expression
1402
+ * @alpha
1403
+ */
1404
+ class InExpr extends BooleanExpr {
1405
+ constructor(lhsExpr, rhsExpr, not) {
1406
+ super(InExpr.type);
1407
+ this.lhsExpr = lhsExpr;
1408
+ this.rhsExpr = rhsExpr;
1409
+ this.not = not;
1410
+ }
1411
+ get children() {
1412
+ const exprs = [this.lhsExpr];
1413
+ if (this.rhsExpr instanceof SubqueryExpr)
1414
+ exprs.push(this.rhsExpr);
1415
+ else {
1416
+ exprs.push(...this.rhsExpr);
1417
+ }
1418
+ return exprs;
1419
+ }
1420
+ static parseOp(op) {
1421
+ return [op.endsWith("IN"), op.startsWith("NOT ")];
1422
+ }
1423
+ static deserialize(node) {
1424
+ if (node.id !== NativeExpIds.BinaryBoolean) {
1425
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.BinaryBoolean'. ${JSON.stringify(node)}`);
1426
+ }
1427
+ const [isIn, isNull] = this.parseOp(node.op);
1428
+ if (!isIn) {
1429
+ throw new Error(`Parse node has 'node.op !== IN'. ${JSON.stringify(node)}`);
1430
+ }
1431
+ const lhs = ValueExpr.deserialize(node.lhs);
1432
+ if (Array.isArray(node.rhs))
1433
+ return new InExpr(lhs, Array.from(node.rhs.map((v) => ValueExpr.deserialize(v))), isNull ? "NOT" : undefined);
1434
+ else if (node.rhs.id === NativeExpIds.Subquery)
1435
+ return new InExpr(lhs, SubqueryExpr.deserialize(node.rhs), isNull ? "NOT" : undefined);
1436
+ else
1437
+ throw new Error(`unknown IN rhs ${node.rhs.id}`);
1438
+ }
1439
+ writeTo(writer) {
1440
+ writer.appendExp(this.lhsExpr);
1441
+ writer.appendSpace();
1442
+ if (this.not) {
1443
+ writer.appendKeyword("NOT");
1444
+ writer.appendSpace();
1445
+ }
1446
+ writer.appendKeyword("IN");
1447
+ writer.appendSpace();
1448
+ if (this.rhsExpr instanceof SubqueryExpr) {
1449
+ writer.appendExp(this.rhsExpr);
1450
+ }
1451
+ else if (Array.isArray(this.rhsExpr)) {
1452
+ writer.append("(");
1453
+ this.rhsExpr.forEach((v, i) => {
1454
+ if (i > 0) {
1455
+ writer.appendComma();
1456
+ }
1457
+ writer.appendExp(v);
1458
+ });
1459
+ writer.append(")");
1460
+ }
1461
+ else {
1462
+ throw new Error("unknown expression on rhs of IN expr.");
1463
+ }
1464
+ }
1465
+ }
1466
+ InExpr.type = ExprType.In;
1467
+ exports.InExpr = InExpr;
1468
+ /**
1469
+ * Describe a <expr> LIKE <expr> [ESCAPE <expr>] boolean expression
1470
+ * @alpha
1471
+ */
1472
+ class LikeExpr extends BooleanExpr {
1473
+ constructor(lhsExpr, patternExpr, escapeExpr, not) {
1474
+ super(LikeExpr.type);
1475
+ this.lhsExpr = lhsExpr;
1476
+ this.patternExpr = patternExpr;
1477
+ this.escapeExpr = escapeExpr;
1478
+ this.not = not;
1479
+ }
1480
+ get children() {
1481
+ const exprs = [this.lhsExpr, this.patternExpr];
1482
+ if (this.escapeExpr)
1483
+ exprs.push(this.escapeExpr);
1484
+ return exprs;
1485
+ }
1486
+ static parseOp(op) {
1487
+ return [op.endsWith("LIKE"), op.startsWith("NOT ")];
1488
+ }
1489
+ static deserialize(node) {
1490
+ if (node.id !== NativeExpIds.BinaryBoolean) {
1491
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.BinaryBoolean'. ${JSON.stringify(node)}`);
1492
+ }
1493
+ const [isLike, isNull] = this.parseOp(node.op);
1494
+ if (!isLike) {
1495
+ throw new Error(`Parse node has 'node.op !== LIKE'. ${JSON.stringify(node)}`);
1496
+ }
1497
+ const lhs = ValueExpr.deserialize(node.lhs);
1498
+ const pattren = ValueExpr.deserialize(node.rhs.pattren);
1499
+ const escape = node.rhs.escape ? ValueExpr.deserialize(node.rhs.escape) : undefined;
1500
+ return new LikeExpr(lhs, pattren, escape, isNull ? "NOT" : undefined);
1501
+ }
1502
+ writeTo(writer) {
1503
+ writer.appendExp(this.lhsExpr);
1504
+ writer.appendSpace();
1505
+ if (this.not) {
1506
+ writer.appendKeyword("NOT");
1507
+ writer.appendSpace();
1508
+ }
1509
+ writer.appendKeyword("LIKE");
1510
+ writer.appendSpace();
1511
+ writer.appendExp(this.patternExpr);
1512
+ if (this.escapeExpr) {
1513
+ writer.appendSpace();
1514
+ writer.appendKeyword("ESCAPE");
1515
+ writer.appendSpace();
1516
+ writer.appendExp(this.escapeExpr);
1517
+ }
1518
+ }
1519
+ }
1520
+ LikeExpr.type = ExprType.Like;
1521
+ exports.LikeExpr = LikeExpr;
1522
+ /**
1523
+ * Describe a <expr> BETWEEN <expr> AND <expr> boolean expression
1524
+ * @alpha
1525
+ */
1526
+ class BetweenExpr extends BooleanExpr {
1527
+ constructor(lhsExpr, lowerBoundExpr, upperBoundExpr, not) {
1528
+ super(BetweenExpr.type);
1529
+ this.lhsExpr = lhsExpr;
1530
+ this.lowerBoundExpr = lowerBoundExpr;
1531
+ this.upperBoundExpr = upperBoundExpr;
1532
+ this.not = not;
1533
+ }
1534
+ get children() {
1535
+ return [this.lhsExpr, this.lowerBoundExpr, this.upperBoundExpr];
1536
+ }
1537
+ static parseOp(op) {
1538
+ return [op.endsWith(" BETWEEN"), op.startsWith("NOT ")];
1539
+ }
1540
+ static deserialize(node) {
1541
+ if (node.id !== NativeExpIds.BinaryBoolean) {
1542
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.BinaryBoolean'. ${JSON.stringify(node)}`);
1543
+ }
1544
+ const [isBetween, isNull] = this.parseOp(node.op);
1545
+ if (!isBetween) {
1546
+ throw new Error(`Parse node has 'node.op !== BETWEEN'. ${JSON.stringify(node)}`);
1547
+ }
1548
+ const rhs = node.rhs;
1549
+ return new BetweenExpr(ValueExpr.deserialize(node.lhs), ValueExpr.deserialize(rhs.lbound), ValueExpr.deserialize(rhs.ubound), isNull ? "NOT" : undefined);
1550
+ }
1551
+ writeTo(writer) {
1552
+ writer.appendExp(this.lhsExpr);
1553
+ writer.appendSpace();
1554
+ if (this.not) {
1555
+ writer.appendKeyword("NOT");
1556
+ writer.appendSpace();
1557
+ }
1558
+ writer.appendKeyword("BETWEEN");
1559
+ writer.appendSpace();
1560
+ writer.appendExp(this.lowerBoundExpr);
1561
+ writer.appendSpace();
1562
+ writer.appendKeyword("AND");
1563
+ writer.appendSpace();
1564
+ writer.appendExp(this.upperBoundExpr);
1565
+ }
1566
+ }
1567
+ BetweenExpr.type = ExprType.Between;
1568
+ exports.BetweenExpr = BetweenExpr;
1569
+ /**
1570
+ * Describe a common table expression base query statement
1571
+ * @alpha
1572
+ */
1573
+ class CteExpr extends StatementExpr {
1574
+ constructor(cteBlocks, query, recursive) {
1575
+ super(CteExpr.type);
1576
+ this.cteBlocks = cteBlocks;
1577
+ this.query = query;
1578
+ this.recursive = recursive;
1579
+ }
1580
+ get children() {
1581
+ return [...this.cteBlocks, this.query];
1582
+ }
1583
+ static deserialize(node) {
1584
+ if (node.id !== NativeExpIds.CommonTable) {
1585
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.CommonTable'. ${JSON.stringify(node)}`);
1586
+ }
1587
+ const blocks = Array.from(node.blocks.map((v) => CteBlockExpr.deserialize(v)));
1588
+ return new CteExpr(blocks, SelectStatementExpr.deserialize(node.select), node.recursive === true ? "RECURSIVE" : undefined);
1589
+ }
1590
+ writeTo(writer) {
1591
+ writer.appendKeyword("WITH");
1592
+ writer.appendSpace();
1593
+ if (this.recursive) {
1594
+ writer.appendKeyword(this.recursive);
1595
+ writer.appendSpace();
1596
+ }
1597
+ this.cteBlocks.forEach((v, i) => {
1598
+ if (i > 0) {
1599
+ writer.appendComma();
1600
+ }
1601
+ writer.appendExp(v);
1602
+ });
1603
+ writer.appendSpace();
1604
+ writer.appendExp(this.query);
1605
+ }
1606
+ }
1607
+ CteExpr.type = ExprType.Cte;
1608
+ exports.CteExpr = CteExpr;
1609
+ /**
1610
+ * Describe a single block of CTE that can be reference in FROM clause of a SELECT
1611
+ * @alpha
1612
+ */
1613
+ class CteBlockExpr extends Expr {
1614
+ constructor(name, query, props) {
1615
+ super(CteBlockExpr.type);
1616
+ this.name = name;
1617
+ this.query = query;
1618
+ this.props = props;
1619
+ }
1620
+ get children() {
1621
+ return [this.query];
1622
+ }
1623
+ static deserialize(node) {
1624
+ if (node.id !== NativeExpIds.CommonTableBlock) {
1625
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.CommonTableBlock'. ${JSON.stringify(node)}`);
1626
+ }
1627
+ return new CteBlockExpr(node.name, SelectStatementExpr.deserialize(node.asQuery), node.args);
1628
+ }
1629
+ writeTo(writer) {
1630
+ writer.appendQuoted(this.name);
1631
+ writer.append("(");
1632
+ this.props.forEach((v, i) => {
1633
+ if (i > 0) {
1634
+ writer.appendComma();
1635
+ }
1636
+ writer.appendQuoted(v);
1637
+ });
1638
+ writer.append(")");
1639
+ writer.appendSpace();
1640
+ writer.appendKeyword("AS");
1641
+ writer.appendSpace();
1642
+ writer.append("(");
1643
+ writer.appendLine();
1644
+ writer.indent();
1645
+ writer.appendExp(this.query);
1646
+ writer.unindent();
1647
+ writer.appendLine();
1648
+ writer.append(")");
1649
+ }
1650
+ }
1651
+ CteBlockExpr.type = ExprType.CteBlock;
1652
+ exports.CteBlockExpr = CteBlockExpr;
1653
+ /**
1654
+ * Describe a name reference to a CTE block.
1655
+ * @alpha
1656
+ */
1657
+ class CteBlockRefExpr extends ClassRefExpr {
1658
+ constructor(name, alias) {
1659
+ super(CteBlockRefExpr.type);
1660
+ this.name = name;
1661
+ this.alias = alias;
1662
+ }
1663
+ get children() {
1664
+ return [];
1665
+ }
1666
+ static deserialize(node) {
1667
+ if (node.id !== NativeExpIds.CommonTableBlockName) {
1668
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.CommonTableBlockName'. ${JSON.stringify(node)}`);
1669
+ }
1670
+ return new CteBlockRefExpr(node.name, node.alias ? node.alias : undefined);
1671
+ }
1672
+ writeTo(writer) {
1673
+ writer.appendQuoted(this.name);
1674
+ if (this.alias) {
1675
+ writer.appendQuoted(this.alias);
1676
+ }
1677
+ }
1678
+ }
1679
+ CteBlockRefExpr.type = ExprType.CteBlockRef;
1680
+ exports.CteBlockRefExpr = CteBlockRefExpr;
1681
+ /**
1682
+ * Describe a table value function expression in ECSQL that appear in FROM clause of query.
1683
+ * @alpha
1684
+ */
1685
+ class TableValuedFuncExpr extends ClassRefExpr {
1686
+ constructor(schemaName, memberFunc, alias) {
1687
+ super(TableValuedFuncExpr.type);
1688
+ this.schemaName = schemaName;
1689
+ this.memberFunc = memberFunc;
1690
+ this.alias = alias;
1691
+ }
1692
+ get children() {
1693
+ return [this.memberFunc];
1694
+ }
1695
+ static deserialize(node) {
1696
+ if (node.id !== NativeExpIds.TableValuedFunction) {
1697
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.TableValuedFunction'. ${JSON.stringify(node)}`);
1698
+ }
1699
+ return new TableValuedFuncExpr(node.schema, MemberFuncCallExpr.deserialize(node.func), node.alias);
1700
+ }
1701
+ writeTo(writer) {
1702
+ writer.appendQuoted(this.schemaName);
1703
+ writer.append(".");
1704
+ writer.appendExp(this.memberFunc);
1705
+ if (this.alias) {
1706
+ writer.appendSpace();
1707
+ writer.appendQuoted(this.alias);
1708
+ }
1709
+ }
1710
+ }
1711
+ TableValuedFuncExpr.type = ExprType.TableValuedFunc;
1712
+ exports.TableValuedFuncExpr = TableValuedFuncExpr;
1713
+ /**
1714
+ * Describe a class name reference in ECSQL that appear in FROM clause of a SELECT.
1715
+ * @alpha
1716
+ */
1717
+ class ClassNameExpr extends ClassRefExpr {
1718
+ constructor(schemaNameOrAlias, className, tablespace, alias, polymorphicInfo, memberFunc) {
1719
+ super(ClassNameExpr.type);
1720
+ this.schemaNameOrAlias = schemaNameOrAlias;
1721
+ this.className = className;
1722
+ this.tablespace = tablespace;
1723
+ this.alias = alias;
1724
+ this.polymorphicInfo = polymorphicInfo;
1725
+ this.memberFunc = memberFunc;
1726
+ }
1727
+ get children() {
1728
+ return [];
1729
+ }
1730
+ static deserialize(node) {
1731
+ if (node.id !== NativeExpIds.ClassName) {
1732
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.ClassName'. ${JSON.stringify(node)}`);
1733
+ }
1734
+ const className = node.className;
1735
+ const tablespace = node.tableSpace ? node.tableSpace : undefined;
1736
+ const schemaName = node.schemaName;
1737
+ const alias = node.alias ? node.alias : undefined;
1738
+ const polymorphicInfo = node.polymorphicInfo ? { disqualify: node.polymorphicInfo.disqualify, allOrAny: node.polymorphicInfo.scope } : undefined;
1739
+ const memberFunc = node.func ? MemberFuncCallExpr.deserialize(node.func) : undefined;
1740
+ return new ClassNameExpr(schemaName, className, tablespace, alias, polymorphicInfo, memberFunc);
1741
+ }
1742
+ writeTo(writer) {
1743
+ if (this.polymorphicInfo) {
1744
+ if (this.polymorphicInfo.disqualify) {
1745
+ writer.append(this.polymorphicInfo.disqualify);
1746
+ }
1747
+ writer.appendKeyword(this.polymorphicInfo.allOrAny);
1748
+ writer.appendSpace();
1749
+ }
1750
+ if (this.tablespace) {
1751
+ writer.append("[").append(this.tablespace).append("]");
1752
+ writer.append(".");
1753
+ }
1754
+ writer.append("[").append(this.schemaNameOrAlias).append("]");
1755
+ writer.append(".");
1756
+ writer.append("[").append(this.className).append("]");
1757
+ if (this.memberFunc) {
1758
+ writer.append(".");
1759
+ writer.appendExp(this.memberFunc);
1760
+ }
1761
+ if (this.alias) {
1762
+ writer.appendSpace();
1763
+ writer.appendQuoted(this.alias);
1764
+ }
1765
+ }
1766
+ static fromECSql(ecsql) {
1767
+ const regex = /\s*((\+)?\s*(ALL|ONLY)\s+)?(\[?(\w+)\]?[\.:])?\[?(\w+)\]?[\.:]\[?(\w+)\]?(\s+(AS)?(\s+(\w+)))?/i;
1768
+ const match = ecsql.match(regex);
1769
+ if (!match) {
1770
+ throw new Error("ECSQL className must follow syntax: [+][ALL|ONLY] [tablespace.][.|:][schemaOrAlias]][.|:][className] [AS] [alias");
1771
+ }
1772
+ const plus = match.at(2);
1773
+ const allOrOnlyStr = match.at(3);
1774
+ const tablespace = match.at(5);
1775
+ const schemaOrAlias = match.at(6);
1776
+ const className = match.at(7);
1777
+ const alias = match.at(11);
1778
+ if (!schemaOrAlias || !className)
1779
+ throw new Error("ECSQL className must follow syntax: [+][ALL|ONLY] [tablespace.][.|:][schemaOrAlias]][.|:][className] [AS] [alias");
1780
+ let polyInfo;
1781
+ if (allOrOnlyStr) {
1782
+ const allOrAny = allOrOnlyStr.toUpperCase();
1783
+ polyInfo = { disqualify: plus, allOrAny };
1784
+ }
1785
+ return new ClassNameExpr(schemaOrAlias, className, tablespace, alias, polyInfo);
1786
+ }
1787
+ }
1788
+ ClassNameExpr.type = ExprType.ClassName;
1789
+ exports.ClassNameExpr = ClassNameExpr;
1790
+ /**
1791
+ * Describe a UPDATE statement in ECSQL.
1792
+ * @alpha
1793
+ */
1794
+ class UpdateStatementExpr extends StatementExpr {
1795
+ constructor(className, assignement, where, options) {
1796
+ super(UpdateStatementExpr.type);
1797
+ this.className = className;
1798
+ this.assignement = assignement;
1799
+ this.where = where;
1800
+ this.options = options;
1801
+ }
1802
+ get children() {
1803
+ const exprs = [this.className, this.assignement];
1804
+ if (this.where)
1805
+ exprs.push(this.where);
1806
+ if (this.options)
1807
+ exprs.push(this.options);
1808
+ return exprs;
1809
+ }
1810
+ static deserialize(node) {
1811
+ if (node.id !== NativeExpIds.UpdateStatement) {
1812
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.UpdateStatement'. ${JSON.stringify(node)}`);
1813
+ }
1814
+ const className = ClassNameExpr.deserialize(node.className);
1815
+ const assignment = SetClauseExpr.deserialize(node.assignment);
1816
+ const where = node.where ? WhereClauseExp.deserialize(node.where) : undefined;
1817
+ const options = node.options ? ECSqlOptionsClauseExpr.deserialize(node.options) : undefined;
1818
+ return new UpdateStatementExpr(className, assignment, where, options);
1819
+ }
1820
+ writeTo(writer) {
1821
+ writer.appendKeyword("UPDATE");
1822
+ writer.appendSpace();
1823
+ writer.appendExp(this.className);
1824
+ writer.appendSpace();
1825
+ writer.appendExp(this.assignement);
1826
+ if (this.where) {
1827
+ writer.appendSpace();
1828
+ writer.appendExp(this.where);
1829
+ }
1830
+ if (this.options) {
1831
+ writer.appendSpace();
1832
+ writer.appendExp(this.options);
1833
+ }
1834
+ }
1835
+ }
1836
+ UpdateStatementExpr.type = ExprType.UpdateStatement;
1837
+ exports.UpdateStatementExpr = UpdateStatementExpr;
1838
+ /**
1839
+ * Describe ECSQL option clause.
1840
+ * @alpha
1841
+ */
1842
+ class ECSqlOptionsClauseExpr extends Expr {
1843
+ constructor(options) {
1844
+ super(ECSqlOptionsClauseExpr.type);
1845
+ this.options = options;
1846
+ }
1847
+ get children() {
1848
+ return [];
1849
+ }
1850
+ static deserialize(node) {
1851
+ if (node.id !== NativeExpIds.Options) {
1852
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.Options'. ${JSON.stringify(node)}`);
1853
+ }
1854
+ return new ECSqlOptionsClauseExpr(node.options);
1855
+ }
1856
+ writeTo(writer) {
1857
+ writer.appendKeyword("ECSQLOPTIONS");
1858
+ writer.appendSpace();
1859
+ this.options.forEach((v, i) => {
1860
+ if (i > 0) {
1861
+ writer.appendSpace();
1862
+ }
1863
+ writer.append(v.name);
1864
+ if (v.value) {
1865
+ writer.appendBinaryOp("=");
1866
+ writer.append(v.value);
1867
+ }
1868
+ });
1869
+ }
1870
+ }
1871
+ ECSqlOptionsClauseExpr.type = ExprType.ECSqlOptionsClause;
1872
+ exports.ECSqlOptionsClauseExpr = ECSqlOptionsClauseExpr;
1873
+ /**
1874
+ * A single property value assignment for update clause
1875
+ * @alpha
1876
+ */
1877
+ class AssignmentExpr extends Expr {
1878
+ constructor(propertyName, valueExpr) {
1879
+ super(SetClauseExpr.type);
1880
+ this.propertyName = propertyName;
1881
+ this.valueExpr = valueExpr;
1882
+ }
1883
+ get children() {
1884
+ return [this.propertyName, this.valueExpr];
1885
+ }
1886
+ static deserialize(node) {
1887
+ if (node.id !== NativeExpIds.Assignment) {
1888
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.Assignment'. ${JSON.stringify(node)}`);
1889
+ }
1890
+ return new AssignmentExpr(PropertyNameExpr.deserialize(node.propertyName), ValueExpr.deserialize(node.value));
1891
+ }
1892
+ writeTo(writer) {
1893
+ writer.appendExp(this.propertyName);
1894
+ writer.appendBinaryOp("=");
1895
+ writer.appendExp(this.valueExpr);
1896
+ }
1897
+ }
1898
+ AssignmentExpr.type = ExprType.Assignment;
1899
+ exports.AssignmentExpr = AssignmentExpr;
1900
+ /**
1901
+ * Describe a set clause in a UPDATE statement
1902
+ * @alpha
1903
+ */
1904
+ class SetClauseExpr extends Expr {
1905
+ constructor(assignments) {
1906
+ super(SetClauseExpr.type);
1907
+ this.assignments = assignments;
1908
+ }
1909
+ get children() {
1910
+ return [...this.assignments];
1911
+ }
1912
+ static deserialize(node) {
1913
+ if (!Array.isArray(node)) {
1914
+ throw new Error(`AssignmentClause expect array of NativeECSqlParseNode. ${JSON.stringify(node)}`);
1915
+ }
1916
+ const setClause = node;
1917
+ const assignments = [];
1918
+ setClause.forEach((v) => {
1919
+ assignments.push(AssignmentExpr.deserialize(v));
1920
+ });
1921
+ return new SetClauseExpr(assignments);
1922
+ }
1923
+ writeTo(writer) {
1924
+ writer.appendKeyword("SET");
1925
+ writer.appendSpace();
1926
+ this.assignments.forEach((v, i) => {
1927
+ if (i > 0) {
1928
+ writer.appendComma();
1929
+ }
1930
+ writer.appendExp(v);
1931
+ });
1932
+ }
1933
+ }
1934
+ SetClauseExpr.type = ExprType.SetClause;
1935
+ exports.SetClauseExpr = SetClauseExpr;
1936
+ /**
1937
+ * Describe a strong typed IIF function in ECSQL
1938
+ * @alpha
1939
+ */
1940
+ class IIFExpr extends ValueExpr {
1941
+ constructor(whenExpr, thenExpr, elseExpr) {
1942
+ super(IIFExpr.type);
1943
+ this.whenExpr = whenExpr;
1944
+ this.thenExpr = thenExpr;
1945
+ this.elseExpr = elseExpr;
1946
+ }
1947
+ get children() {
1948
+ return [this.whenExpr, this.thenExpr, this.elseExpr];
1949
+ }
1950
+ static deserialize(node) {
1951
+ if (node.id !== NativeExpIds.IIF) {
1952
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.IIF'. ${JSON.stringify(node)}`);
1953
+ }
1954
+ return new IIFExpr(BooleanExpr.deserialize(node.when), ValueExpr.deserialize(node.then), ValueExpr.deserialize(node.else));
1955
+ }
1956
+ writeTo(writer) {
1957
+ writer.appendKeyword("IIF");
1958
+ writer.append("(");
1959
+ writer.appendExp(this.whenExpr);
1960
+ writer.appendComma();
1961
+ writer.appendExp(this.thenExpr);
1962
+ writer.appendComma();
1963
+ writer.appendExp(this.elseExpr);
1964
+ writer.append(")");
1965
+ }
1966
+ }
1967
+ IIFExpr.type = ExprType.IIF;
1968
+ exports.IIFExpr = IIFExpr;
1969
+ /**
1970
+ * Describe a CASE-WHEN-THEN expression in ECSQL
1971
+ * @alpha
1972
+ */
1973
+ class SearchCaseExpr extends ValueExpr {
1974
+ constructor(whenThenList, elseExpr) {
1975
+ super(SearchCaseExpr.type);
1976
+ this.whenThenList = whenThenList;
1977
+ this.elseExpr = elseExpr;
1978
+ }
1979
+ get children() {
1980
+ const exprs = [];
1981
+ this.whenThenList.forEach((v) => {
1982
+ exprs.push(v.whenExpr, v.thenExpr);
1983
+ });
1984
+ if (this.elseExpr)
1985
+ exprs.push(this.elseExpr);
1986
+ return exprs;
1987
+ }
1988
+ static deserialize(node) {
1989
+ if (node.id !== NativeExpIds.SearchCaseValue) {
1990
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.SearchCaseValue'. ${JSON.stringify(node)}`);
1991
+ }
1992
+ const whenThenList = [];
1993
+ for (const whenThenProps of node.whenThenList) {
1994
+ whenThenList.push({
1995
+ whenExpr: BooleanExpr.deserialize(whenThenProps.when),
1996
+ thenExpr: ValueExpr.deserialize(whenThenProps.then),
1997
+ });
1998
+ }
1999
+ const elseExp = node.elseExp ? ValueExpr.deserialize(node.elseExp) : undefined;
2000
+ return new SearchCaseExpr(whenThenList, elseExp);
2001
+ }
2002
+ writeTo(writer) {
2003
+ writer.appendKeyword("CASE");
2004
+ this.whenThenList.forEach((v) => {
2005
+ writer.appendSpace();
2006
+ writer.appendKeyword("WHEN");
2007
+ writer.appendSpace();
2008
+ writer.appendExp(v.whenExpr);
2009
+ writer.appendSpace();
2010
+ writer.appendKeyword("THEN");
2011
+ writer.appendSpace();
2012
+ writer.appendExp(v.thenExpr);
2013
+ });
2014
+ if (this.elseExpr) {
2015
+ writer.appendSpace();
2016
+ writer.appendKeyword("ELSE");
2017
+ writer.appendSpace();
2018
+ writer.appendExp(this.elseExpr);
2019
+ }
2020
+ writer.appendSpace();
2021
+ writer.appendKeyword("END");
2022
+ }
2023
+ }
2024
+ SearchCaseExpr.type = ExprType.SearchCase;
2025
+ exports.SearchCaseExpr = SearchCaseExpr;
2026
+ /**
2027
+ * Describe a binary value expression
2028
+ * @alpha
2029
+ */
2030
+ class BinaryValueExpr extends ValueExpr {
2031
+ constructor(op, lhsExpr, rhsExpr) {
2032
+ super(BinaryValueExpr.type);
2033
+ this.op = op;
2034
+ this.lhsExpr = lhsExpr;
2035
+ this.rhsExpr = rhsExpr;
2036
+ }
2037
+ get children() {
2038
+ return [this.lhsExpr, this.rhsExpr];
2039
+ }
2040
+ static deserialize(node) {
2041
+ if (node.id !== NativeExpIds.BinaryValue && node.id !== NativeExpIds.BinaryBoolean) {
2042
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.BinaryValue' . ${JSON.stringify(node)}`);
2043
+ }
2044
+ return new BinaryValueExpr(node.op, ValueExpr.deserialize(node.lhs), ValueExpr.deserialize(node.rhs));
2045
+ }
2046
+ writeTo(writer) {
2047
+ writer.append("(");
2048
+ writer.appendExp(this.lhsExpr);
2049
+ writer.appendBinaryOp(this.op);
2050
+ writer.appendExp(this.rhsExpr);
2051
+ writer.append(")");
2052
+ }
2053
+ }
2054
+ BinaryValueExpr.type = ExprType.BinaryValue;
2055
+ exports.BinaryValueExpr = BinaryValueExpr;
2056
+ /**
2057
+ * Cast a expression into a target time e.g. CAST(<expr> AS STRING)
2058
+ * @alpha
2059
+ */
2060
+ class CastExpr extends ValueExpr {
2061
+ constructor(valueExpr, targetType) {
2062
+ super(CastExpr.type);
2063
+ this.valueExpr = valueExpr;
2064
+ this.targetType = targetType;
2065
+ }
2066
+ get children() {
2067
+ return [this.valueExpr];
2068
+ }
2069
+ static deserialize(node) {
2070
+ if (node.id !== NativeExpIds.Cast) {
2071
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.CastExp'. ${JSON.stringify(node)}`);
2072
+ }
2073
+ return new CastExpr(ValueExpr.deserialize(node.exp), node.as);
2074
+ }
2075
+ writeTo(writer) {
2076
+ writer.appendKeyword("CAST");
2077
+ writer.append("(");
2078
+ writer.appendExp(this.valueExpr);
2079
+ writer.appendSpace();
2080
+ writer.appendKeyword("AS");
2081
+ writer.appendSpace();
2082
+ writer.append(this.targetType);
2083
+ writer.append(")");
2084
+ }
2085
+ }
2086
+ CastExpr.type = ExprType.Cast;
2087
+ exports.CastExpr = CastExpr;
2088
+ /**
2089
+ * Represent a member function called w.r.t a ClassNameExpr
2090
+ * @alpha
2091
+ */
2092
+ class MemberFuncCallExpr extends Expr {
2093
+ constructor(functionName, args) {
2094
+ super(MemberFuncCallExpr.type);
2095
+ this.functionName = functionName;
2096
+ this.args = args;
2097
+ }
2098
+ get children() {
2099
+ return [...this.args];
2100
+ }
2101
+ static deserialize(node) {
2102
+ if (node.id !== NativeExpIds.MemberFunctionCall) {
2103
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.MemberFunctionCall'. ${JSON.stringify(node)}`);
2104
+ }
2105
+ const args = Array.from(node.args.map((v) => ValueExpr.deserialize(v)));
2106
+ return new MemberFuncCallExpr(node.name, args);
2107
+ }
2108
+ writeTo(writer) {
2109
+ writer.appendQuoted(this.functionName);
2110
+ writer.append("(");
2111
+ this.args.forEach((v, i) => {
2112
+ if (i > 0) {
2113
+ writer.appendComma();
2114
+ }
2115
+ writer.appendExp(v);
2116
+ });
2117
+ writer.append(")");
2118
+ }
2119
+ }
2120
+ MemberFuncCallExpr.type = ExprType.MemberFuncCall;
2121
+ exports.MemberFuncCallExpr = MemberFuncCallExpr;
2122
+ /**
2123
+ * Represent a function call in ecsql
2124
+ * @alpha
2125
+ */
2126
+ class FuncCallExpr extends ValueExpr {
2127
+ constructor(functionName, args, allOrDistinct) {
2128
+ super(FuncCallExpr.type);
2129
+ this.functionName = functionName;
2130
+ this.args = args;
2131
+ this.allOrDistinct = allOrDistinct;
2132
+ }
2133
+ get children() {
2134
+ return [...this.args];
2135
+ }
2136
+ static deserialize(node) {
2137
+ if (node.id !== NativeExpIds.FunctionCall) {
2138
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.FunctionCall'. ${JSON.stringify(node)}`);
2139
+ }
2140
+ const args = Array.from(node.args.map((v) => ValueExpr.deserialize(v)));
2141
+ const rowQuantifier = node.quantifier ? node.quantifier : undefined;
2142
+ return new FuncCallExpr(node.name, args, rowQuantifier);
2143
+ }
2144
+ writeTo(writer) {
2145
+ writer.append(this.functionName);
2146
+ writer.append("(");
2147
+ if (this.allOrDistinct) {
2148
+ writer.appendKeyword(this.allOrDistinct);
2149
+ writer.appendSpace();
2150
+ }
2151
+ this.args.forEach((v, i) => {
2152
+ if (i > 0) {
2153
+ writer.appendComma();
2154
+ }
2155
+ writer.appendExp(v);
2156
+ });
2157
+ writer.append(")");
2158
+ }
2159
+ static makeAbs(arg) {
2160
+ return new FuncCallExpr("ABS", [arg]);
2161
+ }
2162
+ static makeLower(arg) {
2163
+ return new FuncCallExpr("LOWER", [arg]);
2164
+ }
2165
+ static makeUpper(arg) {
2166
+ return new FuncCallExpr("UPPER", [arg]);
2167
+ }
2168
+ static makeLTrim(arg0, arg1) {
2169
+ return new FuncCallExpr("LTRIM", arg1 ? [arg0, arg1] : [arg0]);
2170
+ }
2171
+ static makeRTrim(arg0, arg1) {
2172
+ return new FuncCallExpr("RTRIM", arg1 ? [arg0, arg1] : [arg0]);
2173
+ }
2174
+ static makeLHex(arg) {
2175
+ return new FuncCallExpr("HEX", [arg]);
2176
+ }
2177
+ static makeLIfNull(arg0, arg1) {
2178
+ return new FuncCallExpr("IFNULL", [arg0, arg1]);
2179
+ }
2180
+ static makeInstr(arg0, arg1) {
2181
+ return new FuncCallExpr("INSTR", [arg0, arg1]);
2182
+ }
2183
+ static makeLength(arg0) {
2184
+ return new FuncCallExpr("LENGTH", [arg0]);
2185
+ }
2186
+ static makeLike(arg0, arg1, arg2) {
2187
+ return new FuncCallExpr("LIKE", arg2 ? [arg0, arg1, arg2] : [arg0, arg1]);
2188
+ }
2189
+ static makeLikelihood(arg0, arg1) {
2190
+ return new FuncCallExpr("LIKELIHOOD", [arg0, arg1]);
2191
+ }
2192
+ static makeMax(arg, ...optionalArgs) {
2193
+ return new FuncCallExpr("MAX", [arg, ...optionalArgs]);
2194
+ }
2195
+ static makeMin(arg, ...optionalArgs) {
2196
+ return new FuncCallExpr("MIN", [arg, ...optionalArgs]);
2197
+ }
2198
+ static makePrintf(arg, ...optionalArgs) {
2199
+ return new FuncCallExpr("PRINTF", [arg, ...optionalArgs]);
2200
+ }
2201
+ static makeRandom() {
2202
+ return new FuncCallExpr("RANDOM", []);
2203
+ }
2204
+ static makeQuote(arg) {
2205
+ return new FuncCallExpr("QUOTE", [arg]);
2206
+ }
2207
+ static makeRandomBlob(arg) {
2208
+ return new FuncCallExpr("RANDOMBLOB", [arg]);
2209
+ }
2210
+ static makeReplace(arg0, arg1, arg2) {
2211
+ return new FuncCallExpr("REPLACE", [arg0, arg1, arg2]);
2212
+ }
2213
+ static makeRound(arg0, arg1) {
2214
+ return new FuncCallExpr("ROUND", arg1 ? [arg0, arg1] : [arg0]);
2215
+ }
2216
+ static makeSign(arg0) {
2217
+ return new FuncCallExpr("SIGN", [arg0]);
2218
+ }
2219
+ static makeUnhex(arg0) {
2220
+ return new FuncCallExpr("UNHEX", [arg0]);
2221
+ }
2222
+ static makeSoundex(arg0) {
2223
+ return new FuncCallExpr("SOUNDEX", [arg0]);
2224
+ }
2225
+ static makeTrim(arg0, arg1) {
2226
+ return new FuncCallExpr("TRIM", arg1 ? [arg0, arg1] : [arg0]);
2227
+ }
2228
+ static makeTypeOf(arg0) {
2229
+ return new FuncCallExpr("TYPEOF", [arg0]);
2230
+ }
2231
+ static makeZeroBlob(arg0) {
2232
+ return new FuncCallExpr("ZEROBLOB", [arg0]);
2233
+ }
2234
+ static makeUnlikely(arg0) {
2235
+ return new FuncCallExpr("UNLIKELY", [arg0]);
2236
+ }
2237
+ static makeSubstring(arg0, arg1, arg2) {
2238
+ return new FuncCallExpr("SUBSTR", [arg0, arg1, arg2]);
2239
+ }
2240
+ static makeStrToGuid(arg0) {
2241
+ return new FuncCallExpr("STRTOGUID", [arg0]);
2242
+ }
2243
+ static makeGuidToStr(arg0) {
2244
+ return new FuncCallExpr("GUIDTOSTR", [arg0]);
2245
+ }
2246
+ static makeIdToHex(arg0) {
2247
+ return new FuncCallExpr("IDTOHEX", [arg0]);
2248
+ }
2249
+ static makeHexToId(arg0) {
2250
+ return new FuncCallExpr("HEXTOID", [arg0]);
2251
+ }
2252
+ static makeEcClassName(arg0, fmt = "s:c") {
2253
+ return new FuncCallExpr("EC_CLASSNAME", [arg0, new LiteralExpr(LiteralValueType.String, fmt)]);
2254
+ }
2255
+ static makeEcClassId(arg0) {
2256
+ return new FuncCallExpr("EC_CLASSId", [arg0]);
2257
+ }
2258
+ static makeInstanceOf(arg0, arg1) {
2259
+ return new FuncCallExpr("EC_INSTANCEOF", [arg0, arg1]);
2260
+ }
2261
+ }
2262
+ FuncCallExpr.type = ExprType.FuncCall;
2263
+ exports.FuncCallExpr = FuncCallExpr;
2264
+ /**
2265
+ * Represent positional or named parameter
2266
+ * @alpha
2267
+ */
2268
+ class ParameterExpr extends ValueExpr {
2269
+ constructor(name) {
2270
+ super(ParameterExpr.type);
2271
+ this.name = name;
2272
+ }
2273
+ get children() {
2274
+ return [];
2275
+ }
2276
+ static deserialize(node) {
2277
+ if (node.id !== NativeExpIds.Parameter) {
2278
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.Parameter'. ${JSON.stringify(node)}`);
2279
+ }
2280
+ return new ParameterExpr(node.name);
2281
+ }
2282
+ writeTo(writer) {
2283
+ if (this.name && this.name !== "")
2284
+ writer.append(`:${this.name}`);
2285
+ else
2286
+ writer.append(`?`);
2287
+ }
2288
+ }
2289
+ ParameterExpr.type = ExprType.Parameter;
2290
+ exports.ParameterExpr = ParameterExpr;
2291
+ /**
2292
+ * Unary value with operator e.g. [+|-|~]<number>
2293
+ * @alpha
2294
+ */
2295
+ class UnaryValueExpr extends ValueExpr {
2296
+ constructor(op, valueExpr) {
2297
+ super(UnaryValueExpr.type);
2298
+ this.op = op;
2299
+ this.valueExpr = valueExpr;
2300
+ }
2301
+ get children() {
2302
+ return [this.valueExpr];
2303
+ }
2304
+ static deserialize(node) {
2305
+ if (node.id !== NativeExpIds.UnaryValue && node.id !== NativeExpIds.BooleanFactor) {
2306
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.UnaryValue && node.id !== NativeExpIds.BooleanFactor'. ${JSON.stringify(node)}`);
2307
+ }
2308
+ if (node.op !== "+" && node.op !== "-" && node.op !== "~") {
2309
+ throw new Error(`Unrecognized operator in .node.op'. Must me on of UnaryOp. ${JSON.stringify(node)}`);
2310
+ }
2311
+ return new UnaryValueExpr(node.op, ValueExpr.deserialize(node.exp));
2312
+ }
2313
+ writeTo(writer) {
2314
+ writer.append(this.op);
2315
+ writer.appendExp(this.valueExpr);
2316
+ }
2317
+ }
2318
+ UnaryValueExpr.type = ExprType.Unary;
2319
+ exports.UnaryValueExpr = UnaryValueExpr;
2320
+ /**
2321
+ * Represent constant literal like string, data, time, timestamp, number or null
2322
+ * @alpha
2323
+ */
2324
+ class LiteralExpr extends ValueExpr {
2325
+ constructor(valueType, rawValue) {
2326
+ super(LiteralExpr.type);
2327
+ this.valueType = valueType;
2328
+ this.rawValue = rawValue;
2329
+ }
2330
+ get children() {
2331
+ return [];
2332
+ }
2333
+ static deserialize(node) {
2334
+ if (node.id !== NativeExpIds.LiteralValue) {
2335
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.LiteralValue'. ${JSON.stringify(node)}`);
2336
+ }
2337
+ return new LiteralExpr(node.kind, node.value);
2338
+ }
2339
+ writeTo(writer) {
2340
+ if (this.valueType === LiteralValueType.String)
2341
+ writer.appendStringLiteral(this.rawValue);
2342
+ else if (this.valueType === LiteralValueType.Date)
2343
+ writer.appendKeyword("DATE").appendSpace().appendStringLiteral(this.rawValue);
2344
+ else if (this.valueType === LiteralValueType.Time)
2345
+ writer.appendKeyword("TIME").appendSpace().appendStringLiteral(this.rawValue);
2346
+ else if (this.valueType === LiteralValueType.Timestamp)
2347
+ writer.appendKeyword("TIMESTAMP").appendSpace().appendStringLiteral(this.rawValue);
2348
+ else if (this.valueType === LiteralValueType.Null)
2349
+ writer.appendKeyword("NULL");
2350
+ else
2351
+ writer.append(this.rawValue);
2352
+ }
2353
+ static makeRaw(val) { return new LiteralExpr(LiteralValueType.Raw, val); }
2354
+ static makeString(val) { return new LiteralExpr(LiteralValueType.String, val); }
2355
+ static makeNumber(val) { return new LiteralExpr(LiteralValueType.Raw, val.toString()); }
2356
+ static makeDate(val) { return new LiteralExpr(LiteralValueType.Date, val.toDateString()); }
2357
+ static makeTime(val) { return new LiteralExpr(LiteralValueType.String, val.toTimeString()); }
2358
+ static makeTimestamp(val) { return new LiteralExpr(LiteralValueType.String, val.toTimeString()); }
2359
+ static makeNull() { return new LiteralExpr(LiteralValueType.Null, ""); }
2360
+ }
2361
+ LiteralExpr.type = ExprType.Literal;
2362
+ exports.LiteralExpr = LiteralExpr;
2363
+ /**
2364
+ * Represent property name identifier
2365
+ * @alpha
2366
+ */
2367
+ class PropertyNameExpr extends ValueExpr {
2368
+ constructor(propertyPath) {
2369
+ super(PropertyNameExpr.type);
2370
+ this.propertyPath = propertyPath;
2371
+ }
2372
+ get children() {
2373
+ return [];
2374
+ }
2375
+ static deserialize(node) {
2376
+ if (node.id !== NativeExpIds.PropertyName) {
2377
+ throw new Error(`Parse node is 'node.id !== NativeExpIds.PropertyNameExp'. ${JSON.stringify(node)}`);
2378
+ }
2379
+ return new PropertyNameExpr(node.path);
2380
+ }
2381
+ writeTo(writer) {
2382
+ // donot quote $
2383
+ const str = this.propertyPath.split("->");
2384
+ if (str.length === 2) {
2385
+ writer.append(str[0].split(".").map((v) => v.startsWith("[") || v === "$" ? v : `[${v}]`).join("."));
2386
+ writer.append("->");
2387
+ }
2388
+ writer.append(str[str.length - 1].split(".").map((v) => v.startsWith("[") || v === "$" ? v : `[${v}]`).join("."));
2389
+ }
2390
+ }
2391
+ PropertyNameExpr.type = ExprType.PropertyName;
2392
+ exports.PropertyNameExpr = PropertyNameExpr;
2393
+ //# sourceMappingURL=ECSqlAst.js.map