@asla/yoursql 0.7.1 → 0.8.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/mod.d.ts +2 -2
- package/dist/mod.js +75 -106
- package/dist/select/DbTable.d.ts +9 -10
- package/dist/select/TableQuery.d.ts +4 -4
- package/dist/select/_statement.d.ts +0 -4
- package/dist/select/query_chain_abstract.d.ts +91 -0
- package/dist/select/query_chain_insert.d.ts +20 -0
- package/dist/select/query_chain_select.d.ts +48 -0
- package/dist/select/type.d.ts +0 -18
- package/dist/sql_value/sql_value.d.ts +2 -2
- package/dist/util.d.ts +1 -1
- package/package.json +1 -1
- package/dist/select/_update_impl.d.ts +0 -19
- package/dist/select/query_link.d.ts +0 -83
- package/dist/select/selectable.d.ts +0 -41
package/dist/mod.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ export * from "./sql_value/db_type.ts";
|
|
|
2
2
|
export * from "./sql_value/sql_value.ts";
|
|
3
3
|
export * from "./select/type.ts";
|
|
4
4
|
export * from "./select/DbTable.ts";
|
|
5
|
-
export * from "./select/
|
|
6
|
-
export * from "./select/
|
|
5
|
+
export * from "./select/query_chain_select.ts";
|
|
6
|
+
export * from "./select/query_chain_abstract.ts";
|
|
7
7
|
export * from "./select/TableQuery.ts";
|
|
8
8
|
export * from "./util.ts";
|
|
9
9
|
export * from "./your_table/mod.ts";
|
package/dist/mod.js
CHANGED
|
@@ -33,33 +33,6 @@ 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
36
|
function condition(conditions, type = "AND") {
|
|
64
37
|
if (typeof conditions === "function")
|
|
65
38
|
conditions = conditions();
|
|
@@ -186,26 +159,33 @@ function selectColumns(columns) {
|
|
|
186
159
|
case "string":
|
|
187
160
|
return columns;
|
|
188
161
|
case "object": {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
v = columns[k];
|
|
202
|
-
sql += ",";
|
|
162
|
+
if (columns instanceof Array) {
|
|
163
|
+
if (columns.length === 0)
|
|
164
|
+
throw new Error("没有选择任何列");
|
|
165
|
+
return columns.join(",");
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
let sql = "";
|
|
169
|
+
const keys = Object.keys(columns);
|
|
170
|
+
if (keys.length === 0)
|
|
171
|
+
throw new Error("没有选择任何列");
|
|
172
|
+
let k = keys[0];
|
|
173
|
+
let v = columns[k];
|
|
203
174
|
if (typeof v === "string")
|
|
204
175
|
sql += v + " AS " + k;
|
|
205
176
|
else
|
|
206
177
|
sql += k;
|
|
178
|
+
for (let i = 1; i < keys.length; i++) {
|
|
179
|
+
k = keys[i];
|
|
180
|
+
v = columns[k];
|
|
181
|
+
sql += ",";
|
|
182
|
+
if (typeof v === "string")
|
|
183
|
+
sql += v + " AS " + k;
|
|
184
|
+
else
|
|
185
|
+
sql += k;
|
|
186
|
+
}
|
|
187
|
+
return sql;
|
|
207
188
|
}
|
|
208
|
-
return sql;
|
|
209
189
|
}
|
|
210
190
|
default:
|
|
211
191
|
throw new TypeError("columns 应为 string 或 object 类型");
|
|
@@ -275,37 +255,30 @@ function handlerOrderValue(value) {
|
|
|
275
255
|
}
|
|
276
256
|
}
|
|
277
257
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
* 可选择项。可以是 table、查询结果等,它能被 select 语句选择
|
|
281
|
-
* @example
|
|
282
|
-
* ```ts
|
|
283
|
-
* declare const item: SqlSelectable<any>
|
|
284
|
-
* await query(`select * from ${item.toSelect()}`)
|
|
285
|
-
*
|
|
286
|
-
* ```
|
|
287
|
-
* @public
|
|
288
|
-
*/
|
|
289
|
-
class SqlSelectable {
|
|
258
|
+
/** @public */
|
|
259
|
+
class SqlStatement {
|
|
290
260
|
}
|
|
291
|
-
/**
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
261
|
+
/** @public */
|
|
262
|
+
class SqlStatementDataset extends SqlStatement {
|
|
263
|
+
/**
|
|
264
|
+
* 转成子选择语句, 你可以使用 select form xxx 选择
|
|
265
|
+
* 如果是 table 则是 table name
|
|
266
|
+
* 如果是 选择语句,则是 (xxx)
|
|
267
|
+
*/
|
|
268
|
+
toSelect() {
|
|
269
|
+
return "(" + this.toString() + ")";
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/** @public */
|
|
273
|
+
class SqlTextStatementDataset extends SqlStatementDataset {
|
|
296
274
|
constructor(sql) {
|
|
297
275
|
super();
|
|
298
|
-
|
|
299
|
-
__classPrivateFieldSet(this, _SqlQueryStatement_sql, sql.toString(), "f");
|
|
276
|
+
this.sql = sql;
|
|
300
277
|
}
|
|
301
278
|
toString() {
|
|
302
|
-
return
|
|
303
|
-
}
|
|
304
|
-
toSelect() {
|
|
305
|
-
return "(" + this.toString() + ")";
|
|
279
|
+
return this.sql;
|
|
306
280
|
}
|
|
307
281
|
}
|
|
308
|
-
_SqlQueryStatement_sql = new WeakMap();
|
|
309
282
|
|
|
310
283
|
var _YourValuesAs_asName, _YourValuesAs_valuesStr, _YourValuesAs_sql;
|
|
311
284
|
/**
|
|
@@ -568,7 +541,7 @@ class SqlValuesCreator {
|
|
|
568
541
|
return new YourValuesAs(insertKeys, asName, valuesStr.join(",\n"));
|
|
569
542
|
}
|
|
570
543
|
}
|
|
571
|
-
class YourValuesAs extends
|
|
544
|
+
class YourValuesAs extends SqlStatementDataset {
|
|
572
545
|
constructor(columns, asName, valuesStr) {
|
|
573
546
|
super();
|
|
574
547
|
_YourValuesAs_asName.set(this, void 0);
|
|
@@ -646,12 +619,12 @@ const pgSqlTransformer = new Map([
|
|
|
646
619
|
]);
|
|
647
620
|
|
|
648
621
|
var _Selection_instances, _a, _Selection_sql, _Selection_join;
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
622
|
+
/**
|
|
623
|
+
* @public ChainSelectWhere 的默认实现
|
|
624
|
+
*/
|
|
625
|
+
class SqlSelectChain extends SqlTextStatementDataset {
|
|
653
626
|
where(param) {
|
|
654
|
-
return new
|
|
627
|
+
return new SqlSelectChain(this.toString() + where(param));
|
|
655
628
|
}
|
|
656
629
|
groupBy(columns) {
|
|
657
630
|
let sql = this.toString();
|
|
@@ -659,13 +632,13 @@ class AfterSelectImpl extends SqlQueryStatement {
|
|
|
659
632
|
sql += " GROUP BY " + columns;
|
|
660
633
|
else
|
|
661
634
|
sql += " GROUP BY " + columns.join(",");
|
|
662
|
-
return new
|
|
635
|
+
return new SqlSelectChain(sql);
|
|
663
636
|
}
|
|
664
637
|
having(param) {
|
|
665
|
-
return new
|
|
638
|
+
return new SqlSelectChain(this.toString() + having(param));
|
|
666
639
|
}
|
|
667
640
|
orderBy(param) {
|
|
668
|
-
return new
|
|
641
|
+
return new SqlSelectChain(this.toString() + orderBy(param));
|
|
669
642
|
}
|
|
670
643
|
limit(limit, offset) {
|
|
671
644
|
let sql = this.toString();
|
|
@@ -684,7 +657,7 @@ class AfterSelectImpl extends SqlQueryStatement {
|
|
|
684
657
|
else
|
|
685
658
|
throw new TypeError("offset 必须是个整数:" + limit);
|
|
686
659
|
}
|
|
687
|
-
return new
|
|
660
|
+
return new SqlTextStatementDataset(sql);
|
|
688
661
|
}
|
|
689
662
|
}
|
|
690
663
|
function fromAs(selectable, as) {
|
|
@@ -734,7 +707,7 @@ class Selection {
|
|
|
734
707
|
columnsIn = columnsIn();
|
|
735
708
|
let sql = "SELECT " + selectColumns(columnsIn);
|
|
736
709
|
sql += "\n" + this.toString();
|
|
737
|
-
return new
|
|
710
|
+
return new SqlSelectChain(sql);
|
|
738
711
|
}
|
|
739
712
|
}
|
|
740
713
|
_a = Selection, _Selection_sql = new WeakMap(), _Selection_instances = new WeakSet(), _Selection_join = function _Selection_join(type, selectable, as, on) {
|
|
@@ -745,8 +718,11 @@ _a = Selection, _Selection_sql = new WeakMap(), _Selection_instances = new WeakS
|
|
|
745
718
|
return new _a(sql);
|
|
746
719
|
};
|
|
747
720
|
|
|
748
|
-
|
|
749
|
-
|
|
721
|
+
class SqlChainModify extends SqlStatement {
|
|
722
|
+
constructor(sql) {
|
|
723
|
+
super();
|
|
724
|
+
this.sql = sql;
|
|
725
|
+
}
|
|
750
726
|
returning(returns) {
|
|
751
727
|
if (typeof returns === "function")
|
|
752
728
|
returns = returns();
|
|
@@ -755,11 +731,10 @@ class AfterUpdateOrReturn extends SqlQueryStatement {
|
|
|
755
731
|
columnsStr = "*";
|
|
756
732
|
}
|
|
757
733
|
else {
|
|
758
|
-
|
|
759
|
-
columnsStr = res.sqlColumns;
|
|
734
|
+
columnsStr = selectColumns(returns);
|
|
760
735
|
}
|
|
761
736
|
let sql = this.toString() + "\nRETURNING " + columnsStr;
|
|
762
|
-
return new
|
|
737
|
+
return new SqlTextStatementDataset(sql);
|
|
763
738
|
}
|
|
764
739
|
onConflict(onConflict) {
|
|
765
740
|
if (typeof onConflict === "function")
|
|
@@ -767,22 +742,24 @@ class AfterUpdateOrReturn extends SqlQueryStatement {
|
|
|
767
742
|
if (typeof onConflict !== "string")
|
|
768
743
|
onConflict = onConflict.join(",");
|
|
769
744
|
let sql = this.toString() + `\nON CONFLICT (${onConflict})`;
|
|
770
|
-
return new
|
|
745
|
+
return new SqlInsertConflictBranch(sql);
|
|
771
746
|
}
|
|
772
747
|
where(where$1) {
|
|
773
748
|
const sql = where(where$1);
|
|
774
|
-
return new
|
|
749
|
+
return new SqlChainModify(this.toString() + sql);
|
|
750
|
+
}
|
|
751
|
+
toString() {
|
|
752
|
+
return this.sql;
|
|
775
753
|
}
|
|
776
754
|
}
|
|
777
|
-
class
|
|
755
|
+
class SqlInsertConflictBranch {
|
|
778
756
|
constructor(sql) {
|
|
779
|
-
|
|
780
|
-
__classPrivateFieldSet(this, _AfterInsert_sql, sql, "f");
|
|
757
|
+
this.sql = sql;
|
|
781
758
|
}
|
|
782
759
|
doUpdate(set) {
|
|
783
760
|
if (typeof set === "function")
|
|
784
761
|
set = set();
|
|
785
|
-
let sql = this.
|
|
762
|
+
let sql = this.sql;
|
|
786
763
|
if (typeof set === "object") {
|
|
787
764
|
sql += "\nDO UPDATE ";
|
|
788
765
|
sql += createUpdateSetFromObject(set);
|
|
@@ -791,24 +768,19 @@ class AfterInsert {
|
|
|
791
768
|
sql += "DO UPDATE SET\n" + set;
|
|
792
769
|
else
|
|
793
770
|
sql += "DO NOTHING";
|
|
794
|
-
return new
|
|
771
|
+
return new SqlChainModify(sql);
|
|
795
772
|
}
|
|
796
773
|
doNotThing() {
|
|
797
|
-
return new
|
|
798
|
-
}
|
|
799
|
-
toString() {
|
|
800
|
-
return __classPrivateFieldGet(this, _AfterInsert_sql, "f");
|
|
774
|
+
return new SqlChainModify(this.sql + " DO NOTHING");
|
|
801
775
|
}
|
|
802
776
|
}
|
|
803
|
-
_AfterInsert_sql = new WeakMap();
|
|
804
777
|
|
|
805
778
|
/**
|
|
806
779
|
* 数据库表
|
|
807
780
|
* @public
|
|
808
781
|
*/
|
|
809
|
-
class DbTable
|
|
782
|
+
class DbTable {
|
|
810
783
|
constructor(name) {
|
|
811
|
-
super();
|
|
812
784
|
this.name = name;
|
|
813
785
|
}
|
|
814
786
|
fromAs(as) {
|
|
@@ -832,7 +804,7 @@ class DbTable extends SqlSelectable {
|
|
|
832
804
|
if (typeof values !== "string")
|
|
833
805
|
throw new TypeError("values 必须是 string 或 function 类型");
|
|
834
806
|
let sql = `INSERT INTO ${this.name}(${columns})\n${values}`;
|
|
835
|
-
return new
|
|
807
|
+
return new SqlChainModify(sql);
|
|
836
808
|
}
|
|
837
809
|
/**
|
|
838
810
|
* UPDATE 语句,需要注意 SQL 注入
|
|
@@ -848,10 +820,10 @@ class DbTable extends SqlSelectable {
|
|
|
848
820
|
switch (typeof values) {
|
|
849
821
|
case "object": {
|
|
850
822
|
let sql = createUpdateSetFromObject(values);
|
|
851
|
-
return new
|
|
823
|
+
return new SqlChainModify("UPDATE " + this.name + " " + sql);
|
|
852
824
|
}
|
|
853
825
|
case "string":
|
|
854
|
-
return new
|
|
826
|
+
return new SqlChainModify("UPDATE " + this.name + " SET\n" + values);
|
|
855
827
|
default:
|
|
856
828
|
throw new TypeError("参数 values 错误");
|
|
857
829
|
}
|
|
@@ -859,14 +831,11 @@ class DbTable extends SqlSelectable {
|
|
|
859
831
|
delete(option = {}) {
|
|
860
832
|
let sql = "DELETE FROM " + this.name;
|
|
861
833
|
sql += where(option.where);
|
|
862
|
-
return new
|
|
834
|
+
return new SqlChainModify(sql);
|
|
863
835
|
}
|
|
864
836
|
toSelect() {
|
|
865
837
|
return this.name;
|
|
866
838
|
}
|
|
867
|
-
toString() {
|
|
868
|
-
return this.name;
|
|
869
|
-
}
|
|
870
839
|
}
|
|
871
840
|
|
|
872
841
|
/** @public */
|
|
@@ -899,7 +868,7 @@ class DbTableQuery extends DbTable {
|
|
|
899
868
|
throw new Error("插入列不能为空");
|
|
900
869
|
const columnStr = insertCol.join(",");
|
|
901
870
|
let sql = `INSERT INTO ${this.name} (${columnStr})\n${valuesStr}`;
|
|
902
|
-
return new
|
|
871
|
+
return new SqlChainModify(sql);
|
|
903
872
|
}
|
|
904
873
|
/**
|
|
905
874
|
* UPDATE 语句,与 update() 不同的是,它会将值进行安全转换
|
|
@@ -927,7 +896,7 @@ class DbTableQuery extends DbTable {
|
|
|
927
896
|
if (!setStr)
|
|
928
897
|
throw new Error("值不能为空");
|
|
929
898
|
let sql = `UPDATE ${this.name} SET\n${setStr}`;
|
|
930
|
-
return new
|
|
899
|
+
return new SqlChainModify(sql);
|
|
931
900
|
}
|
|
932
901
|
}
|
|
933
902
|
|
|
@@ -1109,4 +1078,4 @@ class YourTable extends DbTableQuery {
|
|
|
1109
1078
|
*/
|
|
1110
1079
|
const v = SqlValuesCreator.create(pgSqlTransformer);
|
|
1111
1080
|
|
|
1112
|
-
export { ColumnMeta, CustomDbType, DbTable, DbTableQuery, Selection,
|
|
1081
|
+
export { ColumnMeta, CustomDbType, DbTable, DbTableQuery, Selection, SqlRaw, SqlSelectChain, SqlStatement, SqlStatementDataset, SqlTextStatementDataset, SqlValuesCreator, TypeChecker, YourTable, YourTypeMap, getObjectListKeys, having, orderBy, pgSqlTransformer, selectColumns, v, where };
|
package/dist/select/DbTable.d.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { SqlSelectable } from "./selectable.ts";
|
|
2
1
|
import { ConditionParam, Constructable } from "../util.ts";
|
|
3
2
|
import type { TableType } from "./type.ts";
|
|
4
|
-
import {
|
|
3
|
+
import { Selection } from "./query_chain_select.ts";
|
|
4
|
+
import { ChainModifyWhere, ChainOnConflict, ChainSelectWhere } from "./query_chain_abstract.ts";
|
|
5
5
|
/**
|
|
6
6
|
* 数据库表
|
|
7
7
|
* @public
|
|
8
8
|
*/
|
|
9
|
-
export declare class DbTable<T extends TableType>
|
|
9
|
+
export declare class DbTable<T extends TableType> {
|
|
10
10
|
readonly name: string;
|
|
11
11
|
constructor(name: string);
|
|
12
12
|
fromAs(as?: string): Selection;
|
|
13
13
|
/** 选择单表全部列 */
|
|
14
|
-
select(columns: "*", as?: string):
|
|
14
|
+
select(columns: "*", as?: string): ChainSelectWhere<T>;
|
|
15
15
|
/** 选择单表 */
|
|
16
|
-
select(columns: Constructable<Record<string, boolean | string> | string>, as?: string):
|
|
16
|
+
select(columns: Constructable<Record<string, boolean | string> | string>, as?: string): ChainSelectWhere<Record<string, any>>;
|
|
17
17
|
/** 选择单表 */
|
|
18
18
|
select<R extends {}>(columns: Constructable<{
|
|
19
19
|
[key in keyof R]: boolean | string;
|
|
20
|
-
} | string>, as?: string):
|
|
20
|
+
} | string>, as?: string): ChainSelectWhere<R>;
|
|
21
21
|
/**
|
|
22
22
|
* INSERT 语句,需要注意 SQL 注入
|
|
23
23
|
* @example
|
|
@@ -25,7 +25,7 @@ export declare class DbTable<T extends TableType> extends SqlSelectable<T> {
|
|
|
25
25
|
* table.insert(["age","name"], "VALUES (18, 'hi'), (17, 'hh')") // INSERT INTO table(age,name) VALUES(18, 'hi'), (17, 'hh')
|
|
26
26
|
* ```
|
|
27
27
|
*/
|
|
28
|
-
insert(columns: string, values: Constructable<string>):
|
|
28
|
+
insert(columns: string, values: Constructable<string>): ChainOnConflict<T>;
|
|
29
29
|
/**
|
|
30
30
|
* UPDATE 语句,需要注意 SQL 注入
|
|
31
31
|
* @example
|
|
@@ -36,10 +36,9 @@ export declare class DbTable<T extends TableType> extends SqlSelectable<T> {
|
|
|
36
36
|
*/
|
|
37
37
|
update(values: Constructable<{
|
|
38
38
|
[key in keyof T]?: string;
|
|
39
|
-
} | string>):
|
|
40
|
-
delete(option?: DeleteOption):
|
|
39
|
+
} | string>): ChainModifyWhere<T>;
|
|
40
|
+
delete(option?: DeleteOption): ChainModifyWhere<T>;
|
|
41
41
|
toSelect(): string;
|
|
42
|
-
toString(): string;
|
|
43
42
|
}
|
|
44
43
|
/** @public */
|
|
45
44
|
export interface DeleteOption {
|
|
@@ -2,7 +2,7 @@ import { SqlValuesCreator } from "../sql_value/sql_value.ts";
|
|
|
2
2
|
import { UpdateRowValue, TableType } from "./type.ts";
|
|
3
3
|
import { Constructable } from "../util.ts";
|
|
4
4
|
import { DbTable } from "./DbTable.ts";
|
|
5
|
-
import
|
|
5
|
+
import { ChainModifyWhere, ChainOnConflict } from "./query_chain_abstract.ts";
|
|
6
6
|
/** @public */
|
|
7
7
|
export declare class DbTableQuery<T extends TableType = Record<string, any>, C extends TableType = Partial<T>> extends DbTable<T> {
|
|
8
8
|
private statement;
|
|
@@ -14,8 +14,8 @@ export declare class DbTableQuery<T extends TableType = Record<string, any>, C e
|
|
|
14
14
|
* table.insert([{age:18, name:"hi"}, {age:17, name:"hh"}]) // INSERT INTO table(age,name) VALUES(18, 'hi'), (17, 'hh')
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
|
-
insert(values: Constructable<UpdateRowValue<C> | UpdateRowValue<C>[]>):
|
|
18
|
-
insert(columns: string, values: Constructable<string>):
|
|
17
|
+
insert(values: Constructable<UpdateRowValue<C> | UpdateRowValue<C>[]>): ChainOnConflict<T>;
|
|
18
|
+
insert(columns: string, values: Constructable<string>): ChainOnConflict<T>;
|
|
19
19
|
/**
|
|
20
20
|
* UPDATE 语句,与 update() 不同的是,它会将值进行安全转换
|
|
21
21
|
* @example
|
|
@@ -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>>): ChainModifyWhere<T>;
|
|
27
27
|
}
|
|
28
28
|
//# sourceMappingURL=TableQuery.d.ts.map
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { ConditionParam, Constructable, OrderByParam, SelectParam } from "../util.ts";
|
|
2
|
+
import { TableType } from "./type.ts";
|
|
3
|
+
/** @public */
|
|
4
|
+
export declare abstract class SqlStatement {
|
|
5
|
+
/** 获取 SQL 语句 */
|
|
6
|
+
abstract toString(): string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* 可选择项。可以是 table、查询结果等,它能被 select 语句选择
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* declare const item: SqlStatementDataset
|
|
13
|
+
* await query(`select * from ${item.toSelect()}`)
|
|
14
|
+
*
|
|
15
|
+
* ```
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
export interface SqlSelectable {
|
|
19
|
+
/**
|
|
20
|
+
* 转成子选择语句, 你可以使用 select form xxx 选择
|
|
21
|
+
* 如果是 table 则是 table name
|
|
22
|
+
* 如果是 选择语句,则是 (xxx)
|
|
23
|
+
*/
|
|
24
|
+
toSelect(): string;
|
|
25
|
+
}
|
|
26
|
+
/** @public */
|
|
27
|
+
export declare abstract class SqlStatementDataset<T> extends SqlStatement implements SqlSelectable {
|
|
28
|
+
/**
|
|
29
|
+
* 转成子选择语句, 你可以使用 select form xxx 选择
|
|
30
|
+
* 如果是 table 则是 table name
|
|
31
|
+
* 如果是 选择语句,则是 (xxx)
|
|
32
|
+
*/
|
|
33
|
+
toSelect(): string;
|
|
34
|
+
}
|
|
35
|
+
/** @public */
|
|
36
|
+
export interface ChainSelectLimit<T extends TableType> extends SqlStatementDataset<T> {
|
|
37
|
+
limit(limit?: number | bigint, offset?: number | bigint): SqlStatementDataset<T>;
|
|
38
|
+
}
|
|
39
|
+
/** @public */
|
|
40
|
+
export interface ChainSelectOrderBy<T extends TableType> extends ChainSelectLimit<T> {
|
|
41
|
+
orderBy(param: Constructable<OrderByParam | void>): ChainSelectLimit<T>;
|
|
42
|
+
}
|
|
43
|
+
/** @public */
|
|
44
|
+
export interface ChainSelectHaving<T extends TableType> extends ChainSelectOrderBy<T> {
|
|
45
|
+
having(param: Constructable<ConditionParam | void>): ChainSelectLimit<T>;
|
|
46
|
+
}
|
|
47
|
+
/** @public */
|
|
48
|
+
export interface ChainSelectGroupBy<T extends TableType> extends ChainSelectOrderBy<T> {
|
|
49
|
+
groupBy(columns: string | string[]): ChainSelectHaving<T>;
|
|
50
|
+
}
|
|
51
|
+
/** @public */
|
|
52
|
+
export interface ChainSelectWhere<T extends TableType> extends ChainSelectGroupBy<T> {
|
|
53
|
+
where(param: Constructable<ConditionParam | void>): ChainSelectGroupBy<T>;
|
|
54
|
+
}
|
|
55
|
+
/** @public */
|
|
56
|
+
export interface ChainModifyReturning<T extends TableType = {}> extends SqlStatement {
|
|
57
|
+
returning(columns: "*"): SqlStatementDataset<T>;
|
|
58
|
+
returning(columns: Constructable<SelectParam>): SqlStatementDataset<Record<string, any>>;
|
|
59
|
+
returning<R extends TableType>(columns: Constructable<SelectParam>): SqlStatementDataset<R>;
|
|
60
|
+
}
|
|
61
|
+
/** @public */
|
|
62
|
+
export interface ChainModifyWhere<T extends TableType = {}> extends ChainModifyReturning<T> {
|
|
63
|
+
where(where: Constructable<ConditionParam | void>): ChainModifyReturning<T>;
|
|
64
|
+
}
|
|
65
|
+
/** @public */
|
|
66
|
+
export interface ChainConflictDo<T extends TableType = {}> {
|
|
67
|
+
doNotThing(): ChainModifyReturning<T>;
|
|
68
|
+
/**
|
|
69
|
+
* 需要注意 SQL 注入
|
|
70
|
+
*/
|
|
71
|
+
doUpdate(set: Constructable<string | {
|
|
72
|
+
[key in keyof T]?: string;
|
|
73
|
+
}>): ChainModifyWhere<T>;
|
|
74
|
+
toString(): string;
|
|
75
|
+
}
|
|
76
|
+
/** @public */
|
|
77
|
+
export interface ChainOnConflict<T extends TableType = {}> extends ChainModifyReturning<T> {
|
|
78
|
+
onConflict(option: Constructable<readonly (keyof T)[] | string>): ChainConflictDo<T>;
|
|
79
|
+
}
|
|
80
|
+
/** @public */
|
|
81
|
+
export declare class SqlTextStatementDataset<T> extends SqlStatementDataset<T> {
|
|
82
|
+
readonly sql: string;
|
|
83
|
+
constructor(sql: string);
|
|
84
|
+
toString(): string;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* 推断查询结果的类型
|
|
88
|
+
* @public
|
|
89
|
+
*/
|
|
90
|
+
export type InferQueryResult<T> = T extends SqlStatementDataset<infer P> ? P : never;
|
|
91
|
+
//# sourceMappingURL=query_chain_abstract.d.ts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ConditionParam, Constructable, SelectParam } from "../util.ts";
|
|
2
|
+
import { SqlStatementDataset, SqlStatement, ChainModifyWhere, ChainOnConflict, ChainConflictDo, ChainModifyReturning } from "./query_chain_abstract.ts";
|
|
3
|
+
import { TableType } from "./type.ts";
|
|
4
|
+
export declare class SqlChainModify<T extends TableType = {}> extends SqlStatement implements ChainOnConflict<T>, ChainModifyWhere<T> {
|
|
5
|
+
readonly sql: string;
|
|
6
|
+
constructor(sql: string);
|
|
7
|
+
returning<R extends {}>(returns: Constructable<SelectParam | "*">): SqlStatementDataset<R>;
|
|
8
|
+
onConflict(onConflict: Constructable<readonly string[] | string>): ChainConflictDo<T>;
|
|
9
|
+
where(where: Constructable<ConditionParam | void>): ChainModifyReturning<T>;
|
|
10
|
+
toString(): string;
|
|
11
|
+
}
|
|
12
|
+
export declare class SqlInsertConflictBranch<T extends TableType = {}> implements ChainConflictDo<T> {
|
|
13
|
+
readonly sql: string;
|
|
14
|
+
constructor(sql: string);
|
|
15
|
+
doUpdate(set?: Constructable<string | {
|
|
16
|
+
[key: string]: string | undefined;
|
|
17
|
+
}>): ChainModifyWhere<T>;
|
|
18
|
+
doNotThing(): ChainModifyReturning<T>;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=query_chain_insert.d.ts.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { ChainSelectGroupBy, ChainSelectHaving, ChainSelectLimit, ChainSelectWhere, SqlSelectable, SqlStatementDataset, SqlTextStatementDataset } from "./query_chain_abstract.ts";
|
|
2
|
+
import { OrderByParam, ConditionParam, SelectParam, Constructable } from "../util.ts";
|
|
3
|
+
import type { TableType } from "./type.ts";
|
|
4
|
+
/**
|
|
5
|
+
* @public ChainSelectWhere 的默认实现
|
|
6
|
+
*/
|
|
7
|
+
export declare class SqlSelectChain<T extends TableType> extends SqlTextStatementDataset<T> implements ChainSelectWhere<T> {
|
|
8
|
+
where(param?: Constructable<ConditionParam | void>): ChainSelectGroupBy<T>;
|
|
9
|
+
groupBy(columns: string | string[]): ChainSelectHaving<T>;
|
|
10
|
+
having(param?: Constructable<ConditionParam | void>): ChainSelectLimit<T>;
|
|
11
|
+
orderBy(param?: Constructable<OrderByParam | void>): ChainSelectLimit<T>;
|
|
12
|
+
limit(limit?: number, offset?: number): SqlStatementDataset<T>;
|
|
13
|
+
}
|
|
14
|
+
/** @public */
|
|
15
|
+
export declare class Selection {
|
|
16
|
+
#private;
|
|
17
|
+
static from(selectable: Constructable<SqlSelectable | string>, as?: string): Selection;
|
|
18
|
+
constructor(selectable: Constructable<SqlSelectable | string>, as?: string);
|
|
19
|
+
toString(): string;
|
|
20
|
+
fullJoin(selectable: Constructable<SqlSelectable | string>, as: string | undefined, on: Constructable<ConditionParam>): Selection;
|
|
21
|
+
innerJoin(selectable: Constructable<SqlSelectable | string>, as: string | undefined, on: Constructable<ConditionParam>): Selection;
|
|
22
|
+
leftJoin(selectable: Constructable<SqlSelectable | string>, as: string | undefined, on: Constructable<ConditionParam>): Selection;
|
|
23
|
+
rightJoin(selectable: Constructable<SqlSelectable | string>, as: string | undefined, on: Constructable<ConditionParam>): Selection;
|
|
24
|
+
naturalJoin(selectable: Constructable<SqlSelectable | string>, as?: string | undefined): Selection;
|
|
25
|
+
crossJoin(selectable: Constructable<SqlSelectable | string>, as?: string | undefined): Selection;
|
|
26
|
+
from(selectable: Constructable<SqlSelectable | string>, as?: string): Selection;
|
|
27
|
+
/** 选择全部列 */
|
|
28
|
+
select<T extends TableType = Record<string, any>>(columns: "*"): ChainSelectWhere<T>;
|
|
29
|
+
/**
|
|
30
|
+
* 自定义SQL选择语句
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* selection.select("t.age, count(*) AS c") // SELECT t.age,count(*) AS c FROM ...
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
select(columns: Constructable<SelectParam>): ChainSelectWhere<Record<string, any>>;
|
|
37
|
+
/**
|
|
38
|
+
* 通过 object 选择 列
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* selection.select({"age":true, c:"count(*)"}) // SELECT age,count(*) AS c FROM ...
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
select<T extends TableType>(columns: Constructable<{
|
|
45
|
+
[key in keyof T]: string | boolean;
|
|
46
|
+
} | string>): ChainSelectWhere<T>;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=query_chain_select.d.ts.map
|
package/dist/select/type.d.ts
CHANGED
|
@@ -20,26 +20,8 @@ export type UpdateRowValue<T extends object> = {
|
|
|
20
20
|
};
|
|
21
21
|
/** @public */
|
|
22
22
|
export type OrderValue = "ASC" | "DESC";
|
|
23
|
-
/**
|
|
24
|
-
* 表的选择参数
|
|
25
|
-
* @public
|
|
26
|
-
*/
|
|
27
|
-
export type ColumnsSelected<T extends TableType> = {
|
|
28
|
-
[key in keyof T]?: boolean | string;
|
|
29
|
-
};
|
|
30
|
-
/**
|
|
31
|
-
* 从一个表格选择列,生成新的表格类型
|
|
32
|
-
* @public
|
|
33
|
-
*/
|
|
34
|
-
export type SelectColumns<T extends TableType, R extends ColumnsSelected<T>> = R extends {
|
|
35
|
-
[key in keyof T]?: boolean | string;
|
|
36
|
-
} ? {
|
|
37
|
-
[key in keyof T as R[key] extends true ? key : StringOnly<R[key]>]: T[key];
|
|
38
|
-
} : never;
|
|
39
|
-
type StringOnly<T> = T extends string ? T : never;
|
|
40
23
|
/** @public */
|
|
41
24
|
export type TableType = {
|
|
42
25
|
[key: string]: any;
|
|
43
26
|
};
|
|
44
|
-
export {};
|
|
45
27
|
//# sourceMappingURL=type.d.ts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SqlStatementDataset } from "../select/query_chain_abstract.ts";
|
|
2
2
|
declare const SQL_RAW: unique symbol;
|
|
3
3
|
/**
|
|
4
4
|
* SQL 原始字符类。可以使用 String 类代替,这只是为了推断类型
|
|
@@ -96,7 +96,7 @@ export declare class SqlValuesCreator {
|
|
|
96
96
|
sqlType: string;
|
|
97
97
|
sqlDefault?: string;
|
|
98
98
|
assertJsType?: AssertJsType;
|
|
99
|
-
}>):
|
|
99
|
+
}>): SqlStatementDataset<T>;
|
|
100
100
|
}
|
|
101
101
|
/** @public */
|
|
102
102
|
export type ColumnToValueConfig = {
|
package/dist/util.d.ts
CHANGED
|
@@ -30,7 +30,7 @@ export declare function where(conditions?: Constructable<ConditionParam | void>,
|
|
|
30
30
|
*/
|
|
31
31
|
export declare function having(conditions?: Constructable<ConditionParam | void>, type?: "AND" | "OR"): string;
|
|
32
32
|
/** @public */
|
|
33
|
-
export type SelectParam = string | Record<string, string | boolean>;
|
|
33
|
+
export type SelectParam = string | string[] | Record<string, string | boolean>;
|
|
34
34
|
/**
|
|
35
35
|
* @public
|
|
36
36
|
* @example
|
package/package.json
CHANGED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { ConditionParam, Constructable } from "../util.ts";
|
|
2
|
-
import { CurrentModifyWhere, CurrentOnConflict, CurrentOnConflictDo, CurrentReturn } from "./query_link.ts";
|
|
3
|
-
import { SqlQueryStatement } from "./selectable.ts";
|
|
4
|
-
import { ColumnsSelected, TableType } from "./type.ts";
|
|
5
|
-
export declare class AfterUpdateOrReturn<T extends TableType = {}> extends SqlQueryStatement<{}> implements CurrentOnConflict<T>, CurrentModifyWhere<T> {
|
|
6
|
-
returning<R extends {}>(returns: Constructable<ColumnsSelected<any> | "*">): SqlQueryStatement<R>;
|
|
7
|
-
onConflict(onConflict: Constructable<readonly string[] | string>): CurrentOnConflictDo<T>;
|
|
8
|
-
where(where: Constructable<ConditionParam | void>): CurrentReturn<T>;
|
|
9
|
-
}
|
|
10
|
-
export declare class AfterInsert<T extends TableType = {}> implements CurrentOnConflictDo<T> {
|
|
11
|
-
#private;
|
|
12
|
-
constructor(sql: string);
|
|
13
|
-
doUpdate(set?: Constructable<string | {
|
|
14
|
-
[key: string]: string | undefined;
|
|
15
|
-
}>): CurrentModifyWhere<T>;
|
|
16
|
-
doNotThing(): CurrentReturn<T>;
|
|
17
|
-
toString(): string;
|
|
18
|
-
}
|
|
19
|
-
//# sourceMappingURL=_update_impl.d.ts.map
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { SqlSelectable, SqlQueryStatement } from "./selectable.ts";
|
|
2
|
-
import { OrderByParam, ConditionParam, SelectParam, Constructable } from "../util.ts";
|
|
3
|
-
import type { ColumnsSelected, TableType } from "./type.ts";
|
|
4
|
-
/** @public */
|
|
5
|
-
export interface CurrentLimit<T extends TableType> extends SqlQueryStatement<T> {
|
|
6
|
-
limit(limit?: number | bigint, offset?: number | bigint): SqlQueryStatement<T>;
|
|
7
|
-
}
|
|
8
|
-
/** @public */
|
|
9
|
-
export interface CurrentOrderBy<T extends TableType> extends CurrentLimit<T> {
|
|
10
|
-
orderBy(param: Constructable<OrderByParam | void>): CurrentLimit<T>;
|
|
11
|
-
}
|
|
12
|
-
/** @public */
|
|
13
|
-
export interface CurrentHaving<T extends TableType> extends CurrentOrderBy<T> {
|
|
14
|
-
having(param: Constructable<ConditionParam | void>): CurrentLimit<T>;
|
|
15
|
-
}
|
|
16
|
-
/** @public */
|
|
17
|
-
export interface CurrentGroupBy<T extends TableType> extends CurrentOrderBy<T> {
|
|
18
|
-
groupBy(columns: string | string[]): CurrentHaving<T>;
|
|
19
|
-
}
|
|
20
|
-
/** @public */
|
|
21
|
-
export interface CurrentWhere<T extends TableType> extends CurrentGroupBy<T> {
|
|
22
|
-
where(param: Constructable<ConditionParam | void>): CurrentGroupBy<T>;
|
|
23
|
-
}
|
|
24
|
-
/** @public */
|
|
25
|
-
export declare class Selection {
|
|
26
|
-
#private;
|
|
27
|
-
static from(selectable: Constructable<SqlSelectable<any> | string>, as?: string): Selection;
|
|
28
|
-
constructor(selectable: Constructable<SqlSelectable<any> | string>, as?: string);
|
|
29
|
-
toString(): string;
|
|
30
|
-
fullJoin(selectable: Constructable<SqlSelectable<any> | string>, as: string | undefined, on: Constructable<ConditionParam>): Selection;
|
|
31
|
-
innerJoin(selectable: Constructable<SqlSelectable<any> | string>, as: string | undefined, on: Constructable<ConditionParam>): Selection;
|
|
32
|
-
leftJoin(selectable: Constructable<SqlSelectable<any> | string>, as: string | undefined, on: Constructable<ConditionParam>): Selection;
|
|
33
|
-
rightJoin(selectable: Constructable<SqlSelectable<any> | string>, as: string | undefined, on: Constructable<ConditionParam>): Selection;
|
|
34
|
-
naturalJoin(selectable: Constructable<SqlSelectable<any> | string>, as?: string | undefined): Selection;
|
|
35
|
-
crossJoin(selectable: Constructable<SqlSelectable<any> | string>, as?: string | undefined): Selection;
|
|
36
|
-
from(selectable: Constructable<SqlSelectable<any> | string>, as?: string): Selection;
|
|
37
|
-
/** 选择全部列 */
|
|
38
|
-
select<T extends TableType = Record<string, any>>(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(columns: Constructable<SelectParam>): CurrentWhere<Record<string, any>>;
|
|
47
|
-
/**
|
|
48
|
-
* 通过 object 选择 列
|
|
49
|
-
* @example
|
|
50
|
-
* ```ts
|
|
51
|
-
* selection.select({"age":true, c:"count(*)"}) // SELECT age,count(*) AS c FROM ...
|
|
52
|
-
* ```
|
|
53
|
-
*/
|
|
54
|
-
select<T extends TableType>(columns: Constructable<{
|
|
55
|
-
[key in keyof T]: string | boolean;
|
|
56
|
-
} | string>): CurrentWhere<T>;
|
|
57
|
-
}
|
|
58
|
-
/** @public */
|
|
59
|
-
export interface CurrentReturn<T extends TableType = {}> extends SqlQueryStatement<{}> {
|
|
60
|
-
returning(columns: "*"): SqlQueryStatement<T>;
|
|
61
|
-
returning(columns: Constructable<ColumnsSelected<T> | string>): SqlQueryStatement<Record<string, any>>;
|
|
62
|
-
returning<R extends TableType>(columns: Constructable<ColumnsSelected<R> | string>): SqlQueryStatement<R>;
|
|
63
|
-
}
|
|
64
|
-
/** @public */
|
|
65
|
-
export type CurrentModifyWhere<T extends TableType = {}> = CurrentReturn<T> & {
|
|
66
|
-
where(where: Constructable<ConditionParam | void>): CurrentReturn<T>;
|
|
67
|
-
};
|
|
68
|
-
/** @public */
|
|
69
|
-
export type CurrentOnConflictDo<T extends TableType = {}> = {
|
|
70
|
-
doNotThing(): CurrentReturn<T>;
|
|
71
|
-
/**
|
|
72
|
-
* 需要注意 SQL 注入
|
|
73
|
-
*/
|
|
74
|
-
doUpdate(set: Constructable<string | {
|
|
75
|
-
[key in keyof T]?: string;
|
|
76
|
-
}>): CurrentModifyWhere<T>;
|
|
77
|
-
toString(): string;
|
|
78
|
-
};
|
|
79
|
-
/** @public */
|
|
80
|
-
export type CurrentOnConflict<T extends TableType = {}> = CurrentReturn<T> & {
|
|
81
|
-
onConflict(option: Constructable<readonly (keyof T)[] | string>): CurrentOnConflictDo<T>;
|
|
82
|
-
};
|
|
83
|
-
//# sourceMappingURL=query_link.d.ts.map
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { TableType } from "./type.ts";
|
|
2
|
-
declare const SQL_SELECTABLE: unique symbol;
|
|
3
|
-
/**
|
|
4
|
-
* 可选择项。可以是 table、查询结果等,它能被 select 语句选择
|
|
5
|
-
* @example
|
|
6
|
-
* ```ts
|
|
7
|
-
* declare const item: SqlSelectable<any>
|
|
8
|
-
* await query(`select * from ${item.toSelect()}`)
|
|
9
|
-
*
|
|
10
|
-
* ```
|
|
11
|
-
* @public
|
|
12
|
-
*/
|
|
13
|
-
export declare abstract class SqlSelectable<T extends TableType> {
|
|
14
|
-
/**
|
|
15
|
-
* 转成子选择语句, 你可以使用 select form xxx 选择
|
|
16
|
-
* 如果是 table 则是 table name
|
|
17
|
-
* 如果是 选择语句,则是 (xxx)
|
|
18
|
-
*/
|
|
19
|
-
abstract toSelect(): string;
|
|
20
|
-
/** 获取 SQL 语句 */
|
|
21
|
-
abstract toString(): string;
|
|
22
|
-
/** 保留以推断类型 */
|
|
23
|
-
protected [SQL_SELECTABLE]: T;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* SELECT 以及 UPDATE、DELETE、INSERT INTO 带结果的 SQL 语句
|
|
27
|
-
* @public
|
|
28
|
-
*/
|
|
29
|
-
export declare class SqlQueryStatement<T extends TableType = TableType> extends SqlSelectable<T> {
|
|
30
|
-
#private;
|
|
31
|
-
constructor(sql: string | SqlQueryStatement);
|
|
32
|
-
toString(): string;
|
|
33
|
-
toSelect(): string;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* 推断查询结果的类型
|
|
37
|
-
* @public
|
|
38
|
-
*/
|
|
39
|
-
export type InferQueryResult<T> = T extends SqlSelectable<infer P> ? (P extends TableType ? P : never) : never;
|
|
40
|
-
export {};
|
|
41
|
-
//# sourceMappingURL=selectable.d.ts.map
|