@asla/yoursql 0.9.2 → 0.9.3
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/sql_gen/mod.d.ts +1 -0
- package/dist/sql_gen/mod.js +1 -0
- package/dist/sql_gen/select/DbTable.d.ts +7 -3
- package/dist/sql_gen/select/DbTable.js +13 -5
- package/dist/sql_gen/select/TableQuery.d.ts +1 -1
- package/dist/sql_gen/select/TableQuery.js +8 -3
- package/dist/sql_gen/select/_statement.d.ts +1 -1
- package/dist/sql_gen/select/_statement.js +6 -3
- package/dist/sql_gen/select/chain_modify.d.ts +39 -4
- package/dist/sql_gen/select/cte.d.ts +44 -0
- package/dist/sql_gen/select/cte.js +38 -0
- package/dist/sql_gen/select/query_chain_insert.d.ts +2 -0
- package/dist/sql_gen/select/query_chain_insert.js +18 -0
- package/package.json +1 -1
package/dist/sql_gen/mod.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export * from "./select/chain_base.ts";
|
|
|
7
7
|
export * from "./select/chain_modify.ts";
|
|
8
8
|
export * from "./select/chain_select.ts";
|
|
9
9
|
export * from "./select/TableQuery.ts";
|
|
10
|
+
export * from "./select/cte.ts";
|
|
10
11
|
export * from "./util.ts";
|
|
11
12
|
export * from "./your_table/mod.ts";
|
|
12
13
|
import { SqlValueFn } from "./sql_value/sql_value.ts";
|
package/dist/sql_gen/mod.js
CHANGED
|
@@ -4,6 +4,7 @@ export { DbTable } from './select/DbTable.js';
|
|
|
4
4
|
export { Selection, SqlSelectChain } from './select/query_chain_select.js';
|
|
5
5
|
export { SqlStatement, SqlStatementDataset, SqlTextStatementDataset } from './select/chain_base.js';
|
|
6
6
|
export { DbTableQuery } from './select/TableQuery.js';
|
|
7
|
+
export { withAs, withRecursiveAs } from './select/cte.js';
|
|
7
8
|
export { getObjectListKeys, having, orderBy, selectColumns, where } from './util.js';
|
|
8
9
|
export { TypeChecker } from './your_table/checker.js';
|
|
9
10
|
export { ColumnMeta, CustomDbType, YourTypeMap } from './your_table/infer_db_type.js';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ConditionParam, Constructable } from "../util.ts";
|
|
2
2
|
import type { TableType } from "./type.ts";
|
|
3
3
|
import { Selection } from "./query_chain_select.ts";
|
|
4
|
-
import { ChainDelete, ChainInsert, ChainUpdate } from "./chain_modify.ts";
|
|
4
|
+
import { ChainDelete, ChainInsert, ChainModifyReturning, ChainUpdate } from "./chain_modify.ts";
|
|
5
5
|
import { ChainSelect } from "./chain_select.ts";
|
|
6
6
|
/**
|
|
7
7
|
* 数据库表
|
|
@@ -43,12 +43,16 @@ export declare class DbTable<T extends TableType> {
|
|
|
43
43
|
*/
|
|
44
44
|
update(values: Constructable<{
|
|
45
45
|
[key in keyof T]?: string;
|
|
46
|
-
} | string
|
|
46
|
+
} | string>, asName?: string): ChainUpdate<T>;
|
|
47
|
+
/** @deprecated 改用 delete().where */
|
|
48
|
+
delete(option: {
|
|
49
|
+
where?: Constructable<ConditionParam | void>;
|
|
50
|
+
}): ChainModifyReturning<T>;
|
|
47
51
|
delete(option?: DeleteOption): ChainDelete<T>;
|
|
48
52
|
toSelect(): string;
|
|
49
53
|
}
|
|
50
54
|
/** @public */
|
|
51
55
|
export interface DeleteOption {
|
|
52
|
-
|
|
56
|
+
asName?: string;
|
|
53
57
|
}
|
|
54
58
|
//# sourceMappingURL=DbTable.d.ts.map
|
|
@@ -43,23 +43,31 @@ class DbTable {
|
|
|
43
43
|
* table.update({age: "3", name: "'hi'", k1: undefined, k2: ""}) // "UPDATE table SET age=3, name='hi'"
|
|
44
44
|
* ```
|
|
45
45
|
*/
|
|
46
|
-
update(values) {
|
|
46
|
+
update(values, asName) {
|
|
47
47
|
if (typeof values === "function")
|
|
48
48
|
values = values();
|
|
49
|
+
let name = asName ? `${this.name} AS ${asName}` : this.name;
|
|
49
50
|
switch (typeof values) {
|
|
50
51
|
case "object": {
|
|
51
|
-
let sql = createUpdateSetFromObject(values);
|
|
52
|
-
return new SqlChainModify("UPDATE " +
|
|
52
|
+
let sql = createUpdateSetFromObject(values, asName);
|
|
53
|
+
return new SqlChainModify("UPDATE " + name + " " + sql);
|
|
53
54
|
}
|
|
54
55
|
case "string":
|
|
55
|
-
return new SqlChainModify("UPDATE " +
|
|
56
|
+
return new SqlChainModify("UPDATE " + name + " SET\n" + values);
|
|
56
57
|
default:
|
|
57
58
|
throw new TypeError("参数 values 错误");
|
|
58
59
|
}
|
|
59
60
|
}
|
|
60
61
|
delete(option = {}) {
|
|
61
62
|
let sql = "DELETE FROM " + this.name;
|
|
62
|
-
|
|
63
|
+
if (option.where) {
|
|
64
|
+
sql += where(option.where);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
if (option.asName) {
|
|
68
|
+
sql += ` AS ${option.asName}`;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
63
71
|
return new SqlChainModify(sql);
|
|
64
72
|
}
|
|
65
73
|
toSelect() {
|
|
@@ -23,6 +23,6 @@ export declare class DbTableQuery<T extends TableType = Record<string, any>, C e
|
|
|
23
23
|
* table.update({age:3, name:"hi"}, true) // "UPDATE table SET age=3, name='hi'"
|
|
24
24
|
* ```
|
|
25
25
|
*/
|
|
26
|
-
updateFrom(values: Constructable<UpdateRowValue<T
|
|
26
|
+
updateFrom(values: Constructable<UpdateRowValue<T>>, asName?: string): ChainUpdate<T>;
|
|
27
27
|
}
|
|
28
28
|
//# sourceMappingURL=TableQuery.d.ts.map
|
|
@@ -42,7 +42,7 @@ class DbTableQuery extends DbTable {
|
|
|
42
42
|
* table.update({age:3, name:"hi"}, true) // "UPDATE table SET age=3, name='hi'"
|
|
43
43
|
* ```
|
|
44
44
|
*/
|
|
45
|
-
updateFrom(values) {
|
|
45
|
+
updateFrom(values, asName) {
|
|
46
46
|
if (typeof values === "function")
|
|
47
47
|
values = values();
|
|
48
48
|
let setStr;
|
|
@@ -54,13 +54,18 @@ class DbTableQuery extends DbTable {
|
|
|
54
54
|
for (const [k, v] of updateKey) {
|
|
55
55
|
if (v === undefined)
|
|
56
56
|
continue;
|
|
57
|
-
|
|
57
|
+
if (asName) {
|
|
58
|
+
setList.push(`${asName}.${k}= ` + this.statement.toSqlStr(v));
|
|
59
|
+
}
|
|
60
|
+
else
|
|
61
|
+
setList.push(k + "= " + this.statement.toSqlStr(v));
|
|
58
62
|
}
|
|
59
63
|
setStr = setList.join(",\n");
|
|
60
64
|
}
|
|
61
65
|
if (!setStr)
|
|
62
66
|
throw new Error("值不能为空");
|
|
63
|
-
|
|
67
|
+
const name = asName ? `${this.name} AS ${asName}` : this.name;
|
|
68
|
+
let sql = `UPDATE ${name} SET\n${setStr}`;
|
|
64
69
|
return new SqlChainModify(sql);
|
|
65
70
|
}
|
|
66
71
|
}
|
|
@@ -4,6 +4,6 @@ type ConditionParam = string | string[];
|
|
|
4
4
|
* 生成条件语句
|
|
5
5
|
*/
|
|
6
6
|
export declare function condition(conditions?: Constructable<ConditionParam | void>, type?: "AND" | "OR"): string;
|
|
7
|
-
export declare function createUpdateSetFromObject(set: Record<string, string | undefined
|
|
7
|
+
export declare function createUpdateSetFromObject(set: Record<string, string | undefined>, prefix?: string): string;
|
|
8
8
|
export {};
|
|
9
9
|
//# sourceMappingURL=_statement.d.ts.map
|
|
@@ -17,20 +17,22 @@ function condition(conditions, type = "AND") {
|
|
|
17
17
|
return;
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
|
-
function createUpdateSetFromObject(set) {
|
|
20
|
+
function createUpdateSetFromObject(set, prefix) {
|
|
21
21
|
const updateKey = Object.keys(set);
|
|
22
22
|
let i = 0;
|
|
23
23
|
let key;
|
|
24
|
+
let sqlKey;
|
|
24
25
|
let value;
|
|
25
26
|
let sql;
|
|
26
27
|
for (; i < updateKey.length; i++) {
|
|
27
28
|
key = updateKey[i];
|
|
29
|
+
sqlKey = prefix ? `${prefix}.${key}` : key;
|
|
28
30
|
value = set[key];
|
|
29
31
|
if (value === undefined)
|
|
30
32
|
continue;
|
|
31
33
|
if (typeof value === "string") {
|
|
32
34
|
if (value) {
|
|
33
|
-
sql = "SET\n" +
|
|
35
|
+
sql = "SET\n" + sqlKey + "= " + value;
|
|
34
36
|
break;
|
|
35
37
|
}
|
|
36
38
|
}
|
|
@@ -41,12 +43,13 @@ function createUpdateSetFromObject(set) {
|
|
|
41
43
|
i++;
|
|
42
44
|
for (; i < updateKey.length; i++) {
|
|
43
45
|
key = updateKey[i];
|
|
46
|
+
sqlKey = prefix ? `${prefix}.${key}` : key;
|
|
44
47
|
value = set[key];
|
|
45
48
|
if (value === undefined)
|
|
46
49
|
continue;
|
|
47
50
|
if (typeof value === "string") {
|
|
48
51
|
if (value)
|
|
49
|
-
sql += "," +
|
|
52
|
+
sql += "," + sqlKey + "= " + value;
|
|
50
53
|
}
|
|
51
54
|
else
|
|
52
55
|
throw new TypeError(`key ${key} 类型错误(${typeof value})`);
|
|
@@ -19,15 +19,50 @@ export interface ChainAfterConflictDo<T extends TableType = {}> {
|
|
|
19
19
|
toString(): string;
|
|
20
20
|
}
|
|
21
21
|
/** @public */
|
|
22
|
+
export interface ChainUpdateWhere<T extends TableType = {}> extends ChainModifyReturning<T> {
|
|
23
|
+
where(where: Constructable<ConditionParam | void>): ChainModifyReturning<T>;
|
|
24
|
+
}
|
|
25
|
+
/** @public */
|
|
26
|
+
export interface ChainDeleteWhere<T extends TableType = {}> extends ChainModifyReturning<T> {
|
|
27
|
+
where(where: Constructable<ConditionParam | void>): ChainModifyReturning<T>;
|
|
28
|
+
}
|
|
29
|
+
/** @public */
|
|
22
30
|
export interface ChainInsert<T extends TableType = {}> extends ChainModifyReturning<T> {
|
|
23
31
|
onConflict(option: Constructable<readonly (keyof T)[] | string>): ChainAfterConflictDo<T>;
|
|
24
32
|
}
|
|
25
33
|
/** @public */
|
|
26
|
-
export interface ChainUpdate<T extends TableType = {}> extends
|
|
27
|
-
|
|
34
|
+
export interface ChainUpdate<T extends TableType = {}> extends ChainUpdateWhere<T> {
|
|
35
|
+
/**
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
*
|
|
39
|
+
* table.update({age: "3", name: "'hi'", count:"b.count"}, "a").from("table1 AS b", "(SELECT k FROM table2) AS c").where(...)
|
|
40
|
+
* // UPDATE table AS a
|
|
41
|
+
* // FROM table1 AS b, (SELECT k FROM table2) AS c
|
|
42
|
+
* // SET a.age=3, a.name='hi', a.count=b.count
|
|
43
|
+
* // WHERE ...
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
from(...table: (Constructable<string> | {
|
|
47
|
+
selectable: Constructable<string>;
|
|
48
|
+
as: string;
|
|
49
|
+
})[]): ChainUpdateWhere<T>;
|
|
28
50
|
}
|
|
29
51
|
/** @public */
|
|
30
|
-
export interface ChainDelete<T extends TableType = {}> extends
|
|
31
|
-
|
|
52
|
+
export interface ChainDelete<T extends TableType = {}> extends ChainDeleteWhere<T> {
|
|
53
|
+
/**
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
*
|
|
57
|
+
* table.delete().using("table1 AS b", "(SELECT k FROM table2) AS c").where(...)
|
|
58
|
+
* // DELETE FROM table
|
|
59
|
+
* // USING table1 AS b, (SELECT k FROM table2) AS c
|
|
60
|
+
* // WHERE ...
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
using(...table: (Constructable<string> | {
|
|
64
|
+
selectable: Constructable<string>;
|
|
65
|
+
as: string;
|
|
66
|
+
})[]): ChainDeleteWhere<T>;
|
|
32
67
|
}
|
|
33
68
|
//# sourceMappingURL=chain_modify.d.ts.map
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Constructable } from "../util.ts";
|
|
2
|
+
/**
|
|
3
|
+
* @public
|
|
4
|
+
*/
|
|
5
|
+
export interface ChainCTE {
|
|
6
|
+
as(statement: Constructable<string>): ChainCTE;
|
|
7
|
+
as(name: string, statement: Constructable<string>): ChainCTE;
|
|
8
|
+
toString(): string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* @public
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
*
|
|
15
|
+
* //WITH cte1 AS(SELECT * FROM table1)
|
|
16
|
+
*
|
|
17
|
+
* withAs("cte1","SELECT * FROM table1")
|
|
18
|
+
* withAs("cte1",() => "SELECT * FROM table1")
|
|
19
|
+
*
|
|
20
|
+
* withAs(() => "cte1 AS(SELECT * FROM table1)")
|
|
21
|
+
* withAs("cte1 AS(SELECT * FROM table1)")
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare function withAs(statement: Constructable<string>): ChainCTE;
|
|
25
|
+
/** @public */
|
|
26
|
+
export declare function withAs(name: string, statement: Constructable<string>): ChainCTE;
|
|
27
|
+
/**
|
|
28
|
+
* @public
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
*
|
|
32
|
+
* //WITH RECURSIVE cte1 AS(SELECT * FROM table1)
|
|
33
|
+
*
|
|
34
|
+
* withRecursiveAs("cte1","SELECT * FROM table1")
|
|
35
|
+
* withRecursiveAs("cte1",() => "SELECT * FROM table1")
|
|
36
|
+
*
|
|
37
|
+
* withRecursiveAs(() => "cte1 AS(SELECT * FROM table1)")
|
|
38
|
+
* withRecursiveAs("cte1 AS(SELECT * FROM table1)")
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare function withRecursiveAs(statement: Constructable<string>): ChainCTE;
|
|
42
|
+
/** @public */
|
|
43
|
+
export declare function withRecursiveAs(name: string, statement: Constructable<string>): ChainCTE;
|
|
44
|
+
//# sourceMappingURL=cte.d.ts.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
function withAs(nameOrStatement, statement) {
|
|
2
|
+
return new ChainCETImpl(`WITH \n${concat(nameOrStatement, statement)}`);
|
|
3
|
+
}
|
|
4
|
+
function withRecursiveAs(nameOrStatement, statement) {
|
|
5
|
+
return new ChainCETImpl(`WITH RECURSIVE \n${concat(nameOrStatement, statement)}`);
|
|
6
|
+
}
|
|
7
|
+
class ChainCETImpl {
|
|
8
|
+
sql;
|
|
9
|
+
constructor(sql) {
|
|
10
|
+
this.sql = sql;
|
|
11
|
+
}
|
|
12
|
+
as(nameOrStatement, statement) {
|
|
13
|
+
if (statement) {
|
|
14
|
+
return new ChainCETImpl(`${this.sql}\n${concat(nameOrStatement, statement)}`);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
return new ChainCETImpl(concat(nameOrStatement, statement));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
toString() {
|
|
21
|
+
return this.sql;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function concat(nameOrStatement, statement) {
|
|
25
|
+
if (statement) {
|
|
26
|
+
if (typeof statement === "function")
|
|
27
|
+
statement = statement();
|
|
28
|
+
return `${nameOrStatement} AS(${statement})`;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
statement = nameOrStatement;
|
|
32
|
+
if (typeof statement === "function")
|
|
33
|
+
statement = statement();
|
|
34
|
+
return statement;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { withAs, withRecursiveAs };
|
|
@@ -5,6 +5,8 @@ import { TableType } from "./type.ts";
|
|
|
5
5
|
export declare class SqlChainModify<T extends TableType = {}> extends SqlStatement implements ChainInsert<T>, ChainUpdate<T>, ChainDelete<T> {
|
|
6
6
|
readonly sql: string;
|
|
7
7
|
constructor(sql: string);
|
|
8
|
+
from(...from: Constructable<string>[]): SqlChainModify<T>;
|
|
9
|
+
using(...from: Constructable<string>[]): SqlChainModify<T>;
|
|
8
10
|
returning<R extends {}>(returns: Constructable<SelectParam | "*">): SqlStatementDataset<R>;
|
|
9
11
|
onConflict(onConflict: Constructable<readonly string[] | string>): ChainAfterConflictDo<T>;
|
|
10
12
|
where(where: Constructable<ConditionParam | void>): ChainModifyReturning<T>;
|
|
@@ -8,6 +8,24 @@ class SqlChainModify extends SqlStatement {
|
|
|
8
8
|
super();
|
|
9
9
|
this.sql = sql;
|
|
10
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 SqlChainModify(sql);
|
|
19
|
+
}
|
|
20
|
+
using(...from) {
|
|
21
|
+
const textList = from.map((f, i) => {
|
|
22
|
+
if (typeof f === "function")
|
|
23
|
+
return f();
|
|
24
|
+
return f;
|
|
25
|
+
});
|
|
26
|
+
const sql = this.genSql() + `\nUSING ${textList.join(", ")}`;
|
|
27
|
+
return new SqlChainModify(sql);
|
|
28
|
+
}
|
|
11
29
|
returning(returns) {
|
|
12
30
|
if (typeof returns === "function")
|
|
13
31
|
returns = returns();
|