Fauna v10 .NET/C# Driver 1.0.1
 
Loading...
Searching...
No Matches
IntermediateQueryHelpers.cs
Go to the documentation of this file.
1namespace Fauna.Linq;
2
3internal static class IntermediateQueryHelpers
4{
5 public static QueryExpr Expr(string fql) => new(new List<IQueryFragment> { new QueryLiteral(fql) });
6
7 public static QueryVal Const(object? v) => new(v);
8
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);
13
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);
18
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);
25
26 public static Query Op(Query a, string op, Query b) =>
27 a.Concat(Expr(op)).Concat(b);
28
29 public static Query FieldAccess(Query callee, string f) =>
30 callee.Concat($".{f}");
31
32 public static Query FnCall(string m) =>
33 Expr($"{m}()");
34
35 public static Query FnCall(string m, Query arg) =>
36 Expr($"{m}(").Concat(arg).Concat(_rparen);
37
38 public static Query FnCall(string m, IEnumerable<Query> args) =>
39 Join(args, Expr($"{m}("), ",", _rparen);
40
41 public static Query MethodCall(Query callee, string m) =>
42 callee.Concat($".{m}()");
43
44 public static Query MethodCall(Query callee, string m, Query a1) =>
45 callee.Concat($".{m}(").Concat(a1).Concat(_rparen);
46
47 public static Query MethodCall(Query callee, string m, Query a1, Query a2) =>
48 callee.Concat($".{m}(").Concat(a1).Concat(",").Concat(a2).Concat(_rparen);
49
50 public static Query MethodCall(Query callee, string m, IEnumerable<Query> args) =>
51 Join(args, callee.Concat($".{m}("), ",", _rparen);
52
53 public static Query Join(IEnumerable<Query> ies, Query l, string sep, Query r)
54 {
55 Query ret = l;
56 var init = true;
57 foreach (var ie in ies)
58 {
59 if (init) init = false; else ret = ret.Concat(sep);
60 ret = ret.Concat(ie);
61 }
62 ret = ret.Concat(r);
63 return ret;
64 }
65
66 public static Query CollectionAll(DataContext.ICollection col) =>
67 MethodCall(Expr(col.Name), "all");
68
69 public static Query CollectionIndex(DataContext.IIndex idx) =>
70 MethodCall(Expr(idx.Collection.Name), idx.Name, idx.Args.Select(Const));
71
72 public static Query Function(string name, object[] args) =>
73 FnCall(name, args.Select(Const));
74
75 public static QueryExpr Concat(this Query q1, string str)
76 {
77 var frags = new List<IQueryFragment>();
78
79 if (q1 is QueryExpr e1)
80 {
81 if (e1.Fragments.Last() is QueryLiteral l1)
82 {
83 frags.AddRange(e1.Fragments.SkipLast(1));
84 frags.Add(new QueryLiteral(l1.Unwrap + str));
85 }
86 else
87 {
88 frags.AddRange(e1.Fragments);
89 frags.Add(new QueryLiteral(str));
90 }
91 }
92 else
93 {
94 frags.Add(q1);
95 frags.Add(new QueryLiteral(str));
96 }
97
98 return new QueryExpr(frags);
99 }
100
101 public static QueryExpr Concat(this Query q1, Query q2)
102 {
103 var frags = new List<IQueryFragment>();
104
105 if (q1 is QueryExpr e1)
106 {
107 if (q2 is QueryExpr e2)
108 {
109 if (e1.Fragments.Last() is QueryLiteral l1 &&
110 e2.Fragments.First() is QueryLiteral l2)
111 {
112 frags.AddRange(e1.Fragments.SkipLast(1));
113 frags.Add(new QueryLiteral(l1.Unwrap + l2.Unwrap));
114 frags.AddRange(e2.Fragments.Skip(1));
115 }
116 else
117 {
118 frags.AddRange(e1.Fragments);
119 frags.AddRange(e2.Fragments);
120 }
121 }
122 else
123 {
124 frags.AddRange(e1.Fragments);
125 frags.Add(q2);
126 }
127 }
128 else
129 {
130 if (q2 is QueryExpr e2)
131 {
132 frags.Add(q1);
133 frags.AddRange(e2.Fragments);
134 }
135 else
136 {
137 frags.Add(q1);
138 frags.Add(q2);
139 }
140 }
141
142 return new QueryExpr(frags);
143 }
144}
@ Query
When a "pure" query, no local processing is required except deserialization.