@sqb/builder 4.2.1 → 4.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (206) hide show
  1. package/README.md +2 -2
  2. package/cjs/classes.ns.js +41 -0
  3. package/{dist → cjs}/enums.js +6 -3
  4. package/{dist → cjs}/extensions.js +0 -0
  5. package/{dist → cjs}/helpers.js +0 -0
  6. package/cjs/index.js +27 -0
  7. package/{dist → cjs}/op.initializers.js +42 -42
  8. package/cjs/package.json +3 -0
  9. package/{dist/query/DeleteQuery.js → cjs/query/delete-query.js} +13 -11
  10. package/{dist/query/InsertQuery.js → cjs/query/insert-query.js} +15 -13
  11. package/{dist/query/Query.js → cjs/query/query.js} +5 -4
  12. package/{dist/query/ReturningQuery.js → cjs/query/returning-query.js} +9 -8
  13. package/cjs/query/select-query.js +294 -0
  14. package/{dist/query/UpdateQuery.js → cjs/query/update-query.js} +17 -14
  15. package/{dist/Serializable.js → cjs/serializable.js} +0 -0
  16. package/{dist/SerializeContext.js → cjs/serialize-context.js} +36 -26
  17. package/cjs/sql-objects/base-field.js +11 -0
  18. package/{dist/sql-objects/CaseStatement.js → cjs/sql-objects/case-statement.js} +10 -6
  19. package/{dist/sql-objects/CoalesceStatement.js → cjs/sql-objects/coalesce-statement.js} +6 -4
  20. package/{dist/sql-objects/CountStatement.js → cjs/sql-objects/count-statement.js} +5 -4
  21. package/cjs/sql-objects/expression.js +10 -0
  22. package/{dist/sql-objects/FieldExpression.js → cjs/sql-objects/field-expression.js} +5 -4
  23. package/{dist/sql-objects/GroupColumn.js → cjs/sql-objects/group-column.js} +4 -4
  24. package/{dist/sql-objects/JoinStatement.js → cjs/sql-objects/join-statement.js} +20 -18
  25. package/{dist/sql-objects/LowerStatement.js → cjs/sql-objects/lower-statement.js} +6 -4
  26. package/cjs/sql-objects/max-statement.js +41 -0
  27. package/cjs/sql-objects/min-statement.js +41 -0
  28. package/{dist/sql-objects/Operator.js → cjs/sql-objects/operator.js} +2 -2
  29. package/{dist/sql-objects/operators/CompOperator.js → cjs/sql-objects/operators/comp-operator.js} +17 -13
  30. package/{dist/sql-objects/operators/LogicalOperator.js → cjs/sql-objects/operators/logical-operator.js} +11 -11
  31. package/cjs/sql-objects/operators/op-and.js +9 -0
  32. package/{dist/sql-objects/operators/OpBetween.js → cjs/sql-objects/operators/op-between.js} +5 -5
  33. package/cjs/sql-objects/operators/op-eq.js +13 -0
  34. package/{dist/sql-objects/operators/OpExists.js → cjs/sql-objects/operators/op-exists.js} +7 -7
  35. package/cjs/sql-objects/operators/op-gt.js +13 -0
  36. package/cjs/sql-objects/operators/op-gte.js +13 -0
  37. package/cjs/sql-objects/operators/op-ilike.js +10 -0
  38. package/cjs/sql-objects/operators/op-in.js +19 -0
  39. package/cjs/sql-objects/operators/op-is-not.js +13 -0
  40. package/cjs/sql-objects/operators/op-is.js +13 -0
  41. package/{dist/sql-objects/operators/OpLike.js → cjs/sql-objects/operators/op-like.js} +7 -7
  42. package/cjs/sql-objects/operators/op-lt.js +13 -0
  43. package/cjs/sql-objects/operators/op-lte.js +13 -0
  44. package/cjs/sql-objects/operators/op-ne.js +13 -0
  45. package/cjs/sql-objects/operators/op-not-between.js +10 -0
  46. package/cjs/sql-objects/operators/op-not-exists.js +10 -0
  47. package/cjs/sql-objects/operators/op-not-ilike.js +10 -0
  48. package/cjs/sql-objects/operators/op-not-in.js +10 -0
  49. package/cjs/sql-objects/operators/op-not-like.js +10 -0
  50. package/cjs/sql-objects/operators/op-or.js +9 -0
  51. package/{dist/sql-objects/OrderColumn.js → cjs/sql-objects/order-column.js} +5 -4
  52. package/{dist/sql-objects/ParamExpression.js → cjs/sql-objects/param-expression.js} +7 -4
  53. package/{dist/sql-objects/RawStatement.js → cjs/sql-objects/raw-statement.js} +5 -4
  54. package/{dist/sql-objects/ReturningColumn.js → cjs/sql-objects/returning-column.js} +5 -4
  55. package/cjs/sql-objects/sequence-getter-statement.js +51 -0
  56. package/cjs/sql-objects/string-agg-statement.js +82 -0
  57. package/{dist/sql-objects/TableName.js → cjs/sql-objects/table-name.js} +7 -4
  58. package/{dist/sql-objects/UpperStatement.js → cjs/sql-objects/upper-statement.js} +6 -4
  59. package/cjs/sqlobject.initializers.js +129 -0
  60. package/{dist → cjs}/typeguards.js +21 -21
  61. package/{dist → cjs}/types.js +0 -0
  62. package/esm/classes.ns.d.ts +38 -0
  63. package/esm/classes.ns.js +38 -0
  64. package/{dist → esm}/enums.d.ts +6 -3
  65. package/esm/enums.js +95 -0
  66. package/{dist → esm}/extensions.d.ts +1 -1
  67. package/esm/extensions.js +11 -0
  68. package/{dist → esm}/helpers.d.ts +0 -0
  69. package/esm/helpers.js +27 -0
  70. package/esm/index.d.ts +20 -0
  71. package/esm/index.js +20 -0
  72. package/{dist → esm}/op.initializers.d.ts +22 -22
  73. package/esm/op.initializers.js +126 -0
  74. package/{dist/query/DeleteQuery.d.ts → esm/query/delete-query.d.ts} +6 -6
  75. package/esm/query/delete-query.js +51 -0
  76. package/{dist/query/InsertQuery.d.ts → esm/query/insert-query.d.ts} +5 -5
  77. package/esm/query/insert-query.js +73 -0
  78. package/{dist/query/Query.d.ts → esm/query/query.d.ts} +2 -2
  79. package/esm/query/query.js +36 -0
  80. package/{dist/query/ReturningQuery.d.ts → esm/query/returning-query.d.ts} +3 -3
  81. package/esm/query/returning-query.js +41 -0
  82. package/{dist/query/SelectQuery.d.ts → esm/query/select-query.d.ts} +9 -9
  83. package/{dist/query/SelectQuery.js → esm/query/select-query.js} +38 -32
  84. package/{dist/query/UpdateQuery.d.ts → esm/query/update-query.d.ts} +6 -6
  85. package/esm/query/update-query.js +79 -0
  86. package/{dist/Serializable.d.ts → esm/serializable.d.ts} +2 -2
  87. package/esm/serializable.js +2 -0
  88. package/{dist/SerializeContext.d.ts → esm/serialize-context.d.ts} +1 -1
  89. package/esm/serialize-context.js +145 -0
  90. package/{dist/sql-objects/BaseField.d.ts → esm/sql-objects/base-field.d.ts} +1 -1
  91. package/esm/sql-objects/base-field.js +7 -0
  92. package/{dist/sql-objects/CaseStatement.d.ts → esm/sql-objects/case-statement.d.ts} +6 -6
  93. package/esm/sql-objects/case-statement.js +85 -0
  94. package/{dist/sql-objects/CoalesceStatement.d.ts → esm/sql-objects/coalesce-statement.d.ts} +3 -3
  95. package/esm/sql-objects/coalesce-statement.js +42 -0
  96. package/{dist/sql-objects/CountStatement.d.ts → esm/sql-objects/count-statement.d.ts} +3 -3
  97. package/esm/sql-objects/count-statement.js +31 -0
  98. package/{dist/sql-objects/Expression.d.ts → esm/sql-objects/expression.d.ts} +2 -2
  99. package/esm/sql-objects/expression.js +6 -0
  100. package/{dist/sql-objects/FieldExpression.d.ts → esm/sql-objects/field-expression.d.ts} +3 -3
  101. package/esm/sql-objects/field-expression.js +50 -0
  102. package/{dist/sql-objects/GroupColumn.d.ts → esm/sql-objects/group-column.d.ts} +3 -3
  103. package/esm/sql-objects/group-column.js +34 -0
  104. package/{dist/sql-objects/JoinStatement.d.ts → esm/sql-objects/join-statement.d.ts} +7 -7
  105. package/esm/sql-objects/join-statement.js +79 -0
  106. package/{dist/sql-objects/LowerStatement.d.ts → esm/sql-objects/lower-statement.d.ts} +3 -3
  107. package/esm/sql-objects/lower-statement.js +37 -0
  108. package/esm/sql-objects/max-statement.d.ts +22 -0
  109. package/esm/sql-objects/max-statement.js +37 -0
  110. package/esm/sql-objects/min-statement.d.ts +22 -0
  111. package/esm/sql-objects/min-statement.js +37 -0
  112. package/{dist/sql-objects/Operator.d.ts → esm/sql-objects/operator.d.ts} +2 -2
  113. package/esm/sql-objects/operator.js +3 -0
  114. package/{dist/sql-objects/operators/CompOperator.d.ts → esm/sql-objects/operators/comp-operator.d.ts} +4 -4
  115. package/esm/sql-objects/operators/comp-operator.js +86 -0
  116. package/{dist/sql-objects/operators/LogicalOperator.d.ts → esm/sql-objects/operators/logical-operator.d.ts} +4 -4
  117. package/esm/sql-objects/operators/logical-operator.js +79 -0
  118. package/esm/sql-objects/operators/op-and.d.ts +5 -0
  119. package/esm/sql-objects/operators/op-and.js +5 -0
  120. package/{dist/sql-objects/operators/OpBetween.d.ts → esm/sql-objects/operators/op-between.d.ts} +4 -4
  121. package/esm/sql-objects/operators/op-between.js +31 -0
  122. package/{dist/sql-objects/operators/OpEq.d.ts → esm/sql-objects/operators/op-eq.d.ts} +3 -3
  123. package/esm/sql-objects/operators/op-eq.js +9 -0
  124. package/{dist/sql-objects/operators/OpExists.d.ts → esm/sql-objects/operators/op-exists.d.ts} +4 -4
  125. package/esm/sql-objects/operators/op-exists.js +26 -0
  126. package/{dist/sql-objects/operators/OpGt.d.ts → esm/sql-objects/operators/op-gt.d.ts} +3 -3
  127. package/esm/sql-objects/operators/op-gt.js +9 -0
  128. package/{dist/sql-objects/operators/OpGte.d.ts → esm/sql-objects/operators/op-gte.d.ts} +3 -3
  129. package/esm/sql-objects/operators/op-gte.js +9 -0
  130. package/{dist/sql-objects/operators/OpILike.d.ts → esm/sql-objects/operators/op-ilike.d.ts} +2 -2
  131. package/esm/sql-objects/operators/op-ilike.js +6 -0
  132. package/{dist/sql-objects/operators/OpIn.d.ts → esm/sql-objects/operators/op-in.d.ts} +3 -3
  133. package/esm/sql-objects/operators/op-in.js +15 -0
  134. package/{dist/sql-objects/operators/OpIsNot.d.ts → esm/sql-objects/operators/op-is-not.d.ts} +3 -3
  135. package/esm/sql-objects/operators/op-is-not.js +9 -0
  136. package/{dist/sql-objects/operators/OpIs.d.ts → esm/sql-objects/operators/op-is.d.ts} +3 -3
  137. package/esm/sql-objects/operators/op-is.js +9 -0
  138. package/{dist/sql-objects/operators/OpLike.d.ts → esm/sql-objects/operators/op-like.d.ts} +4 -4
  139. package/esm/sql-objects/operators/op-like.js +18 -0
  140. package/{dist/sql-objects/operators/OpLt.d.ts → esm/sql-objects/operators/op-lt.d.ts} +3 -3
  141. package/esm/sql-objects/operators/op-lt.js +9 -0
  142. package/{dist/sql-objects/operators/OpLte.d.ts → esm/sql-objects/operators/op-lte.d.ts} +3 -3
  143. package/esm/sql-objects/operators/op-lte.js +9 -0
  144. package/{dist/sql-objects/operators/OpNe.d.ts → esm/sql-objects/operators/op-ne.d.ts} +3 -3
  145. package/esm/sql-objects/operators/op-ne.js +9 -0
  146. package/{dist/sql-objects/operators/OpNotBetween.d.ts → esm/sql-objects/operators/op-not-between.d.ts} +2 -2
  147. package/esm/sql-objects/operators/op-not-between.js +6 -0
  148. package/{dist/sql-objects/operators/OpNotExists.d.ts → esm/sql-objects/operators/op-not-exists.d.ts} +2 -2
  149. package/esm/sql-objects/operators/op-not-exists.js +6 -0
  150. package/{dist/sql-objects/operators/OpNotILike.d.ts → esm/sql-objects/operators/op-not-ilike.d.ts} +2 -2
  151. package/esm/sql-objects/operators/op-not-ilike.js +6 -0
  152. package/{dist/sql-objects/operators/OpNotIn.d.ts → esm/sql-objects/operators/op-not-in.d.ts} +2 -2
  153. package/esm/sql-objects/operators/op-not-in.js +6 -0
  154. package/{dist/sql-objects/operators/OpNotLike.d.ts → esm/sql-objects/operators/op-not-like.d.ts} +2 -2
  155. package/esm/sql-objects/operators/op-not-like.js +6 -0
  156. package/esm/sql-objects/operators/op-or.d.ts +5 -0
  157. package/esm/sql-objects/operators/op-or.js +5 -0
  158. package/{dist/sql-objects/OrderColumn.d.ts → esm/sql-objects/order-column.d.ts} +3 -3
  159. package/esm/sql-objects/order-column.js +39 -0
  160. package/{dist/sql-objects/ParamExpression.d.ts → esm/sql-objects/param-expression.d.ts} +3 -3
  161. package/esm/sql-objects/param-expression.js +52 -0
  162. package/{dist/sql-objects/RawStatement.d.ts → esm/sql-objects/raw-statement.d.ts} +3 -3
  163. package/esm/sql-objects/raw-statement.js +15 -0
  164. package/{dist/sql-objects/ReturningColumn.d.ts → esm/sql-objects/returning-column.d.ts} +3 -3
  165. package/esm/sql-objects/returning-column.js +29 -0
  166. package/esm/sql-objects/sequence-getter-statement.d.ts +24 -0
  167. package/esm/sql-objects/sequence-getter-statement.js +47 -0
  168. package/{dist/sql-objects/StringAggStatement.d.ts → esm/sql-objects/string-agg-statement.d.ts} +4 -4
  169. package/{dist/sql-objects/StringAggStatement.js → esm/sql-objects/string-agg-statement.js} +15 -15
  170. package/{dist/sql-objects/TableName.d.ts → esm/sql-objects/table-name.d.ts} +3 -3
  171. package/esm/sql-objects/table-name.js +30 -0
  172. package/{dist/sql-objects/UpperStatement.d.ts → esm/sql-objects/upper-statement.d.ts} +3 -3
  173. package/esm/sql-objects/upper-statement.js +37 -0
  174. package/{dist → esm}/sqlobject.initializers.d.ts +23 -16
  175. package/esm/sqlobject.initializers.js +101 -0
  176. package/{dist → esm}/typeguards.d.ts +17 -17
  177. package/esm/typeguards.js +57 -0
  178. package/{dist → esm}/types.d.ts +2 -2
  179. package/esm/types.js +1 -0
  180. package/package.json +48 -33
  181. package/dist/classes.d.ts +0 -38
  182. package/dist/classes.js +0 -41
  183. package/dist/index.d.ts +0 -20
  184. package/dist/index.js +0 -27
  185. package/dist/sql-objects/BaseField.js +0 -11
  186. package/dist/sql-objects/Expression.js +0 -7
  187. package/dist/sql-objects/operators/OpAnd.d.ts +0 -5
  188. package/dist/sql-objects/operators/OpAnd.js +0 -12
  189. package/dist/sql-objects/operators/OpEq.js +0 -13
  190. package/dist/sql-objects/operators/OpGt.js +0 -13
  191. package/dist/sql-objects/operators/OpGte.js +0 -13
  192. package/dist/sql-objects/operators/OpILike.js +0 -13
  193. package/dist/sql-objects/operators/OpIn.js +0 -19
  194. package/dist/sql-objects/operators/OpIs.js +0 -13
  195. package/dist/sql-objects/operators/OpIsNot.js +0 -13
  196. package/dist/sql-objects/operators/OpLt.js +0 -13
  197. package/dist/sql-objects/operators/OpLte.js +0 -13
  198. package/dist/sql-objects/operators/OpNe.js +0 -13
  199. package/dist/sql-objects/operators/OpNotBetween.js +0 -13
  200. package/dist/sql-objects/operators/OpNotExists.js +0 -13
  201. package/dist/sql-objects/operators/OpNotILike.js +0 -13
  202. package/dist/sql-objects/operators/OpNotIn.js +0 -13
  203. package/dist/sql-objects/operators/OpNotLike.js +0 -13
  204. package/dist/sql-objects/operators/OpOr.d.ts +0 -5
  205. package/dist/sql-objects/operators/OpOr.js +0 -12
  206. package/dist/sqlobject.initializers.js +0 -110
@@ -1,6 +1,6 @@
1
- import { DataType, SerializationType } from '../enums';
2
- import { SerializeContext } from '../SerializeContext';
3
- import { BaseField } from './BaseField';
1
+ import { DataType, SerializationType } from '../enums.js';
2
+ import { SerializeContext } from '../serialize-context.js';
3
+ import { BaseField } from './base-field.js';
4
4
  export declare class FieldExpression extends BaseField {
5
5
  _alias?: string;
6
6
  constructor(expression: string, dataType?: DataType, isArray?: boolean);
@@ -0,0 +1,50 @@
1
+ import { SerializationType } from '../enums.js';
2
+ import { BaseField } from './base-field.js';
3
+ const TABLE_COLUMN_PATTERN = /^((?:[a-zA-Z_][\w$_]*\.){0,2}) *([0-9a-zA-Z_][\w$_]*|\*) *(?:as)? *([a-zA-Z_][\w$_]*)?$/;
4
+ export class FieldExpression extends BaseField {
5
+ _alias;
6
+ constructor(arg0, arg1, arg2) {
7
+ super();
8
+ let expression;
9
+ if (typeof arg0 === 'object') {
10
+ expression = arg0.expression;
11
+ this._dataType = arg0.dataType;
12
+ this._isArray = arg0.isArray;
13
+ }
14
+ else {
15
+ expression = arg0;
16
+ this._dataType = arg1;
17
+ this._isArray = arg2;
18
+ }
19
+ const m = expression.match(TABLE_COLUMN_PATTERN);
20
+ if (!m)
21
+ throw new TypeError(`${expression} does not match table column format`);
22
+ this._field = m[2];
23
+ if (m[1]) {
24
+ const a = m[1].split(/\./g);
25
+ a.pop();
26
+ this._table = a.pop();
27
+ this._schema = a.pop();
28
+ }
29
+ this._alias = this._field !== '*' ? m[3] : '';
30
+ }
31
+ get _type() {
32
+ return SerializationType.FIELD_NAME;
33
+ }
34
+ _serialize(ctx) {
35
+ const o = {
36
+ schema: this._schema,
37
+ table: this._table,
38
+ field: this._field,
39
+ alias: this._alias,
40
+ isReservedWord: !!(this._field && ctx.isReservedWord(this._field))
41
+ };
42
+ return ctx.serialize(this._type, o, () => {
43
+ const prefix = ctx.escapeReserved(this._schema ? this._schema + '.' : '') +
44
+ (this._table ? this._table + '.' : '');
45
+ return prefix +
46
+ (!prefix && o.isReservedWord ? '"' + this._field + '"' : this._field) +
47
+ (this._alias ? ' as ' + this._alias : '');
48
+ });
49
+ }
50
+ }
@@ -1,6 +1,6 @@
1
- import { SerializationType } from '../enums';
2
- import { SerializeContext } from '../SerializeContext';
3
- import { BaseField } from './BaseField';
1
+ import { SerializationType } from '../enums.js';
2
+ import { SerializeContext } from '../serialize-context.js';
3
+ import { BaseField } from './base-field.js';
4
4
  export declare class GroupColumn extends BaseField {
5
5
  constructor(value: string);
6
6
  get _type(): SerializationType;
@@ -0,0 +1,34 @@
1
+ import { SerializationType } from '../enums.js';
2
+ import { BaseField } from './base-field.js';
3
+ const GROUP_COLUMN_PATTERN = /^((?:[a-zA-Z][\w$]*\.){0,2})([\w$]*)$/;
4
+ export class GroupColumn extends BaseField {
5
+ constructor(value) {
6
+ super();
7
+ const m = value.match(GROUP_COLUMN_PATTERN);
8
+ if (!m)
9
+ throw new TypeError(`"${value}" does not match group column format`);
10
+ this._field = m[2];
11
+ if (m[1]) {
12
+ const a = m[1].split(/\./g);
13
+ a.pop();
14
+ this._table = a.pop();
15
+ this._schema = a.pop();
16
+ }
17
+ }
18
+ get _type() {
19
+ return SerializationType.GROUP_COLUMN;
20
+ }
21
+ _serialize(ctx) {
22
+ const o = {
23
+ schema: this._schema,
24
+ table: this._table,
25
+ field: this._field,
26
+ isReservedWord: !!(this._field && ctx.isReservedWord(this._field))
27
+ };
28
+ return ctx.serialize(this._type, o, () => {
29
+ return (this._schema ? this._schema + '.' : '') +
30
+ (this._table ? this._table + '.' : '') +
31
+ (o.isReservedWord ? '"' + this._field + '"' : this._field);
32
+ });
33
+ }
34
+ }
@@ -1,10 +1,10 @@
1
- import { JoinType, SerializationType } from '../enums';
2
- import { SelectQuery } from '../query/SelectQuery';
3
- import { Serializable } from '../Serializable';
4
- import { SerializeContext } from '../SerializeContext';
5
- import { LogicalOperator } from './operators/LogicalOperator';
6
- import { RawStatement } from './RawStatement';
7
- import { TableName } from './TableName';
1
+ import { JoinType, SerializationType } from '../enums.js';
2
+ import { SelectQuery } from '../query/select-query.js';
3
+ import { Serializable } from '../serializable.js';
4
+ import { SerializeContext } from '../serialize-context.js';
5
+ import { LogicalOperator } from './operators/logical-operator.js';
6
+ import { RawStatement } from './raw-statement.js';
7
+ import { TableName } from './table-name.js';
8
8
  export declare class JoinStatement extends Serializable {
9
9
  _joinType: JoinType;
10
10
  _table: TableName | SelectQuery | RawStatement;
@@ -0,0 +1,79 @@
1
+ import { JoinType, SerializationType } from '../enums.js';
2
+ import { Serializable } from '../serializable.js';
3
+ import { isRawStatement, isSelectQuery, isTableName } from '../typeguards.js';
4
+ import { OpAnd } from './operators/op-and.js';
5
+ import { TableName } from './table-name.js';
6
+ export class JoinStatement extends Serializable {
7
+ _joinType;
8
+ _table;
9
+ _conditions = new OpAnd();
10
+ constructor(joinType, table) {
11
+ super();
12
+ // noinspection SuspiciousTypeOfGuard
13
+ if (!(isSelectQuery(table) || isRawStatement(table) || isTableName(table) ||
14
+ typeof table === 'string'))
15
+ throw new TypeError('Table name, select query or raw object required for Join');
16
+ this._joinType = joinType;
17
+ this._table = typeof table === 'string' ? new TableName(table) : table;
18
+ }
19
+ get _type() {
20
+ return SerializationType.JOIN;
21
+ }
22
+ on(...conditions) {
23
+ this._conditions.add(...conditions);
24
+ return this;
25
+ }
26
+ _serialize(ctx) {
27
+ const o = {
28
+ joinType: this._joinType,
29
+ table: this._table._serialize(ctx),
30
+ conditions: this.__serializeConditions(ctx, this)
31
+ };
32
+ return ctx.serialize(this._type, o, () => {
33
+ let out;
34
+ switch (this._joinType) {
35
+ case JoinType.LEFT:
36
+ out = 'left join';
37
+ break;
38
+ case JoinType.LEFT_OUTER:
39
+ out = 'left outer join';
40
+ break;
41
+ case JoinType.RIGHT:
42
+ out = 'right join';
43
+ break;
44
+ case JoinType.RIGHT_OUTER:
45
+ out = 'right outer join';
46
+ break;
47
+ case JoinType.OUTER:
48
+ out = 'outer join';
49
+ break;
50
+ case JoinType.FULL_OUTER:
51
+ out = 'full outer join';
52
+ break;
53
+ default:
54
+ out = 'inner join';
55
+ break;
56
+ }
57
+ const lf = o.table.length > 40;
58
+ if (isSelectQuery(this._table)) {
59
+ const alias = this._table._alias;
60
+ if (!alias)
61
+ throw new Error('Alias required for sub-select in Join');
62
+ out += ' (' + (lf ? '\n\t' : '') + o.table + (lf ? '\n\b' : '') + ')' +
63
+ ' ' + alias;
64
+ }
65
+ else
66
+ out += ' ' + o.table;
67
+ if (o.conditions)
68
+ out += ' ' + o.conditions;
69
+ return out + (lf ? '\b' : '');
70
+ });
71
+ }
72
+ __serializeConditions(ctx, join) {
73
+ if (join._conditions._items.length) {
74
+ const s = join._conditions._serialize(ctx);
75
+ return ctx.serialize(SerializationType.JOIN_CONDITIONS, s, () => s ? 'on ' + s : '');
76
+ }
77
+ return '';
78
+ }
79
+ }
@@ -1,6 +1,6 @@
1
- import { SerializationType } from '../enums';
2
- import { Serializable } from '../Serializable';
3
- import { SerializeContext } from '../SerializeContext';
1
+ import { SerializationType } from '../enums.js';
2
+ import { Serializable } from '../serializable.js';
3
+ import { SerializeContext } from '../serialize-context.js';
4
4
  export declare class LowerStatement extends Serializable {
5
5
  _expression: any;
6
6
  _alias?: string;
@@ -0,0 +1,37 @@
1
+ import { SerializationType } from '../enums.js';
2
+ import { Serializable } from '../serializable.js';
3
+ export class LowerStatement extends Serializable {
4
+ _expression;
5
+ _alias;
6
+ constructor(expression) {
7
+ super();
8
+ this._expression = expression;
9
+ }
10
+ get _type() {
11
+ return SerializationType.LOWER_STATEMENT;
12
+ }
13
+ /**
14
+ * Sets alias to case expression.
15
+ */
16
+ as(alias) {
17
+ this._alias = alias;
18
+ return this;
19
+ }
20
+ /**
21
+ * Performs serialization
22
+ *
23
+ * @param {Object} ctx
24
+ * @return {string}
25
+ * @override
26
+ */
27
+ _serialize(ctx) {
28
+ if (!this._expression)
29
+ return '';
30
+ const q = ctx.anyToSQL(this._expression);
31
+ return ctx.serialize(this._type, q, () => this.__defaultSerialize(ctx, q));
32
+ }
33
+ __defaultSerialize(ctx, o) {
34
+ return 'lower(' + o + ')' +
35
+ (this._alias ? ' ' + this._alias : '');
36
+ }
37
+ }
@@ -0,0 +1,22 @@
1
+ import { SerializationType } from '../enums.js';
2
+ import { Serializable } from '../serializable.js';
3
+ import { SerializeContext } from '../serialize-context.js';
4
+ export declare class MaxStatement extends Serializable {
5
+ _expression: any;
6
+ _alias?: string;
7
+ constructor(expression: any);
8
+ get _type(): SerializationType;
9
+ /**
10
+ * Sets alias to case expression.
11
+ */
12
+ as(alias: string): this;
13
+ /**
14
+ * Performs serialization
15
+ *
16
+ * @param {Object} ctx
17
+ * @return {string}
18
+ * @override
19
+ */
20
+ _serialize(ctx: SerializeContext): string;
21
+ protected __defaultSerialize(ctx: SerializeContext, o: any): string;
22
+ }
@@ -0,0 +1,37 @@
1
+ import { SerializationType } from '../enums.js';
2
+ import { Serializable } from '../serializable.js';
3
+ export class MaxStatement extends Serializable {
4
+ _expression;
5
+ _alias;
6
+ constructor(expression) {
7
+ super();
8
+ this._expression = expression;
9
+ }
10
+ get _type() {
11
+ return SerializationType.MAX_STATEMENT;
12
+ }
13
+ /**
14
+ * Sets alias to case expression.
15
+ */
16
+ as(alias) {
17
+ this._alias = alias;
18
+ return this;
19
+ }
20
+ /**
21
+ * Performs serialization
22
+ *
23
+ * @param {Object} ctx
24
+ * @return {string}
25
+ * @override
26
+ */
27
+ _serialize(ctx) {
28
+ if (!this._expression)
29
+ return '';
30
+ const q = ctx.anyToSQL(this._expression);
31
+ return ctx.serialize(this._type, q, () => this.__defaultSerialize(ctx, q));
32
+ }
33
+ __defaultSerialize(ctx, o) {
34
+ return 'max(' + o + ')' +
35
+ (this._alias ? ' ' + this._alias : '');
36
+ }
37
+ }
@@ -0,0 +1,22 @@
1
+ import { SerializationType } from '../enums.js';
2
+ import { Serializable } from '../serializable.js';
3
+ import { SerializeContext } from '../serialize-context.js';
4
+ export declare class MinStatement extends Serializable {
5
+ _expression: any;
6
+ _alias?: string;
7
+ constructor(expression: any);
8
+ get _type(): SerializationType;
9
+ /**
10
+ * Sets alias to case expression.
11
+ */
12
+ as(alias: string): this;
13
+ /**
14
+ * Performs serialization
15
+ *
16
+ * @param {Object} ctx
17
+ * @return {string}
18
+ * @override
19
+ */
20
+ _serialize(ctx: SerializeContext): string;
21
+ protected __defaultSerialize(ctx: SerializeContext, o: any): string;
22
+ }
@@ -0,0 +1,37 @@
1
+ import { SerializationType } from '../enums.js';
2
+ import { Serializable } from '../serializable.js';
3
+ export class MinStatement extends Serializable {
4
+ _expression;
5
+ _alias;
6
+ constructor(expression) {
7
+ super();
8
+ this._expression = expression;
9
+ }
10
+ get _type() {
11
+ return SerializationType.MIN_STATEMENT;
12
+ }
13
+ /**
14
+ * Sets alias to case expression.
15
+ */
16
+ as(alias) {
17
+ this._alias = alias;
18
+ return this;
19
+ }
20
+ /**
21
+ * Performs serialization
22
+ *
23
+ * @param {Object} ctx
24
+ * @return {string}
25
+ * @override
26
+ */
27
+ _serialize(ctx) {
28
+ if (!this._expression)
29
+ return '';
30
+ const q = ctx.anyToSQL(this._expression);
31
+ return ctx.serialize(this._type, q, () => this.__defaultSerialize(ctx, q));
32
+ }
33
+ __defaultSerialize(ctx, o) {
34
+ return 'min(' + o + ')' +
35
+ (this._alias ? ' ' + this._alias : '');
36
+ }
37
+ }
@@ -1,5 +1,5 @@
1
- import { OperatorType } from '../enums';
2
- import { Serializable } from '../Serializable';
1
+ import { OperatorType } from '../enums.js';
2
+ import { Serializable } from '../serializable.js';
3
3
  export declare abstract class Operator extends Serializable {
4
4
  abstract _operatorType: OperatorType;
5
5
  }
@@ -0,0 +1,3 @@
1
+ import { Serializable } from '../serializable.js';
2
+ export class Operator extends Serializable {
3
+ }
@@ -1,7 +1,7 @@
1
- import { SerializationType } from '../../enums';
2
- import { Serializable } from '../../Serializable';
3
- import { SerializeContext } from '../../SerializeContext';
4
- import { Operator } from '../Operator';
1
+ import { SerializationType } from '../../enums.js';
2
+ import { Serializable } from '../../serializable.js';
3
+ import { SerializeContext } from '../../serialize-context.js';
4
+ import { Operator } from '../operator.js';
5
5
  export declare abstract class CompOperator extends Operator {
6
6
  _left: Serializable | string;
7
7
  _right?: any | Serializable;
@@ -0,0 +1,86 @@
1
+ import { SerializationType } from '../../enums.js';
2
+ import { Serializable } from '../../serializable.js';
3
+ import { Param } from '../../sqlobject.initializers.js';
4
+ import { FieldExpression } from '../field-expression.js';
5
+ import { Operator } from '../operator.js';
6
+ import { ParamExpression } from '../param-expression.js';
7
+ const EXPRESSION_PATTERN = /^([\w\\.$]+)(\[])?/;
8
+ export class CompOperator extends Operator {
9
+ _left;
10
+ _right;
11
+ _symbol;
12
+ _isArray;
13
+ constructor(left, right) {
14
+ super();
15
+ if (typeof left === 'string') {
16
+ const m = left.match(EXPRESSION_PATTERN);
17
+ if (!m)
18
+ throw new TypeError(`"${left}" is not a valid expression definition`);
19
+ this._left = m[1];
20
+ this._isArray = !!m[2];
21
+ }
22
+ else
23
+ this._left = left;
24
+ this._right = right;
25
+ }
26
+ get _type() {
27
+ return SerializationType.COMPARISON_EXPRESSION;
28
+ }
29
+ _serialize(ctx) {
30
+ const left = this.__serializeItem(ctx, this._left);
31
+ if (this._isArray)
32
+ left.isArray = true;
33
+ const right = this.__serializeItem(ctx, this._right, true);
34
+ const o = {
35
+ operatorType: this._operatorType,
36
+ symbol: this._symbol,
37
+ left,
38
+ right
39
+ };
40
+ return this.__serialize(ctx, o);
41
+ }
42
+ __serializeItem(ctx, x, isRight) {
43
+ if (ctx.strictParams && !(x instanceof Serializable) &&
44
+ (typeof x !== 'string' || isRight)) {
45
+ ctx.strictParamGenId = ctx.strictParamGenId || 0;
46
+ const name = 'strictParam$' + ++ctx.strictParamGenId;
47
+ ctx.params = ctx.params || {};
48
+ ctx.params[name] = x;
49
+ x = Param(name);
50
+ }
51
+ if (x instanceof Serializable) {
52
+ const expression = ctx.anyToSQL(x);
53
+ const result = {
54
+ expression
55
+ };
56
+ if (x instanceof FieldExpression) {
57
+ result.dataType = result._dataType;
58
+ result.isArray = x._isArray;
59
+ }
60
+ if (x instanceof ParamExpression) {
61
+ let value = ctx.params ? ctx.params[x._name] : undefined;
62
+ if (x._isArray && value != null)
63
+ value = [value];
64
+ result.value = value;
65
+ result.isArray = x._isArray || Array.isArray(value);
66
+ result.isParam = true;
67
+ }
68
+ return result;
69
+ }
70
+ else {
71
+ const result = {
72
+ expression: (isRight || typeof x !== 'string') ?
73
+ ctx.anyToSQL(x) : x
74
+ };
75
+ if (isRight || typeof x !== 'string')
76
+ result.isArray = Array.isArray(x);
77
+ return result;
78
+ }
79
+ }
80
+ __serialize(ctx, o) {
81
+ return ctx.serialize(this._type, o, (_ctx, _o) => this.__defaultSerialize(_ctx, _o));
82
+ }
83
+ __defaultSerialize(ctx, o) {
84
+ return o.left.expression + ' ' + o.symbol + ' ' + o.right.expression;
85
+ }
86
+ }
@@ -1,7 +1,7 @@
1
- import { SerializationType } from '../../enums';
2
- import { Serializable } from '../../Serializable';
3
- import { SerializeContext } from '../../SerializeContext';
4
- import { Operator } from '../Operator';
1
+ import { SerializationType } from '../../enums.js';
2
+ import { Serializable } from '../../serializable.js';
3
+ import { SerializeContext } from '../../serialize-context.js';
4
+ import { Operator } from '../operator.js';
5
5
  export declare const WrapOps: {};
6
6
  export declare abstract class LogicalOperator extends Operator {
7
7
  _items: Serializable[];
@@ -0,0 +1,79 @@
1
+ import isPlainObject from 'putil-isplainobject';
2
+ import { SerializationType } from '../../enums.js';
3
+ import { printArray } from '../../helpers.js';
4
+ import { isCompOperator, isLogicalOperator, isRawStatement } from '../../typeguards.js';
5
+ import { Operator } from '../operator.js';
6
+ export const WrapOps = {};
7
+ // noinspection RegExpUnnecessaryNonCapturingGroup
8
+ const COMPARE_LEFT_PATTERN = /^([\w\\.$]+(?:\[])?) *(.*)$/;
9
+ export class LogicalOperator extends Operator {
10
+ _items = [];
11
+ constructor(...expressions) {
12
+ super();
13
+ this.add(...expressions);
14
+ }
15
+ get _type() {
16
+ return SerializationType.LOGICAL_EXPRESSION;
17
+ }
18
+ /**
19
+ * Adds operator(s) to item list
20
+ */
21
+ add(...expressions) {
22
+ for (const item of expressions) {
23
+ if (!item)
24
+ continue;
25
+ if (isLogicalOperator(item)) {
26
+ this._items.push(item);
27
+ }
28
+ else if (isRawStatement(item) || isCompOperator(item)) {
29
+ this._items.push(item);
30
+ }
31
+ else if (isPlainObject(item)) {
32
+ this.add(...this._wrapObject(item));
33
+ }
34
+ else
35
+ throw new TypeError('Operator or Raw type required');
36
+ }
37
+ return this;
38
+ }
39
+ _serialize(ctx) {
40
+ const arr = [];
41
+ for (const t of this._items) {
42
+ const s = ctx.anyToSQL(t);
43
+ /* istanbul ignore else */
44
+ if (s)
45
+ arr.push(s);
46
+ }
47
+ return ctx.serialize(SerializationType.LOGICAL_EXPRESSION, arr, () => {
48
+ const s = printArray(arr, ' ' + String(this._operatorType));
49
+ return (s.indexOf('\n') > 0) ? s.replace('\n', '\n\t') + '\b' : s;
50
+ });
51
+ }
52
+ // noinspection JSMethodCanBeStatic
53
+ _wrapObject(obj) {
54
+ const result = [];
55
+ for (const n of Object.getOwnPropertyNames(obj)) {
56
+ let op;
57
+ const v = obj[n];
58
+ if (['and', 'or'].includes(n.toLowerCase())) {
59
+ op = WrapOps[n.toLowerCase()];
60
+ if (!op)
61
+ throw new Error(`Unknown operator "${n}"`);
62
+ result.push(Array.isArray(v) ? op(...v) : op(v));
63
+ continue;
64
+ }
65
+ if (['exists', '!exists'].includes(n))
66
+ result.push(WrapOps[n](obj[n]));
67
+ else {
68
+ const m = n.match(COMPARE_LEFT_PATTERN);
69
+ if (!m)
70
+ throw new TypeError(`"${n}" is not a valid expression definition`);
71
+ op = WrapOps[m[2] || 'eq'];
72
+ if (!op)
73
+ throw new Error(`Unknown operator "${m[2]}"`);
74
+ result.push(op(m[1], obj[n]));
75
+ }
76
+ }
77
+ return result;
78
+ }
79
+ }
@@ -0,0 +1,5 @@
1
+ import { OperatorType } from '../../enums.js';
2
+ import { LogicalOperator } from './logical-operator.js';
3
+ export declare class OpAnd extends LogicalOperator {
4
+ _operatorType: OperatorType;
5
+ }
@@ -0,0 +1,5 @@
1
+ import { OperatorType } from '../../enums.js';
2
+ import { LogicalOperator } from './logical-operator.js';
3
+ export class OpAnd extends LogicalOperator {
4
+ _operatorType = OperatorType.and;
5
+ }
@@ -1,7 +1,7 @@
1
- import { OperatorType } from '../../enums';
2
- import { Serializable } from '../../Serializable';
3
- import { SerializeContext } from '../../SerializeContext';
4
- import { CompOperator } from './CompOperator';
1
+ import { OperatorType } from '../../enums.js';
2
+ import { Serializable } from '../../serializable.js';
3
+ import { SerializeContext } from '../../serialize-context.js';
4
+ import { CompOperator } from './comp-operator.js';
5
5
  export declare class OpBetween extends CompOperator {
6
6
  _operatorType: OperatorType;
7
7
  _symbol: string;
@@ -0,0 +1,31 @@
1
+ import { OperatorType } from '../../enums.js';
2
+ import { CompOperator } from './comp-operator.js';
3
+ export class OpBetween extends CompOperator {
4
+ _operatorType = OperatorType.between;
5
+ _symbol = 'between';
6
+ constructor(left, right) {
7
+ super(left, right);
8
+ if (right && right[1] == null)
9
+ right[1] = right[0];
10
+ }
11
+ _serialize(ctx) {
12
+ if (!(this._right && this._right.length > 0))
13
+ return '';
14
+ const left = this.__serializeItem(ctx, this._left);
15
+ const right = [
16
+ this.__serializeItem(ctx, this._right[0], true),
17
+ this.__serializeItem(ctx, this._right[1], true)
18
+ ];
19
+ const o = {
20
+ operatorType: this._operatorType,
21
+ symbol: this._symbol,
22
+ left,
23
+ right
24
+ };
25
+ return this.__serialize(ctx, o);
26
+ }
27
+ __defaultSerialize(ctx, o) {
28
+ return o.left.expression + ' ' + o.symbol + ' ' +
29
+ o.right[0].expression + ' and ' + o.right[1].expression;
30
+ }
31
+ }
@@ -1,6 +1,6 @@
1
- import { OperatorType } from '../../enums';
2
- import { Serializable } from '../../Serializable';
3
- import { CompOperator } from './CompOperator';
1
+ import { OperatorType } from '../../enums.js';
2
+ import { Serializable } from '../../serializable.js';
3
+ import { CompOperator } from './comp-operator.js';
4
4
  export declare class OpEq extends CompOperator {
5
5
  _operatorType: OperatorType;
6
6
  _symbol: string;
@@ -0,0 +1,9 @@
1
+ import { OperatorType } from '../../enums.js';
2
+ import { CompOperator } from './comp-operator.js';
3
+ export class OpEq extends CompOperator {
4
+ _operatorType = OperatorType.eq;
5
+ _symbol = '=';
6
+ constructor(left, right) {
7
+ super(left, right);
8
+ }
9
+ }