@asla/yoursql 0.11.1 → 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.
@@ -1,6 +1,5 @@
1
- import { SqlStatementDataset } from "./_type.ts";
2
- import { DbQuery } from "./DbQuery.ts";
3
- import type { MultipleQueryResult, QueryRowsResult, 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 = any>(sql: SqlStatementDataset<T>): Promise<QueryRowsResult<T>>;
16
- query<T = any>(sql: SqlLike): Promise<QueryRowsResult<T>>;
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 object = any>(sql: SqlStatementDataset<T>): Promise<QueryRowsResult<T>>;
24
- query<T extends object = any>(sql: SqlLike): Promise<QueryRowsResult<T>>;
25
- multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlStatementDataset<T>): Promise<T>;
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.#query = ((sql, multiple) => {
22
- return new Promise((resolve, reject) => {
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
- #pending;
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.query("COMMIT");
29
+ const promise = this.#conn.execute("COMMIT");
63
30
  this.#release(this.#conn);
64
- await promise;
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.query("ROLLBACK");
39
+ const promise = this.#conn.execute("ROLLBACK");
72
40
  this.#release(this.#conn);
73
- await promise;
41
+ return promise;
42
+ }
43
+ else {
44
+ this.#release(undefined);
74
45
  }
75
46
  }
76
47
  savePoint(savePoint) {
77
- return this.query("SAVEPOINT" + savePoint).then(() => { });
48
+ return this.execute("SAVEPOINT " + savePoint);
78
49
  }
79
50
  rollbackTo(savePoint) {
80
- return this.query("ROLLBACK TO " + savePoint).then(() => { });
51
+ return this.execute("ROLLBACK TO " + savePoint);
81
52
  }
82
- #queryAfter(sql, multiple) {
83
- const conn = this.#conn;
84
- const onFinish = (res) => {
85
- this.#query = this.#queryAfter;
86
- this.#pending = undefined;
87
- return res;
88
- };
89
- const onError = (e) => {
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
- const onOk = () => {
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, e);
100
- throw e;
102
+ this.#release(this.#conn);
101
103
  }
102
104
  };
103
- if (multiple)
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
- if (this.#pending)
111
- return Promise.reject(new ParallelQueryError());
112
- return this.#query(sql);
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
- if (this.#pending)
116
- return Promise.reject(new ParallelQueryError());
117
- return this.#query(sql, true);
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.release();
142
+ conn?.release();
125
143
  }
126
144
  get released() {
127
145
  return !!this.#error;
@@ -1,36 +1,47 @@
1
1
  import { SqlLike } from "./interfaces.ts";
2
- import { MultipleQueryResult, DbQueryBase, QueryRowsResult } from "./DbQueryBase.ts";
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
- export declare abstract class DbQuery implements DbQueryBase {
9
- abstract query<T = any>(sql: SqlLike): Promise<QueryRowsResult<T>>;
14
+ export declare abstract class DbQuery {
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() */
10
20
  abstract multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlLike | SqlLike[]): Promise<T>;
11
21
  /** 单语句查询受影响的行 */
12
- queryCount(sql: SqlLike): Promise<number>;
22
+ queryCount(sql: QueryInput): Promise<number>;
13
23
  /** 单语句查询,不应查询多语句,否则返回错误值 */
14
- queryRows<T = any>(sql: SqlStatementDataset<T>): Promise<T[]>;
24
+ queryRows<T = any>(sql: QueryDataInput<T>): Promise<T[]>;
15
25
  /** 单语句查询,不应查询多语句,否则返回错误值 */
16
- queryRows<T = any>(sql: SqlLike): Promise<T[]>;
26
+ queryRows<T = any>(sql: QueryInput): Promise<T[]>;
17
27
  /** 单语句查询,只返回第一行。如果查询没有返回行,则抛出异常。 */
18
- queryFirstRow<T = any>(sql: SqlStatementDataset<T>): Promise<T>;
19
- queryFirstRow<T = any>(sql: SqlLike): Promise<T>;
28
+ queryFirstRow<T = any>(sql: QueryDataInput<T>): Promise<T>;
29
+ queryFirstRow<T = any>(sql: QueryInput): Promise<T>;
20
30
  /**
21
31
  * 查询行
22
32
  * 不应查询单语句,否则返回错误值
33
+ * @deprecated 不建议使用。
23
34
  */
24
35
  multipleQueryRows<T extends any[] = any[]>(sql: SqlLike | SqlLike[]): Promise<T[]>;
25
36
  /**
26
37
  * 指定某一列为key,返回 key 到 row 的映射
27
38
  * 单语句查询,不应查询多语句,否则返回错误值
28
39
  */
29
- queryMap<T extends Record<string, any> = Record<string, any>, K extends keyof T = string>(sql: SqlStatementDataset<T>, key: K): Promise<Map<T[K], T>>;
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>>;
30
41
  /**
31
42
  * 指定某一列为key,返回 key 到 row 的映射
32
43
  * 单语句查询,不应查询多语句,否则返回错误值
33
44
  */
34
- queryMap<T extends Record<string, any> = Record<string, any>, K extends keyof T = string>(sql: SqlLike, key: K): Promise<Map<T[K], T>>;
45
+ queryMap<T extends Record<string, any> = Record<string, any>, K extends keyof T = string>(sql: QueryInput, key: K): Promise<Map<T[K], T>>;
35
46
  }
36
47
  //# sourceMappingURL=DbQuery.d.ts.map
@@ -24,6 +24,7 @@ class DbQuery {
24
24
  /**
25
25
  * 查询行
26
26
  * 不应查询单语句,否则返回错误值
27
+ * @deprecated 不建议使用。
27
28
  */
28
29
  multipleQueryRows(sql) {
29
30
  return this.multipleQuery(sql).then((res) => res.map((item) => item.rows ?? []));
@@ -1,4 +1,4 @@
1
- import { SqlStatementDataset } from "./_type.ts";
1
+ import { SqlTemplate } from "./_type.ts";
2
2
  import { SqlLike } from "./interfaces.ts";
3
3
  /** @public */
4
4
  export interface SingleQueryResult {
@@ -12,17 +12,27 @@ export interface QueryRowsResult<T = any> extends SingleQueryResult {
12
12
  }
13
13
  /** @public */
14
14
  export type MultipleQueryResult = SingleQueryResult[];
15
- /** @public */
16
- export type QueryResult = MultipleQueryResult | SingleQueryResult;
17
- /** @public */
15
+ /**
16
+ * 数据库客户端的最小实现接口
17
+ * @public
18
+ */
18
19
  export interface DbQueryBase {
19
- /** 单语句查询,不应查询多语句,否则返回错误值 */
20
- query<T = any>(sql: SqlStatementDataset<T>): Promise<QueryRowsResult<T>>;
21
- /** 单语句查询,不应查询多语句,否则返回错误值 */
20
+ /** 单语句查询, 忽略返回值 */
21
+ execute(sql: SqlLike | SqlLike[]): Promise<void>;
22
+ /** 单语句查询。单个 SqlLike 不应包含多语句,否则返回错误值 */
22
23
  query<T = any>(sql: SqlLike): Promise<QueryRowsResult<T>>;
23
24
  /** 多语句查询 */
25
+ query<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlLike[]): Promise<T>;
26
+ query(sql: SqlLike[] | SqlLike): Promise<unknown[] | unknown>;
27
+ /** 多语句查询 */
24
28
  multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlLike | SqlLike[]): Promise<T>;
25
29
  }
26
- /** @public */
30
+ /**
31
+ * 将 SqlLike 转换为字符串
32
+ * @public
33
+ *
34
+ */
27
35
  export declare function sqlLikeToString(sqlLike: SqlLike): string;
36
+ /** @public */
37
+ export declare function isSqlTemplate(obj: any): obj is SqlTemplate;
28
38
  //# sourceMappingURL=DbQueryBase.d.ts.map
@@ -1,11 +1,32 @@
1
- /** @public */
1
+ /**
2
+ * 将 SqlLike 转换为字符串
3
+ * @public
4
+ *
5
+ */
2
6
  function sqlLikeToString(sqlLike) {
3
7
  if (typeof sqlLike === "string") {
4
8
  return sqlLike;
5
9
  }
6
10
  else {
7
- return sqlLike.genSql();
11
+ if (isSqlTemplate(sqlLike)) {
12
+ const { templates } = sqlLike;
13
+ const textArgs = sqlLike.toTextArgs();
14
+ let sql = templates[0];
15
+ for (let i = 1; i < templates.length; i++) {
16
+ sql += textArgs[i - 1] + templates[i];
17
+ }
18
+ return sql;
19
+ }
20
+ else {
21
+ return sqlLike.genSql();
22
+ }
8
23
  }
9
24
  }
25
+ /** @public */
26
+ function isSqlTemplate(obj) {
27
+ if (typeof obj !== "object" || obj === null)
28
+ return false;
29
+ return Array.isArray(obj.templates) && Array.isArray(obj.args) && typeof obj.toTextArgs === "function";
30
+ }
10
31
 
11
- export { sqlLikeToString };
32
+ export { isSqlTemplate, sqlLikeToString };
@@ -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 };
@@ -1,2 +1,2 @@
1
- export type { SqlStatementDataset } from "../sql_gen/mod.ts";
1
+ export type { SqlStatementDataset, SqlTemplate, SqlTextTemplate, InferQueryResult } from "../sql_gen/mod.ts";
2
2
  //# sourceMappingURL=_type.d.ts.map
@@ -1,7 +1,7 @@
1
1
  import { DbQuery } from "./DbQuery.ts";
2
2
  import { DbPoolConnection } from "./DbPoolConnection.ts";
3
3
  import { DbCursor, DbCursorOption } from "./DbCursor.ts";
4
- import { SqlStatementDataset } from "./_type.ts";
4
+ import { SqlStatementDataset, SqlTemplate } from "./_type.ts";
5
5
  /**
6
6
  * 数据库连接
7
7
  * @public
@@ -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 DbQueryPool extends DbQuery {
56
+ export interface DbPool {
57
57
  connect(): Promise<DbPoolConnection>;
58
58
  idleCount: number;
59
59
  totalCount: number;
@@ -64,5 +64,5 @@ export interface DbQueryPool extends DbQuery {
64
64
  /** @public */
65
65
  export type SqlLike = {
66
66
  genSql(): string;
67
- } | string;
67
+ } | SqlTemplate | string;
68
68
  //# sourceMappingURL=interfaces.d.ts.map
@@ -5,4 +5,5 @@ export * from "./DbQuery.ts";
5
5
  export * from "./DbCursor.ts";
6
6
  export * from "./DbPoolConnection.ts";
7
7
  export * from "./DbPoolTransaction.ts";
8
+ export * from "./DbQueryPool.ts";
8
9
  //# sourceMappingURL=mod.d.ts.map
@@ -1,6 +1,7 @@
1
1
  export { ConnectionNotAvailableError, ParallelQueryError } from './errors.js';
2
- export { sqlLikeToString } from './DbQueryBase.js';
2
+ export { isSqlTemplate, sqlLikeToString } from './DbQueryBase.js';
3
3
  export { DbQuery } from './DbQuery.js';
4
4
  export { DbCursor } from './DbCursor.js';
5
5
  export { DbPoolConnection } from './DbPoolConnection.js';
6
6
  export { DbPoolTransaction } from './DbPoolTransaction.js';
7
+ export { DbQueryPool } from './DbQueryPool.js';
@@ -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> {
@@ -44,13 +48,14 @@ export declare class SqlTextStatementDataset<T> extends SqlStatementDataset<T> {
44
48
  */
45
49
  export type InferQueryResult<T> = T extends SqlStatementDataset<infer P> ? P : never;
46
50
  /** @public */
47
- export interface SqlTemplate {
51
+ export interface SqlTemplate<T extends readonly any[] = readonly unknown[]> {
48
52
  readonly templates: readonly string[];
49
- readonly args: readonly unknown[];
50
- toTextTemplate(): {
51
- text: string;
52
- args: string[];
53
- };
54
- genSql(): string;
53
+ readonly args: T;
54
+ toTextArgs(): string[];
55
+ }
56
+ /** @public */
57
+ export interface SqlTextTemplate {
58
+ readonly textTemplate: string;
59
+ readonly textArgs: readonly string[];
55
60
  }
56
61
  //# sourceMappingURL=SqlStatement.d.ts.map
@@ -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 {
@@ -11,6 +11,7 @@ export { selectColumns } from './util.js';
11
11
  export { TypeChecker } from './your_table/checker.js';
12
12
  export { ColumnMeta, CustomDbType, YourTypeMap } from './your_table/infer_db_type.js';
13
13
  export { YourTable } from './your_table/table.js';
14
+ export { TemplateSqlStatement as ValueSqlTemplate } from './sql_value/ValueSqlTemplate.js';
14
15
  export { orderBy } from './statement/select_impl.js';
15
16
 
16
17
  /**
@@ -1,13 +1,14 @@
1
- import { SqlTemplate } from "../SqlStatement.ts";
2
- export declare class ValueSqlTemplate implements SqlTemplate {
1
+ import { SqlTemplate, SqlTextTemplate, SqlStatement } from "../SqlStatement.ts";
2
+ /** @alpha */
3
+ export declare class TemplateSqlStatement extends SqlStatement implements SqlTemplate, SqlTextTemplate {
4
+ #private;
3
5
  private v;
4
6
  readonly templates: readonly string[];
5
7
  readonly args: readonly unknown[];
6
8
  constructor(v: (value: unknown) => string, templates: readonly string[], values: readonly unknown[]);
7
- toTextTemplate(): {
8
- text: string;
9
- args: string[];
10
- };
9
+ get textArgs(): readonly string[];
10
+ get textTemplate(): string;
11
+ toTextArgs(): string[];
11
12
  genSql(): string;
12
13
  }
13
14
  //# sourceMappingURL=ValueSqlTemplate.d.ts.map
@@ -1,30 +1,49 @@
1
- class ValueSqlTemplate {
1
+ import { SqlStatement } from '../SqlStatement.js';
2
+
3
+ /** @alpha */
4
+ class TemplateSqlStatement extends SqlStatement {
2
5
  v;
3
6
  templates;
4
7
  args;
5
8
  constructor(v, templates, values) {
9
+ super();
6
10
  this.v = v;
7
11
  this.templates = templates;
8
12
  this.args = values;
9
13
  }
10
- toTextTemplate() {
11
- const { templates, args } = this;
12
- let text = templates[0];
13
- for (let i = 1; i < templates.length; i++) {
14
- text += "$" + i;
15
- text += templates[i];
14
+ #textArgs;
15
+ get textArgs() {
16
+ if (!this.#textArgs) {
17
+ const textArgs = this.args.map((value) => this.v(value));
18
+ this.#textArgs = textArgs;
16
19
  }
17
- const values = args.map((value) => this.v(value));
18
- return { text, args: values };
20
+ return this.#textArgs;
21
+ }
22
+ #textTemplate;
23
+ get textTemplate() {
24
+ if (this.#textTemplate === undefined) {
25
+ const { templates } = this;
26
+ let text = templates[0];
27
+ for (let i = 1; i < templates.length; i++) {
28
+ text += "$" + i;
29
+ text += templates[i];
30
+ }
31
+ this.#textTemplate = text;
32
+ }
33
+ return this.#textTemplate;
34
+ }
35
+ toTextArgs() {
36
+ return [...this.textArgs];
19
37
  }
20
38
  genSql() {
21
- const { templates, args } = this;
39
+ const { templates } = this;
40
+ const textArgs = this.textArgs;
22
41
  let sql = this.templates[0];
23
42
  for (let i = 1; i < templates.length; i++) {
24
- sql += this.v(args[i - 1]) + templates[i];
43
+ sql += textArgs[i - 1] + templates[i];
25
44
  }
26
45
  return sql;
27
46
  }
28
47
  }
29
48
 
30
- export { ValueSqlTemplate };
49
+ export { TemplateSqlStatement };
@@ -1,5 +1,6 @@
1
- import { SqlTemplate } from "../SqlStatement.ts";
1
+ import { TemplateSqlStatement } from "./ValueSqlTemplate.ts";
2
2
  import { AssertJsType, ObjectToValueKeys } from "./type.ts";
3
+ export { TemplateSqlStatement as ValueSqlTemplate } from "./ValueSqlTemplate.ts";
3
4
  /** @public js 对象到编码函数的映射*/
4
5
  export type JsObjectMapSql = Map<new (...args: any[]) => any, SqlValueEncoder>;
5
6
  /** @public 将 js 值转为 SQl 字符串的函数*/
@@ -41,7 +42,7 @@ export declare class SqlValuesCreator {
41
42
  */
42
43
  toSqlStr(value: any, assertJsType?: AssertJsType): string;
43
44
  /** @alpha */
44
- gen(split: TemplateStringsArray, ...values: any[]): SqlTemplate;
45
+ gen(split: TemplateStringsArray, ...values: any[]): TemplateSqlStatement;
45
46
  /** 获取值对应已定义的类 */
46
47
  getClassType(value: object): undefined | (new (...args: unknown[]) => unknown);
47
48
  protected defaultObject(value: object): string;
@@ -1,5 +1,5 @@
1
1
  import { getObjectListKeys } from '../_statement.js';
2
- import { ValueSqlTemplate } from './ValueSqlTemplate.js';
2
+ import { TemplateSqlStatement } from './ValueSqlTemplate.js';
3
3
 
4
4
  /**
5
5
  * SQL value 生成器
@@ -102,7 +102,7 @@ class SqlValuesCreator {
102
102
  sql += this.toSqlStr(values[i]);
103
103
  sql += split[i + 1];
104
104
  }
105
- return new ValueSqlTemplate(this.toSqlStr.bind(this), split, values);
105
+ return new TemplateSqlStatement(this.toSqlStr.bind(this), split, values);
106
106
  }
107
107
  /** 获取值对应已定义的类 */
108
108
  getClassType(value) {
@@ -277,4 +277,4 @@ class SqlExplicitValuesStatement {
277
277
  }
278
278
  }
279
279
 
280
- export { SqlExplicitValuesStatement, SqlValuesCreator };
280
+ export { SqlExplicitValuesStatement, SqlValuesCreator, TemplateSqlStatement as ValueSqlTemplate };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asla/yoursql",
3
- "version": "0.11.1",
3
+ "version": "0.11.3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",