@asla/yoursql 0.8.3 → 0.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -77,3 +77,116 @@ const s = Selection.from("user", "u")
77
77
  .where(`u.name LIKE %${v(searchName)}%`)
78
78
  .toString();
79
79
  ```
80
+
81
+ ### client 抽象类
82
+
83
+ yoursql 还导出了一些抽象类,实现抽象类后可以方便的进行数据查询
84
+
85
+ ```ts
86
+ import {
87
+ type DbQueryPool,
88
+ type DbTransaction,
89
+ type DbConnection,
90
+ DbQuery,
91
+ DbCursor,
92
+ DbPoolConnection,
93
+ DbPoolTransaction,
94
+ } from "@asla/yoursql/client";
95
+ ```
96
+
97
+ #### DbQuery 抽象类
98
+
99
+ ```ts
100
+ class YourQuery extends DbQuery {
101
+ // implement
102
+ }
103
+ const db: DbQuery = new YourQuery();
104
+ ```
105
+
106
+ ```ts
107
+ declare const db: DbQuery;
108
+
109
+ type Row = { name: string; age: number };
110
+ const sqlText = "SELECT * FROM user";
111
+
112
+ const rows: Row[] = await db.queryRows<Row>(sqlText);
113
+ const count: number = await db.queryCount(sqlText);
114
+ const rows: Map<string, Row> = await db.queryMap<Row>(sqlText, "name");
115
+ ```
116
+
117
+ #### DbQueryPool 接口
118
+
119
+ ```ts
120
+ class YourPool extends DbQuery implements DbQuery {
121
+ // implement
122
+ }
123
+ const pool: DbQueryPool = new YourPool();
124
+ ```
125
+
126
+ ##### 普通查询
127
+
128
+ ```ts
129
+ const conn = await pool.connect();
130
+ try {
131
+ await conn.queryRows(sqlText);
132
+ } finally {
133
+ conn.release();
134
+ }
135
+ ```
136
+
137
+ 或者,使用 `using` 语法更优雅
138
+
139
+ ```ts
140
+ using conn = await pool.connect();
141
+ await conn.queryRows(sqlText);
142
+ ```
143
+
144
+ ##### 事务查询
145
+
146
+ ```ts
147
+ const conn = pool.begin();
148
+ try {
149
+ await conn.queryRows(sqlText);
150
+ await conn.queryRows(sqlText);
151
+ await conn.commit();
152
+ } catch (e) {
153
+ await conn.rollback();
154
+ throw e;
155
+ }
156
+ ```
157
+
158
+ 或者,使用 `using` 语法更优雅
159
+
160
+ ```ts
161
+ await using conn = pool.begin();
162
+
163
+ await conn.queryRows(sqlText);
164
+ await conn.queryRows(sqlText);
165
+ await conn.commit();
166
+ ```
167
+
168
+ ##### 游标查询
169
+
170
+ ```ts
171
+ const cursor = await pool.cursor(sqlText);
172
+
173
+ let rows = await cursor.read(20);
174
+ while (rows.length) {
175
+ console.log(rows);
176
+ rows = await cursor.read(20);
177
+ if (conditions) {
178
+ await cursor.close(); // 提前关闭游标
179
+ break;
180
+ }
181
+ }
182
+ ```
183
+
184
+ 或者使用 `for await of` 更优雅
185
+
186
+ ```ts
187
+ const cursor = await pool.cursor(sqlText);
188
+ for await (const element of cursor) {
189
+ console.log(element);
190
+ if (conditions) break; //提前关闭游标
191
+ }
192
+ ```
@@ -3,14 +3,19 @@ import { DbQuery } from "./DbQuery.ts";
3
3
  import type { MultipleQueryResult, QueryRowsResult } from "./DbQuery.ts";
4
4
  import type { DbPoolConnection } from "./DbPoolConnection.ts";
5
5
  import type { DbTransaction, TransactionMode } from "./interfaces.ts";
6
+ /** @public */
7
+ export type DbPoolTransactionOption = {
8
+ errorRollback?: boolean;
9
+ mode?: TransactionMode;
10
+ };
6
11
  /**
7
12
  * @public
8
13
  * 池连接事务
9
14
  */
10
15
  export declare class DbPoolTransaction extends DbQuery implements DbTransaction {
11
16
  #private;
12
- readonly mode?: TransactionMode | undefined;
13
- constructor(connect: () => Promise<DbPoolConnection>, mode?: TransactionMode | undefined);
17
+ readonly mode?: TransactionMode;
18
+ constructor(connect: () => Promise<DbPoolConnection>, option?: TransactionMode | DbPoolTransactionOption);
14
19
  commit(): Promise<void>;
15
20
  rollback(): Promise<void>;
16
21
  savePoint(savePoint: string): Promise<void>;
@@ -6,10 +6,18 @@ import { ParallelQueryError, ConnectionNotAvailableError } from './errors.js';
6
6
  * 池连接事务
7
7
  */
8
8
  class DbPoolTransaction extends DbQuery {
9
+ #errorRollback;
9
10
  mode;
10
- constructor(connect, mode) {
11
+ constructor(connect, option) {
11
12
  super();
12
- this.mode = mode;
13
+ if (option) {
14
+ if (typeof option === "string")
15
+ this.mode = option;
16
+ else {
17
+ this.mode = option.mode;
18
+ this.#errorRollback = option.errorRollback;
19
+ }
20
+ }
13
21
  this.#query = (sql) => {
14
22
  return new Promise((resolve, reject) => {
15
23
  this.#pending = connect()
@@ -24,10 +32,16 @@ class DbPoolTransaction extends DbQuery {
24
32
  this.#pending = undefined;
25
33
  resolve(res[1]);
26
34
  }, (e) => {
35
+ // 语法错误、查询错误、网络错误
27
36
  this.#pending = undefined;
28
37
  reject(e);
29
- if (this.#conn)
30
- this.#release(this.#conn, e);
38
+ const conn = this.#conn;
39
+ if (conn) {
40
+ this.#release(conn, e);
41
+ if (this.#errorRollback) {
42
+ return conn.rollback().catch((e) => { });
43
+ }
44
+ }
31
45
  });
32
46
  });
33
47
  };
@@ -60,12 +74,20 @@ class DbPoolTransaction extends DbQuery {
60
74
  }
61
75
  /** 拿到连接后执行这个 */
62
76
  #queryAfter(sql) {
63
- return this.#conn.query(sql).then((res) => {
77
+ const conn = this.#conn;
78
+ return conn.query(sql).then((res) => {
64
79
  this.#pending = undefined;
65
80
  return res;
66
81
  }, (e) => {
67
82
  this.#pending = undefined;
68
- this.#release(this.#conn, e);
83
+ this.#release(conn, e);
84
+ if (this.#errorRollback) {
85
+ return conn.rollback().then(() => {
86
+ throw e;
87
+ }, () => {
88
+ throw e;
89
+ });
90
+ }
69
91
  throw e;
70
92
  });
71
93
  }
@@ -73,12 +95,12 @@ class DbPoolTransaction extends DbQuery {
73
95
  query(sql) {
74
96
  if (this.#pending)
75
97
  return Promise.reject(new ParallelQueryError());
76
- return this.#query(sql.toString());
98
+ return this.#query(sql);
77
99
  }
78
100
  multipleQuery(sql) {
79
101
  if (this.#pending)
80
102
  return Promise.reject(new ParallelQueryError());
81
- return this.#query(sql.toString());
103
+ return this.#query(sql);
82
104
  }
83
105
  #error;
84
106
  #release(conn, error = new ConnectionNotAvailableError("Connection already release")) {
@@ -3,7 +3,9 @@ export * from "./sql_value/sql_value.ts";
3
3
  export * from "./select/type.ts";
4
4
  export * from "./select/DbTable.ts";
5
5
  export * from "./select/query_chain_select.ts";
6
- export * from "./select/query_chain_abstract.ts";
6
+ export * from "./select/chain_base.ts";
7
+ export * from "./select/chain_modify.ts";
8
+ export * from "./select/chain_select.ts";
7
9
  export * from "./select/TableQuery.ts";
8
10
  export * from "./util.ts";
9
11
  export * from "./your_table/mod.ts";
@@ -3,7 +3,7 @@ import { SqlValuesCreator } from './sql_value/sql_value.js';
3
3
  export { SqlRaw } from './sql_value/sql_value.js';
4
4
  export { DbTable } from './select/DbTable.js';
5
5
  export { Selection, SqlSelectChain } from './select/query_chain_select.js';
6
- export { SqlStatement, SqlStatementDataset, SqlTextStatementDataset } from './select/query_chain_abstract.js';
6
+ export { SqlStatement, SqlStatementDataset, SqlTextStatementDataset } from './select/chain_base.js';
7
7
  export { DbTableQuery } from './select/TableQuery.js';
8
8
  export { getObjectListKeys, having, orderBy, selectColumns, where } from './util.js';
9
9
  export { TypeChecker } from './your_table/checker.js';
@@ -1,7 +1,8 @@
1
1
  import { ConditionParam, Constructable } from "../util.ts";
2
2
  import type { TableType } from "./type.ts";
3
3
  import { Selection } from "./query_chain_select.ts";
4
- import { ChainModifyWhere, ChainOnConflict, ChainSelectWhere } from "./query_chain_abstract.ts";
4
+ import { ChainDelete, ChainInsert, ChainUpdate } from "./chain_modify.ts";
5
+ import { ChainSelect } from "./chain_select.ts";
5
6
  /**
6
7
  * 数据库表
7
8
  * @public
@@ -11,13 +12,17 @@ export declare class DbTable<T extends TableType> {
11
12
  constructor(name: string);
12
13
  fromAs(as?: string): Selection;
13
14
  /** 选择单表全部列 */
14
- select(columns: "*", as?: string): ChainSelectWhere<T>;
15
- /** 选择单表 */
16
- select(columns: Constructable<Record<string, boolean | string> | string>, as?: string): ChainSelectWhere<Record<string, any>>;
15
+ select(columns: "*", as?: string): ChainSelect<T>;
16
+ /** 选择单表,带提示 */
17
+ select(columns: Constructable<{
18
+ [key in keyof T]?: string | boolean;
19
+ } & {
20
+ [key: string]: string | boolean;
21
+ }>, as?: string): ChainSelect<Record<string, any>>;
17
22
  /** 选择单表 */
18
23
  select<R extends {}>(columns: Constructable<{
19
24
  [key in keyof R]: boolean | string;
20
- } | string>, as?: string): ChainSelectWhere<R>;
25
+ } | string | string[]>, as?: string): ChainSelect<R>;
21
26
  /**
22
27
  * INSERT 语句,需要注意 SQL 注入
23
28
  * @example
@@ -25,7 +30,7 @@ export declare class DbTable<T extends TableType> {
25
30
  * table.insert(["age","name"], "VALUES (18, 'hi'), (17, 'hh')") // INSERT INTO table(age,name) VALUES(18, 'hi'), (17, 'hh')
26
31
  * ```
27
32
  */
28
- insert(columns: string, values: Constructable<string>): ChainOnConflict<T>;
33
+ insert(columns: string, values: Constructable<string>): ChainInsert<T>;
29
34
  /**
30
35
  * UPDATE 语句,需要注意 SQL 注入
31
36
  * @example
@@ -36,8 +41,8 @@ export declare class DbTable<T extends TableType> {
36
41
  */
37
42
  update(values: Constructable<{
38
43
  [key in keyof T]?: string;
39
- } | string>): ChainModifyWhere<T>;
40
- delete(option?: DeleteOption): ChainModifyWhere<T>;
44
+ } | string>): ChainUpdate<T>;
45
+ delete(option?: DeleteOption): ChainDelete<T>;
41
46
  toSelect(): string;
42
47
  }
43
48
  /** @public */
@@ -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 { ChainModifyWhere, ChainOnConflict } from "./query_chain_abstract.ts";
5
+ import { ChainInsert, ChainUpdate } from "./chain_modify.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>[]>): ChainOnConflict<T>;
18
- insert(columns: string, values: Constructable<string>): ChainOnConflict<T>;
17
+ insert(values: Constructable<UpdateRowValue<C> | UpdateRowValue<C>[]>): ChainInsert<T>;
18
+ insert(columns: string, values: Constructable<string>): ChainInsert<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>>): ChainModifyWhere<T>;
26
+ updateFrom(values: Constructable<UpdateRowValue<T>>): ChainUpdate<T>;
27
27
  }
28
28
  //# sourceMappingURL=TableQuery.d.ts.map
@@ -0,0 +1,44 @@
1
+ /** @public */
2
+ export declare abstract class SqlStatement {
3
+ /** 获取 SQL 语句 */
4
+ abstract toString(): string;
5
+ }
6
+ /**
7
+ * 可选择项。可以是 table、查询结果等,它能被 select 语句选择
8
+ * @example
9
+ * ```ts
10
+ * declare const item: SqlStatementDataset
11
+ * await query(`select * from ${item.toSelect()}`)
12
+ *
13
+ * ```
14
+ * @public
15
+ */
16
+ export interface SqlSelectable {
17
+ /**
18
+ * 转成子选择语句, 你可以使用 select form xxx 选择
19
+ * 如果是 table 则是 table name
20
+ * 如果是 选择语句,则是 (xxx)
21
+ */
22
+ toSelect(): string;
23
+ }
24
+ /** @public */
25
+ export declare abstract class SqlStatementDataset<T> extends SqlStatement implements SqlSelectable {
26
+ /**
27
+ * 转成子选择语句, 你可以使用 select form xxx 选择
28
+ * 如果是 table 则是 table name
29
+ * 如果是 选择语句,则是 (xxx)
30
+ */
31
+ toSelect(): string;
32
+ }
33
+ /** @public */
34
+ export declare class SqlTextStatementDataset<T> extends SqlStatementDataset<T> {
35
+ readonly sql: string;
36
+ constructor(sql: string);
37
+ toString(): string;
38
+ }
39
+ /**
40
+ * 推断查询结果的类型
41
+ * @public
42
+ */
43
+ export type InferQueryResult<T> = T extends SqlStatementDataset<infer P> ? P : never;
44
+ //# sourceMappingURL=chain_base.d.ts.map
@@ -0,0 +1,33 @@
1
+ import { ConditionParam, Constructable, SelectParam } from "../util.ts";
2
+ import { TableType } from "./type.ts";
3
+ import { SqlStatement, SqlStatementDataset } from "./chain_base.ts";
4
+ /** @public */
5
+ export interface ChainModifyReturning<T extends TableType = {}> extends SqlStatement {
6
+ returning(columns: "*"): SqlStatementDataset<T>;
7
+ returning(columns: Constructable<SelectParam>): SqlStatementDataset<Record<string, any>>;
8
+ returning<R extends TableType>(columns: Constructable<SelectParam>): SqlStatementDataset<R>;
9
+ }
10
+ /** @public */
11
+ export interface ChainAfterConflictDo<T extends TableType = {}> {
12
+ doNotThing(): ChainModifyReturning<T>;
13
+ /**
14
+ * 需要注意 SQL 注入
15
+ */
16
+ doUpdate(set: Constructable<string | {
17
+ [key in keyof T]?: string;
18
+ }>): ChainModifyReturning<T>;
19
+ toString(): string;
20
+ }
21
+ /** @public */
22
+ export interface ChainInsert<T extends TableType = {}> extends ChainModifyReturning<T> {
23
+ onConflict(option: Constructable<readonly (keyof T)[] | string>): ChainAfterConflictDo<T>;
24
+ }
25
+ /** @public */
26
+ export interface ChainUpdate<T extends TableType = {}> extends ChainModifyReturning<T> {
27
+ where(where: Constructable<ConditionParam | void>): ChainModifyReturning<T>;
28
+ }
29
+ /** @public */
30
+ export interface ChainDelete<T extends TableType = {}> extends ChainModifyReturning<T> {
31
+ where(where: Constructable<ConditionParam | void>): ChainModifyReturning<T>;
32
+ }
33
+ //# sourceMappingURL=chain_modify.d.ts.map
@@ -0,0 +1,28 @@
1
+ import { ConditionParam, Constructable, OrderByParam } from "../util.ts";
2
+ import { SqlStatementDataset } from "./chain_base.ts";
3
+ import { TableType } from "./type.ts";
4
+ /** @public */
5
+ export interface ChainSelect<T extends TableType> extends ChainSelectAfterWhere<T> {
6
+ where(param: Constructable<ConditionParam | void>): ChainSelectAfterWhere<T>;
7
+ }
8
+ /** @public */
9
+ export interface ChainSelectAfterWhere<T extends TableType> extends ChainSelectAfterHaving<T> {
10
+ groupBy(columns: string | string[]): ChainSelectAfterGroupBy<T>;
11
+ }
12
+ /** @public */
13
+ export interface ChainSelectAfterGroupBy<T extends TableType> extends ChainSelectAfterHaving<T> {
14
+ orderBy(param: Constructable<OrderByParam | void>): ChainSelectAfterOrderBy<T>;
15
+ having(param: Constructable<ConditionParam | void>): ChainSelectAfterHaving<T>;
16
+ }
17
+ /** @public */
18
+ export interface ChainSelectAfterHaving<T extends TableType> extends ChainSelectAfterOrderBy<T> {
19
+ orderBy(param: Constructable<OrderByParam | void>): ChainSelectAfterOrderBy<T>;
20
+ }
21
+ /** @public */
22
+ export interface ChainSelectAfterOrderBy<T extends TableType> extends SqlStatementDataset<T> {
23
+ limit(limit?: number | bigint, offset?: number | bigint): ChainSelectAfterLimit<T>;
24
+ }
25
+ /** @public */
26
+ export interface ChainSelectAfterLimit<T extends TableType> extends SqlStatementDataset<T> {
27
+ }
28
+ //# sourceMappingURL=chain_select.d.ts.map
@@ -1,20 +1,22 @@
1
1
  import { ConditionParam, Constructable, SelectParam } from "../util.ts";
2
- import { SqlStatementDataset, SqlStatement, ChainModifyWhere, ChainOnConflict, ChainConflictDo, ChainModifyReturning } from "./query_chain_abstract.ts";
2
+ import { SqlStatementDataset, SqlStatement } from "./chain_base.ts";
3
+ import { ChainAfterConflictDo, ChainModifyReturning, ChainInsert, ChainDelete, ChainUpdate } from "./chain_modify.ts";
3
4
  import { TableType } from "./type.ts";
4
- export declare class SqlChainModify<T extends TableType = {}> extends SqlStatement implements ChainOnConflict<T>, ChainModifyWhere<T> {
5
+ export declare class SqlChainModify<T extends TableType = {}> extends SqlStatement implements ChainInsert<T>, ChainUpdate<T>, ChainDelete<T> {
5
6
  readonly sql: string;
6
7
  constructor(sql: string);
7
8
  returning<R extends {}>(returns: Constructable<SelectParam | "*">): SqlStatementDataset<R>;
8
- onConflict(onConflict: Constructable<readonly string[] | string>): ChainConflictDo<T>;
9
+ onConflict(onConflict: Constructable<readonly string[] | string>): ChainAfterConflictDo<T>;
9
10
  where(where: Constructable<ConditionParam | void>): ChainModifyReturning<T>;
10
11
  toString(): string;
11
12
  }
12
- export declare class SqlInsertConflictBranch<T extends TableType = {}> implements ChainConflictDo<T> {
13
+ export declare class SqlInsertConflictBranch<T extends TableType = {}> implements ChainAfterConflictDo<T> {
13
14
  readonly sql: string;
14
15
  constructor(sql: string);
15
16
  doUpdate(set?: Constructable<string | {
16
17
  [key: string]: string | undefined;
17
- }>): ChainModifyWhere<T>;
18
+ }>): ChainModifyReturning<T>;
18
19
  doNotThing(): ChainModifyReturning<T>;
20
+ toString(): string;
19
21
  }
20
22
  //# sourceMappingURL=query_chain_insert.d.ts.map
@@ -1,6 +1,6 @@
1
1
  import { selectColumns, where } from '../util.js';
2
2
  import { createUpdateSetFromObject } from './_statement.js';
3
- import { SqlStatement, SqlTextStatementDataset } from './query_chain_abstract.js';
3
+ import { SqlStatement, SqlTextStatementDataset } from './chain_base.js';
4
4
 
5
5
  class SqlChainModify extends SqlStatement {
6
6
  sql;
@@ -51,7 +51,7 @@ class SqlInsertConflictBranch {
51
51
  sql += createUpdateSetFromObject(set);
52
52
  }
53
53
  else if (set)
54
- sql += "DO UPDATE SET\n" + set;
54
+ sql += "DO UPDATE\n" + set;
55
55
  else
56
56
  sql += "DO NOTHING";
57
57
  return new SqlChainModify(sql);
@@ -59,6 +59,9 @@ class SqlInsertConflictBranch {
59
59
  doNotThing() {
60
60
  return new SqlChainModify(this.sql + " DO NOTHING");
61
61
  }
62
+ toString() {
63
+ return this.sql;
64
+ }
62
65
  }
63
66
 
64
67
  export { SqlChainModify, SqlInsertConflictBranch };
@@ -1,15 +1,16 @@
1
- import { ChainSelectGroupBy, ChainSelectHaving, ChainSelectLimit, ChainSelectWhere, SqlSelectable, SqlStatementDataset, SqlTextStatementDataset } from "./query_chain_abstract.ts";
1
+ import { SqlSelectable, SqlTextStatementDataset } from "./chain_base.ts";
2
+ import { ChainSelect, ChainSelectAfterHaving, ChainSelectAfterLimit, ChainSelectAfterOrderBy, ChainSelectAfterGroupBy, ChainSelectAfterWhere } from "./chain_select.ts";
2
3
  import { OrderByParam, ConditionParam, SelectParam, Constructable } from "../util.ts";
3
4
  import type { TableType } from "./type.ts";
4
5
  /**
5
6
  * @public ChainSelectWhere 的默认实现
6
7
  */
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>;
8
+ export declare class SqlSelectChain<T extends TableType> extends SqlTextStatementDataset<T> implements ChainSelect<T> {
9
+ where(param?: Constructable<ConditionParam | void>): ChainSelectAfterWhere<T>;
10
+ groupBy(columns: string | string[]): ChainSelectAfterGroupBy<T>;
11
+ having(param?: Constructable<ConditionParam | void>): ChainSelectAfterHaving<T>;
12
+ orderBy(param?: Constructable<OrderByParam | void>): ChainSelectAfterOrderBy<T>;
13
+ limit(limit?: number, offset?: number): ChainSelectAfterLimit<T>;
13
14
  }
14
15
  /** @public */
15
16
  export declare class Selection {
@@ -25,7 +26,7 @@ export declare class Selection {
25
26
  crossJoin(selectable: Constructable<SqlSelectable | string>, as?: string | undefined): Selection;
26
27
  from(selectable: Constructable<SqlSelectable | string>, as?: string): Selection;
27
28
  /** 选择全部列 */
28
- select<T extends TableType = Record<string, any>>(columns: "*"): ChainSelectWhere<T>;
29
+ select<T extends TableType = Record<string, any>>(columns: "*"): ChainSelect<T>;
29
30
  /**
30
31
  * 自定义SQL选择语句
31
32
  * @example
@@ -33,7 +34,7 @@ export declare class Selection {
33
34
  * selection.select("t.age, count(*) AS c") // SELECT t.age,count(*) AS c FROM ...
34
35
  * ```
35
36
  */
36
- select(columns: Constructable<SelectParam>): ChainSelectWhere<Record<string, any>>;
37
+ select(columns: Constructable<SelectParam>): ChainSelect<Record<string, any>>;
37
38
  /**
38
39
  * 通过 object 选择 列
39
40
  * @example
@@ -43,6 +44,6 @@ export declare class Selection {
43
44
  */
44
45
  select<T extends TableType>(columns: Constructable<{
45
46
  [key in keyof T]: string | boolean;
46
- } | string>): ChainSelectWhere<T>;
47
+ } | string>): ChainSelect<T>;
47
48
  }
48
49
  //# sourceMappingURL=query_chain_select.d.ts.map
@@ -1,4 +1,4 @@
1
- import { SqlTextStatementDataset } from './query_chain_abstract.js';
1
+ import { SqlTextStatementDataset } from './chain_base.js';
2
2
  import { where, having, orderBy, selectColumns } from '../util.js';
3
3
  import { condition } from './_statement.js';
4
4
 
@@ -1,4 +1,4 @@
1
- import { SqlStatementDataset } from "../select/query_chain_abstract.ts";
1
+ import { SqlStatementDataset } from "../select/chain_base.ts";
2
2
  declare const SQL_RAW: unique symbol;
3
3
  /**
4
4
  * SQL 原始字符类。可以使用 String 类代替,这只是为了推断类型
@@ -1,5 +1,5 @@
1
1
  import { getObjectListKeys } from '../util.js';
2
- import { SqlStatementDataset } from '../select/query_chain_abstract.js';
2
+ import { SqlStatementDataset } from '../select/chain_base.js';
3
3
 
4
4
  /**
5
5
  * SQL 原始字符类。可以使用 String 类代替,这只是为了推断类型
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asla/yoursql",
3
- "version": "0.8.3",
3
+ "version": "0.8.5",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",
@@ -1,91 +0,0 @@
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