@asla/yoursql 0.11.2 → 0.11.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/DbPoolConnection.d.ts +7 -5
- package/dist/client/DbPoolConnection.js +10 -0
- package/dist/client/DbPoolTransaction.d.ts +6 -5
- package/dist/client/DbPoolTransaction.js +91 -73
- package/dist/client/DbQuery.d.ts +19 -9
- package/dist/client/DbQuery.js +1 -0
- package/dist/client/DbQueryBase.d.ts +6 -7
- package/dist/client/DbQueryPool.d.ts +40 -0
- package/dist/client/DbQueryPool.js +56 -0
- package/dist/client/_type.d.ts +1 -1
- package/dist/client/interfaces.d.ts +2 -2
- package/dist/client/mod.d.ts +1 -0
- package/dist/client/mod.js +1 -0
- package/dist/sql_gen/SqlStatement.d.ts +4 -0
- package/dist/sql_gen/SqlStatement.js +6 -0
- package/package.json +1 -1
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { type MultipleQueryResult, type QueryRowsResult, type DbQueryBase } from "./DbQueryBase.ts";
|
|
1
|
+
import { DbQuery, MultipleQueryInput, QueryDataInput, QueryInput } from "./DbQuery.ts";
|
|
2
|
+
import { type QueryRowsResult, type DbQueryBase, MultipleQueryResult } from "./DbQueryBase.ts";
|
|
4
3
|
import type { SqlLike, TransactionMode } from "./interfaces.ts";
|
|
5
4
|
/**
|
|
6
5
|
|
|
@@ -12,8 +11,11 @@ export declare class DbPoolConnection extends DbQuery {
|
|
|
12
11
|
#private;
|
|
13
12
|
constructor(conn: DbQueryBase, onRelease: (conn: DbQueryBase) => void);
|
|
14
13
|
begin(mode?: TransactionMode): Promise<void>;
|
|
15
|
-
query<T =
|
|
16
|
-
query<T = any>(sql:
|
|
14
|
+
query<T extends MultipleQueryResult = MultipleQueryResult>(sql: MultipleQueryInput): Promise<T>;
|
|
15
|
+
query<T = any>(sql: QueryDataInput<T>): Promise<QueryRowsResult<T>>;
|
|
16
|
+
query<T = any>(sql: QueryInput): Promise<QueryRowsResult<T>>;
|
|
17
|
+
execute(sql: QueryInput | MultipleQueryInput): Promise<void>;
|
|
18
|
+
/** @deprecated 不建议使用 */
|
|
17
19
|
multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlLike | SqlLike[]): Promise<T>;
|
|
18
20
|
rollback(): Promise<void>;
|
|
19
21
|
commit(): Promise<void>;
|
|
@@ -22,8 +22,18 @@ class DbPoolConnection extends DbQuery {
|
|
|
22
22
|
query(sql) {
|
|
23
23
|
if (!this.#conn)
|
|
24
24
|
return Promise.reject(new ConnectionNotAvailableError("Connection already release"));
|
|
25
|
+
if (typeof sql === "function")
|
|
26
|
+
sql = sql();
|
|
25
27
|
return this.#conn.query(sql);
|
|
26
28
|
}
|
|
29
|
+
execute(sql) {
|
|
30
|
+
if (!this.#conn)
|
|
31
|
+
return Promise.reject(new ConnectionNotAvailableError("Connection already release"));
|
|
32
|
+
if (typeof sql === "function")
|
|
33
|
+
sql = sql();
|
|
34
|
+
return this.#conn.execute(sql);
|
|
35
|
+
}
|
|
36
|
+
/** @deprecated 不建议使用 */
|
|
27
37
|
multipleQuery(sql) {
|
|
28
38
|
if (!this.#conn)
|
|
29
39
|
return Promise.reject(new ConnectionNotAvailableError("Connection already release"));
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { DbQuery } from "./DbQuery.ts";
|
|
1
|
+
import { DbQuery, MultipleQueryInput, QueryDataInput, QueryInput } from "./DbQuery.ts";
|
|
2
2
|
import type { MultipleQueryResult, QueryRowsResult } from "./DbQueryBase.ts";
|
|
3
3
|
import type { DbPoolConnection } from "./DbPoolConnection.ts";
|
|
4
4
|
import type { DbTransaction, SqlLike, TransactionMode } from "./interfaces.ts";
|
|
5
|
-
import { SqlStatementDataset } from "./_type.ts";
|
|
6
5
|
/** @public */
|
|
7
6
|
export type DbPoolTransactionOption = {
|
|
8
7
|
errorRollback?: boolean;
|
|
@@ -20,9 +19,11 @@ export declare class DbPoolTransaction extends DbQuery implements DbTransaction
|
|
|
20
19
|
rollback(): Promise<void>;
|
|
21
20
|
savePoint(savePoint: string): Promise<void>;
|
|
22
21
|
rollbackTo(savePoint: string): Promise<void>;
|
|
23
|
-
query<T extends
|
|
24
|
-
query<T extends object = any>(sql:
|
|
25
|
-
|
|
22
|
+
query<T extends MultipleQueryResult = MultipleQueryResult>(sql: MultipleQueryInput): Promise<T>;
|
|
23
|
+
query<T extends object = any>(sql: QueryDataInput<T>): Promise<QueryRowsResult<T>>;
|
|
24
|
+
query<T extends object = any>(sql: QueryInput): Promise<QueryRowsResult<T>>;
|
|
25
|
+
execute(sql: QueryInput | MultipleQueryInput): Promise<void>;
|
|
26
|
+
/** @deprecated 不建议使用 */
|
|
26
27
|
multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlLike | SqlLike[]): Promise<T>;
|
|
27
28
|
get released(): boolean;
|
|
28
29
|
[Symbol.asyncDispose](): Promise<void>;
|
|
@@ -8,6 +8,7 @@ import { ParallelQueryError, ConnectionNotAvailableError } from './errors.js';
|
|
|
8
8
|
class DbPoolTransaction extends DbQuery {
|
|
9
9
|
#errorRollback;
|
|
10
10
|
mode;
|
|
11
|
+
#begin;
|
|
11
12
|
constructor(connect, option) {
|
|
12
13
|
super();
|
|
13
14
|
if (option) {
|
|
@@ -18,110 +19,127 @@ class DbPoolTransaction extends DbQuery {
|
|
|
18
19
|
this.#errorRollback = option.errorRollback;
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
|
-
this.#
|
|
22
|
-
|
|
23
|
-
this.#pending = connect()
|
|
24
|
-
.then((conn) => {
|
|
25
|
-
this.#conn = conn;
|
|
26
|
-
const begin = "BEGIN" + (this.mode ? " TRANSACTION ISOLATION LEVEL " + this.mode : "");
|
|
27
|
-
const promise = conn.multipleQuery(sql instanceof Array ? [begin, ...sql] : [begin, sql]);
|
|
28
|
-
this.#pending = promise;
|
|
29
|
-
this.#query = this.#queryAfter;
|
|
30
|
-
return promise;
|
|
31
|
-
})
|
|
32
|
-
.then((res) => {
|
|
33
|
-
this.#pending = undefined;
|
|
34
|
-
resolve(multiple ? res.slice(1) : res[1]);
|
|
35
|
-
}, (e) => {
|
|
36
|
-
// 语法错误、查询错误、网络错误
|
|
37
|
-
this.#pending = undefined;
|
|
38
|
-
const conn = this.#conn;
|
|
39
|
-
if (!conn) {
|
|
40
|
-
reject(e);
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
const onFinally = () => {
|
|
44
|
-
this.#release(conn, e);
|
|
45
|
-
reject(e);
|
|
46
|
-
};
|
|
47
|
-
if (this.#errorRollback) {
|
|
48
|
-
return conn.rollback().then(onFinally, onFinally);
|
|
49
|
-
}
|
|
50
|
-
else
|
|
51
|
-
onFinally();
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
});
|
|
22
|
+
this.#begin = "BEGIN" + (this.mode ? " TRANSACTION ISOLATION LEVEL " + this.mode : "");
|
|
23
|
+
this.#connect = connect;
|
|
55
24
|
}
|
|
56
|
-
#
|
|
25
|
+
#connect;
|
|
57
26
|
#conn;
|
|
58
27
|
async commit() {
|
|
59
|
-
if (this.#pending)
|
|
60
|
-
throw new ParallelQueryError();
|
|
61
28
|
if (this.#conn) {
|
|
62
|
-
const promise = this.#conn.
|
|
29
|
+
const promise = this.#conn.execute("COMMIT");
|
|
63
30
|
this.#release(this.#conn);
|
|
64
|
-
|
|
31
|
+
return promise;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
this.#release(undefined);
|
|
65
35
|
}
|
|
66
36
|
}
|
|
67
37
|
async rollback() {
|
|
68
|
-
if (this.#pending)
|
|
69
|
-
throw new ParallelQueryError();
|
|
70
38
|
if (this.#conn) {
|
|
71
|
-
const promise = this.#conn.
|
|
39
|
+
const promise = this.#conn.execute("ROLLBACK");
|
|
72
40
|
this.#release(this.#conn);
|
|
73
|
-
|
|
41
|
+
return promise;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
this.#release(undefined);
|
|
74
45
|
}
|
|
75
46
|
}
|
|
76
47
|
savePoint(savePoint) {
|
|
77
|
-
return this.
|
|
48
|
+
return this.execute("SAVEPOINT " + savePoint);
|
|
78
49
|
}
|
|
79
50
|
rollbackTo(savePoint) {
|
|
80
|
-
return this.
|
|
51
|
+
return this.execute("ROLLBACK TO " + savePoint);
|
|
81
52
|
}
|
|
82
|
-
#
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
53
|
+
#pending;
|
|
54
|
+
#getConnQuery(call, callIfFirst = call, queryError) {
|
|
55
|
+
if (this.#pending) {
|
|
56
|
+
return Promise.reject(new ParallelQueryError());
|
|
57
|
+
}
|
|
58
|
+
if (this.#error) {
|
|
59
|
+
return Promise.reject(this.#error);
|
|
60
|
+
}
|
|
61
|
+
let promise;
|
|
62
|
+
if (!this.#conn) {
|
|
63
|
+
promise = this.#connect().then((conn) => {
|
|
64
|
+
if (this.released) {
|
|
65
|
+
conn.release();
|
|
66
|
+
throw this.#error;
|
|
67
|
+
}
|
|
68
|
+
this.#conn = conn;
|
|
69
|
+
let promise = callIfFirst(conn);
|
|
70
|
+
if (queryError) {
|
|
71
|
+
promise = promise.catch((e) => {
|
|
72
|
+
queryError();
|
|
73
|
+
throw e;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return promise;
|
|
77
|
+
}, (e) => {
|
|
78
|
+
this.#release(undefined);
|
|
79
|
+
throw e;
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
promise = call(this.#conn);
|
|
84
|
+
if (queryError) {
|
|
85
|
+
promise = promise.catch((e) => {
|
|
86
|
+
queryError();
|
|
87
|
+
throw e;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
this.#pending = promise;
|
|
92
|
+
return promise.finally(() => {
|
|
90
93
|
this.#pending = undefined;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
#query(call, callIfFirst) {
|
|
97
|
+
const onError = () => {
|
|
91
98
|
if (this.#errorRollback) {
|
|
92
|
-
|
|
93
|
-
this.#release(conn, e);
|
|
94
|
-
throw e;
|
|
95
|
-
};
|
|
96
|
-
return conn.rollback().then(onOk, onOk);
|
|
99
|
+
this.rollback();
|
|
97
100
|
}
|
|
98
101
|
else {
|
|
99
|
-
this.#release(conn
|
|
100
|
-
throw e;
|
|
102
|
+
this.#release(this.#conn);
|
|
101
103
|
}
|
|
102
104
|
};
|
|
103
|
-
|
|
104
|
-
return conn.multipleQuery(sql).then(onFinish, onError);
|
|
105
|
-
else
|
|
106
|
-
return conn.query(sql).then(onFinish, onError);
|
|
105
|
+
return this.#getConnQuery(call, callIfFirst, onError);
|
|
107
106
|
}
|
|
108
|
-
#query;
|
|
109
107
|
query(sql) {
|
|
110
|
-
|
|
111
|
-
return
|
|
112
|
-
|
|
108
|
+
return this.#query((conn) => {
|
|
109
|
+
return conn.query(sql);
|
|
110
|
+
}, async (conn) => {
|
|
111
|
+
if (typeof sql === "function")
|
|
112
|
+
sql = sql();
|
|
113
|
+
const isArray = sql instanceof Array;
|
|
114
|
+
const result = await conn.query(isArray ? [this.#begin, ...sql] : [this.#begin, sql]);
|
|
115
|
+
if (isArray)
|
|
116
|
+
return result.slice(1);
|
|
117
|
+
else
|
|
118
|
+
return result[1];
|
|
119
|
+
});
|
|
113
120
|
}
|
|
121
|
+
async execute(sql) {
|
|
122
|
+
return this.#query((conn) => {
|
|
123
|
+
return conn.execute(sql);
|
|
124
|
+
}, (conn) => {
|
|
125
|
+
if (typeof sql === "function")
|
|
126
|
+
sql = sql();
|
|
127
|
+
return conn.execute(sql instanceof Array ? [this.#begin, ...sql] : [this.#begin, sql]);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/** @deprecated 不建议使用 */
|
|
114
131
|
multipleQuery(sql) {
|
|
115
|
-
|
|
116
|
-
return
|
|
117
|
-
|
|
132
|
+
return this.#query((conn) => {
|
|
133
|
+
return conn.multipleQuery(sql);
|
|
134
|
+
}, (conn) => {
|
|
135
|
+
return conn.multipleQuery(sql instanceof Array ? [this.#begin, ...sql] : [this.#begin, sql]);
|
|
136
|
+
});
|
|
118
137
|
}
|
|
119
138
|
#error;
|
|
120
139
|
#release(conn, error = new ConnectionNotAvailableError("Connection already release")) {
|
|
121
140
|
this.#error = error;
|
|
122
|
-
this.#query = () => Promise.reject(this.#error);
|
|
123
141
|
this.#conn = undefined;
|
|
124
|
-
conn
|
|
142
|
+
conn?.release();
|
|
125
143
|
}
|
|
126
144
|
get released() {
|
|
127
145
|
return !!this.#error;
|
package/dist/client/DbQuery.d.ts
CHANGED
|
@@ -1,37 +1,47 @@
|
|
|
1
1
|
import { SqlLike } from "./interfaces.ts";
|
|
2
2
|
import { MultipleQueryResult, QueryRowsResult } from "./DbQueryBase.ts";
|
|
3
3
|
import { SqlStatementDataset } from "./_type.ts";
|
|
4
|
+
/** @public */
|
|
5
|
+
export type QueryInput = SqlLike | (() => SqlLike);
|
|
6
|
+
/** @public */
|
|
7
|
+
export type QueryDataInput<T> = SqlStatementDataset<T> | (() => SqlStatementDataset<T>);
|
|
8
|
+
/** @public */
|
|
9
|
+
export type MultipleQueryInput = SqlLike[] | (() => SqlLike[]);
|
|
4
10
|
/**
|
|
5
11
|
* SQL 查询相关操作
|
|
6
12
|
* @public
|
|
7
13
|
*/
|
|
8
14
|
export declare abstract class DbQuery {
|
|
9
|
-
abstract
|
|
10
|
-
abstract query<T =
|
|
15
|
+
abstract execute(sql: QueryInput | MultipleQueryInput): Promise<void>;
|
|
16
|
+
abstract query<T extends MultipleQueryResult = MultipleQueryResult>(sql: MultipleQueryInput): Promise<T>;
|
|
17
|
+
abstract query<T = any>(sql: QueryDataInput<T>): Promise<QueryRowsResult<T>>;
|
|
18
|
+
abstract query<T = any>(sql: QueryInput): Promise<QueryRowsResult<T>>;
|
|
19
|
+
/** @deprecated 不建议使用。改用 query() */
|
|
11
20
|
abstract multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlLike | SqlLike[]): Promise<T>;
|
|
12
21
|
/** 单语句查询受影响的行 */
|
|
13
|
-
queryCount(sql:
|
|
22
|
+
queryCount(sql: QueryInput): Promise<number>;
|
|
14
23
|
/** 单语句查询,不应查询多语句,否则返回错误值 */
|
|
15
|
-
queryRows<T = any>(sql:
|
|
24
|
+
queryRows<T = any>(sql: QueryDataInput<T>): Promise<T[]>;
|
|
16
25
|
/** 单语句查询,不应查询多语句,否则返回错误值 */
|
|
17
|
-
queryRows<T = any>(sql:
|
|
26
|
+
queryRows<T = any>(sql: QueryInput): Promise<T[]>;
|
|
18
27
|
/** 单语句查询,只返回第一行。如果查询没有返回行,则抛出异常。 */
|
|
19
|
-
queryFirstRow<T = any>(sql:
|
|
20
|
-
queryFirstRow<T = any>(sql:
|
|
28
|
+
queryFirstRow<T = any>(sql: QueryDataInput<T>): Promise<T>;
|
|
29
|
+
queryFirstRow<T = any>(sql: QueryInput): Promise<T>;
|
|
21
30
|
/**
|
|
22
31
|
* 查询行
|
|
23
32
|
* 不应查询单语句,否则返回错误值
|
|
33
|
+
* @deprecated 不建议使用。
|
|
24
34
|
*/
|
|
25
35
|
multipleQueryRows<T extends any[] = any[]>(sql: SqlLike | SqlLike[]): Promise<T[]>;
|
|
26
36
|
/**
|
|
27
37
|
* 指定某一列为key,返回 key 到 row 的映射
|
|
28
38
|
* 单语句查询,不应查询多语句,否则返回错误值
|
|
29
39
|
*/
|
|
30
|
-
queryMap<T extends Record<string, any> = Record<string, any>, K extends keyof T = string>(sql:
|
|
40
|
+
queryMap<T extends Record<string, any> = Record<string, any>, K extends keyof T = string>(sql: QueryDataInput<T>, key: K): Promise<Map<T[K], T>>;
|
|
31
41
|
/**
|
|
32
42
|
* 指定某一列为key,返回 key 到 row 的映射
|
|
33
43
|
* 单语句查询,不应查询多语句,否则返回错误值
|
|
34
44
|
*/
|
|
35
|
-
queryMap<T extends Record<string, any> = Record<string, any>, K extends keyof T = string>(sql:
|
|
45
|
+
queryMap<T extends Record<string, any> = Record<string, any>, K extends keyof T = string>(sql: QueryInput, key: K): Promise<Map<T[K], T>>;
|
|
36
46
|
}
|
|
37
47
|
//# sourceMappingURL=DbQuery.d.ts.map
|
package/dist/client/DbQuery.js
CHANGED
|
@@ -12,20 +12,19 @@ export interface QueryRowsResult<T = any> extends SingleQueryResult {
|
|
|
12
12
|
}
|
|
13
13
|
/** @public */
|
|
14
14
|
export type MultipleQueryResult = SingleQueryResult[];
|
|
15
|
-
/**
|
|
16
|
-
*
|
|
17
|
-
* @deprecated 已废弃
|
|
18
|
-
* @public
|
|
19
|
-
*/
|
|
20
|
-
export type QueryResult = MultipleQueryResult | SingleQueryResult;
|
|
21
15
|
/**
|
|
22
16
|
* 数据库客户端的最小实现接口
|
|
23
17
|
* @public
|
|
24
18
|
*/
|
|
25
19
|
export interface DbQueryBase {
|
|
26
|
-
/**
|
|
20
|
+
/** 单语句查询, 忽略返回值 */
|
|
21
|
+
execute(sql: SqlLike | SqlLike[]): Promise<void>;
|
|
22
|
+
/** 单语句查询。单个 SqlLike 不应包含多语句,否则返回错误值 */
|
|
27
23
|
query<T = any>(sql: SqlLike): Promise<QueryRowsResult<T>>;
|
|
28
24
|
/** 多语句查询 */
|
|
25
|
+
query<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlLike[]): Promise<T>;
|
|
26
|
+
query(sql: SqlLike[] | SqlLike): Promise<unknown[] | unknown>;
|
|
27
|
+
/** 多语句查询 */
|
|
29
28
|
multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlLike | SqlLike[]): Promise<T>;
|
|
30
29
|
}
|
|
31
30
|
/**
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { DbCursor, DbCursorOption } from "./DbCursor.ts";
|
|
2
|
+
import { DbPoolConnection } from "./DbPoolConnection.ts";
|
|
3
|
+
import { DbQuery } from "./DbQuery.ts";
|
|
4
|
+
import { DbPool, DbTransaction, SqlLike, TransactionMode } from "./interfaces.ts";
|
|
5
|
+
import { QueryRowsResult } from "./DbQueryBase.ts";
|
|
6
|
+
import { SqlStatementDataset, InferQueryResult } from "./_type.ts";
|
|
7
|
+
/** @public */
|
|
8
|
+
export interface ExecutableSQL<T = unknown> {
|
|
9
|
+
genSql(): string;
|
|
10
|
+
then(resolve: (data: T) => void, reject: () => void): void;
|
|
11
|
+
}
|
|
12
|
+
/** @public */
|
|
13
|
+
export interface QueryableDataSQL<Raw, Res = QueryRowsResult<Raw>> extends ExecutableSQL<Res> {
|
|
14
|
+
query(): Promise<QueryRowsResult<Raw>>;
|
|
15
|
+
queryCount(): Promise<number>;
|
|
16
|
+
queryRows(): Promise<Raw[]>;
|
|
17
|
+
queryFirstRow(): Promise<Raw>;
|
|
18
|
+
queryMap<K>(key: string): Promise<Map<K, Raw>>;
|
|
19
|
+
cursor(): Promise<DbCursor<Raw>>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* @public
|
|
23
|
+
* 池链接查询
|
|
24
|
+
*/
|
|
25
|
+
export declare abstract class DbQueryPool extends DbQuery implements DbPool {
|
|
26
|
+
abstract connect(): Promise<DbPoolConnection>;
|
|
27
|
+
/** 连接池空闲链接数量 */
|
|
28
|
+
abstract idleCount: number;
|
|
29
|
+
/** 连接池总链接数量 */
|
|
30
|
+
abstract totalCount: number;
|
|
31
|
+
abstract begin(mode?: TransactionMode): DbTransaction;
|
|
32
|
+
abstract cursor<T extends {}>(sql: SqlStatementDataset<T>): Promise<DbCursor<T>>;
|
|
33
|
+
abstract cursor<T>(sql: SqlLike, option?: DbCursorOption): Promise<DbCursor<T>>;
|
|
34
|
+
createQueryableSQL<Raw>(statement: SqlStatementDataset<Raw>): QueryableDataSQL<Raw, void>;
|
|
35
|
+
createQueryableSQL<Raw>(statement: SqlLike): QueryableDataSQL<Raw, void>;
|
|
36
|
+
createQueryableSQL<T extends SqlStatementDataset<any>, Res>(statement: T, transform: (queryable: DbQueryPool, statement: T) => Promise<Res>): QueryableDataSQL<InferQueryResult<T>, Awaited<Res>>;
|
|
37
|
+
createQueryableSQL<Raw, Res>(statement: SqlLike, transform: (queryable: DbQueryPool, statement: SqlLike) => Promise<Res>): QueryableDataSQL<Raw, Res>;
|
|
38
|
+
createExecutableSQL(statement: SqlLike): ExecutableSQL<void>;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=DbQueryPool.d.ts.map
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { DbQuery } from './DbQuery.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
* 池链接查询
|
|
6
|
+
*/
|
|
7
|
+
class DbQueryPool extends DbQuery {
|
|
8
|
+
createQueryableSQL(statement, transform) {
|
|
9
|
+
return new QueryableSqlImpl(this, statement, transform);
|
|
10
|
+
}
|
|
11
|
+
createExecutableSQL(statement) {
|
|
12
|
+
return new QueryableSqlImpl(this, statement);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
class QueryableSqlImpl {
|
|
16
|
+
queryClient;
|
|
17
|
+
statement;
|
|
18
|
+
transform;
|
|
19
|
+
constructor(queryClient, statement, transform = defaultTransform) {
|
|
20
|
+
this.queryClient = queryClient;
|
|
21
|
+
this.statement = statement;
|
|
22
|
+
this.transform = transform;
|
|
23
|
+
}
|
|
24
|
+
genSql() {
|
|
25
|
+
return this.statement.toString();
|
|
26
|
+
}
|
|
27
|
+
toString() {
|
|
28
|
+
return this.genSql();
|
|
29
|
+
}
|
|
30
|
+
query() {
|
|
31
|
+
return this.queryClient.query(this.statement);
|
|
32
|
+
}
|
|
33
|
+
queryCount() {
|
|
34
|
+
return this.queryClient.queryCount(this.statement);
|
|
35
|
+
}
|
|
36
|
+
queryRows() {
|
|
37
|
+
return this.queryClient.queryRows(this.statement);
|
|
38
|
+
}
|
|
39
|
+
queryFirstRow() {
|
|
40
|
+
return this.queryClient.queryFirstRow(this.statement);
|
|
41
|
+
}
|
|
42
|
+
queryMap(key) {
|
|
43
|
+
return this.queryClient.queryMap(this.statement, key);
|
|
44
|
+
}
|
|
45
|
+
cursor() {
|
|
46
|
+
return this.queryClient.cursor(this.statement);
|
|
47
|
+
}
|
|
48
|
+
then(resolve, reject) {
|
|
49
|
+
this.transform(this.queryClient, this.statement).then(resolve, reject);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function defaultTransform(queryClient, statement) {
|
|
53
|
+
return queryClient.execute(statement);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export { DbQueryPool };
|
package/dist/client/_type.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export type { SqlStatementDataset, SqlTemplate, SqlTextTemplate } from "../sql_gen/mod.ts";
|
|
1
|
+
export type { SqlStatementDataset, SqlTemplate, SqlTextTemplate, InferQueryResult } from "../sql_gen/mod.ts";
|
|
2
2
|
//# sourceMappingURL=_type.d.ts.map
|
|
@@ -50,10 +50,10 @@ export interface DbTransaction extends DbQuery, AsyncDisposable {
|
|
|
50
50
|
commit(): Promise<void>;
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
|
+
* 数据库连接池
|
|
53
54
|
* @public
|
|
54
|
-
* 池链接查询
|
|
55
55
|
*/
|
|
56
|
-
export interface
|
|
56
|
+
export interface DbPool {
|
|
57
57
|
connect(): Promise<DbPoolConnection>;
|
|
58
58
|
idleCount: number;
|
|
59
59
|
totalCount: number;
|
package/dist/client/mod.d.ts
CHANGED
package/dist/client/mod.js
CHANGED
|
@@ -31,6 +31,10 @@ export declare abstract class SqlStatementDataset<T> extends SqlStatement implem
|
|
|
31
31
|
* 如果是 选择语句,则是 (xxx)
|
|
32
32
|
*/
|
|
33
33
|
toSelect(asName?: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* 仅用于类型推断,不应被调用
|
|
36
|
+
*/
|
|
37
|
+
__infer(v: T): never;
|
|
34
38
|
}
|
|
35
39
|
/** @public */
|
|
36
40
|
export declare class SqlTextStatementDataset<T> extends SqlStatementDataset<T> {
|
|
@@ -18,6 +18,12 @@ class SqlStatementDataset extends SqlStatement {
|
|
|
18
18
|
result += " AS " + asName;
|
|
19
19
|
return result;
|
|
20
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* 仅用于类型推断,不应被调用
|
|
23
|
+
*/
|
|
24
|
+
__infer(v) {
|
|
25
|
+
throw new Error("cannot call test on SqlStatementDataset");
|
|
26
|
+
}
|
|
21
27
|
}
|
|
22
28
|
/** @public */
|
|
23
29
|
class SqlTextStatementDataset extends SqlStatementDataset {
|