rado 0.1.59 → 0.1.61

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,4 +1,5 @@
1
1
  import { Driver } from '../lib/Driver';
2
+ import { CompileOptions } from '../lib/Formatter';
2
3
  import { EV, Expr } from './Expr';
3
4
  import { OrderBy } from './OrderBy';
4
5
  import { Query } from './Query';
@@ -13,7 +14,7 @@ export declare class Cursor<T> {
13
14
  query(): Query<T>;
14
15
  on(driver: Driver.Sync): T;
15
16
  on(driver: Driver.Async): Promise<T>;
16
- compile(driver: Driver): import("..").Statement;
17
+ compile(driver: Driver, options?: CompileOptions): import("..").Statement;
17
18
  toJSON(): Query<T>;
18
19
  }
19
20
  export declare namespace Cursor {
@@ -24,8 +24,8 @@ var Cursor = class {
24
24
  on(driver) {
25
25
  return driver.executeQuery(this.query());
26
26
  }
27
- compile(driver) {
28
- return driver.formatter.compile(this.query());
27
+ compile(driver, options) {
28
+ return driver.formatter.compile(this.query(), options);
29
29
  }
30
30
  toJSON() {
31
31
  return this.query();
@@ -5,11 +5,11 @@ import { ParamData } from './Param';
5
5
  import { Query } from './Query';
6
6
  import { Selection } from './Selection';
7
7
  import { Target } from './Target';
8
- export declare enum UnOp {
8
+ export declare enum UnOpType {
9
9
  Not = "Not",
10
10
  IsNull = "IsNull"
11
11
  }
12
- export declare enum BinOp {
12
+ export declare enum BinOpType {
13
13
  Add = "Add",
14
14
  Subt = "Subt",
15
15
  Mult = "Mult",
@@ -44,56 +44,74 @@ export declare enum ExprType {
44
44
  Merge = "Merge",
45
45
  Case = "Case"
46
46
  }
47
- export type ExprData = {
48
- type: ExprType.UnOp;
49
- op: UnOp;
50
- expr: ExprData;
51
- } | {
52
- type: ExprType.BinOp;
53
- op: BinOp;
54
- a: ExprData;
55
- b: ExprData;
56
- } | {
57
- type: ExprType.Field;
58
- expr: ExprData;
59
- field: string;
60
- } | {
61
- type: ExprType.Param;
62
- param: ParamData;
63
- } | {
64
- type: ExprType.Call;
65
- method: string;
66
- params: Array<ExprData>;
67
- } | {
68
- type: ExprType.Query;
69
- query: Query.Select;
70
- } | {
71
- type: ExprType.Record;
72
- fields: Record<string, ExprData>;
73
- } | {
74
- type: ExprType.Merge;
75
- a: ExprData;
76
- b: ExprData;
77
- } | {
78
- type: ExprType.Row;
79
- target: Target;
80
- } | {
81
- type: ExprType.Map;
82
- target: Target;
83
- result: ExprData;
84
- } | {
85
- type: ExprType.Filter;
86
- target: Target;
87
- condition: ExprData;
88
- } | {
89
- type: ExprType.Case;
90
- expr: ExprData;
91
- cases: Record<string, ExprData>;
92
- defaultCase?: ExprData;
93
- };
47
+ export type ExprData = ExprData.UnOp | ExprData.BinOp | ExprData.Field | ExprData.Param | ExprData.Call | ExprData.Query | ExprData.Record | ExprData.Merge | ExprData.Row | ExprData.Map | ExprData.Filter | ExprData.Case;
48
+ export declare namespace ExprData {
49
+ type UnOp = {
50
+ type: ExprType.UnOp;
51
+ op: UnOpType;
52
+ expr: ExprData;
53
+ };
54
+ type BinOp = {
55
+ type: ExprType.BinOp;
56
+ op: BinOpType;
57
+ a: ExprData;
58
+ b: ExprData;
59
+ };
60
+ type Field = {
61
+ type: ExprType.Field;
62
+ expr: ExprData;
63
+ field: string;
64
+ };
65
+ type Param = {
66
+ type: ExprType.Param;
67
+ param: ParamData;
68
+ };
69
+ type Call = {
70
+ type: ExprType.Call;
71
+ method: string;
72
+ params: Array<ExprData>;
73
+ };
74
+ type Query = {
75
+ type: ExprType.Query;
76
+ query: Query.Select;
77
+ };
78
+ type Record = {
79
+ type: ExprType.Record;
80
+ fields: {
81
+ [key: string]: ExprData;
82
+ };
83
+ };
84
+ type Merge = {
85
+ type: ExprType.Merge;
86
+ a: ExprData;
87
+ b: ExprData;
88
+ };
89
+ type Row = {
90
+ type: ExprType.Row;
91
+ target: Target;
92
+ };
93
+ type Map = {
94
+ type: ExprType.Map;
95
+ target: Target;
96
+ result: ExprData;
97
+ };
98
+ type Filter = {
99
+ type: ExprType.Filter;
100
+ target: Target;
101
+ condition: ExprData;
102
+ };
103
+ type Case = {
104
+ type: ExprType.Case;
105
+ expr: ExprData;
106
+ cases: {
107
+ [key: string]: ExprData;
108
+ };
109
+ defaultCase?: ExprData;
110
+ };
111
+ }
94
112
  export declare const ExprData: {
95
- UnOp(op: UnOp, expr: ExprData): ExprData;
96
- BinOp(op: BinOp, a: ExprData, b: ExprData): ExprData;
113
+ UnOp(op: UnOpType, expr: ExprData): ExprData;
114
+ BinOp(op: BinOpType, a: ExprData, b: ExprData): ExprData;
97
115
  Field(expr: ExprData, field: string): ExprData;
98
116
  Param(param: ParamData): ExprData;
99
117
  Call(method: string, params: Array<ExprData>): ExprData;
@@ -9,33 +9,33 @@ var __publicField = (obj, key, value) => {
9
9
  import { OrderDirection } from "./OrderBy.js";
10
10
  import { ParamData, ParamType } from "./Param.js";
11
11
  import { Target } from "./Target.js";
12
- var UnOp = /* @__PURE__ */ ((UnOp2) => {
13
- UnOp2["Not"] = "Not";
14
- UnOp2["IsNull"] = "IsNull";
15
- return UnOp2;
16
- })(UnOp || {});
17
- var BinOp = /* @__PURE__ */ ((BinOp2) => {
18
- BinOp2["Add"] = "Add";
19
- BinOp2["Subt"] = "Subt";
20
- BinOp2["Mult"] = "Mult";
21
- BinOp2["Mod"] = "Mod";
22
- BinOp2["Div"] = "Div";
23
- BinOp2["Greater"] = "Greater";
24
- BinOp2["GreaterOrEqual"] = "GreaterOrEqual";
25
- BinOp2["Less"] = "Less";
26
- BinOp2["LessOrEqual"] = "LessOrEqual";
27
- BinOp2["Equals"] = "Equals";
28
- BinOp2["NotEquals"] = "NotEquals";
29
- BinOp2["And"] = "And";
30
- BinOp2["Or"] = "Or";
31
- BinOp2["Like"] = "Like";
32
- BinOp2["Glob"] = "Glob";
33
- BinOp2["Match"] = "Match";
34
- BinOp2["In"] = "In";
35
- BinOp2["NotIn"] = "NotIn";
36
- BinOp2["Concat"] = "Concat";
37
- return BinOp2;
38
- })(BinOp || {});
12
+ var UnOpType = /* @__PURE__ */ ((UnOpType2) => {
13
+ UnOpType2["Not"] = "Not";
14
+ UnOpType2["IsNull"] = "IsNull";
15
+ return UnOpType2;
16
+ })(UnOpType || {});
17
+ var BinOpType = /* @__PURE__ */ ((BinOpType2) => {
18
+ BinOpType2["Add"] = "Add";
19
+ BinOpType2["Subt"] = "Subt";
20
+ BinOpType2["Mult"] = "Mult";
21
+ BinOpType2["Mod"] = "Mod";
22
+ BinOpType2["Div"] = "Div";
23
+ BinOpType2["Greater"] = "Greater";
24
+ BinOpType2["GreaterOrEqual"] = "GreaterOrEqual";
25
+ BinOpType2["Less"] = "Less";
26
+ BinOpType2["LessOrEqual"] = "LessOrEqual";
27
+ BinOpType2["Equals"] = "Equals";
28
+ BinOpType2["NotEquals"] = "NotEquals";
29
+ BinOpType2["And"] = "And";
30
+ BinOpType2["Or"] = "Or";
31
+ BinOpType2["Like"] = "Like";
32
+ BinOpType2["Glob"] = "Glob";
33
+ BinOpType2["Match"] = "Match";
34
+ BinOpType2["In"] = "In";
35
+ BinOpType2["NotIn"] = "NotIn";
36
+ BinOpType2["Concat"] = "Concat";
37
+ return BinOpType2;
38
+ })(BinOpType || {});
39
39
  var ExprType = /* @__PURE__ */ ((ExprType2) => {
40
40
  ExprType2["UnOp"] = "UnOp";
41
41
  ExprType2["BinOp"] = "BinOp";
@@ -113,7 +113,7 @@ function isConstant(e, value) {
113
113
  case "Param" /* Param */:
114
114
  switch (e.param.type) {
115
115
  case ParamType.Value:
116
- return e.param.value == value;
116
+ return e.param.value === value;
117
117
  default:
118
118
  return false;
119
119
  }
@@ -178,7 +178,7 @@ var _Expr = class {
178
178
  return this.isNull().not();
179
179
  }
180
180
  isNot(that) {
181
- if (that == null || that instanceof _Expr && isConstant(that.expr, null))
181
+ if (that === null || that instanceof _Expr && isConstant(that.expr, null))
182
182
  return this.isNotNull();
183
183
  return binop(this, "NotEquals" /* NotEquals */, that);
184
184
  }
@@ -276,9 +276,9 @@ function binop(self, type, that) {
276
276
  return new Expr(ExprData.BinOp(type, self.expr, toExpr(that)));
277
277
  }
278
278
  export {
279
- BinOp,
279
+ BinOpType,
280
280
  Expr,
281
281
  ExprData,
282
282
  ExprType,
283
- UnOp
283
+ UnOpType
284
284
  };
@@ -20,17 +20,20 @@ export interface FormatContext {
20
20
  forceInline?: boolean;
21
21
  formatAsJson?: boolean;
22
22
  formatAsInsert?: boolean;
23
+ formatAsIn?: boolean;
23
24
  topLevel?: boolean;
24
25
  }
25
26
  type FormatSubject = (stmt: Statement, mkSubject: () => void) => void;
27
+ export interface CompileOptions extends Partial<FormatContext>, StatementOptions {
28
+ }
26
29
  export declare abstract class Formatter implements Sanitizer {
27
30
  constructor();
28
31
  abstract escapeValue(value: any): string;
29
32
  abstract escapeIdentifier(ident: string): string;
30
33
  abstract formatParamValue(paramValue: any): any;
31
34
  abstract formatAccess(ctx: FormatContext, mkSubject: () => void, field: string): Statement;
32
- compile<T>(query: Query<T>, options?: Partial<FormatContext>): Statement;
33
- createContext(options?: Partial<FormatContext> & StatementOptions): {
35
+ compile<T>(query: Query<T>, options?: CompileOptions): Statement;
36
+ createContext(options?: CompileOptions): {
34
37
  stmt: Statement;
35
38
  nameResult?: string | undefined;
36
39
  skipTableName?: boolean | undefined;
@@ -38,6 +41,7 @@ export declare abstract class Formatter implements Sanitizer {
38
41
  forceInline?: boolean | undefined;
39
42
  formatAsJson?: boolean | undefined;
40
43
  formatAsInsert?: boolean | undefined;
44
+ formatAsIn?: boolean | undefined;
41
45
  topLevel?: boolean | undefined;
42
46
  skipNewlines?: boolean | undefined;
43
47
  };
@@ -67,7 +71,6 @@ export declare abstract class Formatter implements Sanitizer {
67
71
  formatSelection(ctx: FormatContext, selection: ExprData, formatSubject?: FormatSubject): Statement;
68
72
  formatExprJson(ctx: FormatContext, expr: ExprData): Statement;
69
73
  formatExprValue(ctx: FormatContext, expr: ExprData): Statement;
70
- formatIn(ctx: FormatContext, expr: ExprData): Statement;
71
74
  retrieveField(expr: ExprData, field: string): ExprData | undefined;
72
75
  formatField(ctx: FormatContext, expr: ExprData, field: string): Statement;
73
76
  formatString(ctx: FormatContext, input: string): Statement;
@@ -1,31 +1,31 @@
1
1
  // src/lib/Formatter.ts
2
2
  import { ColumnType } from "../define/Column.js";
3
- import { BinOp, Expr, ExprData, ExprType, UnOp } from "../define/Expr.js";
3
+ import { BinOpType, Expr, ExprData, ExprType, UnOpType } from "../define/Expr.js";
4
4
  import { OrderDirection } from "../define/OrderBy.js";
5
5
  import { ParamType } from "../define/Param.js";
6
6
  import { Query, QueryType } from "../define/Query.js";
7
7
  import { Target, TargetType } from "../define/Target.js";
8
8
  import { Statement } from "./Statement.js";
9
9
  var binOps = {
10
- [BinOp.Add]: "+",
11
- [BinOp.Subt]: "-",
12
- [BinOp.Mult]: "*",
13
- [BinOp.Mod]: "%",
14
- [BinOp.Div]: "/",
15
- [BinOp.Greater]: ">",
16
- [BinOp.GreaterOrEqual]: ">=",
17
- [BinOp.Less]: "<",
18
- [BinOp.LessOrEqual]: "<=",
19
- [BinOp.Equals]: "=",
20
- [BinOp.NotEquals]: "!=",
21
- [BinOp.And]: "and",
22
- [BinOp.Or]: "or",
23
- [BinOp.Like]: "like",
24
- [BinOp.Glob]: "glob",
25
- [BinOp.Match]: "match",
26
- [BinOp.In]: "in",
27
- [BinOp.NotIn]: "not in",
28
- [BinOp.Concat]: "||"
10
+ [BinOpType.Add]: "+",
11
+ [BinOpType.Subt]: "-",
12
+ [BinOpType.Mult]: "*",
13
+ [BinOpType.Mod]: "%",
14
+ [BinOpType.Div]: "/",
15
+ [BinOpType.Greater]: ">",
16
+ [BinOpType.GreaterOrEqual]: ">=",
17
+ [BinOpType.Less]: "<",
18
+ [BinOpType.LessOrEqual]: "<=",
19
+ [BinOpType.Equals]: "=",
20
+ [BinOpType.NotEquals]: "!=",
21
+ [BinOpType.And]: "and",
22
+ [BinOpType.Or]: "or",
23
+ [BinOpType.Like]: "like",
24
+ [BinOpType.Glob]: "glob",
25
+ [BinOpType.Match]: "match",
26
+ [BinOpType.In]: "in",
27
+ [BinOpType.NotIn]: "not in",
28
+ [BinOpType.Concat]: "||"
29
29
  };
30
30
  var joins = {
31
31
  left: "left",
@@ -420,21 +420,6 @@ var Formatter = class {
420
420
  formatExprValue(ctx, expr) {
421
421
  return this.formatExpr({ ...ctx, formatAsJson: false }, expr);
422
422
  }
423
- formatIn(ctx, expr) {
424
- const { stmt } = ctx;
425
- switch (expr.type) {
426
- case ExprType.Field:
427
- stmt.openParenthesis();
428
- stmt.raw("SELECT value FROM json_each");
429
- stmt.openParenthesis();
430
- this.formatExprJson(ctx, expr);
431
- stmt.closeParenthesis();
432
- stmt.closeParenthesis();
433
- return stmt;
434
- default:
435
- return this.formatExprValue(ctx, expr);
436
- }
437
- }
438
423
  retrieveField(expr, field) {
439
424
  switch (expr.type) {
440
425
  case ExprType.Record:
@@ -458,7 +443,7 @@ var Formatter = class {
458
443
  const asBoolean = column?.type === ColumnType.Boolean && ctx.formatAsJson;
459
444
  const asJson = column?.type === ColumnType.Json && ctx.formatAsJson;
460
445
  if (asJson) {
461
- stmt.add("json");
446
+ stmt.raw("json");
462
447
  stmt.openParenthesis();
463
448
  }
464
449
  if (asBoolean)
@@ -533,9 +518,9 @@ var Formatter = class {
533
518
  switch (expr.type) {
534
519
  case ExprType.UnOp:
535
520
  switch (expr.op) {
536
- case UnOp.IsNull:
521
+ case UnOpType.IsNull:
537
522
  return this.formatExprValue(ctx, expr.expr).add("IS NULL");
538
- case UnOp.Not:
523
+ case UnOpType.Not:
539
524
  stmt.raw("NOT").openParenthesis();
540
525
  this.formatExprValue(ctx, expr.expr);
541
526
  return stmt.closeParenthesis();
@@ -543,10 +528,8 @@ var Formatter = class {
543
528
  case ExprType.BinOp:
544
529
  stmt.openParenthesis();
545
530
  this.formatExprValue(ctx, expr.a).add(binOps[expr.op]).space();
546
- if (expr.op === BinOp.In || expr.op === BinOp.NotIn)
547
- this.formatIn(ctx, expr.b);
548
- else
549
- this.formatExprValue(ctx, expr.b);
531
+ const asIn = expr.op === BinOpType.In || expr.op === BinOpType.NotIn;
532
+ this.formatExprValue({ ...ctx, formatAsIn: asIn }, expr.b);
550
533
  return stmt.closeParenthesis();
551
534
  case ExprType.Param:
552
535
  switch (expr.param.type) {
@@ -555,8 +538,18 @@ var Formatter = class {
555
538
  case ParamType.Named:
556
539
  return stmt.param(expr.param);
557
540
  }
558
- case ExprType.Field:
559
- return this.formatField(ctx, expr.expr, expr.field);
541
+ case ExprType.Field: {
542
+ const { formatAsIn, ...rest } = ctx;
543
+ if (formatAsIn) {
544
+ stmt.openParenthesis();
545
+ stmt.raw("SELECT value FROM json_each");
546
+ stmt.openParenthesis();
547
+ this.formatExprJson(rest, expr);
548
+ stmt.closeParenthesis();
549
+ return stmt.closeParenthesis();
550
+ }
551
+ return this.formatField(rest, expr.expr, expr.field);
552
+ }
560
553
  case ExprType.Call: {
561
554
  if (expr.method === "cast") {
562
555
  const [e, type] = expr.params;
@@ -625,32 +618,48 @@ var Formatter = class {
625
618
  }
626
619
  return stmt;
627
620
  case ExprType.Filter: {
621
+ const { formatAsIn, ...rest } = ctx;
628
622
  const { target, condition } = expr;
629
623
  switch (target.type) {
630
624
  case TargetType.Expr:
631
- stmt.openParenthesis().raw("SELECT json_group_array(json(result)) FROM ").openParenthesis().raw("SELECT value AS result").add("FROM json_each").openParenthesis();
632
- this.formatExprJson(ctx, target.expr);
625
+ stmt.openParenthesis();
626
+ if (!formatAsIn) {
627
+ stmt.raw("SELECT json_group_array(json(result)) FROM ").openParenthesis();
628
+ }
629
+ stmt.raw("SELECT value AS result").add("FROM json_each").openParenthesis();
630
+ this.formatExprJson(rest, target.expr);
633
631
  stmt.closeParenthesis();
634
632
  if (target.alias)
635
633
  stmt.add("AS").addIdentifier(target.alias);
636
634
  stmt.add("WHERE").space();
637
- this.formatExprValue(ctx, condition);
638
- return stmt.closeParenthesis().closeParenthesis();
635
+ this.formatExprValue(rest, condition);
636
+ if (!formatAsIn) {
637
+ stmt.closeParenthesis();
638
+ }
639
+ return stmt.closeParenthesis();
639
640
  default:
640
641
  throw new Error("todo");
641
642
  }
642
643
  }
643
644
  case ExprType.Map: {
645
+ const { formatAsIn, ...rest } = ctx;
644
646
  const { target, result } = expr;
645
647
  switch (target.type) {
646
648
  case TargetType.Expr:
647
- stmt.openParenthesis().raw("SELECT json_group_array(json(result)) FROM ").openParenthesis().raw("SELECT").space();
648
- this.formatExprJson(ctx, result).add("AS result").add("FROM json_each").openParenthesis();
649
- this.formatExprJson(ctx, target.expr);
649
+ stmt.openParenthesis();
650
+ if (!formatAsIn) {
651
+ stmt.raw("SELECT json_group_array(json(result)) FROM ").openParenthesis();
652
+ }
653
+ stmt.raw("SELECT").space();
654
+ this.formatExprJson(rest, result).add("AS result").add("FROM json_each").openParenthesis();
655
+ this.formatExprJson(rest, target.expr);
650
656
  stmt.closeParenthesis();
651
657
  if (target.alias)
652
658
  stmt.add("AS").addIdentifier(target.alias);
653
- return stmt.closeParenthesis().closeParenthesis();
659
+ if (!formatAsIn) {
660
+ stmt.closeParenthesis();
661
+ }
662
+ return stmt.closeParenthesis();
654
663
  default:
655
664
  throw new Error("todo");
656
665
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rado",
3
- "version": "0.1.59",
3
+ "version": "0.1.61",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",