@sqb/builder 5.0.4 → 5.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/serialize-context.d.ts +1 -0
- package/serialize-context.js +4 -4
- package/sql/delete.js +7 -8
- package/sql/insert.js +5 -7
- package/sql/operators/comp-operator.js +11 -6
- package/sql/operators/in.js +2 -2
- package/sql/query.js +1 -0
- package/sql/select.js +4 -4
- package/sql/update.js +9 -12
- package/type-guards.d.ts +4 -2
- package/type-guards.js +27 -27
package/package.json
CHANGED
package/serialize-context.d.ts
CHANGED
package/serialize-context.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { SerializationType } from './enums.js';
|
|
2
2
|
import { SerializerRegistry } from './extensions.js';
|
|
3
|
-
import { SqlElement } from './serializable.js';
|
|
4
3
|
import { Query } from './sql/index.js';
|
|
5
|
-
import { isLogicalOperator, isQuery,
|
|
4
|
+
import { isLogicalOperator, isQuery, isSqlElement } from './type-guards.js';
|
|
6
5
|
export class SerializeContext {
|
|
7
6
|
rootQuery;
|
|
8
7
|
reservedWords = [
|
|
@@ -66,6 +65,7 @@ export class SerializeContext {
|
|
|
66
65
|
dialect;
|
|
67
66
|
prettyPrint;
|
|
68
67
|
params;
|
|
68
|
+
orgParams;
|
|
69
69
|
dialectVersion;
|
|
70
70
|
strictParams;
|
|
71
71
|
serializeHooks;
|
|
@@ -110,7 +110,7 @@ export class SerializeContext {
|
|
|
110
110
|
')');
|
|
111
111
|
}
|
|
112
112
|
if (typeof v === 'object') {
|
|
113
|
-
if (
|
|
113
|
+
if (isSqlElement(v)) {
|
|
114
114
|
const s = v._serialize(this);
|
|
115
115
|
return s
|
|
116
116
|
? isQuery(v) || isLogicalOperator(v)
|
|
@@ -132,7 +132,7 @@ export class SerializeContext {
|
|
|
132
132
|
if (typeof v === 'number') {
|
|
133
133
|
return this.serialize(SerializationType.NUMBER_VALUE, v, () => this.numberToSQL(v));
|
|
134
134
|
}
|
|
135
|
-
if (v
|
|
135
|
+
if (isSqlElement(v))
|
|
136
136
|
return v._serialize(this);
|
|
137
137
|
return v;
|
|
138
138
|
}
|
package/sql/delete.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SerializationType } from '../enums.js';
|
|
2
2
|
import { SerializeContext } from '../serialize-context.js';
|
|
3
|
-
import { isRaw } from '../type-guards.js';
|
|
3
|
+
import { isRaw, isSqlElement } from '../type-guards.js';
|
|
4
4
|
import { Raw } from './elements/raw.js';
|
|
5
5
|
import { TableName } from './elements/table-name.js';
|
|
6
6
|
import { And } from './operators/and.js';
|
|
@@ -50,17 +50,16 @@ export const Delete = function (tableName) {
|
|
|
50
50
|
return new Delete(tableName);
|
|
51
51
|
Query.call(this);
|
|
52
52
|
if (!(tableName &&
|
|
53
|
-
(tableName
|
|
53
|
+
(isSqlElement(tableName, SerializationType.TABLE_NAME) ||
|
|
54
54
|
typeof tableName === 'string' ||
|
|
55
55
|
isRaw(tableName)))) {
|
|
56
56
|
throw new TypeError('String or Raw instance required as first argument (tableName) for Delete');
|
|
57
57
|
}
|
|
58
|
-
this._table =
|
|
59
|
-
tableName
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
: tableName;
|
|
58
|
+
this._table = isSqlElement(tableName, SerializationType.TABLE_NAME)
|
|
59
|
+
? tableName
|
|
60
|
+
: typeof tableName === 'string'
|
|
61
|
+
? TableName(tableName)
|
|
62
|
+
: tableName;
|
|
64
63
|
};
|
|
65
64
|
Delete.prototype = DeleteClass.prototype;
|
|
66
65
|
Delete.prototype.constructor = Delete;
|
package/sql/insert.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { SerializationType } from '../enums.js';
|
|
2
2
|
import { printArray } from '../helpers.js';
|
|
3
|
-
import { SqlElement } from '../serializable.js';
|
|
4
3
|
import { SerializeContext } from '../serialize-context.js';
|
|
5
|
-
import { isRaw, isSelect,
|
|
4
|
+
import { isRaw, isSelect, isSqlElement } from '../type-guards.js';
|
|
6
5
|
import { Raw } from './elements/raw.js';
|
|
7
6
|
import { TableName } from './elements/table-name.js';
|
|
8
7
|
import { Query } from './query.js';
|
|
@@ -60,7 +59,7 @@ class InsertClass extends ReturningQuery {
|
|
|
60
59
|
*
|
|
61
60
|
*/
|
|
62
61
|
__serializeValues(ctx) {
|
|
63
|
-
if (
|
|
62
|
+
if (isSqlElement(this._input))
|
|
64
63
|
return this._input._serialize(ctx);
|
|
65
64
|
const arr = [];
|
|
66
65
|
const allValues = this._input;
|
|
@@ -80,9 +79,8 @@ export const Insert = function (tableName, input) {
|
|
|
80
79
|
}
|
|
81
80
|
if (!input ||
|
|
82
81
|
!((typeof input === 'object' && !Array.isArray(input)) ||
|
|
83
|
-
(input
|
|
84
|
-
|
|
85
|
-
input._type === SerializationType.RAW)))) {
|
|
82
|
+
isSelect(input) ||
|
|
83
|
+
isRaw(input))) {
|
|
86
84
|
throw new TypeError('Object or Select instance required as second argument (input) for Insert');
|
|
87
85
|
}
|
|
88
86
|
this._table =
|
|
@@ -96,5 +94,5 @@ Insert.prototype.constructor = Insert;
|
|
|
96
94
|
* @param value
|
|
97
95
|
*/
|
|
98
96
|
export function isInsertQuery(value) {
|
|
99
|
-
return (
|
|
97
|
+
return isSqlElement(value, SerializationType.INSERT_QUERY);
|
|
100
98
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { SerializationType } from '../../enums.js';
|
|
2
2
|
import { SqlElement } from '../../serializable.js';
|
|
3
3
|
import { SerializeContext } from '../../serialize-context.js';
|
|
4
|
-
import {
|
|
4
|
+
import { isFieldName, isParam, isSqlElement } from '../../type-guards.js';
|
|
5
|
+
import { Param } from '../elements/index.js';
|
|
5
6
|
import { Operator } from './operator.js';
|
|
6
7
|
const EXPRESSION_PATTERN = /^([\w\\.$]+)(\[])?/;
|
|
7
8
|
class CompOperatorClass extends Operator {
|
|
@@ -22,38 +23,42 @@ class CompOperatorClass extends Operator {
|
|
|
22
23
|
symbol: this._symbol,
|
|
23
24
|
left,
|
|
24
25
|
right,
|
|
26
|
+
orgLeft: this._left,
|
|
27
|
+
orgRight: this._right,
|
|
25
28
|
};
|
|
26
29
|
return this.__serialize(ctx, o);
|
|
27
30
|
}
|
|
28
31
|
__serializeItem(ctx, x, left) {
|
|
29
32
|
const isRight = !!left;
|
|
30
|
-
if (ctx.strictParams && !(x
|
|
33
|
+
if (ctx.strictParams && !isSqlElement(x) && isRight) {
|
|
31
34
|
ctx.strictParamGenId = ctx.strictParamGenId || 0;
|
|
32
35
|
const name = 'P$_' + ++ctx.strictParamGenId;
|
|
33
36
|
ctx.params = ctx.params || {};
|
|
37
|
+
ctx.orgParams = ctx.orgParams || {};
|
|
34
38
|
ctx.params[name] = x;
|
|
39
|
+
ctx.orgParams[name] = x;
|
|
35
40
|
x = Param({
|
|
36
41
|
name,
|
|
37
42
|
dataType: left?.dataType,
|
|
38
43
|
isArray: left?.isArray || Array.isArray(x),
|
|
39
44
|
});
|
|
40
45
|
}
|
|
41
|
-
if (x
|
|
46
|
+
if (isSqlElement(x)) {
|
|
42
47
|
const expression = ctx.anyToSQL(x);
|
|
43
48
|
const result = {
|
|
44
49
|
expression,
|
|
45
50
|
};
|
|
46
|
-
if (x
|
|
51
|
+
if (isFieldName(x)) {
|
|
47
52
|
result.dataType = x._dataType;
|
|
48
53
|
result.isArray = x._isArray;
|
|
49
54
|
}
|
|
50
|
-
if (x
|
|
55
|
+
if (isParam(x)) {
|
|
51
56
|
let value = ctx.params ? ctx.params[x._name] : undefined;
|
|
52
57
|
if (x._isArray && value != null && !Array.isArray(value))
|
|
53
58
|
value = [value];
|
|
54
59
|
result.value = value;
|
|
55
60
|
result.isArray = x._isArray || Array.isArray(value);
|
|
56
|
-
result.isParam =
|
|
61
|
+
result.isParam = ctx.params?.[x._name] != undefined;
|
|
57
62
|
}
|
|
58
63
|
return result;
|
|
59
64
|
}
|
package/sql/operators/in.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { OperatorType } from '../../enums.js';
|
|
2
2
|
import { SerializeContext } from '../../serialize-context.js';
|
|
3
|
-
import {
|
|
3
|
+
import { isSqlElement } from '../../type-guards.js';
|
|
4
4
|
import { CompOperator } from './comp-operator.js';
|
|
5
5
|
class InClass extends CompOperator {
|
|
6
6
|
_serialize(ctx) {
|
|
@@ -22,7 +22,7 @@ export const In = function (left, right) {
|
|
|
22
22
|
this._left = m[1];
|
|
23
23
|
this._isArray = !!m[2];
|
|
24
24
|
}
|
|
25
|
-
this._right = Array.isArray(right) ||
|
|
25
|
+
this._right = Array.isArray(right) || isSqlElement(right) ? right : [right];
|
|
26
26
|
};
|
|
27
27
|
In.prototype = InClass.prototype;
|
|
28
28
|
In.prototype.constructor = In;
|
package/sql/query.js
CHANGED
|
@@ -11,6 +11,7 @@ class QueryClass extends SqlElement {
|
|
|
11
11
|
const ctx = new SerializeContext(this, options);
|
|
12
12
|
if (this._params)
|
|
13
13
|
ctx.params = { ...ctx.params, ...this._params };
|
|
14
|
+
ctx.orgParams = { ...ctx.params };
|
|
14
15
|
ctx.serializeHooks = this.listeners('serialize');
|
|
15
16
|
/* generate output */
|
|
16
17
|
let sql = this._serialize(ctx);
|
package/sql/select.js
CHANGED
|
@@ -4,7 +4,7 @@ import { SerializationType } from '../enums.js';
|
|
|
4
4
|
import { printArray } from '../helpers.js';
|
|
5
5
|
import { SqlElement } from '../serializable.js';
|
|
6
6
|
import { SerializeContext } from '../serialize-context.js';
|
|
7
|
-
import { isJoin } from '../type-guards.js';
|
|
7
|
+
import { isJoin, isSelect, isTableName } from '../type-guards.js';
|
|
8
8
|
import { Field } from './elements/field.js';
|
|
9
9
|
import { GroupColumn } from './elements/group-column.js';
|
|
10
10
|
import { Join } from './elements/join.js';
|
|
@@ -66,7 +66,7 @@ class SelectClass extends Query {
|
|
|
66
66
|
for (const arg of table) {
|
|
67
67
|
if (!arg)
|
|
68
68
|
continue;
|
|
69
|
-
this._tables.push(arg
|
|
69
|
+
this._tables.push(isTableName(arg)
|
|
70
70
|
? arg
|
|
71
71
|
: typeof arg === 'string'
|
|
72
72
|
? new TableName(arg)
|
|
@@ -217,7 +217,7 @@ class SelectClass extends Query {
|
|
|
217
217
|
const s = ctx.anyToSQL(t);
|
|
218
218
|
// t._serialize(ctx);
|
|
219
219
|
if (s) {
|
|
220
|
-
if (t
|
|
220
|
+
if (isSelect(t)) {
|
|
221
221
|
if (!t._alias)
|
|
222
222
|
throw new TypeError('Alias required for sub-select in columns');
|
|
223
223
|
arr.push(s + ' ' + t._alias);
|
|
@@ -239,7 +239,7 @@ class SelectClass extends Query {
|
|
|
239
239
|
const s = t._serialize(ctx);
|
|
240
240
|
/* istanbul ignore else */
|
|
241
241
|
if (s) {
|
|
242
|
-
if (t
|
|
242
|
+
if (isSelect(t)) {
|
|
243
243
|
if (!t._alias)
|
|
244
244
|
throw new TypeError('Alias required for sub-select in "from"');
|
|
245
245
|
arr.push({
|
package/sql/update.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { SerializationType } from '../enums.js';
|
|
2
2
|
import { printArray } from '../helpers.js';
|
|
3
|
-
import { SqlElement } from '../serializable.js';
|
|
4
3
|
import { SerializeContext } from '../serialize-context.js';
|
|
5
|
-
import { isRaw } from '../type-guards.js';
|
|
4
|
+
import { isRaw, isSelect, isTableName } from '../type-guards.js';
|
|
6
5
|
import { Raw } from './elements/raw.js';
|
|
7
6
|
import { TableName } from './elements/table-name.js';
|
|
8
7
|
import { And } from './operators/and.js';
|
|
@@ -76,24 +75,22 @@ export const Update = function (tableName, input) {
|
|
|
76
75
|
return new Update(tableName, input);
|
|
77
76
|
Query.call(this);
|
|
78
77
|
if (!(tableName &&
|
|
79
|
-
(tableName
|
|
78
|
+
(isTableName(tableName) ||
|
|
80
79
|
typeof tableName === 'string' ||
|
|
81
80
|
isRaw(tableName)))) {
|
|
82
81
|
throw new TypeError('String or Raw instance required as first argument (tableName) for Update');
|
|
83
82
|
}
|
|
84
83
|
if (!input ||
|
|
85
84
|
!((typeof input === 'object' && !Array.isArray(input)) ||
|
|
86
|
-
(input
|
|
87
|
-
|
|
88
|
-
input._type === SerializationType.RAW)))) {
|
|
85
|
+
isSelect(input) ||
|
|
86
|
+
isRaw(input))) {
|
|
89
87
|
throw new TypeError('Object or Raw instance required as second argument (input) for Update');
|
|
90
88
|
}
|
|
91
|
-
this._table =
|
|
92
|
-
tableName
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
: tableName;
|
|
89
|
+
this._table = isTableName(tableName)
|
|
90
|
+
? tableName
|
|
91
|
+
: typeof tableName === 'string'
|
|
92
|
+
? new TableName(tableName)
|
|
93
|
+
: tableName;
|
|
97
94
|
this._input = input;
|
|
98
95
|
};
|
|
99
96
|
Update.prototype = UpdateClass.prototype;
|
package/type-guards.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { SerializationType } from './enums.js';
|
|
1
2
|
import type { SqlElement } from './serializable.js';
|
|
2
3
|
import type { Case, CompOperator, Count, Delete, Field, GroupColumn, Insert, Join, LogicalOperator, OrderColumn, Param, Query, Raw, ReturningColumn, Select, TableName, Update } from './sql/index.js';
|
|
3
|
-
export declare function isSqlElement(value: any): value is SqlElement;
|
|
4
|
-
export declare
|
|
4
|
+
export declare function isSqlElement(value: any, type?: SerializationType): value is SqlElement;
|
|
5
|
+
export declare const isSerializable: typeof isSqlElement;
|
|
5
6
|
export declare function isQuery(value: any): value is Query;
|
|
6
7
|
export declare function isRaw(value: any): value is Raw;
|
|
7
8
|
export declare function isSelect(value: any): value is Select;
|
|
@@ -20,3 +21,4 @@ export declare function isOrderColumn(value: any): value is OrderColumn;
|
|
|
20
21
|
export declare function isGroupColumn(value: any): value is GroupColumn;
|
|
21
22
|
export declare function isReturningColumn(value: any): value is ReturningColumn;
|
|
22
23
|
export declare function isTableName(value: any): value is TableName;
|
|
24
|
+
export declare function isFieldName(value: any): value is Field;
|
package/type-guards.js
CHANGED
|
@@ -1,68 +1,68 @@
|
|
|
1
1
|
import { SerializationType } from './enums.js';
|
|
2
|
-
export function isSqlElement(value) {
|
|
3
|
-
return
|
|
2
|
+
export function isSqlElement(value, type) {
|
|
3
|
+
return (value &&
|
|
4
|
+
typeof value === 'object' &&
|
|
5
|
+
typeof value._serialize === 'function' &&
|
|
6
|
+
(!type || value._type === type));
|
|
4
7
|
}
|
|
5
8
|
/* Backward compatibility */
|
|
6
|
-
export
|
|
7
|
-
return isSqlElement(value);
|
|
8
|
-
}
|
|
9
|
+
export const isSerializable = isSqlElement;
|
|
9
10
|
export function isQuery(value) {
|
|
10
|
-
return (
|
|
11
|
+
return (isSqlElement(value) &&
|
|
11
12
|
typeof value.generate === 'function' &&
|
|
12
13
|
typeof value.values === 'function');
|
|
13
14
|
}
|
|
14
15
|
export function isRaw(value) {
|
|
15
|
-
return
|
|
16
|
+
return isSqlElement(value, SerializationType.RAW);
|
|
16
17
|
}
|
|
17
18
|
export function isSelect(value) {
|
|
18
|
-
return (
|
|
19
|
+
return isSqlElement(value, SerializationType.SELECT_QUERY);
|
|
19
20
|
}
|
|
20
21
|
export function isInsert(value) {
|
|
21
|
-
return (
|
|
22
|
+
return isSqlElement(value, SerializationType.INSERT_QUERY);
|
|
22
23
|
}
|
|
23
24
|
export function isIUpdate(value) {
|
|
24
|
-
return (
|
|
25
|
+
return isSqlElement(value, SerializationType.UPDATE_QUERY);
|
|
25
26
|
}
|
|
26
27
|
export function isDelete(value) {
|
|
27
|
-
return (
|
|
28
|
+
return isSqlElement(value, SerializationType.DELETE_QUERY);
|
|
28
29
|
}
|
|
29
30
|
export function isJoin(value) {
|
|
30
|
-
return
|
|
31
|
+
return isSqlElement(value) && value._type === SerializationType.JOIN;
|
|
31
32
|
}
|
|
32
33
|
export function isCase(value) {
|
|
33
|
-
return (
|
|
34
|
+
return isSqlElement(value, SerializationType.CASE_STATEMENT);
|
|
34
35
|
}
|
|
35
36
|
export function isCount(value) {
|
|
36
|
-
return (
|
|
37
|
+
return isSqlElement(value, SerializationType.COUNT_STATEMENT);
|
|
37
38
|
}
|
|
38
39
|
export function isParam(value) {
|
|
39
|
-
return (
|
|
40
|
-
value._type === SerializationType.EXTERNAL_PARAMETER);
|
|
40
|
+
return isSqlElement(value, SerializationType.EXTERNAL_PARAMETER);
|
|
41
41
|
}
|
|
42
42
|
export function isLogicalOperator(value) {
|
|
43
|
-
return (
|
|
44
|
-
value._type === SerializationType.LOGICAL_EXPRESSION);
|
|
43
|
+
return isSqlElement(value, SerializationType.LOGICAL_EXPRESSION);
|
|
45
44
|
}
|
|
46
45
|
export function isCompOperator(value) {
|
|
47
|
-
return (
|
|
48
|
-
value._type === SerializationType.COMPARISON_EXPRESSION);
|
|
46
|
+
return isSqlElement(value, SerializationType.COMPARISON_EXPRESSION);
|
|
49
47
|
}
|
|
50
48
|
export function isNot(value) {
|
|
51
|
-
return (
|
|
52
|
-
value._type === SerializationType.NEGATIVE_EXPRESSION);
|
|
49
|
+
return isSqlElement(value, SerializationType.NEGATIVE_EXPRESSION);
|
|
53
50
|
}
|
|
54
51
|
export function isSelectColumn(value) {
|
|
55
|
-
return
|
|
52
|
+
return isSqlElement(value, SerializationType.FIELD_NAME);
|
|
56
53
|
}
|
|
57
54
|
export function isOrderColumn(value) {
|
|
58
|
-
return (
|
|
55
|
+
return isSqlElement(value, SerializationType.ORDER_COLUMN);
|
|
59
56
|
}
|
|
60
57
|
export function isGroupColumn(value) {
|
|
61
|
-
return (
|
|
58
|
+
return isSqlElement(value, SerializationType.GROUP_COLUMN);
|
|
62
59
|
}
|
|
63
60
|
export function isReturningColumn(value) {
|
|
64
|
-
return (
|
|
61
|
+
return isSqlElement(value, SerializationType.RETURNING_COLUMN);
|
|
65
62
|
}
|
|
66
63
|
export function isTableName(value) {
|
|
67
|
-
return
|
|
64
|
+
return isSqlElement(value, SerializationType.TABLE_NAME);
|
|
65
|
+
}
|
|
66
|
+
export function isFieldName(value) {
|
|
67
|
+
return isSqlElement(value, SerializationType.FIELD_NAME);
|
|
68
68
|
}
|