Skip to content

Commit

Permalink
Fix some CodeFactor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
maca88 committed Nov 13, 2019
1 parent 766a403 commit d3557db
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 182 deletions.
1 change: 0 additions & 1 deletion src/NHibernate.Test/Async/Linq/EnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public async Task ConditionalNavigationPropertyAsync()
? (o.NullableEnum1.HasValue ? o.Enum1 : EnumStoredAsString.Unspecified)
: EnumStoredAsString.Small) == EnumStoredAsString.Medium).ToListAsync());


await (db.Users.Where(o => (o.Enum1 == EnumStoredAsString.Large ? o.Role : o.Role).Name == "test").ToListAsync());
}

Expand Down
1 change: 0 additions & 1 deletion src/NHibernate.Test/Linq/EnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public void ConditionalNavigationProperty()
? (o.NullableEnum1.HasValue ? o.Enum1 : EnumStoredAsString.Unspecified)
: EnumStoredAsString.Small) == EnumStoredAsString.Medium).ToList();


db.Users.Where(o => (o.Enum1 == EnumStoredAsString.Large ? o.Role : o.Role).Name == "test").ToList();
}

Expand Down
1 change: 0 additions & 1 deletion src/NHibernate/Hql/Ast/ANTLR/HqlSqlWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public HqlSqlWalker(
string collectionRole)
: this(qti, sfi, input, tokenReplacements, null, collectionRole)
{

}

internal HqlSqlWalker(
Expand Down
54 changes: 26 additions & 28 deletions src/NHibernate/Hql/Ast/ANTLR/Tree/BinaryArithmeticOperatorNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,52 +32,50 @@ public void Initialize()
IType lhType = (lhs is SqlNode) ? ((SqlNode)lhs).DataType : null;
IType rhType = (rhs is SqlNode) ? ((SqlNode)rhs).DataType : null;

if (lhs is IExpectedTypeAwareNode lhsTypeAwareNode && rhType != null && lhsTypeAwareNode.ExpectedType == null)
TrySetExpectedType(lhs, rhType, true);
TrySetExpectedType(rhs, lhType, false);
}

private void TrySetExpectedType(IASTNode operand, IType otherOperandType, bool leftHandOperand)
{
if (!(operand is IExpectedTypeAwareNode typeAwareNode) ||
otherOperandType == null ||
typeAwareNode.ExpectedType != null)
{
IType expectedType;
return;
}

IType expectedType = null;

// we have something like : "? [op] rhs"
if (IsDateTimeType(rhType))
// we have something like : "lhs [op] ?" or "? [op] rhs"
if (IsDateTimeType(otherOperandType))
{
if (leftHandOperand)
{
// more specifically : "? [op] datetime"
// 1) if the operator is MINUS, the param needs to be of
// some datetime type
// 2) if the operator is PLUS, the param needs to be of
// some numeric type
expectedType = Type == HqlSqlWalker.PLUS ? NHibernateUtil.Double : rhType;
expectedType = Type == HqlSqlWalker.PLUS ? NHibernateUtil.Double : otherOperandType;
}
else
{
expectedType = rhType;
}

lhsTypeAwareNode.ExpectedType = expectedType;
}
else if (rhs is IExpectedTypeAwareNode rhsTypeAwareNode && lhType != null && rhsTypeAwareNode.ExpectedType == null)
{
IType expectedType = null;

// we have something like : "lhs [op] ?"
if (IsDateTimeType(lhType))
else if (Type == HqlSqlWalker.PLUS)
{
// more specifically : "datetime [op] ?"
// 1) if the operator is MINUS, we really cannot determine
// the expected type as either another datetime or
// numeric would be valid
// 2) if the operator is PLUS, the param needs to be of
// some numeric type
if (Type == HqlSqlWalker.PLUS)
{
expectedType = NHibernateUtil.Double;
}
}
else
{
expectedType = lhType;
expectedType = NHibernateUtil.Double;
}

rhsTypeAwareNode.ExpectedType = expectedType;
}
else
{
expectedType = otherOperandType;
}

typeAwareNode.ExpectedType = expectedType;
}

public override IType DataType
Expand Down
113 changes: 40 additions & 73 deletions src/NHibernate/Hql/Ast/HqlTreeNode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Hql.Ast.ANTLR;
Expand Down Expand Up @@ -204,6 +205,23 @@ internal HqlQuery(IASTFactory factory, params HqlStatement[] children)

public class HqlIdent : HqlExpression
{
private static readonly Dictionary<TypeCode, string> SupportedIdentTypes = new Dictionary<TypeCode, string>
{
{TypeCode.Boolean, "bool"},
{TypeCode.Int16, "short"},
{TypeCode.Int32, "integer"},
{TypeCode.Int64, "long"},
{TypeCode.UInt16, "ushort"},
{TypeCode.UInt32, "uint"},
{TypeCode.UInt64, "ulong"},
{TypeCode.Decimal, "decimal"},
{TypeCode.Single, "single"},
{TypeCode.DateTime, "datetime"},
{TypeCode.String, "string"},
{TypeCode.Char, "char"},
{TypeCode.Double, "double"}
};

internal HqlIdent(IASTFactory factory, string ident)
: base(HqlSqlWalker.IDENT, ident, factory)
{
Expand All @@ -212,88 +230,37 @@ internal HqlIdent(IASTFactory factory, string ident)
internal HqlIdent(IASTFactory factory, System.Type type)
: base(HqlSqlWalker.IDENT, "", factory)
{
type = type.UnwrapIfNullable();

switch (System.Type.GetTypeCode(type))
if (!TryGetTypeName(type, out var typeName))
{
case TypeCode.Boolean:
SetText("bool");
break;
case TypeCode.Int16:
SetText("short");
break;
case TypeCode.Int32:
SetText("integer");
break;
case TypeCode.Int64:
SetText("long");
break;
case TypeCode.UInt16:
SetText("ushort");
break;
case TypeCode.UInt32:
SetText("uint");
break;
case TypeCode.UInt64:
SetText("ulong");
break;
case TypeCode.Decimal:
SetText("decimal");
break;
case TypeCode.Single:
SetText("single");
break;
case TypeCode.DateTime:
SetText("datetime");
break;
case TypeCode.String:
SetText("string");
break;
case TypeCode.Char:
SetText("char");
break;
case TypeCode.Double:
SetText("double");
break;
default:
if (type == typeof(Guid))
{
SetText("guid");
break;
}
if (type == typeof(DateTimeOffset))
{
SetText("datetimeoffset");
break;
}
throw new NotSupportedException(string.Format("Don't currently support idents of type {0}", type.Name));
throw new NotSupportedException($"Don't currently support idents of type {type.Name}");
}

SetText(typeName);
}

internal static bool SupportsType(System.Type type)
{
return TryGetTypeName(type, out _);
}

private static bool TryGetTypeName(System.Type type, out string typeName)
{
type = type.UnwrapIfNullable();
switch (System.Type.GetTypeCode(type))
if (SupportedIdentTypes.TryGetValue(System.Type.GetTypeCode(type), out typeName))
{
case TypeCode.Boolean:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Decimal:
case TypeCode.Single:
case TypeCode.DateTime:
case TypeCode.String:
case TypeCode.Char:
case TypeCode.Double:
return true;
default:
return
type == typeof(Guid) ||
type == typeof(DateTimeOffset);
return true;
}

if (type == typeof(Guid))
{
typeName = "guid";
}
else if (type == typeof(DateTimeOffset))
{
typeName = "datetimeoffset";
}

return typeName != null;
}
}

Expand Down
105 changes: 27 additions & 78 deletions src/NHibernate/Linq/Visitors/ExpressionKeyVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -800,68 +800,51 @@ public override void VisitOrdering(Ordering ordering, QueryModel queryModel, Ord
public override void VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, int index)
{
_string.Append(" => ");

// Custom visitors for operators that do not expose all information with TransformExpressions method in order to mimic their ToString method
switch (resultOperator)
{
case AllResultOperator allResult:
VisitResultOperator("All", allResult.Predicate);
break;
case AggregateResultOperator aggregateResult:
VisitResultOperator("Aggregate", aggregateResult.Func);
break;
case ContainsResultOperator containsResult:
VisitResultOperator("Contains", containsResult.Item);
break;
case AggregateFromSeedResultOperator aggregateFromSeedResult:
VisitAggregateFromSeedResultOperator(aggregateFromSeedResult);
break;
case CastResultOperator castResult:
VisitTypeChangeOperator("Cast", castResult.CastItemType);
break;
case ConcatResultOperator concatResult:
VisitResultOperator("Concat", concatResult.Source2);
break;
case DefaultIfEmptyResultOperator defaultIfEmptyResult:
VisitDefaultIfEmptyResultOperator(defaultIfEmptyResult);
break;
case ExceptResultOperator exceptResult:
VisitResultOperator("Except", exceptResult.Source2);
break;
case FetchRequestBase fetchBase:
VisitFetchRequestBase(fetchBase);
break;
case ChoiceResultOperatorBase choiceResult:
VisitChoiceResultOperatorBase(choiceResult);
break;
case GroupResultOperator groupResult:
GroupResultOperator(groupResult);
break;
case IntersectResultOperator intersectResult:
VisitResultOperator("Intersect", intersectResult.Source2);
case ChoiceResultOperatorBase operatorBase:
VisitChoiceResultOperatorBase(operatorBase);
break;
case OfTypeResultOperator ofTypeResult:
VisitTypeChangeOperator("OfType", ofTypeResult.SearchedItemType);
break;
case SkipResultOperator skipResult:
VisitResultOperator("Skip", skipResult.Count);
break;
case TakeResultOperator takeResult:
VisitResultOperator("Take", takeResult.Count);
break;
case UnionResultOperator unionResult:
VisitResultOperator("Union", unionResult.Source2);
break;
default:
_string.Append(resultOperator);
VisitResultOperatorBase(resultOperator);
break;
}
}

private void GroupResultOperator(GroupResultOperator resultOperator)
private void VisitResultOperatorBase(ResultOperatorBase resultOperator)
{
_string.Append("GroupBy(");
_keyVisitor.Visit(resultOperator.KeySelector);
_string.Append(',');
_keyVisitor.Visit(resultOperator.ElementSelector);
_string.Append(resultOperator.GetType().Name.Replace("ResultOperator", "("));
var index = 0;
resultOperator.TransformExpressions(
expression =>
{
if (expression == null)
{
return null;
}
if (index > 0)
{
_string.Append(',');
}
_keyVisitor.Visit(expression);
index++;
return expression;
});

_string.Append(')');
}

Expand Down Expand Up @@ -890,32 +873,6 @@ private void VisitFetchRequestBase(FetchRequestBase fetchBase)
}
}

private void VisitDefaultIfEmptyResultOperator(DefaultIfEmptyResultOperator resultOperator)
{
_string.Append("DefaultIfEmpty(");
if (resultOperator.OptionalDefaultValue != null)
{
_keyVisitor.Visit(resultOperator.OptionalDefaultValue);
}

_string.Append(')');
}

private void VisitAggregateFromSeedResultOperator(AggregateFromSeedResultOperator resultOperator)
{
_string.Append("Aggregate(");
_keyVisitor.Visit(resultOperator.Seed);
_string.Append(',');
_keyVisitor.Visit(resultOperator.Func);
if (resultOperator.OptionalResultSelector != null)
{
_string.Append(',');
_keyVisitor.Visit(resultOperator.OptionalResultSelector);
}

_string.Append(')');
}

private void VisitTypeChangeOperator(string name, System.Type type)
{
_string.Append(name);
Expand All @@ -924,14 +881,6 @@ private void VisitTypeChangeOperator(string name, System.Type type)
_string.Append(">()");
}

private void VisitResultOperator(string name, Expression expression)
{
_string.Append(name);
_string.Append("(");
_keyVisitor.Visit(expression);
_string.Append(")");
}

private void VisitJoin(System.Type itemType, string itemName, Expression expression)
{
_string.Append(" join ");
Expand Down

0 comments on commit d3557db

Please sign in to comment.