rado 0.2.21 → 0.2.23

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,8 +1,7 @@
1
1
  // src/lib/Driver.ts
2
- import { Cursor } from "../define/Cursor.js";
3
2
  import { Expr, ExprData } from "../define/Expr.js";
4
3
  import { ParamData } from "../define/Param.js";
5
- import { Query, QueryType } from "../define/Query.js";
4
+ import { Query, QueryData, QueryType } from "../define/Query.js";
6
5
  import { Schema } from "../define/Schema.js";
7
6
  import { Table } from "../define/Table.js";
8
7
  import { Callable } from "../util/Callable.js";
@@ -13,13 +12,13 @@ var DriverBase = class extends Callable {
13
12
  constructor(formatter) {
14
13
  super((...args) => {
15
14
  const [input, ...rest] = args;
16
- if (input instanceof Cursor && rest.length === 0)
17
- return this.executeQuery(input[Cursor.Query]);
15
+ if (Query.isQuery(input) && rest.length === 0)
16
+ return this.executeQuery(input[Query.Data]);
18
17
  if (isTemplateStringsArray(input))
19
18
  return this.executeTemplate(void 0, input, ...rest);
20
19
  return this.executeQuery(
21
- Query.Batch({
22
- queries: args.filter((arg) => arg instanceof Cursor).map((arg) => arg[Cursor.Query])
20
+ new QueryData.Batch({
21
+ queries: args.filter(Query.isQuery).map((arg) => arg[Query.Data])
23
22
  })
24
23
  );
25
24
  });
@@ -29,33 +28,32 @@ var DriverBase = class extends Callable {
29
28
  const { length } = create;
30
29
  const paramNames = Array.from({ length }, (_, i) => `p${i}`);
31
30
  const params = paramNames.map(
32
- (name) => new Expr(ExprData.Param(ParamData.Named(name)))
31
+ (name) => new Expr(new ExprData.Param(new ParamData.Named(name)))
33
32
  );
34
33
  const cursor = create(...params);
35
- const query = cursor[Cursor.Query];
34
+ const query = cursor[Query.Data];
36
35
  return [query, this.formatter.compile(query)];
37
36
  }
38
37
  all(...args) {
39
38
  const [input, ...rest] = args;
40
- if (input instanceof Cursor.SelectSingle)
41
- return this.executeQuery(input.all()[Cursor.Query]);
42
- if (input instanceof Cursor)
43
- return this.executeQuery(input[Cursor.Query]);
39
+ if (Query.isSingle(input))
40
+ return this.executeQuery(input.all()[Query.Data]);
41
+ if (Query.isQuery(input))
42
+ return this.executeQuery(input[Query.Data]);
44
43
  return this.executeTemplate("rows", input, ...rest);
45
44
  }
46
45
  get(...args) {
47
46
  const [input, ...rest] = args;
48
- if (input instanceof Cursor.SelectMultiple)
49
- return this.executeQuery(input.maybeFirst()[Cursor.Query]);
50
- if (input instanceof Cursor)
51
- return this.executeQuery(input[Cursor.Query]);
47
+ if (Query.isMultiple(input))
48
+ return this.executeQuery(input.maybeFirst()[Query.Data]);
49
+ if (Query.isQuery(input))
50
+ return this.executeQuery(input[Query.Data]);
52
51
  return this.executeTemplate("row", input, ...rest);
53
52
  }
54
- sql(strings, ...params) {
55
- return new Cursor(Query.Raw({ strings, params }));
56
- }
57
53
  executeTemplate(expectedReturn, strings, ...params) {
58
- return this.executeQuery(Query.Raw({ strings, params, expectedReturn }));
54
+ return this.executeQuery(
55
+ new QueryData.Raw({ strings, params, expectedReturn })
56
+ );
59
57
  }
60
58
  };
61
59
  var SyncDriver = class extends DriverBase {
@@ -71,7 +69,11 @@ var SyncDriver = class extends DriverBase {
71
69
  const namedParams = Object.fromEntries(
72
70
  params.map((value, i) => [`p${i}`, value])
73
71
  );
74
- return this.executeQuery(query, prepared, compiled.params(namedParams));
72
+ return this.executeQuery(
73
+ query,
74
+ prepared,
75
+ compiled.params(namedParams)
76
+ );
75
77
  };
76
78
  }
77
79
  migrateSchema(...tables) {
@@ -87,7 +89,7 @@ var SyncDriver = class extends DriverBase {
87
89
  queries.push(...changes);
88
90
  }
89
91
  }
90
- return this.executeQuery(Query.Batch({ queries }));
92
+ return this.executeQuery(new QueryData.Batch({ queries }));
91
93
  }
92
94
  executeQuery(query, stmt, params) {
93
95
  switch (query.type) {
@@ -139,7 +141,7 @@ var SyncDriver = class extends DriverBase {
139
141
  }
140
142
  *iterate(cursor) {
141
143
  const stmt = this.prepareStatement(
142
- this.formatter.compile(cursor[Cursor.Query]),
144
+ this.formatter.compile(cursor[Query.Data]),
143
145
  true
144
146
  );
145
147
  for (const row of stmt.iterate()) {
@@ -149,17 +151,23 @@ var SyncDriver = class extends DriverBase {
149
151
  transaction(run) {
150
152
  const id = `t${this.transactionId++}`;
151
153
  this.executeQuery(
152
- Query.Transaction({ op: Query.TransactionOperation.Begin, id })
154
+ new QueryData.Transaction({ op: QueryData.TransactionOperation.Begin, id })
153
155
  );
154
156
  try {
155
157
  const res = run(this);
156
158
  this.executeQuery(
157
- Query.Transaction({ op: Query.TransactionOperation.Commit, id })
159
+ new QueryData.Transaction({
160
+ op: QueryData.TransactionOperation.Commit,
161
+ id
162
+ })
158
163
  );
159
164
  return res;
160
165
  } catch (e) {
161
166
  this.executeQuery(
162
- Query.Transaction({ op: Query.TransactionOperation.Rollback, id })
167
+ new QueryData.Transaction({
168
+ op: QueryData.TransactionOperation.Rollback,
169
+ id
170
+ })
163
171
  );
164
172
  throw e;
165
173
  }
@@ -181,7 +189,11 @@ var AsyncDriver = class extends DriverBase {
181
189
  const namedParams = Object.fromEntries(
182
190
  params.map((value, i) => [`p${i}`, value])
183
191
  );
184
- return this.executeQuery(query, prepared, compiled.params(namedParams));
192
+ return this.executeQuery(
193
+ query,
194
+ prepared,
195
+ compiled.params(namedParams)
196
+ );
185
197
  };
186
198
  }
187
199
  async migrateSchema(...tables) {
@@ -197,7 +209,7 @@ var AsyncDriver = class extends DriverBase {
197
209
  queries.push(...changes);
198
210
  }
199
211
  }
200
- return this.executeQuery(Query.Batch({ queries }));
212
+ return this.executeQuery(new QueryData.Batch({ queries }));
201
213
  }
202
214
  async executeQuery(query, stmt, params) {
203
215
  switch (query.type) {
@@ -251,7 +263,7 @@ var AsyncDriver = class extends DriverBase {
251
263
  }
252
264
  async *iterate(cursor) {
253
265
  const stmt = this.prepareStatement(
254
- this.formatter.compile(cursor[Cursor.Query]),
266
+ this.formatter.compile(cursor[Query.Data]),
255
267
  true
256
268
  );
257
269
  for await (const row of stmt.iterate()) {
@@ -262,17 +274,23 @@ var AsyncDriver = class extends DriverBase {
262
274
  const id = `t${this.transactionId++}`;
263
275
  const [connection, release] = this.isolate();
264
276
  await connection.executeQuery(
265
- Query.Transaction({ op: Query.TransactionOperation.Begin, id })
277
+ new QueryData.Transaction({ op: QueryData.TransactionOperation.Begin, id })
266
278
  );
267
279
  try {
268
280
  const res = await run(connection);
269
281
  await connection.executeQuery(
270
- Query.Transaction({ op: Query.TransactionOperation.Commit, id })
282
+ new QueryData.Transaction({
283
+ op: QueryData.TransactionOperation.Commit,
284
+ id
285
+ })
271
286
  );
272
287
  return res;
273
288
  } catch (e) {
274
289
  await connection.executeQuery(
275
- Query.Transaction({ op: Query.TransactionOperation.Rollback, id })
290
+ new QueryData.Transaction({
291
+ op: QueryData.TransactionOperation.Rollback,
292
+ id
293
+ })
276
294
  );
277
295
  throw e;
278
296
  } finally {
@@ -1,7 +1,7 @@
1
1
  import { ColumnData, ColumnType } from '../define/Column';
2
2
  import { ExprData } from '../define/Expr';
3
3
  import { OrderBy } from '../define/OrderBy';
4
- import { Query } from '../define/Query';
4
+ import { QueryData } from '../define/Query';
5
5
  import { Target } from '../define/Target';
6
6
  import { Sanitizer } from './Sanitizer';
7
7
  import { Statement, StatementOptions } from './Statement';
@@ -33,7 +33,7 @@ export declare abstract class Formatter implements Sanitizer {
33
33
  abstract escapeIdentifier(ident: string): string;
34
34
  abstract formatParamValue(paramValue: any): any;
35
35
  abstract formatAccess(ctx: FormatContext, mkSubject: () => void, field: string): Statement;
36
- compile<T>(query: Query<T>, options?: CompileOptions): Statement;
36
+ compile(query: QueryData, options?: CompileOptions): Statement;
37
37
  createContext(options?: CompileOptions): {
38
38
  stmt: Statement;
39
39
  nameResult?: string | undefined;
@@ -47,19 +47,19 @@ export declare abstract class Formatter implements Sanitizer {
47
47
  selectAsColumns?: boolean | undefined;
48
48
  skipNewlines?: boolean | undefined;
49
49
  };
50
- format<T>(ctx: FormatContext, query: Query<T>): Statement;
51
- formatSelect({ topLevel, ...ctx }: FormatContext, query: Query.Select): Statement;
52
- formatInsert(ctx: FormatContext, query: Query.Insert): Statement;
53
- formatUpdate(ctx: FormatContext, query: Query.Update): Statement;
54
- formatDelete(ctx: FormatContext, query: Query.Delete): Statement;
55
- formatCreateTable(ctx: FormatContext, query: Query.CreateTable): Statement;
56
- formatCreateIndex(ctx: FormatContext, query: Query.CreateIndex): Statement;
57
- formatDropIndex(ctx: FormatContext, query: Query.DropIndex): Statement;
58
- formatAlterTable(ctx: FormatContext, query: Query.AlterTable): Statement;
59
- formatDropTable(ctx: FormatContext, query: Query.DropTable): Statement;
60
- formatTransaction(ctx: FormatContext, { op, id }: Query.Transaction): Statement;
61
- formatBatch(ctx: FormatContext, { queries }: Query.Batch): Statement;
62
- formatRaw(ctx: FormatContext, { strings, params }: Query.Raw): Statement;
50
+ format(ctx: FormatContext, query: QueryData): Statement;
51
+ formatSelect({ topLevel, ...ctx }: FormatContext, query: QueryData.Select): Statement;
52
+ formatInsert(ctx: FormatContext, query: QueryData.Insert): Statement;
53
+ formatUpdate(ctx: FormatContext, query: QueryData.Update): Statement;
54
+ formatDelete(ctx: FormatContext, query: QueryData.Delete): Statement;
55
+ formatCreateTable(ctx: FormatContext, query: QueryData.CreateTable): Statement;
56
+ formatCreateIndex(ctx: FormatContext, query: QueryData.CreateIndex): Statement;
57
+ formatDropIndex(ctx: FormatContext, query: QueryData.DropIndex): Statement;
58
+ formatAlterTable(ctx: FormatContext, query: QueryData.AlterTable): Statement;
59
+ formatDropTable(ctx: FormatContext, query: QueryData.DropTable): Statement;
60
+ formatTransaction(ctx: FormatContext, { op, id }: QueryData.Transaction): Statement;
61
+ formatBatch(ctx: FormatContext, { queries }: QueryData.Batch): Statement;
62
+ formatRaw(ctx: FormatContext, { strings, params }: QueryData.Raw): Statement;
63
63
  formatColumn(ctx: FormatContext, column: ColumnData): Statement;
64
64
  formatContraintReference(ctx: FormatContext, reference: ExprData): Statement;
65
65
  formatType(ctx: FormatContext, type: ColumnType): Statement;
@@ -70,15 +70,26 @@ export declare abstract class Formatter implements Sanitizer {
70
70
  formatHaving({ topLevel, ...ctx }: FormatContext, expr: ExprData | undefined): Statement;
71
71
  formatGroupBy(ctx: FormatContext, groupBy: Array<ExprData> | undefined): Statement;
72
72
  formatOrderBy(ctx: FormatContext, orderBy: Array<OrderBy> | undefined): Statement;
73
- formatLimit(ctx: FormatContext, { limit, offset, singleResult }: Query.QueryBase): Statement;
73
+ formatLimit(ctx: FormatContext, { limit, offset, singleResult }: QueryData): Statement;
74
74
  formatSelection(ctx: FormatContext, selection: ExprData, formatSubject?: FormatSubject): Statement;
75
- formatExprJson(ctx: FormatContext, expr: ExprData): Statement;
76
- formatExprValue(ctx: FormatContext, expr: ExprData): Statement;
77
- retrieveField(expr: ExprData, field: string): ExprData | undefined;
78
- formatField(ctx: FormatContext, expr: ExprData, field: string): Statement;
79
75
  formatString(ctx: FormatContext, input: string): Statement;
80
76
  formatInlineValue(ctx: FormatContext, rawValue: any): Statement;
81
77
  formatValue(ctx: FormatContext, rawValue: any): Statement;
82
- formatExpr({ formatAsIn, ...ctx }: FormatContext, expr: ExprData): Statement;
78
+ formatExprJson(ctx: FormatContext, expr: ExprData): Statement;
79
+ formatExprValue(ctx: FormatContext, expr: ExprData): Statement;
80
+ formatExpr(ctx: FormatContext, expr: ExprData): Statement;
81
+ formatUnOp(ctx: FormatContext, expr: ExprData.UnOp): Statement;
82
+ formatBinOp(ctx: FormatContext, expr: ExprData.BinOp): Statement;
83
+ formatParam(ctx: FormatContext, expr: ExprData.Param): Statement;
84
+ retrieveField(expr: ExprData, field: string): ExprData | undefined;
85
+ formatFieldOf(ctx: FormatContext, from: ExprData, field: string): Statement;
86
+ formatField({ formatAsIn, ...ctx }: FormatContext, expr: ExprData.Field): Statement;
87
+ formatCall(ctx: FormatContext, expr: ExprData.Call): Statement;
88
+ formatQuery(ctx: FormatContext, expr: ExprData.Query): Statement;
89
+ formatRow(ctx: FormatContext, expr: ExprData.Row): Statement;
90
+ formatMerge(ctx: FormatContext, expr: ExprData.Merge): Statement;
91
+ formatRecord(ctx: FormatContext, expr: ExprData.Record): Statement;
92
+ formatFilter({ formatAsIn, ...ctx }: FormatContext, expr: ExprData.Filter): Statement;
93
+ formatMap({ formatAsIn, ...ctx }: FormatContext, expr: ExprData.Map): Statement;
83
94
  }
84
95
  export {};