Skip to content

Commit

Permalink
Merge pull request #718 from SimonCropp/redundantEquality
Browse files Browse the repository at this point in the history
remove some redundant equality checks
  • Loading branch information
mikependon authored Jan 13, 2021
2 parents a3f36af + bc7f263 commit 7206c4f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 43 deletions.
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/TraceLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected internal TraceLog(Guid sessionId,
Statement = statement;
Parameter = parameter;
Result = result;
if (executionTime != null && executionTime.HasValue)
if (executionTime != null)
{
ExecutionTime = executionTime.Value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,15 @@ private static class PropertyGetterFuncCache<TEntity, TResult>
/// <returns></returns>
public static Func<TEntity, TResult> GetFunc(ClassProperty classProperty)
{
var func = (Func<TEntity, TResult>)null;
if (cache.TryGetValue(classProperty.GetHashCode(), out func) == false)
if (cache.TryGetValue(classProperty.GetHashCode(), out var func) == false)
{
if (classProperty != null)
{
var typeOfEntity = typeof(TEntity);
var entity = Expression.Parameter(typeOfEntity, "entity");
var body = Expression.Convert(Expression.Call(entity, classProperty.PropertyInfo.GetMethod), typeof(TResult));
var typeOfEntity = typeof(TEntity);
var entity = Expression.Parameter(typeOfEntity, "entity");
var body = Expression.Convert(Expression.Call(entity, classProperty.PropertyInfo.GetMethod), typeof(TResult));

func = Expression
.Lambda<Func<TEntity, TResult>>(body, entity)
.Compile();
}
func = Expression
.Lambda<Func<TEntity, TResult>>(body, entity)
.Compile();

cache.TryAdd(classProperty.GetHashCode(), func);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,31 +263,28 @@ public override string CreateInsertAll(QueryBuilder queryBuilder,
}
}

if (identityField != null)
{
// Variables needed
var commandTexts = new List<string>();
var splitted = commandText.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
// Variables needed
var commandTexts = new List<string>();
var splitted = commandText.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

// Iterate the indexes
for (var index = 0; index < splitted.Length; index++)
{
var line = splitted[index].Trim();

// Set the return value
var returnValue = identityField != null ?
string.IsNullOrEmpty(databaseType) ?
identityField.Name.AsQuoted(DbSetting) :
string.Concat($"CAST({identityField.Name.AsQuoted(DbSetting)} AS {databaseType})") :
primaryField != null ? primaryField.Name.AsQuoted(DbSetting) : "NULL";
commandTexts.Add(string.Concat(line, " RETURNING ", returnValue, " AS ", "Id".AsQuoted(DbSetting), ", ",
$"{DbSetting.ParameterPrefix}__RepoDb_OrderColumn_{index} AS ", "OrderColumn".AsQuoted(DbSetting), " ;"));
}
// Iterate the indexes
for (var index = 0; index < splitted.Length; index++)
{
var line = splitted[index].Trim();

// Set the command text
commandText = commandTexts.Join(" ");
// Set the return value
var returnValue = identityField != null ?
string.IsNullOrEmpty(databaseType) ?
identityField.Name.AsQuoted(DbSetting) :
string.Concat($"CAST({identityField.Name.AsQuoted(DbSetting)} AS {databaseType})") :
primaryField != null ? primaryField.Name.AsQuoted(DbSetting) : "NULL";
commandTexts.Add(string.Concat(line, " RETURNING ", returnValue, " AS ", "Id".AsQuoted(DbSetting), ", ",
$"{DbSetting.ParameterPrefix}__RepoDb_OrderColumn_{index} AS ", "OrderColumn".AsQuoted(DbSetting), " ;"));
}

// Set the command text
commandText = commandTexts.Join(" ");

// Return the query
return commandText;
}
Expand Down Expand Up @@ -399,11 +396,9 @@ public override string CreateMerge(QueryBuilder queryBuilder,
}

// Set the return value
var result = identityField != null ?
string.IsNullOrEmpty(databaseType) ?
identityField.Name.AsQuoted(DbSetting) :
string.Concat($"CAST({identityField.Name.AsQuoted(DbSetting)} AS {databaseType})") :
primaryField != null ? primaryField.Name.AsParameter(DbSetting) : "NULL";
var result = identityField == null ? primaryField.Name.AsParameter(DbSetting) :
string.IsNullOrEmpty(databaseType) ? identityField.Name.AsQuoted(DbSetting) :
string.Concat($"CAST({identityField.Name.AsQuoted(DbSetting)} AS {databaseType})");

if (!string.IsNullOrEmpty(result))
{
Expand Down Expand Up @@ -505,11 +500,9 @@ public override string CreateMergeAll(QueryBuilder queryBuilder,
}

// Set the return value
var result = identityField != null ?
string.IsNullOrEmpty(databaseType) ?
identityField.Name.AsQuoted(DbSetting) :
string.Concat($"CAST({identityField.Name.AsQuoted(DbSetting)} AS {databaseType})") :
primaryField != null ? primaryField.Name.AsParameter(DbSetting) : "NULL";
var result = identityField == null ? primaryField.Name.AsParameter(DbSetting) :
string.IsNullOrEmpty(databaseType) ? identityField.Name.AsQuoted(DbSetting) :
string.Concat($"CAST({identityField.Name.AsQuoted(DbSetting)} AS {databaseType})");

// Clear the builder
builder.Clear();
Expand Down

0 comments on commit 7206c4f

Please sign in to comment.