Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OrderBy to queries #5

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .idea/.idea.Circles/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Circles.Index/Data/DbIdentifierAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ public static string GetIdentifier(this Enum enumValue)
throw new InvalidOperationException($"Enum value {enumValue} (type: {type}) does not have a name.");
}
var field = type.GetField(name);
if (field == null)
{
throw new InvalidOperationException($"Enum {type} does not have a field named {name}.");
}
var attribute = Attribute.GetCustomAttribute(field, typeof(DbIdentifierAttribute)) as DbIdentifierAttribute;
var dbName = attribute?.Identifier ??
throw new InvalidOperationException(
$"Enum value {enumValue} does not have a DbIdentifierAttribute.");

// Escape reserved keywords
// TODO: Replace with a more robust solution
if (dbName.ToLower() == "limit")
{
return "\"limit\"";
Expand Down
11 changes: 0 additions & 11 deletions Circles.Index/Data/Model/CirclesHubTransferDto.cs

This file was deleted.

19 changes: 0 additions & 19 deletions Circles.Index/Data/Model/CirclesHubTransferQuery.cs

This file was deleted.

10 changes: 0 additions & 10 deletions Circles.Index/Data/Model/CirclesSignupDto.cs

This file was deleted.

12 changes: 0 additions & 12 deletions Circles.Index/Data/Model/CirclesSignupQuery.cs

This file was deleted.

12 changes: 0 additions & 12 deletions Circles.Index/Data/Model/CirclesTransferDto.cs

This file was deleted.

14 changes: 0 additions & 14 deletions Circles.Index/Data/Model/CirclesTransferQuery.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Circles.Index/Data/Model/CirclesTrustDto.cs

This file was deleted.

13 changes: 0 additions & 13 deletions Circles.Index/Data/Model/CirclesTrustQuery.cs

This file was deleted.

7 changes: 0 additions & 7 deletions Circles.Index/Data/Model/SortOrder.cs

This file was deleted.

54 changes: 0 additions & 54 deletions Circles.Index/Data/Postgresql/CursorUtils.cs

This file was deleted.

6 changes: 3 additions & 3 deletions Circles.Index/Data/Query/Equals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public class Equals : IQuery
public readonly Tables Table;
public readonly Columns Column;
private readonly string _parameterName;
public readonly object Value;
public readonly object? Value;
private readonly DbProviderFactory _provider;

internal Equals(DbProviderFactory provider, Tables table, Columns column, object value)
internal Equals(DbProviderFactory provider, Tables table, Columns column, object? value)
{
_provider = provider;
Table = table;
Expand All @@ -34,7 +34,7 @@ public IEnumerable<IDataParameter> GetParameters()

parameter.ParameterName = _parameterName;
var targetType = Schema.TableSchemas[Table].Columns.First(o => o.Column == Column).Type;
parameter.Value = Query.Convert(Value, targetType);
parameter.Value = Query.Convert(Value, targetType) ?? DBNull.Value;
yield return parameter;
}
}
16 changes: 13 additions & 3 deletions Circles.Index/Data/Query/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ public static IEnumerable<object[]> Execute(DbConnection connection, IQuery quer
}
}

public static object Convert(object input, ValueTypes target)
public static object? Convert(object? input, ValueTypes target)
{
if (input == null)
{
return null;
}
switch (target)
{
case ValueTypes.String:
Expand All @@ -94,10 +98,16 @@ public static object Convert(object input, ValueTypes target)
case ValueTypes.Address when input is string i:
return i.ToLowerInvariant();
case ValueTypes.Address:
return input.ToString().ToLowerInvariant();
return input.ToString()?.ToLowerInvariant();
case ValueTypes.Boolean when input is bool b:
return b;
case ValueTypes.Boolean when input is string s:
return bool.Parse(s);
case ValueTypes.Boolean when input is int i:
return i != 0;
default:
throw new ArgumentOutOfRangeException(nameof(target), target,
$"Input was {input} of type {input.GetType()}");
$"Cannot convert input {input} (type: {input.GetType()?.Name ?? "<null>"}) to target type {target}.");
}
}
}
9 changes: 6 additions & 3 deletions Circles.Index/Data/Query/Select.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class Select : IQuery
public readonly IEnumerable<Columns> Columns;
private readonly List<string> _fields;
public readonly List<IQuery> Conditions;
public readonly List<(Columns Column, SortOrder Order)> OrderBy = new();

public Select(Tables table, IEnumerable<Columns> columns)
{
Expand All @@ -35,6 +36,11 @@ public string ToSql()
sql.Append(" WHERE ");
sql.Append(string.Join(" AND ", Conditions.Select(c => c.ToSql())));
}
if (OrderBy.Any())
{
sql.Append(" ORDER BY ");
sql.Append(string.Join(", ", OrderBy.Select(o => $"{o.Column.GetIdentifier()} {o.Order}")));
}

return sql.ToString();
}
Expand All @@ -50,9 +56,6 @@ public IEnumerable<IDataParameter> GetParameters()
}
}

public IQuery And(IQuery other) => Query.And(this, other);
public IQuery Or(IQuery other) => Query.Or(this, other);

public override string ToString()
{
StringBuilder sb = new();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Circles.Index.Data.Model;
namespace Circles.Index.Data;

public class Range<T>
{
Expand Down
7 changes: 7 additions & 0 deletions Circles.Index/Data/SortOrder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Circles.Index.Data;

public enum SortOrder
{
Asc,
Desc
}
1 change: 0 additions & 1 deletion Circles.Index/Indexer/BlockIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Nethermind.Blockchain;
using Nethermind.Blockchain.Receipts;
using Nethermind.Core;
using Circles.Index.Data.Model;
using Nethermind.Core.Crypto;

namespace Circles.Index.Indexer;
Expand Down
1 change: 0 additions & 1 deletion Circles.Index/Indexer/StateMachine.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Circles.Index.Data;
using Circles.Index.Data.Model;
using Circles.Index.Data.Postgresql;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Receipts;
Expand Down
31 changes: 25 additions & 6 deletions Circles.Index/Rpc/CirclesRpcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,17 @@ public ResultWrapper<IEnumerable<object[]>> circles_query(CirclesQuery query)
using NpgsqlConnection connection = new(_indexConnectionString);
connection.Open();

if (query.Table == null)
{
throw new InvalidOperationException("Table is null");
}

Tables parsedTableName = Enum.Parse<Tables>(query.Table);

var select = Query.Select(parsedTableName,
query.Columns?.Select(c => Enum.Parse<Columns>(c))
query.Columns?.Select(Enum.Parse<Columns>)
?? throw new InvalidOperationException("Columns are null"));

Console.WriteLine(select.ToString());

if (query.Conditions.Any())
{
foreach (var condition in query.Conditions)
Expand All @@ -148,9 +151,25 @@ public ResultWrapper<IEnumerable<object[]>> circles_query(CirclesQuery query)
}
}

Console.WriteLine(select.ToString());

if (query.OrderBy.Any())
{
foreach (var orderBy in query.OrderBy)
{
if (orderBy.Column == null || orderBy.SortOrder == null)
{
throw new InvalidOperationException("OrderBy: Column or SortOrder is null");
}

Columns parsedColumnName = Enum.Parse<Columns>(orderBy.Column);
select.OrderBy.Add((
parsedColumnName,
orderBy.SortOrder.ToLowerInvariant() == "asc"
? SortOrder.Asc
: SortOrder.Desc));
}
}

Console.WriteLine(select.ToString());
var result = Query.Execute(connection, select).ToList();

return ResultWrapper<IEnumerable<object[]>>.Success(result);
Expand Down
10 changes: 9 additions & 1 deletion Circles.Index/Rpc/ICirclesRpcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@ public CirclesTokenBalance(Address token, string balance)

public class CirclesQuery
{
public string Table { get; set; }
public string? Table { get; set; }
public string[]? Columns { get; set; }
public List<Expression> Conditions { get; set; } = new();

public List<OrderBy> OrderBy { get; set; } = new();
}

public class OrderBy
{
public string? Column { get; set; }
public string? SortOrder { get; set; }
}

public class Expression
Expand Down
Loading