@stamhoofd/sql 2.118.1 → 2.120.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{src/ModelCache.d.ts → ModelCache.d.ts} +1 -1
- package/dist/ModelCache.d.ts.map +1 -0
- package/{src/ModelCache.ts → dist/ModelCache.js} +8 -16
- package/dist/ModelCache.js.map +1 -0
- package/dist/{src/QueryableModel.d.ts → QueryableModel.d.ts} +7 -6
- package/dist/QueryableModel.d.ts.map +1 -0
- package/dist/{src/QueryableModel.js → QueryableModel.js} +9 -13
- package/dist/QueryableModel.js.map +1 -0
- package/dist/{src/SQL.d.ts → SQL.d.ts} +7 -5
- package/dist/SQL.d.ts.map +1 -0
- package/dist/SQL.js +100 -0
- package/dist/SQL.js.map +1 -0
- package/dist/{src/SQLDelete.d.ts → SQLDelete.d.ts} +2 -2
- package/dist/SQLDelete.d.ts.map +1 -0
- package/dist/{src/SQLDelete.js → SQLDelete.js} +8 -12
- package/dist/SQLDelete.js.map +1 -0
- package/dist/SQLExpression.d.ts.map +1 -0
- package/dist/{src/SQLExpression.js → SQLExpression.js} +3 -8
- package/dist/SQLExpression.js.map +1 -0
- package/dist/{src/SQLExpressions.d.ts → SQLExpressions.d.ts} +2 -2
- package/dist/SQLExpressions.d.ts.map +1 -0
- package/dist/{src/SQLExpressions.js → SQLExpressions.js} +78 -115
- package/dist/SQLExpressions.js.map +1 -0
- package/dist/{src/SQLInsert.d.ts → SQLInsert.d.ts} +3 -2
- package/dist/SQLInsert.d.ts.map +1 -0
- package/dist/{src/SQLInsert.js → SQLInsert.js} +17 -21
- package/dist/SQLInsert.js.map +1 -0
- package/dist/{src/SQLJoin.d.ts → SQLJoin.d.ts} +1 -1
- package/dist/SQLJoin.d.ts.map +1 -0
- package/{src/SQLJoin.ts → dist/SQLJoin.js} +15 -18
- package/dist/SQLJoin.js.map +1 -0
- package/dist/{src/SQLJsonExpressions.d.ts → SQLJsonExpressions.d.ts} +2 -2
- package/dist/SQLJsonExpressions.d.ts.map +1 -0
- package/dist/{src/SQLJsonExpressions.js → SQLJsonExpressions.js} +32 -45
- package/dist/SQLJsonExpressions.js.map +1 -0
- package/dist/SQLLogger.d.ts.map +1 -0
- package/dist/{src/SQLLogger.js → SQLLogger.js} +5 -8
- package/dist/SQLLogger.js.map +1 -0
- package/dist/{src/SQLOrderBy.d.ts → SQLOrderBy.d.ts} +1 -1
- package/dist/SQLOrderBy.d.ts.map +1 -0
- package/dist/{src/SQLOrderBy.js → SQLOrderBy.js} +8 -13
- package/dist/SQLOrderBy.js.map +1 -0
- package/dist/{src/SQLSelect.d.ts → SQLSelect.d.ts} +3 -3
- package/dist/SQLSelect.d.ts.map +1 -0
- package/dist/{src/SQLSelect.js → SQLSelect.js} +24 -29
- package/dist/SQLSelect.js.map +1 -0
- package/dist/{src/SQLTranslatedStringHelper.d.ts → SQLTranslatedStringHelper.d.ts} +3 -2
- package/dist/SQLTranslatedStringHelper.d.ts.map +1 -0
- package/dist/SQLTranslatedStringHelper.js +33 -0
- package/dist/SQLTranslatedStringHelper.js.map +1 -0
- package/dist/{src/SQLUpdate.d.ts → SQLUpdate.d.ts} +3 -2
- package/dist/SQLUpdate.d.ts.map +1 -0
- package/{src/SQLUpdate.ts → dist/SQLUpdate.js} +18 -35
- package/dist/SQLUpdate.js.map +1 -0
- package/dist/{src/SQLWhere.d.ts → SQLWhere.d.ts} +3 -3
- package/dist/SQLWhere.d.ts.map +1 -0
- package/dist/{src/SQLWhere.js → SQLWhere.js} +42 -56
- package/dist/SQLWhere.js.map +1 -0
- package/dist/{src/filters → filters}/SQLFilter.d.ts +10 -6
- package/dist/filters/SQLFilter.d.ts.map +1 -0
- package/{src/filters/SQLFilter.ts → dist/filters/SQLFilter.js} +66 -127
- package/dist/filters/SQLFilter.js.map +1 -0
- package/dist/{src/filters → filters}/SQLSorter.d.ts +6 -6
- package/dist/filters/SQLSorter.d.ts.map +1 -0
- package/dist/{src/filters → filters}/SQLSorter.js +5 -8
- package/dist/filters/SQLSorter.js.map +1 -0
- package/dist/filters/compilers/contains.d.ts +4 -0
- package/dist/filters/compilers/contains.d.ts.map +1 -0
- package/dist/filters/compilers/contains.js +26 -0
- package/dist/filters/compilers/contains.js.map +1 -0
- package/dist/filters/compilers/equals.d.ts +4 -0
- package/dist/filters/compilers/equals.d.ts.map +1 -0
- package/dist/filters/compilers/equals.js +44 -0
- package/dist/filters/compilers/equals.js.map +1 -0
- package/dist/filters/compilers/greater.d.ts +4 -0
- package/dist/filters/compilers/greater.d.ts.map +1 -0
- package/dist/filters/compilers/greater.js +15 -0
- package/dist/filters/compilers/greater.js.map +1 -0
- package/dist/filters/compilers/in.d.ts +4 -0
- package/dist/filters/compilers/in.d.ts.map +1 -0
- package/{src/filters/compilers/in.ts → dist/filters/compilers/in.js} +11 -26
- package/dist/filters/compilers/in.js.map +1 -0
- package/dist/filters/compilers/index.d.ts.map +1 -0
- package/{src/filters/compilers/index.ts → dist/filters/compilers/index.js} +1 -0
- package/dist/filters/compilers/index.js.map +1 -0
- package/dist/filters/compilers/less.d.ts +4 -0
- package/dist/filters/compilers/less.d.ts.map +1 -0
- package/dist/filters/compilers/less.js +15 -0
- package/dist/filters/compilers/less.js.map +1 -0
- package/dist/filters/helpers/isJSONColumn.d.ts +5 -0
- package/dist/filters/helpers/isJSONColumn.d.ts.map +1 -0
- package/{src/filters/helpers/isJSONColumn.ts → dist/filters/helpers/isJSONColumn.js} +5 -5
- package/dist/filters/helpers/isJSONColumn.js.map +1 -0
- package/dist/{src/filters → filters}/helpers/normalizeCompareValue.d.ts +1 -1
- package/dist/filters/helpers/normalizeCompareValue.d.ts.map +1 -0
- package/{src/filters/helpers/normalizeCompareValue.ts → dist/filters/helpers/normalizeCompareValue.js} +3 -20
- package/dist/filters/helpers/normalizeCompareValue.js.map +1 -0
- package/dist/index.d.ts +14 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -25
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
- package/dist/src/ModelCache.d.ts.map +0 -1
- package/dist/src/ModelCache.js +0 -57
- package/dist/src/ModelCache.js.map +0 -1
- package/dist/src/QueryableModel.d.ts.map +0 -1
- package/dist/src/QueryableModel.js.map +0 -1
- package/dist/src/SQL.d.ts.map +0 -1
- package/dist/src/SQL.js +0 -103
- package/dist/src/SQL.js.map +0 -1
- package/dist/src/SQLDelete.d.ts.map +0 -1
- package/dist/src/SQLDelete.js.map +0 -1
- package/dist/src/SQLExpression.d.ts.map +0 -1
- package/dist/src/SQLExpression.js.map +0 -1
- package/dist/src/SQLExpressions.d.ts.map +0 -1
- package/dist/src/SQLExpressions.js.map +0 -1
- package/dist/src/SQLInsert.d.ts.map +0 -1
- package/dist/src/SQLInsert.js.map +0 -1
- package/dist/src/SQLJoin.d.ts.map +0 -1
- package/dist/src/SQLJoin.js +0 -61
- package/dist/src/SQLJoin.js.map +0 -1
- package/dist/src/SQLJsonExpressions.d.ts.map +0 -1
- package/dist/src/SQLJsonExpressions.js.map +0 -1
- package/dist/src/SQLLogger.d.ts.map +0 -1
- package/dist/src/SQLLogger.js.map +0 -1
- package/dist/src/SQLOrderBy.d.ts.map +0 -1
- package/dist/src/SQLOrderBy.js.map +0 -1
- package/dist/src/SQLSelect.d.ts.map +0 -1
- package/dist/src/SQLSelect.js.map +0 -1
- package/dist/src/SQLTranslatedStringHelper.d.ts.map +0 -1
- package/dist/src/SQLTranslatedStringHelper.js +0 -37
- package/dist/src/SQLTranslatedStringHelper.js.map +0 -1
- package/dist/src/SQLUpdate.d.ts.map +0 -1
- package/dist/src/SQLUpdate.js +0 -60
- package/dist/src/SQLUpdate.js.map +0 -1
- package/dist/src/SQLWhere.d.ts.map +0 -1
- package/dist/src/SQLWhere.js.map +0 -1
- package/dist/src/filters/SQLFilter.d.ts.map +0 -1
- package/dist/src/filters/SQLFilter.js +0 -216
- package/dist/src/filters/SQLFilter.js.map +0 -1
- package/dist/src/filters/SQLSorter.d.ts.map +0 -1
- package/dist/src/filters/SQLSorter.js.map +0 -1
- package/dist/src/filters/compilers/contains.d.ts +0 -4
- package/dist/src/filters/compilers/contains.d.ts.map +0 -1
- package/dist/src/filters/compilers/contains.js +0 -28
- package/dist/src/filters/compilers/contains.js.map +0 -1
- package/dist/src/filters/compilers/equals.d.ts +0 -4
- package/dist/src/filters/compilers/equals.d.ts.map +0 -1
- package/dist/src/filters/compilers/equals.js +0 -46
- package/dist/src/filters/compilers/equals.js.map +0 -1
- package/dist/src/filters/compilers/greater.d.ts +0 -4
- package/dist/src/filters/compilers/greater.d.ts.map +0 -1
- package/dist/src/filters/compilers/greater.js +0 -17
- package/dist/src/filters/compilers/greater.js.map +0 -1
- package/dist/src/filters/compilers/in.d.ts +0 -4
- package/dist/src/filters/compilers/in.d.ts.map +0 -1
- package/dist/src/filters/compilers/in.js +0 -51
- package/dist/src/filters/compilers/in.js.map +0 -1
- package/dist/src/filters/compilers/index.d.ts.map +0 -1
- package/dist/src/filters/compilers/index.js +0 -8
- package/dist/src/filters/compilers/index.js.map +0 -1
- package/dist/src/filters/compilers/less.d.ts +0 -4
- package/dist/src/filters/compilers/less.d.ts.map +0 -1
- package/dist/src/filters/compilers/less.js +0 -17
- package/dist/src/filters/compilers/less.js.map +0 -1
- package/dist/src/filters/helpers/isJSONColumn.d.ts +0 -4
- package/dist/src/filters/helpers/isJSONColumn.d.ts.map +0 -1
- package/dist/src/filters/helpers/isJSONColumn.js +0 -16
- package/dist/src/filters/helpers/isJSONColumn.js.map +0 -1
- package/dist/src/filters/helpers/normalizeCompareValue.d.ts.map +0 -1
- package/dist/src/filters/helpers/normalizeCompareValue.js +0 -87
- package/dist/src/filters/helpers/normalizeCompareValue.js.map +0 -1
- package/dist/tests/filters/$and.test.d.ts +0 -2
- package/dist/tests/filters/$and.test.d.ts.map +0 -1
- package/dist/tests/filters/$and.test.js +0 -216
- package/dist/tests/filters/$and.test.js.map +0 -1
- package/dist/tests/filters/$contains.test.d.ts +0 -2
- package/dist/tests/filters/$contains.test.d.ts.map +0 -1
- package/dist/tests/filters/$contains.test.js +0 -701
- package/dist/tests/filters/$contains.test.js.map +0 -1
- package/dist/tests/filters/$eq.test.d.ts +0 -2
- package/dist/tests/filters/$eq.test.d.ts.map +0 -1
- package/dist/tests/filters/$eq.test.js +0 -1048
- package/dist/tests/filters/$eq.test.js.map +0 -1
- package/dist/tests/filters/$gt.test.d.ts +0 -2
- package/dist/tests/filters/$gt.test.d.ts.map +0 -1
- package/dist/tests/filters/$gt.test.js +0 -463
- package/dist/tests/filters/$gt.test.js.map +0 -1
- package/dist/tests/filters/$gte.test.d.ts +0 -2
- package/dist/tests/filters/$gte.test.d.ts.map +0 -1
- package/dist/tests/filters/$gte.test.js +0 -433
- package/dist/tests/filters/$gte.test.js.map +0 -1
- package/dist/tests/filters/$in.test.d.ts +0 -2
- package/dist/tests/filters/$in.test.d.ts.map +0 -1
- package/dist/tests/filters/$in.test.js +0 -590
- package/dist/tests/filters/$in.test.js.map +0 -1
- package/dist/tests/filters/$lt.test.d.ts +0 -2
- package/dist/tests/filters/$lt.test.d.ts.map +0 -1
- package/dist/tests/filters/$lt.test.js +0 -433
- package/dist/tests/filters/$lt.test.js.map +0 -1
- package/dist/tests/filters/$lte.test.d.ts +0 -2
- package/dist/tests/filters/$lte.test.d.ts.map +0 -1
- package/dist/tests/filters/$lte.test.js +0 -472
- package/dist/tests/filters/$lte.test.js.map +0 -1
- package/dist/tests/filters/$neq.test.d.ts +0 -2
- package/dist/tests/filters/$neq.test.d.ts.map +0 -1
- package/dist/tests/filters/$neq.test.js +0 -32
- package/dist/tests/filters/$neq.test.js.map +0 -1
- package/dist/tests/filters/$not.test.d.ts +0 -2
- package/dist/tests/filters/$not.test.d.ts.map +0 -1
- package/dist/tests/filters/$not.test.js +0 -50
- package/dist/tests/filters/$not.test.js.map +0 -1
- package/dist/tests/filters/$or.test.d.ts +0 -2
- package/dist/tests/filters/$or.test.d.ts.map +0 -1
- package/dist/tests/filters/$or.test.js +0 -185
- package/dist/tests/filters/$or.test.js.map +0 -1
- package/dist/tests/filters/SQLTranslatedStringHelper.test.d.ts +0 -2
- package/dist/tests/filters/SQLTranslatedStringHelper.test.d.ts.map +0 -1
- package/dist/tests/filters/SQLTranslatedStringHelper.test.js +0 -491
- package/dist/tests/filters/SQLTranslatedStringHelper.test.js.map +0 -1
- package/dist/tests/filters/dot-syntax.test.d.ts +0 -2
- package/dist/tests/filters/dot-syntax.test.d.ts.map +0 -1
- package/dist/tests/filters/dot-syntax.test.js +0 -210
- package/dist/tests/filters/dot-syntax.test.js.map +0 -1
- package/dist/tests/filters/exists.test.d.ts +0 -2
- package/dist/tests/filters/exists.test.d.ts.map +0 -1
- package/dist/tests/filters/exists.test.js +0 -96
- package/dist/tests/filters/exists.test.js.map +0 -1
- package/dist/tests/filters/joined-relations.test.d.ts +0 -2
- package/dist/tests/filters/joined-relations.test.d.ts.map +0 -1
- package/dist/tests/filters/joined-relations.test.js +0 -167
- package/dist/tests/filters/joined-relations.test.js.map +0 -1
- package/dist/tests/filters/special-cases.test.d.ts +0 -2
- package/dist/tests/filters/special-cases.test.d.ts.map +0 -1
- package/dist/tests/filters/special-cases.test.js +0 -114
- package/dist/tests/filters/special-cases.test.js.map +0 -1
- package/dist/tests/filters/wildcard.test.d.ts +0 -2
- package/dist/tests/filters/wildcard.test.d.ts.map +0 -1
- package/dist/tests/filters/wildcard.test.js +0 -67
- package/dist/tests/filters/wildcard.test.js.map +0 -1
- package/dist/tests/jest.global.setup.d.ts +0 -3
- package/dist/tests/jest.global.setup.d.ts.map +0 -1
- package/dist/tests/jest.global.setup.js +0 -7
- package/dist/tests/jest.global.setup.js.map +0 -1
- package/dist/tests/jest.setup.d.ts +0 -2
- package/dist/tests/jest.setup.d.ts.map +0 -1
- package/dist/tests/jest.setup.js +0 -5
- package/dist/tests/jest.setup.js.map +0 -1
- package/dist/tests/utils/index.d.ts +0 -57
- package/dist/tests/utils/index.d.ts.map +0 -1
- package/dist/tests/utils/index.js +0 -206
- package/dist/tests/utils/index.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/src/QueryableModel.ts +0 -134
- package/src/SQL.ts +0 -128
- package/src/SQLDelete.ts +0 -73
- package/src/SQLExpression.ts +0 -34
- package/src/SQLExpressions.ts +0 -587
- package/src/SQLInsert.ts +0 -113
- package/src/SQLJsonExpressions.ts +0 -283
- package/src/SQLLogger.ts +0 -82
- package/src/SQLOrderBy.ts +0 -69
- package/src/SQLSelect.ts +0 -520
- package/src/SQLTranslatedStringHelper.ts +0 -40
- package/src/SQLWhere.ts +0 -744
- package/src/filters/SQLSorter.ts +0 -60
- package/src/filters/compilers/contains.ts +0 -43
- package/src/filters/compilers/equals.ts +0 -72
- package/src/filters/compilers/greater.ts +0 -20
- package/src/filters/compilers/less.ts +0 -19
- /package/dist/{src/SQLExpression.d.ts → SQLExpression.d.ts} +0 -0
- /package/dist/{src/SQLLogger.d.ts → SQLLogger.d.ts} +0 -0
- /package/dist/{src/filters → filters}/compilers/index.d.ts +0 -0
|
@@ -1,283 +0,0 @@
|
|
|
1
|
-
import { SQLExpression, SQLExpressionOptions, SQLQuery, joinSQLQuery } from './SQLExpression.js';
|
|
2
|
-
import { SQLJSONFalse, SQLJSONNull, SQLJSONTrue, SQLSafeValue, SQLScalar, SQLScalarValue } from './SQLExpressions.js';
|
|
3
|
-
import { SQLWhere } from './SQLWhere.js';
|
|
4
|
-
|
|
5
|
-
export function scalarToSQLJSONExpression(s: SQLScalarValue | null): SQLExpression {
|
|
6
|
-
if (s === null) {
|
|
7
|
-
return SQLJSONNull;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
if (s === true) {
|
|
11
|
-
return SQLJSONTrue;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
if (s === false) {
|
|
15
|
-
return SQLJSONFalse;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return new SQLScalar(s);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export class SQLJsonUnquote implements SQLExpression {
|
|
22
|
-
target: SQLExpression;
|
|
23
|
-
|
|
24
|
-
constructor(target: SQLExpression) {
|
|
25
|
-
this.target = target;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
getSQL(options?: SQLExpressionOptions): SQLQuery {
|
|
29
|
-
return joinSQLQuery([
|
|
30
|
-
'JSON_UNQUOTE(',
|
|
31
|
-
this.target.getSQL(options),
|
|
32
|
-
')',
|
|
33
|
-
]);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export type SQLJsonValueType = 'FLOAT' | 'DOUBLE' | 'DECIMAL' | 'SIGNED' | 'UNSIGNED' | 'DATE' | 'TIME' | 'DATETIME' | 'YEAR' | 'CHAR' | 'JSON';
|
|
38
|
-
|
|
39
|
-
export class SQLJsonValue implements SQLExpression {
|
|
40
|
-
target: SQLExpression;
|
|
41
|
-
path: SQLExpression;
|
|
42
|
-
type?: SQLJsonValueType;
|
|
43
|
-
|
|
44
|
-
constructor(target: SQLExpression, type?: SQLJsonValueType, path?: SQLExpression) {
|
|
45
|
-
this.target = target;
|
|
46
|
-
this.type = type;
|
|
47
|
-
|
|
48
|
-
if (!path && target instanceof SQLJsonExtract) {
|
|
49
|
-
// If the target is a SQLJsonExtract, we can use its path directly
|
|
50
|
-
this.target = target.target;
|
|
51
|
-
this.path = target.path;
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
this.path = path ?? new SQLSafeValue('$');
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
getSQL(options?: SQLExpressionOptions): SQLQuery {
|
|
59
|
-
return joinSQLQuery([
|
|
60
|
-
'JSON_VALUE(',
|
|
61
|
-
this.target.getSQL(options),
|
|
62
|
-
',',
|
|
63
|
-
this.path.getSQL(options),
|
|
64
|
-
(this.type ? ' RETURNING ' + this.type + (this.type === 'CHAR' ? ' CHARACTER SET utf8mb4' : '') : ''),
|
|
65
|
-
' ERROR ON ERROR)',
|
|
66
|
-
]);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Same as target->path, JSON_EXTRACT(target, path)
|
|
72
|
-
*/
|
|
73
|
-
export class SQLJsonExtract implements SQLExpression {
|
|
74
|
-
target: SQLExpression;
|
|
75
|
-
path: SQLExpression;
|
|
76
|
-
|
|
77
|
-
constructor(target: SQLExpression, path: SQLExpression) {
|
|
78
|
-
this.target = target;
|
|
79
|
-
this.path = path;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
getSQL(options?: SQLExpressionOptions): SQLQuery {
|
|
83
|
-
return joinSQLQuery([
|
|
84
|
-
'JSON_EXTRACT(',
|
|
85
|
-
this.target.getSQL(options),
|
|
86
|
-
',',
|
|
87
|
-
this.path.getSQL(options),
|
|
88
|
-
')',
|
|
89
|
-
]);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* NOTE: this has to be combined with asScalar = true! Never let user input in a query without passing it as a parameter.
|
|
94
|
-
*
|
|
95
|
-
* E.g. SQL.jsonValue(SQL.column('settings'), `$.value.prices[*].bundleDiscounts.${SQLJsonExtract.escapePathComponent(key)}`, true) (note that last true!)
|
|
96
|
-
*/
|
|
97
|
-
static escapePathComponent(str: string) {
|
|
98
|
-
return '"' + str.replace(/(["\\])/g, '\\$1') + '"';
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export class SQLJsonType implements SQLExpression {
|
|
103
|
-
target: SQLExpression;
|
|
104
|
-
|
|
105
|
-
constructor(target: SQLExpression) {
|
|
106
|
-
this.target = target;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
getSQL(options?: SQLExpressionOptions): SQLQuery {
|
|
110
|
-
return joinSQLQuery([
|
|
111
|
-
'JSON_TYPE(',
|
|
112
|
-
this.target.getSQL(options),
|
|
113
|
-
')',
|
|
114
|
-
]);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Same as target->path, JSON_EXTRACT(target, path)
|
|
120
|
-
*/
|
|
121
|
-
export class SQLJsonKeys implements SQLExpression {
|
|
122
|
-
target: SQLExpression;
|
|
123
|
-
|
|
124
|
-
constructor(target: SQLExpression) {
|
|
125
|
-
this.target = target;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
getSQL(options?: SQLExpressionOptions): SQLQuery {
|
|
129
|
-
return joinSQLQuery([
|
|
130
|
-
'JSON_KEYS(',
|
|
131
|
-
this.target.getSQL(options),
|
|
132
|
-
')',
|
|
133
|
-
]);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export class SQLJsonLength implements SQLExpression {
|
|
138
|
-
target: SQLExpression;
|
|
139
|
-
path?: SQLExpression;
|
|
140
|
-
|
|
141
|
-
constructor(target: SQLExpression, path?: SQLExpression) {
|
|
142
|
-
this.target = target;
|
|
143
|
-
this.path = path;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
getSQL(options?: SQLExpressionOptions): SQLQuery {
|
|
147
|
-
return joinSQLQuery([
|
|
148
|
-
'JSON_LENGTH(',
|
|
149
|
-
this.target.getSQL(options),
|
|
150
|
-
...(this.path
|
|
151
|
-
? [
|
|
152
|
-
',',
|
|
153
|
-
this.path.getSQL(options),
|
|
154
|
-
]
|
|
155
|
-
: []),
|
|
156
|
-
')',
|
|
157
|
-
]);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* JSON_SEARCH(json_doc, one_or_all, search_str[, escape_char[, path] ...])
|
|
162
|
-
*/
|
|
163
|
-
export class SQLJsonSearch implements SQLExpression {
|
|
164
|
-
target: SQLExpression;
|
|
165
|
-
oneOrAll: 'one' | 'all';
|
|
166
|
-
searchStr: SQLExpression;
|
|
167
|
-
path: SQLExpression | null;
|
|
168
|
-
|
|
169
|
-
constructor(target: SQLExpression, oneOrAll: 'one' | 'all', searchStr: SQLExpression, path: SQLExpression | null = null) {
|
|
170
|
-
this.target = target;
|
|
171
|
-
this.oneOrAll = oneOrAll;
|
|
172
|
-
this.searchStr = searchStr;
|
|
173
|
-
this.path = path;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
getSQL(options?: SQLExpressionOptions): SQLQuery {
|
|
177
|
-
return joinSQLQuery([
|
|
178
|
-
'JSON_SEARCH(',
|
|
179
|
-
this.target.getSQL(options),
|
|
180
|
-
',',
|
|
181
|
-
new SQLSafeValue(this.oneOrAll).getSQL(options),
|
|
182
|
-
',',
|
|
183
|
-
this.searchStr.getSQL(options),
|
|
184
|
-
...(this.path
|
|
185
|
-
? [
|
|
186
|
-
',',
|
|
187
|
-
this.path.getSQL(options),
|
|
188
|
-
]
|
|
189
|
-
: []),
|
|
190
|
-
')',
|
|
191
|
-
]);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* JSON_CONTAINS(target, candidate[, path])
|
|
197
|
-
*/
|
|
198
|
-
export class SQLJsonContains extends SQLWhere {
|
|
199
|
-
target: SQLExpression;
|
|
200
|
-
candidate: SQLExpression;
|
|
201
|
-
path: SQLExpression | null;
|
|
202
|
-
|
|
203
|
-
constructor(target: SQLExpression, candidate: SQLExpression, path: SQLExpression | null = null) {
|
|
204
|
-
super();
|
|
205
|
-
this.target = target;
|
|
206
|
-
this.candidate = candidate;
|
|
207
|
-
this.path = path;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
getSQL(options?: SQLExpressionOptions): SQLQuery {
|
|
211
|
-
return joinSQLQuery([
|
|
212
|
-
'JSON_CONTAINS(',
|
|
213
|
-
this.target.getSQL(options),
|
|
214
|
-
',',
|
|
215
|
-
this.candidate.getSQL(options),
|
|
216
|
-
...(this.path
|
|
217
|
-
? [
|
|
218
|
-
',',
|
|
219
|
-
this.path.getSQL(options),
|
|
220
|
-
]
|
|
221
|
-
: []),
|
|
222
|
-
')',
|
|
223
|
-
]);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
clone(): this {
|
|
227
|
-
const c = (new (this.constructor as any)(this.target, this.candidate, this.path)) as this;
|
|
228
|
-
return c;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* JSON_CONTAINS(json_doc1, json_doc2)
|
|
234
|
-
*/
|
|
235
|
-
export class SQLJsonOverlaps extends SQLWhere {
|
|
236
|
-
jsonDoc1: SQLExpression;
|
|
237
|
-
jsonDoc2: SQLExpression;
|
|
238
|
-
|
|
239
|
-
constructor(jsonDoc1: SQLExpression, jsonDoc2: SQLExpression) {
|
|
240
|
-
super();
|
|
241
|
-
this.jsonDoc1 = jsonDoc1;
|
|
242
|
-
this.jsonDoc2 = jsonDoc2;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
getSQL(options?: SQLExpressionOptions): SQLQuery {
|
|
246
|
-
return joinSQLQuery([
|
|
247
|
-
'JSON_OVERLAPS(',
|
|
248
|
-
this.jsonDoc1.getSQL(options),
|
|
249
|
-
',',
|
|
250
|
-
this.jsonDoc2.getSQL(options),
|
|
251
|
-
')',
|
|
252
|
-
]);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
clone(): this {
|
|
256
|
-
const c = (new (this.constructor as any)(this.jsonDoc1, this.jsonDoc2)) as this;
|
|
257
|
-
return c;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
export class SQLLpad implements SQLExpression {
|
|
262
|
-
target: SQLExpression;
|
|
263
|
-
length: SQLExpression;
|
|
264
|
-
value: SQLExpression;
|
|
265
|
-
|
|
266
|
-
constructor(target: SQLExpression, length: SQLExpression, value: SQLExpression) {
|
|
267
|
-
this.target = target;
|
|
268
|
-
this.length = length;
|
|
269
|
-
this.value = value;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
getSQL(options?: SQLExpressionOptions): SQLQuery {
|
|
273
|
-
return joinSQLQuery([
|
|
274
|
-
'LPAD(',
|
|
275
|
-
this.target.getSQL(options),
|
|
276
|
-
',',
|
|
277
|
-
this.length.getSQL(options),
|
|
278
|
-
',',
|
|
279
|
-
this.value.getSQL(options),
|
|
280
|
-
')',
|
|
281
|
-
]);
|
|
282
|
-
}
|
|
283
|
-
}
|
package/src/SQLLogger.ts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { Database } from '@simonbackx/simple-database';
|
|
2
|
-
import { StyledText } from '@simonbackx/simple-logging';
|
|
3
|
-
|
|
4
|
-
class StaticSQLLogger {
|
|
5
|
-
slowQueryThresholdMs: number | null = null;
|
|
6
|
-
explainAllAndLogInefficient = false;
|
|
7
|
-
customLoggers: ((query: string, params: any[], elapsedTimeMs: number) => void)[] = [];
|
|
8
|
-
|
|
9
|
-
async explain(query: string, params: any[]) {
|
|
10
|
-
// Run an EXPLAIN on the query to see what is going on
|
|
11
|
-
try {
|
|
12
|
-
const explainQuery = 'EXPLAIN ' + query;
|
|
13
|
-
const [explainRows] = await Database.select(explainQuery, params, { nestTables: true });
|
|
14
|
-
return explainRows[0]?.[''] ?? null;
|
|
15
|
-
}
|
|
16
|
-
catch (e) {
|
|
17
|
-
console.error('Error running EXPLAIN on slow query', e);
|
|
18
|
-
}
|
|
19
|
-
return null;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async logInefficientExplainResult(query: string, params: any[], explainResult: any, elapsedTimeMs?: number) {
|
|
23
|
-
if (!explainResult) {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
if (elapsedTimeMs !== undefined && elapsedTimeMs < 20) {
|
|
27
|
-
// Probably fine
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (explainResult['type'] === 'ALL') {
|
|
32
|
-
console.warn(
|
|
33
|
-
new StyledText('[FULL TABLE SCAN] ').addClass('error').addTag('query'),
|
|
34
|
-
`Inefficient query detected${elapsedTimeMs !== undefined ? ' (' + elapsedTimeMs.toFixed(0) + 'ms)' : ''}:`,
|
|
35
|
-
query,
|
|
36
|
-
params,
|
|
37
|
-
'EXPLAIN result:',
|
|
38
|
-
explainResult,
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
async log<T>(queryPromise: Promise<T>, query: string, params: any[]): Promise<T> {
|
|
44
|
-
if (this.slowQueryThresholdMs === null) {
|
|
45
|
-
if (this.explainAllAndLogInefficient) {
|
|
46
|
-
const result = await queryPromise;
|
|
47
|
-
await this.logInefficientExplainResult(query, params, await this.explain(query, params));
|
|
48
|
-
return result;
|
|
49
|
-
}
|
|
50
|
-
return queryPromise;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const startTime = process.hrtime.bigint();
|
|
54
|
-
const result = await queryPromise;
|
|
55
|
-
const elapsedTime = process.hrtime.bigint() - startTime;
|
|
56
|
-
|
|
57
|
-
// Convert to ms
|
|
58
|
-
const elapsedTimeMs = Number(elapsedTime) / 1000 / 1000;
|
|
59
|
-
|
|
60
|
-
if (elapsedTimeMs > this.slowQueryThresholdMs) {
|
|
61
|
-
console.trace('Slow SQL query (' + elapsedTimeMs.toFixed(2) + 'ms)\nQuery: ' + query, params);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
for (const logger of this.customLoggers) {
|
|
65
|
-
try {
|
|
66
|
-
logger(query, params, elapsedTimeMs);
|
|
67
|
-
}
|
|
68
|
-
catch (e) {
|
|
69
|
-
console.error('Error in custom SQL logger', e);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (this.explainAllAndLogInefficient) {
|
|
74
|
-
await this.logInefficientExplainResult(query, params, await this.explain(query, params), elapsedTimeMs);
|
|
75
|
-
return result;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return result;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export const SQLLogger = new StaticSQLLogger();
|
package/src/SQLOrderBy.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { SQLExpression, SQLExpressionOptions, SQLQuery, joinSQLQuery } from './SQLExpression.js';
|
|
2
|
-
import { SQLColumnExpression } from './SQLExpressions.js';
|
|
3
|
-
|
|
4
|
-
type Constructor<T = object> = new (...args: any[]) => T;
|
|
5
|
-
|
|
6
|
-
export function Orderable<Sup extends Constructor<object>>(Base: Sup) {
|
|
7
|
-
return class extends Base {
|
|
8
|
-
_orderBy: SQLOrderBy | null = null;
|
|
9
|
-
|
|
10
|
-
orderBy<T>(this: T, orderBy: SQLOrderBy): T;
|
|
11
|
-
orderBy<T>(this: T, column: SQLExpression | string, direction?: SQLOrderByDirection): T;
|
|
12
|
-
orderBy<T>(this: T, columnOrOrderBy: SQLExpression | string, direction?: SQLOrderByDirection): T {
|
|
13
|
-
let o = columnOrOrderBy as SQLOrderBy;
|
|
14
|
-
if (!(columnOrOrderBy instanceof SQLOrderBy)) {
|
|
15
|
-
o = new SQLOrderBy({
|
|
16
|
-
column: typeof columnOrOrderBy === 'string' ? new SQLColumnExpression(columnOrOrderBy) : columnOrOrderBy,
|
|
17
|
-
direction: direction ?? 'ASC',
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const me = this as any; // stupid typescript looses type information if we don't do the this: T dance
|
|
22
|
-
|
|
23
|
-
if (me._orderBy) {
|
|
24
|
-
me._orderBy.add(o);
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
me._orderBy = o;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return me;
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export type SQLOrderByDirection = 'ASC' | 'DESC';
|
|
36
|
-
export class SQLOrderBy implements SQLExpression {
|
|
37
|
-
orderBy: { column: SQLExpression; direction: SQLOrderByDirection }[] = [];
|
|
38
|
-
|
|
39
|
-
constructor(...orderBy: { column: SQLExpression; direction: SQLOrderByDirection }[]) {
|
|
40
|
-
this.orderBy = orderBy;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
static combine(orderBy: SQLOrderBy[]) {
|
|
44
|
-
return new SQLOrderBy(...orderBy.flatMap(o => o.orderBy));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
add(orderBy: SQLOrderBy) {
|
|
48
|
-
this.orderBy.push(...orderBy.orderBy);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
getSQL(options?: SQLExpressionOptions): SQLQuery {
|
|
52
|
-
if (this.orderBy.length === 0) {
|
|
53
|
-
return '';
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return joinSQLQuery([
|
|
57
|
-
'ORDER BY ',
|
|
58
|
-
joinSQLQuery(
|
|
59
|
-
this.orderBy.map((o) => {
|
|
60
|
-
return joinSQLQuery([
|
|
61
|
-
o.column.getSQL(options),
|
|
62
|
-
o.direction,
|
|
63
|
-
], ' ');
|
|
64
|
-
}),
|
|
65
|
-
', ',
|
|
66
|
-
),
|
|
67
|
-
]);
|
|
68
|
-
}
|
|
69
|
-
}
|