-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
170 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,7 @@ public async Task ExecuteAsync(EdgeDBClient client) | |
private static async Task QueryBuilderDemo(EdgeDBClient client) | ||
{ | ||
// Selecting a type with autogen shape | ||
var query = QueryBuilder.Select<Person>().Build().Prettify(); | ||
var query = QueryBuilder.Select<Person>().Compile().Prettify(); | ||
|
||
// Adding a filter, orderby, offset, and limit | ||
query = QueryBuilder | ||
|
@@ -52,19 +52,19 @@ private static async Task QueryBuilderDemo(EdgeDBClient client) | |
.OrderByDesending(x => x.Name) | ||
.Offset(2) | ||
.Limit(10) | ||
.Build() | ||
.Compile() | ||
.Prettify(); | ||
|
||
// Specifying a shape | ||
query = QueryBuilder.Select<Person>(shape => | ||
shape | ||
.Explicitly(p => new { p.Name, p.Email, p.BestFriend }) | ||
).Build().Prettify(); | ||
).Compile().Prettify(); | ||
|
||
// selecting things that are not types | ||
query = QueryBuilder.SelectExpression(() => | ||
EdgeQL.Count(QueryBuilder.Select<Person>()) | ||
).Build().Prettify(); | ||
).Compile().Prettify(); | ||
|
||
// selecting 'free objects' | ||
query = QueryBuilder.SelectExpression(ctx => new | ||
|
@@ -73,7 +73,7 @@ private static async Task QueryBuilderDemo(EdgeDBClient client) | |
MyNumber = 42, | ||
SeveralNumbers = new long[] { 1, 2, 3 }, | ||
People = ctx.SubQuery(QueryBuilder.Select<Person>()) | ||
}).Build().Prettify(); | ||
}).Compile().Prettify(); | ||
|
||
// Backlinks | ||
query = QueryBuilder.Select<Person>(shape => shape | ||
|
@@ -85,43 +85,43 @@ private static async Task QueryBuilderDemo(EdgeDBClient client) | |
// you can pass in a string instead of an expression to select out a 'EdgeDBObject' type. | ||
ReferencedFriends = ctx.BackLink<Person>(x => x.Friends) | ||
}) | ||
).Build().Prettify(); | ||
).Compile().Prettify(); | ||
|
||
// With object variables | ||
query = QueryBuilder | ||
.With(new { Args = EdgeQL.AsJson(new { Name = "Example", Email = "[email protected]" }) }) | ||
.SelectExpression(ctx => new | ||
{ | ||
PassedName = ctx.Variables.Args.Value.Name, PassedEmail = ctx.Variables.Args.Value.Email | ||
}).Build().Prettify(); | ||
}).Compile().Prettify(); | ||
|
||
// Inserting a new type | ||
var person = new Person { Email = "[email protected]", Name = "example" }; | ||
|
||
query = QueryBuilder.Insert(person).Build().Prettify(); | ||
query = QueryBuilder.Insert(person).Compile().Prettify(); | ||
|
||
// Complex insert with links & dealing with conflicts | ||
query = (await QueryBuilder | ||
.Insert(new Person { BestFriend = person, Name = "example2", Email = "[email protected]" }) | ||
.UnlessConflict() | ||
.ElseReturn() | ||
.BuildAsync(client)) | ||
.CompileAsync(client)) | ||
.Prettify(); | ||
|
||
// Manual conflicts | ||
query = QueryBuilder | ||
.Insert(person) | ||
.UnlessConflictOn(x => x.Email) | ||
.ElseReturn() | ||
.Build() | ||
.Compile() | ||
.Prettify(); | ||
|
||
// Autogenerating unless conflict with introspection | ||
query = (await QueryBuilder | ||
.Insert(person) | ||
.UnlessConflict() | ||
.ElseReturn() | ||
.BuildAsync(client)) | ||
.CompileAsync(client)) | ||
.Prettify(); | ||
|
||
// Bulk inserts | ||
|
@@ -138,7 +138,7 @@ private static async Task QueryBuilderDemo(EdgeDBClient client) | |
|
||
var tquery = await QueryBuilder.For(data, | ||
x => QueryBuilder.Insert(x) | ||
).BuildAsync(client); | ||
).CompileAsync(client); | ||
|
||
// Else statements (upsert demo) | ||
query = (await QueryBuilder | ||
|
@@ -147,21 +147,21 @@ private static async Task QueryBuilderDemo(EdgeDBClient client) | |
.Else(q => | ||
q.Update(old => new Person { Name = old!.Name!.ToLower() }) | ||
) | ||
.BuildAsync(client)) | ||
.CompileAsync(client)) | ||
.Prettify(); | ||
|
||
// Updating a type | ||
query = QueryBuilder | ||
.Update<Person>(old => new Person { Name = "example new name" }) | ||
.Filter(x => x.Email == "[email protected]") | ||
.Build() | ||
.Compile() | ||
.Prettify(); | ||
|
||
// Deleting types | ||
query = QueryBuilder | ||
.Delete<Person>() | ||
.Filter(x => EdgeQL.ILike(x.Name, "e%")) | ||
.Build() | ||
.Compile() | ||
.Prettify(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.Collections.Immutable; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace EdgeDB; | ||
|
||
public class CompiledQuery(string query, Dictionary<string, object?>? variables) | ||
{ | ||
public string Query { get; } = query; | ||
|
||
public IReadOnlyDictionary<string, object?>? Variables { get; } | ||
= variables?.ToImmutableDictionary(); | ||
|
||
internal readonly Dictionary<string, object?>? RawVariables = variables; | ||
|
||
/// <summary> | ||
/// Prettifies the query text. | ||
/// </summary> | ||
/// <remarks> | ||
/// This method uses a lot of regex and can be unreliable, if | ||
/// you're using this in a production setting please use with care. | ||
/// </remarks> | ||
/// <returns>A prettified version of <see cref="Query"/>.</returns> | ||
public string Prettify() | ||
{ | ||
// add newlines | ||
var result = Regex.Replace(Query, @"({|\(|\)|}|,)", m => | ||
{ | ||
switch (m.Groups[1].Value) | ||
{ | ||
case "{" or "(" or ",": | ||
if (m.Groups[1].Value == "{" && Query[m.Index + 1] == '}') | ||
return m.Groups[1].Value; | ||
|
||
return $"{m.Groups[1].Value}\n"; | ||
|
||
default: | ||
return $"{((m.Groups[1].Value == "}" && (Query[m.Index - 1] == '{' || Query[m.Index - 1] == '}')) ? "" : "\n")}{m.Groups[1].Value}{((Query.Length != m.Index + 1 && (Query[m.Index + 1] != ',')) ? "\n" : "")}"; | ||
} | ||
}).Trim().Replace("\n ", "\n"); | ||
|
||
// clean up newline func | ||
result = Regex.Replace(result, "\n\n", m => "\n"); | ||
|
||
// add indentation | ||
result = Regex.Replace(result, "^", m => | ||
{ | ||
int indent = 0; | ||
|
||
foreach (var c in result[..m.Index]) | ||
{ | ||
if (c is '(' or '{') | ||
indent++; | ||
if (c is ')' or '}') | ||
indent--; | ||
} | ||
|
||
var next = result.Length != m.Index ? result[m.Index] : '\0'; | ||
|
||
if (next is '}' or ')') | ||
indent--; | ||
|
||
return "".PadLeft(indent * 2); | ||
}, RegexOptions.Multiline); | ||
|
||
return result; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/EdgeDB.Net.QueryBuilder/Compiled/DebugCompiledQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace EdgeDB.Compiled; | ||
|
||
public sealed class DebugCompiledQuery : CompiledQuery | ||
{ | ||
|
||
|
||
internal DebugCompiledQuery(string query, Dictionary<string, object?> variablesInternal, QueryWriter writer) | ||
: base(query, variablesInternal) | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.