@asla/yoursql 0.2.0 → 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 +29 -17
- package/dist/sql_value/sql_value.d.ts +11 -8
- package/package.json +1 -1
package/dist/mod.js
CHANGED
|
@@ -106,7 +106,10 @@ class SqlValuesCreator {
|
|
|
106
106
|
let basicType;
|
|
107
107
|
if (expectType) {
|
|
108
108
|
if (typeof expectType === "function") {
|
|
109
|
-
|
|
109
|
+
let type = this.map.get(expectType);
|
|
110
|
+
if (!type)
|
|
111
|
+
throw new Error("类型不存在");
|
|
112
|
+
return type.call(this, value);
|
|
110
113
|
}
|
|
111
114
|
else {
|
|
112
115
|
basicType = expectType;
|
|
@@ -123,8 +126,13 @@ class SqlValuesCreator {
|
|
|
123
126
|
return SqlValuesCreator.string(value);
|
|
124
127
|
case "boolean":
|
|
125
128
|
return value.toString();
|
|
126
|
-
case "object":
|
|
127
|
-
|
|
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
|
+
}
|
|
128
136
|
case "undefined":
|
|
129
137
|
return "DEFAULT";
|
|
130
138
|
default:
|
|
@@ -133,16 +141,13 @@ class SqlValuesCreator {
|
|
|
133
141
|
throw new Error("不支持转换 " + type + " 类型");
|
|
134
142
|
}
|
|
135
143
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
return "NULL";
|
|
139
|
-
if (value instanceof SqlRaw)
|
|
140
|
-
return value.toString();
|
|
144
|
+
/** 获取值对应的 SqlValueEncoder */
|
|
145
|
+
getObjectType(value) {
|
|
141
146
|
for (const Class of this.map.keys()) {
|
|
142
147
|
if (value instanceof Class)
|
|
143
|
-
return this.map.get(Class)
|
|
148
|
+
return this.map.get(Class);
|
|
144
149
|
}
|
|
145
|
-
return this.defaultObject
|
|
150
|
+
return this.defaultObject;
|
|
146
151
|
}
|
|
147
152
|
defaultObject(value) {
|
|
148
153
|
return SqlValuesCreator.string(JSON.stringify(value));
|
|
@@ -235,13 +240,20 @@ const pgSqlTransformer = new Map([
|
|
|
235
240
|
[
|
|
236
241
|
Array,
|
|
237
242
|
function encodePgArray(value) {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
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(",") + "]";
|
|
245
257
|
},
|
|
246
258
|
],
|
|
247
259
|
[Date, (value) => SqlValuesCreator.string(value.toISOString())],
|
|
@@ -13,18 +13,20 @@ 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);
|
|
18
|
+
export type ManualType = "bigint" | "number" | "string" | "boolean" | "object" | (new (...args: any[]) => any);
|
|
19
|
+
/** @public */
|
|
20
|
+
export interface SqlValueFn {
|
|
21
|
+
/** 将 JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT */
|
|
22
|
+
(value: any, expectType?: ManualType): string;
|
|
23
|
+
}
|
|
19
24
|
/**
|
|
20
25
|
* SQL value 生成器
|
|
21
26
|
* @public
|
|
22
27
|
*/
|
|
23
28
|
export declare class SqlValuesCreator {
|
|
24
|
-
static create(map?: JsObjectMapSql): SqlValuesCreator &
|
|
25
|
-
/** 将 JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT */
|
|
26
|
-
(value: any, expectType?: ManualType): string;
|
|
27
|
-
};
|
|
29
|
+
static create(map?: JsObjectMapSql): SqlValuesCreator & SqlValueFn;
|
|
28
30
|
/**
|
|
29
31
|
* 将字符串转为 SQL 的字符串值的形式(单引号会被转义)。
|
|
30
32
|
* @example 输入 a'b'c , 返回 a''b''c.
|
|
@@ -38,8 +40,9 @@ export declare class SqlValuesCreator {
|
|
|
38
40
|
setTransformer<T>(type: new (...args: any[]) => T, transformer?: SqlValueEncoder): void;
|
|
39
41
|
private readonly map;
|
|
40
42
|
/** 将 JS 对象转为 SQL 的字符值的形式 。 undefined 将被转换为 DEFAULT */
|
|
41
|
-
toSqlStr(value: any, expectType?:
|
|
42
|
-
|
|
43
|
+
toSqlStr(value: any, expectType?: ManualType): string;
|
|
44
|
+
/** 获取值对应的 SqlValueEncoder */
|
|
45
|
+
getObjectType(value: object): SqlValueEncoder;
|
|
43
46
|
protected defaultObject(value: object): string;
|
|
44
47
|
/**
|
|
45
48
|
* 将对象列表转为 SQL 的 VALUES
|