rado 0.2.39 → 0.2.41

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.
@@ -20,7 +20,6 @@ export interface FormatContext {
20
20
  forceInline?: boolean;
21
21
  formatAsJson?: boolean;
22
22
  formatAsInsert?: boolean;
23
- formatAsIn?: boolean;
24
23
  topLevel?: boolean;
25
24
  selectAsColumns?: boolean;
26
25
  formattingCte?: string;
@@ -43,7 +42,6 @@ export declare abstract class Formatter implements Sanitizer {
43
42
  forceInline?: boolean | undefined;
44
43
  formatAsJson?: boolean | undefined;
45
44
  formatAsInsert?: boolean | undefined;
46
- formatAsIn?: boolean | undefined;
47
45
  topLevel?: boolean | undefined;
48
46
  selectAsColumns?: boolean | undefined;
49
47
  formattingCte?: string | undefined;
@@ -85,14 +83,24 @@ export declare abstract class Formatter implements Sanitizer {
85
83
  formatBinOp(ctx: FormatContext, expr: ExprData.BinOp): Statement;
86
84
  formatParam(ctx: FormatContext, expr: ExprData.Param): Statement;
87
85
  retrieveField(expr: ExprData, field: string): ExprData | undefined;
86
+ /**
87
+ * Format a in "{a}->>{b}"
88
+ */
89
+ formatExprAccess(ctx: FormatContext, expr: ExprData): Statement;
90
+ /**
91
+ * Format b in "{a} in {b}"
92
+ */
93
+ formatIn(ctx: FormatContext, expr: ExprData): Statement;
88
94
  formatFieldOf(ctx: FormatContext, from: ExprData, field: string): Statement;
89
- formatField({ formatAsIn, ...ctx }: FormatContext, expr: ExprData.Field): Statement;
95
+ formatField(ctx: FormatContext, expr: ExprData.Field): Statement;
90
96
  formatCall(ctx: FormatContext, expr: ExprData.Call): Statement;
91
97
  formatQuery(ctx: FormatContext, expr: ExprData.Query): Statement;
92
98
  formatRow(ctx: FormatContext, expr: ExprData.Row): Statement;
93
99
  formatMerge(ctx: FormatContext, expr: ExprData.Merge): Statement;
94
100
  formatRecord(ctx: FormatContext, expr: ExprData.Record): Statement;
95
- formatFilter({ formatAsIn, ...ctx }: FormatContext, expr: ExprData.Filter): Statement;
96
- formatMap({ formatAsIn, ...ctx }: FormatContext, expr: ExprData.Map): Statement;
101
+ formatFilterIn(ctx: FormatContext, expr: ExprData.Filter): Statement;
102
+ formatFilter(ctx: FormatContext, expr: ExprData.Filter): Statement;
103
+ formatMapIn(ctx: FormatContext, expr: ExprData.Map): Statement;
104
+ formatMap(ctx: FormatContext, expr: ExprData.Map): Statement;
97
105
  }
98
106
  export {};
@@ -440,7 +440,7 @@ class Formatter {
440
440
  const { stmt } = ctx;
441
441
  if (!groupBy)
442
442
  return stmt;
443
- stmt.addLine("GROUP BY");
443
+ stmt.addLine("GROUP BY").space();
444
444
  for (const expr of stmt.separate(groupBy))
445
445
  this.formatExprValue(ctx, expr);
446
446
  return stmt;
@@ -584,7 +584,10 @@ class Formatter {
584
584
  stmt.openParenthesis();
585
585
  this.formatExprValue(ctx, expr.a).add(binOps[expr.op]).space();
586
586
  const asIn = expr.op === BinOpType.In || expr.op === BinOpType.NotIn;
587
- this.formatExprValue({ ...ctx, formatAsIn: asIn }, expr.b);
587
+ if (asIn)
588
+ this.formatIn(ctx, expr.b);
589
+ else
590
+ this.formatExprValue(ctx, expr.b);
588
591
  return stmt.closeParenthesis();
589
592
  }
590
593
  formatParam(ctx, expr) {
@@ -606,6 +609,51 @@ class Formatter {
606
609
  return void 0;
607
610
  }
608
611
  }
612
+ /**
613
+ * Format a in "{a}->>{b}"
614
+ */
615
+ formatExprAccess(ctx, expr) {
616
+ const { stmt } = ctx;
617
+ if (expr.type === ExprType.Field && expr.expr.type === ExprType.Row) {
618
+ const from = expr.expr;
619
+ switch (from.target.type) {
620
+ case TargetType.Table:
621
+ if (!ctx.skipTableName) {
622
+ stmt.identifier(from.target.table.alias || from.target.table.name).raw(".");
623
+ }
624
+ return stmt.identifier(expr.field);
625
+ }
626
+ }
627
+ return this.formatExpr(ctx, expr);
628
+ }
629
+ /**
630
+ * Format b in "{a} in {b}"
631
+ */
632
+ formatIn(ctx, expr) {
633
+ const { stmt } = ctx;
634
+ switch (expr.type) {
635
+ case ExprType.Filter:
636
+ return this.formatFilterIn(ctx, expr);
637
+ case ExprType.Map:
638
+ return this.formatMapIn(ctx, expr);
639
+ case ExprType.Field:
640
+ stmt.openParenthesis();
641
+ stmt.raw("SELECT value FROM json_each");
642
+ stmt.openParenthesis();
643
+ this.formatExprValue(ctx, expr);
644
+ stmt.closeParenthesis();
645
+ return stmt.closeParenthesis();
646
+ case ExprType.Param:
647
+ if (expr.param.type === ParamType.Value && Array.isArray(expr.param.value)) {
648
+ stmt.openParenthesis();
649
+ for (const v of stmt.separate(expr.param.value))
650
+ this.formatValue(ctx, v);
651
+ return stmt.closeParenthesis();
652
+ }
653
+ default:
654
+ return this.formatExpr(ctx, expr);
655
+ }
656
+ }
609
657
  formatFieldOf(ctx, from, field) {
610
658
  const { stmt } = ctx;
611
659
  const fieldExpr = this.retrieveField(from, field);
@@ -640,19 +688,14 @@ class Formatter {
640
688
  return stmt;
641
689
  }
642
690
  default:
643
- return this.formatAccess(ctx, () => this.formatExpr(ctx, from), field);
691
+ return this.formatAccess(
692
+ ctx,
693
+ () => this.formatExprAccess(ctx, from),
694
+ field
695
+ );
644
696
  }
645
697
  }
646
- formatField({ formatAsIn, ...ctx }, expr) {
647
- const { stmt } = ctx;
648
- if (formatAsIn) {
649
- stmt.openParenthesis();
650
- stmt.raw("SELECT value FROM json_each");
651
- stmt.openParenthesis();
652
- this.formatExprJson(ctx, expr);
653
- stmt.closeParenthesis();
654
- return stmt.closeParenthesis();
655
- }
698
+ formatField(ctx, expr) {
656
699
  return this.formatFieldOf(ctx, expr.expr, expr.field);
657
700
  }
658
701
  formatCall(ctx, expr) {
@@ -678,7 +721,7 @@ class Formatter {
678
721
  const { stmt } = ctx;
679
722
  if (!ctx.formatAsJson) {
680
723
  stmt.openParenthesis();
681
- this.format({ ...ctx, formatAsIn: false }, expr.query);
724
+ this.format(ctx, expr.query);
682
725
  return stmt.closeParenthesis();
683
726
  }
684
727
  if (expr.query.singleResult) {
@@ -747,15 +790,12 @@ class Formatter {
747
790
  }
748
791
  return stmt.closeParenthesis();
749
792
  }
750
- formatFilter({ formatAsIn, ...ctx }, expr) {
793
+ formatFilterIn(ctx, expr) {
751
794
  const { stmt } = ctx;
752
795
  const { target, condition } = expr;
753
796
  switch (target.type) {
754
797
  case TargetType.Expr:
755
798
  stmt.openParenthesis();
756
- if (!formatAsIn) {
757
- stmt.raw("SELECT json_group_array(json(result)) FROM ").openParenthesis();
758
- }
759
799
  stmt.raw("SELECT value AS result").add("FROM json_each").openParenthesis();
760
800
  this.formatExpr(ctx, target.expr);
761
801
  stmt.closeParenthesis();
@@ -763,37 +803,52 @@ class Formatter {
763
803
  stmt.add("AS").addIdentifier(target.alias);
764
804
  stmt.add("WHERE").space();
765
805
  this.formatExprValue(ctx, condition);
766
- if (!formatAsIn) {
767
- stmt.closeParenthesis();
768
- }
769
806
  return stmt.closeParenthesis();
770
807
  default:
771
808
  throw new Error("todo");
772
809
  }
773
810
  }
774
- formatMap({ formatAsIn, ...ctx }, expr) {
811
+ formatFilter(ctx, expr) {
812
+ const { stmt } = ctx;
813
+ switch (expr.target.type) {
814
+ case TargetType.Expr:
815
+ stmt.openParenthesis().raw("SELECT json_group_array(json(result)) FROM ");
816
+ this.formatFilterIn(ctx, expr);
817
+ stmt.closeParenthesis();
818
+ return stmt;
819
+ default:
820
+ throw new Error("todo");
821
+ }
822
+ }
823
+ formatMapIn(ctx, expr) {
775
824
  const { stmt } = ctx;
776
825
  const { target, result } = expr;
777
826
  switch (target.type) {
778
827
  case TargetType.Expr:
779
828
  stmt.openParenthesis();
780
- if (!formatAsIn) {
781
- stmt.raw("SELECT json_group_array(json(result)) FROM ").openParenthesis();
782
- }
783
829
  stmt.raw("SELECT").space();
784
830
  this.formatExpr(ctx, result).add("AS result").add("FROM json_each").openParenthesis();
785
831
  this.formatExprJson(ctx, target.expr);
786
832
  stmt.closeParenthesis();
787
833
  if (target.alias)
788
834
  stmt.add("AS").addIdentifier(target.alias);
789
- if (!formatAsIn) {
790
- stmt.closeParenthesis();
791
- }
792
835
  return stmt.closeParenthesis();
793
836
  default:
794
837
  throw new Error("todo");
795
838
  }
796
839
  }
840
+ formatMap(ctx, expr) {
841
+ const { stmt } = ctx;
842
+ switch (expr.target.type) {
843
+ case TargetType.Expr:
844
+ stmt.openParenthesis().raw("SELECT json_group_array(json(result)) FROM ");
845
+ this.formatMapIn(ctx, expr);
846
+ stmt.closeParenthesis();
847
+ return stmt;
848
+ default:
849
+ throw new Error("todo");
850
+ }
851
+ }
797
852
  }
798
853
  export {
799
854
  Formatter
@@ -170,9 +170,12 @@ class Statement {
170
170
  return this.sql.replace(/\?/g, () => {
171
171
  const param = this.paramData[index];
172
172
  index++;
173
- if (param.type === ParamType.Named)
174
- throw new TypeError(`Missing parameter ${param.name}`);
175
- return this.sanitizer.escapeValue(param.value);
173
+ switch (param.type) {
174
+ case ParamType.Named:
175
+ return `?${param.name}`;
176
+ case ParamType.Value:
177
+ return this.sanitizer.escapeValue(param.value);
178
+ }
176
179
  });
177
180
  }
178
181
  toString() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rado",
3
- "version": "0.2.39",
3
+ "version": "0.2.41",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",