@asla/yoursql 0.1.2 → 0.3.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/mod.js +36 -16
- package/dist/sql_value/sql_value.d.ts +8 -8
- package/package.json +1 -1
package/dist/mod.js
CHANGED
|
@@ -75,6 +75,12 @@ _SqlRaw_value = new WeakMap();
|
|
|
75
75
|
* @public
|
|
76
76
|
*/
|
|
77
77
|
class SqlValuesCreator {
|
|
78
|
+
static create(map) {
|
|
79
|
+
const obj = new this(map);
|
|
80
|
+
const fn = obj.toSqlStr.bind(obj);
|
|
81
|
+
Reflect.setPrototypeOf(fn, obj);
|
|
82
|
+
return fn;
|
|
83
|
+
}
|
|
78
84
|
/**
|
|
79
85
|
* 将字符串转为 SQL 的字符串值的形式(单引号会被转义)。
|
|
80
86
|
* @example 输入 a'b'c , 返回 a''b''c.
|
|
@@ -87,10 +93,6 @@ class SqlValuesCreator {
|
|
|
87
93
|
*/
|
|
88
94
|
constructor(map = new Map()) {
|
|
89
95
|
this.map = map;
|
|
90
|
-
const fn = this.toSqlStr.bind(this);
|
|
91
|
-
this.toSqlStr = fn;
|
|
92
|
-
Reflect.setPrototypeOf(fn, this);
|
|
93
|
-
return fn;
|
|
94
96
|
}
|
|
95
97
|
/** 设置转换器 */
|
|
96
98
|
setTransformer(type, transformer) {
|
|
@@ -104,7 +106,10 @@ class SqlValuesCreator {
|
|
|
104
106
|
let basicType;
|
|
105
107
|
if (expectType) {
|
|
106
108
|
if (typeof expectType === "function") {
|
|
107
|
-
|
|
109
|
+
let type = this.map.get(expectType);
|
|
110
|
+
if (!type)
|
|
111
|
+
throw new Error("类型不存在");
|
|
112
|
+
return type.call(this, value);
|
|
108
113
|
}
|
|
109
114
|
else {
|
|
110
115
|
basicType = expectType;
|
|
@@ -121,8 +126,13 @@ class SqlValuesCreator {
|
|
|
121
126
|
return SqlValuesCreator.string(value);
|
|
122
127
|
case "boolean":
|
|
123
128
|
return value.toString();
|
|
124
|
-
case "object":
|
|
125
|
-
|
|
129
|
+
case "object": {
|
|
130
|
+
if (value === null)
|
|
131
|
+
return "NULL";
|
|
132
|
+
if (value instanceof SqlRaw)
|
|
133
|
+
return value.toString();
|
|
134
|
+
return this.getObjectType(value).call(this, value);
|
|
135
|
+
}
|
|
126
136
|
case "undefined":
|
|
127
137
|
return "DEFAULT";
|
|
128
138
|
default:
|
|
@@ -131,16 +141,13 @@ class SqlValuesCreator {
|
|
|
131
141
|
throw new Error("不支持转换 " + type + " 类型");
|
|
132
142
|
}
|
|
133
143
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
return "NULL";
|
|
137
|
-
if (value instanceof SqlRaw)
|
|
138
|
-
return value.toString();
|
|
144
|
+
/** 获取值对应的 SqlValueEncoder */
|
|
145
|
+
getObjectType(value) {
|
|
139
146
|
for (const Class of this.map.keys()) {
|
|
140
147
|
if (value instanceof Class)
|
|
141
|
-
return this.map.get(Class)
|
|
148
|
+
return this.map.get(Class);
|
|
142
149
|
}
|
|
143
|
-
return this.defaultObject
|
|
150
|
+
return this.defaultObject;
|
|
144
151
|
}
|
|
145
152
|
defaultObject(value) {
|
|
146
153
|
return SqlValuesCreator.string(JSON.stringify(value));
|
|
@@ -233,7 +240,20 @@ const pgSqlTransformer = new Map([
|
|
|
233
240
|
[
|
|
234
241
|
Array,
|
|
235
242
|
function encodePgArray(value) {
|
|
236
|
-
|
|
243
|
+
if (value.length === 0)
|
|
244
|
+
return "NULL";
|
|
245
|
+
const valueStr = [];
|
|
246
|
+
let type;
|
|
247
|
+
let basicType;
|
|
248
|
+
for (let i = 0; i < value.length; i++) {
|
|
249
|
+
{
|
|
250
|
+
basicType = typeof value[i];
|
|
251
|
+
if (value[i] === null || basicType === "undefined")
|
|
252
|
+
basicType = undefined;
|
|
253
|
+
valueStr[i] = this.toSqlStr(value[i], type);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return "ARRAY[" + valueStr.join(",") + "]";
|
|
237
257
|
},
|
|
238
258
|
],
|
|
239
259
|
[Date, (value) => SqlValuesCreator.string(value.toISOString())],
|
|
@@ -646,7 +666,7 @@ class DbTableQuery extends DbTable {
|
|
|
646
666
|
for (const [k, v] of updateKey) {
|
|
647
667
|
if (v === undefined)
|
|
648
668
|
continue;
|
|
649
|
-
setList.push(k + " = " + this.statement(v));
|
|
669
|
+
setList.push(k + " = " + this.statement.toSqlStr(v));
|
|
650
670
|
}
|
|
651
671
|
let sql = `UPDATE ${this.name}\nSET ${setList.join(",\n")}`;
|
|
652
672
|
if (where)
|
|
@@ -13,13 +13,11 @@ export declare class SqlRaw<T = any> {
|
|
|
13
13
|
/** @public */
|
|
14
14
|
export type JsObjectMapSql = Map<new (...args: any[]) => any, SqlValueEncoder>;
|
|
15
15
|
/** @public */
|
|
16
|
-
export type SqlValueEncoder<T = any> = (this: SqlValuesCreator, value: T
|
|
16
|
+
export type SqlValueEncoder<T = any> = (this: SqlValuesCreator, value: T) => string;
|
|
17
17
|
/** @public */
|
|
18
|
-
export type ManualType = "bigint" | "number" | "string" | "boolean" | (new (...args: any[]) => any);
|
|
19
|
-
/**
|
|
20
|
-
|
|
21
|
-
*/
|
|
22
|
-
export interface SqlValuesCreator {
|
|
18
|
+
export type ManualType = "bigint" | "number" | "string" | "boolean" | "object" | (new (...args: any[]) => any);
|
|
19
|
+
/** @public */
|
|
20
|
+
export interface SqlValueFn {
|
|
23
21
|
/** 将 JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT */
|
|
24
22
|
(value: any, expectType?: ManualType): string;
|
|
25
23
|
}
|
|
@@ -28,6 +26,7 @@ export interface SqlValuesCreator {
|
|
|
28
26
|
* @public
|
|
29
27
|
*/
|
|
30
28
|
export declare class SqlValuesCreator {
|
|
29
|
+
static create(map?: JsObjectMapSql): SqlValuesCreator & SqlValueFn;
|
|
31
30
|
/**
|
|
32
31
|
* 将字符串转为 SQL 的字符串值的形式(单引号会被转义)。
|
|
33
32
|
* @example 输入 a'b'c , 返回 a''b''c.
|
|
@@ -41,8 +40,9 @@ export declare class SqlValuesCreator {
|
|
|
41
40
|
setTransformer<T>(type: new (...args: any[]) => T, transformer?: SqlValueEncoder): void;
|
|
42
41
|
private readonly map;
|
|
43
42
|
/** 将 JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT */
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
toSqlStr(value: any, expectType?: ManualType): string;
|
|
44
|
+
/** 获取值对应的 SqlValueEncoder */
|
|
45
|
+
getObjectType(value: object): SqlValueEncoder;
|
|
46
46
|
protected defaultObject(value: object): string;
|
|
47
47
|
/**
|
|
48
48
|
* 将对象列表转为 SQL 的 VALUES
|