Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
warning-explosive committed Oct 31, 2024
1 parent 3c207fd commit b72254b
Show file tree
Hide file tree
Showing 28 changed files with 524 additions and 388 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public string Translate(ColumnExpression expression, int depth)
}

sb.Append('"');
sb.Append(expression.Name);
sb.Append(expression.Member.Name);
sb.Append('"');

return sb.ToString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace SpaceEngineers.Core.DataAccess.Orm.Sql.Postgres.Translation
{
using System;
using System.Linq;
using System.Text;
using AutoRegistration.Api.Abstractions;
using AutoRegistration.Api.Attributes;
Expand Down Expand Up @@ -38,7 +39,7 @@ public string Translate(ColumnsChainExpression expression, int depth)
}

sb.Append('"');
sb.Append(expression.Name);
sb.Append(string.Join("_", expression.Members.Select(member => member.Name)));
sb.Append('"');

return sb.ToString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace SpaceEngineers.Core.DataAccess.Orm.Sql.Postgres.Translation
{
using System;
using System.Linq;
using System.Text;
using AutoRegistration.Api.Abstractions;
using AutoRegistration.Api.Attributes;
Expand Down Expand Up @@ -35,7 +36,7 @@ public string Translate(RenameExpression expression, int depth)

sb.Append(" AS ");
sb.Append('"');
sb.Append(expression.Name);
sb.Append(string.Join("_", expression.Members.Select(member => member.Name)));
sb.Append('"');

return sb.ToString();
Expand Down
3 changes: 3 additions & 0 deletions src/Modules/DataAccess.Orm.Sql/DataAccess.Orm.Sql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -414,5 +414,8 @@
<Compile Update="Translation\Expressions\ColumnsChainExpression.cs">
<DependentUpon>ISqlExpression.cs</DependentUpon>
</Compile>
<Compile Update="Translation\RelationsExpressionVisitor.cs">
<DependentUpon>TranslationExpressionVisitor.cs</DependentUpon>
</Compile>
</ItemGroup>
</Project>
72 changes: 22 additions & 50 deletions src/Modules/DataAccess.Orm.Sql/Linq/SqlCommandMaterializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private async IAsyncEnumerable<T> MaterializeInternal<T>(
IDictionary<string, object?> values,
CancellationToken token)
{
var relationValues = InitializeRelations(type, values);
MaterializeRelations(transaction, type, values);

var multipleRelationValues = InitializeMultipleRelations(type, values);

Expand All @@ -96,7 +96,7 @@ private async IAsyncEnumerable<T> MaterializeInternal<T>(
object? built;

if (type.IsDatabaseEntity()
&& TryGetValueFromTransaction(transaction, type, values[nameof(IUniqueIdentified.PrimaryKey)] !, out var stored))
&& TryGetValueFromTransaction(transaction, type, arrangedValues[nameof(IUniqueIdentified.PrimaryKey)] !, out var stored))
{
// TODO: why refill from query results?
_objectBuilder.Fill(type, stored, arrangedValues);
Expand All @@ -109,8 +109,6 @@ private async IAsyncEnumerable<T> MaterializeInternal<T>(

StoreInTransaction(transaction, built);

MaterializeRelations(transaction, built, relationValues);

await MaterializeMultipleRelations(transaction, built, type, multipleRelationValues, token).ConfigureAwait(false);

return built;
Expand Down Expand Up @@ -280,65 +278,39 @@ private static void StoreInTransaction(
}
}

private IReadOnlyDictionary<ColumnInfo, (object? PrimaryKey, IDictionary<string, object?> Values)> InitializeRelations(
private void MaterializeRelations(
IAdvancedDatabaseTransaction transaction,
Type type,
IDictionary<string, object?> values)
{
return _modelProvider
var relations = _modelProvider
.Columns(type)
.Values
.Where(column => column.IsRelation)
.ToDictionary(
column => column,
column =>
{
/*
* initialize database entity with null relation and set it later
*/
var primaryKey = values[column.Name];
values[column.Name] = column.Relation.Target.DefaultValue();
var relationKeys = values
.Select(pair => pair.Key)
.Where(key => key.StartsWith($"{column.Name}_"))
.ToList();
var relationValues = new Dictionary<string, object?>();
.Where(column => column.IsRelation);

foreach (var relationKey in relationKeys)
{
if (values.Remove(relationKey, out var value))
{
var cleanRelationKey = relationKey.Substring(column.Name.Length + 1);
relationValues[cleanRelationKey] = value;
}
}
foreach (var column in relations)
{
var primaryKey = values[column.Name];

return (primaryKey, (IDictionary<string, object?>)relationValues);
});
}
var relationKeys = values
.Select(pair => pair.Key)
.Where(key => key.StartsWith($"{column.Name}_"))
.ToList();

private void MaterializeRelations(
IAdvancedDatabaseTransaction transaction,
object? built,
IReadOnlyDictionary<ColumnInfo, (object?, IDictionary<string, object?>)> relationValues)
{
foreach (var (column, pair) in relationValues)
{
var (primaryKey, arrangedValues) = pair;
var relationValues = new Dictionary<string, object?>();

if (primaryKey is null or DBNull)
foreach (var relationKey in relationKeys)
{
continue;
if (values.Remove(relationKey, out var value))
{
var cleanRelationKey = relationKey.Substring(column.Name.Length + 1);
relationValues[cleanRelationKey] = value;
}
}

var relation = column.Table.IsMtmTable
values[column.Name] = column.Table.IsMtmTable
? primaryKey
: MaterializeRelation(transaction, column.Relation.Target, primaryKey, arrangedValues);

column.Relation.Property.Declared.SetValue(built, relation);
: MaterializeRelation(transaction, column.Relation.Target, primaryKey!, relationValues);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public bool TryVisit(
var parameterExpression = new Expressions.ParameterExpression(itemType, context.NextLambdaParameterName());
var source = new NamedSourceExpression(itemType, context.SqlExpression, parameterExpression);

using (context.OpenParameterScope(parameterExpression))
using (context.OpenParametersScope(parameterExpression))
{
visitor.Visit(methodCallExpression.Arguments[1]);
}
Expand All @@ -54,7 +54,7 @@ public bool TryVisit(
var right = new Expressions.MethodCallExpression(typeof(int), nameof(Queryable.Count), null, new[] { new StarExpression() });
var binaryExpression = new Expressions.BinaryExpression(typeof(bool), BinaryOperator.Equal, left, right);
var parenthesesExpression = new ParenthesesExpression(binaryExpression);
var renameExpression = new RenameExpression(typeof(bool), method.Name, parenthesesExpression);
var renameExpression = new RenameExpression(typeof(bool), new[] { method }, parenthesesExpression);
var projectionExpression = new ProjectionExpression(itemType, source, new[] { renameExpression }, null, null);
context.Remember(projectionExpression);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public bool TryVisit(
var right = new QueryParameterExpression(typeof(int), name);
var binaryExpression = new Expressions.BinaryExpression(typeof(bool), BinaryOperator.GreaterThan, left, right);
var parenthesesExpression = new ParenthesesExpression(binaryExpression);
var renameExpression = new RenameExpression(typeof(bool), method.Name, parenthesesExpression);
var renameExpression = new RenameExpression(typeof(bool), new[] { method }, parenthesesExpression);
var projectionExpression = new ProjectionExpression(itemType, source, new[] { renameExpression }, null, null);
context.Remember(projectionExpression);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public bool TryVisit(
var parameterExpression = new Expressions.ParameterExpression(itemType, context.NextLambdaParameterName());
var source = new NamedSourceExpression(itemType, new ParenthesesExpression(context.SqlExpression), parameterExpression);
var countAllMethodCall = new Expressions.MethodCallExpression(typeof(int), nameof(Queryable.Count), null, new[] { new StarExpression() });
var renameExpression = new RenameExpression(typeof(int), method.Name, countAllMethodCall);
var renameExpression = new RenameExpression(typeof(int), new[] { method }, countAllMethodCall);
var projectionExpression = new ProjectionExpression(itemType, source, new[] { renameExpression }, null, null);
context.Remember(projectionExpression);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,35 @@ namespace SpaceEngineers.Core.DataAccess.Orm.Sql.Translation.Expressions
/// </summary>
public class ColumnExpression : ISqlExpression
{
private readonly string? _nameOverride;

/// <summary> .cctor </summary>
/// <param name="type">Type</param>
/// <param name="member">MemberInfo</param>
/// <param name="source">Source</param>
/// <param name="nameOverride">nameOverride</param>
public ColumnExpression(
Type type,
MemberInfo member,
ISqlExpression source,
string? nameOverride = null)
ISqlExpression source)
{
if (source is not ParameterExpression)
{
throw new ArgumentException($"{nameof(ColumnExpression)} doesn't support {source.GetType().Name} as {nameof(source)} argument");
}

_nameOverride = nameOverride;

Member = member;
Type = type;
Member = member;
Source = source;
}

/// <summary>
/// Name
/// Type
/// </summary>
public string Name => _nameOverride ?? Member.Name;
public Type Type { get; }

/// <summary>
/// Member
/// </summary>
public MemberInfo Member { get; }

/// <summary>
/// Type
/// </summary>
public Type Type { get; }

/// <summary>
/// Source
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ namespace SpaceEngineers.Core.DataAccess.Orm.Sql.Translation.Expressions
using System;
using System.Collections.Generic;
using System.Reflection;
using Basics;

/// <summary>
/// ColumnsChainExpression
Expand All @@ -12,7 +11,7 @@ public class ColumnsChainExpression : ISqlExpression
{
/// <summary> .cctor </summary>
/// <param name="type">Type</param>
/// <param name="members">MemberInfos</param>
/// <param name="members">MemberInfo</param>
/// <param name="source">Source</param>
public ColumnsChainExpression(
Type type,
Expand All @@ -24,26 +23,21 @@ public ColumnsChainExpression(
throw new ArgumentException($"{nameof(ColumnsChainExpression)} doesn't support {source.GetType().Name} as {nameof(source)} argument");
}

Members = members;
Type = type;
Members = members;
Source = source;
}

/// <summary>
/// Name
/// Type
/// </summary>
public string Name => Members.ToString("_", member => member.Name);
public Type Type { get; }

/// <summary>
/// Member
/// Members
/// </summary>
public IReadOnlyCollection<MemberInfo> Members { get; }

/// <summary>
/// Type
/// </summary>
public Type Type { get; }

/// <summary>
/// Source
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public DeleteExpression(
}

/// <summary>
/// Type
/// ItemType
/// </summary>
public Type ItemType { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public FilterExpression(
}

/// <summary>
/// Type
/// ItemType
/// </summary>
public Type ItemType { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public InsertExpression(
}

/// <summary>
/// Type
/// ItemType
/// </summary>
public Type ItemType { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
public class JoinExpression : ISqlExpression
{
/// <summary> .cctor </summary>
/// <param name="itemType">ItemType</param>
/// <param name="leftSource">Left source expression</param>
/// <param name="rightSource">Right source expression</param>
/// <param name="on">On expression</param>
public JoinExpression(
Type itemType,
ISqlExpression leftSource,
ISqlExpression rightSource,
BinaryExpression on)
Expand All @@ -28,11 +30,17 @@ public JoinExpression(
throw new ArgumentException($"{nameof(JoinExpression)} doesn't support {rightSource.GetType().Name} as {nameof(rightSource)} argument");
}

ItemType = itemType;
LeftSource = leftSource;
RightSource = rightSource;
On = on;
}

/// <summary>
/// ItemType
/// </summary>
public Type ItemType { get; }

/// <summary>
/// Left source expression
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ public NamedSourceExpression(
ISqlExpression source,
ParameterExpression parameter)
{
if (source is not JoinExpression
&& source is not OrderByExpression
&& source is not ParenthesesExpression
&& source is not ProjectionExpression
if (source is not ParenthesesExpression
&& source is not QuerySourceExpression)
{
throw new ArgumentException($"{nameof(NamedSourceExpression)} doesn't support {source.GetType().Name} as {nameof(source)} argument");
Expand All @@ -31,7 +28,7 @@ public NamedSourceExpression(
}

/// <summary>
/// Type
/// ItemType
/// </summary>
public Type ItemType { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public NewExpression(Type itemType, IReadOnlyCollection<ISqlExpression> paramete
}

/// <summary>
/// Type
/// ItemType
/// </summary>
public Type ItemType { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ProjectionExpression(
}

/// <summary>
/// Type
/// ItemType
/// </summary>
public Type ItemType { get; }

Expand Down
Loading

0 comments on commit b72254b

Please sign in to comment.