3internal static class IntermediateQueryHelpers
5 public static QueryExpr Expr(
string fql) =>
new(
new List<IQueryFragment> {
new QueryLiteral(fql) });
7 public static QueryVal Const(
object? v) =>
new(v);
9 private static readonly
Query _larr = Expr(
"[");
10 private static readonly
Query _rarr = Expr(
"]");
11 public static Query Array(
Query inner) => _larr.Concat(inner).Concat(_rarr);
12 public static Query Array(IEnumerable<Query> inners) => Join(inners, _larr,
",", _rarr);
14 private static readonly
Query _lparen = Expr(
"(");
15 private static readonly
Query _rparen = Expr(
")");
16 public static Query Parens(
Query inner) => _lparen.Concat(inner).Concat(_rparen);
17 public static Query Parens(IEnumerable<Query> inners) => Join(inners, _lparen,
",", _rparen);
19 private static readonly
Query _lbrace = Expr(
"{");
20 private static readonly
Query _rbrace = Expr(
"}");
21 public static Query Block(
Query inner) => _lbrace.Concat(inner).Concat(
"}");
22 public static Query Block(IEnumerable<Query> inners) => Join(inners, _lbrace,
";", _rbrace);
23 public static Query Obj(
Query inner) => _lbrace.Concat(inner).Concat(
"}");
24 public static Query Obj(IEnumerable<Query> inners) => Join(inners, _lbrace,
",", _rbrace);
27 a.Concat(Expr(op)).Concat(b);
29 public static Query FieldAccess(
Query callee,
string f) =>
30 callee.Concat($
".{f}");
32 public static Query FnCall(
string m) =>
35 public static Query FnCall(
string m,
Query arg) =>
36 Expr($
"{m}(").Concat(arg).Concat(_rparen);
38 public static Query FnCall(
string m, IEnumerable<Query> args) =>
39 Join(args, Expr($
"{m}("),
",", _rparen);
41 public static Query MethodCall(
Query callee,
string m) =>
42 callee.Concat($
".{m}()");
45 callee.Concat($
".{m}(").Concat(a1).Concat(_rparen);
48 callee.Concat($
".{m}(").Concat(a1).Concat(
",").Concat(a2).Concat(_rparen);
50 public static Query MethodCall(
Query callee,
string m, IEnumerable<Query> args) =>
51 Join(args, callee.Concat($
".{m}("),
",", _rparen);
53 public static Query Join(IEnumerable<Query> ies,
Query l,
string sep,
Query r)
57 foreach (var ie
in ies)
59 if (init) init =
false;
else ret = ret.Concat(sep);
66 public static Query CollectionAll(DataContext.ICollection col) =>
67 MethodCall(Expr(col.Name),
"all");
69 public static Query CollectionIndex(DataContext.IIndex idx) =>
70 MethodCall(Expr(idx.Collection.Name), idx.Name, idx.Args.Select(Const));
72 public static Query Function(
string name,
object[] args) =>
73 FnCall(name, args.Select(Const));
75 public static QueryExpr Concat(
this Query q1,
string str)
77 var frags =
new List<IQueryFragment>();
79 if (q1 is QueryExpr e1)
81 if (e1.Fragments.Last() is QueryLiteral l1)
83 frags.AddRange(e1.Fragments.SkipLast(1));
84 frags.Add(
new QueryLiteral(l1.Unwrap + str));
88 frags.AddRange(e1.Fragments);
89 frags.Add(
new QueryLiteral(str));
95 frags.Add(
new QueryLiteral(str));
98 return new QueryExpr(frags);
101 public static QueryExpr Concat(
this Query q1,
Query q2)
103 var frags =
new List<IQueryFragment>();
105 if (q1 is QueryExpr e1)
107 if (q2 is QueryExpr e2)
109 if (e1.Fragments.Last() is QueryLiteral l1 &&
110 e2.Fragments.First() is QueryLiteral l2)
112 frags.AddRange(e1.Fragments.SkipLast(1));
113 frags.Add(
new QueryLiteral(l1.Unwrap + l2.Unwrap));
114 frags.AddRange(e2.Fragments.Skip(1));
118 frags.AddRange(e1.Fragments);
119 frags.AddRange(e2.Fragments);
124 frags.AddRange(e1.Fragments);
130 if (q2 is QueryExpr e2)
133 frags.AddRange(e2.Fragments);
142 return new QueryExpr(frags);
@ Query
When a "pure" query, no local processing is required except deserialization.