Fauna .NET Driver 0.1.0-beta
 
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) =>
6 new QueryExpr(new List<IQueryFragment> { new QueryLiteral(fql) });
7
8 public static QueryVal Const(object? v) => new QueryVal(v);
9
10 private static readonly Query _larr = Expr("[");
11 private static readonly Query _rarr = Expr("]");
12 public static Query Array(Query inner) => _larr.Concat(inner).Concat(_rarr);
13 public static Query Array(IEnumerable<Query> inners) => Join(inners, _larr, ",", _rarr);
14
15 private static readonly Query _lparen = Expr("(");
16 private static readonly Query _rparen = Expr(")");
17 public static Query Parens(Query inner) => _lparen.Concat(inner).Concat(_rparen);
18 public static Query Parens(IEnumerable<Query> inners) => Join(inners, _lparen, ",", _rparen);
19
20 private static readonly Query _lbrace = Expr("{");
21 private static readonly Query _rbrace = Expr("}");
22 public static Query Block(Query inner) => _lbrace.Concat(inner).Concat("}");
23 public static Query Block(IEnumerable<Query> inners) => Join(inners, _lbrace, ";", _rbrace);
24 public static Query Obj(Query inner) => _lbrace.Concat(inner).Concat("}");
25 public static Query Obj(IEnumerable<Query> inners) => Join(inners, _lbrace, ",", _rbrace);
26
27 public static Query Op(Query a, string op, Query b) =>
28 a.Concat(Expr(op)).Concat(b);
29
30 public static Query FieldAccess(Query callee, string f) =>
31 callee.Concat($".{f}");
32
33 public static Query FnCall(string m) =>
34 Expr($"{m}()");
35
36 public static Query FnCall(string m, Query arg) =>
37 Expr($"{m}(").Concat(arg).Concat(_rparen);
38
39 public static Query FnCall(string m, IEnumerable<Query> args) =>
40 Join(args, Expr($"{m}("), ",", _rparen);
41
42 public static Query MethodCall(Query callee, string m) =>
43 callee.Concat($".{m}()");
44
45 public static Query MethodCall(Query callee, string m, Query a1) =>
46 callee.Concat($".{m}(").Concat(a1).Concat(_rparen);
47
48 public static Query MethodCall(Query callee, string m, Query a1, Query a2) =>
49 callee.Concat($".{m}(").Concat(a1).Concat(",").Concat(a2).Concat(_rparen);
50
51 public static Query MethodCall(Query callee, string m, IEnumerable<Query> args) =>
52 Join(args, callee.Concat($".{m}("), ",", _rparen);
53
54 public static Query Join(IEnumerable<Query> ies, Query l, string sep, Query r)
55 {
56 Query ret = l;
57 var init = true;
58 foreach (var ie in ies)
59 {
60 if (init) init = false; else ret = ret.Concat(sep);
61 ret = ret.Concat(ie);
62 }
63 ret = ret.Concat(r);
64 return ret;
65 }
66
67 public static Query CollectionAll(DataContext.Collection col) =>
68 MethodCall(Expr(col.Name), "all");
69
70 public static Query CollectionIndex(DataContext.Index idx) =>
71 MethodCall(Expr(idx.Collection.Name), idx.Name, idx.Args.Select(Const));
72
73 public static QueryExpr Concat(this Query q1, string str)
74 {
75 var frags = new List<IQueryFragment>();
76
77 if (q1 is QueryExpr e1)
78 {
79 if (e1.Fragments.Last() is QueryLiteral l1)
80 {
81 frags.AddRange(e1.Fragments.SkipLast(1));
82 frags.Add(new QueryLiteral(l1.Unwrap + str));
83 }
84 else
85 {
86 frags.AddRange(e1.Fragments);
87 frags.Add(new QueryLiteral(str));
88 }
89 }
90 else
91 {
92 frags.Add(q1);
93 frags.Add(new QueryLiteral(str));
94 }
95
96 return new QueryExpr(frags);
97 }
98
99 public static QueryExpr Concat(this Query q1, Query q2)
100 {
101 var frags = new List<IQueryFragment>();
102
103 if (q1 is QueryExpr e1)
104 {
105 if (q2 is QueryExpr e2)
106 {
107 if (e1.Fragments.Last() is QueryLiteral l1 &&
108 e2.Fragments.First() is QueryLiteral l2)
109 {
110 frags.AddRange(e1.Fragments.SkipLast(1));
111 frags.Add(new QueryLiteral(l1.Unwrap + l2.Unwrap));
112 frags.AddRange(e2.Fragments.Skip(1));
113 }
114 else
115 {
116 frags.AddRange(e1.Fragments);
117 frags.AddRange(e2.Fragments);
118 }
119 }
120 else
121 {
122 frags.AddRange(e1.Fragments);
123 frags.Add(q2);
124 }
125 }
126 else
127 {
128 if (q2 is QueryExpr e2)
129 {
130 frags.Add(q1);
131 frags.AddRange(e2.Fragments);
132 }
133 else
134 {
135 frags.Add(q1);
136 frags.Add(q2);
137 }
138 }
139
140 return new QueryExpr(frags);
141 }
142}