@asla/yoursql 0.10.2 → 0.11.1
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/client/DbPoolConnection.d.ts +1 -1
- package/dist/client/DbPoolTransaction.d.ts +1 -1
- package/dist/client/DbQuery.d.ts +1 -1
- package/dist/client/DbQueryBase.d.ts +1 -1
- package/dist/client/_type.d.ts +2 -0
- package/dist/client/interfaces.d.ts +1 -1
- package/dist/sql_gen/mod.js +2 -1
- package/dist/sql_gen/sql_value/sql_value.d.ts +2 -41
- package/dist/sql_gen/sql_value/sql_value.js +1 -37
- package/dist/sql_gen/statement/cte.d.ts +8 -0
- package/dist/sql_gen/statement/cte.js +33 -3
- package/dist/sql_gen/statement/delete.d.ts +2 -13
- package/dist/sql_gen/statement/delete.js +4 -38
- package/dist/sql_gen/statement/delete_chain.d.ts +15 -0
- package/dist/sql_gen/statement/delete_impl.d.ts +12 -0
- package/dist/sql_gen/statement/delete_impl.js +31 -0
- package/dist/sql_gen/statement/insert.d.ts +2 -4
- package/dist/sql_gen/statement/insert.js +4 -98
- package/dist/sql_gen/statement/insert_chain.d.ts +5 -0
- package/dist/sql_gen/statement/insert_impl.d.ts +14 -0
- package/dist/sql_gen/statement/insert_impl.js +99 -0
- package/dist/sql_gen/statement/select.d.ts +3 -36
- package/dist/sql_gen/statement/select.js +6 -146
- package/dist/sql_gen/statement/select_chain.d.ts +12 -0
- package/dist/sql_gen/statement/select_impl.d.ts +31 -0
- package/dist/sql_gen/statement/select_impl.js +146 -0
- package/dist/sql_gen/statement/update.d.ts +2 -15
- package/dist/sql_gen/statement/update.js +4 -80
- package/dist/sql_gen/statement/update_chain.d.ts +16 -0
- package/dist/sql_gen/statement/update_impl.d.ts +23 -0
- package/dist/sql_gen/statement/update_impl.js +72 -0
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { SqlStatementDataset } from "./_type.ts";
|
|
2
2
|
import { DbQuery } from "./DbQuery.ts";
|
|
3
3
|
import type { MultipleQueryResult, QueryRowsResult, DbQueryBase } from "./DbQueryBase.ts";
|
|
4
4
|
import type { SqlLike, TransactionMode } from "./interfaces.ts";
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { SqlStatementDataset } from "../sql_gen/mod.ts";
|
|
2
1
|
import { DbQuery } from "./DbQuery.ts";
|
|
3
2
|
import type { MultipleQueryResult, QueryRowsResult } from "./DbQueryBase.ts";
|
|
4
3
|
import type { DbPoolConnection } from "./DbPoolConnection.ts";
|
|
5
4
|
import type { DbTransaction, SqlLike, TransactionMode } from "./interfaces.ts";
|
|
5
|
+
import { SqlStatementDataset } from "./_type.ts";
|
|
6
6
|
/** @public */
|
|
7
7
|
export type DbPoolTransactionOption = {
|
|
8
8
|
errorRollback?: boolean;
|
package/dist/client/DbQuery.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { SqlStatementDataset } from "../sql_gen/mod.ts";
|
|
2
1
|
import { SqlLike } from "./interfaces.ts";
|
|
3
2
|
import { MultipleQueryResult, DbQueryBase, QueryRowsResult } from "./DbQueryBase.ts";
|
|
3
|
+
import { SqlStatementDataset } from "./_type.ts";
|
|
4
4
|
/**
|
|
5
5
|
* SQL 查询相关操作
|
|
6
6
|
* @public
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { SqlStatementDataset } from "../sql_gen/mod.ts";
|
|
2
1
|
import { DbQuery } from "./DbQuery.ts";
|
|
3
2
|
import { DbPoolConnection } from "./DbPoolConnection.ts";
|
|
4
3
|
import { DbCursor, DbCursorOption } from "./DbCursor.ts";
|
|
4
|
+
import { SqlStatementDataset } from "./_type.ts";
|
|
5
5
|
/**
|
|
6
6
|
* 数据库连接
|
|
7
7
|
* @public
|
package/dist/sql_gen/mod.js
CHANGED
|
@@ -2,7 +2,7 @@ import { pgSqlTransformer } from './sql_value/db_type.js';
|
|
|
2
2
|
import { SqlValuesCreator } from './sql_value/sql_value.js';
|
|
3
3
|
export { SqlExplicitValuesStatement } from './sql_value/sql_value.js';
|
|
4
4
|
export { SqlStatement, SqlStatementDataset, SqlTextStatementDataset } from './SqlStatement.js';
|
|
5
|
-
export {
|
|
5
|
+
export { select } from './statement/select.js';
|
|
6
6
|
export { insertInto } from './statement/insert.js';
|
|
7
7
|
export { update } from './statement/update.js';
|
|
8
8
|
export { deleteFrom } from './statement/delete.js';
|
|
@@ -11,6 +11,7 @@ export { selectColumns } from './util.js';
|
|
|
11
11
|
export { TypeChecker } from './your_table/checker.js';
|
|
12
12
|
export { ColumnMeta, CustomDbType, YourTypeMap } from './your_table/infer_db_type.js';
|
|
13
13
|
export { YourTable } from './your_table/table.js';
|
|
14
|
+
export { orderBy } from './statement/select_impl.js';
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* 默认的 SqlValuesCreator 实列
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AssertJsType,
|
|
1
|
+
import { SqlTemplate } from "../SqlStatement.ts";
|
|
2
|
+
import { AssertJsType, ObjectToValueKeys } from "./type.ts";
|
|
3
3
|
/** @public js 对象到编码函数的映射*/
|
|
4
4
|
export type JsObjectMapSql = Map<new (...args: any[]) => any, SqlValueEncoder>;
|
|
5
5
|
/** @public 将 js 值转为 SQl 字符串的函数*/
|
|
@@ -45,10 +45,6 @@ export declare class SqlValuesCreator {
|
|
|
45
45
|
/** 获取值对应已定义的类 */
|
|
46
46
|
getClassType(value: object): undefined | (new (...args: unknown[]) => unknown);
|
|
47
47
|
protected defaultObject(value: object): string;
|
|
48
|
-
/** @deprecated 改用 createImplicitValues */
|
|
49
|
-
objectListToValuesList<T extends object>(objectList: T[], keys?: ObjectToValueKeys<T>): string;
|
|
50
|
-
/** @deprecated 请使用 objectListToValues 代替 */
|
|
51
|
-
objectListToValues<T extends object>(objectList: T[], columns?: ObjectToValueKeys<T>): SqlValuesTextData;
|
|
52
48
|
/**
|
|
53
49
|
* 将对象列表转为 SQL 的 VALUES。如果 objectList 中有某个对象的属性值为 undefined,则会被转换为 "DEFAULT"
|
|
54
50
|
* @example 返回的文本示例: " (...),(...) "
|
|
@@ -65,25 +61,6 @@ export declare class SqlValuesCreator {
|
|
|
65
61
|
createExplicitValues<T extends object>(objectList: T[], columns?: ObjectToValueKeys<T>): SqlExplicitValuesStatement;
|
|
66
62
|
private _objectListToValues;
|
|
67
63
|
private _objectToValue;
|
|
68
|
-
/**
|
|
69
|
-
* @deprecated 请使用 objectToValue 代替
|
|
70
|
-
*/
|
|
71
|
-
objectToValues<T extends object>(object: T, keys?: ObjectToValueKeys<T>): string;
|
|
72
|
-
/**
|
|
73
|
-
* @deprecated 请使用 createImplicitValues 代替
|
|
74
|
-
* 将对象转为 SQL 的 value
|
|
75
|
-
* @example
|
|
76
|
-
* ```ts
|
|
77
|
-
* v.objectToValue({ a: 1, b: "2", c: null, d: undefined }).text // "('1', '2', NULL, DEFAULT)"
|
|
78
|
-
* v.objectToValue({ a: 1, b: "2", c: null, d: undefined }, ["a", "b"]).text // "('1', '2')"
|
|
79
|
-
* v.objectToValue(
|
|
80
|
-
* { a: 1, b: "2", c: null, d: undefined },
|
|
81
|
-
* { a: "INT", b: "TEXT", c: "JSONB", d: "TEXT" }
|
|
82
|
-
* ).text // "('1'::INT, '2'::TEXT, NULL::JSONB, DEFAULT::TEXT)"
|
|
83
|
-
* ```
|
|
84
|
-
* @param keys - 如果指定了key, object undefined 的属性值将填充为 null,如果不指定,将自获取 object 所有非 undefined 的属性的key
|
|
85
|
-
*/
|
|
86
|
-
objectToValue<T extends object>(object: T, keys?: ObjectToValueKeys<T>): SqlValuesTextData;
|
|
87
64
|
private _getObjectValueInfo;
|
|
88
65
|
private _internalObjectToValues;
|
|
89
66
|
/**
|
|
@@ -94,22 +71,6 @@ export declare class SqlValuesCreator {
|
|
|
94
71
|
* ```
|
|
95
72
|
*/
|
|
96
73
|
toValues(values: readonly any[]): string;
|
|
97
|
-
/**
|
|
98
|
-
* @deprecated 请使用 objectListToValues 代替
|
|
99
|
-
* @public 创建 VALUES AS 语句
|
|
100
|
-
* @example
|
|
101
|
-
* ```ts
|
|
102
|
-
* sqlValue.createValues(
|
|
103
|
-
* "customName",
|
|
104
|
-
* [{age:8, name:"hhh"}, {age:9, name:"row2"}],
|
|
105
|
-
* {age:"INT", name:"TEXT"}
|
|
106
|
-
* )
|
|
107
|
-
* // (VALUES (8:INT,'hhh':TEXT),(9,'row2')) AS customName(age, name)
|
|
108
|
-
* ```
|
|
109
|
-
*/
|
|
110
|
-
createValues<T extends {}>(asName: string, values: T[], valuesTypes: Record<string, string | ({
|
|
111
|
-
sqlType: string;
|
|
112
|
-
} & Pick<ColumnToValueConfig, "assertJsType" | "sqlDefault">)>): SqlStatementDataset<T>;
|
|
113
74
|
}
|
|
114
75
|
/** @public */
|
|
115
76
|
export type SqlValuesTextData = {
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { SqlTextStatementDataset } from '../SqlStatement.js';
|
|
2
1
|
import { getObjectListKeys } from '../_statement.js';
|
|
3
2
|
import { ValueSqlTemplate } from './ValueSqlTemplate.js';
|
|
4
3
|
|
|
@@ -115,14 +114,6 @@ class SqlValuesCreator {
|
|
|
115
114
|
defaultObject(value) {
|
|
116
115
|
return SqlValuesCreator.string(JSON.stringify(value));
|
|
117
116
|
}
|
|
118
|
-
/** @deprecated 改用 createImplicitValues */
|
|
119
|
-
objectListToValuesList(objectList, keys) {
|
|
120
|
-
return this.objectListToValues(objectList, keys).text;
|
|
121
|
-
}
|
|
122
|
-
/** @deprecated 请使用 objectListToValues 代替 */
|
|
123
|
-
objectListToValues(objectList, columns) {
|
|
124
|
-
return this.createImplicitValues(objectList, columns);
|
|
125
|
-
}
|
|
126
117
|
createImplicitValues(objectList, columns) {
|
|
127
118
|
let res;
|
|
128
119
|
if (objectList instanceof Array) {
|
|
@@ -191,27 +182,6 @@ class SqlValuesCreator {
|
|
|
191
182
|
const text = this._internalObjectToValues(object, keys, type, undefinedDefault);
|
|
192
183
|
return new SqlExplicitValuesStatement(keys, `(${text})`);
|
|
193
184
|
}
|
|
194
|
-
objectToValues(object, keys_types) {
|
|
195
|
-
const { keys, type } = this._getObjectValueInfo(object, keys_types);
|
|
196
|
-
return this._internalObjectToValues(object, keys, type, "DEFAULT");
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* @deprecated 请使用 createImplicitValues 代替
|
|
200
|
-
* 将对象转为 SQL 的 value
|
|
201
|
-
* @example
|
|
202
|
-
* ```ts
|
|
203
|
-
* v.objectToValue({ a: 1, b: "2", c: null, d: undefined }).text // "('1', '2', NULL, DEFAULT)"
|
|
204
|
-
* v.objectToValue({ a: 1, b: "2", c: null, d: undefined }, ["a", "b"]).text // "('1', '2')"
|
|
205
|
-
* v.objectToValue(
|
|
206
|
-
* { a: 1, b: "2", c: null, d: undefined },
|
|
207
|
-
* { a: "INT", b: "TEXT", c: "JSONB", d: "TEXT" }
|
|
208
|
-
* ).text // "('1'::INT, '2'::TEXT, NULL::JSONB, DEFAULT::TEXT)"
|
|
209
|
-
* ```
|
|
210
|
-
* @param keys - 如果指定了key, object undefined 的属性值将填充为 null,如果不指定,将自获取 object 所有非 undefined 的属性的key
|
|
211
|
-
*/
|
|
212
|
-
objectToValue(object, keys) {
|
|
213
|
-
return this.createImplicitValues(object, keys);
|
|
214
|
-
}
|
|
215
185
|
_getObjectValueInfo(object, keys_types) {
|
|
216
186
|
let type;
|
|
217
187
|
let keys;
|
|
@@ -224,7 +194,7 @@ class SqlValuesCreator {
|
|
|
224
194
|
type = initColumnAssert(keys, keys_types);
|
|
225
195
|
}
|
|
226
196
|
else {
|
|
227
|
-
keys = Object.keys(object);
|
|
197
|
+
keys = Object.keys(object).filter((k) => object[k] !== undefined);
|
|
228
198
|
type = [];
|
|
229
199
|
}
|
|
230
200
|
return { keys, type };
|
|
@@ -272,12 +242,6 @@ class SqlValuesCreator {
|
|
|
272
242
|
throw new Error("values 不能为空");
|
|
273
243
|
return values.map((v) => this.toSqlStr(v)).join(",");
|
|
274
244
|
}
|
|
275
|
-
createValues(asName, values, valuesTypes) {
|
|
276
|
-
if (values.length === 0)
|
|
277
|
-
throw new Error("values 不能为空");
|
|
278
|
-
const res = this.createExplicitValues(values, valuesTypes); // 预检数据
|
|
279
|
-
return new SqlTextStatementDataset(res.toSelect(asName));
|
|
280
|
-
}
|
|
281
245
|
}
|
|
282
246
|
class AssertError extends TypeError {
|
|
283
247
|
constructor(assertType, actual) {
|
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import { Constructable } from "../util.ts";
|
|
2
|
+
import { SelectSqlGenerator } from "./select_chain.ts";
|
|
3
|
+
import { UpdateSqlGenerator } from "./update_chain.ts";
|
|
4
|
+
import { DeleteFromSqlGenerator } from "./delete_chain.ts";
|
|
5
|
+
import { InsertIntoSqlGenerator } from "./insert_chain.ts";
|
|
2
6
|
/**
|
|
3
7
|
* @public
|
|
4
8
|
*/
|
|
5
9
|
export interface ChainCTE {
|
|
6
10
|
as(statement: Constructable<string>): ChainCTE;
|
|
7
11
|
as(name: string, statement: Constructable<string>): ChainCTE;
|
|
12
|
+
select: SelectSqlGenerator;
|
|
13
|
+
update: UpdateSqlGenerator;
|
|
14
|
+
deleteFrom: DeleteFromSqlGenerator;
|
|
15
|
+
insertInto: InsertIntoSqlGenerator;
|
|
8
16
|
toString(): string;
|
|
9
17
|
}
|
|
10
18
|
/**
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import { selectColumns } from '../util.js';
|
|
2
|
+
import { SelectChainAfterSelect } from './select_impl.js';
|
|
3
|
+
import { DeleteChain } from './delete_impl.js';
|
|
4
|
+
import { UpdateChain } from './update_impl.js';
|
|
5
|
+
import { InsertChain } from './insert_impl.js';
|
|
6
|
+
|
|
1
7
|
function withAs(nameOrStatement, statement) {
|
|
2
8
|
return new ChainCETImpl(`WITH \n${concat(nameOrStatement, statement)}`);
|
|
3
9
|
}
|
|
@@ -10,11 +16,35 @@ class ChainCETImpl {
|
|
|
10
16
|
this.sql = sql;
|
|
11
17
|
}
|
|
12
18
|
as(nameOrStatement, statement) {
|
|
13
|
-
|
|
14
|
-
|
|
19
|
+
return new ChainCETImpl(`${this.sql},\n${concat(nameOrStatement, statement)}`);
|
|
20
|
+
}
|
|
21
|
+
select(columns) {
|
|
22
|
+
if (!columns)
|
|
23
|
+
return new SelectChainAfterSelect("SELECT ");
|
|
24
|
+
if (typeof columns === "function")
|
|
25
|
+
columns = columns();
|
|
26
|
+
return new SelectChainAfterSelect("SELECT " + selectColumns(columns));
|
|
27
|
+
}
|
|
28
|
+
update(table, options) {
|
|
29
|
+
let sql = `${this.sql}\nUPDATE ${table}`;
|
|
30
|
+
if (options?.as) {
|
|
31
|
+
sql += ` AS ${options.as}`;
|
|
32
|
+
}
|
|
33
|
+
return new UpdateChain(sql);
|
|
34
|
+
}
|
|
35
|
+
deleteFrom(table, option) {
|
|
36
|
+
let sql = `${this.sql}\nDELETE FROM ${table}`;
|
|
37
|
+
if (option?.as) {
|
|
38
|
+
sql += ` AS ${option.as}`;
|
|
39
|
+
}
|
|
40
|
+
return new DeleteChain(sql);
|
|
41
|
+
}
|
|
42
|
+
insertInto(table, columns) {
|
|
43
|
+
if (columns) {
|
|
44
|
+
return new InsertChain(`INSERT INTO ${table}(${columns.join(",")})`);
|
|
15
45
|
}
|
|
16
46
|
else {
|
|
17
|
-
return new
|
|
47
|
+
return new InsertChain(`INSERT INTO ${table}`);
|
|
18
48
|
}
|
|
19
49
|
}
|
|
20
50
|
toString() {
|
|
@@ -1,15 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DeleteFromSqlGenerator } from "./delete_chain.ts";
|
|
2
2
|
/** @public */
|
|
3
|
-
export
|
|
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;
|
|
3
|
+
export declare const deleteFrom: DeleteFromSqlGenerator;
|
|
15
4
|
//# sourceMappingURL=delete.d.ts.map
|
|
@@ -1,46 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { returningToString, whereToString } from '../_statement.js';
|
|
1
|
+
import { DeleteChain } from './delete_impl.js';
|
|
3
2
|
|
|
4
|
-
/**
|
|
5
|
-
|
|
6
|
-
* @example
|
|
7
|
-
* ```ts
|
|
8
|
-
* deleteFrom("table1").where("id = 1") // DELETE FROM table1 WHERE id = 1
|
|
9
|
-
* deleteFrom("table1 AS t").where("t.id = 1") // DELETE FROM table1 AS t WHERE t.id = 1
|
|
10
|
-
* ```
|
|
11
|
-
*/
|
|
12
|
-
function deleteFrom(table, option) {
|
|
3
|
+
/** @public */
|
|
4
|
+
const deleteFrom = function deleteFrom(table, option) {
|
|
13
5
|
let sql = `DELETE FROM ${table}`;
|
|
14
6
|
if (option?.as) {
|
|
15
7
|
sql += ` AS ${option.as}`;
|
|
16
8
|
}
|
|
17
9
|
return new DeleteChain(sql);
|
|
18
|
-
}
|
|
19
|
-
class DeleteChain extends SqlStatement {
|
|
20
|
-
sql;
|
|
21
|
-
constructor(sql) {
|
|
22
|
-
super();
|
|
23
|
-
this.sql = sql;
|
|
24
|
-
}
|
|
25
|
-
using(...from) {
|
|
26
|
-
const textList = from.map((f, i) => {
|
|
27
|
-
if (typeof f === "function")
|
|
28
|
-
return f();
|
|
29
|
-
return f;
|
|
30
|
-
});
|
|
31
|
-
const sql = this.genSql() + `\nUSING ${textList.join(", ")}`;
|
|
32
|
-
return new DeleteChain(sql);
|
|
33
|
-
}
|
|
34
|
-
returning(returns) {
|
|
35
|
-
return new SqlTextStatementDataset(this.genSql() + returningToString(returns));
|
|
36
|
-
}
|
|
37
|
-
where(where) {
|
|
38
|
-
const sql = whereToString(where);
|
|
39
|
-
return new DeleteChain(this.genSql() + sql);
|
|
40
|
-
}
|
|
41
|
-
genSql() {
|
|
42
|
-
return this.sql;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
10
|
+
};
|
|
45
11
|
|
|
46
12
|
export { deleteFrom };
|
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
import { ConditionParam, Constructable } from "../util.ts";
|
|
2
2
|
import { ChainModifyReturning } from "./_modify.ts";
|
|
3
3
|
/** @public */
|
|
4
|
+
export interface DeleteOption {
|
|
5
|
+
as?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* @public
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* deleteFrom("table1").where("id = 1") // DELETE FROM table1 WHERE id = 1
|
|
12
|
+
* deleteFrom("table1 AS t").where("t.id = 1") // DELETE FROM table1 AS t WHERE t.id = 1
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export interface DeleteFromSqlGenerator {
|
|
16
|
+
(table: string, option?: DeleteOption): ChainDelete;
|
|
17
|
+
}
|
|
18
|
+
/** @public */
|
|
4
19
|
export interface ChainDelete extends ChainDeleteAfterUsing {
|
|
5
20
|
/**
|
|
6
21
|
* @example
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ConditionParam, Constructable, SelectParam } from "../util.ts";
|
|
2
|
+
import { SqlStatementDataset, SqlStatement } from "../SqlStatement.ts";
|
|
3
|
+
import { ChainDelete, ChainDeleteAfterUsing, ChainDeleteReturning } from "./delete_chain.ts";
|
|
4
|
+
export declare class DeleteChain extends SqlStatement implements ChainDelete {
|
|
5
|
+
readonly sql: string;
|
|
6
|
+
constructor(sql: string);
|
|
7
|
+
using(...from: Constructable<string>[]): ChainDeleteAfterUsing;
|
|
8
|
+
returning<R extends {}>(returns: Constructable<SelectParam | "*">): SqlStatementDataset<R>;
|
|
9
|
+
where(where: Constructable<ConditionParam | void>): ChainDeleteReturning;
|
|
10
|
+
genSql(): string;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=delete_impl.d.ts.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { SqlStatement, SqlTextStatementDataset } from '../SqlStatement.js';
|
|
2
|
+
import { returningToString, whereToString } from '../_statement.js';
|
|
3
|
+
|
|
4
|
+
class DeleteChain extends SqlStatement {
|
|
5
|
+
sql;
|
|
6
|
+
constructor(sql) {
|
|
7
|
+
super();
|
|
8
|
+
this.sql = sql;
|
|
9
|
+
}
|
|
10
|
+
using(...from) {
|
|
11
|
+
const textList = from.map((f, i) => {
|
|
12
|
+
if (typeof f === "function")
|
|
13
|
+
return f();
|
|
14
|
+
return f;
|
|
15
|
+
});
|
|
16
|
+
const sql = this.genSql() + `\nUSING ${textList.join(", ")}`;
|
|
17
|
+
return new DeleteChain(sql);
|
|
18
|
+
}
|
|
19
|
+
returning(returns) {
|
|
20
|
+
return new SqlTextStatementDataset(this.genSql() + returningToString(returns));
|
|
21
|
+
}
|
|
22
|
+
where(where) {
|
|
23
|
+
const sql = whereToString(where);
|
|
24
|
+
return new DeleteChain(this.genSql() + sql);
|
|
25
|
+
}
|
|
26
|
+
genSql() {
|
|
27
|
+
return this.sql;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { DeleteChain };
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { InsertIntoSqlGenerator } from "./insert_chain.ts";
|
|
2
2
|
/** @public */
|
|
3
|
-
export declare
|
|
4
|
-
/** @public */
|
|
5
|
-
export declare function insertInto(table: string, columns: readonly string[]): ChainInsert;
|
|
3
|
+
export declare const insertInto: InsertIntoSqlGenerator;
|
|
6
4
|
//# sourceMappingURL=insert.d.ts.map
|
|
@@ -1,107 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { whereToString, createUpdateSetFromObject } from '../_statement.js';
|
|
3
|
-
import { SqlStatement, SqlTextStatementDataset } from '../SqlStatement.js';
|
|
1
|
+
import { InsertChain } from './insert_impl.js';
|
|
4
2
|
|
|
5
|
-
|
|
3
|
+
/** @public */
|
|
4
|
+
const insertInto = function insertInto(table, columns) {
|
|
6
5
|
if (columns) {
|
|
7
6
|
return new InsertChain(`INSERT INTO ${table}(${columns.join(",")})`);
|
|
8
7
|
}
|
|
9
8
|
else {
|
|
10
9
|
return new InsertChain(`INSERT INTO ${table}`);
|
|
11
10
|
}
|
|
12
|
-
}
|
|
13
|
-
class InsertChain extends SqlStatement {
|
|
14
|
-
sql;
|
|
15
|
-
constructor(sql) {
|
|
16
|
-
super();
|
|
17
|
-
this.sql = sql;
|
|
18
|
-
}
|
|
19
|
-
values(statement) {
|
|
20
|
-
if (typeof statement === "function")
|
|
21
|
-
statement = statement();
|
|
22
|
-
switch (typeof statement) {
|
|
23
|
-
case "object":
|
|
24
|
-
if (statement instanceof Array) {
|
|
25
|
-
statement = statement.join(",\n");
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
throw new TypeError("参数 statement 类型错误");
|
|
29
|
-
}
|
|
30
|
-
break;
|
|
31
|
-
case "string":
|
|
32
|
-
break;
|
|
33
|
-
default:
|
|
34
|
-
throw new TypeError("参数 statement 类型错误");
|
|
35
|
-
}
|
|
36
|
-
return new InsertChain(this.sql + `\nVALUES\n${statement}`);
|
|
37
|
-
}
|
|
38
|
-
select(statement) {
|
|
39
|
-
if (typeof statement === "function")
|
|
40
|
-
statement = statement();
|
|
41
|
-
return new InsertChain(this.sql + `\n${statement}`);
|
|
42
|
-
}
|
|
43
|
-
returning(returns) {
|
|
44
|
-
if (typeof returns === "function")
|
|
45
|
-
returns = returns();
|
|
46
|
-
let columnsStr;
|
|
47
|
-
if (returns === "*") {
|
|
48
|
-
columnsStr = "*";
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
columnsStr = selectColumns(returns);
|
|
52
|
-
}
|
|
53
|
-
let sql = this.genSql() + "\nRETURNING " + columnsStr;
|
|
54
|
-
return new SqlTextStatementDataset(sql);
|
|
55
|
-
}
|
|
56
|
-
onConflict(onConflict) {
|
|
57
|
-
if (typeof onConflict === "function")
|
|
58
|
-
onConflict = onConflict();
|
|
59
|
-
if (typeof onConflict !== "string")
|
|
60
|
-
onConflict = onConflict.join(",");
|
|
61
|
-
let sql = this.genSql() + `\nON CONFLICT (${onConflict})`;
|
|
62
|
-
return new InsertAfterOnConflict(sql);
|
|
63
|
-
}
|
|
64
|
-
where(where) {
|
|
65
|
-
const sql = whereToString(where);
|
|
66
|
-
return new InsertChain(this.genSql() + sql);
|
|
67
|
-
}
|
|
68
|
-
genSql() {
|
|
69
|
-
return this.sql;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
class InsertAfterOnConflict {
|
|
73
|
-
sql;
|
|
74
|
-
constructor(sql) {
|
|
75
|
-
this.sql = sql;
|
|
76
|
-
}
|
|
77
|
-
doUpdate(set) {
|
|
78
|
-
if (typeof set === "function")
|
|
79
|
-
set = set();
|
|
80
|
-
let sql;
|
|
81
|
-
switch (typeof set) {
|
|
82
|
-
case "object": {
|
|
83
|
-
if (set instanceof Array) {
|
|
84
|
-
sql = "\nDO UPDATE SET " + set.join(", ");
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
sql = "\nDO UPDATE " + createUpdateSetFromObject(set);
|
|
88
|
-
}
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
case "string":
|
|
92
|
-
sql = "\nDO UPDATE " + set;
|
|
93
|
-
break;
|
|
94
|
-
default:
|
|
95
|
-
throw new TypeError("参数 set 类型错误");
|
|
96
|
-
}
|
|
97
|
-
return new InsertChain(this.sql + sql);
|
|
98
|
-
}
|
|
99
|
-
doNotThing() {
|
|
100
|
-
return new InsertChain(this.sql + " DO NOTHING");
|
|
101
|
-
}
|
|
102
|
-
toString() {
|
|
103
|
-
return this.sql;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
11
|
+
};
|
|
106
12
|
|
|
107
13
|
export { insertInto };
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { Constructable } from "../util.ts";
|
|
2
2
|
import { ChainModifyReturning } from "./_modify.ts";
|
|
3
3
|
/** @public */
|
|
4
|
+
export interface InsertIntoSqlGenerator {
|
|
5
|
+
(target: string): ChainInsert;
|
|
6
|
+
(table: string, columns: readonly string[]): ChainInsert;
|
|
7
|
+
}
|
|
8
|
+
/** @public */
|
|
4
9
|
export interface ChainAfterConflict {
|
|
5
10
|
doNotThing(): ChainInsertReturning;
|
|
6
11
|
doUpdate(set: Constructable<string | readonly string[] | Record<string, string>>): ChainInsertReturning;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ConditionParam, Constructable, SelectParam } from "../util.ts";
|
|
2
|
+
import { SqlStatementDataset, SqlStatement } from "../SqlStatement.ts";
|
|
3
|
+
import { ChainAfterConflict, ChainInsert, ChainInsertAfterValues, ChainInsertReturning } from "./insert_chain.ts";
|
|
4
|
+
export declare class InsertChain extends SqlStatement implements ChainInsert {
|
|
5
|
+
private sql;
|
|
6
|
+
constructor(sql: string);
|
|
7
|
+
values(statement: Constructable<string | readonly string[]>): ChainInsertAfterValues;
|
|
8
|
+
select(statement: Constructable<string>): ChainInsertAfterValues;
|
|
9
|
+
returning<R extends {}>(returns: Constructable<SelectParam | "*">): SqlStatementDataset<R>;
|
|
10
|
+
onConflict(onConflict: Constructable<readonly string[] | string>): ChainAfterConflict;
|
|
11
|
+
where(where: Constructable<ConditionParam | void>): ChainInsertReturning;
|
|
12
|
+
genSql(): string;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=insert_impl.d.ts.map
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { selectColumns } from '../util.js';
|
|
2
|
+
import { whereToString, createUpdateSetFromObject } from '../_statement.js';
|
|
3
|
+
import { SqlStatement, SqlTextStatementDataset } from '../SqlStatement.js';
|
|
4
|
+
|
|
5
|
+
class InsertChain extends SqlStatement {
|
|
6
|
+
sql;
|
|
7
|
+
constructor(sql) {
|
|
8
|
+
super();
|
|
9
|
+
this.sql = sql;
|
|
10
|
+
}
|
|
11
|
+
values(statement) {
|
|
12
|
+
if (typeof statement === "function")
|
|
13
|
+
statement = statement();
|
|
14
|
+
switch (typeof statement) {
|
|
15
|
+
case "object":
|
|
16
|
+
if (statement instanceof Array) {
|
|
17
|
+
statement = statement.join(",\n");
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
throw new TypeError("参数 statement 类型错误");
|
|
21
|
+
}
|
|
22
|
+
break;
|
|
23
|
+
case "string":
|
|
24
|
+
break;
|
|
25
|
+
default:
|
|
26
|
+
throw new TypeError("参数 statement 类型错误");
|
|
27
|
+
}
|
|
28
|
+
return new InsertChain(this.sql + `\nVALUES\n${statement}`);
|
|
29
|
+
}
|
|
30
|
+
select(statement) {
|
|
31
|
+
if (typeof statement === "function")
|
|
32
|
+
statement = statement();
|
|
33
|
+
return new InsertChain(this.sql + `\n${statement}`);
|
|
34
|
+
}
|
|
35
|
+
returning(returns) {
|
|
36
|
+
if (typeof returns === "function")
|
|
37
|
+
returns = returns();
|
|
38
|
+
let columnsStr;
|
|
39
|
+
if (returns === "*") {
|
|
40
|
+
columnsStr = "*";
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
columnsStr = selectColumns(returns);
|
|
44
|
+
}
|
|
45
|
+
let sql = this.genSql() + "\nRETURNING " + columnsStr;
|
|
46
|
+
return new SqlTextStatementDataset(sql);
|
|
47
|
+
}
|
|
48
|
+
onConflict(onConflict) {
|
|
49
|
+
if (typeof onConflict === "function")
|
|
50
|
+
onConflict = onConflict();
|
|
51
|
+
if (typeof onConflict !== "string")
|
|
52
|
+
onConflict = onConflict.join(",");
|
|
53
|
+
let sql = this.genSql() + `\nON CONFLICT (${onConflict})`;
|
|
54
|
+
return new InsertAfterOnConflict(sql);
|
|
55
|
+
}
|
|
56
|
+
where(where) {
|
|
57
|
+
const sql = whereToString(where);
|
|
58
|
+
return new InsertChain(this.genSql() + sql);
|
|
59
|
+
}
|
|
60
|
+
genSql() {
|
|
61
|
+
return this.sql;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
class InsertAfterOnConflict {
|
|
65
|
+
sql;
|
|
66
|
+
constructor(sql) {
|
|
67
|
+
this.sql = sql;
|
|
68
|
+
}
|
|
69
|
+
doUpdate(set) {
|
|
70
|
+
if (typeof set === "function")
|
|
71
|
+
set = set();
|
|
72
|
+
let sql;
|
|
73
|
+
switch (typeof set) {
|
|
74
|
+
case "object": {
|
|
75
|
+
if (set instanceof Array) {
|
|
76
|
+
sql = "\nDO UPDATE SET " + set.join(", ");
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
sql = "\nDO UPDATE " + createUpdateSetFromObject(set);
|
|
80
|
+
}
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
case "string":
|
|
84
|
+
sql = "\nDO UPDATE " + set;
|
|
85
|
+
break;
|
|
86
|
+
default:
|
|
87
|
+
throw new TypeError("参数 set 类型错误");
|
|
88
|
+
}
|
|
89
|
+
return new InsertChain(this.sql + sql);
|
|
90
|
+
}
|
|
91
|
+
doNotThing() {
|
|
92
|
+
return new InsertChain(this.sql + " DO NOTHING");
|
|
93
|
+
}
|
|
94
|
+
toString() {
|
|
95
|
+
return this.sql;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export { InsertChain };
|
|
@@ -1,38 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ChainSelect, OrderByParam } from "./select_chain.ts";
|
|
1
|
+
import { SelectSqlGenerator } from "./select_chain.ts";
|
|
3
2
|
/** @public */
|
|
4
|
-
export declare
|
|
5
|
-
|
|
6
|
-
export declare function select<T extends TableType>(columns: "*"): ChainSelect<T>;
|
|
7
|
-
/** @public */
|
|
8
|
-
export declare function select<T extends TableType>(columns: Constructable<{
|
|
9
|
-
[key in keyof T]: string | boolean;
|
|
10
|
-
}>): ChainSelect<T>;
|
|
11
|
-
/** @public */
|
|
12
|
-
export declare function select<T extends TableType>(columns: Constructable<string | string[]>): ChainSelect<T>;
|
|
13
|
-
/** @public */
|
|
14
|
-
export declare function select<T extends TableType>(columns: Constructable<string | readonly string[] | {
|
|
15
|
-
readonly [key in keyof T]: string | boolean;
|
|
16
|
-
}>): ChainSelect<T>;
|
|
17
|
-
/**
|
|
18
|
-
* 生成 ORDER BY 语句
|
|
19
|
-
* @public
|
|
20
|
-
* @example
|
|
21
|
-
* ```ts
|
|
22
|
-
*
|
|
23
|
-
* orderBy([]) // ""
|
|
24
|
-
* orderBy({}) // ""
|
|
25
|
-
* orderBy() // ""
|
|
26
|
-
*
|
|
27
|
-
* // 以下生成 "\nORDER BY age DESC NULLS FIRST,num ASC"
|
|
28
|
-
* orderBy("age DESC NULLS FIRST,num ASC");
|
|
29
|
-
* orderBy(["age DESC NULLS FIRST", "num ASC"]);
|
|
30
|
-
* orderBy([
|
|
31
|
-
* { key: "age", asc: false, nullLast: false },
|
|
32
|
-
* { key: "num", asc: true },
|
|
33
|
-
* ]);
|
|
34
|
-
*
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
export declare function orderBy(by?: Constructable<OrderByParam | void>): string;
|
|
3
|
+
export declare const select: SelectSqlGenerator;
|
|
4
|
+
export { orderBy } from "./select_impl.ts";
|
|
38
5
|
//# sourceMappingURL=select.d.ts.map
|
|
@@ -1,154 +1,14 @@
|
|
|
1
1
|
import { selectColumns } from '../util.js';
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import { SelectChainAfterSelect } from './select_impl.js';
|
|
3
|
+
export { orderBy } from './select_impl.js';
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
/** @public */
|
|
6
|
+
const select = function select(columns) {
|
|
6
7
|
if (!columns)
|
|
7
8
|
return new SelectChainAfterSelect("SELECT ");
|
|
8
9
|
if (typeof columns === "function")
|
|
9
10
|
columns = columns();
|
|
10
11
|
return new SelectChainAfterSelect("SELECT " + selectColumns(columns));
|
|
11
|
-
}
|
|
12
|
-
class SelectChainAfterSelect {
|
|
13
|
-
constructor(sql) {
|
|
14
|
-
this.#sql = sql;
|
|
15
|
-
}
|
|
16
|
-
#sql;
|
|
17
|
-
from(selectable, option = {}) {
|
|
18
|
-
let sql = this.#sql + "\nFROM " + selectableToString(selectable, option.as);
|
|
19
|
-
return new SelectChain(sql);
|
|
20
|
-
}
|
|
21
|
-
toString() {
|
|
22
|
-
return this.#sql;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
class SelectChain extends SqlTextStatementDataset {
|
|
26
|
-
constructor(sql) {
|
|
27
|
-
super(sql);
|
|
28
|
-
}
|
|
29
|
-
from(selectable, option = {}) {
|
|
30
|
-
let sql = this.genSql() + ", " + selectableToString(selectable, option.as);
|
|
31
|
-
return new SelectChain(sql);
|
|
32
|
-
}
|
|
33
|
-
fullJoin(selectable, options) {
|
|
34
|
-
return joinToString(this.genSql() + "\nFULL JOIN ", selectable, options);
|
|
35
|
-
}
|
|
36
|
-
innerJoin(selectable, options) {
|
|
37
|
-
return joinToString(this.genSql() + "\nINNER JOIN ", selectable, options);
|
|
38
|
-
}
|
|
39
|
-
leftJoin(selectable, options) {
|
|
40
|
-
return joinToString(this.genSql() + "\nLEFT JOIN ", selectable, options);
|
|
41
|
-
}
|
|
42
|
-
rightJoin(selectable, options) {
|
|
43
|
-
return joinToString(this.genSql() + "\nRIGHT JOIN ", selectable, options);
|
|
44
|
-
}
|
|
45
|
-
naturalJoin(selectable, options) {
|
|
46
|
-
return joinToString(this.genSql() + "\nNATURAL JOIN ", selectable, options);
|
|
47
|
-
}
|
|
48
|
-
crossJoin(selectable, options) {
|
|
49
|
-
return joinToString(this.genSql() + "\nCROSS JOIN ", selectable, options);
|
|
50
|
-
}
|
|
51
|
-
where(param) {
|
|
52
|
-
return new SelectChain(this.genSql() + whereToString(param));
|
|
53
|
-
}
|
|
54
|
-
groupBy(columns) {
|
|
55
|
-
let add;
|
|
56
|
-
if (typeof columns === "string")
|
|
57
|
-
add = columns;
|
|
58
|
-
else if (columns)
|
|
59
|
-
add = columns.join(",");
|
|
60
|
-
if (!add)
|
|
61
|
-
return new SelectChain(this.genSql());
|
|
62
|
-
return new SelectChain(this.genSql() + "\nGROUP BY " + add);
|
|
63
|
-
}
|
|
64
|
-
having(param) {
|
|
65
|
-
return new SelectChain(this.genSql() + havingToString(param));
|
|
66
|
-
}
|
|
67
|
-
orderBy(param) {
|
|
68
|
-
return new SelectChain(this.genSql() + orderBy(param));
|
|
69
|
-
}
|
|
70
|
-
limit(limit, offset) {
|
|
71
|
-
let sql = this.genSql();
|
|
72
|
-
let type;
|
|
73
|
-
type = typeof limit;
|
|
74
|
-
if (type === "number" || type === "bigint")
|
|
75
|
-
sql += "\nLIMIT " + limit;
|
|
76
|
-
type = typeof offset;
|
|
77
|
-
if (type === "number" || type === "bigint")
|
|
78
|
-
sql += "\nOFFSET " + offset;
|
|
79
|
-
return new SqlTextStatementDataset(sql);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
function joinToString(sql, selectable, options = {}) {
|
|
83
|
-
sql += selectableToString(selectable, options.as);
|
|
84
|
-
if (options.on) {
|
|
85
|
-
let on = options.on;
|
|
86
|
-
if (typeof on === "function")
|
|
87
|
-
on = on();
|
|
88
|
-
sql += " ON " + condition(on);
|
|
89
|
-
}
|
|
90
|
-
return new SelectChain(sql);
|
|
91
|
-
}
|
|
92
|
-
function havingToString(conditions, type) {
|
|
93
|
-
const sql = condition(conditions, type);
|
|
94
|
-
if (sql)
|
|
95
|
-
return "\nHAVING " + sql;
|
|
96
|
-
return "";
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* 生成 ORDER BY 语句
|
|
100
|
-
* @public
|
|
101
|
-
* @example
|
|
102
|
-
* ```ts
|
|
103
|
-
*
|
|
104
|
-
* orderBy([]) // ""
|
|
105
|
-
* orderBy({}) // ""
|
|
106
|
-
* orderBy() // ""
|
|
107
|
-
*
|
|
108
|
-
* // 以下生成 "\nORDER BY age DESC NULLS FIRST,num ASC"
|
|
109
|
-
* orderBy("age DESC NULLS FIRST,num ASC");
|
|
110
|
-
* orderBy(["age DESC NULLS FIRST", "num ASC"]);
|
|
111
|
-
* orderBy([
|
|
112
|
-
* { key: "age", asc: false, nullLast: false },
|
|
113
|
-
* { key: "num", asc: true },
|
|
114
|
-
* ]);
|
|
115
|
-
*
|
|
116
|
-
* ```
|
|
117
|
-
*/
|
|
118
|
-
function orderBy(by) {
|
|
119
|
-
if (typeof by === "function")
|
|
120
|
-
by = by();
|
|
121
|
-
let sql = "";
|
|
122
|
-
if (!by)
|
|
123
|
-
return sql;
|
|
124
|
-
if (typeof by === "string") {
|
|
125
|
-
sql += "\nORDER BY " + by;
|
|
126
|
-
}
|
|
127
|
-
else if (by instanceof Array) {
|
|
128
|
-
if (by.length) {
|
|
129
|
-
sql += "\nORDER BY " + handlerOrderValue(by[0]);
|
|
130
|
-
for (let i = 1; i < by.length; i++)
|
|
131
|
-
sql += "," + handlerOrderValue(by[i]);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
sql += "\nORDER BY " + handlerOrderValue(by);
|
|
136
|
-
}
|
|
137
|
-
return sql;
|
|
138
|
-
}
|
|
139
|
-
function handlerOrderValue(value) {
|
|
140
|
-
if (typeof value === "string")
|
|
141
|
-
return value;
|
|
142
|
-
let target;
|
|
143
|
-
if (value.target) {
|
|
144
|
-
target = value.target;
|
|
145
|
-
}
|
|
146
|
-
else {
|
|
147
|
-
target = value.asc ? "ASC" : "DESC";
|
|
148
|
-
if (value.nullLast !== undefined)
|
|
149
|
-
target += value.nullLast ? " NULLS LAST" : " NULLS FIRST";
|
|
150
|
-
}
|
|
151
|
-
return value.key + " " + target;
|
|
152
|
-
}
|
|
12
|
+
};
|
|
153
13
|
|
|
154
|
-
export {
|
|
14
|
+
export { select };
|
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import { ConditionParam, Constructable, TableType } from "../util.ts";
|
|
2
2
|
import { SqlSelectable, SqlStatementDataset } from "../SqlStatement.ts";
|
|
3
3
|
/** @public */
|
|
4
|
+
export interface SelectSqlGenerator {
|
|
5
|
+
(columns?: undefined | ""): ChainSelect<{}>;
|
|
6
|
+
<T extends TableType>(columns: "*"): ChainSelect<T>;
|
|
7
|
+
<T extends TableType>(columns: Constructable<{
|
|
8
|
+
[key in keyof T]: string | boolean;
|
|
9
|
+
}>): ChainSelect<T>;
|
|
10
|
+
<T extends TableType>(columns: Constructable<string | string[]>): ChainSelect<T>;
|
|
11
|
+
<T extends TableType>(columns: Constructable<string | readonly string[] | {
|
|
12
|
+
readonly [key in keyof T]: string | boolean;
|
|
13
|
+
}>): ChainSelect<T>;
|
|
14
|
+
}
|
|
15
|
+
/** @public */
|
|
4
16
|
export type SelectAsNameOption = {
|
|
5
17
|
as?: string;
|
|
6
18
|
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Constructable, TableType } from "../util.ts";
|
|
2
|
+
import { SqlSelectable } from "../SqlStatement.ts";
|
|
3
|
+
import { ChainSelectAfterFirstFrom, ChainSelect, SelectAsNameOption, OrderByParam } from "./select_chain.ts";
|
|
4
|
+
export declare class SelectChainAfterSelect<T extends TableType> implements ChainSelect<T> {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(sql: string);
|
|
7
|
+
from(selectable: Constructable<SqlSelectable | string>, option?: SelectAsNameOption): ChainSelectAfterFirstFrom<T>;
|
|
8
|
+
toString(): string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* 生成 ORDER BY 语句
|
|
12
|
+
* @public
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
*
|
|
16
|
+
* orderBy([]) // ""
|
|
17
|
+
* orderBy({}) // ""
|
|
18
|
+
* orderBy() // ""
|
|
19
|
+
*
|
|
20
|
+
* // 以下生成 "\nORDER BY age DESC NULLS FIRST,num ASC"
|
|
21
|
+
* orderBy("age DESC NULLS FIRST,num ASC");
|
|
22
|
+
* orderBy(["age DESC NULLS FIRST", "num ASC"]);
|
|
23
|
+
* orderBy([
|
|
24
|
+
* { key: "age", asc: false, nullLast: false },
|
|
25
|
+
* { key: "num", asc: true },
|
|
26
|
+
* ]);
|
|
27
|
+
*
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function orderBy(by?: Constructable<OrderByParam | void>): string;
|
|
31
|
+
//# sourceMappingURL=select_impl.d.ts.map
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { selectableToString, whereToString, condition } from '../_statement.js';
|
|
2
|
+
import { SqlTextStatementDataset } from '../SqlStatement.js';
|
|
3
|
+
|
|
4
|
+
class SelectChainAfterSelect {
|
|
5
|
+
constructor(sql) {
|
|
6
|
+
this.#sql = sql;
|
|
7
|
+
}
|
|
8
|
+
#sql;
|
|
9
|
+
from(selectable, option = {}) {
|
|
10
|
+
let sql = this.#sql + "\nFROM " + selectableToString(selectable, option.as);
|
|
11
|
+
return new SelectChain(sql);
|
|
12
|
+
}
|
|
13
|
+
toString() {
|
|
14
|
+
return this.#sql;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
class SelectChain extends SqlTextStatementDataset {
|
|
18
|
+
constructor(sql) {
|
|
19
|
+
super(sql);
|
|
20
|
+
}
|
|
21
|
+
from(selectable, option = {}) {
|
|
22
|
+
let sql = this.genSql() + ", " + selectableToString(selectable, option.as);
|
|
23
|
+
return new SelectChain(sql);
|
|
24
|
+
}
|
|
25
|
+
fullJoin(selectable, options) {
|
|
26
|
+
return joinToString(this.genSql() + "\nFULL JOIN ", selectable, options);
|
|
27
|
+
}
|
|
28
|
+
innerJoin(selectable, options) {
|
|
29
|
+
return joinToString(this.genSql() + "\nINNER JOIN ", selectable, options);
|
|
30
|
+
}
|
|
31
|
+
leftJoin(selectable, options) {
|
|
32
|
+
return joinToString(this.genSql() + "\nLEFT JOIN ", selectable, options);
|
|
33
|
+
}
|
|
34
|
+
rightJoin(selectable, options) {
|
|
35
|
+
return joinToString(this.genSql() + "\nRIGHT JOIN ", selectable, options);
|
|
36
|
+
}
|
|
37
|
+
naturalJoin(selectable, options) {
|
|
38
|
+
return joinToString(this.genSql() + "\nNATURAL JOIN ", selectable, options);
|
|
39
|
+
}
|
|
40
|
+
crossJoin(selectable, options) {
|
|
41
|
+
return joinToString(this.genSql() + "\nCROSS JOIN ", selectable, options);
|
|
42
|
+
}
|
|
43
|
+
where(param) {
|
|
44
|
+
return new SelectChain(this.genSql() + whereToString(param));
|
|
45
|
+
}
|
|
46
|
+
groupBy(columns) {
|
|
47
|
+
let add;
|
|
48
|
+
if (typeof columns === "string")
|
|
49
|
+
add = columns;
|
|
50
|
+
else if (columns)
|
|
51
|
+
add = columns.join(",");
|
|
52
|
+
if (!add)
|
|
53
|
+
return new SelectChain(this.genSql());
|
|
54
|
+
return new SelectChain(this.genSql() + "\nGROUP BY " + add);
|
|
55
|
+
}
|
|
56
|
+
having(param) {
|
|
57
|
+
return new SelectChain(this.genSql() + havingToString(param));
|
|
58
|
+
}
|
|
59
|
+
orderBy(param) {
|
|
60
|
+
return new SelectChain(this.genSql() + orderBy(param));
|
|
61
|
+
}
|
|
62
|
+
limit(limit, offset) {
|
|
63
|
+
let sql = this.genSql();
|
|
64
|
+
let type;
|
|
65
|
+
type = typeof limit;
|
|
66
|
+
if (type === "number" || type === "bigint")
|
|
67
|
+
sql += "\nLIMIT " + limit;
|
|
68
|
+
type = typeof offset;
|
|
69
|
+
if (type === "number" || type === "bigint")
|
|
70
|
+
sql += "\nOFFSET " + offset;
|
|
71
|
+
return new SqlTextStatementDataset(sql);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function joinToString(sql, selectable, options = {}) {
|
|
75
|
+
sql += selectableToString(selectable, options.as);
|
|
76
|
+
if (options.on) {
|
|
77
|
+
let on = options.on;
|
|
78
|
+
if (typeof on === "function")
|
|
79
|
+
on = on();
|
|
80
|
+
sql += " ON " + condition(on);
|
|
81
|
+
}
|
|
82
|
+
return new SelectChain(sql);
|
|
83
|
+
}
|
|
84
|
+
function havingToString(conditions, type) {
|
|
85
|
+
const sql = condition(conditions, type);
|
|
86
|
+
if (sql)
|
|
87
|
+
return "\nHAVING " + sql;
|
|
88
|
+
return "";
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 生成 ORDER BY 语句
|
|
92
|
+
* @public
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
*
|
|
96
|
+
* orderBy([]) // ""
|
|
97
|
+
* orderBy({}) // ""
|
|
98
|
+
* orderBy() // ""
|
|
99
|
+
*
|
|
100
|
+
* // 以下生成 "\nORDER BY age DESC NULLS FIRST,num ASC"
|
|
101
|
+
* orderBy("age DESC NULLS FIRST,num ASC");
|
|
102
|
+
* orderBy(["age DESC NULLS FIRST", "num ASC"]);
|
|
103
|
+
* orderBy([
|
|
104
|
+
* { key: "age", asc: false, nullLast: false },
|
|
105
|
+
* { key: "num", asc: true },
|
|
106
|
+
* ]);
|
|
107
|
+
*
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
function orderBy(by) {
|
|
111
|
+
if (typeof by === "function")
|
|
112
|
+
by = by();
|
|
113
|
+
let sql = "";
|
|
114
|
+
if (!by)
|
|
115
|
+
return sql;
|
|
116
|
+
if (typeof by === "string") {
|
|
117
|
+
sql += "\nORDER BY " + by;
|
|
118
|
+
}
|
|
119
|
+
else if (by instanceof Array) {
|
|
120
|
+
if (by.length) {
|
|
121
|
+
sql += "\nORDER BY " + handlerOrderValue(by[0]);
|
|
122
|
+
for (let i = 1; i < by.length; i++)
|
|
123
|
+
sql += "," + handlerOrderValue(by[i]);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
sql += "\nORDER BY " + handlerOrderValue(by);
|
|
128
|
+
}
|
|
129
|
+
return sql;
|
|
130
|
+
}
|
|
131
|
+
function handlerOrderValue(value) {
|
|
132
|
+
if (typeof value === "string")
|
|
133
|
+
return value;
|
|
134
|
+
let target;
|
|
135
|
+
if (value.target) {
|
|
136
|
+
target = value.target;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
target = value.asc ? "ASC" : "DESC";
|
|
140
|
+
if (value.nullLast !== undefined)
|
|
141
|
+
target += value.nullLast ? " NULLS LAST" : " NULLS FIRST";
|
|
142
|
+
}
|
|
143
|
+
return value.key + " " + target;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export { SelectChainAfterSelect, orderBy };
|
|
@@ -1,17 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ChainUpdate } from "./update_chain.ts";
|
|
1
|
+
import { UpdateSqlGenerator } from "./update_chain.ts";
|
|
3
2
|
/** @public */
|
|
4
|
-
export
|
|
5
|
-
as?: string;
|
|
6
|
-
}
|
|
7
|
-
/**
|
|
8
|
-
* @public
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* update("table1 AS t").where("t.id = b.id") // UPDATE table1 AS t WHERE t.id = b.id
|
|
12
|
-
* update("table1",{ as: "t" }).set({ k:"'v'"}) // UPDATE table1 AS t SET t.k = 'v'
|
|
13
|
-
* update("table1").where("id = 1") // UPDATE table1 AS t WHERE id = 1
|
|
14
|
-
* ```
|
|
15
|
-
*/
|
|
16
|
-
export declare function update<T extends TableType>(table: string, options?: UpdateOption): ChainUpdate<T>;
|
|
3
|
+
export declare const update: UpdateSqlGenerator;
|
|
17
4
|
//# sourceMappingURL=update.d.ts.map
|
|
@@ -1,88 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { SqlStatement, SqlTextStatementDataset } from '../SqlStatement.js';
|
|
3
|
-
import { createUpdateSetFromObject, whereToString } from '../_statement.js';
|
|
1
|
+
import { UpdateChain } from './update_impl.js';
|
|
4
2
|
|
|
5
|
-
/**
|
|
6
|
-
|
|
7
|
-
* @example
|
|
8
|
-
* ```ts
|
|
9
|
-
* update("table1 AS t").where("t.id = b.id") // UPDATE table1 AS t WHERE t.id = b.id
|
|
10
|
-
* update("table1",{ as: "t" }).set({ k:"'v'"}) // UPDATE table1 AS t SET t.k = 'v'
|
|
11
|
-
* update("table1").where("id = 1") // UPDATE table1 AS t WHERE id = 1
|
|
12
|
-
* ```
|
|
13
|
-
*/
|
|
14
|
-
function update(table, options) {
|
|
3
|
+
/** @public */
|
|
4
|
+
const update = function update(table, options) {
|
|
15
5
|
let sql = `UPDATE ${table}`;
|
|
16
6
|
if (options?.as) {
|
|
17
7
|
sql += ` AS ${options.as}`;
|
|
18
8
|
}
|
|
19
9
|
return new UpdateChain(sql);
|
|
20
|
-
}
|
|
21
|
-
class UpdateChain extends SqlStatement {
|
|
22
|
-
sql;
|
|
23
|
-
constructor(sql) {
|
|
24
|
-
super();
|
|
25
|
-
this.sql = sql;
|
|
26
|
-
}
|
|
27
|
-
from(...from) {
|
|
28
|
-
const textList = from.map((f, i) => {
|
|
29
|
-
if (typeof f === "function")
|
|
30
|
-
return f();
|
|
31
|
-
return f;
|
|
32
|
-
});
|
|
33
|
-
const sql = this.genSql() + `\nFROM ${textList.join(", ")}`;
|
|
34
|
-
return new UpdateChain(sql);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* @example
|
|
38
|
-
* ```ts
|
|
39
|
-
* // SET age=3, name='hi', count=b.count
|
|
40
|
-
* set({age: "3", name: "'hi'", count:"b.count"})
|
|
41
|
-
* set(["age = 3", "name = 'hi'", "count = b.count"])
|
|
42
|
-
* set("age = 3, name = 'hi', count = b.count")
|
|
43
|
-
*
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
|
-
set(values) {
|
|
47
|
-
if (typeof values === "function")
|
|
48
|
-
values = values();
|
|
49
|
-
switch (typeof values) {
|
|
50
|
-
case "object": {
|
|
51
|
-
if (values instanceof Array) {
|
|
52
|
-
let sql = values.join(", ");
|
|
53
|
-
return new UpdateChain(this.sql + " " + sql);
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
let sql = createUpdateSetFromObject(values);
|
|
57
|
-
return new UpdateChain(this.sql + " " + sql);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
case "string":
|
|
61
|
-
return new UpdateChain(this.sql + " SET\n" + values);
|
|
62
|
-
default:
|
|
63
|
-
throw new TypeError("参数 values 类型错误");
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
returning(returns) {
|
|
67
|
-
if (typeof returns === "function")
|
|
68
|
-
returns = returns();
|
|
69
|
-
let columnsStr;
|
|
70
|
-
if (returns === "*") {
|
|
71
|
-
columnsStr = "*";
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
columnsStr = selectColumns(returns);
|
|
75
|
-
}
|
|
76
|
-
let sql = this.genSql() + "\nRETURNING " + columnsStr;
|
|
77
|
-
return new SqlTextStatementDataset(sql);
|
|
78
|
-
}
|
|
79
|
-
where(where) {
|
|
80
|
-
const sql = whereToString(where);
|
|
81
|
-
return new UpdateChain(this.genSql() + sql);
|
|
82
|
-
}
|
|
83
|
-
genSql() {
|
|
84
|
-
return this.sql;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
10
|
+
};
|
|
87
11
|
|
|
88
12
|
export { update };
|
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
import { ConditionParam, Constructable, TableType } from "../util.ts";
|
|
2
2
|
import { ChainModifyReturning } from "./_modify.ts";
|
|
3
3
|
/** @public */
|
|
4
|
+
export interface UpdateOption {
|
|
5
|
+
as?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* @public
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* update("table1 AS t").where("t.id = b.id") // UPDATE table1 AS t WHERE t.id = b.id
|
|
12
|
+
* update("table1",{ as: "t" }).set({ k:"'v'"}) // UPDATE table1 AS t SET t.k = 'v'
|
|
13
|
+
* update("table1").where("id = 1") // UPDATE table1 AS t WHERE id = 1
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export interface UpdateSqlGenerator {
|
|
17
|
+
<T extends TableType>(table: string, options?: UpdateOption): ChainUpdate<T>;
|
|
18
|
+
}
|
|
19
|
+
/** @public */
|
|
4
20
|
export interface ChainUpdate<T extends TableType = TableType> {
|
|
5
21
|
set(value: Constructable<{
|
|
6
22
|
[key in keyof T]?: string;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ConditionParam, Constructable, SelectParam } from "../util.ts";
|
|
2
|
+
import { SqlStatementDataset, SqlStatement } from "../SqlStatement.ts";
|
|
3
|
+
import { ChainUpdate, ChainUpdateAfterForm, ChainUpdateAfterSet, ChainUpdateReturning } from "./update_chain.ts";
|
|
4
|
+
export declare class UpdateChain extends SqlStatement implements ChainUpdate {
|
|
5
|
+
private sql;
|
|
6
|
+
constructor(sql: string);
|
|
7
|
+
from(...from: Constructable<string>[]): ChainUpdateAfterForm;
|
|
8
|
+
/**
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* // SET age=3, name='hi', count=b.count
|
|
12
|
+
* set({age: "3", name: "'hi'", count:"b.count"})
|
|
13
|
+
* set(["age = 3", "name = 'hi'", "count = b.count"])
|
|
14
|
+
* set("age = 3, name = 'hi', count = b.count")
|
|
15
|
+
*
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
set(values: Constructable<Record<string, string | undefined> | string[] | string>): ChainUpdateAfterSet;
|
|
19
|
+
returning<R extends {}>(returns: Constructable<SelectParam | "*">): SqlStatementDataset<R>;
|
|
20
|
+
where(where: Constructable<ConditionParam | void>): ChainUpdateReturning;
|
|
21
|
+
genSql(): string;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=update_impl.d.ts.map
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { selectColumns } from '../util.js';
|
|
2
|
+
import { SqlStatement, SqlTextStatementDataset } from '../SqlStatement.js';
|
|
3
|
+
import { createUpdateSetFromObject, whereToString } from '../_statement.js';
|
|
4
|
+
|
|
5
|
+
class UpdateChain extends SqlStatement {
|
|
6
|
+
sql;
|
|
7
|
+
constructor(sql) {
|
|
8
|
+
super();
|
|
9
|
+
this.sql = sql;
|
|
10
|
+
}
|
|
11
|
+
from(...from) {
|
|
12
|
+
const textList = from.map((f, i) => {
|
|
13
|
+
if (typeof f === "function")
|
|
14
|
+
return f();
|
|
15
|
+
return f;
|
|
16
|
+
});
|
|
17
|
+
const sql = this.genSql() + `\nFROM ${textList.join(", ")}`;
|
|
18
|
+
return new UpdateChain(sql);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* // SET age=3, name='hi', count=b.count
|
|
24
|
+
* set({age: "3", name: "'hi'", count:"b.count"})
|
|
25
|
+
* set(["age = 3", "name = 'hi'", "count = b.count"])
|
|
26
|
+
* set("age = 3, name = 'hi', count = b.count")
|
|
27
|
+
*
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
set(values) {
|
|
31
|
+
if (typeof values === "function")
|
|
32
|
+
values = values();
|
|
33
|
+
switch (typeof values) {
|
|
34
|
+
case "object": {
|
|
35
|
+
if (values instanceof Array) {
|
|
36
|
+
let sql = values.join(", ");
|
|
37
|
+
return new UpdateChain(this.sql + " " + sql);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
let sql = createUpdateSetFromObject(values);
|
|
41
|
+
return new UpdateChain(this.sql + " " + sql);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
case "string":
|
|
45
|
+
return new UpdateChain(this.sql + " SET\n" + values);
|
|
46
|
+
default:
|
|
47
|
+
throw new TypeError("参数 values 类型错误");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
returning(returns) {
|
|
51
|
+
if (typeof returns === "function")
|
|
52
|
+
returns = returns();
|
|
53
|
+
let columnsStr;
|
|
54
|
+
if (returns === "*") {
|
|
55
|
+
columnsStr = "*";
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
columnsStr = selectColumns(returns);
|
|
59
|
+
}
|
|
60
|
+
let sql = this.genSql() + "\nRETURNING " + columnsStr;
|
|
61
|
+
return new SqlTextStatementDataset(sql);
|
|
62
|
+
}
|
|
63
|
+
where(where) {
|
|
64
|
+
const sql = whereToString(where);
|
|
65
|
+
return new UpdateChain(this.genSql() + sql);
|
|
66
|
+
}
|
|
67
|
+
genSql() {
|
|
68
|
+
return this.sql;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export { UpdateChain };
|