@asla/yoursql 0.7.0 → 0.8.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/mod.d.ts +2 -2
- package/dist/mod.js +52 -62
- package/dist/select/DbTable.d.ts +11 -11
- package/dist/select/TableQuery.d.ts +4 -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/sql_value/sql_value.d.ts +2 -2
- package/package.json +1 -1
- package/dist/select/_update_impl.d.ts +0 -19
- package/dist/select/query_link.d.ts +0 -84
- 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
|
@@ -275,37 +275,30 @@ function handlerOrderValue(value) {
|
|
|
275
275
|
}
|
|
276
276
|
}
|
|
277
277
|
|
|
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 {
|
|
278
|
+
/** @public */
|
|
279
|
+
class SqlStatement {
|
|
290
280
|
}
|
|
291
|
-
/**
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
281
|
+
/** @public */
|
|
282
|
+
class SqlStatementDataset extends SqlStatement {
|
|
283
|
+
/**
|
|
284
|
+
* 转成子选择语句, 你可以使用 select form xxx 选择
|
|
285
|
+
* 如果是 table 则是 table name
|
|
286
|
+
* 如果是 选择语句,则是 (xxx)
|
|
287
|
+
*/
|
|
288
|
+
toSelect() {
|
|
289
|
+
return "(" + this.toString() + ")";
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
/** @public */
|
|
293
|
+
class SqlTextStatementDataset extends SqlStatementDataset {
|
|
296
294
|
constructor(sql) {
|
|
297
295
|
super();
|
|
298
|
-
|
|
299
|
-
__classPrivateFieldSet(this, _SqlQueryStatement_sql, sql.toString(), "f");
|
|
296
|
+
this.sql = sql;
|
|
300
297
|
}
|
|
301
298
|
toString() {
|
|
302
|
-
return
|
|
303
|
-
}
|
|
304
|
-
toSelect() {
|
|
305
|
-
return "(" + this.toString() + ")";
|
|
299
|
+
return this.sql;
|
|
306
300
|
}
|
|
307
301
|
}
|
|
308
|
-
_SqlQueryStatement_sql = new WeakMap();
|
|
309
302
|
|
|
310
303
|
var _YourValuesAs_asName, _YourValuesAs_valuesStr, _YourValuesAs_sql;
|
|
311
304
|
/**
|
|
@@ -568,7 +561,7 @@ class SqlValuesCreator {
|
|
|
568
561
|
return new YourValuesAs(insertKeys, asName, valuesStr.join(",\n"));
|
|
569
562
|
}
|
|
570
563
|
}
|
|
571
|
-
class YourValuesAs extends
|
|
564
|
+
class YourValuesAs extends SqlStatementDataset {
|
|
572
565
|
constructor(columns, asName, valuesStr) {
|
|
573
566
|
super();
|
|
574
567
|
_YourValuesAs_asName.set(this, void 0);
|
|
@@ -646,12 +639,12 @@ const pgSqlTransformer = new Map([
|
|
|
646
639
|
]);
|
|
647
640
|
|
|
648
641
|
var _Selection_instances, _a, _Selection_sql, _Selection_join;
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
642
|
+
/**
|
|
643
|
+
* @public ChainSelectWhere 的默认实现
|
|
644
|
+
*/
|
|
645
|
+
class SqlSelectChain extends SqlTextStatementDataset {
|
|
653
646
|
where(param) {
|
|
654
|
-
return new
|
|
647
|
+
return new SqlSelectChain(this.toString() + where(param));
|
|
655
648
|
}
|
|
656
649
|
groupBy(columns) {
|
|
657
650
|
let sql = this.toString();
|
|
@@ -659,13 +652,13 @@ class AfterSelectImpl extends SqlQueryStatement {
|
|
|
659
652
|
sql += " GROUP BY " + columns;
|
|
660
653
|
else
|
|
661
654
|
sql += " GROUP BY " + columns.join(",");
|
|
662
|
-
return new
|
|
655
|
+
return new SqlSelectChain(sql);
|
|
663
656
|
}
|
|
664
657
|
having(param) {
|
|
665
|
-
return new
|
|
658
|
+
return new SqlSelectChain(this.toString() + having(param));
|
|
666
659
|
}
|
|
667
660
|
orderBy(param) {
|
|
668
|
-
return new
|
|
661
|
+
return new SqlSelectChain(this.toString() + orderBy(param));
|
|
669
662
|
}
|
|
670
663
|
limit(limit, offset) {
|
|
671
664
|
let sql = this.toString();
|
|
@@ -684,7 +677,7 @@ class AfterSelectImpl extends SqlQueryStatement {
|
|
|
684
677
|
else
|
|
685
678
|
throw new TypeError("offset 必须是个整数:" + limit);
|
|
686
679
|
}
|
|
687
|
-
return new
|
|
680
|
+
return new SqlTextStatementDataset(sql);
|
|
688
681
|
}
|
|
689
682
|
}
|
|
690
683
|
function fromAs(selectable, as) {
|
|
@@ -734,7 +727,7 @@ class Selection {
|
|
|
734
727
|
columnsIn = columnsIn();
|
|
735
728
|
let sql = "SELECT " + selectColumns(columnsIn);
|
|
736
729
|
sql += "\n" + this.toString();
|
|
737
|
-
return new
|
|
730
|
+
return new SqlSelectChain(sql);
|
|
738
731
|
}
|
|
739
732
|
}
|
|
740
733
|
_a = Selection, _Selection_sql = new WeakMap(), _Selection_instances = new WeakSet(), _Selection_join = function _Selection_join(type, selectable, as, on) {
|
|
@@ -745,8 +738,11 @@ _a = Selection, _Selection_sql = new WeakMap(), _Selection_instances = new WeakS
|
|
|
745
738
|
return new _a(sql);
|
|
746
739
|
};
|
|
747
740
|
|
|
748
|
-
|
|
749
|
-
|
|
741
|
+
class SqlChainModify extends SqlStatement {
|
|
742
|
+
constructor(sql) {
|
|
743
|
+
super();
|
|
744
|
+
this.sql = sql;
|
|
745
|
+
}
|
|
750
746
|
returning(returns) {
|
|
751
747
|
if (typeof returns === "function")
|
|
752
748
|
returns = returns();
|
|
@@ -759,7 +755,7 @@ class AfterUpdateOrReturn extends SqlQueryStatement {
|
|
|
759
755
|
columnsStr = res.sqlColumns;
|
|
760
756
|
}
|
|
761
757
|
let sql = this.toString() + "\nRETURNING " + columnsStr;
|
|
762
|
-
return new
|
|
758
|
+
return new SqlTextStatementDataset(sql);
|
|
763
759
|
}
|
|
764
760
|
onConflict(onConflict) {
|
|
765
761
|
if (typeof onConflict === "function")
|
|
@@ -767,22 +763,24 @@ class AfterUpdateOrReturn extends SqlQueryStatement {
|
|
|
767
763
|
if (typeof onConflict !== "string")
|
|
768
764
|
onConflict = onConflict.join(",");
|
|
769
765
|
let sql = this.toString() + `\nON CONFLICT (${onConflict})`;
|
|
770
|
-
return new
|
|
766
|
+
return new SqlInsertConflictBranch(sql);
|
|
771
767
|
}
|
|
772
768
|
where(where$1) {
|
|
773
769
|
const sql = where(where$1);
|
|
774
|
-
return new
|
|
770
|
+
return new SqlChainModify(this.toString() + sql);
|
|
771
|
+
}
|
|
772
|
+
toString() {
|
|
773
|
+
return this.sql;
|
|
775
774
|
}
|
|
776
775
|
}
|
|
777
|
-
class
|
|
776
|
+
class SqlInsertConflictBranch {
|
|
778
777
|
constructor(sql) {
|
|
779
|
-
|
|
780
|
-
__classPrivateFieldSet(this, _AfterInsert_sql, sql, "f");
|
|
778
|
+
this.sql = sql;
|
|
781
779
|
}
|
|
782
780
|
doUpdate(set) {
|
|
783
781
|
if (typeof set === "function")
|
|
784
782
|
set = set();
|
|
785
|
-
let sql = this.
|
|
783
|
+
let sql = this.sql;
|
|
786
784
|
if (typeof set === "object") {
|
|
787
785
|
sql += "\nDO UPDATE ";
|
|
788
786
|
sql += createUpdateSetFromObject(set);
|
|
@@ -791,24 +789,19 @@ class AfterInsert {
|
|
|
791
789
|
sql += "DO UPDATE SET\n" + set;
|
|
792
790
|
else
|
|
793
791
|
sql += "DO NOTHING";
|
|
794
|
-
return new
|
|
792
|
+
return new SqlChainModify(sql);
|
|
795
793
|
}
|
|
796
794
|
doNotThing() {
|
|
797
|
-
return new
|
|
798
|
-
}
|
|
799
|
-
toString() {
|
|
800
|
-
return __classPrivateFieldGet(this, _AfterInsert_sql, "f");
|
|
795
|
+
return new SqlChainModify(this.sql + " DO NOTHING");
|
|
801
796
|
}
|
|
802
797
|
}
|
|
803
|
-
_AfterInsert_sql = new WeakMap();
|
|
804
798
|
|
|
805
799
|
/**
|
|
806
800
|
* 数据库表
|
|
807
801
|
* @public
|
|
808
802
|
*/
|
|
809
|
-
class DbTable
|
|
803
|
+
class DbTable {
|
|
810
804
|
constructor(name) {
|
|
811
|
-
super();
|
|
812
805
|
this.name = name;
|
|
813
806
|
}
|
|
814
807
|
fromAs(as) {
|
|
@@ -832,7 +825,7 @@ class DbTable extends SqlSelectable {
|
|
|
832
825
|
if (typeof values !== "string")
|
|
833
826
|
throw new TypeError("values 必须是 string 或 function 类型");
|
|
834
827
|
let sql = `INSERT INTO ${this.name}(${columns})\n${values}`;
|
|
835
|
-
return new
|
|
828
|
+
return new SqlChainModify(sql);
|
|
836
829
|
}
|
|
837
830
|
/**
|
|
838
831
|
* UPDATE 语句,需要注意 SQL 注入
|
|
@@ -848,10 +841,10 @@ class DbTable extends SqlSelectable {
|
|
|
848
841
|
switch (typeof values) {
|
|
849
842
|
case "object": {
|
|
850
843
|
let sql = createUpdateSetFromObject(values);
|
|
851
|
-
return new
|
|
844
|
+
return new SqlChainModify("UPDATE " + this.name + " " + sql);
|
|
852
845
|
}
|
|
853
846
|
case "string":
|
|
854
|
-
return new
|
|
847
|
+
return new SqlChainModify("UPDATE " + this.name + " SET\n" + values);
|
|
855
848
|
default:
|
|
856
849
|
throw new TypeError("参数 values 错误");
|
|
857
850
|
}
|
|
@@ -859,14 +852,11 @@ class DbTable extends SqlSelectable {
|
|
|
859
852
|
delete(option = {}) {
|
|
860
853
|
let sql = "DELETE FROM " + this.name;
|
|
861
854
|
sql += where(option.where);
|
|
862
|
-
return new
|
|
855
|
+
return new SqlChainModify(sql);
|
|
863
856
|
}
|
|
864
857
|
toSelect() {
|
|
865
858
|
return this.name;
|
|
866
859
|
}
|
|
867
|
-
toString() {
|
|
868
|
-
return this.name;
|
|
869
|
-
}
|
|
870
860
|
}
|
|
871
861
|
|
|
872
862
|
/** @public */
|
|
@@ -899,7 +889,7 @@ class DbTableQuery extends DbTable {
|
|
|
899
889
|
throw new Error("插入列不能为空");
|
|
900
890
|
const columnStr = insertCol.join(",");
|
|
901
891
|
let sql = `INSERT INTO ${this.name} (${columnStr})\n${valuesStr}`;
|
|
902
|
-
return new
|
|
892
|
+
return new SqlChainModify(sql);
|
|
903
893
|
}
|
|
904
894
|
/**
|
|
905
895
|
* UPDATE 语句,与 update() 不同的是,它会将值进行安全转换
|
|
@@ -927,7 +917,7 @@ class DbTableQuery extends DbTable {
|
|
|
927
917
|
if (!setStr)
|
|
928
918
|
throw new Error("值不能为空");
|
|
929
919
|
let sql = `UPDATE ${this.name} SET\n${setStr}`;
|
|
930
|
-
return new
|
|
920
|
+
return new SqlChainModify(sql);
|
|
931
921
|
}
|
|
932
922
|
}
|
|
933
923
|
|
|
@@ -1109,4 +1099,4 @@ class YourTable extends DbTableQuery {
|
|
|
1109
1099
|
*/
|
|
1110
1100
|
const v = SqlValuesCreator.create(pgSqlTransformer);
|
|
1111
1101
|
|
|
1112
|
-
export { ColumnMeta, CustomDbType, DbTable, DbTableQuery, Selection,
|
|
1102
|
+
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,22 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ConditionParam, Constructable, SelectParam } from "../util.ts";
|
|
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
|
+
/** 选择单表 */
|
|
16
|
+
select(columns: Constructable<Record<string, boolean | string> | string>, as?: string): ChainSelectWhere<Record<string, any>>;
|
|
15
17
|
/** 选择单表 */
|
|
16
18
|
select<R extends {}>(columns: Constructable<{
|
|
17
19
|
[key in keyof R]: boolean | string;
|
|
18
|
-
} | string>, as?: string):
|
|
19
|
-
select<R extends {}>(columns: Constructable<SelectParam>): CurrentWhere<R>;
|
|
20
|
+
} | string>, as?: string): ChainSelectWhere<R>;
|
|
20
21
|
/**
|
|
21
22
|
* INSERT 语句,需要注意 SQL 注入
|
|
22
23
|
* @example
|
|
@@ -24,7 +25,7 @@ export declare class DbTable<T extends TableType> extends SqlSelectable<T> {
|
|
|
24
25
|
* table.insert(["age","name"], "VALUES (18, 'hi'), (17, 'hh')") // INSERT INTO table(age,name) VALUES(18, 'hi'), (17, 'hh')
|
|
25
26
|
* ```
|
|
26
27
|
*/
|
|
27
|
-
insert(columns: string, values: Constructable<string>):
|
|
28
|
+
insert(columns: string, values: Constructable<string>): ChainOnConflict<T>;
|
|
28
29
|
/**
|
|
29
30
|
* UPDATE 语句,需要注意 SQL 注入
|
|
30
31
|
* @example
|
|
@@ -35,10 +36,9 @@ export declare class DbTable<T extends TableType> extends SqlSelectable<T> {
|
|
|
35
36
|
*/
|
|
36
37
|
update(values: Constructable<{
|
|
37
38
|
[key in keyof T]?: string;
|
|
38
|
-
} | string>):
|
|
39
|
-
delete(option?: DeleteOption):
|
|
39
|
+
} | string>): ChainModifyWhere<T>;
|
|
40
|
+
delete(option?: DeleteOption): ChainModifyWhere<T>;
|
|
40
41
|
toSelect(): string;
|
|
41
|
-
toString(): string;
|
|
42
42
|
}
|
|
43
43
|
/** @public */
|
|
44
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 } from "../util.ts";
|
|
2
|
+
import { ColumnsSelected, 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<ColumnsSelected<T> | string>): SqlStatementDataset<Record<string, any>>;
|
|
59
|
+
returning<R extends TableType>(columns: Constructable<ColumnsSelected<R> | string>): 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 } from "../util.ts";
|
|
2
|
+
import { SqlStatementDataset, SqlStatement, ChainModifyWhere, ChainOnConflict, ChainConflictDo, ChainModifyReturning } from "./query_chain_abstract.ts";
|
|
3
|
+
import { ColumnsSelected, 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<ColumnsSelected<any> | "*">): 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
|
|
@@ -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/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,84 +0,0 @@
|
|
|
1
|
-
import { SqlSelectable, SqlQueryStatement } from "./selectable.ts";
|
|
2
|
-
import { OrderByParam, ConditionParam, SelectParam, Constructable } from "../util.ts";
|
|
3
|
-
import type { ColumnsSelected, SelectColumns, 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 = 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: Constructable<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
|
-
*/
|
|
54
|
-
select<T extends TableType>(columns: Constructable<{
|
|
55
|
-
[key in keyof T]: string | boolean;
|
|
56
|
-
}>): CurrentWhere<T>;
|
|
57
|
-
select<R extends {}>(columns: Constructable<SelectParam>): CurrentWhere<R>;
|
|
58
|
-
}
|
|
59
|
-
/** @public */
|
|
60
|
-
export interface CurrentReturn<T extends TableType = {}> extends SqlQueryStatement<{}> {
|
|
61
|
-
returning(columns: "*"): SqlQueryStatement<T>;
|
|
62
|
-
returning<R extends ColumnsSelected<T>>(columns: Constructable<R>): SqlQueryStatement<SelectColumns<T, R>>;
|
|
63
|
-
returning<R extends TableType>(columns: Constructable<R | string>): SqlQueryStatement<T>;
|
|
64
|
-
}
|
|
65
|
-
/** @public */
|
|
66
|
-
export type CurrentModifyWhere<T extends TableType = {}> = CurrentReturn<T> & {
|
|
67
|
-
where(where: Constructable<ConditionParam | void>): CurrentReturn<T>;
|
|
68
|
-
};
|
|
69
|
-
/** @public */
|
|
70
|
-
export type CurrentOnConflictDo<T extends TableType = {}> = {
|
|
71
|
-
doNotThing(): CurrentReturn<T>;
|
|
72
|
-
/**
|
|
73
|
-
* 需要注意 SQL 注入
|
|
74
|
-
*/
|
|
75
|
-
doUpdate(set: Constructable<{
|
|
76
|
-
[key in keyof T]?: string;
|
|
77
|
-
}>): CurrentModifyWhere<T>;
|
|
78
|
-
toString(): string;
|
|
79
|
-
};
|
|
80
|
-
/** @public */
|
|
81
|
-
export type CurrentOnConflict<T extends TableType = {}> = CurrentReturn<T> & {
|
|
82
|
-
onConflict(option: Constructable<readonly (keyof T)[] | string>): CurrentOnConflictDo<T>;
|
|
83
|
-
};
|
|
84
|
-
//# 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
|