@asla/yoursql 0.9.2 → 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.
- package/README.md +11 -11
- package/dist/sql_gen/{select/chain_base.d.ts → SqlStatement.d.ts} +13 -3
- package/dist/sql_gen/{select/chain_base.js → SqlStatement.js} +5 -2
- package/dist/sql_gen/_statement.d.ts +29 -0
- package/dist/sql_gen/_statement.js +127 -0
- package/dist/sql_gen/mod.d.ts +3 -7
- package/dist/sql_gen/mod.js +7 -5
- package/dist/sql_gen/sql_value/ValueSqlTemplate.d.ts +13 -0
- package/dist/sql_gen/sql_value/ValueSqlTemplate.js +30 -0
- package/dist/sql_gen/sql_value/_utils.d.ts +11 -0
- package/dist/sql_gen/sql_value/_utils.js +37 -0
- package/dist/sql_gen/sql_value/sql_value.d.ts +23 -22
- package/dist/sql_gen/sql_value/sql_value.js +25 -48
- package/dist/sql_gen/sql_value/type.d.ts +19 -0
- package/dist/sql_gen/statement/_modify.d.ts +6 -0
- package/dist/sql_gen/statement/cte.d.ts +44 -0
- package/dist/sql_gen/statement/cte.js +38 -0
- package/dist/sql_gen/statement/delete.d.ts +15 -0
- package/dist/sql_gen/statement/delete.js +46 -0
- package/dist/sql_gen/statement/delete_chain.d.ts +26 -0
- package/dist/sql_gen/statement/insert.d.ts +6 -0
- package/dist/sql_gen/statement/insert.js +107 -0
- package/dist/sql_gen/statement/insert_chain.d.ts +28 -0
- package/dist/sql_gen/statement/mod.d.ts +10 -0
- package/dist/sql_gen/statement/select.d.ts +38 -0
- package/dist/sql_gen/statement/select.js +154 -0
- package/dist/sql_gen/statement/select_chain.d.ts +102 -0
- package/dist/sql_gen/statement/update.d.ts +17 -0
- package/dist/sql_gen/statement/update.js +88 -0
- package/dist/sql_gen/statement/update_chain.d.ts +36 -0
- package/dist/sql_gen/util.d.ts +20 -53
- package/dist/sql_gen/util.js +1 -118
- package/dist/sql_gen/your_table/table.d.ts +4 -5
- package/dist/sql_gen/your_table/table.js +4 -4
- package/package.json +1 -1
- package/dist/sql_gen/select/DbTable.d.ts +0 -54
- package/dist/sql_gen/select/DbTable.js +0 -70
- package/dist/sql_gen/select/TableQuery.d.ts +0 -28
- package/dist/sql_gen/select/TableQuery.js +0 -68
- package/dist/sql_gen/select/_statement.d.ts +0 -9
- package/dist/sql_gen/select/_statement.js +0 -60
- package/dist/sql_gen/select/chain_modify.d.ts +0 -33
- package/dist/sql_gen/select/chain_select.d.ts +0 -28
- package/dist/sql_gen/select/query_chain_insert.d.ts +0 -22
- package/dist/sql_gen/select/query_chain_insert.js +0 -67
- package/dist/sql_gen/select/query_chain_select.d.ts +0 -49
- package/dist/sql_gen/select/query_chain_select.js +0 -105
- package/dist/sql_gen/select/type.d.ts +0 -40
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ConditionParam, Constructable, TableType } from "../util.ts";
|
|
2
|
+
import { ChainModifyReturning } from "./_modify.ts";
|
|
3
|
+
/** @public */
|
|
4
|
+
export interface ChainUpdate<T extends TableType = TableType> {
|
|
5
|
+
set(value: Constructable<{
|
|
6
|
+
[key in keyof T]?: string;
|
|
7
|
+
} | string>): ChainUpdateAfterSet;
|
|
8
|
+
}
|
|
9
|
+
/** @public */
|
|
10
|
+
export interface ChainUpdateAfterSet extends ChainUpdateAfterForm {
|
|
11
|
+
/**
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
*
|
|
15
|
+
* update({age: "3", name: "'hi'", count:"b.count"}, "a")
|
|
16
|
+
* .from("table1 AS b", "(SELECT k FROM table2) AS c")
|
|
17
|
+
* .where(...)
|
|
18
|
+
* // UPDATE table AS a
|
|
19
|
+
* // FROM table1 AS b, (SELECT k FROM table2) AS c
|
|
20
|
+
* // SET a.age=3, a.name='hi', a.count=b.count
|
|
21
|
+
* // WHERE ...
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
from(...table: (Constructable<string> | {
|
|
25
|
+
selectable: Constructable<string>;
|
|
26
|
+
as: string;
|
|
27
|
+
})[]): ChainUpdateAfterForm;
|
|
28
|
+
}
|
|
29
|
+
/** @public */
|
|
30
|
+
export interface ChainUpdateAfterForm extends ChainUpdateReturning {
|
|
31
|
+
where(where: Constructable<ConditionParam | void>): ChainUpdateReturning;
|
|
32
|
+
}
|
|
33
|
+
/** @public */
|
|
34
|
+
export interface ChainUpdateReturning extends ChainModifyReturning {
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=update_chain.d.ts.map
|
package/dist/sql_gen/util.d.ts
CHANGED
|
@@ -1,34 +1,7 @@
|
|
|
1
|
-
import { OrderValue } from "./select/type.ts";
|
|
2
|
-
/**
|
|
3
|
-
* 获取对象数组中的 key 的集合
|
|
4
|
-
* @public
|
|
5
|
-
* @param keepUndefinedKey - 是否保留值为 undefined 的 key
|
|
6
|
-
*/
|
|
7
|
-
export declare function getObjectListKeys(objectList: any[], keepUndefinedKey?: boolean): Set<string>;
|
|
8
|
-
/** @public */
|
|
9
|
-
export type Constructable<T> = T | (() => T);
|
|
10
1
|
/** @public */
|
|
11
2
|
export type ConditionParam = string | string[];
|
|
12
|
-
/**
|
|
13
|
-
|
|
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 where(conditions?: Constructable<ConditionParam | void>, type?: "AND" | "OR"): string;
|
|
26
|
-
/**
|
|
27
|
-
*
|
|
28
|
-
* 生成 HAVING 语句
|
|
29
|
-
* @public
|
|
30
|
-
*/
|
|
31
|
-
export declare function having(conditions?: Constructable<ConditionParam | void>, type?: "AND" | "OR"): string;
|
|
3
|
+
/** @public */
|
|
4
|
+
export type Constructable<T> = T | (() => T);
|
|
32
5
|
/** @public */
|
|
33
6
|
export type SelectParam = string | string[] | Record<string, string | boolean>;
|
|
34
7
|
/**
|
|
@@ -40,31 +13,25 @@ export type SelectParam = string | string[] | Record<string, string | boolean>;
|
|
|
40
13
|
* ```
|
|
41
14
|
*/
|
|
42
15
|
export declare function selectColumns(columns: Constructable<SelectParam>): string;
|
|
43
|
-
/** @public */
|
|
44
|
-
export type OrderBehavior = {
|
|
45
|
-
key: string;
|
|
46
|
-
asc: boolean;
|
|
47
|
-
nullLast?: boolean;
|
|
48
|
-
};
|
|
49
|
-
/** @public */
|
|
50
|
-
export type OrderByParam = string | (string | OrderBehavior)[] | Record<string, boolean | `${OrderValue} ${"NULLS FIRST" | "NULLS LAST"}`>;
|
|
51
16
|
/**
|
|
52
|
-
*
|
|
17
|
+
* 推断表插入类型
|
|
53
18
|
* @public
|
|
54
|
-
* @
|
|
55
|
-
*
|
|
56
|
-
* // 以下生成 "\nORDER BY age DESC NULLS FIRST,num ASC"
|
|
57
|
-
* orderBy("age DESC NULLS FIRST,num ASC");
|
|
58
|
-
* orderBy(["age DESC NULLS FIRST", "num ASC"]);
|
|
59
|
-
* orderBy([
|
|
60
|
-
* { key: "age", asc: false, nullLast: false },
|
|
61
|
-
* { key: "num", asc: true },
|
|
62
|
-
* ]);
|
|
63
|
-
* orderBy({ age: "DESC NULLS FIRST", num: true });
|
|
64
|
-
*
|
|
65
|
-
* orderBy([]) // ""
|
|
66
|
-
* orderBy({}) // ""
|
|
67
|
-
* ```
|
|
19
|
+
* @param T - 表格创建类型
|
|
20
|
+
* @param Pa - 可选列
|
|
68
21
|
*/
|
|
69
|
-
export
|
|
22
|
+
export type ToInsertType<T extends {
|
|
23
|
+
[key: string]: any;
|
|
24
|
+
}, Pa extends keyof T = never> = {
|
|
25
|
+
[key in keyof T as key extends Pa ? never : null extends T[key] ? never : key]: T[key];
|
|
26
|
+
} & {
|
|
27
|
+
[key in keyof T as null extends T[key] ? key : key extends Pa ? key : never]?: T[key];
|
|
28
|
+
};
|
|
29
|
+
/** @public */
|
|
30
|
+
export type UpdateRowValue<T extends object> = {
|
|
31
|
+
[key in keyof T]?: T[key] | String;
|
|
32
|
+
};
|
|
33
|
+
/** @public */
|
|
34
|
+
export type TableType = {
|
|
35
|
+
[key: string]: any;
|
|
36
|
+
};
|
|
70
37
|
//# sourceMappingURL=util.d.ts.map
|
package/dist/sql_gen/util.js
CHANGED
|
@@ -1,57 +1,3 @@
|
|
|
1
|
-
import { condition } from './select/_statement.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 获取对象数组中的 key 的集合
|
|
5
|
-
* @public
|
|
6
|
-
* @param keepUndefinedKey - 是否保留值为 undefined 的 key
|
|
7
|
-
*/
|
|
8
|
-
function getObjectListKeys(objectList, keepUndefinedKey) {
|
|
9
|
-
let keys = new Set();
|
|
10
|
-
for (let i = 0; i < objectList.length; i++) {
|
|
11
|
-
let obj = objectList[i];
|
|
12
|
-
let hasKeys = Object.keys(obj);
|
|
13
|
-
let k;
|
|
14
|
-
for (let j = 0; j < hasKeys.length; j++) {
|
|
15
|
-
k = hasKeys[j];
|
|
16
|
-
if (typeof k !== "string")
|
|
17
|
-
continue;
|
|
18
|
-
if (!keepUndefinedKey && obj[k] === undefined)
|
|
19
|
-
continue;
|
|
20
|
-
keys.add(k);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return keys;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* 生成 WHERE 语句
|
|
27
|
-
* @public
|
|
28
|
-
* @example
|
|
29
|
-
* ```ts
|
|
30
|
-
* where(['a=1','b=2']) // "\nWHERE a=1 AND b=2"
|
|
31
|
-
* where(['a=1','b=2'],"OR") // "\nWHERE a=1 OR b=2"
|
|
32
|
-
* where("a=1 OR b=2") // "\nWHERE a=1 OR b=2"
|
|
33
|
-
* where(()=>"a=1 OR b=2") // "\nWHERE a=1 AND b=2"
|
|
34
|
-
* where([]) // ""
|
|
35
|
-
* where(undefined) // ""
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
function where(conditions, type) {
|
|
39
|
-
const sql = condition(conditions, type);
|
|
40
|
-
if (sql)
|
|
41
|
-
return "\nWHERE " + sql;
|
|
42
|
-
return "";
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
*
|
|
46
|
-
* 生成 HAVING 语句
|
|
47
|
-
* @public
|
|
48
|
-
*/
|
|
49
|
-
function having(conditions, type) {
|
|
50
|
-
const sql = condition(conditions, type);
|
|
51
|
-
if (sql)
|
|
52
|
-
return "\nHAVING " + sql;
|
|
53
|
-
return "";
|
|
54
|
-
}
|
|
55
1
|
/**
|
|
56
2
|
* @public
|
|
57
3
|
* @example
|
|
@@ -99,68 +45,5 @@ function selectColumns(columns) {
|
|
|
99
45
|
throw new TypeError("columns 应为 string 或 object 类型");
|
|
100
46
|
}
|
|
101
47
|
}
|
|
102
|
-
/**
|
|
103
|
-
* 生成 ORDER BY 语句, d
|
|
104
|
-
* @public
|
|
105
|
-
* @example
|
|
106
|
-
* ```ts
|
|
107
|
-
* // 以下生成 "\nORDER BY age DESC NULLS FIRST,num ASC"
|
|
108
|
-
* orderBy("age DESC NULLS FIRST,num ASC");
|
|
109
|
-
* orderBy(["age DESC NULLS FIRST", "num ASC"]);
|
|
110
|
-
* orderBy([
|
|
111
|
-
* { key: "age", asc: false, nullLast: false },
|
|
112
|
-
* { key: "num", asc: true },
|
|
113
|
-
* ]);
|
|
114
|
-
* orderBy({ age: "DESC NULLS FIRST", num: true });
|
|
115
|
-
*
|
|
116
|
-
* orderBy([]) // ""
|
|
117
|
-
* orderBy({}) // ""
|
|
118
|
-
* ```
|
|
119
|
-
*/
|
|
120
|
-
function orderBy(by) {
|
|
121
|
-
if (typeof by === "function")
|
|
122
|
-
by = by();
|
|
123
|
-
let sql = "";
|
|
124
|
-
if (!by)
|
|
125
|
-
return sql;
|
|
126
|
-
if (typeof by === "string") {
|
|
127
|
-
sql += "\nORDER BY " + by;
|
|
128
|
-
}
|
|
129
|
-
else if (by instanceof Array) {
|
|
130
|
-
if (by.length) {
|
|
131
|
-
sql += "\nORDER BY " + handlerOrderValue(by[0]);
|
|
132
|
-
for (let i = 1; i < by.length; i++)
|
|
133
|
-
sql += "," + handlerOrderValue(by[i]);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
let keys = Object.keys(by);
|
|
138
|
-
if (keys.length) {
|
|
139
|
-
let key = keys[0];
|
|
140
|
-
let value = by[key];
|
|
141
|
-
sql += "\nORDER BY " + key + " " + (typeof value === "string" ? value : value ? "ASC" : "DESC");
|
|
142
|
-
for (let i = 1; i < keys.length; i++) {
|
|
143
|
-
key = keys[i];
|
|
144
|
-
value = by[key];
|
|
145
|
-
sql += "," + key + " ";
|
|
146
|
-
if (typeof value === "string")
|
|
147
|
-
sql += value;
|
|
148
|
-
else
|
|
149
|
-
sql += value ? "ASC" : "DESC";
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
return sql;
|
|
154
|
-
}
|
|
155
|
-
function handlerOrderValue(value) {
|
|
156
|
-
if (typeof value === "string")
|
|
157
|
-
return value;
|
|
158
|
-
else {
|
|
159
|
-
let str = value.key + " " + (value.asc ? "ASC" : "DESC");
|
|
160
|
-
if (value.nullLast !== undefined)
|
|
161
|
-
str += value.nullLast ? " NULLS LAST" : " NULLS FIRST";
|
|
162
|
-
return str;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
48
|
|
|
166
|
-
export {
|
|
49
|
+
export { selectColumns };
|
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import { DbTableQuery } from "../select/TableQuery.ts";
|
|
2
|
-
import type { TableType } from "../select/type.ts";
|
|
3
|
-
import type { SqlValuesCreator } from "../sql_value/sql_value.ts";
|
|
4
1
|
import type { ColumnMeta, TableDefined } from "./infer_db_type.ts";
|
|
5
2
|
import { TypeChecker } from "./checker.ts";
|
|
3
|
+
import { TableType } from "../util.ts";
|
|
6
4
|
/**
|
|
7
5
|
* 完整数据库表数据
|
|
8
6
|
* @public
|
|
9
7
|
*/
|
|
10
|
-
export declare class YourTable<T extends TableType = TableType
|
|
8
|
+
export declare class YourTable<T extends TableType = TableType> {
|
|
9
|
+
readonly name: string;
|
|
11
10
|
private define;
|
|
12
|
-
constructor(name: string, define: TableDefined
|
|
11
|
+
constructor(name: string, define: TableDefined);
|
|
13
12
|
readonly columns: readonly string[];
|
|
14
13
|
getColumnMeta(name: keyof T): ColumnMeta<unknown>;
|
|
15
14
|
createTypeChecker<T>(keys: readonly string[]): TypeChecker<T>;
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { DbTableQuery } from '../select/TableQuery.js';
|
|
2
1
|
import { TypeChecker } from './checker.js';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* 完整数据库表数据
|
|
6
5
|
* @public
|
|
7
6
|
*/
|
|
8
|
-
class YourTable
|
|
7
|
+
class YourTable {
|
|
8
|
+
name;
|
|
9
9
|
define;
|
|
10
|
-
constructor(name, define
|
|
11
|
-
|
|
10
|
+
constructor(name, define) {
|
|
11
|
+
this.name = name;
|
|
12
12
|
this.define = define;
|
|
13
13
|
this.columns = Object.keys(define);
|
|
14
14
|
}
|
package/package.json
CHANGED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { ConditionParam, Constructable } from "../util.ts";
|
|
2
|
-
import type { TableType } from "./type.ts";
|
|
3
|
-
import { Selection } from "./query_chain_select.ts";
|
|
4
|
-
import { ChainDelete, ChainInsert, ChainUpdate } from "./chain_modify.ts";
|
|
5
|
-
import { ChainSelect } from "./chain_select.ts";
|
|
6
|
-
/**
|
|
7
|
-
* 数据库表
|
|
8
|
-
* @public
|
|
9
|
-
*/
|
|
10
|
-
export declare class DbTable<T extends TableType> {
|
|
11
|
-
readonly name: string;
|
|
12
|
-
constructor(name: string);
|
|
13
|
-
fromAs(as?: string): Selection;
|
|
14
|
-
/** 选择单表全部列 */
|
|
15
|
-
select(columns: "*", as?: string): ChainSelect<T>;
|
|
16
|
-
/** 选择单表,带提示 */
|
|
17
|
-
select(columns: Constructable<{
|
|
18
|
-
[key in keyof T]?: string | boolean;
|
|
19
|
-
} & {
|
|
20
|
-
[key: string]: string | boolean;
|
|
21
|
-
}>, as?: string): ChainSelect<Record<string, any>>;
|
|
22
|
-
/** 选择单表 */
|
|
23
|
-
select<R extends {} = Record<string, any>>(columns: Constructable<string | string[]>, as?: string): ChainSelect<R>;
|
|
24
|
-
/** 选择单表 */
|
|
25
|
-
select<R extends {}>(columns: Constructable<{
|
|
26
|
-
[key in keyof R]: boolean | string;
|
|
27
|
-
} | string | string[]>, as?: string): ChainSelect<R>;
|
|
28
|
-
/**
|
|
29
|
-
* INSERT 语句,需要注意 SQL 注入
|
|
30
|
-
* @example
|
|
31
|
-
* ```ts
|
|
32
|
-
* table.insert(["age","name"], "VALUES (18, 'hi'), (17, 'hh')") // INSERT INTO table(age,name) VALUES(18, 'hi'), (17, 'hh')
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
insert(columns: string, values: Constructable<string>): ChainInsert<T>;
|
|
36
|
-
/**
|
|
37
|
-
* UPDATE 语句,需要注意 SQL 注入
|
|
38
|
-
* @example
|
|
39
|
-
* ```ts
|
|
40
|
-
* table.update("age=3, name='hi'") // "UPDATE table SET age=3, name='hi'"
|
|
41
|
-
* table.update({age: "3", name: "'hi'", k1: undefined, k2: ""}) // "UPDATE table SET age=3, name='hi'"
|
|
42
|
-
* ```
|
|
43
|
-
*/
|
|
44
|
-
update(values: Constructable<{
|
|
45
|
-
[key in keyof T]?: string;
|
|
46
|
-
} | string>): ChainUpdate<T>;
|
|
47
|
-
delete(option?: DeleteOption): ChainDelete<T>;
|
|
48
|
-
toSelect(): string;
|
|
49
|
-
}
|
|
50
|
-
/** @public */
|
|
51
|
-
export interface DeleteOption {
|
|
52
|
-
where?: Constructable<ConditionParam | void>;
|
|
53
|
-
}
|
|
54
|
-
//# sourceMappingURL=DbTable.d.ts.map
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { where } from '../util.js';
|
|
2
|
-
import { Selection } from './query_chain_select.js';
|
|
3
|
-
import { SqlChainModify } from './query_chain_insert.js';
|
|
4
|
-
import { createUpdateSetFromObject } from './_statement.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 数据库表
|
|
8
|
-
* @public
|
|
9
|
-
*/
|
|
10
|
-
class DbTable {
|
|
11
|
-
name;
|
|
12
|
-
constructor(name) {
|
|
13
|
-
this.name = name;
|
|
14
|
-
}
|
|
15
|
-
fromAs(as) {
|
|
16
|
-
return new Selection(this.name, as);
|
|
17
|
-
}
|
|
18
|
-
select(columns, as) {
|
|
19
|
-
return this.fromAs(as).select(columns);
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* INSERT 语句,需要注意 SQL 注入
|
|
23
|
-
* @example
|
|
24
|
-
* ```ts
|
|
25
|
-
* table.insert(["age","name"], "VALUES (18, 'hi'), (17, 'hh')") // INSERT INTO table(age,name) VALUES(18, 'hi'), (17, 'hh')
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
insert(columns, values) {
|
|
29
|
-
if (typeof columns !== "string" || !columns)
|
|
30
|
-
throw new TypeError("columns 必须是有效的 string 类型");
|
|
31
|
-
if (typeof values === "function")
|
|
32
|
-
values = values();
|
|
33
|
-
if (typeof values !== "string")
|
|
34
|
-
throw new TypeError("values 必须是 string 或 function 类型");
|
|
35
|
-
let sql = `INSERT INTO ${this.name}(${columns})\n${values}`;
|
|
36
|
-
return new SqlChainModify(sql);
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* UPDATE 语句,需要注意 SQL 注入
|
|
40
|
-
* @example
|
|
41
|
-
* ```ts
|
|
42
|
-
* table.update("age=3, name='hi'") // "UPDATE table SET age=3, name='hi'"
|
|
43
|
-
* table.update({age: "3", name: "'hi'", k1: undefined, k2: ""}) // "UPDATE table SET age=3, name='hi'"
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
|
-
update(values) {
|
|
47
|
-
if (typeof values === "function")
|
|
48
|
-
values = values();
|
|
49
|
-
switch (typeof values) {
|
|
50
|
-
case "object": {
|
|
51
|
-
let sql = createUpdateSetFromObject(values);
|
|
52
|
-
return new SqlChainModify("UPDATE " + this.name + " " + sql);
|
|
53
|
-
}
|
|
54
|
-
case "string":
|
|
55
|
-
return new SqlChainModify("UPDATE " + this.name + " SET\n" + values);
|
|
56
|
-
default:
|
|
57
|
-
throw new TypeError("参数 values 错误");
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
delete(option = {}) {
|
|
61
|
-
let sql = "DELETE FROM " + this.name;
|
|
62
|
-
sql += where(option.where);
|
|
63
|
-
return new SqlChainModify(sql);
|
|
64
|
-
}
|
|
65
|
-
toSelect() {
|
|
66
|
-
return this.name;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export { DbTable };
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { SqlValuesCreator } from "../sql_value/sql_value.ts";
|
|
2
|
-
import { UpdateRowValue, TableType } from "./type.ts";
|
|
3
|
-
import { Constructable } from "../util.ts";
|
|
4
|
-
import { DbTable } from "./DbTable.ts";
|
|
5
|
-
import { ChainInsert, ChainUpdate } from "./chain_modify.ts";
|
|
6
|
-
/** @public */
|
|
7
|
-
export declare class DbTableQuery<T extends TableType = Record<string, any>, C extends TableType = Partial<T>> extends DbTable<T> {
|
|
8
|
-
private statement;
|
|
9
|
-
constructor(name: string, statement: SqlValuesCreator);
|
|
10
|
-
/**
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* table.insert({age:18, name:"hi"}) // INSERT INTO table(age,name) VALUES (18, 'hi')
|
|
14
|
-
* table.insert([{age:18, name:"hi"}, {age:17, name:"hh"}]) // INSERT INTO table(age,name) VALUES(18, 'hi'), (17, 'hh')
|
|
15
|
-
* ```
|
|
16
|
-
*/
|
|
17
|
-
insert(values: Constructable<UpdateRowValue<C> | UpdateRowValue<C>[]>): ChainInsert<T>;
|
|
18
|
-
insert(columns: string, values: Constructable<string>): ChainInsert<T>;
|
|
19
|
-
/**
|
|
20
|
-
* UPDATE 语句,与 update() 不同的是,它会将值进行安全转换
|
|
21
|
-
* @example
|
|
22
|
-
* ```ts
|
|
23
|
-
* table.update({age:3, name:"hi"}, true) // "UPDATE table SET age=3, name='hi'"
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
updateFrom(values: Constructable<UpdateRowValue<T>>): ChainUpdate<T>;
|
|
27
|
-
}
|
|
28
|
-
//# sourceMappingURL=TableQuery.d.ts.map
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { getObjectListKeys } from '../util.js';
|
|
2
|
-
import { SqlChainModify } from './query_chain_insert.js';
|
|
3
|
-
import { DbTable } from './DbTable.js';
|
|
4
|
-
|
|
5
|
-
/** @public */
|
|
6
|
-
class DbTableQuery extends DbTable {
|
|
7
|
-
statement;
|
|
8
|
-
constructor(name, statement) {
|
|
9
|
-
super(name);
|
|
10
|
-
this.statement = statement;
|
|
11
|
-
}
|
|
12
|
-
insert(values_column, _values) {
|
|
13
|
-
if (_values)
|
|
14
|
-
return super.insert(values_column, _values);
|
|
15
|
-
let values = values_column;
|
|
16
|
-
if (typeof values === "function")
|
|
17
|
-
values = values();
|
|
18
|
-
if (typeof values !== "object")
|
|
19
|
-
throw new TypeError("values 类型错误");
|
|
20
|
-
let valuesStr;
|
|
21
|
-
let insertCol;
|
|
22
|
-
if (values instanceof Array) {
|
|
23
|
-
if (values.length === 0)
|
|
24
|
-
throw new Error("值不能为空");
|
|
25
|
-
insertCol = Array.from(getObjectListKeys(values));
|
|
26
|
-
valuesStr = `VALUES\n${this.statement.objectListToValuesList(values, insertCol)}`;
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
insertCol = Object.keys(values);
|
|
30
|
-
valuesStr = `VALUES\n(${this.statement.objectToValues(values, insertCol)})`;
|
|
31
|
-
}
|
|
32
|
-
if (insertCol.length === 0)
|
|
33
|
-
throw new Error("插入列不能为空");
|
|
34
|
-
const columnStr = insertCol.join(",");
|
|
35
|
-
let sql = `INSERT INTO ${this.name} (${columnStr})\n${valuesStr}`;
|
|
36
|
-
return new SqlChainModify(sql);
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* UPDATE 语句,与 update() 不同的是,它会将值进行安全转换
|
|
40
|
-
* @example
|
|
41
|
-
* ```ts
|
|
42
|
-
* table.update({age:3, name:"hi"}, true) // "UPDATE table SET age=3, name='hi'"
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
updateFrom(values) {
|
|
46
|
-
if (typeof values === "function")
|
|
47
|
-
values = values();
|
|
48
|
-
let setStr;
|
|
49
|
-
if (typeof values === "string")
|
|
50
|
-
setStr = values;
|
|
51
|
-
else {
|
|
52
|
-
const updateKey = Object.entries(values);
|
|
53
|
-
let setList = [];
|
|
54
|
-
for (const [k, v] of updateKey) {
|
|
55
|
-
if (v === undefined)
|
|
56
|
-
continue;
|
|
57
|
-
setList.push(k + "= " + this.statement.toSqlStr(v));
|
|
58
|
-
}
|
|
59
|
-
setStr = setList.join(",\n");
|
|
60
|
-
}
|
|
61
|
-
if (!setStr)
|
|
62
|
-
throw new Error("值不能为空");
|
|
63
|
-
let sql = `UPDATE ${this.name} SET\n${setStr}`;
|
|
64
|
-
return new SqlChainModify(sql);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export { DbTableQuery };
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { Constructable } from "../util.ts";
|
|
2
|
-
type ConditionParam = string | string[];
|
|
3
|
-
/**
|
|
4
|
-
* 生成条件语句
|
|
5
|
-
*/
|
|
6
|
-
export declare function condition(conditions?: Constructable<ConditionParam | void>, type?: "AND" | "OR"): string;
|
|
7
|
-
export declare function createUpdateSetFromObject(set: Record<string, string | undefined>): string;
|
|
8
|
-
export {};
|
|
9
|
-
//# sourceMappingURL=_statement.d.ts.map
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
function condition(conditions, type = "AND") {
|
|
2
|
-
if (typeof conditions === "function")
|
|
3
|
-
conditions = conditions();
|
|
4
|
-
if (!conditions)
|
|
5
|
-
return;
|
|
6
|
-
if (typeof conditions === "string")
|
|
7
|
-
return conditions;
|
|
8
|
-
else {
|
|
9
|
-
if (conditions.length) {
|
|
10
|
-
let sql = "";
|
|
11
|
-
type = " " + type + " ";
|
|
12
|
-
sql += conditions[0];
|
|
13
|
-
for (let i = 1; i < conditions.length; i++)
|
|
14
|
-
sql += type + conditions[i];
|
|
15
|
-
return sql;
|
|
16
|
-
}
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
function createUpdateSetFromObject(set) {
|
|
21
|
-
const updateKey = Object.keys(set);
|
|
22
|
-
let i = 0;
|
|
23
|
-
let key;
|
|
24
|
-
let value;
|
|
25
|
-
let sql;
|
|
26
|
-
for (; i < updateKey.length; i++) {
|
|
27
|
-
key = updateKey[i];
|
|
28
|
-
value = set[key];
|
|
29
|
-
if (value === undefined)
|
|
30
|
-
continue;
|
|
31
|
-
if (typeof value === "string") {
|
|
32
|
-
if (value) {
|
|
33
|
-
sql = "SET\n" + key + "= " + value;
|
|
34
|
-
break;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
else
|
|
38
|
-
throw new TypeError(`key ${key} 类型错误(${typeof value})`);
|
|
39
|
-
}
|
|
40
|
-
if (sql) {
|
|
41
|
-
i++;
|
|
42
|
-
for (; i < updateKey.length; i++) {
|
|
43
|
-
key = updateKey[i];
|
|
44
|
-
value = set[key];
|
|
45
|
-
if (value === undefined)
|
|
46
|
-
continue;
|
|
47
|
-
if (typeof value === "string") {
|
|
48
|
-
if (value)
|
|
49
|
-
sql += "," + key + "= " + value;
|
|
50
|
-
}
|
|
51
|
-
else
|
|
52
|
-
throw new TypeError(`key ${key} 类型错误(${typeof value})`);
|
|
53
|
-
}
|
|
54
|
-
return sql;
|
|
55
|
-
}
|
|
56
|
-
else
|
|
57
|
-
throw new Error("值不能为空");
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export { condition, createUpdateSetFromObject };
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { ConditionParam, Constructable, SelectParam } from "../util.ts";
|
|
2
|
-
import { TableType } from "./type.ts";
|
|
3
|
-
import { SqlStatement, SqlStatementDataset } from "./chain_base.ts";
|
|
4
|
-
/** @public */
|
|
5
|
-
export interface ChainModifyReturning<T extends TableType = {}> extends SqlStatement {
|
|
6
|
-
returning(columns: "*"): SqlStatementDataset<T>;
|
|
7
|
-
returning(columns: Constructable<SelectParam>): SqlStatementDataset<Record<string, any>>;
|
|
8
|
-
returning<R extends TableType>(columns: Constructable<SelectParam>): SqlStatementDataset<R>;
|
|
9
|
-
}
|
|
10
|
-
/** @public */
|
|
11
|
-
export interface ChainAfterConflictDo<T extends TableType = {}> {
|
|
12
|
-
doNotThing(): ChainModifyReturning<T>;
|
|
13
|
-
/**
|
|
14
|
-
* 需要注意 SQL 注入
|
|
15
|
-
*/
|
|
16
|
-
doUpdate(set: Constructable<string | {
|
|
17
|
-
[key in keyof T]?: string;
|
|
18
|
-
}>): ChainModifyReturning<T>;
|
|
19
|
-
toString(): string;
|
|
20
|
-
}
|
|
21
|
-
/** @public */
|
|
22
|
-
export interface ChainInsert<T extends TableType = {}> extends ChainModifyReturning<T> {
|
|
23
|
-
onConflict(option: Constructable<readonly (keyof T)[] | string>): ChainAfterConflictDo<T>;
|
|
24
|
-
}
|
|
25
|
-
/** @public */
|
|
26
|
-
export interface ChainUpdate<T extends TableType = {}> extends ChainModifyReturning<T> {
|
|
27
|
-
where(where: Constructable<ConditionParam | void>): ChainModifyReturning<T>;
|
|
28
|
-
}
|
|
29
|
-
/** @public */
|
|
30
|
-
export interface ChainDelete<T extends TableType = {}> extends ChainModifyReturning<T> {
|
|
31
|
-
where(where: Constructable<ConditionParam | void>): ChainModifyReturning<T>;
|
|
32
|
-
}
|
|
33
|
-
//# sourceMappingURL=chain_modify.d.ts.map
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { ConditionParam, Constructable, OrderByParam } from "../util.ts";
|
|
2
|
-
import { SqlStatementDataset } from "./chain_base.ts";
|
|
3
|
-
import { TableType } from "./type.ts";
|
|
4
|
-
/** @public */
|
|
5
|
-
export interface ChainSelect<T extends TableType> extends ChainSelectAfterWhere<T> {
|
|
6
|
-
where(param: Constructable<ConditionParam | void>): ChainSelectAfterWhere<T>;
|
|
7
|
-
}
|
|
8
|
-
/** @public */
|
|
9
|
-
export interface ChainSelectAfterWhere<T extends TableType> extends ChainSelectAfterHaving<T> {
|
|
10
|
-
groupBy(columns: string | string[]): ChainSelectAfterGroupBy<T>;
|
|
11
|
-
}
|
|
12
|
-
/** @public */
|
|
13
|
-
export interface ChainSelectAfterGroupBy<T extends TableType> extends ChainSelectAfterHaving<T> {
|
|
14
|
-
orderBy(param: Constructable<OrderByParam | void>): ChainSelectAfterOrderBy<T>;
|
|
15
|
-
having(param: Constructable<ConditionParam | void>): ChainSelectAfterHaving<T>;
|
|
16
|
-
}
|
|
17
|
-
/** @public */
|
|
18
|
-
export interface ChainSelectAfterHaving<T extends TableType> extends ChainSelectAfterOrderBy<T> {
|
|
19
|
-
orderBy(param: Constructable<OrderByParam | void>): ChainSelectAfterOrderBy<T>;
|
|
20
|
-
}
|
|
21
|
-
/** @public */
|
|
22
|
-
export interface ChainSelectAfterOrderBy<T extends TableType> extends SqlStatementDataset<T> {
|
|
23
|
-
limit(limit?: number | bigint, offset?: number | bigint): ChainSelectAfterLimit<T>;
|
|
24
|
-
}
|
|
25
|
-
/** @public */
|
|
26
|
-
export interface ChainSelectAfterLimit<T extends TableType> extends SqlStatementDataset<T> {
|
|
27
|
-
}
|
|
28
|
-
//# sourceMappingURL=chain_select.d.ts.map
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { ConditionParam, Constructable, SelectParam } from "../util.ts";
|
|
2
|
-
import { SqlStatementDataset, SqlStatement } from "./chain_base.ts";
|
|
3
|
-
import { ChainAfterConflictDo, ChainModifyReturning, ChainInsert, ChainDelete, ChainUpdate } from "./chain_modify.ts";
|
|
4
|
-
import { TableType } from "./type.ts";
|
|
5
|
-
export declare class SqlChainModify<T extends TableType = {}> extends SqlStatement implements ChainInsert<T>, ChainUpdate<T>, ChainDelete<T> {
|
|
6
|
-
readonly sql: string;
|
|
7
|
-
constructor(sql: string);
|
|
8
|
-
returning<R extends {}>(returns: Constructable<SelectParam | "*">): SqlStatementDataset<R>;
|
|
9
|
-
onConflict(onConflict: Constructable<readonly string[] | string>): ChainAfterConflictDo<T>;
|
|
10
|
-
where(where: Constructable<ConditionParam | void>): ChainModifyReturning<T>;
|
|
11
|
-
genSql(): string;
|
|
12
|
-
}
|
|
13
|
-
export declare class SqlInsertConflictBranch<T extends TableType = {}> implements ChainAfterConflictDo<T> {
|
|
14
|
-
readonly sql: string;
|
|
15
|
-
constructor(sql: string);
|
|
16
|
-
doUpdate(set?: Constructable<string | {
|
|
17
|
-
[key: string]: string | undefined;
|
|
18
|
-
}>): ChainModifyReturning<T>;
|
|
19
|
-
doNotThing(): ChainModifyReturning<T>;
|
|
20
|
-
toString(): string;
|
|
21
|
-
}
|
|
22
|
-
//# sourceMappingURL=query_chain_insert.d.ts.map
|