@asla/yoursql 0.13.1 → 0.14.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.
Files changed (78) hide show
  1. package/README.md +14 -140
  2. package/dist/sql_gen/SqlStatement.d.ts +0 -1
  3. package/dist/sql_gen/SqlStatement.js +3 -5
  4. package/dist/sql_gen/_statement.d.ts +0 -1
  5. package/dist/sql_gen/_statement.js +10 -13
  6. package/dist/sql_gen/mod.d.ts +0 -1
  7. package/dist/sql_gen/mod.js +10 -18
  8. package/dist/sql_gen/sql_value/SqlValuesDataset.d.ts +0 -1
  9. package/dist/sql_gen/sql_value/SqlValuesDataset.js +1 -3
  10. package/dist/sql_gen/sql_value/ValueSqlTemplate.d.ts +0 -1
  11. package/dist/sql_gen/sql_value/ValueSqlTemplate.js +2 -5
  12. package/dist/sql_gen/sql_value/_to_values.d.ts +0 -1
  13. package/dist/sql_gen/sql_value/_to_values.js +5 -8
  14. package/dist/sql_gen/sql_value/db_type.d.ts +0 -1
  15. package/dist/sql_gen/sql_value/db_type.js +2 -5
  16. package/dist/sql_gen/sql_value/sql_value.d.ts +0 -1
  17. package/dist/sql_gen/sql_value/sql_value.js +5 -7
  18. package/dist/sql_gen/sql_value/type.d.ts +0 -1
  19. package/dist/sql_gen/sql_value/type.js +1 -0
  20. package/dist/sql_gen/statement/_modify.d.ts +0 -1
  21. package/dist/sql_gen/statement/_modify.js +1 -0
  22. package/dist/sql_gen/statement/cte.d.ts +0 -1
  23. package/dist/sql_gen/statement/cte.js +7 -10
  24. package/dist/sql_gen/statement/delete.d.ts +0 -1
  25. package/dist/sql_gen/statement/delete.js +2 -5
  26. package/dist/sql_gen/statement/delete_chain.d.ts +0 -1
  27. package/dist/sql_gen/statement/delete_chain.js +1 -0
  28. package/dist/sql_gen/statement/delete_impl.d.ts +0 -1
  29. package/dist/sql_gen/statement/delete_impl.js +3 -6
  30. package/dist/sql_gen/statement/insert.d.ts +0 -1
  31. package/dist/sql_gen/statement/insert.js +2 -5
  32. package/dist/sql_gen/statement/insert_chain.d.ts +0 -1
  33. package/dist/sql_gen/statement/insert_chain.js +1 -0
  34. package/dist/sql_gen/statement/insert_impl.d.ts +0 -1
  35. package/dist/sql_gen/statement/insert_impl.js +4 -7
  36. package/dist/sql_gen/statement/mod.d.ts +0 -1
  37. package/dist/sql_gen/statement/mod.js +9 -0
  38. package/dist/sql_gen/statement/select.d.ts +0 -1
  39. package/dist/sql_gen/statement/select.js +4 -7
  40. package/dist/sql_gen/statement/select_chain.d.ts +0 -1
  41. package/dist/sql_gen/statement/select_chain.js +1 -0
  42. package/dist/sql_gen/statement/select_impl.d.ts +0 -1
  43. package/dist/sql_gen/statement/select_impl.js +4 -7
  44. package/dist/sql_gen/statement/update.d.ts +0 -1
  45. package/dist/sql_gen/statement/update.js +2 -5
  46. package/dist/sql_gen/statement/update_chain.d.ts +0 -1
  47. package/dist/sql_gen/statement/update_chain.js +1 -0
  48. package/dist/sql_gen/statement/update_impl.d.ts +0 -1
  49. package/dist/sql_gen/statement/update_impl.js +4 -7
  50. package/dist/sql_gen/util.d.ts +0 -1
  51. package/dist/sql_gen/util.js +1 -3
  52. package/dist/sql_gen/your_table/checker.d.ts +0 -1
  53. package/dist/sql_gen/your_table/checker.js +2 -5
  54. package/dist/sql_gen/your_table/infer_db_type.d.ts +0 -1
  55. package/dist/sql_gen/your_table/infer_db_type.js +3 -5
  56. package/dist/sql_gen/your_table/mod.d.ts +0 -1
  57. package/dist/sql_gen/your_table/mod.js +3 -0
  58. package/dist/sql_gen/your_table/table.d.ts +0 -1
  59. package/dist/sql_gen/your_table/table.js +2 -5
  60. package/package.json +31 -34
  61. package/dist/client/DbCursor.d.ts +0 -14
  62. package/dist/client/DbCursor.js +0 -21
  63. package/dist/client/DbPoolConnection.d.ts +0 -5
  64. package/dist/client/DbPoolConnection.js +0 -74
  65. package/dist/client/DbPoolTransaction.d.ts +0 -9
  66. package/dist/client/DbPoolTransaction.js +0 -146
  67. package/dist/client/DbQuery.d.ts +0 -50
  68. package/dist/client/DbQuery.js +0 -42
  69. package/dist/client/DbQueryBase.d.ts +0 -38
  70. package/dist/client/DbQueryBase.js +0 -32
  71. package/dist/client/DbQueryPool.d.ts +0 -39
  72. package/dist/client/DbQueryPool.js +0 -56
  73. package/dist/client/_type.d.ts +0 -2
  74. package/dist/client/errors.d.ts +0 -9
  75. package/dist/client/errors.js +0 -14
  76. package/dist/client/interfaces.d.ts +0 -87
  77. package/dist/client/mod.d.ts +0 -9
  78. package/dist/client/mod.js +0 -7
@@ -1,8 +1,7 @@
1
- import { selectColumns } from '../util.js';
2
- import { whereToString, createUpdateSetFromObject } from '../_statement.js';
3
- import { SqlStatement, SqlTextStatementDataset } from '../SqlStatement.js';
4
-
5
- class InsertChain extends SqlStatement {
1
+ import { selectColumns } from "../util.js";
2
+ import { createUpdateSetFromObject, whereToString } from "../_statement.js";
3
+ import { SqlStatement, SqlTextStatementDataset } from "../SqlStatement.js";
4
+ export class InsertChain extends SqlStatement {
6
5
  sql;
7
6
  constructor(sql) {
8
7
  super();
@@ -95,5 +94,3 @@ class InsertAfterOnConflict {
95
94
  return this.sql;
96
95
  }
97
96
  }
98
-
99
- export { InsertChain };
@@ -7,4 +7,3 @@ export * from "./update_chain.ts";
7
7
  export * from "./delete.ts";
8
8
  export * from "./delete_chain.ts";
9
9
  export * from "./cte.ts";
10
- //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1,9 @@
1
+ export * from "./select.js";
2
+ export * from "./select_chain.js";
3
+ export * from "./insert.js";
4
+ export * from "./insert_chain.js";
5
+ export * from "./update.js";
6
+ export * from "./update_chain.js";
7
+ export * from "./delete.js";
8
+ export * from "./delete_chain.js";
9
+ export * from "./cte.js";
@@ -2,4 +2,3 @@ import { SelectSqlGenerator } from "./select_chain.ts";
2
2
  /** @public */
3
3
  export declare const select: SelectSqlGenerator;
4
4
  export { orderBy } from "./select_impl.ts";
5
- //# sourceMappingURL=select.d.ts.map
@@ -1,14 +1,11 @@
1
- import { selectColumns } from '../util.js';
2
- import { SelectChainAfterSelect } from './select_impl.js';
3
- export { orderBy } from './select_impl.js';
4
-
1
+ import { selectColumns } from "../util.js";
2
+ import { SelectChainAfterSelect } from "./select_impl.js";
5
3
  /** @public */
6
- const select = function select(columns) {
4
+ export const select = function select(columns) {
7
5
  if (!columns)
8
6
  return new SelectChainAfterSelect("SELECT ");
9
7
  if (typeof columns === "function")
10
8
  columns = columns();
11
9
  return new SelectChainAfterSelect("SELECT " + selectColumns(columns));
12
10
  };
13
-
14
- export { select };
11
+ export { orderBy } from "./select_impl.js";
@@ -111,4 +111,3 @@ export type OrderBehavior = {
111
111
  /** @public */
112
112
  export type OrderByParam = string | OrderBehavior | (string | OrderBehavior)[];
113
113
  export {};
114
- //# sourceMappingURL=select_chain.d.ts.map
@@ -0,0 +1 @@
1
+ export {};
@@ -28,4 +28,3 @@ export declare class SelectChainAfterSelect<T extends TableType> implements Chai
28
28
  * ```
29
29
  */
30
30
  export declare function orderBy(by?: Constructable<OrderByParam | void>): string;
31
- //# sourceMappingURL=select_impl.d.ts.map
@@ -1,7 +1,6 @@
1
- import { selectableToString, whereToString, condition } from '../_statement.js';
2
- import { SqlTextStatementDataset } from '../SqlStatement.js';
3
-
4
- class SelectChainAfterSelect {
1
+ import { condition, selectableToString, whereToString } from "../_statement.js";
2
+ import { SqlTextStatementDataset } from "../SqlStatement.js";
3
+ export class SelectChainAfterSelect {
5
4
  constructor(sql) {
6
5
  this.#sql = sql;
7
6
  }
@@ -107,7 +106,7 @@ function havingToString(conditions, type) {
107
106
  *
108
107
  * ```
109
108
  */
110
- function orderBy(by) {
109
+ export function orderBy(by) {
111
110
  if (typeof by === "function")
112
111
  by = by();
113
112
  let sql = "";
@@ -142,5 +141,3 @@ function handlerOrderValue(value) {
142
141
  }
143
142
  return value.key + " " + target;
144
143
  }
145
-
146
- export { SelectChainAfterSelect, orderBy };
@@ -1,4 +1,3 @@
1
1
  import { UpdateSqlGenerator } from "./update_chain.ts";
2
2
  /** @public */
3
3
  export declare const update: UpdateSqlGenerator;
4
- //# sourceMappingURL=update.d.ts.map
@@ -1,12 +1,9 @@
1
- import { UpdateChain } from './update_impl.js';
2
-
1
+ import { UpdateChain } from "./update_impl.js";
3
2
  /** @public */
4
- const update = function update(table, options) {
3
+ export const update = function update(table, options) {
5
4
  let sql = `UPDATE ${table}`;
6
5
  if (options?.as) {
7
6
  sql += ` AS ${options.as}`;
8
7
  }
9
8
  return new UpdateChain(sql);
10
9
  };
11
-
12
- export { update };
@@ -49,4 +49,3 @@ export interface ChainUpdateAfterForm extends ChainUpdateReturning {
49
49
  /** @public */
50
50
  export interface ChainUpdateReturning extends ChainModifyReturning {
51
51
  }
52
- //# sourceMappingURL=update_chain.d.ts.map
@@ -0,0 +1 @@
1
+ export {};
@@ -20,4 +20,3 @@ export declare class UpdateChain extends SqlStatement implements ChainUpdate {
20
20
  where(where: Constructable<ConditionParam | void>): ChainUpdateReturning;
21
21
  genSql(): string;
22
22
  }
23
- //# sourceMappingURL=update_impl.d.ts.map
@@ -1,8 +1,7 @@
1
- import { selectColumns } from '../util.js';
2
- import { SqlStatement, SqlTextStatementDataset } from '../SqlStatement.js';
3
- import { createUpdateSetFromObject, whereToString } from '../_statement.js';
4
-
5
- class UpdateChain extends SqlStatement {
1
+ import { selectColumns } from "../util.js";
2
+ import { SqlStatement, SqlTextStatementDataset } from "../SqlStatement.js";
3
+ import { createUpdateSetFromObject, whereToString } from "../_statement.js";
4
+ export class UpdateChain extends SqlStatement {
6
5
  sql;
7
6
  constructor(sql) {
8
7
  super();
@@ -68,5 +67,3 @@ class UpdateChain extends SqlStatement {
68
67
  return this.sql;
69
68
  }
70
69
  }
71
-
72
- export { UpdateChain };
@@ -34,4 +34,3 @@ export type UpdateRowValue<T extends object> = {
34
34
  export type TableType = {
35
35
  [key: string]: any;
36
36
  };
37
- //# sourceMappingURL=util.d.ts.map
@@ -6,7 +6,7 @@
6
6
  * selectColumns("c1,count(*) AS c2,column as c3") // "c1,count(*) AS c2,column as c3"
7
7
  * ```
8
8
  */
9
- function selectColumns(columns) {
9
+ export function selectColumns(columns) {
10
10
  if (typeof columns === "function")
11
11
  columns = columns();
12
12
  switch (typeof columns) {
@@ -45,5 +45,3 @@ function selectColumns(columns) {
45
45
  throw new TypeError("columns 应为 string 或 object 类型");
46
46
  }
47
47
  }
48
-
49
- export { selectColumns };
@@ -10,4 +10,3 @@ export declare class TypeChecker<T> {
10
10
  private checkArray;
11
11
  private checkItem;
12
12
  }
13
- //# sourceMappingURL=checker.d.ts.map
@@ -1,7 +1,6 @@
1
- import { CustomDbType } from './infer_db_type.js';
2
-
1
+ import { CustomDbType } from "./infer_db_type.js";
3
2
  /** @public */
4
- class TypeChecker {
3
+ export class TypeChecker {
5
4
  map;
6
5
  constructor(map) {
7
6
  this.map = map;
@@ -78,5 +77,3 @@ class TypeChecker {
78
77
  function getErrStr(expect, actual) {
79
78
  return `Expect ${expect}, Actual ${actual}`;
80
79
  }
81
-
82
- export { TypeChecker };
@@ -71,4 +71,3 @@ export type InferTableDefined<T extends TableDefined> = {
71
71
  [key in keyof T]: T[key] extends ColumnMeta<infer P> ? P : unknown;
72
72
  };
73
73
  export {};
74
- //# sourceMappingURL=infer_db_type.d.ts.map
@@ -2,7 +2,7 @@
2
2
  * 表格列的信息
3
3
  * @public
4
4
  */
5
- class ColumnMeta {
5
+ export class ColumnMeta {
6
6
  type;
7
7
  sqlType;
8
8
  notNull;
@@ -34,7 +34,7 @@ class ColumnMeta {
34
34
  * 数据库类型到JS类型的映射
35
35
  * @public
36
36
  */
37
- class YourTypeMap {
37
+ export class YourTypeMap {
38
38
  typeMap;
39
39
  static create(rawTypeMap) {
40
40
  return new this(rawTypeMap);
@@ -60,7 +60,7 @@ function baseType(v) {
60
60
  * 自定义数据类型
61
61
  * @public
62
62
  */
63
- class CustomDbType {
63
+ export class CustomDbType {
64
64
  is;
65
65
  name;
66
66
  static bigint = new CustomDbType(baseType, "bigint");
@@ -72,5 +72,3 @@ class CustomDbType {
72
72
  this.name = name;
73
73
  }
74
74
  }
75
-
76
- export { ColumnMeta, CustomDbType, YourTypeMap };
@@ -1,4 +1,3 @@
1
1
  export * from "./checker.ts";
2
2
  export * from "./infer_db_type.ts";
3
3
  export * from "./table.ts";
4
- //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1,3 @@
1
+ export * from "./checker.js";
2
+ export * from "./infer_db_type.js";
3
+ export * from "./table.js";
@@ -13,4 +13,3 @@ export declare class YourTable<T extends TableType = TableType> {
13
13
  getColumnMeta(name: keyof T): ColumnMeta<unknown>;
14
14
  createTypeChecker<T>(keys: readonly string[]): TypeChecker<T>;
15
15
  }
16
- //# sourceMappingURL=table.d.ts.map
@@ -1,10 +1,9 @@
1
- import { TypeChecker } from './checker.js';
2
-
1
+ import { TypeChecker } from "./checker.js";
3
2
  /**
4
3
  * 完整数据库表数据
5
4
  * @public
6
5
  */
7
- class YourTable {
6
+ export class YourTable {
8
7
  name;
9
8
  define;
10
9
  constructor(name, define) {
@@ -29,5 +28,3 @@ class YourTable {
29
28
  return new TypeChecker(map);
30
29
  }
31
30
  }
32
-
33
- export { YourTable };
package/package.json CHANGED
@@ -1,54 +1,51 @@
1
1
  {
2
2
  "name": "@asla/yoursql",
3
- "version": "0.13.1",
3
+ "version": "0.14.0",
4
4
  "description": "",
5
- "type": "module",
6
- "types": "./dist/mod.d.ts",
7
- "packageManager": "pnpm@10.4.0",
8
- "scripts": {
9
- "ci:test": "vitest run",
10
- "ci:build": "pnpm rollup -c build/rollup.config.js",
11
- "build": "pnpm rollup -c build/rollup.config.js && pnpm run ci:check-api -l",
12
- "test": "vitest",
13
- "type:check": "tsc",
14
- "publish-check": "deno publish --dry-run --allow-dirty",
15
- "ci:check-api": "api-extractor run -c api-extractor.jsonc -v"
16
- },
17
- "devDependencies": {
18
- "@microsoft/api-extractor": "^7.47.9",
19
- "@rollup/plugin-typescript": "^12.1.0",
20
- "prettier": "^3.5.3",
21
- "rollup": "^4.22.4",
22
- "tslib": "^2.7.0",
23
- "typescript": "^5.6.2",
24
- "vitest": "^2.1.1"
5
+ "keywords": [
6
+ "sql"
7
+ ],
8
+ "license": "MIT",
9
+ "author": {
10
+ "name": "Eavid",
11
+ "url": "https://github.com/eavidy"
25
12
  },
26
13
  "repository": {
27
14
  "type": "git",
28
15
  "url": "https://github.com/asnowc/yoursql"
29
16
  },
30
- "exports": {
31
- ".": "./dist/sql_gen/mod.js",
32
- "./client": "./dist/client/mod.js"
33
- },
34
17
  "files": [
35
18
  "dist/**/*.js",
36
19
  "dist/**/*d.ts"
37
20
  ],
21
+ "type": "module",
22
+ "types": "./dist/mod.d.ts",
23
+ "exports": {
24
+ ".": "./dist/sql_gen/mod.js"
25
+ },
38
26
  "publishConfig": {
39
27
  "access": "public",
40
- "registry": "https://registry.npmjs.org",
41
- "provenance": true
28
+ "provenance": true,
29
+ "registry": "https://registry.npmjs.org"
42
30
  },
43
- "keywords": [
44
- "sql"
45
- ],
46
- "author": {
47
- "name": "Eavid",
48
- "url": "https://github.com/eavidy"
31
+ "scripts": {
32
+ "ci:test": "vitest run",
33
+ "ci:build": "tsc -p tsconfig.lib.json",
34
+ "build": "tsc -p tsconfig.lib.json",
35
+ "test": "vitest",
36
+ "type:check": "tsc -p tsconfig.lib.json --noEmit && tsc -p tsconfig.node.json --noEmit",
37
+ "publish-check": "deno publish --dry-run --allow-dirty"
38
+ },
39
+ "devDependencies": {
40
+ "@types/node": "^25.6.2",
41
+ "oxfmt": "^0.48.0",
42
+ "tslib": "^2.8.1",
43
+ "typescript": "^6.0.3",
44
+ "vite": "^8.0.11",
45
+ "vitest": "^4.1.5"
49
46
  },
50
47
  "engines": {
51
48
  "node": ">=18"
52
49
  },
53
- "license": "MIT"
50
+ "packageManager": "pnpm@11.0.9"
54
51
  }
@@ -1,14 +0,0 @@
1
- /** @public */
2
- export interface DbCursorOption {
3
- defaultSize?: number;
4
- }
5
- /** @public */
6
- export declare abstract class DbCursor<T> implements AsyncDisposable, AsyncIterable<T> {
7
- /** 读取游标,如果读取结束,返回空数组 */
8
- abstract read(maxSize?: number): Promise<T[]>;
9
- /** 提前关闭游标,如果多次调用,会被忽略 */
10
- abstract close(): Promise<void>;
11
- [Symbol.asyncDispose](): Promise<void>;
12
- [Symbol.asyncIterator](): AsyncGenerator<T, undefined, void>;
13
- }
14
- //# sourceMappingURL=DbCursor.d.ts.map
@@ -1,21 +0,0 @@
1
- /** @public */
2
- class DbCursor {
3
- // implement
4
- [Symbol.asyncDispose]() {
5
- return this.close();
6
- }
7
- async *[Symbol.asyncIterator]() {
8
- let data = await this.read();
9
- try {
10
- while (data.length) {
11
- yield* data;
12
- data = await this.read();
13
- }
14
- }
15
- finally {
16
- await this.close();
17
- }
18
- }
19
- }
20
-
21
- export { DbCursor };
@@ -1,5 +0,0 @@
1
- import { type DbQueryBase } from "./DbQueryBase.ts";
2
- import type { DbPoolConnection } from "./interfaces.ts";
3
- /** @public */
4
- export declare function createDbPoolConnection(conn: DbQueryBase, onRelease: (conn: DbQueryBase) => void, onDispose?: (conn: DbQueryBase) => void): DbPoolConnection;
5
- //# sourceMappingURL=DbPoolConnection.d.ts.map
@@ -1,74 +0,0 @@
1
- import { DbQuery } from './DbQuery.js';
2
- import { ConnectionNotAvailableError } from './errors.js';
3
-
4
- class DbPoolConnectionImpl extends DbQuery {
5
- constructor(conn, onRelease, onDispose = onRelease) {
6
- super();
7
- this.#conn = conn;
8
- this.#onRelease = onRelease;
9
- this.#onDispose = onDispose;
10
- }
11
- #onRelease;
12
- #onDispose;
13
- //implement
14
- async begin(mode) {
15
- await this.execute("BEGIN" + (mode ? " TRANSACTION ISOLATION LEVEL " + mode : ""));
16
- }
17
- #conn;
18
- query(sql) {
19
- if (!this.#conn)
20
- return Promise.reject(new ConnectionNotAvailableError("Connection already release"));
21
- if (typeof sql === "function")
22
- sql = sql();
23
- return this.#conn.query(sql);
24
- }
25
- execute(sql) {
26
- if (!this.#conn)
27
- return Promise.reject(new ConnectionNotAvailableError("Connection already release"));
28
- if (typeof sql === "function")
29
- sql = sql();
30
- return this.#conn.execute(sql);
31
- }
32
- /** @deprecated 不建议使用 */
33
- multipleQuery(sql) {
34
- if (!this.#conn)
35
- return Promise.reject(new ConnectionNotAvailableError("Connection already release"));
36
- return this.#conn.multipleQuery(sql);
37
- }
38
- //implement
39
- async rollback() {
40
- await this.execute("ROLLBACK");
41
- }
42
- //implement
43
- async commit() {
44
- await this.execute("COMMIT");
45
- }
46
- get released() {
47
- return !this.#conn;
48
- }
49
- /** 调用 release() 时,如果事务未提交,则抛出异常 */
50
- release() {
51
- const conn = this.#conn;
52
- if (conn) {
53
- this.#conn = undefined;
54
- this.#onRelease(conn);
55
- }
56
- }
57
- dispose() {
58
- const conn = this.#conn;
59
- if (conn) {
60
- this.#conn = undefined;
61
- this.#onDispose(conn);
62
- }
63
- }
64
- //implement
65
- [Symbol.dispose]() {
66
- return this.release();
67
- }
68
- }
69
- /** @public */
70
- function createDbPoolConnection(conn, onRelease, onDispose = onRelease) {
71
- return new DbPoolConnectionImpl(conn, onRelease, onDispose);
72
- }
73
-
74
- export { createDbPoolConnection };
@@ -1,9 +0,0 @@
1
- import type { DbPoolConnection, DbPoolTransaction, TransactionMode } from "./interfaces.ts";
2
- /** @public */
3
- export type DbPoolTransactionOption = {
4
- errorRollback?: boolean;
5
- mode?: TransactionMode;
6
- };
7
- /** @public */
8
- export declare function createDbPoolTransaction(connect: () => Promise<DbPoolConnection>, option?: TransactionMode | DbPoolTransactionOption): DbPoolTransaction;
9
- //# sourceMappingURL=DbPoolTransaction.d.ts.map
@@ -1,146 +0,0 @@
1
- import { DbQuery } from './DbQuery.js';
2
- import { ParallelQueryError, ConnectionNotAvailableError } from './errors.js';
3
-
4
- class DbPoolTransactionImpl extends DbQuery {
5
- #errorRollback;
6
- mode;
7
- #begin;
8
- constructor(connect, option) {
9
- super();
10
- if (option) {
11
- if (typeof option === "string")
12
- this.mode = option;
13
- else {
14
- this.mode = option.mode;
15
- this.#errorRollback = option.errorRollback;
16
- }
17
- }
18
- this.#begin = "BEGIN" + (this.mode ? " TRANSACTION ISOLATION LEVEL " + this.mode : "");
19
- this.#connect = connect;
20
- }
21
- #connect;
22
- #conn;
23
- async commit() {
24
- if (this.#conn) {
25
- const conn = this.#conn;
26
- const promise = conn.execute("COMMIT").then(() => conn.release(), (e) => conn.dispose());
27
- return promise;
28
- }
29
- this.#release(undefined);
30
- }
31
- async rollback() {
32
- if (this.#conn) {
33
- const conn = this.#conn;
34
- const promise = conn.execute("ROLLBACK").then(() => conn.release(), (e) => conn.dispose());
35
- return promise;
36
- }
37
- this.#release(undefined);
38
- }
39
- savePoint(savePoint) {
40
- return this.execute("SAVEPOINT " + savePoint);
41
- }
42
- rollbackTo(savePoint) {
43
- return this.execute("ROLLBACK TO " + savePoint);
44
- }
45
- #pending;
46
- #getConnQuery(call, callIfFirst = call, queryErrorCatch) {
47
- if (this.#pending) {
48
- return Promise.reject(new ParallelQueryError());
49
- }
50
- if (this.#error) {
51
- return Promise.reject(this.#error);
52
- }
53
- let promise;
54
- if (!this.#conn) {
55
- promise = this.#connect().then((conn) => {
56
- if (this.released) {
57
- conn.release();
58
- throw this.#error;
59
- }
60
- this.#conn = conn;
61
- let promise = callIfFirst(conn);
62
- if (queryErrorCatch) {
63
- promise = promise.catch(queryErrorCatch);
64
- }
65
- return promise;
66
- }, (e) => {
67
- this.#release(undefined);
68
- throw e;
69
- });
70
- }
71
- else {
72
- promise = call(this.#conn);
73
- if (queryErrorCatch) {
74
- promise = promise.catch(queryErrorCatch);
75
- }
76
- }
77
- this.#pending = promise;
78
- return promise.finally(() => {
79
- this.#pending = undefined;
80
- });
81
- }
82
- #query(call, callIfFirst) {
83
- const onError = (e) => {
84
- if (this.#errorRollback) {
85
- const passError = () => {
86
- throw e;
87
- };
88
- return this.rollback().then(passError, passError);
89
- }
90
- else {
91
- this.#release(this.#conn);
92
- throw e;
93
- }
94
- };
95
- return this.#getConnQuery(call, callIfFirst, onError);
96
- }
97
- query(sql) {
98
- return this.#query((conn) => {
99
- return conn.query(sql);
100
- }, async (conn) => {
101
- if (typeof sql === "function")
102
- sql = sql();
103
- const isArray = sql instanceof Array;
104
- const result = await conn.query(isArray ? [this.#begin, ...sql] : [this.#begin, sql]);
105
- if (isArray)
106
- return result.slice(1);
107
- else
108
- return result[1];
109
- });
110
- }
111
- async execute(sql) {
112
- return this.#query((conn) => {
113
- return conn.execute(sql);
114
- }, (conn) => {
115
- if (typeof sql === "function")
116
- sql = sql();
117
- return conn.execute(sql instanceof Array ? [this.#begin, ...sql] : [this.#begin, sql]);
118
- });
119
- }
120
- /** @deprecated 不建议使用 */
121
- multipleQuery(sql) {
122
- return this.#query((conn) => {
123
- return conn.multipleQuery(sql);
124
- }, (conn) => {
125
- return conn.multipleQuery(sql instanceof Array ? [this.#begin, ...sql] : [this.#begin, sql]);
126
- });
127
- }
128
- #error;
129
- #release(conn, error = new ConnectionNotAvailableError("Connection already release")) {
130
- this.#error = error;
131
- this.#conn = undefined;
132
- conn?.release();
133
- }
134
- get released() {
135
- return !!this.#error;
136
- }
137
- [Symbol.asyncDispose]() {
138
- return this.rollback();
139
- }
140
- }
141
- /** @public */
142
- function createDbPoolTransaction(connect, option) {
143
- return new DbPoolTransactionImpl(connect, option);
144
- }
145
-
146
- export { createDbPoolTransaction };