@asla/yoursql 0.9.3 → 0.10.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/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 -8
- package/dist/sql_gen/mod.js +7 -6
- 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 +24 -22
- package/dist/sql_gen/sql_value/sql_value.js +27 -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/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 +21 -54
- 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 -58
- package/dist/sql_gen/select/DbTable.js +0 -78
- package/dist/sql_gen/select/TableQuery.d.ts +0 -28
- package/dist/sql_gen/select/TableQuery.js +0 -73
- package/dist/sql_gen/select/_statement.d.ts +0 -9
- package/dist/sql_gen/select/_statement.js +0 -63
- package/dist/sql_gen/select/chain_modify.d.ts +0 -68
- package/dist/sql_gen/select/chain_select.d.ts +0 -28
- package/dist/sql_gen/select/query_chain_insert.d.ts +0 -24
- package/dist/sql_gen/select/query_chain_insert.js +0 -85
- 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
- /package/dist/sql_gen/{select → statement}/cte.d.ts +0 -0
- /package/dist/sql_gen/{select → statement}/cte.js +0 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { SqlStatement, SqlTextStatementDataset } from '../SqlStatement.js';
|
|
2
|
+
import { returningToString, whereToString } from '../_statement.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @public
|
|
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) {
|
|
13
|
+
let sql = `DELETE FROM ${table}`;
|
|
14
|
+
if (option?.as) {
|
|
15
|
+
sql += ` AS ${option.as}`;
|
|
16
|
+
}
|
|
17
|
+
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
|
+
}
|
|
45
|
+
|
|
46
|
+
export { deleteFrom };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ConditionParam, Constructable } from "../util.ts";
|
|
2
|
+
import { ChainModifyReturning } from "./_modify.ts";
|
|
3
|
+
/** @public */
|
|
4
|
+
export interface ChainDelete extends ChainDeleteAfterUsing {
|
|
5
|
+
/**
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
*
|
|
9
|
+
* using("table1 AS b", "(SELECT k FROM table2) AS c").where(...)
|
|
10
|
+
* // USING table1 AS b, (SELECT k FROM table2) AS c
|
|
11
|
+
* // WHERE ...
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
using(...table: (Constructable<string> | {
|
|
15
|
+
selectable: Constructable<string>;
|
|
16
|
+
as: string;
|
|
17
|
+
})[]): ChainDeleteAfterUsing;
|
|
18
|
+
}
|
|
19
|
+
/** @public */
|
|
20
|
+
export interface ChainDeleteAfterUsing extends ChainDeleteReturning {
|
|
21
|
+
where(where: Constructable<ConditionParam | void>): ChainDeleteReturning;
|
|
22
|
+
}
|
|
23
|
+
/** @public */
|
|
24
|
+
export interface ChainDeleteReturning extends ChainModifyReturning {
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=delete_chain.d.ts.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ChainInsert } from "./insert_chain.ts";
|
|
2
|
+
/** @public */
|
|
3
|
+
export declare function insertInto(target: string): ChainInsert;
|
|
4
|
+
/** @public */
|
|
5
|
+
export declare function insertInto(table: string, columns: readonly string[]): ChainInsert;
|
|
6
|
+
//# sourceMappingURL=insert.d.ts.map
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { selectColumns } from '../util.js';
|
|
2
|
+
import { whereToString, createUpdateSetFromObject } from '../_statement.js';
|
|
3
|
+
import { SqlStatement, SqlTextStatementDataset } from '../SqlStatement.js';
|
|
4
|
+
|
|
5
|
+
function insertInto(table, columns) {
|
|
6
|
+
if (columns) {
|
|
7
|
+
return new InsertChain(`INSERT INTO ${table}(${columns.join(",")})`);
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
return new InsertChain(`INSERT INTO ${table}`);
|
|
11
|
+
}
|
|
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
|
+
}
|
|
106
|
+
|
|
107
|
+
export { insertInto };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Constructable } from "../util.ts";
|
|
2
|
+
import { ChainModifyReturning } from "./_modify.ts";
|
|
3
|
+
/** @public */
|
|
4
|
+
export interface ChainAfterConflict {
|
|
5
|
+
doNotThing(): ChainInsertReturning;
|
|
6
|
+
doUpdate(set: Constructable<string | readonly string[] | Record<string, string>>): ChainInsertReturning;
|
|
7
|
+
toString(): string;
|
|
8
|
+
}
|
|
9
|
+
/** @public */
|
|
10
|
+
export interface ChainInsert extends ChainInsertAfterValues {
|
|
11
|
+
/**
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* values("(18, 'hi'), (17, 'hh')") // " VALUES(18, 'hi'), (17, 'hh')"
|
|
15
|
+
* values(["(18, 'hi')", "(17, 'hh')"]) // " VALUES(18, 'hi'), (17, 'hh')"
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
values(statement: Constructable<string | readonly string[]>): ChainInsertAfterValues;
|
|
19
|
+
select(statement: Constructable<string>): ChainInsertAfterValues;
|
|
20
|
+
}
|
|
21
|
+
/** @public */
|
|
22
|
+
export interface ChainInsertAfterValues extends ChainInsertReturning {
|
|
23
|
+
onConflict(columns: string | string[]): ChainAfterConflict;
|
|
24
|
+
}
|
|
25
|
+
/** @public */
|
|
26
|
+
export interface ChainInsertReturning extends ChainModifyReturning {
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=insert_chain.d.ts.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./select.ts";
|
|
2
|
+
export * from "./select_chain.ts";
|
|
3
|
+
export * from "./insert.ts";
|
|
4
|
+
export * from "./insert_chain.ts";
|
|
5
|
+
export * from "./update.ts";
|
|
6
|
+
export * from "./update_chain.ts";
|
|
7
|
+
export * from "./delete.ts";
|
|
8
|
+
export * from "./delete_chain.ts";
|
|
9
|
+
export * from "./cte.ts";
|
|
10
|
+
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Constructable, TableType } from "../util.ts";
|
|
2
|
+
import { ChainSelect, OrderByParam } from "./select_chain.ts";
|
|
3
|
+
/** @public */
|
|
4
|
+
export declare function select(columns?: undefined | ""): ChainSelect<{}>;
|
|
5
|
+
/** @public */
|
|
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;
|
|
38
|
+
//# sourceMappingURL=select.d.ts.map
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { selectColumns } from '../util.js';
|
|
2
|
+
import { selectableToString, whereToString, condition } from '../_statement.js';
|
|
3
|
+
import { SqlTextStatementDataset } from '../SqlStatement.js';
|
|
4
|
+
|
|
5
|
+
function select(columns) {
|
|
6
|
+
if (!columns)
|
|
7
|
+
return new SelectChainAfterSelect("SELECT ");
|
|
8
|
+
if (typeof columns === "function")
|
|
9
|
+
columns = columns();
|
|
10
|
+
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
|
+
}
|
|
153
|
+
|
|
154
|
+
export { orderBy, select };
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { ConditionParam, Constructable, TableType } from "../util.ts";
|
|
2
|
+
import { SqlSelectable, SqlStatementDataset } from "../SqlStatement.ts";
|
|
3
|
+
/** @public */
|
|
4
|
+
export type SelectAsNameOption = {
|
|
5
|
+
as?: string;
|
|
6
|
+
};
|
|
7
|
+
/** @public */
|
|
8
|
+
export interface ChainSelect<T extends TableType> {
|
|
9
|
+
/**
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* from("table1", {as:"t1"}).from("table2 AS t2") // SELECT ... FROM table1 AS t1 FROM table2 AS t2
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
from(selectable: Constructable<SqlSelectable | string>, option?: SelectAsNameOption): ChainSelectAfterFirstFrom<T>;
|
|
16
|
+
}
|
|
17
|
+
/** @public */
|
|
18
|
+
export interface ChainSelectAfterFirstFrom<T extends TableType> extends ChainSelectAfterFrom<T> {
|
|
19
|
+
from(selectable: Constructable<SqlSelectable | string>, option?: SelectAsNameOption): ChainSelectAfterFirstFrom<T>;
|
|
20
|
+
}
|
|
21
|
+
/** @public */
|
|
22
|
+
export type SelectJoinOption = SelectAsNameOption & {
|
|
23
|
+
on?: Constructable<ConditionParam>;
|
|
24
|
+
};
|
|
25
|
+
/** @public */
|
|
26
|
+
export interface ChainSelectAfterFrom<T extends TableType> extends ChainSelectAfterJoin<T> {
|
|
27
|
+
innerJoin(selectable: Constructable<SqlSelectable | string>, options?: SelectJoinOption): ChainSelectAfterFrom<T>;
|
|
28
|
+
leftJoin(selectable: Constructable<SqlSelectable | string>, options?: SelectJoinOption): ChainSelectAfterFrom<T>;
|
|
29
|
+
rightJoin(selectable: Constructable<SqlSelectable | string>, options?: SelectJoinOption): ChainSelectAfterFrom<T>;
|
|
30
|
+
fullJoin(selectable: Constructable<SqlSelectable | string>, options?: SelectJoinOption): ChainSelectAfterFrom<T>;
|
|
31
|
+
naturalJoin(selectable: Constructable<SqlSelectable | string>, options?: SelectAsNameOption): ChainSelectAfterFrom<T>;
|
|
32
|
+
crossJoin(selectable: Constructable<SqlSelectable | string>, options?: SelectAsNameOption): ChainSelectAfterFrom<T>;
|
|
33
|
+
}
|
|
34
|
+
/** @public */
|
|
35
|
+
export interface ChainSelectAfterJoin<T extends TableType> extends ChainSelectAfterWhere<T> {
|
|
36
|
+
/**
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* where(undefined) // SELECT ... FROM table1
|
|
40
|
+
* where([]) // SELECT ... FROM table1
|
|
41
|
+
* where("age > 18") // SELECT ... FROM table1 WHERE age > 18
|
|
42
|
+
* where(["age > 18", "name = 'Alice'"]) // SELECT ... FROM table1 WHERE age > 18 AND name = 'Alice'
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
where(param: Constructable<ConditionParam | void>): ChainSelectAfterWhere<T>;
|
|
46
|
+
}
|
|
47
|
+
/** @public */
|
|
48
|
+
export interface ChainSelectAfterWhere<T extends TableType> extends ChainSelectAfterHaving<T> {
|
|
49
|
+
/**
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* groupBy("age") // SELECT ... FROM table1 GROUP BY age
|
|
53
|
+
* groupBy(["age", "name"]) // SELECT ... FROM table1 GROUP BY age, name
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
groupBy(columns?: string | string[]): ChainSelectAfterGroupBy<T>;
|
|
57
|
+
}
|
|
58
|
+
/** @public */
|
|
59
|
+
export interface ChainSelectAfterGroupBy<T extends TableType> extends ChainSelectAfterHaving<T> {
|
|
60
|
+
having(param: Constructable<ConditionParam | void>): ChainSelectAfterHaving<T>;
|
|
61
|
+
}
|
|
62
|
+
/** @public */
|
|
63
|
+
export interface ChainSelectAfterHaving<T extends TableType> extends ChainSelectAfterOrderBy<T> {
|
|
64
|
+
/**
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* orderBy([]) // ""
|
|
68
|
+
* orderBy({}) // ""
|
|
69
|
+
* orderBy() // ""
|
|
70
|
+
* orderBy("age") // ORDER BY age
|
|
71
|
+
* orderBy(["age DESC", "name ASC NULLS LAST"]) // ORDER BY age DESC, name ASC NULLS LAST
|
|
72
|
+
* orderBy({key:"age", target:"DESC NULLS LAST"}) // ORDER BY age DESC NULLS LAST
|
|
73
|
+
* orderBy([{ key: "age", asc: false, nullLast: true }, { key: "name", asc: true }])
|
|
74
|
+
* // ORDER BY age DESC NULLS LAST, name ASC
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
orderBy(param: Constructable<OrderByParam | void>): ChainSelectAfterOrderBy<T>;
|
|
78
|
+
}
|
|
79
|
+
/** @public */
|
|
80
|
+
export interface ChainSelectAfterOrderBy<T extends TableType> extends SqlStatementDataset<T> {
|
|
81
|
+
limit(limit?: number | bigint, offset?: number | bigint): ChainSelectAfterLimit<T>;
|
|
82
|
+
}
|
|
83
|
+
/** @public */
|
|
84
|
+
export interface ChainSelectAfterLimit<T extends TableType> extends SqlStatementDataset<T> {
|
|
85
|
+
}
|
|
86
|
+
/** @public */
|
|
87
|
+
export type OrderValue = "ASC" | "DESC";
|
|
88
|
+
type OrderByValue = `${OrderValue} NULLS ${"FIRST" | "LAST"}` | OrderValue;
|
|
89
|
+
/** @public */
|
|
90
|
+
export type OrderBehavior = {
|
|
91
|
+
key: string;
|
|
92
|
+
asc: boolean;
|
|
93
|
+
nullLast?: boolean;
|
|
94
|
+
target?: undefined;
|
|
95
|
+
} | {
|
|
96
|
+
key: string;
|
|
97
|
+
target: OrderByValue;
|
|
98
|
+
};
|
|
99
|
+
/** @public */
|
|
100
|
+
export type OrderByParam = string | OrderBehavior | (string | OrderBehavior)[];
|
|
101
|
+
export {};
|
|
102
|
+
//# sourceMappingURL=select_chain.d.ts.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { TableType } from "../util.ts";
|
|
2
|
+
import { ChainUpdate } from "./update_chain.ts";
|
|
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 declare function update<T extends TableType>(table: string, options?: UpdateOption): ChainUpdate<T>;
|
|
17
|
+
//# sourceMappingURL=update.d.ts.map
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { selectColumns } from '../util.js';
|
|
2
|
+
import { SqlStatement, SqlTextStatementDataset } from '../SqlStatement.js';
|
|
3
|
+
import { createUpdateSetFromObject, whereToString } from '../_statement.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @public
|
|
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) {
|
|
15
|
+
let sql = `UPDATE ${table}`;
|
|
16
|
+
if (options?.as) {
|
|
17
|
+
sql += ` AS ${options.as}`;
|
|
18
|
+
}
|
|
19
|
+
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
|
+
}
|
|
87
|
+
|
|
88
|
+
export { update };
|
|
@@ -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
|