Skip to content

Commit

Permalink
Improve select clause transformation for Linq provider
Browse files Browse the repository at this point in the history
  • Loading branch information
maca88 committed Apr 7, 2019
1 parent e444607 commit a1a0da2
Show file tree
Hide file tree
Showing 13 changed files with 1,637 additions and 145 deletions.
6 changes: 4 additions & 2 deletions src/NHibernate.DomainModel/Northwind/Entities/Animal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class Animal
public virtual Animal Father { get; set; }
public virtual IList<Animal> Children { get; set; }
public virtual string SerialNumber { get; set; }
}
public virtual string FatherSerialNumber => Father?.SerialNumber;
public virtual bool HasFather => Father != null;
}

public abstract class Reptile : Animal
{
Expand All @@ -30,4 +32,4 @@ public abstract class Mammal : Animal
public class Dog : Mammal { }

public class Cat : Mammal { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ public class UserComponent
public string Property1 { get; set; }
public string Property2 { get; set; }
public UserComponent2 OtherComponent { get; set; }

public string Property3 => $"{Property1}{Property2}";

}

public class UserComponent2
{
public string OtherProperty1 { get; set; }

public string OtherProperty2 { get; set; }

public string OtherProperty3 => OtherProperty1;
}
}
}
1 change: 1 addition & 0 deletions src/NHibernate.DomainModel/Northwind/Mappings/User.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

<component name="OtherComponent" class="UserComponent2">
<property name="OtherProperty1" type="AnsiString" />
<property name="OtherProperty2" type="AnsiString" formula="OtherProperty1"/>
</component>
</component>
</class>
Expand Down
474 changes: 457 additions & 17 deletions src/NHibernate.Test/Async/Linq/SelectionTests.cs

Large diffs are not rendered by default.

503 changes: 486 additions & 17 deletions src/NHibernate.Test/Linq/SelectionTests.cs

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions src/NHibernate/Hql/Ast/HqlTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,30 @@ internal HqlIdent(IASTFactory factory, System.Type type)
throw new NotSupportedException(string.Format("Don't currently support idents of type {0}", type.Name));
}
}

internal static bool SupportsType(System.Type type)
{
type = type.UnwrapIfNullable();
switch (System.Type.GetTypeCode(type))
{
case TypeCode.Boolean:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Decimal:
case TypeCode.Single:
case TypeCode.DateTime:
case TypeCode.String:
case TypeCode.Double:
return true;
default:
return new[]
{
typeof(Guid),
typeof(DateTimeOffset)
}.Contains(type);
}
}
}

public class HqlRange : HqlStatement
Expand Down
18 changes: 18 additions & 0 deletions src/NHibernate/Linq/Functions/StringGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Engine;
using NHibernate.Hql.Ast;
using NHibernate.Linq.Visitors;
using NHibernate.Util;
Expand Down Expand Up @@ -70,6 +71,23 @@ public LengthGenerator()

public override HqlTreeNode BuildHql(MemberInfo member, Expression expression, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
if (visitor.SessionFactory is ISessionFactoryImplementor sessionFactory)
{
var sqlFunction = sessionFactory.SQLFunctionRegistry.FindSQLFunction("length");
if (sqlFunction == null)
{
throw new InvalidOperationException("Sql function length is not supported for the current dialect.");
}

var returnType = sqlFunction.ReturnType(NHibernateUtil.Int32, sessionFactory);
if (!NHibernateUtil.Int32.Equals(returnType))
{
return treeBuilder.Cast(
treeBuilder.MethodCall("length", visitor.Visit(expression).AsExpression()),
typeof(int));
}
}

return treeBuilder.MethodCall("length", visitor.Visit(expression).AsExpression());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Linq/NestedSelects/NestedSelectRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private static Expression GetIdentifier(ISessionFactory sessionFactory, Expressi

var classMetadata = sessionFactory.GetClassMetadata(expression.Type);
if (classMetadata == null)
return Expression.Constant(null);
return null;

var propertyName=classMetadata.IdentifierPropertyName;
NHibernate.Type.EmbeddedComponentType componentType;
Expand Down
8 changes: 6 additions & 2 deletions src/NHibernate/Linq/NestedSelects/SelectClauseRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ public SelectClauseRewriter(Expression parameter, ICollection<ExpressionHolder>
this.expressions = expressions;
this.parameter = parameter;
this.tuple = tuple;
this.expressions.Add(new ExpressionHolder { Expression = expression, Tuple = tuple }); //ID placeholder
if (expression != null)
{
this.expressions.Add(new ExpressionHolder { Expression = expression, Tuple = tuple }); //ID placeholder
}

_dictionary = dictionary;
}

Expand Down Expand Up @@ -59,4 +63,4 @@ private Expression AddAndConvertExpression(Expression expression)
expression.Type);
}
}
}
}
Loading

0 comments on commit a1a0da2

Please sign in to comment.