@asla/yoursql 0.11.1 → 0.11.2

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,6 @@
1
1
  import { SqlStatementDataset } from "./_type.ts";
2
2
  import { DbQuery } from "./DbQuery.ts";
3
- import type { MultipleQueryResult, QueryRowsResult, DbQueryBase } from "./DbQueryBase.ts";
3
+ import { type MultipleQueryResult, type QueryRowsResult, type DbQueryBase } from "./DbQueryBase.ts";
4
4
  import type { SqlLike, TransactionMode } from "./interfaces.ts";
5
5
  /**
6
6
 
@@ -1,11 +1,12 @@
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
4
  /**
5
5
  * SQL 查询相关操作
6
6
  * @public
7
7
  */
8
- export declare abstract class DbQuery implements DbQueryBase {
8
+ export declare abstract class DbQuery {
9
+ abstract query<T = any>(sql: SqlStatementDataset<T>): Promise<QueryRowsResult<T>>;
9
10
  abstract query<T = any>(sql: SqlLike): Promise<QueryRowsResult<T>>;
10
11
  abstract multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlLike | SqlLike[]): Promise<T>;
11
12
  /** 单语句查询受影响的行 */
@@ -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,28 @@ export interface QueryRowsResult<T = any> extends SingleQueryResult {
12
12
  }
13
13
  /** @public */
14
14
  export type MultipleQueryResult = SingleQueryResult[];
15
- /** @public */
15
+ /**
16
+ *
17
+ * @deprecated 已废弃
18
+ * @public
19
+ */
16
20
  export type QueryResult = MultipleQueryResult | SingleQueryResult;
17
- /** @public */
21
+ /**
22
+ * 数据库客户端的最小实现接口
23
+ * @public
24
+ */
18
25
  export interface DbQueryBase {
19
- /** 单语句查询,不应查询多语句,否则返回错误值 */
20
- query<T = any>(sql: SqlStatementDataset<T>): Promise<QueryRowsResult<T>>;
21
26
  /** 单语句查询,不应查询多语句,否则返回错误值 */
22
27
  query<T = any>(sql: SqlLike): Promise<QueryRowsResult<T>>;
23
28
  /** 多语句查询 */
24
29
  multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlLike | SqlLike[]): Promise<T>;
25
30
  }
26
- /** @public */
31
+ /**
32
+ * 将 SqlLike 转换为字符串
33
+ * @public
34
+ *
35
+ */
27
36
  export declare function sqlLikeToString(sqlLike: SqlLike): string;
37
+ /** @public */
38
+ export declare function isSqlTemplate(obj: any): obj is SqlTemplate;
28
39
  //# 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 };
@@ -1,2 +1,2 @@
1
- export type { SqlStatementDataset } from "../sql_gen/mod.ts";
1
+ export type { SqlStatementDataset, SqlTemplate, SqlTextTemplate } 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
@@ -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
@@ -1,5 +1,5 @@
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';
@@ -44,13 +44,14 @@ export declare class SqlTextStatementDataset<T> extends SqlStatementDataset<T> {
44
44
  */
45
45
  export type InferQueryResult<T> = T extends SqlStatementDataset<infer P> ? P : never;
46
46
  /** @public */
47
- export interface SqlTemplate {
47
+ export interface SqlTemplate<T extends readonly any[] = readonly unknown[]> {
48
48
  readonly templates: readonly string[];
49
- readonly args: readonly unknown[];
50
- toTextTemplate(): {
51
- text: string;
52
- args: string[];
53
- };
54
- genSql(): string;
49
+ readonly args: T;
50
+ toTextArgs(): string[];
51
+ }
52
+ /** @public */
53
+ export interface SqlTextTemplate {
54
+ readonly textTemplate: string;
55
+ readonly textArgs: readonly string[];
55
56
  }
56
57
  //# sourceMappingURL=SqlStatement.d.ts.map
@@ -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.2",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",