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

Escape in runner, not in mapping provider #171

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 8 additions & 3 deletions Source/EntityFramework.Extended/Batch/MySqlBatchRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private int InternalDelete<TEntity>(ObjectContext objectContext, EntityMap entit
sqlBuilder.Append("DELETE j0");
sqlBuilder.AppendLine();

sqlBuilder.AppendFormat("FROM {0} AS j0 INNER JOIN (", entityMap.TableName);
sqlBuilder.AppendFormat("FROM {0} AS j0 INNER JOIN (", QuoteIdentifier(entityMap.TableName));
sqlBuilder.AppendLine();
sqlBuilder.AppendLine(innerSelect);
sqlBuilder.Append(") AS j1 ON (");
Expand All @@ -119,7 +119,7 @@ private int InternalDelete<TEntity>(ObjectContext objectContext, EntityMap entit
}
sqlBuilder.Append(")");

deleteCommand.CommandText = sqlBuilder.ToString().Replace("[", "").Replace("]", "");
deleteCommand.CommandText = sqlBuilder.ToString().Replace("[", "`").Replace("]", "`");

#if NET45
int result = async
Expand Down Expand Up @@ -230,7 +230,7 @@ private int InternalUpdate<TEntity>(ObjectContext objectContext, EntityMap entit

sqlBuilder.Append("UPDATE ");
sqlBuilder.Append(entityMap.TableName);
sqlBuilder.AppendFormat(" AS j0 INNER JOIN (", entityMap.TableName);
sqlBuilder.AppendFormat(" AS j0 INNER JOIN (", QuoteIdentifier(entityMap.TableName));
sqlBuilder.AppendLine();
sqlBuilder.AppendLine(innerSelect);
sqlBuilder.Append(") AS j1 ON (");
Expand Down Expand Up @@ -449,5 +449,10 @@ private static string GetSelectSql<TEntity>(ObjectQuery<TEntity> query, EntityMa

return innerJoinSql;
}

private static string QuoteIdentifier(string name)
{
return ("`" + name + "`");
}
}
}
34 changes: 30 additions & 4 deletions Source/EntityFramework.Extended/Batch/SqlServerBatchRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,24 @@ private int InternalDelete<TEntity>(ObjectContext objectContext, EntityMap entit

var innerSelect = GetSelectSql(query, entityMap, deleteCommand);

string tableName;
if (entityMap.SchemaName != null)
{
tableName = new StringBuilder(50)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to simple + operator. See comment below

.Append(QuoteIdentifier(entityMap.SchemaName))
.Append('.')
.Append(QuoteIdentifier(entityMap.TableName)).ToString();
}
else
tableName = QuoteIdentifier(entityMap.TableName);

var sqlBuilder = new StringBuilder(innerSelect.Length * 2);

sqlBuilder.Append("DELETE ");
sqlBuilder.Append(entityMap.TableName);
sqlBuilder.Append(tableName);
sqlBuilder.AppendLine();

sqlBuilder.AppendFormat("FROM {0} AS j0 INNER JOIN (", entityMap.TableName);
sqlBuilder.AppendFormat("FROM {0} AS j0 INNER JOIN (", tableName);
sqlBuilder.AppendLine();
sqlBuilder.AppendLine(innerSelect);
sqlBuilder.Append(") AS j1 ON (");
Expand Down Expand Up @@ -223,11 +234,21 @@ private int InternalUpdate<TEntity>(ObjectContext objectContext, EntityMap entit
if (objectContext.CommandTimeout.HasValue)
updateCommand.CommandTimeout = objectContext.CommandTimeout.Value;

string tableName;
if (entityMap.SchemaName != null)
{
tableName = new StringBuilder(50)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not what StringBuilder is for... you use it when you have multiple varying things to concatenate in a loop.

In this case, much better to simply do the concatenation by simple + operator which C# compiler will compile as a String.Concat call.

.Append(QuoteIdentifier(entityMap.SchemaName))
.Append('.')
.Append(QuoteIdentifier(entityMap.TableName)).ToString();
}
else
tableName = QuoteIdentifier(entityMap.TableName);
var innerSelect = GetSelectSql(query, entityMap, updateCommand);
var sqlBuilder = new StringBuilder(innerSelect.Length * 2);

sqlBuilder.Append("UPDATE ");
sqlBuilder.Append(entityMap.TableName);
sqlBuilder.Append(tableName);
sqlBuilder.AppendLine(" SET ");

var memberInitExpression = updateExpression.Body as MemberInitExpression;
Expand Down Expand Up @@ -347,7 +368,7 @@ private int InternalUpdate<TEntity>(ObjectContext objectContext, EntityMap entit
}

sqlBuilder.AppendLine(" ");
sqlBuilder.AppendFormat("FROM {0} AS j0 INNER JOIN (", entityMap.TableName);
sqlBuilder.AppendFormat("FROM {0} AS j0 INNER JOIN (", tableName);
sqlBuilder.AppendLine();
sqlBuilder.AppendLine(innerSelect);
sqlBuilder.Append(") AS j1 ON (");
Expand Down Expand Up @@ -449,5 +470,10 @@ private static string GetSelectSql<TEntity>(ObjectQuery<TEntity> query, EntityMa

return innerJoinSql;
}

private static string QuoteIdentifier(string name)
{
return ("[" + name.Replace("]", "]]") + "]");
}
}
}
8 changes: 8 additions & 0 deletions Source/EntityFramework.Extended/Mapping/EntityMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public Type EntityType
/// </value>
public string TableName { get; set; }

/// <summary>
/// Gets or sets the name of the schema
/// </summary>
/// <value>
/// The name of the schema (usually `dbo` on MS SQL Server)
/// </value>
public string SchemaName { get; set; }

private readonly List<PropertyMap> _propertyMaps;
/// <summary>
/// Gets the property maps.
Expand Down
21 changes: 5 additions & 16 deletions Source/EntityFramework.Extended/Mapping/MetadataMappingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public EntityMap GetEntityMap(Type type, ObjectContext objectContext)
var entityMap = new EntityMap(type);
var metadata = objectContext.MetadataWorkspace;

// Get the part of the model that contains info about the actual CLR types
var objectItemCollection = ((ObjectItemCollection)metadata.GetItemCollection(DataSpace.OSpace));

var entityType = metadata.GetItems<EntityType>(DataSpace.OSpace).Single(e => objectItemCollection.GetClrType(e) == type);

var entitySet = metadata.GetItems<EntityContainer>(DataSpace.CSpace)
Expand Down Expand Up @@ -186,8 +188,6 @@ private static void SetProperties(EntityMap entityMap, EntitySetMapping mapping,

private static void SetTableName(EntityMap entityMap)
{
var builder = new StringBuilder(50);

EntitySet storeSet = entityMap.StoreSet;

string table = null;
Expand Down Expand Up @@ -226,20 +226,9 @@ private static void SetTableName(EntityMap entityMap)
schema = schemaProperty.Value as string;
}

if (!string.IsNullOrWhiteSpace(schema))
{
builder.Append(QuoteIdentifier(schema));
builder.Append(".");
}

builder.Append(QuoteIdentifier(table));

entityMap.TableName = builder.ToString();
entityMap.SchemaName = schema;
entityMap.TableName = table;
}

private static string QuoteIdentifier(string name)
{
return ("[" + name.Replace("]", "]]") + "]");
}
}
}
}