@sqb/postgres-dialect 4.0.10 → 4.0.12
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/PostgresSerializer.d.ts +9 -0
- package/dist/PostgresSerializer.js +82 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -0
- package/package.json +1 -1
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SerializerExtension, SerializeContext, DefaultSerializeFunction, SerializationType } from '@sqb/builder';
|
|
2
|
+
export declare class PostgresSerializer implements SerializerExtension {
|
|
3
|
+
dialect: string;
|
|
4
|
+
isReservedWord(ctx: any, s: any): any;
|
|
5
|
+
serialize(ctx: SerializeContext, type: SerializationType | string, o: any, defFn: DefaultSerializeFunction): string | undefined;
|
|
6
|
+
private _serializeSelect;
|
|
7
|
+
private _serializeComparison;
|
|
8
|
+
private _serializeParameter;
|
|
9
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PostgresSerializer = void 0;
|
|
4
|
+
const builder_1 = require("@sqb/builder");
|
|
5
|
+
const reservedWords = ['comment'];
|
|
6
|
+
class PostgresSerializer {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.dialect = 'postgres';
|
|
9
|
+
}
|
|
10
|
+
isReservedWord(ctx, s) {
|
|
11
|
+
return s && typeof s === 'string' &&
|
|
12
|
+
reservedWords.includes(s.toLowerCase());
|
|
13
|
+
}
|
|
14
|
+
serialize(ctx, type, o, defFn) {
|
|
15
|
+
switch (type) {
|
|
16
|
+
case builder_1.SerializationType.SELECT_QUERY:
|
|
17
|
+
return this._serializeSelect(ctx, o, defFn);
|
|
18
|
+
case builder_1.SerializationType.COMPARISON_EXPRESSION:
|
|
19
|
+
return this._serializeComparison(ctx, o, defFn);
|
|
20
|
+
case builder_1.SerializationType.EXTERNAL_PARAMETER:
|
|
21
|
+
return this._serializeParameter(ctx, o, defFn);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
_serializeSelect(ctx, o, defFn) {
|
|
25
|
+
let out = defFn(ctx, o);
|
|
26
|
+
const limit = o.limit || 0;
|
|
27
|
+
const offset = Math.max((o.offset || 0), 0);
|
|
28
|
+
if (limit)
|
|
29
|
+
out += '\nLIMIT ' + limit;
|
|
30
|
+
if (offset)
|
|
31
|
+
out += (!limit ? '\n' : ' ') + 'OFFSET ' + offset;
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
_serializeComparison(ctx, o, defFn) {
|
|
35
|
+
if (o.right) {
|
|
36
|
+
if (o.right.expression.toLowerCase() === 'null') {
|
|
37
|
+
if (o.operatorType === 'eq')
|
|
38
|
+
return defFn(ctx, { ...o, operatorType: builder_1.OperatorType.is, symbol: 'is' });
|
|
39
|
+
if (o.operatorType === 'ne')
|
|
40
|
+
return defFn(ctx, { ...o, operatorType: builder_1.OperatorType.isNot, symbol: 'is not' });
|
|
41
|
+
}
|
|
42
|
+
if (o.left.isParam && o.left.isArray && o.left.value != null && !Array.isArray(o.left.value))
|
|
43
|
+
o.left.value = [o.left.value];
|
|
44
|
+
if (o.right.isParam && o.right.isArray && o.right.value != null && !Array.isArray(o.right.value))
|
|
45
|
+
o.right.value = [o.right.value];
|
|
46
|
+
if ((o.operatorType === 'in' || o.operatorType === 'notIn')) {
|
|
47
|
+
if (o.left.isArray && !o.right.isArray && o.right.isParam) {
|
|
48
|
+
const left = o.left;
|
|
49
|
+
const right = o.right;
|
|
50
|
+
left.expression = 'ANY(' + left.expression + ')';
|
|
51
|
+
return defFn(ctx, {
|
|
52
|
+
...o,
|
|
53
|
+
operatorType: builder_1.OperatorType.eq,
|
|
54
|
+
symbol: o.operatorType === 'notIn' ? '!=' : '=',
|
|
55
|
+
left: right,
|
|
56
|
+
right: left
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
if (o.left.isArray && o.right.isArray) {
|
|
60
|
+
if (o.operatorType === 'notIn')
|
|
61
|
+
o.left.expression = 'not ' + o.left.expression;
|
|
62
|
+
return defFn(ctx, { ...o, symbol: '&&' });
|
|
63
|
+
}
|
|
64
|
+
if (!o.left.isArray && o.right.isArray && o.right.isParam) {
|
|
65
|
+
o.right.expression = 'ANY(' + o.right.expression + ')';
|
|
66
|
+
return defFn(ctx, {
|
|
67
|
+
...o,
|
|
68
|
+
operatorType: builder_1.OperatorType.eq,
|
|
69
|
+
symbol: o.operatorType === 'notIn' ? '!=' : '='
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return defFn(ctx, o);
|
|
75
|
+
}
|
|
76
|
+
_serializeParameter(ctx, o, defFn) {
|
|
77
|
+
ctx.preparedParams = ctx.preparedParams || [];
|
|
78
|
+
defFn(ctx, o);
|
|
79
|
+
return '$' + ctx.preparedParams.length;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
exports.PostgresSerializer = PostgresSerializer;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.js
ADDED
package/package.json
CHANGED