Skip to content

Commit e5873d0

Browse files
author
zzzprojects
committed
Fix MySQL Schema + Fix Cast + Fix Null Value + Fix Future and Effort
Fix MySQL Schema + Fix Cast + Fix Null Value + Fix Future and Effort
1 parent 07af0ca commit e5873d0

File tree

8 files changed

+98
-11
lines changed

8 files changed

+98
-11
lines changed

src/shared/Z.EF.Plus.BatchDelete.Shared/BatchDelete.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,8 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
380380
// GET command
381381
var command = query.Context.CreateStoreCommand();
382382

383-
bool isPostgreSql = command.GetType().Name == "NpgsqlCommand";
384-
bool isMySql = command.GetType().FullName.Contains("MySql");
383+
var isPostgreSql = command.GetType().Name == "NpgsqlCommand";
384+
var isMySql = command.GetType().FullName.Contains("MySql");
385385
var isSqlCe = command.GetType().Name == "SqlCeCommand";
386386
var isOracle = command.GetType().Namespace.Contains("Oracle");
387387
var isSQLite = command.GetType().Namespace.Contains("SQLite");
@@ -404,7 +404,9 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
404404

405405
if (isMySql)
406406
{
407-
tableName = string.Concat("`", store.Table, "`");
407+
tableName = string.IsNullOrEmpty(store.Schema) || store.Schema == "dbo" ?
408+
string.Concat("`", store.Table, "`") :
409+
string.Concat("`", store.Schema, ".", store.Table, "`");
408410
}
409411
else if (isSqlCe)
410412
{
@@ -420,6 +422,12 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
420422
string.Concat("\"", store.Table, "\"") :
421423
string.Concat("\"", store.Schema, "\".\"", store.Table, "\"");
422424
}
425+
else if (isPostgreSql)
426+
{
427+
tableName = string.IsNullOrEmpty(store.Schema) ?
428+
string.Concat("\"", store.Table, "\"") :
429+
string.Concat("\"", store.Schema, "\".\"", store.Table, "\"");
430+
}
423431
else
424432
{
425433
tableName = string.IsNullOrEmpty(store.Schema) ?

src/shared/Z.EF.Plus.BatchUpdate.Shared/BatchUpdate.cs

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ namespace Z.EntityFramework.Plus
3939
/// <summary>Class to batch delete.</summary>
4040
public class BatchUpdate
4141
{
42+
public class NullValue
43+
{
44+
public Type Type;
45+
}
46+
4247
/// <summary>The command text template.</summary>
4348
internal const string CommandTextTemplate = @"
4449
UPDATE A {Hint}
@@ -380,7 +385,9 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
380385

381386
if (isMySql)
382387
{
383-
tableName = string.Concat("`", store.Table, "`");
388+
tableName = string.IsNullOrEmpty(store.Schema) || store.Schema == "dbo" ?
389+
string.Concat("`", store.Table, "`") :
390+
string.Concat("`", store.Schema, ".", store.Table, "`");
384391
}
385392
else if (isSqlCe)
386393
{
@@ -533,12 +540,28 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
533540
534541
var parameter = command.CreateParameter();
535542
parameter.ParameterName = parameterPrefix + "zzz_BatchUpdate_" + i;
536-
parameter.Value = values[i].Item2 ?? DBNull.Value;
543+
544+
var paramValue = values[i].Item2;
545+
var paramNullValue = paramValue as NullValue;
546+
if (paramNullValue != null)
547+
{
548+
parameter.Value = DBNull.Value;
549+
}
550+
else
551+
{
552+
parameter.Value = paramValue ?? DBNull.Value;
553+
}
554+
537555

538556
if (parameter is SqlParameter)
539557
{
540558
var sqlParameter = (SqlParameter)parameter;
541-
if (sqlParameter.DbType == DbType.DateTime)
559+
560+
if (paramNullValue != null && paramNullValue.Type == typeof(byte[]))
561+
{
562+
sqlParameter.SqlDbType = SqlDbType.VarBinary;
563+
}
564+
else if (sqlParameter.DbType == DbType.DateTime)
542565
{
543566
sqlParameter.DbType = DbType.DateTime2;
544567
}
@@ -816,7 +839,18 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity, List<Tuple<
816839

817840
var parameter = command.CreateParameter();
818841
parameter.ParameterName = "@zzz_BatchUpdate_" + i;
819-
parameter.Value = values[i].Item2 ?? DBNull.Value;
842+
843+
var paramValue = values[i].Item2;
844+
var paramNullValue = paramValue as NullValue;
845+
if (paramNullValue != null)
846+
{
847+
parameter.Value = DBNull.Value;
848+
}
849+
else
850+
{
851+
parameter.Value = paramValue ?? DBNull.Value;
852+
}
853+
820854
command.Parameters.Add(parameter);
821855
}
822856

@@ -1101,6 +1135,11 @@ public Dictionary<string, object> ResolveUpdateFromQueryDictValues<T>(Expression
11011135
{
11021136
var dictValues = new Dictionary<string, object>();
11031137
var updateExpressionBody = updateFactory.Body;
1138+
1139+
while (updateExpressionBody.NodeType == ExpressionType.Convert || updateExpressionBody.NodeType == ExpressionType.ConvertChecked)
1140+
{
1141+
updateExpressionBody = ((UnaryExpression)updateExpressionBody).Operand;
1142+
}
11041143
var entityType = typeof(T);
11051144

11061145
// ENSURE: new T() { MemberInitExpression }
@@ -1142,7 +1181,15 @@ public Dictionary<string, object> ResolveUpdateFromQueryDictValues<T>(Expression
11421181

11431182
if (constantExpression != null)
11441183
{
1145-
value = constantExpression.Value;
1184+
if (constantExpression.Value == null)
1185+
{
1186+
value = new NullValue() {Type = constantExpression.Type};
1187+
}
1188+
else
1189+
{
1190+
value = constantExpression.Value;
1191+
}
1192+
11461193
}
11471194
else
11481195
{

src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureBatch.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ public void ExecuteQueries()
106106

107107
#if EF5 || EF6
108108
var connection = (EntityConnection)Context.Connection;
109+
110+
#if EF6
111+
if (Context.IsInMemoryEffortQueryContext())
112+
{
113+
foreach (var query in Queries)
114+
{
115+
query.GetResultDirectly();
116+
}
117+
118+
Queries.Clear();
119+
return;
120+
}
121+
#endif
109122
#elif EFCORE
110123
if (IsInMemory)
111124
{

src/shared/Z.EF.Plus._Core.Shared/EF6/DbContext/IsInMemoryEffortQueryContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if FULL || BATCH_DELETE || BATCH_UPDATE
1+
#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_FUTURE || QUERY_INCLUDEOPTIMIZED
22
#if EF6
33
using System.Data.Entity;
44

src/shared/Z.EF.Plus._Core.Shared/EF6/ObjectContext/GetDbContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// More projects: http://www.zzzprojects.com/
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

8-
#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE || QUERY_FILTER
8+
#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE || QUERY_FILTER || QUERY_FUTURE || QUERY_INCLUDEOPTIMIZED
99
#if EF6
1010
using System.Data.Entity;
1111
using System.Data.Entity.Core.Objects;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_FUTURE || QUERY_INCLUDEOPTIMIZED
2+
#if EF6
3+
using System.Data.Entity;
4+
using System.Data.Entity.Core.Objects;
5+
6+
namespace Z.EntityFramework.Plus
7+
{
8+
internal static partial class InternalExtensions
9+
{
10+
public static bool IsInMemoryEffortQueryContext(this ObjectContext @this)
11+
{
12+
return @this.GetDbContext().IsInMemoryEffortQueryContext();
13+
}
14+
}
15+
}
16+
17+
#endif
18+
#endif

src/shared/Z.EF.Plus._Core.Shared/Z.EF.Plus._Core.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<Compile Include="$(MSBuildThisFileDirectory)EF6\IQueryable`\IsInMemoryEffortQueryContext.cs" />
4343
<Compile Include="$(MSBuildThisFileDirectory)EF6\ObjectContext\GetDbContext.cs" />
4444
<Compile Include="$(MSBuildThisFileDirectory)EF6\ObjectContext\GetInterceptionContext.cs" />
45+
<Compile Include="$(MSBuildThisFileDirectory)EF6\ObjectContext\IsInMemoryEffortQueryContext.cs" />
4546
<Compile Include="$(MSBuildThisFileDirectory)EF6\ObjectQuery\GetCommandTextAndParameters.cs" />
4647
<Compile Include="$(MSBuildThisFileDirectory)EFCore\CreateEntity\CreateEntityCommand.cs" />
4748
<Compile Include="$(MSBuildThisFileDirectory)EFCore\CreateEntity\CreateEntityConnection.cs" />

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.7.5
1+
v1.7.6

0 commit comments

Comments
 (0)