Skip to content
Merged
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
Prev Previous commit
Fixed compilation problems
  • Loading branch information
antonio.anzivino committed Jul 7, 2015
commit 12251277e30ef7cca21f3d79dda1c8f4e4418f6f
121 changes: 99 additions & 22 deletions Source/EntityFramework.Extended/Batch/MySqlBatchRunner.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Data;
using System.Data.Common;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Data.Entity.Core.EntityClient;
using System.Data.Entity.Core.Objects;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Expressions;
Expand All @@ -11,13 +11,14 @@
using EntityFramework.Extensions;
using EntityFramework.Mapping;
using EntityFramework.Reflection;
using System.Threading.Tasks;

namespace EntityFramework.Batch
{
/// <summary>
/// A batch execution runner for MySQL Server.
/// </summary>
public class MySqlBatchRunner : IBatchRunner
public class MySqlBatchRunner : IBatchRunner
{
/// <summary>
/// Create and run a batch delete statement.
Expand All @@ -32,6 +33,38 @@ public class MySqlBatchRunner : IBatchRunner
public int Delete<TEntity>(ObjectContext objectContext, EntityMap entityMap, ObjectQuery<TEntity> query)
where TEntity : class
{
#if NET45
return InternalDelete(objectContext, entityMap, query, false).Result;
#else
return InternalDelete(objectContext, entityMap, query);
#endif
}

#if NET45
/// <summary>
/// Create and run a batch delete statement asynchronously.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="objectContext">The <see cref="ObjectContext"/> to get connection and metadata information from.</param>
/// <param name="entityMap">The <see cref="EntityMap"/> for <typeparamref name="TEntity"/>.</param>
/// <param name="query">The query to create the where clause from.</param>
/// <returns>
/// The number of rows deleted.
/// </returns>
public Task<int> DeleteAsync<TEntity>(ObjectContext objectContext, EntityMap entityMap, ObjectQuery<TEntity> query) where TEntity : class
{
return InternalDelete(objectContext, entityMap, query, true);
}
#endif

#if NET45
private async Task<int> InternalDelete<TEntity>(ObjectContext objectContext, EntityMap entityMap, ObjectQuery<TEntity> query, bool async = false)
where TEntity : class
#else
private int InternalDelete<TEntity>(ObjectContext objectContext, EntityMap entityMap, ObjectQuery<TEntity> query)
where TEntity : class
#endif
{
DbConnection deleteConnection = null;
DbTransaction deleteTransaction = null;
DbCommand deleteCommand = null;
Expand Down Expand Up @@ -67,7 +100,7 @@ public int Delete<TEntity>(ObjectContext objectContext, EntityMap entityMap, Obj

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

sqlBuilder.Append("DELETE j0");
sqlBuilder.Append("DELETE j0");
sqlBuilder.AppendLine();

sqlBuilder.AppendFormat("FROM {0} AS j0 INNER JOIN (", entityMap.TableName);
Expand All @@ -86,9 +119,15 @@ public int Delete<TEntity>(ObjectContext objectContext, EntityMap entityMap, Obj
}
sqlBuilder.Append(")");

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

#if NET45
int result = async
? await deleteCommand.ExecuteNonQueryAsync()
: deleteCommand.ExecuteNonQuery();
#else
int result = deleteCommand.ExecuteNonQuery();
#endif

// only commit if created transaction
if (ownTransaction)
Expand Down Expand Up @@ -120,8 +159,40 @@ public int Delete<TEntity>(ObjectContext objectContext, EntityMap entityMap, Obj
/// <returns>
/// The number of rows updated.
/// </returns>
public int Update<TEntity>(ObjectContext objectContext, EntityMap entityMap, ObjectQuery<TEntity> query, Expression<Func<TEntity, TEntity>> updateExpression)
public int Update<TEntity>(ObjectContext objectContext, EntityMap entityMap, ObjectQuery<TEntity> query, Expression<Func<TEntity, TEntity>> updateExpression) where TEntity : class
{
#if NET45
return InternalUpdate(objectContext, entityMap, query, updateExpression, false).Result;
#else
return InternalUpdate(objectContext, entityMap, query, updateExpression);
#endif
}

#if NET45
/// <summary>
/// Create and run a batch update statement asynchronously.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="objectContext">The <see cref="ObjectContext"/> to get connection and metadata information from.</param>
/// <param name="entityMap">The <see cref="EntityMap"/> for <typeparamref name="TEntity"/>.</param>
/// <param name="query">The query to create the where clause from.</param>
/// <param name="updateExpression">The update expression.</param>
/// <returns>
/// The number of rows updated.
/// </returns>
public Task<int> UpdateAsync<TEntity>(ObjectContext objectContext, EntityMap entityMap, ObjectQuery<TEntity> query, Expression<Func<TEntity, TEntity>> updateExpression) where TEntity : class
{
return InternalUpdate(objectContext, entityMap, query, updateExpression, true);
}
#endif

#if NET45
private async Task<int> InternalUpdate<TEntity>(ObjectContext objectContext, EntityMap entityMap, ObjectQuery<TEntity> query, Expression<Func<TEntity, TEntity>> updateExpression, bool async = false)
where TEntity : class
#else
private int InternalUpdate<TEntity>(ObjectContext objectContext, EntityMap entityMap, ObjectQuery<TEntity> query, Expression<Func<TEntity, TEntity>> updateExpression, bool async = false)
where TEntity : class
#endif
{
DbConnection updateConnection = null;
DbTransaction updateTransaction = null;
Expand Down Expand Up @@ -159,22 +230,22 @@ public int Update<TEntity>(ObjectContext objectContext, EntityMap entityMap, Obj

sqlBuilder.Append("UPDATE ");
sqlBuilder.Append(entityMap.TableName);
sqlBuilder.AppendFormat(" AS j0 INNER JOIN (", entityMap.TableName);
sqlBuilder.AppendLine();
sqlBuilder.AppendLine(innerSelect);
sqlBuilder.Append(") AS j1 ON (");

bool wroteKey = false;
foreach (var keyMap in entityMap.KeyMaps)
{
if (wroteKey)
sqlBuilder.Append(" AND ");

sqlBuilder.AppendFormat("j0.{0} = j1.{0}", keyMap.ColumnName);
wroteKey = true;
}
sqlBuilder.Append(")");
sqlBuilder.AppendLine(" ");
sqlBuilder.AppendFormat(" AS j0 INNER JOIN (", entityMap.TableName);
sqlBuilder.AppendLine();
sqlBuilder.AppendLine(innerSelect);
sqlBuilder.Append(") AS j1 ON (");

bool wroteKey = false;
foreach (var keyMap in entityMap.KeyMaps)
{
if (wroteKey)
sqlBuilder.Append(" AND ");

sqlBuilder.AppendFormat("j0.{0} = j1.{0}", keyMap.ColumnName);
wroteKey = true;
}
sqlBuilder.Append(")");
sqlBuilder.AppendLine(" ");

sqlBuilder.AppendLine(" SET ");

Expand Down Expand Up @@ -297,7 +368,13 @@ public int Update<TEntity>(ObjectContext objectContext, EntityMap entityMap, Obj

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

#if NET45
int result = async
? await updateCommand.ExecuteNonQueryAsync()
: updateCommand.ExecuteNonQuery();
#else
int result = updateCommand.ExecuteNonQuery();
#endif

// only commit if created transaction
if (ownTransaction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="Audit\AuditLog.cs" />
<Compile Include="Audit\AuditLogger.cs" />
<Compile Include="Batch\IBatchRunner.cs" />
<Compile Include="Batch\MySqlBatchRunner.cs" />
<Compile Include="Batch\SqlServerBatchRunner.cs" />
<Compile Include="Caching\CacheExpirationMode.cs" />
<Compile Include="Caching\CacheKey.cs" />
Expand Down