taon-type-sql 21.0.23 → 21.0.25
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 +26 -14
- package/lib-prod/builder/column/basic-column.js +8 -10
- package/lib-prod/builder/column/boolean-column.js +8 -11
- package/lib-prod/builder/column/comparable-column.js +35 -38
- package/lib-prod/builder/column/date-column.js +8 -11
- package/lib-prod/builder/column/number-column.js +14 -17
- package/lib-prod/builder/column/query-column.js +15 -21
- package/lib-prod/builder/column/string-column.js +29 -32
- package/lib-prod/builder/column/value-column.js +20 -22
- package/lib-prod/builder/condition/query-column-condition.js +13 -19
- package/lib-prod/builder/condition/query-condition-chain.js +24 -31
- package/lib-prod/builder/condition/query-condition.js +2 -4
- package/lib-prod/builder/condition/query-join-condition.js +13 -19
- package/lib-prod/builder/helpers/generics-helper.js +2 -4
- package/lib-prod/builder/helpers/internal-types.js +1 -0
- package/lib-prod/builder/join/joined-tables-chain.js +9 -12
- package/lib-prod/builder/join/joined-tables.js +17 -20
- package/lib-prod/builder/other/query-ordering.js +13 -19
- package/lib-prod/builder/query/select-query.js +44 -50
- package/lib-prod/builder/query/table-condition-query.js +21 -26
- package/lib-prod/builder/query/table-query.js +49 -51
- package/lib-prod/builder/query-source.js +15 -16
- package/lib-prod/builder/query-table.js +18 -23
- package/lib-prod/client/mysql.js +4 -7
- package/lib-prod/client/pg.js +4 -7
- package/lib-prod/client/query-processor.js +64 -39
- package/lib-prod/converter/param-converter.js +24 -21
- package/lib-prod/converter/parameterized-converter.js +10 -13
- package/lib-prod/converter/query-converter.js +256 -244
- package/lib-prod/converter/result-converter.js +70 -62
- package/lib-prod/converter/sql-converter.js +3 -6
- package/lib-prod/converter/type-converter.js +32 -28
- package/lib-prod/converter/types.js +1 -0
- package/lib-prod/env/env.angular-node-app.js +66 -130
- package/lib-prod/env/env.docs-webapp.js +66 -130
- package/lib-prod/env/env.electron-app.js +66 -130
- package/lib-prod/env/env.mobile-app.js +66 -130
- package/lib-prod/env/env.npm-lib-and-cli-tool.js +66 -130
- package/lib-prod/env/env.vscode-plugin.js +66 -130
- package/lib-prod/env/index.js +6 -6
- package/lib-prod/index._auto-generated_.js +5 -0
- package/lib-prod/index.js +25 -46
- package/lib-prod/migrations/index.js +2 -1
- package/lib-prod/migrations/migrations_index._auto-generated_.js +3 -0
- 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
|
@@ -1,20 +1,14 @@
|
|
|
1
|
-
class QueryOrdering {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
this._nullsPosition = "LAST";
|
|
15
|
-
return this;
|
|
16
|
-
}
|
|
1
|
+
export default class QueryOrdering {
|
|
2
|
+
constructor(column, direction) {
|
|
3
|
+
this._column = column;
|
|
4
|
+
this._direction = direction;
|
|
5
|
+
}
|
|
6
|
+
nullsFirst() {
|
|
7
|
+
this._nullsPosition = 'FIRST';
|
|
8
|
+
return this;
|
|
9
|
+
}
|
|
10
|
+
nullsLast() {
|
|
11
|
+
this._nullsPosition = 'LAST';
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
17
14
|
}
|
|
18
|
-
export {
|
|
19
|
-
QueryOrdering as default
|
|
20
|
-
};
|
|
@@ -1,51 +1,45 @@
|
|
|
1
|
-
class SelectQuery {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
this._action = "select";
|
|
46
|
-
return this._queryProcessor(this);
|
|
47
|
-
}
|
|
1
|
+
export default class SelectQuery {
|
|
2
|
+
constructor(_queryProcessor, _tables) {
|
|
3
|
+
this._queryProcessor = _queryProcessor;
|
|
4
|
+
this._tables = _tables;
|
|
5
|
+
this._distinct = false;
|
|
6
|
+
this._conditions = [];
|
|
7
|
+
this._groupBy = [];
|
|
8
|
+
this._having = [];
|
|
9
|
+
this._orderings = [];
|
|
10
|
+
this._columns = [];
|
|
11
|
+
}
|
|
12
|
+
offset(offset) {
|
|
13
|
+
this._offset = offset;
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
limit(limit) {
|
|
17
|
+
this._limit = limit;
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
distinct() {
|
|
21
|
+
this._distinct = true;
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
where(...conditions) {
|
|
25
|
+
this._conditions = conditions;
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
groupBy(...columns) {
|
|
29
|
+
this._groupBy = columns;
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
having(...conditions) {
|
|
33
|
+
this._having = conditions;
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
orderBy(...orderings) {
|
|
37
|
+
this._orderings = orderings;
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
select(...columns) {
|
|
41
|
+
this._columns = columns;
|
|
42
|
+
this._action = 'select';
|
|
43
|
+
return this._queryProcessor(this);
|
|
44
|
+
}
|
|
48
45
|
}
|
|
49
|
-
export {
|
|
50
|
-
SelectQuery as default
|
|
51
|
-
};
|
|
@@ -1,27 +1,22 @@
|
|
|
1
|
-
class TableConditionQuery {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return this._queryProcessor(this).then((rows) => rows[0]);
|
|
23
|
-
}
|
|
1
|
+
export default class TableConditionQuery {
|
|
2
|
+
constructor(_queryProcessor, _table, _conditions) {
|
|
3
|
+
this._queryProcessor = _queryProcessor;
|
|
4
|
+
this._table = _table;
|
|
5
|
+
this._conditions = _conditions;
|
|
6
|
+
this._columns = [];
|
|
7
|
+
}
|
|
8
|
+
update(entity) {
|
|
9
|
+
this._entity = entity;
|
|
10
|
+
this._action = 'update';
|
|
11
|
+
return this._queryProcessor(this);
|
|
12
|
+
}
|
|
13
|
+
delete() {
|
|
14
|
+
this._action = 'delete';
|
|
15
|
+
return this._queryProcessor(this);
|
|
16
|
+
}
|
|
17
|
+
count() {
|
|
18
|
+
this._columns = [this._table.$all.count()];
|
|
19
|
+
this._action = 'select';
|
|
20
|
+
return this._queryProcessor(this).then((rows) => rows[0]);
|
|
21
|
+
}
|
|
24
22
|
}
|
|
25
|
-
export {
|
|
26
|
-
TableConditionQuery as default
|
|
27
|
-
};
|
|
@@ -1,54 +1,52 @@
|
|
|
1
1
|
import TableConditionQuery from "./table-condition-query";
|
|
2
2
|
import ValueColumn from "../column/value-column";
|
|
3
|
-
class TableQuery {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
3
|
+
export default class TableQuery {
|
|
4
|
+
constructor(_queryProcessor, _table) {
|
|
5
|
+
this._queryProcessor = _queryProcessor;
|
|
6
|
+
this._table = _table;
|
|
7
|
+
this._columns = [];
|
|
8
|
+
}
|
|
9
|
+
where(...conditions) {
|
|
10
|
+
return new TableConditionQuery(this._queryProcessor, this._table, conditions);
|
|
11
|
+
}
|
|
12
|
+
insert(param) {
|
|
13
|
+
this._entity = param;
|
|
14
|
+
this._action = 'insert';
|
|
15
|
+
return this._queryProcessor(this);
|
|
16
|
+
}
|
|
17
|
+
deleteAll() {
|
|
18
|
+
this._action = 'delete';
|
|
19
|
+
return this._queryProcessor(this);
|
|
20
|
+
}
|
|
21
|
+
updateAll(entity) {
|
|
22
|
+
this._entity = entity;
|
|
23
|
+
this._action = 'update';
|
|
24
|
+
return this._queryProcessor(this);
|
|
25
|
+
}
|
|
26
|
+
countAll() {
|
|
27
|
+
this._columns = [this._table.$all.count()];
|
|
28
|
+
this._action = 'select';
|
|
29
|
+
return this._queryProcessor(this).then((rows) => rows[0]);
|
|
30
|
+
}
|
|
31
|
+
delete(id) {
|
|
32
|
+
return this._whereId(id).delete().then(count => count > 0);
|
|
33
|
+
}
|
|
34
|
+
update(id, entity) {
|
|
35
|
+
return this._whereId(id).update(entity).then(count => count > 0);
|
|
36
|
+
}
|
|
37
|
+
get(id) {
|
|
38
|
+
let query = this._whereId(id);
|
|
39
|
+
return this._queryProcessor({ _action: 'select', ...query })
|
|
40
|
+
.then((rows) => rows[0]);
|
|
41
|
+
}
|
|
42
|
+
_whereId(id) {
|
|
43
|
+
// TODO remove assertions if $id typing is fixed
|
|
44
|
+
let $id = this._table.$id;
|
|
45
|
+
if ($id instanceof ValueColumn) {
|
|
46
|
+
return this.where($id.eq(id));
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
return this.where(...Object.keys($id).map(key => this._table[key].eq(id[key])));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
51
52
|
}
|
|
52
|
-
export {
|
|
53
|
-
TableQuery as default
|
|
54
|
-
};
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import SelectQuery from
|
|
1
|
+
import SelectQuery from './query/select-query';
|
|
2
2
|
import TableQuery from "./query/table-query";
|
|
3
|
-
class QuerySource {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
3
|
+
export default class QuerySource {
|
|
4
|
+
constructor(_queryProcessor) {
|
|
5
|
+
this._queryProcessor = _queryProcessor;
|
|
6
|
+
}
|
|
7
|
+
from(table1, table2, table3) {
|
|
8
|
+
if (table3 != null)
|
|
9
|
+
return new SelectQuery(this._queryProcessor, [table1, table2, table3]);
|
|
10
|
+
else if (table2 != null)
|
|
11
|
+
return new SelectQuery(this._queryProcessor, [table1, table2]);
|
|
12
|
+
return new SelectQuery(this._queryProcessor, [table1]);
|
|
13
|
+
}
|
|
14
|
+
table(table) {
|
|
15
|
+
return new TableQuery(this._queryProcessor, table);
|
|
16
|
+
}
|
|
15
17
|
}
|
|
16
|
-
export {
|
|
17
|
-
QuerySource as default
|
|
18
|
-
};
|
|
@@ -1,27 +1,22 @@
|
|
|
1
1
|
import BasicColumn from "./column/basic-column";
|
|
2
2
|
import JoinedTablesChain from "./join/joined-tables-chain";
|
|
3
3
|
class QueryTable {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return new JoinedTablesChain(table, "full", this);
|
|
22
|
-
}
|
|
4
|
+
constructor(_$name) {
|
|
5
|
+
this._$name = _$name;
|
|
6
|
+
// abstract readonly $id; // FIXME I got a dozen incomprehensible type errors
|
|
7
|
+
this.$all = new BasicColumn(this, '*');
|
|
8
|
+
}
|
|
9
|
+
innerJoin(table) {
|
|
10
|
+
return new JoinedTablesChain(table, 'inner', this);
|
|
11
|
+
}
|
|
12
|
+
leftJoin(table) {
|
|
13
|
+
return new JoinedTablesChain(table, 'left', this);
|
|
14
|
+
}
|
|
15
|
+
rightJoin(table) {
|
|
16
|
+
return new JoinedTablesChain(table, 'right', this);
|
|
17
|
+
}
|
|
18
|
+
fullJoin(table) {
|
|
19
|
+
return new JoinedTablesChain(table, 'full', this);
|
|
20
|
+
}
|
|
23
21
|
}
|
|
24
|
-
|
|
25
|
-
export {
|
|
26
|
-
query_table_default as default
|
|
27
|
-
};
|
|
22
|
+
export default QueryTable;
|
package/lib-prod/client/mysql.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import QuerySource from "../builder/query-source";
|
|
2
2
|
import { createQueryProcessor } from "./query-processor";
|
|
3
|
-
class MySqlQuerySource extends QuerySource {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
export default class MySqlQuerySource extends QuerySource {
|
|
4
|
+
constructor(client, options = {}) {
|
|
5
|
+
super(createQueryProcessor(client, options, 'mysql'));
|
|
6
|
+
}
|
|
7
7
|
}
|
|
8
|
-
export {
|
|
9
|
-
MySqlQuerySource as default
|
|
10
|
-
};
|
package/lib-prod/client/pg.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import QuerySource from "../builder/query-source";
|
|
2
2
|
import { createQueryProcessor } from "./query-processor";
|
|
3
|
-
class PgQuerySource extends QuerySource {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
export default class PgQuerySource extends QuerySource {
|
|
4
|
+
constructor(client, options = {}) {
|
|
5
|
+
super(createQueryProcessor(client, options, 'pg'));
|
|
6
|
+
}
|
|
7
7
|
}
|
|
8
|
-
export {
|
|
9
|
-
PgQuerySource as default
|
|
10
|
-
};
|
|
@@ -1,44 +1,69 @@
|
|
|
1
|
-
import { Log } from
|
|
2
|
-
import { convertQueryToParameterizedSQL } from
|
|
3
|
-
import { convertQueryToSQL } from
|
|
4
|
-
const log = Log.create(
|
|
5
|
-
"query processor"
|
|
6
|
-
// Level.__NOTHING
|
|
7
|
-
);
|
|
1
|
+
import { Log } from 'ng2-logger/lib-prod';
|
|
2
|
+
import { convertQueryToParameterizedSQL } from '../converter/parameterized-converter';
|
|
3
|
+
import { convertQueryToSQL } from '../converter/sql-converter';
|
|
4
|
+
const log = Log.create('query processor');
|
|
8
5
|
const DEFAULT_OPTIONS = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
lineBreaks: false,
|
|
7
|
+
parameterized: true,
|
|
8
|
+
logging: true,
|
|
9
|
+
identifierQuote: '"'
|
|
13
10
|
};
|
|
14
11
|
function mySqlTypeCast(field, next) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
function createQueryProcessor(client, _options = {}, engine = "pg") {
|
|
27
|
-
let options = Object.assign({}, DEFAULT_OPTIONS, _options);
|
|
28
|
-
let queryOptions = {
|
|
29
|
-
lineBreak: options.lineBreaks ? "\n" : " ",
|
|
30
|
-
nameEscape: _options.identifierQuote || (engine === "mysql" ? "`" : '"')
|
|
31
|
-
};
|
|
32
|
-
return (query) => {
|
|
33
|
-
if (options.parameterized) {
|
|
34
|
-
let { sql, params } = convertQueryToParameterizedSQL(query, queryOptions, engine);
|
|
35
|
-
return client.query(sql, params);
|
|
36
|
-
} else {
|
|
37
|
-
let sql = convertQueryToSQL(query, queryOptions, engine);
|
|
38
|
-
return client.query(sql, void 0);
|
|
12
|
+
if (field.type == 'TINY' && field.length == 1) { // Boolean
|
|
13
|
+
let value = field.string();
|
|
14
|
+
if (value == '1')
|
|
15
|
+
return true;
|
|
16
|
+
if (value == '0')
|
|
17
|
+
return false;
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
else if (field.type == 'JSON') {
|
|
21
|
+
let value = field.string();
|
|
22
|
+
return value == null ? null : JSON.parse(value);
|
|
39
23
|
}
|
|
40
|
-
|
|
24
|
+
return next();
|
|
25
|
+
}
|
|
26
|
+
export function createQueryProcessor(client, _options = {}, engine = 'pg') {
|
|
27
|
+
let options = Object.assign({}, DEFAULT_OPTIONS, _options);
|
|
28
|
+
let queryOptions = {
|
|
29
|
+
lineBreak: options.lineBreaks ? '\n' : ' ',
|
|
30
|
+
nameEscape: _options.identifierQuote || (engine === 'mysql' ? '`' : '"')
|
|
31
|
+
};
|
|
32
|
+
// function processSql(query: any, sql: string, params: any[] | undefined, callback: any): Promise<any> {
|
|
33
|
+
// if (options.logging) log.i(sql);
|
|
34
|
+
// if (options.logger) options.logger(sql, params);
|
|
35
|
+
// return new Promise((resolve, reject) => {
|
|
36
|
+
// callback(sql, params, (err: any, result: any) => {
|
|
37
|
+
// if (err) reject(err);
|
|
38
|
+
// else resolve(convertResult(query, result, engine));
|
|
39
|
+
// });
|
|
40
|
+
// });
|
|
41
|
+
// }
|
|
42
|
+
// function executeSql(sql: string, params: any[] | undefined, cb: any) {
|
|
43
|
+
// if (engine === 'pg') {
|
|
44
|
+
// client.query(sql, params || cb, params ? cb : undefined);
|
|
45
|
+
// } else if (engine === 'mysql') {
|
|
46
|
+
// client.query({
|
|
47
|
+
// sql,
|
|
48
|
+
// values: params,
|
|
49
|
+
// typeCast: mySqlTypeCast
|
|
50
|
+
// }, cb);
|
|
51
|
+
// } else throw new Error('Unknown DB engine: ' + engine);
|
|
52
|
+
// }
|
|
53
|
+
return (query) => {
|
|
54
|
+
if (options.parameterized) {
|
|
55
|
+
let { sql, params } = convertQueryToParameterizedSQL(query, queryOptions, engine);
|
|
56
|
+
// if (Helpers.isWebSQL || Helpers.isNode) {
|
|
57
|
+
return client.query(sql, params);
|
|
58
|
+
// }
|
|
59
|
+
// return processSql(query, sql, params, (sql: string, params: any[], cb: any) => executeSql(sql, params, cb));
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
let sql = convertQueryToSQL(query, queryOptions, engine);
|
|
63
|
+
// if (Helpers.isWebSQL || Helpers.isNode) {
|
|
64
|
+
return client.query(sql, undefined);
|
|
65
|
+
// }
|
|
66
|
+
// return processSql(query, sql, undefined, (sql: string, params: undefined, cb: any) => executeSql(sql, undefined, cb));
|
|
67
|
+
}
|
|
68
|
+
};
|
|
41
69
|
}
|
|
42
|
-
export {
|
|
43
|
-
createQueryProcessor
|
|
44
|
-
};
|
|
@@ -1,23 +1,26 @@
|
|
|
1
|
-
function convertSubstitutionParam(param) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
export function convertSubstitutionParam(param) {
|
|
2
|
+
if (param == null)
|
|
3
|
+
return 'NULL';
|
|
4
|
+
if (typeof param === 'string' || param instanceof String) {
|
|
5
|
+
return `'${String(param)}'`;
|
|
6
|
+
}
|
|
7
|
+
else if (typeof param === 'boolean' || param instanceof Boolean) {
|
|
8
|
+
return String(param).toUpperCase();
|
|
9
|
+
}
|
|
10
|
+
else if (param instanceof Date) {
|
|
11
|
+
return `'${param.toISOString()}'`;
|
|
12
|
+
}
|
|
13
|
+
else if (typeof param === 'number' || param instanceof Number) {
|
|
14
|
+
return String(param);
|
|
15
|
+
}
|
|
16
|
+
return `'${JSON.stringify(param)}'`;
|
|
13
17
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
// node mysql doesn't have an typeCast equivalent solution for the other direction
|
|
19
|
+
// node-postgres: https://github.com/brianc/node-postgres/issues/442
|
|
20
|
+
export function convertEscapedParam(param) {
|
|
21
|
+
if (typeof param === 'object' && !(param == null || param instanceof String || param instanceof Number ||
|
|
22
|
+
param instanceof Boolean || param instanceof Date)) {
|
|
23
|
+
return JSON.stringify(param);
|
|
24
|
+
}
|
|
25
|
+
return param;
|
|
19
26
|
}
|
|
20
|
-
export {
|
|
21
|
-
convertEscapedParam,
|
|
22
|
-
convertSubstitutionParam
|
|
23
|
-
};
|
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
import { createQueryConverter } from "./query-converter";
|
|
2
|
-
import { convertEscapedParam } from
|
|
3
|
-
let pgParamConverter = (index) =>
|
|
4
|
-
let mySqlParamConverter = (index) =>
|
|
2
|
+
import { convertEscapedParam } from './param-converter';
|
|
3
|
+
let pgParamConverter = (index) => '$' + index;
|
|
4
|
+
let mySqlParamConverter = (index) => '?';
|
|
5
5
|
function convertSingleParam(param, params, paramConverter) {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
params.push(convertEscapedParam(param));
|
|
7
|
+
return paramConverter(params.length);
|
|
8
8
|
}
|
|
9
|
-
function convertQueryToParameterizedSQL(query, options, engine) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
export function convertQueryToParameterizedSQL(query, options, engine) {
|
|
10
|
+
let params = [];
|
|
11
|
+
let paramConverter = engine === 'mysql' ? mySqlParamConverter : pgParamConverter;
|
|
12
|
+
let sql = createQueryConverter((param) => convertSingleParam(param, params, paramConverter), options, engine)(query);
|
|
13
|
+
return { sql, params };
|
|
14
14
|
}
|
|
15
|
-
export {
|
|
16
|
-
convertQueryToParameterizedSQL
|
|
17
|
-
};
|