@asla/yoursql 0.5.1 → 0.5.2
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/mod.js +89 -87
- package/dist/select/TableQuery.d.ts +5 -3
- package/dist/select/_statement.d.ts +6 -0
- package/dist/select/select.d.ts +26 -8
- package/dist/util.d.ts +7 -2
- package/package.json +1 -1
package/dist/mod.js
CHANGED
|
@@ -33,6 +33,53 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
33
33
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
34
34
|
};
|
|
35
35
|
|
|
36
|
+
function selectColumnsOrTable(columns) {
|
|
37
|
+
let sqlSelect;
|
|
38
|
+
let select;
|
|
39
|
+
if (columns instanceof Array) {
|
|
40
|
+
sqlSelect = columns;
|
|
41
|
+
select = columns;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
sqlSelect = [];
|
|
45
|
+
select = [];
|
|
46
|
+
let c;
|
|
47
|
+
for (const key of Object.keys(columns)) {
|
|
48
|
+
c = columns[key];
|
|
49
|
+
if (typeof c === "string" && c !== key) {
|
|
50
|
+
sqlSelect.push(key + " AS " + c);
|
|
51
|
+
select.push(c);
|
|
52
|
+
}
|
|
53
|
+
else if (c) {
|
|
54
|
+
sqlSelect.push(key);
|
|
55
|
+
select.push(key);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (select.length === 0)
|
|
60
|
+
throw new Error("选择列为空");
|
|
61
|
+
return { columns: select, sqlColumns: sqlSelect.join(", ") };
|
|
62
|
+
}
|
|
63
|
+
function condition(conditions, type = "AND") {
|
|
64
|
+
if (typeof conditions === "function")
|
|
65
|
+
conditions = conditions();
|
|
66
|
+
if (!conditions)
|
|
67
|
+
return;
|
|
68
|
+
if (typeof conditions === "string")
|
|
69
|
+
return conditions;
|
|
70
|
+
else {
|
|
71
|
+
if (conditions.length) {
|
|
72
|
+
let sql = "";
|
|
73
|
+
type = " " + type + " ";
|
|
74
|
+
sql += conditions[0];
|
|
75
|
+
for (let i = 1; i < conditions.length; i++)
|
|
76
|
+
sql += type + conditions[i];
|
|
77
|
+
return sql;
|
|
78
|
+
}
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
36
83
|
/**
|
|
37
84
|
* 获取对象数组中的 key 的集合
|
|
38
85
|
* @public
|
|
@@ -58,6 +105,7 @@ function getObjectListKeys(objectList, keepUndefinedKey) {
|
|
|
58
105
|
/**
|
|
59
106
|
* 生成 WHERE 语句
|
|
60
107
|
* @public
|
|
108
|
+
* @example
|
|
61
109
|
* ```ts
|
|
62
110
|
*
|
|
63
111
|
* ```
|
|
@@ -79,67 +127,50 @@ function having(conditions, type) {
|
|
|
79
127
|
return "\nHAVING " + sql;
|
|
80
128
|
return "";
|
|
81
129
|
}
|
|
82
|
-
function condition(conditions, type = "AND") {
|
|
83
|
-
if (typeof conditions === "function")
|
|
84
|
-
conditions = conditions();
|
|
85
|
-
if (!conditions)
|
|
86
|
-
return;
|
|
87
|
-
if (typeof conditions === "string")
|
|
88
|
-
return conditions;
|
|
89
|
-
else {
|
|
90
|
-
if (conditions.length) {
|
|
91
|
-
let sql = "";
|
|
92
|
-
type = " " + type + " ";
|
|
93
|
-
sql += conditions[0];
|
|
94
|
-
for (let i = 1; i < conditions.length; i++)
|
|
95
|
-
sql += type + conditions[i];
|
|
96
|
-
return sql;
|
|
97
|
-
}
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
130
|
/**
|
|
102
131
|
* @public
|
|
132
|
+
* @example
|
|
103
133
|
* ```ts
|
|
104
134
|
* selectColumns({c1: true, c2: "count(*)", c3: "column"}) // "c1,count(*) AS c2,column as c3"
|
|
105
|
-
* selectColumns(
|
|
135
|
+
* selectColumns("c1,count(*) AS c2,column as c3") // "c1,count(*) AS c2,column as c3"
|
|
106
136
|
* ```
|
|
107
137
|
*/
|
|
108
138
|
function selectColumns(columns) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
throw new Error("没有选择任何列");
|
|
122
|
-
let k = keys[0];
|
|
123
|
-
let v = columns[k];
|
|
124
|
-
if (typeof v === "string")
|
|
125
|
-
sql += v + " AS " + k;
|
|
126
|
-
else
|
|
127
|
-
sql += k;
|
|
128
|
-
for (let i = 1; i < keys.length; i++) {
|
|
129
|
-
k = keys[i];
|
|
130
|
-
v = columns[k];
|
|
131
|
-
sql += ",";
|
|
139
|
+
if (typeof columns === "function")
|
|
140
|
+
columns = columns();
|
|
141
|
+
switch (typeof columns) {
|
|
142
|
+
case "string":
|
|
143
|
+
return columns;
|
|
144
|
+
case "object": {
|
|
145
|
+
let sql = "";
|
|
146
|
+
const keys = Object.keys(columns);
|
|
147
|
+
if (keys.length === 0)
|
|
148
|
+
throw new Error("没有选择任何列");
|
|
149
|
+
let k = keys[0];
|
|
150
|
+
let v = columns[k];
|
|
132
151
|
if (typeof v === "string")
|
|
133
152
|
sql += v + " AS " + k;
|
|
134
153
|
else
|
|
135
154
|
sql += k;
|
|
155
|
+
for (let i = 1; i < keys.length; i++) {
|
|
156
|
+
k = keys[i];
|
|
157
|
+
v = columns[k];
|
|
158
|
+
sql += ",";
|
|
159
|
+
if (typeof v === "string")
|
|
160
|
+
sql += v + " AS " + k;
|
|
161
|
+
else
|
|
162
|
+
sql += k;
|
|
163
|
+
}
|
|
164
|
+
return sql;
|
|
136
165
|
}
|
|
166
|
+
default:
|
|
167
|
+
throw new TypeError("columns 应为 string 或 object 类型");
|
|
137
168
|
}
|
|
138
|
-
return sql;
|
|
139
169
|
}
|
|
140
170
|
/**
|
|
141
171
|
* 生成 ORDER BY 语句, d
|
|
142
172
|
* @public
|
|
173
|
+
* @example
|
|
143
174
|
* ```ts
|
|
144
175
|
* // 以下生成 "\nORDER BY age DESC NULLS FIRST,num ASC"
|
|
145
176
|
* orderBy("age DESC NULLS FIRST,num ASC");
|
|
@@ -174,14 +205,16 @@ function orderBy(by) {
|
|
|
174
205
|
let keys = Object.keys(by);
|
|
175
206
|
if (keys.length) {
|
|
176
207
|
let key = keys[0];
|
|
177
|
-
|
|
208
|
+
let value = by[key];
|
|
209
|
+
sql += "\nORDER BY " + key + " " + (typeof value === "string" ? value : value ? "ASC" : "DESC");
|
|
178
210
|
for (let i = 1; i < keys.length; i++) {
|
|
179
211
|
key = keys[i];
|
|
212
|
+
value = by[key];
|
|
180
213
|
sql += "," + key + " ";
|
|
181
|
-
if (typeof
|
|
182
|
-
sql +=
|
|
214
|
+
if (typeof value === "string")
|
|
215
|
+
sql += value;
|
|
183
216
|
else
|
|
184
|
-
sql +=
|
|
217
|
+
sql += value ? "ASC" : "DESC";
|
|
185
218
|
}
|
|
186
219
|
}
|
|
187
220
|
}
|
|
@@ -616,55 +649,24 @@ class Selection {
|
|
|
616
649
|
return new _a(__classPrivateFieldGet(this, _Selection_sql, "f") + "," + fromAs(selectable, as));
|
|
617
650
|
}
|
|
618
651
|
select(columnsIn) {
|
|
619
|
-
let sql = "SELECT ";
|
|
620
652
|
let columns = [];
|
|
621
|
-
if (typeof columnsIn === "
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
else
|
|
627
|
-
columns = Object.keys(columnsIn);
|
|
628
|
-
}
|
|
653
|
+
if (typeof columnsIn === "function")
|
|
654
|
+
columnsIn = columnsIn();
|
|
655
|
+
if (typeof columnsIn === "object")
|
|
656
|
+
columns = Object.keys(columnsIn);
|
|
657
|
+
let sql = "SELECT " + selectColumns(columnsIn);
|
|
629
658
|
sql += "\n" + this.toString();
|
|
630
659
|
return new AfterSelectImpl(sql, columns);
|
|
631
660
|
}
|
|
632
661
|
}
|
|
633
662
|
_a = Selection, _Selection_sql = new WeakMap(), _Selection_instances = new WeakSet(), _Selection_join = function _Selection_join(type, selectable, as, on) {
|
|
634
663
|
let sql = __classPrivateFieldGet(this, _Selection_sql, "f") + "\n" + type + " " + fromAs(selectable, as);
|
|
635
|
-
if (on)
|
|
636
|
-
sql += " ON " + on;
|
|
664
|
+
if (on) {
|
|
665
|
+
sql += " ON " + condition(on);
|
|
666
|
+
}
|
|
637
667
|
return new _a(sql);
|
|
638
668
|
};
|
|
639
669
|
|
|
640
|
-
function selectColumnsOrTable(columns) {
|
|
641
|
-
let sqlSelect;
|
|
642
|
-
let select;
|
|
643
|
-
if (columns instanceof Array) {
|
|
644
|
-
sqlSelect = columns;
|
|
645
|
-
select = columns;
|
|
646
|
-
}
|
|
647
|
-
else {
|
|
648
|
-
sqlSelect = [];
|
|
649
|
-
select = [];
|
|
650
|
-
let c;
|
|
651
|
-
for (const key of Object.keys(columns)) {
|
|
652
|
-
c = columns[key];
|
|
653
|
-
if (typeof c === "string" && c !== key) {
|
|
654
|
-
sqlSelect.push(key + " AS " + c);
|
|
655
|
-
select.push(c);
|
|
656
|
-
}
|
|
657
|
-
else if (c) {
|
|
658
|
-
sqlSelect.push(key);
|
|
659
|
-
select.push(key);
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
if (select.length === 0)
|
|
664
|
-
throw new Error("选择列为空");
|
|
665
|
-
return { columns: select, sqlColumns: sqlSelect.join(", ") };
|
|
666
|
-
}
|
|
667
|
-
|
|
668
670
|
/** @public */
|
|
669
671
|
class DbTableQuery extends DbTable {
|
|
670
672
|
constructor(name, columns, statement) {
|
|
@@ -16,13 +16,15 @@ export declare class DbTableQuery<T extends TableType = Record<string, any>, C e
|
|
|
16
16
|
*/
|
|
17
17
|
select<R extends {
|
|
18
18
|
[key in keyof T]?: string | boolean;
|
|
19
|
-
} | Record<string, string>>(columns: R, as?: string): CurrentWhere<{
|
|
19
|
+
} | Record<string, string>>(columns: R | (() => R), as?: string): CurrentWhere<{
|
|
20
20
|
[key in keyof R]: R[key] extends boolean ? key extends keyof T ? T[key] : unknown : R[key] extends keyof T ? T[R[key]] : unknown;
|
|
21
21
|
}>;
|
|
22
22
|
/** 选择单表- 所有类型 */
|
|
23
|
-
select<R extends {}>(columns:
|
|
23
|
+
select<R extends {}>(columns: string | {
|
|
24
24
|
[key in keyof R]?: key extends keyof T ? string | boolean : string;
|
|
25
|
-
}
|
|
25
|
+
} | (() => string | {
|
|
26
|
+
[key in keyof R]?: key extends keyof T ? string | boolean : string;
|
|
27
|
+
}), as?: string): CurrentWhere<R>;
|
|
26
28
|
insert(values: C[] | SqlQueryStatement<C>, option?: InsertOption<T>): string;
|
|
27
29
|
insertWithResult<R extends ColumnsSelected<T>>(values: C[] | SqlQueryStatement<C>, returns: R, option?: InsertOption<T>): SqlQueryStatement<SelectColumns<T, R>>;
|
|
28
30
|
update(values: UpdateRowValue<T>, option?: UpdateOption): string;
|
|
@@ -3,4 +3,10 @@ export declare function selectColumnsOrTable(columns: ColumnsSelectAs<any> | str
|
|
|
3
3
|
columns: string[];
|
|
4
4
|
sqlColumns: string;
|
|
5
5
|
};
|
|
6
|
+
type ConditionParam = string | string[];
|
|
7
|
+
/**
|
|
8
|
+
* 生成条件语句
|
|
9
|
+
*/
|
|
10
|
+
export declare function condition(conditions?: ConditionParam | (() => ConditionParam | void), type?: "AND" | "OR"): string;
|
|
11
|
+
export {};
|
|
6
12
|
//# sourceMappingURL=_statement.d.ts.map
|
package/dist/select/select.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SqlSelectable, SqlQueryStatement } from "./selectable.ts";
|
|
2
|
-
import { OrderByParam, ConditionParam } from "../util.ts";
|
|
2
|
+
import { OrderByParam, ConditionParam, SelectParam } from "../util.ts";
|
|
3
3
|
import type { TableType } from "./type.ts";
|
|
4
4
|
/** @public */
|
|
5
5
|
export interface CurrentLimit<T extends TableType> extends SqlQueryStatement<T> {
|
|
@@ -27,17 +27,35 @@ export declare class Selection {
|
|
|
27
27
|
static from(selectable: SqlSelectable<any> | string, as?: string): Selection;
|
|
28
28
|
constructor(selectable: SqlSelectable<any> | string, as?: string);
|
|
29
29
|
toString(): string;
|
|
30
|
-
fullJoin(selectable: SqlSelectable<any>, as: string | undefined, on:
|
|
31
|
-
innerJoin(selectable: SqlSelectable<any>, as: string | undefined, on:
|
|
32
|
-
leftJoin(selectable: SqlSelectable<any>, as: string | undefined, on:
|
|
33
|
-
rightJoin(selectable: SqlSelectable<any>, as: string | undefined, on:
|
|
30
|
+
fullJoin(selectable: SqlSelectable<any>, as: string | undefined, on: ConditionParam | (() => ConditionParam)): Selection;
|
|
31
|
+
innerJoin(selectable: SqlSelectable<any>, as: string | undefined, on: ConditionParam | (() => ConditionParam)): Selection;
|
|
32
|
+
leftJoin(selectable: SqlSelectable<any>, as: string | undefined, on: ConditionParam | (() => ConditionParam)): Selection;
|
|
33
|
+
rightJoin(selectable: SqlSelectable<any>, as: string | undefined, on: ConditionParam | (() => ConditionParam)): Selection;
|
|
34
34
|
naturalJoin(selectable: SqlSelectable<any>, as?: string | undefined): Selection;
|
|
35
35
|
crossJoin(selectable: SqlSelectable<any>, as?: string | undefined): Selection;
|
|
36
36
|
from(selectable: SqlSelectable<any> | string, as?: string): Selection;
|
|
37
|
-
|
|
37
|
+
/** 选择全部列 */
|
|
38
|
+
select<T extends TableType = TableType>(columns: "*"): CurrentWhere<T>;
|
|
39
|
+
/**
|
|
40
|
+
* 自定义SQL选择语句
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* selection.select("t.age, count(*) AS c") // SELECT t.age,count(*) AS c FROM ...
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
select<T extends TableType = TableType>(columns: string): CurrentWhere<T>;
|
|
47
|
+
/**
|
|
48
|
+
* 通过 object 选择 列
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* selection.select({"age":true, c:"count(*)"}) // SELECT age,count(*) AS c FROM ...
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
38
54
|
select<T extends TableType>(columns: {
|
|
39
55
|
[key in keyof T]: string | boolean;
|
|
40
|
-
})
|
|
41
|
-
|
|
56
|
+
} | (() => {
|
|
57
|
+
[key in keyof T]: string | boolean;
|
|
58
|
+
})): CurrentWhere<T>;
|
|
59
|
+
select(columns: SelectParam | (() => SelectParam)): CurrentWhere<TableType>;
|
|
42
60
|
}
|
|
43
61
|
//# sourceMappingURL=select.d.ts.map
|
package/dist/util.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export type ConditionParam = string | string[];
|
|
|
10
10
|
/**
|
|
11
11
|
* 生成 WHERE 语句
|
|
12
12
|
* @public
|
|
13
|
+
* @example
|
|
13
14
|
* ```ts
|
|
14
15
|
*
|
|
15
16
|
* ```
|
|
@@ -21,14 +22,17 @@ export declare function where(conditions?: ConditionParam | (() => ConditionPara
|
|
|
21
22
|
* @public
|
|
22
23
|
*/
|
|
23
24
|
export declare function having(conditions?: ConditionParam | (() => ConditionParam | void), type?: "AND" | "OR"): string;
|
|
25
|
+
/** @public */
|
|
26
|
+
export type SelectParam = string | Record<string, string | boolean>;
|
|
24
27
|
/**
|
|
25
28
|
* @public
|
|
29
|
+
* @example
|
|
26
30
|
* ```ts
|
|
27
31
|
* selectColumns({c1: true, c2: "count(*)", c3: "column"}) // "c1,count(*) AS c2,column as c3"
|
|
28
|
-
* selectColumns(
|
|
32
|
+
* selectColumns("c1,count(*) AS c2,column as c3") // "c1,count(*) AS c2,column as c3"
|
|
29
33
|
* ```
|
|
30
34
|
*/
|
|
31
|
-
export declare function selectColumns(columns:
|
|
35
|
+
export declare function selectColumns(columns: SelectParam | (() => SelectParam)): string;
|
|
32
36
|
/** @public */
|
|
33
37
|
export type OrderBehavior = {
|
|
34
38
|
key: string;
|
|
@@ -40,6 +44,7 @@ export type OrderByParam = string | (string | OrderBehavior)[] | Record<string,
|
|
|
40
44
|
/**
|
|
41
45
|
* 生成 ORDER BY 语句, d
|
|
42
46
|
* @public
|
|
47
|
+
* @example
|
|
43
48
|
* ```ts
|
|
44
49
|
* // 以下生成 "\nORDER BY age DESC NULLS FIRST,num ASC"
|
|
45
50
|
* orderBy("age DESC NULLS FIRST,num ASC");
|