taon-type-sql 21.0.21 → 21.0.23
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/browser/package.json +1 -1
- package/browser-prod/package.json +1 -1
- package/lib/build-info._auto-generated_.d.ts +1 -1
- package/lib/build-info._auto-generated_.js +1 -1
- package/lib/package.json +1 -1
- package/lib-prod/build-info._auto-generated_.js +14 -30
- package/lib-prod/builder/column/basic-column.js +12 -14
- package/lib-prod/builder/column/boolean-column.js +13 -14
- package/lib-prod/builder/column/comparable-column.js +41 -41
- package/lib-prod/builder/column/date-column.js +13 -14
- package/lib-prod/builder/column/number-column.js +18 -19
- package/lib-prod/builder/column/query-column.js +22 -24
- package/lib-prod/builder/column/string-column.js +35 -36
- package/lib-prod/builder/column/value-column.js +27 -28
- package/lib-prod/builder/condition/query-column-condition.js +21 -22
- package/lib-prod/builder/condition/query-condition-chain.js +32 -33
- package/lib-prod/builder/condition/query-condition.js +4 -5
- package/lib-prod/builder/condition/query-join-condition.js +21 -22
- package/lib-prod/builder/helpers/generics-helper.js +3 -5
- package/lib-prod/builder/helpers/internal-types.js +0 -4
- package/lib-prod/builder/join/joined-tables-chain.js +12 -16
- package/lib-prod/builder/join/joined-tables.js +20 -23
- package/lib-prod/builder/other/query-ordering.js +18 -19
- package/lib-prod/builder/query/select-query.js +49 -52
- package/lib-prod/builder/query/table-condition-query.js +25 -29
- package/lib-prod/builder/query/table-query.js +52 -58
- package/lib-prod/builder/query-source.js +16 -20
- package/lib-prod/builder/query-table.js +25 -26
- package/lib-prod/client/mysql.js +9 -10
- package/lib-prod/client/pg.js +9 -10
- package/lib-prod/client/query-processor.js +39 -68
- package/lib-prod/converter/param-converter.js +19 -27
- package/lib-prod/converter/parameterized-converter.js +13 -14
- package/lib-prod/converter/query-converter.js +243 -259
- package/lib-prod/converter/result-converter.js +61 -73
- package/lib-prod/converter/sql-converter.js +6 -7
- package/lib-prod/converter/type-converter.js +24 -35
- package/lib-prod/converter/types.js +0 -3
- package/lib-prod/env/env.angular-node-app.js +130 -71
- package/lib-prod/env/env.docs-webapp.js +130 -71
- package/lib-prod/env/env.electron-app.js +130 -71
- package/lib-prod/env/env.mobile-app.js +130 -71
- package/lib-prod/env/env.npm-lib-and-cli-tool.js +130 -71
- package/lib-prod/env/env.vscode-plugin.js +130 -71
- package/lib-prod/env/index.js +6 -23
- package/lib-prod/index._auto-generated_.js +0 -6
- package/lib-prod/index.js +46 -48
- package/lib-prod/migrations/index.js +1 -19
- package/lib-prod/migrations/migrations_index._auto-generated_.js +0 -4
- package/lib-prod/package.json +1 -1
- package/package.json +1 -1
- package/websql/package.json +1 -1
- package/websql-prod/package.json +1 -1
- package/lib-prod/build-info._auto-generated_.d.ts +0 -24
- package/lib-prod/build-info._auto-generated_.js.map +0 -1
- package/lib-prod/builder/column/basic-column.d.ts +0 -8
- package/lib-prod/builder/column/basic-column.js.map +0 -1
- package/lib-prod/builder/column/boolean-column.d.ts +0 -9
- package/lib-prod/builder/column/boolean-column.js.map +0 -1
- package/lib-prod/builder/column/comparable-column.d.ts +0 -18
- package/lib-prod/builder/column/comparable-column.js.map +0 -1
- package/lib-prod/builder/column/date-column.d.ts +0 -9
- package/lib-prod/builder/column/date-column.js.map +0 -1
- package/lib-prod/builder/column/number-column.d.ts +0 -10
- package/lib-prod/builder/column/number-column.js.map +0 -1
- package/lib-prod/builder/column/query-column.d.ts +0 -18
- package/lib-prod/builder/column/query-column.js.map +0 -1
- package/lib-prod/builder/column/string-column.d.ts +0 -17
- package/lib-prod/builder/column/string-column.js.map +0 -1
- package/lib-prod/builder/column/value-column.d.ts +0 -15
- package/lib-prod/builder/column/value-column.js.map +0 -1
- package/lib-prod/builder/condition/query-column-condition.d.ts +0 -13
- package/lib-prod/builder/condition/query-column-condition.js.map +0 -1
- package/lib-prod/builder/condition/query-condition-chain.d.ts +0 -17
- package/lib-prod/builder/condition/query-condition-chain.js.map +0 -1
- package/lib-prod/builder/condition/query-condition.d.ts +0 -7
- package/lib-prod/builder/condition/query-condition.js.map +0 -1
- package/lib-prod/builder/condition/query-join-condition.d.ts +0 -12
- package/lib-prod/builder/condition/query-join-condition.js.map +0 -1
- package/lib-prod/builder/helpers/generics-helper.d.ts +0 -2
- package/lib-prod/builder/helpers/generics-helper.js.map +0 -1
- package/lib-prod/builder/helpers/internal-types.d.ts +0 -15
- package/lib-prod/builder/helpers/internal-types.js.map +0 -1
- package/lib-prod/builder/join/joined-tables-chain.d.ts +0 -11
- package/lib-prod/builder/join/joined-tables-chain.js.map +0 -1
- package/lib-prod/builder/join/joined-tables.d.ts +0 -12
- package/lib-prod/builder/join/joined-tables.js.map +0 -1
- package/lib-prod/builder/other/query-ordering.d.ts +0 -10
- package/lib-prod/builder/other/query-ordering.js.map +0 -1
- package/lib-prod/builder/query/select-query.d.ts +0 -29
- package/lib-prod/builder/query/select-query.js.map +0 -1
- package/lib-prod/builder/query/table-condition-query.d.ts +0 -16
- package/lib-prod/builder/query/table-condition-query.js.map +0 -1
- package/lib-prod/builder/query/table-query.d.ts +0 -23
- package/lib-prod/builder/query/table-query.js.map +0 -1
- package/lib-prod/builder/query-source.d.ts +0 -14
- package/lib-prod/builder/query-source.js.map +0 -1
- package/lib-prod/builder/query-table.d.ts +0 -15
- package/lib-prod/builder/query-table.js.map +0 -1
- package/lib-prod/client/mysql.d.ts +0 -5
- package/lib-prod/client/mysql.js.map +0 -1
- package/lib-prod/client/pg.d.ts +0 -5
- package/lib-prod/client/pg.js.map +0 -1
- package/lib-prod/client/query-processor.d.ts +0 -10
- package/lib-prod/client/query-processor.js.map +0 -1
- package/lib-prod/converter/param-converter.d.ts +0 -2
- package/lib-prod/converter/param-converter.js.map +0 -1
- package/lib-prod/converter/parameterized-converter.d.ts +0 -5
- package/lib-prod/converter/parameterized-converter.js.map +0 -1
- package/lib-prod/converter/query-converter.d.ts +0 -2
- package/lib-prod/converter/query-converter.js.map +0 -1
- package/lib-prod/converter/result-converter.d.ts +0 -2
- package/lib-prod/converter/result-converter.js.map +0 -1
- package/lib-prod/converter/sql-converter.d.ts +0 -2
- package/lib-prod/converter/sql-converter.js.map +0 -1
- package/lib-prod/converter/type-converter.d.ts +0 -4
- package/lib-prod/converter/type-converter.js.map +0 -1
- package/lib-prod/converter/types.d.ts +0 -5
- package/lib-prod/converter/types.js.map +0 -1
- package/lib-prod/env/env.angular-node-app.d.ts +0 -64
- package/lib-prod/env/env.angular-node-app.js.map +0 -1
- package/lib-prod/env/env.docs-webapp.d.ts +0 -64
- package/lib-prod/env/env.docs-webapp.js.map +0 -1
- package/lib-prod/env/env.electron-app.d.ts +0 -64
- package/lib-prod/env/env.electron-app.js.map +0 -1
- package/lib-prod/env/env.mobile-app.d.ts +0 -64
- package/lib-prod/env/env.mobile-app.js.map +0 -1
- package/lib-prod/env/env.npm-lib-and-cli-tool.d.ts +0 -64
- package/lib-prod/env/env.npm-lib-and-cli-tool.js.map +0 -1
- package/lib-prod/env/env.vscode-plugin.d.ts +0 -64
- package/lib-prod/env/env.vscode-plugin.js.map +0 -1
- package/lib-prod/env/index.d.ts +0 -6
- package/lib-prod/env/index.js.map +0 -1
- package/lib-prod/index._auto-generated_.d.ts +0 -0
- package/lib-prod/index._auto-generated_.js.map +0 -1
- package/lib-prod/index.d.ts +0 -25
- package/lib-prod/index.js.map +0 -1
- package/lib-prod/migrations/index.d.ts +0 -1
- package/lib-prod/migrations/index.js.map +0 -1
- package/lib-prod/migrations/migrations_index._auto-generated_.d.ts +0 -0
- package/lib-prod/migrations/migrations_index._auto-generated_.js.map +0 -1
|
@@ -1,284 +1,268 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createQueryConverter = createQueryConverter;
|
|
4
|
-
const type_converter_1 = require("./type-converter");
|
|
1
|
+
import { string, number, date, boolean } from "./type-converter";
|
|
5
2
|
function createQueryConverter(paramConverter, options, engine) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
3
|
+
return convertQuery;
|
|
4
|
+
function convertQuery(query) {
|
|
5
|
+
if (query._action === "select") return convertSelectQuery(query);
|
|
6
|
+
if (query._action === "delete") return convertDeleteQuery(query);
|
|
7
|
+
if (query._action === "update") return convertUpdateQuery(query);
|
|
8
|
+
if (query._action === "insert") return convertInsertQuery(query);
|
|
9
|
+
throw new Error("Unknown query type:" + query._action);
|
|
10
|
+
}
|
|
11
|
+
function convertDeleteQuery(query) {
|
|
12
|
+
let s = "DELETE FROM " + convertTable(query._table);
|
|
13
|
+
s += convertConditions(query._conditions);
|
|
14
|
+
return s;
|
|
15
|
+
}
|
|
16
|
+
function convertUpdateQuery(query) {
|
|
17
|
+
let s = "UPDATE " + convertTable(query._table) + " SET ";
|
|
18
|
+
s += convertUpdateSetters(query._table, query._entity);
|
|
19
|
+
s += convertConditions(query._conditions);
|
|
20
|
+
return s;
|
|
21
|
+
}
|
|
22
|
+
function convertUpdateSetters(table, entity) {
|
|
23
|
+
return Object.keys(entity).sort().map((key) => {
|
|
24
|
+
let value = entity[key];
|
|
25
|
+
let column = table[key];
|
|
26
|
+
return convertColumnName(column) + " = " + convertParam(column, value);
|
|
27
|
+
}).join(", ");
|
|
28
|
+
}
|
|
29
|
+
function convertInsertQuery(query) {
|
|
30
|
+
let items = Array.isArray(query._entity) ? query._entity : [query._entity];
|
|
31
|
+
let keySet = items.reduce((set, item) => {
|
|
32
|
+
Object.keys(item).forEach((key) => set.add(key));
|
|
33
|
+
return set;
|
|
34
|
+
}, /* @__PURE__ */ new Set());
|
|
35
|
+
let keys = Array.from(keySet).sort();
|
|
36
|
+
let s = "INSERT INTO " + convertTable(query._table) + " ";
|
|
37
|
+
s += "(" + keys.map((key) => convertColumnName(query._table[key])).join(", ") + ")";
|
|
38
|
+
s += options.lineBreak + "VALUES ";
|
|
39
|
+
s += items.map((item) => convertInsertItem(query._table, item, keys)).map((row) => "(" + row + ")").join(", ");
|
|
40
|
+
s += getPgInsertReturningIfNeeded(query);
|
|
41
|
+
return s;
|
|
42
|
+
}
|
|
43
|
+
function getPgInsertReturningIfNeeded(query) {
|
|
44
|
+
if (engine === "pg" && query._action === "insert" && !Array.isArray(query._entity) && query._table.$id && query._table.$id._table && query._table.$id._name) {
|
|
45
|
+
return " RETURNING " + convertColumnName(query._table.$id);
|
|
17
46
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
47
|
+
return "";
|
|
48
|
+
}
|
|
49
|
+
function convertInsertItem(table, entity, keys) {
|
|
50
|
+
return keys.map((key) => {
|
|
51
|
+
let value = entity[key];
|
|
52
|
+
let column = table[key];
|
|
53
|
+
return convertParam(column, value);
|
|
54
|
+
}).join(", ");
|
|
55
|
+
}
|
|
56
|
+
function convertSelectQuery(query) {
|
|
57
|
+
let s = "SELECT ";
|
|
58
|
+
if (query._distinct) {
|
|
59
|
+
s += "DISTINCT ";
|
|
22
60
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return s;
|
|
61
|
+
if (query._columns == null || query._columns.length === 0) {
|
|
62
|
+
s += "*";
|
|
63
|
+
} else {
|
|
64
|
+
s += query._columns.map((column) => convertColumn(column)).join(", ");
|
|
28
65
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}).join(', ');
|
|
66
|
+
s += options.lineBreak + "FROM ";
|
|
67
|
+
if (query._tables) {
|
|
68
|
+
s += query._tables.map((table) => table._parent ? convertJoin(table) : convertTable(table)).join(", ");
|
|
69
|
+
} else {
|
|
70
|
+
s += convertTable(query._table);
|
|
35
71
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return set;
|
|
41
|
-
}, new Set());
|
|
42
|
-
let keys = Array.from(keySet).sort();
|
|
43
|
-
let s = 'INSERT INTO ' + convertTable(query._table) + ' ';
|
|
44
|
-
s += '(' + keys.map(key => convertColumnName(query._table[key])).join(', ') + ')';
|
|
45
|
-
s += options.lineBreak + 'VALUES ';
|
|
46
|
-
s += items.map(item => convertInsertItem(query._table, item, keys))
|
|
47
|
-
.map((row) => '(' + row + ')').join(', ');
|
|
48
|
-
s += getPgInsertReturningIfNeeded(query);
|
|
49
|
-
return s;
|
|
72
|
+
s += convertConditions(query._conditions);
|
|
73
|
+
if (query._groupBy && query._groupBy.length > 0) {
|
|
74
|
+
s += options.lineBreak + "GROUP BY ";
|
|
75
|
+
s += query._groupBy.map((column) => convertColumn(column)).join(", ");
|
|
50
76
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
return '';
|
|
77
|
+
s += convertConditions(query._having, "HAVING");
|
|
78
|
+
if (query._orderings && query._orderings.length > 0) {
|
|
79
|
+
s += options.lineBreak + "ORDER BY ";
|
|
80
|
+
s += query._orderings.map((ordering) => convertOrdering(ordering)).join(", ");
|
|
57
81
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
let value = entity[key];
|
|
61
|
-
let column = table[key];
|
|
62
|
-
return convertParam(column, value);
|
|
63
|
-
}).join(', ');
|
|
82
|
+
if (query._limit != null) {
|
|
83
|
+
s += options.lineBreak + "LIMIT " + number(query._limit);
|
|
64
84
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (query._distinct) {
|
|
68
|
-
s += 'DISTINCT ';
|
|
69
|
-
}
|
|
70
|
-
if (query._columns == null || query._columns.length === 0) {
|
|
71
|
-
s += '*';
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
s += query._columns.map((column) => convertColumn(column)).join(', ');
|
|
75
|
-
}
|
|
76
|
-
s += options.lineBreak + 'FROM ';
|
|
77
|
-
if (query._tables) {
|
|
78
|
-
s += query._tables.map((table) => table._parent ? convertJoin(table) : convertTable(table)).join(', ');
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
s += convertTable(query._table);
|
|
82
|
-
}
|
|
83
|
-
s += convertConditions(query._conditions);
|
|
84
|
-
if (query._groupBy && query._groupBy.length > 0) {
|
|
85
|
-
s += options.lineBreak + 'GROUP BY ';
|
|
86
|
-
s += query._groupBy.map((column) => convertColumn(column)).join(', ');
|
|
87
|
-
}
|
|
88
|
-
s += convertConditions(query._having, 'HAVING');
|
|
89
|
-
if (query._orderings && query._orderings.length > 0) {
|
|
90
|
-
s += options.lineBreak + 'ORDER BY ';
|
|
91
|
-
s += query._orderings.map((ordering) => convertOrdering(ordering)).join(', ');
|
|
92
|
-
}
|
|
93
|
-
if (query._limit != null) {
|
|
94
|
-
s += options.lineBreak + 'LIMIT ' + (0, type_converter_1.number)(query._limit);
|
|
95
|
-
}
|
|
96
|
-
if (query._offset != null) {
|
|
97
|
-
s += options.lineBreak + 'OFFSET ' + (0, type_converter_1.number)(query._offset);
|
|
98
|
-
}
|
|
99
|
-
return s;
|
|
85
|
+
if (query._offset != null) {
|
|
86
|
+
s += options.lineBreak + "OFFSET " + number(query._offset);
|
|
100
87
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
88
|
+
return s;
|
|
89
|
+
}
|
|
90
|
+
function convertConditions(conditions, keyword = "WHERE") {
|
|
91
|
+
let s = "";
|
|
92
|
+
if (conditions && conditions.length > 0) {
|
|
93
|
+
s += options.lineBreak + keyword + " ";
|
|
94
|
+
preprocessConditions(conditions);
|
|
95
|
+
s += conditions.map((condition) => convertCondition(condition, true)).join(" AND ");
|
|
109
96
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
let s = convertTable(root);
|
|
118
|
-
for (let i = items.length - 2; i >= 0; i -= 2) {
|
|
119
|
-
let table = items[i]._table;
|
|
120
|
-
let modifier = items[i]._modifier;
|
|
121
|
-
let condition = items[i - 1]._condition;
|
|
122
|
-
let param = convertColumn(condition._otherColumn);
|
|
123
|
-
s += ' ' + modifier.toUpperCase() + ' JOIN ' + convertTable(table) + ' ON ' +
|
|
124
|
-
convertColumnCondition(condition, param);
|
|
125
|
-
}
|
|
126
|
-
return s;
|
|
97
|
+
return s;
|
|
98
|
+
}
|
|
99
|
+
function convertJoin(joinChain) {
|
|
100
|
+
let items = [];
|
|
101
|
+
while (joinChain) {
|
|
102
|
+
items.push(joinChain);
|
|
103
|
+
joinChain = joinChain._parent;
|
|
127
104
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (ordering._direction === 'DESC')
|
|
137
|
-
s += ' DESC';
|
|
138
|
-
return s;
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
return convertColumn(ordering);
|
|
142
|
-
}
|
|
105
|
+
let root = items[items.length - 1];
|
|
106
|
+
let s = convertTable(root);
|
|
107
|
+
for (let i = items.length - 2; i >= 0; i -= 2) {
|
|
108
|
+
let table = items[i]._table;
|
|
109
|
+
let modifier = items[i]._modifier;
|
|
110
|
+
let condition = items[i - 1]._condition;
|
|
111
|
+
let param = convertColumn(condition._otherColumn);
|
|
112
|
+
s += " " + modifier.toUpperCase() + " JOIN " + convertTable(table) + " ON " + convertColumnCondition(condition, param);
|
|
143
113
|
}
|
|
144
|
-
|
|
145
|
-
|
|
114
|
+
return s;
|
|
115
|
+
}
|
|
116
|
+
function convertOrdering(ordering) {
|
|
117
|
+
if (ordering._column) {
|
|
118
|
+
let s = convertColumn(ordering._column);
|
|
119
|
+
if (ordering._nullsPosition != null) {
|
|
120
|
+
s += " IS NULL " + (ordering._nullsPosition === "FIRST" ? "DESC" : "ASC") + ", " + s;
|
|
121
|
+
}
|
|
122
|
+
if (ordering._direction === "ASC") s += " ASC";
|
|
123
|
+
if (ordering._direction === "DESC") s += " DESC";
|
|
124
|
+
return s;
|
|
125
|
+
} else {
|
|
126
|
+
return convertColumn(ordering);
|
|
146
127
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
128
|
+
}
|
|
129
|
+
function convertTable(table) {
|
|
130
|
+
return options.nameEscape + table._$name + options.nameEscape;
|
|
131
|
+
}
|
|
132
|
+
function convertColumn(column) {
|
|
133
|
+
let s = "";
|
|
134
|
+
if (!(column._name === "*" && column._modifiers.length > 0 && column._modifiers[0].name === "count")) {
|
|
135
|
+
s += convertTable(column._table) + ".";
|
|
154
136
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
else if (name === 'max')
|
|
172
|
-
s = 'MAX(' + s + ')';
|
|
173
|
-
else if (name === 'as')
|
|
174
|
-
s = s + ' AS ' + options.nameEscape + modifier.params + options.nameEscape;
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
return s + '';
|
|
137
|
+
s += convertColumnName(column);
|
|
138
|
+
return convertColumnModifiers(s, column);
|
|
139
|
+
}
|
|
140
|
+
function convertColumnModifiers(s, column) {
|
|
141
|
+
if (column._modifiers) {
|
|
142
|
+
column._modifiers.forEach((modifier) => {
|
|
143
|
+
let name = modifier.name;
|
|
144
|
+
if (name === "lower") s = "LOWER(" + s + ")";
|
|
145
|
+
else if (name === "upper") s = "UPPER(" + s + ")";
|
|
146
|
+
else if (name === "count") s = "COUNT(" + s + ")";
|
|
147
|
+
else if (name === "sum") s = "SUM(" + s + ")";
|
|
148
|
+
else if (name === "avg") s = "AVG(" + s + ")";
|
|
149
|
+
else if (name === "min") s = "MIN(" + s + ")";
|
|
150
|
+
else if (name === "max") s = "MAX(" + s + ")";
|
|
151
|
+
else if (name === "as") s = s + " AS " + options.nameEscape + modifier.params + options.nameEscape;
|
|
152
|
+
});
|
|
178
153
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
154
|
+
return s + "";
|
|
155
|
+
}
|
|
156
|
+
function convertColumnName(column) {
|
|
157
|
+
if (column._name === "*") return column._name;
|
|
158
|
+
let name = typeof column._name === "string" ? column._name : column._name.name;
|
|
159
|
+
return options.nameEscape + name + options.nameEscape;
|
|
160
|
+
}
|
|
161
|
+
function preprocessConditions(conditions) {
|
|
162
|
+
conditions.forEach((condition) => {
|
|
163
|
+
if (conditions.length > 1 && condition._sibling) {
|
|
164
|
+
condition._parenthesis = true;
|
|
165
|
+
}
|
|
166
|
+
preprocessParams(condition);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
function preprocessParams(condition) {
|
|
170
|
+
if (condition._sibling) {
|
|
171
|
+
preprocessParams(condition._sibling);
|
|
184
172
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
if (conditions.length > 1 && condition._sibling) {
|
|
188
|
-
condition._parenthesis = true;
|
|
189
|
-
}
|
|
190
|
-
preprocessParams(condition);
|
|
191
|
-
});
|
|
173
|
+
if (!condition._sibling && !condition._child) {
|
|
174
|
+
condition.__param = getConditionParam(condition);
|
|
192
175
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
if (condition._sibling) {
|
|
196
|
-
preprocessParams(condition._sibling);
|
|
197
|
-
}
|
|
198
|
-
if (!condition._sibling && !condition._child) {
|
|
199
|
-
condition.__param = getConditionParam(condition);
|
|
200
|
-
}
|
|
201
|
-
if (condition._child) {
|
|
202
|
-
preprocessParams(condition._child);
|
|
203
|
-
}
|
|
176
|
+
if (condition._child) {
|
|
177
|
+
preprocessParams(condition._child);
|
|
204
178
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
let s = '';
|
|
210
|
-
if (condition._child) {
|
|
211
|
-
s += convertCondition(condition._child);
|
|
212
|
-
}
|
|
213
|
-
if (condition._sibling) {
|
|
214
|
-
s = convertCondition(condition._sibling, root) + ' ' + condition._chainType.toUpperCase() + ' ' + s;
|
|
215
|
-
}
|
|
216
|
-
if (condition._parenthesis || ((!root || condition._negation) && condition._child)) {
|
|
217
|
-
s = '( ' + s + ' )';
|
|
218
|
-
}
|
|
219
|
-
if (condition._negation) {
|
|
220
|
-
s = 'NOT ' + s;
|
|
221
|
-
}
|
|
222
|
-
return s;
|
|
179
|
+
}
|
|
180
|
+
function convertCondition(condition, root = false) {
|
|
181
|
+
if (!condition._sibling && !condition._child) {
|
|
182
|
+
return convertColumnCondition(condition, condition.__param);
|
|
223
183
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
return s;
|
|
184
|
+
let s = "";
|
|
185
|
+
if (condition._child) {
|
|
186
|
+
s += convertCondition(condition._child);
|
|
228
187
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
case 'eq': return ' = ' + param;
|
|
232
|
-
case 'ne': return ' <> ' + param;
|
|
233
|
-
case 'lt': return ' < ' + param;
|
|
234
|
-
case 'gt': return ' > ' + param;
|
|
235
|
-
case 'lte': return ' <= ' + param;
|
|
236
|
-
case 'gte': return ' >= ' + param;
|
|
237
|
-
case 'is-null': return ' IS NULL';
|
|
238
|
-
case 'is-not-null': return ' IS NOT NULL';
|
|
239
|
-
case 'like': return ' LIKE ' + param;
|
|
240
|
-
case 'not-like': return ' NOT LIKE ' + param;
|
|
241
|
-
case 'in': return ' IN (' + param + ')';
|
|
242
|
-
case 'not-in': return ' NOT IN (' + param + ')';
|
|
243
|
-
case 'between': return ' BETWEEN ' + param;
|
|
244
|
-
case 'not-between': return ' NOT BETWEEN ' + param;
|
|
245
|
-
default: return '';
|
|
246
|
-
}
|
|
188
|
+
if (condition._sibling) {
|
|
189
|
+
s = convertCondition(condition._sibling, root) + " " + condition._chainType.toUpperCase() + " " + s;
|
|
247
190
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if (condition._otherColumn) {
|
|
251
|
-
param = convertColumn(condition._otherColumn);
|
|
252
|
-
}
|
|
253
|
-
else {
|
|
254
|
-
let _convertParam = (param) => convertParam(condition._column, param);
|
|
255
|
-
if (condition._type === 'in' || condition._type === 'not-in') {
|
|
256
|
-
param = condition._values.map((value) => _convertParam(value)).join(', ');
|
|
257
|
-
}
|
|
258
|
-
else if (condition._type === 'between' || condition._type === 'not-between') {
|
|
259
|
-
param = _convertParam(condition._values[0]) + ' AND ' + _convertParam(condition._values[1]);
|
|
260
|
-
}
|
|
261
|
-
else if (condition._type !== 'is-null' && condition._type !== 'is-not-null') {
|
|
262
|
-
param = _convertParam(condition._values[0]);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
return param;
|
|
191
|
+
if (condition._parenthesis || (!root || condition._negation) && condition._child) {
|
|
192
|
+
s = "( " + s + " )";
|
|
266
193
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
return 'NULL';
|
|
270
|
-
return paramConverter(getTypedParam(column._type, param));
|
|
194
|
+
if (condition._negation) {
|
|
195
|
+
s = "NOT " + s;
|
|
271
196
|
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
197
|
+
return s;
|
|
198
|
+
}
|
|
199
|
+
function convertColumnCondition(condition, param) {
|
|
200
|
+
let s = convertColumn(condition._column);
|
|
201
|
+
s += getConditionString(condition, param);
|
|
202
|
+
return s;
|
|
203
|
+
}
|
|
204
|
+
function getConditionString(condition, param) {
|
|
205
|
+
switch (condition._type) {
|
|
206
|
+
case "eq":
|
|
207
|
+
return " = " + param;
|
|
208
|
+
case "ne":
|
|
209
|
+
return " <> " + param;
|
|
210
|
+
case "lt":
|
|
211
|
+
return " < " + param;
|
|
212
|
+
case "gt":
|
|
213
|
+
return " > " + param;
|
|
214
|
+
case "lte":
|
|
215
|
+
return " <= " + param;
|
|
216
|
+
case "gte":
|
|
217
|
+
return " >= " + param;
|
|
218
|
+
case "is-null":
|
|
219
|
+
return " IS NULL";
|
|
220
|
+
case "is-not-null":
|
|
221
|
+
return " IS NOT NULL";
|
|
222
|
+
case "like":
|
|
223
|
+
return " LIKE " + param;
|
|
224
|
+
case "not-like":
|
|
225
|
+
return " NOT LIKE " + param;
|
|
226
|
+
case "in":
|
|
227
|
+
return " IN (" + param + ")";
|
|
228
|
+
case "not-in":
|
|
229
|
+
return " NOT IN (" + param + ")";
|
|
230
|
+
case "between":
|
|
231
|
+
return " BETWEEN " + param;
|
|
232
|
+
case "not-between":
|
|
233
|
+
return " NOT BETWEEN " + param;
|
|
234
|
+
default:
|
|
235
|
+
return "";
|
|
282
236
|
}
|
|
237
|
+
}
|
|
238
|
+
function getConditionParam(condition) {
|
|
239
|
+
let param = "";
|
|
240
|
+
if (condition._otherColumn) {
|
|
241
|
+
param = convertColumn(condition._otherColumn);
|
|
242
|
+
} else {
|
|
243
|
+
let _convertParam = (param2) => convertParam(condition._column, param2);
|
|
244
|
+
if (condition._type === "in" || condition._type === "not-in") {
|
|
245
|
+
param = condition._values.map((value) => _convertParam(value)).join(", ");
|
|
246
|
+
} else if (condition._type === "between" || condition._type === "not-between") {
|
|
247
|
+
param = _convertParam(condition._values[0]) + " AND " + _convertParam(condition._values[1]);
|
|
248
|
+
} else if (condition._type !== "is-null" && condition._type !== "is-not-null") {
|
|
249
|
+
param = _convertParam(condition._values[0]);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return param;
|
|
253
|
+
}
|
|
254
|
+
function convertParam(column, param) {
|
|
255
|
+
if (param == null) return "NULL";
|
|
256
|
+
return paramConverter(getTypedParam(column._type, param));
|
|
257
|
+
}
|
|
258
|
+
function getTypedParam(type, param) {
|
|
259
|
+
if (type === "number") return number(param);
|
|
260
|
+
else if (type === "boolean") return boolean(param);
|
|
261
|
+
else if (type === "date") return date(param);
|
|
262
|
+
else if (type === "string") return string(param);
|
|
263
|
+
return param;
|
|
264
|
+
}
|
|
283
265
|
}
|
|
284
|
-
|
|
266
|
+
export {
|
|
267
|
+
createQueryConverter
|
|
268
|
+
};
|