@asla/yoursql 0.10.1 → 0.11.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 (37) hide show
  1. package/dist/client/DbPoolConnection.d.ts +1 -1
  2. package/dist/client/DbPoolTransaction.d.ts +1 -1
  3. package/dist/client/DbQuery.d.ts +1 -1
  4. package/dist/client/DbQueryBase.d.ts +1 -1
  5. package/dist/client/_type.d.ts +2 -0
  6. package/dist/client/interfaces.d.ts +1 -1
  7. package/dist/sql_gen/_statement.d.ts +1 -1
  8. package/dist/sql_gen/_statement.js +1 -1
  9. package/dist/sql_gen/mod.js +3 -1
  10. package/dist/sql_gen/sql_value/sql_value.d.ts +25 -41
  11. package/dist/sql_gen/sql_value/sql_value.js +70 -74
  12. package/dist/sql_gen/sql_value/type.d.ts +8 -9
  13. package/dist/sql_gen/statement/cte.d.ts +8 -0
  14. package/dist/sql_gen/statement/cte.js +33 -3
  15. package/dist/sql_gen/statement/delete.d.ts +2 -13
  16. package/dist/sql_gen/statement/delete.js +4 -38
  17. package/dist/sql_gen/statement/delete_chain.d.ts +15 -0
  18. package/dist/sql_gen/statement/delete_impl.d.ts +12 -0
  19. package/dist/sql_gen/statement/delete_impl.js +31 -0
  20. package/dist/sql_gen/statement/insert.d.ts +2 -4
  21. package/dist/sql_gen/statement/insert.js +4 -98
  22. package/dist/sql_gen/statement/insert_chain.d.ts +5 -0
  23. package/dist/sql_gen/statement/insert_impl.d.ts +14 -0
  24. package/dist/sql_gen/statement/insert_impl.js +99 -0
  25. package/dist/sql_gen/statement/select.d.ts +3 -36
  26. package/dist/sql_gen/statement/select.js +6 -146
  27. package/dist/sql_gen/statement/select_chain.d.ts +12 -0
  28. package/dist/sql_gen/statement/select_impl.d.ts +31 -0
  29. package/dist/sql_gen/statement/select_impl.js +146 -0
  30. package/dist/sql_gen/statement/update.d.ts +2 -15
  31. package/dist/sql_gen/statement/update.js +4 -80
  32. package/dist/sql_gen/statement/update_chain.d.ts +16 -0
  33. package/dist/sql_gen/statement/update_impl.d.ts +23 -0
  34. package/dist/sql_gen/statement/update_impl.js +72 -0
  35. package/package.json +1 -1
  36. package/dist/sql_gen/sql_value/_utils.d.ts +0 -11
  37. package/dist/sql_gen/sql_value/_utils.js +0 -37
@@ -1,4 +1,4 @@
1
- import type { SqlStatementDataset } from "../sql_gen/mod.ts";
1
+ import { SqlStatementDataset } from "./_type.ts";
2
2
  import { DbQuery } from "./DbQuery.ts";
3
3
  import type { MultipleQueryResult, QueryRowsResult, DbQueryBase } from "./DbQueryBase.ts";
4
4
  import type { SqlLike, TransactionMode } from "./interfaces.ts";
@@ -1,8 +1,8 @@
1
- import type { SqlStatementDataset } from "../sql_gen/mod.ts";
2
1
  import { DbQuery } from "./DbQuery.ts";
3
2
  import type { MultipleQueryResult, QueryRowsResult } from "./DbQueryBase.ts";
4
3
  import type { DbPoolConnection } from "./DbPoolConnection.ts";
5
4
  import type { DbTransaction, SqlLike, TransactionMode } from "./interfaces.ts";
5
+ import { SqlStatementDataset } from "./_type.ts";
6
6
  /** @public */
7
7
  export type DbPoolTransactionOption = {
8
8
  errorRollback?: boolean;
@@ -1,6 +1,6 @@
1
- import type { SqlStatementDataset } from "../sql_gen/mod.ts";
2
1
  import { SqlLike } from "./interfaces.ts";
3
2
  import { MultipleQueryResult, DbQueryBase, QueryRowsResult } from "./DbQueryBase.ts";
3
+ import { SqlStatementDataset } from "./_type.ts";
4
4
  /**
5
5
  * SQL 查询相关操作
6
6
  * @public
@@ -1,4 +1,4 @@
1
- import type { SqlStatementDataset } from "../sql_gen/mod.ts";
1
+ import { SqlStatementDataset } from "./_type.ts";
2
2
  import { SqlLike } from "./interfaces.ts";
3
3
  /** @public */
4
4
  export interface SingleQueryResult {
@@ -0,0 +1,2 @@
1
+ export type { SqlStatementDataset } from "../sql_gen/mod.ts";
2
+ //# sourceMappingURL=_type.d.ts.map
@@ -1,7 +1,7 @@
1
- import type { SqlStatementDataset } from "../sql_gen/mod.ts";
2
1
  import { DbQuery } from "./DbQuery.ts";
3
2
  import { DbPoolConnection } from "./DbPoolConnection.ts";
4
3
  import { DbCursor, DbCursorOption } from "./DbCursor.ts";
4
+ import { SqlStatementDataset } from "./_type.ts";
5
5
  /**
6
6
  * 数据库连接
7
7
  * @public
@@ -4,7 +4,7 @@ import { SqlSelectable } from "./SqlStatement.ts";
4
4
  * 获取对象数组中的 key 的集合
5
5
  * @param keepUndefinedKey - 是否保留值为 undefined 的 key
6
6
  */
7
- export declare function getObjectListKeys(objectList: any[], keepUndefinedKey?: boolean): Set<string>;
7
+ export declare function getObjectListKeys(objectList: readonly any[], keepUndefinedKey?: boolean): Set<string>;
8
8
  /**
9
9
  * 生成条件语句
10
10
  */
@@ -14,7 +14,7 @@ function getObjectListKeys(objectList, keepUndefinedKey) {
14
14
  k = hasKeys[j];
15
15
  if (typeof k !== "string")
16
16
  continue;
17
- if (!keepUndefinedKey && obj[k] === undefined)
17
+ if (obj[k] === undefined)
18
18
  continue;
19
19
  keys.add(k);
20
20
  }
@@ -1,7 +1,8 @@
1
1
  import { pgSqlTransformer } from './sql_value/db_type.js';
2
2
  import { SqlValuesCreator } from './sql_value/sql_value.js';
3
+ export { SqlExplicitValuesStatement } from './sql_value/sql_value.js';
3
4
  export { SqlStatement, SqlStatementDataset, SqlTextStatementDataset } from './SqlStatement.js';
4
- export { orderBy, select } from './statement/select.js';
5
+ export { select } from './statement/select.js';
5
6
  export { insertInto } from './statement/insert.js';
6
7
  export { update } from './statement/update.js';
7
8
  export { deleteFrom } from './statement/delete.js';
@@ -10,6 +11,7 @@ export { selectColumns } from './util.js';
10
11
  export { TypeChecker } from './your_table/checker.js';
11
12
  export { ColumnMeta, CustomDbType, YourTypeMap } from './your_table/infer_db_type.js';
12
13
  export { YourTable } from './your_table/table.js';
14
+ export { orderBy } from './statement/select_impl.js';
13
15
 
14
16
  /**
15
17
  * 默认的 SqlValuesCreator 实列
@@ -1,5 +1,5 @@
1
- import { SqlStatementDataset, SqlTemplate } from "../SqlStatement.ts";
2
- import { AssertJsType, ObjectToValueKeys, SqlValuesTextData } from "./type.ts";
1
+ import { SqlTemplate } from "../SqlStatement.ts";
2
+ import { AssertJsType, ObjectToValueKeys } from "./type.ts";
3
3
  /** @public js 对象到编码函数的映射*/
4
4
  export type JsObjectMapSql = Map<new (...args: any[]) => any, SqlValueEncoder>;
5
5
  /** @public 将 js 值转为 SQl 字符串的函数*/
@@ -45,33 +45,22 @@ export declare class SqlValuesCreator {
45
45
  /** 获取值对应已定义的类 */
46
46
  getClassType(value: object): undefined | (new (...args: unknown[]) => unknown);
47
47
  protected defaultObject(value: object): string;
48
- /** @deprecated 改用 objectListToValues */
49
- objectListToValuesList<T extends object>(objectList: T[], keys?: ObjectToValueKeys<T>, keepUndefinedKey?: boolean): string;
50
48
  /**
51
- * 将对象列表转为 SQL 的 VALUES。与 objectToValues类似。如果设定了 keys 的类型,只会转换第一行的数据。
49
+ * 将对象列表转为 SQL 的 VALUES。如果 objectList 中有某个对象的属性值为 undefined,则会被转换为 "DEFAULT"
52
50
  * @example 返回的文本示例: " (...),(...) "
53
- * @param keys - 选择的键。如果指定了 keys, 值为 undefined 的属性将自动填充为 null; 如果未指定 keys,将选择 objectList 所有不是 undefined 项的键的并集
54
- * @param keepUndefinedKey - 是否保留 undefined 的键。默认值为 false,如果为 true , 数组的某一个字段均为 undefined时,将忽略字段,
51
+ * @param columns - 选择的键。如果指定了 columns, 值为 undefined 的属性将自动填充为 null; 如果未指定 columns,将选择 objectList 所有不是 undefined 项的键的并集
55
52
  */
56
- objectListToValues<T extends object>(objectList: T[], keys?: ObjectToValueKeys<T>, keepUndefinedKey?: boolean): SqlValuesTextData;
53
+ createImplicitValues<T extends object>(objectList: T, columns?: ObjectToValueKeys<T>): SqlValuesTextData;
54
+ createImplicitValues<T extends object>(objectList: T[], columns?: ObjectToValueKeys<T>): SqlValuesTextData;
57
55
  /**
58
- * @deprecated 请使用 objectToValue 代替
59
- */
60
- objectToValues<T extends object>(object: T, keys?: ObjectToValueKeys<T>): string;
61
- /**
62
- * 将对象转为 SQL 的 value
63
- * @example
64
- * ```ts
65
- * v.objectToValue({ a: 1, b: "2", c: null, d: undefined }).text // "('1', '2', NULL, DEFAULT)"
66
- * v.objectToValue({ a: 1, b: "2", c: null, d: undefined }, ["a", "b"]).text // "('1', '2')"
67
- * v.objectToValue(
68
- * { a: 1, b: "2", c: null, d: undefined },
69
- * { a: "INT", b: "TEXT", c: "JSONB", d: "TEXT" }
70
- * ).text // "('1'::INT, '2'::TEXT, NULL::JSONB, DEFAULT::TEXT)"
71
- * ```
72
- * @param keys - 如果指定了key, object undefined 的属性值将填充为 null,如果不指定,将自获取 object 所有非 undefined 的属性的key
56
+ * 将对象列表转为 SQL VALUES。如果 objectList 中有某个对象的属性值为 undefined,则会被转换为 "NULL"
57
+ * @example 返回的文本示例: " (...),(...) "
58
+ * @param columns - 选择的键。
73
59
  */
74
- objectToValue<T extends object>(object: T, keys?: ObjectToValueKeys<T>): SqlValuesTextData;
60
+ createExplicitValues<T extends object>(objectList: T, columns?: ObjectToValueKeys<T>): SqlExplicitValuesStatement;
61
+ createExplicitValues<T extends object>(objectList: T[], columns?: ObjectToValueKeys<T>): SqlExplicitValuesStatement;
62
+ private _objectListToValues;
63
+ private _objectToValue;
75
64
  private _getObjectValueInfo;
76
65
  private _internalObjectToValues;
77
66
  /**
@@ -82,22 +71,17 @@ export declare class SqlValuesCreator {
82
71
  * ```
83
72
  */
84
73
  toValues(values: readonly any[]): string;
85
- /**
86
- * @public 创建 VALUES AS 语句
87
- * @example
88
- * ```ts
89
- * sqlValue.createValues(
90
- * "customName",
91
- * [{age:8, name:"hhh"}, {age:9, name:"row2"}],
92
- * {age:"INT", name:"TEXT"}
93
- * )
94
- * // (VALUES (8:INT,'hhh':TEXT),(9,'row2')) AS customName(age, name)
95
- * ```
96
- */
97
- createValues<T extends {}>(asName: string, values: T[], valuesTypes: Record<string, string | {
98
- sqlType: string;
99
- sqlDefault?: string;
100
- assertJsType?: AssertJsType;
101
- }>): SqlStatementDataset<T>;
74
+ }
75
+ /** @public */
76
+ export type SqlValuesTextData = {
77
+ columns: string[];
78
+ text: string;
79
+ };
80
+ /** @public */
81
+ export declare class SqlExplicitValuesStatement {
82
+ columns: readonly string[];
83
+ readonly text: string;
84
+ constructor(columns: readonly string[], text: string);
85
+ toSelect(name: string): string;
102
86
  }
103
87
  //# sourceMappingURL=sql_value.d.ts.map
@@ -1,6 +1,5 @@
1
1
  import { getObjectListKeys } from '../_statement.js';
2
2
  import { ValueSqlTemplate } from './ValueSqlTemplate.js';
3
- import { initColumnAssert, YourValuesAs } from './_utils.js';
4
3
 
5
4
  /**
6
5
  * SQL value 生成器
@@ -115,32 +114,48 @@ class SqlValuesCreator {
115
114
  defaultObject(value) {
116
115
  return SqlValuesCreator.string(JSON.stringify(value));
117
116
  }
118
- /** @deprecated 改用 objectListToValues */
119
- objectListToValuesList(objectList, keys, keepUndefinedKey) {
120
- return this.objectListToValues(objectList, keys, keepUndefinedKey).text;
117
+ createImplicitValues(objectList, columns) {
118
+ let res;
119
+ if (objectList instanceof Array) {
120
+ res = this._objectListToValues(objectList, columns, { undefinedDefault: "DEFAULT" });
121
+ }
122
+ else {
123
+ res = this._objectToValue(objectList, columns, { undefinedDefault: "DEFAULT" });
124
+ }
125
+ return { columns: [...res.columns], text: res.text };
126
+ }
127
+ createExplicitValues(objectList, columns) {
128
+ if (objectList instanceof Array) {
129
+ return this._objectListToValues(objectList, columns, { undefinedDefault: "NULL" });
130
+ }
131
+ else {
132
+ return this._objectToValue(objectList, columns, { undefinedDefault: "NULL" });
133
+ }
121
134
  }
122
- objectListToValues(objectList, keys_types, keepUndefinedKey) {
135
+ _objectListToValues(objectList, columns, option = {}) {
123
136
  if (objectList.length <= 0)
124
137
  throw new Error("objectList 不能是空数组");
125
138
  let keys;
126
139
  let asserts;
127
- if (!keys_types) {
128
- keys = Array.from(getObjectListKeys(objectList, keepUndefinedKey));
140
+ if (!columns) {
141
+ keys = Array.from(getObjectListKeys(objectList));
129
142
  asserts = [];
130
143
  }
131
- else if (keys_types instanceof Array) {
132
- keys = keys_types;
144
+ else if (columns instanceof Array) {
145
+ keys = [...columns];
133
146
  asserts = [];
134
147
  }
135
148
  else {
136
- keys = Object.keys(keys_types);
137
- asserts = initColumnAssert(keys, keys_types);
149
+ keys = Object.keys(columns);
150
+ asserts = initColumnAssert(keys, columns);
138
151
  }
139
- let str = "(" + this._internalObjectToValues(objectList[0], keys, asserts) + ")";
152
+ const undefinedDefault = option.undefinedDefault || "DEFAULT";
153
+ let str = "(" + this._internalObjectToValues(objectList[0], keys, asserts, undefinedDefault) + ")";
140
154
  let i = 1;
141
155
  let j;
142
156
  let value;
143
157
  let rows;
158
+ let assert;
144
159
  try {
145
160
  for (; i < objectList.length; i++) {
146
161
  const object = objectList[i];
@@ -148,7 +163,9 @@ class SqlValuesCreator {
148
163
  j = 0;
149
164
  for (; j < keys.length; j++) {
150
165
  value = object[keys[j]];
151
- rows[j] = this.toSqlStr(value, asserts[j]?.assertJsType);
166
+ assert = asserts[j];
167
+ rows[j] =
168
+ value === undefined ? assert?.sqlDefault || undefinedDefault : this.toSqlStr(value, assert?.assertJsType);
152
169
  }
153
170
  str += ",\n(" + rows.join(",") + ")";
154
171
  }
@@ -157,16 +174,13 @@ class SqlValuesCreator {
157
174
  let message = error instanceof Error ? error.message : String(error);
158
175
  throw new Error("第 " + i + " 项,字段 '" + keys[j] + "' 异常," + message);
159
176
  }
160
- return { columns: keys, text: str };
177
+ return new SqlExplicitValuesStatement(keys, str);
161
178
  }
162
- objectToValues(object, keys_types) {
179
+ _objectToValue(object, keys_types, option = {}) {
163
180
  const { keys, type } = this._getObjectValueInfo(object, keys_types);
164
- return this._internalObjectToValues(object, keys, type);
165
- }
166
- objectToValue(object, keys_types) {
167
- const { keys, type } = this._getObjectValueInfo(object, keys_types);
168
- const text = this._internalObjectToValues(object, keys, type);
169
- return { columns: keys, text: "(" + text + ")" };
181
+ const undefinedDefault = option.undefinedDefault || "DEFAULT";
182
+ const text = this._internalObjectToValues(object, keys, type, undefinedDefault);
183
+ return new SqlExplicitValuesStatement(keys, `(${text})`);
170
184
  }
171
185
  _getObjectValueInfo(object, keys_types) {
172
186
  let type;
@@ -185,7 +199,7 @@ class SqlValuesCreator {
185
199
  }
186
200
  return { keys, type };
187
201
  }
188
- _internalObjectToValues(object, keys, type) {
202
+ _internalObjectToValues(object, keys, type, undefinedDefault) {
189
203
  const values = [];
190
204
  let i = 0;
191
205
  let key;
@@ -197,12 +211,15 @@ class SqlValuesCreator {
197
211
  value = object[key];
198
212
  assertType = type[i];
199
213
  if (assertType) {
200
- values[i] = this.toSqlStr(value, assertType.assertJsType);
214
+ values[i] =
215
+ value === undefined
216
+ ? assertType.sqlDefault || undefinedDefault
217
+ : this.toSqlStr(value, assertType.assertJsType);
201
218
  if (assertType.sqlType)
202
219
  values[i] += "::" + assertType.sqlType;
203
220
  }
204
221
  else
205
- values[i] = this.toSqlStr(value);
222
+ values[i] = value === undefined ? undefinedDefault : this.toSqlStr(value);
206
223
  }
207
224
  }
208
225
  catch (error) {
@@ -225,60 +242,39 @@ class SqlValuesCreator {
225
242
  throw new Error("values 不能为空");
226
243
  return values.map((v) => this.toSqlStr(v)).join(",");
227
244
  }
228
- createValues(asName, values, valuesTypes) {
229
- if (values.length === 0)
230
- throw new Error("values 不能为空");
231
- const insertKeys = Object.keys(valuesTypes);
232
- const defaultValues = [];
233
- const asserts = new Array(insertKeys.length);
234
- const valuesStr = new Array(values.length);
235
- {
236
- const column0 = new Array(insertKeys.length);
237
- let columnName;
238
- let item;
239
- let sqlType;
240
- let assertJsType;
241
- let value;
242
- for (let i = 0; i < insertKeys.length; i++) {
243
- columnName = insertKeys[i];
244
- item = valuesTypes[columnName];
245
- if (typeof item === "string") {
246
- sqlType = item;
247
- defaultValues[i] = "NULL";
248
- }
249
- else {
250
- sqlType = item.sqlType;
251
- assertJsType = item.assertJsType;
252
- asserts[i] = assertJsType;
253
- defaultValues[i] = item.sqlDefault ?? "NULL";
254
- }
255
- value = values[0][columnName];
256
- if (value === undefined)
257
- column0[i] = defaultValues[i] + "::" + sqlType;
258
- else
259
- column0[i] = this.toSqlStr(value, assertJsType) + "::" + sqlType;
260
- }
261
- valuesStr[0] = "(" + column0.join(",") + ")";
262
- }
263
- let items = new Array(insertKeys.length);
264
- let value;
265
- for (let i = 1; i < values.length; i++) {
266
- for (let j = 0; j < insertKeys.length; j++) {
267
- value = values[i][insertKeys[j]];
268
- if (value === undefined)
269
- items[j] = defaultValues[j];
270
- else
271
- items[j] = this.toSqlStr(value, asserts[j]);
272
- }
273
- valuesStr[i] = "(" + items.join(",") + ")";
274
- }
275
- return new YourValuesAs(insertKeys, asName, valuesStr.join(",\n"));
276
- }
277
245
  }
278
246
  class AssertError extends TypeError {
279
247
  constructor(assertType, actual) {
280
248
  super(`Assert ${assertType} type, Actual ${actual} type`);
281
249
  }
282
250
  }
251
+ function initColumnAssert(keys, keys_types) {
252
+ let key;
253
+ let value;
254
+ let type = new Array(keys.length);
255
+ for (let i = 0; i < keys.length; i++) {
256
+ key = keys[i];
257
+ value = keys_types[key];
258
+ if (typeof value === "string") {
259
+ type[i] = { sqlType: value };
260
+ }
261
+ else {
262
+ type[i] = value;
263
+ }
264
+ }
265
+ return type;
266
+ }
267
+ /** @public */
268
+ class SqlExplicitValuesStatement {
269
+ columns;
270
+ text;
271
+ constructor(columns, text) {
272
+ this.columns = columns;
273
+ this.text = text;
274
+ }
275
+ toSelect(name) {
276
+ return `(VALUES\n${this.text})\nAS ${name}(${this.columns.join(",")})`;
277
+ }
278
+ }
283
279
 
284
- export { SqlValuesCreator };
280
+ export { SqlExplicitValuesStatement, SqlValuesCreator };
@@ -1,19 +1,18 @@
1
1
  /** @public 断言类型 */
2
2
  export type AssertJsType = "bigint" | "number" | "string" | "boolean" | "object" | (new (...args: any[]) => any);
3
3
  /** @public */
4
- export type SqlValuesTextData = {
5
- columns: string[];
6
- text: string;
7
- };
8
- /** @public */
9
4
  export type ColumnToValueConfig = {
10
- /** 设置显式 SQL 类型,设置后会显示转换 SQL 值 */
5
+ /** 设置显式 SQL 类型,设置后会显式转换 SQL 值 */
11
6
  sqlType?: string;
12
7
  /** 设置 JS 转换器类型,引导转换器如何将 JS 值转换为 SQL 值 */
13
8
  assertJsType?: AssertJsType;
9
+ /** undefined 会被替换的值 */
10
+ sqlDefault?: string;
14
11
  };
15
12
  /** @public */
16
- export type ObjectToValueKeys<T extends {}> = readonly (keyof T)[] | {
17
- [key in keyof T]?: string | undefined | ColumnToValueConfig;
18
- };
13
+ export type ObjectToValueKeys<T extends {}> = readonly string[] | ({
14
+ [key in keyof T as key extends string ? key : never]?: string | ColumnToValueConfig;
15
+ } & {
16
+ [key: string]: string | undefined | ColumnToValueConfig;
17
+ });
19
18
  //# sourceMappingURL=type.d.ts.map
@@ -1,10 +1,18 @@
1
1
  import { Constructable } from "../util.ts";
2
+ import { SelectSqlGenerator } from "./select_chain.ts";
3
+ import { UpdateSqlGenerator } from "./update_chain.ts";
4
+ import { DeleteFromSqlGenerator } from "./delete_chain.ts";
5
+ import { InsertIntoSqlGenerator } from "./insert_chain.ts";
2
6
  /**
3
7
  * @public
4
8
  */
5
9
  export interface ChainCTE {
6
10
  as(statement: Constructable<string>): ChainCTE;
7
11
  as(name: string, statement: Constructable<string>): ChainCTE;
12
+ select: SelectSqlGenerator;
13
+ update: UpdateSqlGenerator;
14
+ deleteFrom: DeleteFromSqlGenerator;
15
+ insertInto: InsertIntoSqlGenerator;
8
16
  toString(): string;
9
17
  }
10
18
  /**
@@ -1,3 +1,9 @@
1
+ import { selectColumns } from '../util.js';
2
+ import { SelectChainAfterSelect } from './select_impl.js';
3
+ import { DeleteChain } from './delete_impl.js';
4
+ import { UpdateChain } from './update_impl.js';
5
+ import { InsertChain } from './insert_impl.js';
6
+
1
7
  function withAs(nameOrStatement, statement) {
2
8
  return new ChainCETImpl(`WITH \n${concat(nameOrStatement, statement)}`);
3
9
  }
@@ -10,11 +16,35 @@ class ChainCETImpl {
10
16
  this.sql = sql;
11
17
  }
12
18
  as(nameOrStatement, statement) {
13
- if (statement) {
14
- return new ChainCETImpl(`${this.sql}\n${concat(nameOrStatement, statement)}`);
19
+ return new ChainCETImpl(`${this.sql},\n${concat(nameOrStatement, statement)}`);
20
+ }
21
+ select(columns) {
22
+ if (!columns)
23
+ return new SelectChainAfterSelect("SELECT ");
24
+ if (typeof columns === "function")
25
+ columns = columns();
26
+ return new SelectChainAfterSelect("SELECT " + selectColumns(columns));
27
+ }
28
+ update(table, options) {
29
+ let sql = `${this.sql}\nUPDATE ${table}`;
30
+ if (options?.as) {
31
+ sql += ` AS ${options.as}`;
32
+ }
33
+ return new UpdateChain(sql);
34
+ }
35
+ deleteFrom(table, option) {
36
+ let sql = `${this.sql}\nDELETE FROM ${table}`;
37
+ if (option?.as) {
38
+ sql += ` AS ${option.as}`;
39
+ }
40
+ return new DeleteChain(sql);
41
+ }
42
+ insertInto(table, columns) {
43
+ if (columns) {
44
+ return new InsertChain(`INSERT INTO ${table}(${columns.join(",")})`);
15
45
  }
16
46
  else {
17
- return new ChainCETImpl(concat(nameOrStatement, statement));
47
+ return new InsertChain(`INSERT INTO ${table}`);
18
48
  }
19
49
  }
20
50
  toString() {
@@ -1,15 +1,4 @@
1
- import { ChainDelete } from "./delete_chain.ts";
1
+ import { DeleteFromSqlGenerator } from "./delete_chain.ts";
2
2
  /** @public */
3
- export interface DeleteOption {
4
- as?: string;
5
- }
6
- /**
7
- * @public
8
- * @example
9
- * ```ts
10
- * deleteFrom("table1").where("id = 1") // DELETE FROM table1 WHERE id = 1
11
- * deleteFrom("table1 AS t").where("t.id = 1") // DELETE FROM table1 AS t WHERE t.id = 1
12
- * ```
13
- */
14
- export declare function deleteFrom(table: string, option?: DeleteOption): ChainDelete;
3
+ export declare const deleteFrom: DeleteFromSqlGenerator;
15
4
  //# sourceMappingURL=delete.d.ts.map
@@ -1,46 +1,12 @@
1
- import { SqlStatement, SqlTextStatementDataset } from '../SqlStatement.js';
2
- import { returningToString, whereToString } from '../_statement.js';
1
+ import { DeleteChain } from './delete_impl.js';
3
2
 
4
- /**
5
- * @public
6
- * @example
7
- * ```ts
8
- * deleteFrom("table1").where("id = 1") // DELETE FROM table1 WHERE id = 1
9
- * deleteFrom("table1 AS t").where("t.id = 1") // DELETE FROM table1 AS t WHERE t.id = 1
10
- * ```
11
- */
12
- function deleteFrom(table, option) {
3
+ /** @public */
4
+ const deleteFrom = function deleteFrom(table, option) {
13
5
  let sql = `DELETE FROM ${table}`;
14
6
  if (option?.as) {
15
7
  sql += ` AS ${option.as}`;
16
8
  }
17
9
  return new DeleteChain(sql);
18
- }
19
- class DeleteChain extends SqlStatement {
20
- sql;
21
- constructor(sql) {
22
- super();
23
- this.sql = sql;
24
- }
25
- using(...from) {
26
- const textList = from.map((f, i) => {
27
- if (typeof f === "function")
28
- return f();
29
- return f;
30
- });
31
- const sql = this.genSql() + `\nUSING ${textList.join(", ")}`;
32
- return new DeleteChain(sql);
33
- }
34
- returning(returns) {
35
- return new SqlTextStatementDataset(this.genSql() + returningToString(returns));
36
- }
37
- where(where) {
38
- const sql = whereToString(where);
39
- return new DeleteChain(this.genSql() + sql);
40
- }
41
- genSql() {
42
- return this.sql;
43
- }
44
- }
10
+ };
45
11
 
46
12
  export { deleteFrom };
@@ -1,6 +1,21 @@
1
1
  import { ConditionParam, Constructable } from "../util.ts";
2
2
  import { ChainModifyReturning } from "./_modify.ts";
3
3
  /** @public */
4
+ export interface DeleteOption {
5
+ as?: string;
6
+ }
7
+ /**
8
+ * @public
9
+ * @example
10
+ * ```ts
11
+ * deleteFrom("table1").where("id = 1") // DELETE FROM table1 WHERE id = 1
12
+ * deleteFrom("table1 AS t").where("t.id = 1") // DELETE FROM table1 AS t WHERE t.id = 1
13
+ * ```
14
+ */
15
+ export interface DeleteFromSqlGenerator {
16
+ (table: string, option?: DeleteOption): ChainDelete;
17
+ }
18
+ /** @public */
4
19
  export interface ChainDelete extends ChainDeleteAfterUsing {
5
20
  /**
6
21
  * @example
@@ -0,0 +1,12 @@
1
+ import { ConditionParam, Constructable, SelectParam } from "../util.ts";
2
+ import { SqlStatementDataset, SqlStatement } from "../SqlStatement.ts";
3
+ import { ChainDelete, ChainDeleteAfterUsing, ChainDeleteReturning } from "./delete_chain.ts";
4
+ export declare class DeleteChain extends SqlStatement implements ChainDelete {
5
+ readonly sql: string;
6
+ constructor(sql: string);
7
+ using(...from: Constructable<string>[]): ChainDeleteAfterUsing;
8
+ returning<R extends {}>(returns: Constructable<SelectParam | "*">): SqlStatementDataset<R>;
9
+ where(where: Constructable<ConditionParam | void>): ChainDeleteReturning;
10
+ genSql(): string;
11
+ }
12
+ //# sourceMappingURL=delete_impl.d.ts.map
@@ -0,0 +1,31 @@
1
+ import { SqlStatement, SqlTextStatementDataset } from '../SqlStatement.js';
2
+ import { returningToString, whereToString } from '../_statement.js';
3
+
4
+ class DeleteChain extends SqlStatement {
5
+ sql;
6
+ constructor(sql) {
7
+ super();
8
+ this.sql = sql;
9
+ }
10
+ using(...from) {
11
+ const textList = from.map((f, i) => {
12
+ if (typeof f === "function")
13
+ return f();
14
+ return f;
15
+ });
16
+ const sql = this.genSql() + `\nUSING ${textList.join(", ")}`;
17
+ return new DeleteChain(sql);
18
+ }
19
+ returning(returns) {
20
+ return new SqlTextStatementDataset(this.genSql() + returningToString(returns));
21
+ }
22
+ where(where) {
23
+ const sql = whereToString(where);
24
+ return new DeleteChain(this.genSql() + sql);
25
+ }
26
+ genSql() {
27
+ return this.sql;
28
+ }
29
+ }
30
+
31
+ export { DeleteChain };
@@ -1,6 +1,4 @@
1
- import { ChainInsert } from "./insert_chain.ts";
1
+ import { InsertIntoSqlGenerator } from "./insert_chain.ts";
2
2
  /** @public */
3
- export declare function insertInto(target: string): ChainInsert;
4
- /** @public */
5
- export declare function insertInto(table: string, columns: readonly string[]): ChainInsert;
3
+ export declare const insertInto: InsertIntoSqlGenerator;
6
4
  //# sourceMappingURL=insert.d.ts.map