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

execution timeout implemented. nchar and char suport added. #112

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public interface IEFBatchOperationBase<TContext, T> where T : class
/// <param name="items">The items to insert</param>
/// <param name="connection">The DbConnection to use for the insert. Only needed when for example a profiler wraps the connection. Then you need to provide a connection of the type the provider use.</param>
/// <param name="batchSize">The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default</param>
void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null) where TEntity : class, T;
void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null, int? executeTimeout = null) where TEntity : class, T;

IEFBatchOperationFiltered<TContext, T> Where(Expression<Func<T, bool>> predicate);


Expand Down Expand Up @@ -66,7 +67,7 @@ public static IEFBatchOperationBase<TContext, T> For<TContext, T>(TContext conte
return EFBatchOperation<TContext, T>.For(context, set);
}
}
public class EFBatchOperation<TContext, T> : IEFBatchOperationBase<TContext, T>, IEFBatchOperationFiltered<TContext, T>
public class EFBatchOperation<TContext, T> : IEFBatchOperationBase<TContext, T>, IEFBatchOperationFiltered<TContext, T>
where T : class
where TContext : DbContext
{
Expand Down Expand Up @@ -95,7 +96,7 @@ public static IEFBatchOperationBase<TContext, T> For<TContext, T>(TContext conte
/// <param name="items">The items to insert</param>
/// <param name="connection">The DbConnection to use for the insert. Only needed when for example a profiler wraps the connection. Then you need to provide a connection of the type the provider use.</param>
/// <param name="batchSize">The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default</param>
public void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null) where TEntity : class, T
public void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null, int? executeTimeout = null) where TEntity : class, T
{
var con = context.Connection as EntityConnection;
if (con == null && connection == null)
Expand All @@ -117,6 +118,7 @@ public void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connecti
var properties = tableMapping.PropertyMappings
.Where(p => currentType.IsSubclassOf(p.ForEntityType) || p.ForEntityType == currentType)
.Select(p => new ColumnMapping { NameInDatabase = p.ColumnName, NameOnObject = p.PropertyName }).ToList();

if (tableMapping.TPHConfiguration != null)
{
properties.Add(new ColumnMapping
Expand Down Expand Up @@ -157,12 +159,13 @@ public void UpdateAll<TEntity>(IEnumerable<TEntity> items, Action<UpdateSpecific

var properties = tableMapping.PropertyMappings
.Where(p => currentType.IsSubclassOf(p.ForEntityType) || p.ForEntityType == currentType)
.Select(p => new ColumnMapping {
NameInDatabase = p.ColumnName,
NameOnObject = p.PropertyName,
.Select(p => new ColumnMapping
{
NameInDatabase = p.ColumnName,
NameOnObject = p.PropertyName,
DataType = p.DataTypeFull,
IsPrimaryKey = p.IsPrimaryKey
}).ToList();
}).ToList();

var spec = new UpdateSpecification<TEntity>();
updateSpecification(spec);
Expand Down Expand Up @@ -203,7 +206,7 @@ public int Delete()
}
else
{
Configuration.Log("Found provider: " + (provider == null ? "[]" : provider.GetType().Name ) + " for " + con.StoreConnection.GetType().Name);
Configuration.Log("Found provider: " + (provider == null ? "[]" : provider.GetType().Name) + " for " + con.StoreConnection.GetType().Name);
return Fallbacks.DefaultDelete(context, this.predicate);
}
}
Expand Down Expand Up @@ -231,7 +234,7 @@ public int Update<TP>(Expression<Func<T, TP>> prop, Expression<Func<T, TP>> modi
var mqueryInfo = provider.GetQueryInformation<T>(mquery);

var update = provider.GetUpdateQuery(queryInformation, mqueryInfo);

var parameters = query.Parameters
.Concat(mquery.Parameters)
.Select(p => new SqlParameter { Value = p.Value, ParameterName = p.Name })
Expand All @@ -247,6 +250,6 @@ public int Update<TP>(Expression<Func<T, TP>> prop, Expression<Func<T, TP>> modi
}



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,24 @@ public EFDataReader(IEnumerable<T> items, IEnumerable<ColumnMapping> properties)
var temp = info;
foreach (var part in parts.Skip(1))
{
var i = temp.PropertyType.GetProperty(part);
var g = i.GetGetMethod();

var old = getter;
getter = x => g.Invoke(old(x), null);

temp = i;
try
{
var i = temp.PropertyType.GetProperty(part);
var g = i.GetGetMethod();

var old = getter;
getter = x => g.Invoke(old(x), null);

temp = i;
}
catch (Exception ex)
{
throw ex;
}
}



return getter;

}).ToList();
Items = items;
Enumerator = items.GetEnumerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\EntityFramework.6.1.3\lib\net40\EntityFramework.dll</HintPath>
<HintPath>..\..\..\rounds\packages\EntityFramework.6.2.0\lib\net40\EntityFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\EntityFramework.6.1.3\lib\net40\EntityFramework.SqlServer.dll</HintPath>
<HintPath>..\..\..\rounds\packages\EntityFramework.6.2.0\lib\net40\EntityFramework.SqlServer.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ public interface IQueryProvider

string GetDeleteQuery(QueryInformation queryInformation);
string GetUpdateQuery(QueryInformation predicateQueryInfo, QueryInformation modificationQueryInfo);
void InsertItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize);
void UpdateItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize, UpdateSpecification<T> updateSpecification);
void InsertItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize, int? executeTimeout = null);
void UpdateItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize, UpdateSpecification<T> updateSpecification, int? executeTimeout = null);

bool CanHandle(DbConnection storeConnection);


QueryInformation GetQueryInformation<T>(System.Data.Entity.Core.Objects.ObjectQuery<T> query);


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ public EfMapping(DbContext db)

private string GetFullTypeName(ScalarPropertyMapping scalar)
{
if (scalar.Column.TypeName == "nvarchar" || scalar.Column.TypeName == "varchar")
if (scalar.Column.TypeName == "nvarchar" ||
scalar.Column.TypeName == "varchar" ||
scalar.Column.TypeName == "nchar" ||
scalar.Column.TypeName == "char")
{
return string.Format("{0}({1})", scalar.Column.TypeName, scalar.Column.MaxLength);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public string GetUpdateQuery(QueryInformation predicateQueryInfo, QueryInformati
}
else
{
updateSql = string.Join(" = ", update.Split(new string[]{" = "}, StringSplitOptions.RemoveEmptyEntries).Reverse());
updateSql = string.Join(" = ", update.Split(new string[] { " = " }, StringSplitOptions.RemoveEmptyEntries).Reverse());
}


return string.Format("UPDATE [{0}].[{1}] SET {2} {3}", predicateQueryInfo.Schema, predicateQueryInfo.Table, updateSql, predicateQueryInfo.WhereSql);
}

public void InsertItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize)
public void InsertItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize, int? executeTimeout = null)
{
using (var reader = new EFDataReader<T>(items, properties))
{
Expand All @@ -56,9 +56,12 @@ public void InsertItems<T>(IEnumerable<T> items, string schema, string tableName
{
con.Open();
}
using (SqlBulkCopy copy = new SqlBulkCopy(con))

using (SqlBulkCopy copy = new SqlBulkCopy(con, SqlBulkCopyOptions.TableLock, null))
{
copy.BulkCopyTimeout = executeTimeout ?? 600;
copy.BatchSize = Math.Min(reader.RecordsAffected, batchSize ?? 15000); //default batch size

if (!string.IsNullOrWhiteSpace(schema))
{
copy.DestinationTableName = string.Format("[{0}].[{1}]", schema, tableName);
Expand All @@ -67,7 +70,7 @@ public void InsertItems<T>(IEnumerable<T> items, string schema, string tableName
{
copy.DestinationTableName = "[" + tableName + "]";
}

copy.NotifyAfter = 0;

foreach (var i in Enumerable.Range(0, reader.FieldCount))
Expand All @@ -81,7 +84,7 @@ public void InsertItems<T>(IEnumerable<T> items, string schema, string tableName
}


public void UpdateItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize, UpdateSpecification<T> updateSpecification)
public void UpdateItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize, UpdateSpecification<T> updateSpecification, int? executeTimeout = null)
{
var tempTableName = "temp_" + tableName + "_" + DateTime.Now.Ticks;
var columnsToUpdate = updateSpecification.Properties.Select(p => p.GetPropertyName()).ToDictionary(x => x);
Expand All @@ -99,28 +102,31 @@ public void UpdateItems<T>(IEnumerable<T> items, string schema, string tableName

var setters = string.Join(",", filtered.Where(c => !c.IsPrimaryKey).Select(c => "[" + c.NameInDatabase + "] = TEMP.[" + c.NameInDatabase + "]"));
var pks = properties.Where(p => p.IsPrimaryKey).Select(x => "ORIG.[" + x.NameInDatabase + "] = TEMP.[" + x.NameInDatabase + "]");
var filter = string.Join(" and ", pks);
var mergeCommand = string.Format(@"UPDATE [{0}]
var filter = string.Join(" and ", pks);
var mergeCommand = string.Format(@"UPDATE {0}.[{1}]
SET
{3}
{4}
FROM
[{0}] ORIG
{0}.[{1}] ORIG
INNER JOIN
[{1}] TEMP
{0}.[{2}] TEMP
ON
{2}", tableName, tempTableName, filter, setters);
{3}", schema, tableName, tempTableName, filter, setters);

using (var createCommand = new SqlCommand(str, con))
using (var mCommand = new SqlCommand(mergeCommand, con))
using (var dCommand = new SqlCommand(string.Format("DROP table {0}.[{1}]", schema, tempTableName), con))
{
createCommand.CommandTimeout = executeTimeout ?? 600;
mCommand.CommandTimeout = executeTimeout ?? 600;

createCommand.ExecuteNonQuery();
InsertItems(items, schema, tempTableName, filtered, storeConnection, batchSize);
mCommand.ExecuteNonQuery();
dCommand.ExecuteNonQuery();
}


}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.3" targetFramework="net40" />
<package id="EntityFramework" version="6.2.0" targetFramework="net40" />
</packages>