@asla/yoursql 0.9.3 → 0.10.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.
Files changed (48) hide show
  1. package/README.md +11 -11
  2. package/dist/sql_gen/{select/chain_base.d.ts → SqlStatement.d.ts} +13 -3
  3. package/dist/sql_gen/{select/chain_base.js → SqlStatement.js} +5 -2
  4. package/dist/sql_gen/_statement.d.ts +29 -0
  5. package/dist/sql_gen/_statement.js +127 -0
  6. package/dist/sql_gen/mod.d.ts +3 -8
  7. package/dist/sql_gen/mod.js +7 -6
  8. package/dist/sql_gen/sql_value/ValueSqlTemplate.d.ts +13 -0
  9. package/dist/sql_gen/sql_value/ValueSqlTemplate.js +30 -0
  10. package/dist/sql_gen/sql_value/_utils.d.ts +11 -0
  11. package/dist/sql_gen/sql_value/_utils.js +37 -0
  12. package/dist/sql_gen/sql_value/sql_value.d.ts +23 -22
  13. package/dist/sql_gen/sql_value/sql_value.js +25 -48
  14. package/dist/sql_gen/sql_value/type.d.ts +19 -0
  15. package/dist/sql_gen/statement/_modify.d.ts +6 -0
  16. package/dist/sql_gen/statement/delete.d.ts +15 -0
  17. package/dist/sql_gen/statement/delete.js +46 -0
  18. package/dist/sql_gen/statement/delete_chain.d.ts +26 -0
  19. package/dist/sql_gen/statement/insert.d.ts +6 -0
  20. package/dist/sql_gen/statement/insert.js +107 -0
  21. package/dist/sql_gen/statement/insert_chain.d.ts +28 -0
  22. package/dist/sql_gen/statement/mod.d.ts +10 -0
  23. package/dist/sql_gen/statement/select.d.ts +38 -0
  24. package/dist/sql_gen/statement/select.js +154 -0
  25. package/dist/sql_gen/statement/select_chain.d.ts +102 -0
  26. package/dist/sql_gen/statement/update.d.ts +17 -0
  27. package/dist/sql_gen/statement/update.js +88 -0
  28. package/dist/sql_gen/statement/update_chain.d.ts +36 -0
  29. package/dist/sql_gen/util.d.ts +20 -53
  30. package/dist/sql_gen/util.js +1 -118
  31. package/dist/sql_gen/your_table/table.d.ts +4 -5
  32. package/dist/sql_gen/your_table/table.js +4 -4
  33. package/package.json +1 -1
  34. package/dist/sql_gen/select/DbTable.d.ts +0 -58
  35. package/dist/sql_gen/select/DbTable.js +0 -78
  36. package/dist/sql_gen/select/TableQuery.d.ts +0 -28
  37. package/dist/sql_gen/select/TableQuery.js +0 -73
  38. package/dist/sql_gen/select/_statement.d.ts +0 -9
  39. package/dist/sql_gen/select/_statement.js +0 -63
  40. package/dist/sql_gen/select/chain_modify.d.ts +0 -68
  41. package/dist/sql_gen/select/chain_select.d.ts +0 -28
  42. package/dist/sql_gen/select/query_chain_insert.d.ts +0 -24
  43. package/dist/sql_gen/select/query_chain_insert.js +0 -85
  44. package/dist/sql_gen/select/query_chain_select.d.ts +0 -49
  45. package/dist/sql_gen/select/query_chain_select.js +0 -105
  46. package/dist/sql_gen/select/type.d.ts +0 -40
  47. /package/dist/sql_gen/{select → statement}/cte.d.ts +0 -0
  48. /package/dist/sql_gen/{select → statement}/cte.js +0 -0
package/README.md CHANGED
@@ -70,19 +70,19 @@ import { v } from "@asla/yoursql";
70
70
  v.toValues([1, "abc", null, undefined, { key: "value" }]); // `1,'abc',NULL,DEFAULT,'{"key":"value"}'`
71
71
  ```
72
72
 
73
- #### v.objectToValues()
73
+ #### v.objectToValue()
74
74
 
75
75
  转换对象为 values 的单个值
76
76
 
77
77
  ```ts
78
78
  import { v } from "@asla/yoursql";
79
79
  const obj = { a: "a1", b: "b1", c: undefined, d: "d1" };
80
- v.objectToValues(obj); // "'a1','b1',DEFAULT,'d1'"
81
- v.objectToValues(obj, ["b", "a"]); // "'b1','a1'"
82
- v.objectToValues(obj, [{ a: "TEXT", b: {} }]); // 'a1'::TEXT,'b1'"
80
+ v.objectToValue(obj); // "'a1','b1',DEFAULT,'d1'"
81
+ v.objectToValue(obj, ["b", "a"]); // "'b1','a1'"
82
+ v.objectToValue(obj, [{ a: "TEXT", b: {} }]); // 'a1'::TEXT,'b1'"
83
83
  ```
84
84
 
85
- #### v.objectListToValuesList()
85
+ #### v.objectListToValues()
86
86
 
87
87
  转换对象数组为 values
88
88
 
@@ -92,10 +92,10 @@ import { v } from "@asla/yoursql";
92
92
  const values = [{ a: 1, b: 2 }, { c: 3 }];
93
93
 
94
94
  // 这将自动选择数组中所有键的并集
95
- v.objectListToValuesList(values); // "(1,2,null),(null,null,3)"
95
+ v.objectListToValues(values); // "(1,2,null),(null,null,3)"
96
96
 
97
97
  // 或者你可以指定选择键并指定顺序
98
- const valueStr = v.objectListToValuesList(values, ["c", "b"]); // "(null,2),(3,3)"
98
+ const valueStr = v.objectListToValues(values, ["c", "b"]); // "(null,2),(3,3)"
99
99
 
100
100
  const sql = `INSERT INTO user(name, role) VALUES ${valueStr}`;
101
101
  ```
@@ -121,12 +121,12 @@ v.createValues("customName", objectList, {
121
121
  ### 生成 SQL 语句
122
122
 
123
123
  ```ts
124
- import { Selection, v } from "@asla/yoursql";
124
+ import { select, v } from "@asla/yoursql";
125
125
 
126
126
  const searchName = "Bob";
127
- const s = Selection.from("user", "u")
128
- .innerJoin("role", "r", "u.id=r.user_id")
129
- .select({ uid: "u.id", rid: "r.id", example: "u.id||r.id" }) // SELECT u.id AS uid, r.id AS rid u.id||u.id AS example
127
+ const s = select({ uid: "u.id", rid: "r.id", example: "u.id||r.id" })
128
+ .from("user AS u")
129
+ .innerJoin("role", { as: "r", on: "u.id=r.user_id" })
130
130
  .where(`u.name LIKE %${v(searchName)}%`)
131
131
  .toString();
132
132
  ```
@@ -21,7 +21,7 @@ export interface SqlSelectable {
21
21
  * 如果是 table 则是 table name
22
22
  * 如果是 选择语句,则是 (xxx)
23
23
  */
24
- toSelect(): string;
24
+ toSelect(asName?: string): string;
25
25
  }
26
26
  /** @public */
27
27
  export declare abstract class SqlStatementDataset<T> extends SqlStatement implements SqlSelectable {
@@ -30,7 +30,7 @@ export declare abstract class SqlStatementDataset<T> extends SqlStatement implem
30
30
  * 如果是 table 则是 table name
31
31
  * 如果是 选择语句,则是 (xxx)
32
32
  */
33
- toSelect(): string;
33
+ toSelect(asName?: string): string;
34
34
  }
35
35
  /** @public */
36
36
  export declare class SqlTextStatementDataset<T> extends SqlStatementDataset<T> {
@@ -43,4 +43,14 @@ export declare class SqlTextStatementDataset<T> extends SqlStatementDataset<T> {
43
43
  * @public
44
44
  */
45
45
  export type InferQueryResult<T> = T extends SqlStatementDataset<infer P> ? P : never;
46
- //# sourceMappingURL=chain_base.d.ts.map
46
+ /** @public */
47
+ export interface SqlTemplate {
48
+ readonly templates: readonly string[];
49
+ readonly args: readonly unknown[];
50
+ toTextTemplate(): {
51
+ text: string;
52
+ args: string[];
53
+ };
54
+ genSql(): string;
55
+ }
56
+ //# sourceMappingURL=SqlStatement.d.ts.map
@@ -12,8 +12,11 @@ class SqlStatementDataset extends SqlStatement {
12
12
  * 如果是 table 则是 table name
13
13
  * 如果是 选择语句,则是 (xxx)
14
14
  */
15
- toSelect() {
16
- return "(" + this.genSql() + ")";
15
+ toSelect(asName) {
16
+ let result = "(" + this.genSql() + ")";
17
+ if (asName)
18
+ result += " AS " + asName;
19
+ return result;
17
20
  }
18
21
  }
19
22
  /** @public */
@@ -0,0 +1,29 @@
1
+ import { ConditionParam, Constructable, SelectParam } from "./util.ts";
2
+ import { SqlSelectable } from "./SqlStatement.ts";
3
+ /**
4
+ * 获取对象数组中的 key 的集合
5
+ * @param keepUndefinedKey - 是否保留值为 undefined 的 key
6
+ */
7
+ export declare function getObjectListKeys(objectList: any[], keepUndefinedKey?: boolean): Set<string>;
8
+ /**
9
+ * 生成条件语句
10
+ */
11
+ export declare function condition(conditions?: Constructable<ConditionParam | void>, type?: "AND" | "OR"): string;
12
+ /**
13
+ * 生成 WHERE 语句
14
+ * @public
15
+ * @example
16
+ * ```ts
17
+ * where(['a=1','b=2']) // "\nWHERE a=1 AND b=2"
18
+ * where(['a=1','b=2'],"OR") // "\nWHERE a=1 OR b=2"
19
+ * where("a=1 OR b=2") // "\nWHERE a=1 OR b=2"
20
+ * where(()=>"a=1 OR b=2") // "\nWHERE a=1 AND b=2"
21
+ * where([]) // ""
22
+ * where(undefined) // ""
23
+ * ```
24
+ */
25
+ export declare function whereToString(conditions?: Constructable<ConditionParam | void>, type?: "AND" | "OR"): string;
26
+ export declare function createUpdateSetFromObject(set: Record<string, string | undefined>, prefix?: string): string;
27
+ export declare function selectableToString(selectable: Constructable<SqlSelectable | string>, as?: string): string;
28
+ export declare function returningToString(returns: Constructable<SelectParam | "*">): string;
29
+ //# sourceMappingURL=_statement.d.ts.map
@@ -0,0 +1,127 @@
1
+ import { selectColumns } from './util.js';
2
+
3
+ /**
4
+ * 获取对象数组中的 key 的集合
5
+ * @param keepUndefinedKey - 是否保留值为 undefined 的 key
6
+ */
7
+ function getObjectListKeys(objectList, keepUndefinedKey) {
8
+ let keys = new Set();
9
+ for (let i = 0; i < objectList.length; i++) {
10
+ let obj = objectList[i];
11
+ let hasKeys = Object.keys(obj);
12
+ let k;
13
+ for (let j = 0; j < hasKeys.length; j++) {
14
+ k = hasKeys[j];
15
+ if (typeof k !== "string")
16
+ continue;
17
+ if (!keepUndefinedKey && obj[k] === undefined)
18
+ continue;
19
+ keys.add(k);
20
+ }
21
+ }
22
+ return keys;
23
+ }
24
+ function condition(conditions, type = "AND") {
25
+ if (typeof conditions === "function")
26
+ conditions = conditions();
27
+ if (!conditions)
28
+ return;
29
+ if (typeof conditions === "string")
30
+ return conditions;
31
+ else {
32
+ if (conditions.length) {
33
+ let sql = "";
34
+ type = " " + type + " ";
35
+ sql += conditions[0];
36
+ for (let i = 1; i < conditions.length; i++)
37
+ sql += type + conditions[i];
38
+ return sql;
39
+ }
40
+ return;
41
+ }
42
+ }
43
+ /**
44
+ * 生成 WHERE 语句
45
+ * @public
46
+ * @example
47
+ * ```ts
48
+ * where(['a=1','b=2']) // "\nWHERE a=1 AND b=2"
49
+ * where(['a=1','b=2'],"OR") // "\nWHERE a=1 OR b=2"
50
+ * where("a=1 OR b=2") // "\nWHERE a=1 OR b=2"
51
+ * where(()=>"a=1 OR b=2") // "\nWHERE a=1 AND b=2"
52
+ * where([]) // ""
53
+ * where(undefined) // ""
54
+ * ```
55
+ */
56
+ function whereToString(conditions, type) {
57
+ const sql = condition(conditions, type);
58
+ if (sql)
59
+ return "\nWHERE " + sql;
60
+ return "";
61
+ }
62
+ function createUpdateSetFromObject(set, prefix) {
63
+ const updateKey = Object.keys(set);
64
+ let i = 0;
65
+ let key;
66
+ let sqlKey;
67
+ let value;
68
+ let sql;
69
+ for (; i < updateKey.length; i++) {
70
+ key = updateKey[i];
71
+ sqlKey = key;
72
+ value = set[key];
73
+ if (value === undefined)
74
+ continue;
75
+ if (typeof value === "string") {
76
+ if (value) {
77
+ sql = "SET\n" + sqlKey + "= " + value;
78
+ break;
79
+ }
80
+ }
81
+ else
82
+ throw new TypeError(`key ${key} 类型错误(${typeof value})`);
83
+ }
84
+ if (sql) {
85
+ i++;
86
+ for (; i < updateKey.length; i++) {
87
+ key = updateKey[i];
88
+ sqlKey = key;
89
+ value = set[key];
90
+ if (value === undefined)
91
+ continue;
92
+ if (typeof value === "string") {
93
+ if (value)
94
+ sql += "," + sqlKey + "= " + value;
95
+ }
96
+ else
97
+ throw new TypeError(`key ${key} 类型错误(${typeof value})`);
98
+ }
99
+ return sql;
100
+ }
101
+ else
102
+ throw new Error("值不能为空");
103
+ }
104
+ function selectableToString(selectable, as) {
105
+ if (typeof selectable === "function")
106
+ selectable = selectable();
107
+ let sql = typeof selectable === "string" ? selectable : selectable.toSelect();
108
+ if (!sql)
109
+ throw new Error("selectable can not be empty");
110
+ if (as)
111
+ sql += " AS " + as;
112
+ return sql;
113
+ }
114
+ function returningToString(returns) {
115
+ if (typeof returns === "function")
116
+ returns = returns();
117
+ let columnsStr;
118
+ if (returns === "*") {
119
+ columnsStr = "*";
120
+ }
121
+ else {
122
+ columnsStr = selectColumns(returns);
123
+ }
124
+ return "\nRETURNING " + columnsStr;
125
+ }
126
+
127
+ export { condition, createUpdateSetFromObject, getObjectListKeys, returningToString, selectableToString, whereToString };
@@ -1,13 +1,8 @@
1
1
  export * from "./sql_value/db_type.ts";
2
2
  export * from "./sql_value/sql_value.ts";
3
- export * from "./select/type.ts";
4
- export * from "./select/DbTable.ts";
5
- export * from "./select/query_chain_select.ts";
6
- export * from "./select/chain_base.ts";
7
- export * from "./select/chain_modify.ts";
8
- export * from "./select/chain_select.ts";
9
- export * from "./select/TableQuery.ts";
10
- export * from "./select/cte.ts";
3
+ export * from "./sql_value/type.ts";
4
+ export * from "./SqlStatement.ts";
5
+ export * from "./statement/mod.ts";
11
6
  export * from "./util.ts";
12
7
  export * from "./your_table/mod.ts";
13
8
  import { SqlValueFn } from "./sql_value/sql_value.ts";
@@ -1,11 +1,12 @@
1
1
  import { pgSqlTransformer } from './sql_value/db_type.js';
2
2
  import { SqlValuesCreator } from './sql_value/sql_value.js';
3
- export { DbTable } from './select/DbTable.js';
4
- export { Selection, SqlSelectChain } from './select/query_chain_select.js';
5
- export { SqlStatement, SqlStatementDataset, SqlTextStatementDataset } from './select/chain_base.js';
6
- export { DbTableQuery } from './select/TableQuery.js';
7
- export { withAs, withRecursiveAs } from './select/cte.js';
8
- export { getObjectListKeys, having, orderBy, selectColumns, where } from './util.js';
3
+ export { SqlStatement, SqlStatementDataset, SqlTextStatementDataset } from './SqlStatement.js';
4
+ export { orderBy, select } from './statement/select.js';
5
+ export { insertInto } from './statement/insert.js';
6
+ export { update } from './statement/update.js';
7
+ export { deleteFrom } from './statement/delete.js';
8
+ export { withAs, withRecursiveAs } from './statement/cte.js';
9
+ export { selectColumns } from './util.js';
9
10
  export { TypeChecker } from './your_table/checker.js';
10
11
  export { ColumnMeta, CustomDbType, YourTypeMap } from './your_table/infer_db_type.js';
11
12
  export { YourTable } from './your_table/table.js';
@@ -0,0 +1,13 @@
1
+ import { SqlTemplate } from "../SqlStatement.ts";
2
+ export declare class ValueSqlTemplate implements SqlTemplate {
3
+ private v;
4
+ readonly templates: readonly string[];
5
+ readonly args: readonly unknown[];
6
+ constructor(v: (value: unknown) => string, templates: readonly string[], values: readonly unknown[]);
7
+ toTextTemplate(): {
8
+ text: string;
9
+ args: string[];
10
+ };
11
+ genSql(): string;
12
+ }
13
+ //# sourceMappingURL=ValueSqlTemplate.d.ts.map
@@ -0,0 +1,30 @@
1
+ class ValueSqlTemplate {
2
+ v;
3
+ templates;
4
+ args;
5
+ constructor(v, templates, values) {
6
+ this.v = v;
7
+ this.templates = templates;
8
+ this.args = values;
9
+ }
10
+ toTextTemplate() {
11
+ const { templates, args } = this;
12
+ let text = templates[0];
13
+ for (let i = 1; i < templates.length; i++) {
14
+ text += "$" + i;
15
+ text += templates[i];
16
+ }
17
+ const values = args.map((value) => this.v(value));
18
+ return { text, args: values };
19
+ }
20
+ genSql() {
21
+ const { templates, args } = this;
22
+ let sql = this.templates[0];
23
+ for (let i = 1; i < templates.length; i++) {
24
+ sql += this.v(args[i - 1]) + templates[i];
25
+ }
26
+ return sql;
27
+ }
28
+ }
29
+
30
+ export { ValueSqlTemplate };
@@ -0,0 +1,11 @@
1
+ import { SqlStatementDataset } from "../SqlStatement.ts";
2
+ import { TableType } from "../util.ts";
3
+ import { ColumnToValueConfig } from "./type.ts";
4
+ export declare class YourValuesAs<T extends TableType> extends SqlStatementDataset<T> {
5
+ #private;
6
+ constructor(columns: readonly string[], asName: string, valuesStr: string);
7
+ toSelect(): string;
8
+ genSql(): string;
9
+ }
10
+ export declare function initColumnAssert(keys: readonly string[], keys_types: Record<string, string | undefined | ColumnToValueConfig>): any[];
11
+ //# sourceMappingURL=_utils.d.ts.map
@@ -0,0 +1,37 @@
1
+ import { SqlStatementDataset } from '../SqlStatement.js';
2
+
3
+ class YourValuesAs extends SqlStatementDataset {
4
+ constructor(columns, asName, valuesStr) {
5
+ super();
6
+ this.#asName = asName;
7
+ this.#valuesStr = valuesStr;
8
+ this.#sql = `(VALUES\n${this.#valuesStr})\nAS ${this.#asName}(${columns.join(",")})`;
9
+ }
10
+ #asName;
11
+ #valuesStr;
12
+ #sql;
13
+ toSelect() {
14
+ return this.#sql;
15
+ }
16
+ genSql() {
17
+ return this.#sql;
18
+ }
19
+ }
20
+ function initColumnAssert(keys, keys_types) {
21
+ let key;
22
+ let value;
23
+ let type = new Array(keys.length);
24
+ for (let i = 0; i < keys.length; i++) {
25
+ key = keys[i];
26
+ value = keys_types[key];
27
+ if (typeof value === "string") {
28
+ type[i] = { sqlType: value };
29
+ }
30
+ else {
31
+ type[i] = value;
32
+ }
33
+ }
34
+ return type;
35
+ }
36
+
37
+ export { YourValuesAs, initColumnAssert };
@@ -1,10 +1,9 @@
1
- import { SqlStatementDataset } from "../select/chain_base.ts";
1
+ import { SqlStatementDataset, SqlTemplate } from "../SqlStatement.ts";
2
+ import { AssertJsType, ObjectToValueKeys, SqlValueData } from "./type.ts";
2
3
  /** @public js 对象到编码函数的映射*/
3
4
  export type JsObjectMapSql = Map<new (...args: any[]) => any, SqlValueEncoder>;
4
5
  /** @public 将 js 值转为 SQl 字符串的函数*/
5
6
  export type SqlValueEncoder<T = any> = (this: SqlValuesCreator, value: T) => string;
6
- /** @public 断言类型 */
7
- export type AssertJsType = "bigint" | "number" | "string" | "boolean" | "object" | (new (...args: any[]) => any);
8
7
  /** @public */
9
8
  export type SqlValueFn = SqlValuesCreator & {
10
9
  /**
@@ -41,31 +40,38 @@ export declare class SqlValuesCreator {
41
40
  * ```
42
41
  */
43
42
  toSqlStr(value: any, assertJsType?: AssertJsType): string;
44
- /**
45
- * @deprecated 已废弃
46
- * 获取值对应的 SqlValueEncoder
47
- */
48
- getObjectType(value: object): SqlValueEncoder;
43
+ /** @alpha */
44
+ gen(split: TemplateStringsArray, ...values: any[]): SqlTemplate;
49
45
  /** 获取值对应已定义的类 */
50
46
  getClassType(value: object): undefined | (new (...args: unknown[]) => unknown);
51
47
  protected defaultObject(value: object): string;
48
+ /** @deprecated 改用 objectListToValues */
49
+ objectListToValuesList<T extends object>(objectList: T[], keys?: ObjectToValueKeys<T>, keepUndefinedKey?: boolean): string;
52
50
  /**
53
- * 将对象列表转为 SQL 的 VALUES
54
- * @example 返回示例: " (...),(...) "
51
+ * 将对象列表转为 SQL 的 VALUES。与 objectToValues类似。如果设定了 keys 的类型,只会转换第一行的数据。
52
+ * @example 返回的文本示例: " (...),(...) "
55
53
  * @param keys - 选择的键。如果指定了 keys, 值为 undefined 的属性将自动填充为 null; 如果未指定 keys,将选择 objectList 所有不是 undefined 项的键的并集
56
54
  * @param keepUndefinedKey - 是否保留 undefined 的键。默认值为 false,如果为 true , 数组的某一个字段均为 undefined时,将忽略字段,
57
55
  */
58
- objectListToValuesList<T extends object>(objectList: T[], keys?: readonly (keyof T)[] | {
59
- [key in keyof T]?: string | undefined | ColumnToValueConfig;
60
- }, keepUndefinedKey?: boolean): string;
56
+ objectListToValues<T extends object>(objectList: T[], keys?: ObjectToValueKeys<T>, keepUndefinedKey?: boolean): SqlValueData;
57
+ /**
58
+ * @deprecated 请使用 objectToValue 代替
59
+ */
60
+ objectToValues<T extends object>(object: T, keys?: ObjectToValueKeys<T>): string;
61
61
  /**
62
62
  * 将对象转为 SQL 的 value
63
- * @example 返回示例: " 'abc', '6', 'now()' "
63
+ * @example
64
+ * ```ts
65
+ * v.objectToValues({ a: 1, b: "2", c: null, d: undefined }).text // " '1', '2', NULL, DEFAULT "
66
+ * v.objectToValues({ a: 1, b: "2", c: null, d: undefined }, ["a", "b"]).text // " '1', '2' "
67
+ * v.objectToValues(
68
+ * { a: 1, b: "2", c: null, d: undefined },
69
+ * { a: "INT", b: "TEXT", c: "JSONB", d: "TEXT" }
70
+ * ).text // " '1'::INT, '2'::TEXT, NULL::JSONB, DEFAULT::TEXT "
71
+ * ```
64
72
  * @param keys - 如果指定了key, object undefined 的属性值将填充为 null,如果不指定,将自获取 object 所有非 undefined 的属性的key
65
73
  */
66
- objectToValues<T extends object>(object: T, keys?: readonly (keyof T)[] | {
67
- [key in keyof T]?: string | undefined | ColumnToValueConfig;
68
- }): string;
74
+ objectToValue<T extends object>(object: T, keys?: ObjectToValueKeys<T>): SqlValueData;
69
75
  private _internalObjectToValues;
70
76
  /**
71
77
  * 将数组列表转为 SQL 的一个 value
@@ -93,9 +99,4 @@ export declare class SqlValuesCreator {
93
99
  assertJsType?: AssertJsType;
94
100
  }>): SqlStatementDataset<T>;
95
101
  }
96
- /** @public */
97
- export type ColumnToValueConfig = {
98
- sqlType?: string;
99
- assertJsType?: AssertJsType;
100
- };
101
102
  //# sourceMappingURL=sql_value.d.ts.map
@@ -1,5 +1,6 @@
1
- import { getObjectListKeys } from '../util.js';
2
- import { SqlStatementDataset } from '../select/chain_base.js';
1
+ import { getObjectListKeys } from '../_statement.js';
2
+ import { ValueSqlTemplate } from './ValueSqlTemplate.js';
3
+ import { initColumnAssert, YourValuesAs } from './_utils.js';
3
4
 
4
5
  /**
5
6
  * SQL value 生成器
@@ -95,16 +96,14 @@ class SqlValuesCreator {
95
96
  throw new Error("不支持 " + type + " 类型");
96
97
  }
97
98
  }
98
- /**
99
- * @deprecated 已废弃
100
- * 获取值对应的 SqlValueEncoder
101
- */
102
- getObjectType(value) {
103
- for (const Class of this._map.keys()) {
104
- if (value instanceof Class)
105
- return this._map.get(Class);
99
+ /** @alpha */
100
+ gen(split, ...values) {
101
+ let sql = split[0];
102
+ for (let i = 0; i < values.length; i++) {
103
+ sql += this.toSqlStr(values[i]);
104
+ sql += split[i + 1];
106
105
  }
107
- return this.defaultObject;
106
+ return new ValueSqlTemplate(this.toSqlStr.bind(this), split, values);
108
107
  }
109
108
  /** 获取值对应已定义的类 */
110
109
  getClassType(value) {
@@ -116,7 +115,11 @@ class SqlValuesCreator {
116
115
  defaultObject(value) {
117
116
  return SqlValuesCreator.string(JSON.stringify(value));
118
117
  }
119
- objectListToValuesList(objectList, keys_types, keepUndefinedKey) {
118
+ /** @deprecated 改用 objectListToValues */
119
+ objectListToValuesList(objectList, keys, keepUndefinedKey) {
120
+ return this.objectListToValues(objectList, keys, keepUndefinedKey).text;
121
+ }
122
+ objectListToValues(objectList, keys_types, keepUndefinedKey) {
120
123
  if (objectList.length <= 0)
121
124
  throw new Error("objectList 不能是空数组");
122
125
  let keys;
@@ -154,9 +157,15 @@ class SqlValuesCreator {
154
157
  let message = error instanceof Error ? error.message : String(error);
155
158
  throw new Error("第 " + i + " 项,字段 '" + keys[j] + "' 异常," + message);
156
159
  }
157
- return str;
160
+ return { columns: keys, text: str };
158
161
  }
159
- objectToValues(object, keys_types) {
162
+ /**
163
+ * @deprecated 请使用 objectToValue 代替
164
+ */
165
+ objectToValues(object, keys) {
166
+ return this.objectToValue(object, keys).text;
167
+ }
168
+ objectToValue(object, keys_types) {
160
169
  let type;
161
170
  let keys;
162
171
  if (keys_types instanceof Array) {
@@ -171,7 +180,8 @@ class SqlValuesCreator {
171
180
  keys = Object.keys(object);
172
181
  type = [];
173
182
  }
174
- return this._internalObjectToValues(object, keys, type);
183
+ const text = this._internalObjectToValues(object, keys, type);
184
+ return { columns: keys, text };
175
185
  }
176
186
  _internalObjectToValues(object, keys, type) {
177
187
  const values = [];
@@ -263,39 +273,6 @@ class SqlValuesCreator {
263
273
  return new YourValuesAs(insertKeys, asName, valuesStr.join(",\n"));
264
274
  }
265
275
  }
266
- class YourValuesAs extends SqlStatementDataset {
267
- constructor(columns, asName, valuesStr) {
268
- super();
269
- this.#asName = asName;
270
- this.#valuesStr = valuesStr;
271
- this.#sql = `(VALUES\n${this.#valuesStr})\nAS ${this.#asName}(${columns.join(",")})`;
272
- }
273
- #asName;
274
- #valuesStr;
275
- #sql;
276
- toSelect() {
277
- return this.#sql;
278
- }
279
- genSql() {
280
- return this.#sql;
281
- }
282
- }
283
- function initColumnAssert(keys, keys_types) {
284
- let key;
285
- let value;
286
- let type = new Array(keys.length);
287
- for (let i = 0; i < keys.length; i++) {
288
- key = keys[i];
289
- value = keys_types[key];
290
- if (typeof value === "string") {
291
- type[i] = { sqlType: value };
292
- }
293
- else {
294
- type[i] = value;
295
- }
296
- }
297
- return type;
298
- }
299
276
  class AssertError extends TypeError {
300
277
  constructor(assertType, actual) {
301
278
  super(`Assert ${assertType} type, Actual ${actual} type`);
@@ -0,0 +1,19 @@
1
+ /** @public 断言类型 */
2
+ export type AssertJsType = "bigint" | "number" | "string" | "boolean" | "object" | (new (...args: any[]) => any);
3
+ /** @public */
4
+ export type SqlValueData = {
5
+ columns: readonly string[];
6
+ text: string;
7
+ };
8
+ /** @public */
9
+ export type ColumnToValueConfig = {
10
+ /** 设置显式 SQL 类型,设置后会显示转换 SQL 值 */
11
+ sqlType?: string;
12
+ /** 设置 JS 转换器类型,引导转换器如何将 JS 值转换为 SQL 值 */
13
+ assertJsType?: AssertJsType;
14
+ };
15
+ /** @public */
16
+ export type ObjectToValueKeys<T extends {}> = readonly (keyof T)[] | {
17
+ [key in keyof T]?: string | undefined | ColumnToValueConfig;
18
+ };
19
+ //# sourceMappingURL=type.d.ts.map
@@ -0,0 +1,6 @@
1
+ import { Constructable, SelectParam, TableType } from "../util.ts";
2
+ import { SqlStatementDataset, SqlStatement } from "../SqlStatement.ts";
3
+ export interface ChainModifyReturning extends SqlStatement {
4
+ returning<R extends TableType>(columns: Constructable<SelectParam>): SqlStatementDataset<R>;
5
+ }
6
+ //# sourceMappingURL=_modify.d.ts.map
@@ -0,0 +1,15 @@
1
+ import { ChainDelete } from "./delete_chain.ts";
2
+ /** @public */
3
+ export interface DeleteOption {
4
+ as?: string;
5
+ }
6
+ /**
7
+ * @public
8
+ * @example
9
+ * ```ts
10
+ * deleteFrom("table1").where("id = 1") // DELETE FROM table1 WHERE id = 1
11
+ * deleteFrom("table1 AS t").where("t.id = 1") // DELETE FROM table1 AS t WHERE t.id = 1
12
+ * ```
13
+ */
14
+ export declare function deleteFrom(table: string, option?: DeleteOption): ChainDelete;
15
+ //# sourceMappingURL=delete.d.ts.map