@asla/yoursql 0.10.1 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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/_statement.d.ts +1 -1
- package/dist/sql_gen/_statement.js +1 -1
- package/dist/sql_gen/mod.js +3 -1
- package/dist/sql_gen/sql_value/sql_value.d.ts +25 -41
- package/dist/sql_gen/sql_value/sql_value.js +70 -74
- package/dist/sql_gen/sql_value/type.d.ts +8 -9
- 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
- package/dist/sql_gen/sql_value/_utils.d.ts +0 -11
- package/dist/sql_gen/sql_value/_utils.js +0 -37
|
@@ -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
|