Skip to content

Commit b11d271

Browse files
Fix BatchDelete && BatchUpdate
1 parent a59f310 commit b11d271

File tree

2 files changed

+237
-93
lines changed

2 files changed

+237
-93
lines changed

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

Lines changed: 180 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ WHERE EXISTS ( SELECT 1 FROM ({Select}) AS B
5858
internal const string CommandTextOracleTemplate = @"
5959
DELETE
6060
FROM {TableName}
61+
WHERE EXISTS ( SELECT 1 FROM ({Select}) B
62+
WHERE {PrimaryKeys}
63+
)
64+
";
65+
66+
internal const string CommandTextOracleTemplateCore = @"
67+
DELETE
68+
FROM {TableName} A
6169
WHERE EXISTS ( SELECT 1 FROM ({Select}) B
6270
WHERE {PrimaryKeys}
6371
)
@@ -579,6 +587,7 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity)
579587
bool isSQLite = false;
580588
bool isOracle = false;
581589
bool isDevOracle = false;
590+
bool devOracleDisableQuoting = false;
582591

583592
if (assemblyName == "Microsoft.EntityFrameworkCore.SqlServer")
584593
{
@@ -597,10 +606,14 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity)
597606
else if (assemblyName == "MySql.Data.EntityFrameworkCore")
598607
{
599608
isMySql = true;
600-
var type = assembly.GetType("MySQL.Data.EntityFrameworkCore.MySQLMetadataExtensions");
601-
dynamicProviderEntityType = type.GetMethod("MySQL", new[] { typeof(IEntityType) });
602-
dynamicProviderProperty = type.GetMethod("MySQL", new[] { typeof(IProperty) });
603-
}
609+
//var type = assembly.GetType("MySQL.Data.EntityFrameworkCore.MySQLMetadataExtensions");
610+
//dynamicProviderEntityType = type.GetMethod("MySQL", new[] { typeof(IEntityType) });
611+
//dynamicProviderProperty = type.GetMethod("MySQL", new[] { typeof(IProperty) });
612+
613+
614+
dynamicProviderEntityType = typeof(RelationalMetadataExtensions).GetMethod("Relational", new[] { typeof(IEntityType) });
615+
dynamicProviderProperty = typeof(RelationalMetadataExtensions).GetMethod("Relational", new[] { typeof(IProperty) });
616+
}
604617
else if (assemblyName == "Pomelo.EntityFrameworkCore.MySql")
605618
{
606619
isMySqlPomelo = true;
@@ -628,6 +641,15 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity)
628641
{
629642
isDevOracle = true;
630643

644+
var config = assembly.GetType("Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfig");
645+
646+
if (config != null)
647+
{
648+
var instanceProperty = config.GetProperty("Instance", BindingFlags.Static | BindingFlags.Public);
649+
var instance = instanceProperty.GetValue(config);
650+
devOracleDisableQuoting = (bool)((dynamic)instance).Workarounds.DisableQuoting;
651+
}
652+
631653
// CHANGE all for this one?
632654
dynamicProviderEntityType = typeof(RelationalMetadataExtensions).GetMethod("Relational", new[] { typeof(IEntityType) });
633655
dynamicProviderProperty = typeof(RelationalMetadataExtensions).GetMethod("Relational", new[] { typeof(IProperty) });
@@ -687,98 +709,176 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity)
687709
propertyAndColumnName.Add(new Tuple<string, string>(propertyKey.Name, (string)columnNameProperty.GetValue(mappingProperty)));
688710
}
689711

690-
primaryKeys = string.Join(Environment.NewLine + "AND ", propertyAndColumnName.Select(x => string.Concat("A.\"", x.Item2, "\" = B.\"", x.Item1, "\"")));
712+
primaryKeys = propertyAndColumnName.Count > 1 ?
713+
string.Join(Environment.NewLine + "AND ", propertyAndColumnName.Select(x => string.Concat("A.\"", x.Item2, "\" = B.\"", x.Item1, "\""))):
714+
string.Join(Environment.NewLine + "AND ", propertyAndColumnName.Select(x => string.Concat("A.\"", x.Item2, "\" = B.\"", x.Item2, "\"")));
691715
}
692-
else if (isMySqlPomelo)
693-
{
694-
if (dynamicProviderEntityType == null)
695-
{
696-
// GET mapping
697-
tableName = string.Concat("`", entity.Relational().TableName, "`");
716+
else if (isMySqlPomelo)
717+
{
718+
if (dynamicProviderEntityType == null)
719+
{
720+
// GET mapping
721+
tableName = string.Concat("`", entity.Relational().TableName, "`");
722+
723+
if (dynamicProviderProperty != null)
724+
{
725+
List<Tuple<string, string>> propertyAndColumnName = new List<Tuple<string, string>>();
726+
727+
// GET keys mappings
728+
foreach (var propertyKey in entity.GetKeys().ToList()[0].Properties)
729+
{
730+
var mappingProperty = dynamicProviderProperty.Invoke(null, new[] { propertyKey });
731+
732+
var columnNameProperty = mappingProperty.GetType().GetProperty("ColumnName", BindingFlags.Public | BindingFlags.Instance);
733+
//columnKeys.Add((string)columnNameProperty.GetValue(mappingProperty));
734+
propertyAndColumnName.Add(new Tuple<string, string>(propertyKey.Name, (string)columnNameProperty.GetValue(mappingProperty)));
735+
}
736+
737+
primaryKeys = propertyAndColumnName.Count > 1 ?
738+
string.Join(Environment.NewLine + "AND ", propertyAndColumnName.Select(x => string.Concat("A.`", x.Item2, "` = B.`", x.Item1, "`"))) :
739+
string.Join(Environment.NewLine + "AND ", propertyAndColumnName.Select(x => string.Concat("A.`", x.Item2, "` = B.`", x.Item2, "`")));
740+
}
741+
else
742+
{
743+
// GET keys mappings
744+
foreach (var propertyKey in entity.GetKeys().ToList()[0].Properties)
745+
{
746+
columnKeys.Add(propertyKey.Relational().ColumnName);
747+
}
748+
749+
primaryKeys = string.Join(Environment.NewLine + "AND ", columnKeys.Select(x => string.Concat("A.`", x, "` = B.`", x, "`")));
750+
}
751+
}
752+
else
753+
{
754+
var sqlServer = (IRelationalEntityTypeAnnotations)dynamicProviderEntityType.Invoke(null, new[] { entity });
755+
756+
// GET mapping
757+
tableName = string.Concat("`", sqlServer.TableName, "`");
758+
759+
if (dynamicProviderProperty != null)
760+
{
761+
List<Tuple<string, string>> propertyAndColumnName = new List<Tuple<string, string>>();
762+
763+
// GET keys mappings
764+
foreach (var propertyKey in entity.GetKeys().ToList()[0].Properties)
765+
{
766+
var mappingProperty = dynamicProviderProperty.Invoke(null, new[] { propertyKey });
767+
768+
var columnNameProperty = mappingProperty.GetType().GetProperty("ColumnName", BindingFlags.Public | BindingFlags.Instance);
769+
//columnKeys.Add((string)columnNameProperty.GetValue(mappingProperty));
770+
propertyAndColumnName.Add(new Tuple<string, string>(propertyKey.Name, (string)columnNameProperty.GetValue(mappingProperty)));
771+
}
772+
773+
primaryKeys = propertyAndColumnName.Count > 1 ?
774+
string.Join(Environment.NewLine + "AND ", propertyAndColumnName.Select(x => string.Concat("A.`", x.Item2, "` = B.`", x.Item1, "`"))) :
775+
string.Join(Environment.NewLine + "AND ", propertyAndColumnName.Select(x => string.Concat("A.`", x.Item2, "` = B.`", x.Item2, "`")));
776+
}
777+
else
778+
{
779+
780+
// GET keys mappings
781+
foreach (var propertyKey in entity.GetKeys().ToList()[0].Properties)
782+
{
783+
var mappingProperty = dynamicProviderProperty.Invoke(null, new[] {propertyKey});
784+
785+
var columnNameProperty = mappingProperty.GetType().GetProperty("ColumnName", BindingFlags.Public | BindingFlags.Instance);
786+
columnKeys.Add((string) columnNameProperty.GetValue(mappingProperty));
787+
}
788+
789+
primaryKeys = string.Join(Environment.NewLine + "AND ", columnKeys.Select(x => string.Concat("A.`", x, "` = B.`", x, "`")));
790+
}
791+
}
792+
}
793+
else if (isMySql)
794+
{
795+
var sqlServer = (IRelationalEntityTypeAnnotations)dynamicProviderEntityType.Invoke(null, new[] { entity });
698796

699-
// GET keys mappings
700-
foreach (var propertyKey in entity.GetKeys().ToList()[0].Properties)
701-
{
702-
columnKeys.Add(propertyKey.Relational().ColumnName);
703-
}
797+
// GET mapping
798+
tableName = string.Concat("`", sqlServer.TableName, "`");
704799

705-
primaryKeys = string.Join(Environment.NewLine + "AND ", columnKeys.Select(x => string.Concat("A.`", x, "` = B.`", x, "`")));
706-
}
707-
else
708-
{
709-
var sqlServer = (IRelationalEntityTypeAnnotations)dynamicProviderEntityType.Invoke(null, new[] { entity });
800+
List<Tuple<string, string>> propertyAndColumnName = new List<Tuple<string, string>>();
710801

711-
// GET mapping
712-
tableName = string.Concat("`", sqlServer.TableName, "`");
802+
// GET keys mappings
803+
foreach (var propertyKey in entity.GetKeys().ToList()[0].Properties)
804+
{
805+
var mappingProperty = dynamicProviderProperty.Invoke(null, new[] { propertyKey });
713806

714-
// GET keys mappings
715-
foreach (var propertyKey in entity.GetKeys().ToList()[0].Properties)
716-
{
717-
var mappingProperty = dynamicProviderProperty.Invoke(null, new[] { propertyKey });
807+
var columnNameProperty = mappingProperty.GetType().GetProperty("ColumnName", BindingFlags.Public | BindingFlags.Instance);
808+
//columnKeys.Add((string)columnNameProperty.GetValue(mappingProperty));
809+
propertyAndColumnName.Add(new Tuple<string, string>(propertyKey.Name, (string)columnNameProperty.GetValue(mappingProperty)));
810+
}
718811

719-
var columnNameProperty = mappingProperty.GetType().GetProperty("ColumnName", BindingFlags.Public | BindingFlags.Instance);
720-
columnKeys.Add((string)columnNameProperty.GetValue(mappingProperty));
721-
}
812+
primaryKeys = propertyAndColumnName.Count > 1 ?
813+
string.Join(Environment.NewLine + "AND ", propertyAndColumnName.Select(x => string.Concat("A.`", x.Item2, "` = B.`", x.Item1, "`"))) :
814+
string.Join(Environment.NewLine + "AND ", propertyAndColumnName.Select(x => string.Concat("A.`", x.Item2, "` = B.`", x.Item2, "`")));
815+
}
816+
else if (isSQLite)
817+
{
818+
var sqlServer = (IRelationalEntityTypeAnnotations)dynamicProviderEntityType.Invoke(null, new[] { entity });
722819

723-
primaryKeys = string.Join(Environment.NewLine + "AND ", columnKeys.Select(x => string.Concat("A.`", x, "` = B.`", x, "`")));
724-
}
725-
726-
}
727-
else if (isMySql)
728-
{
729-
var sqlServer = (IRelationalEntityTypeAnnotations)dynamicProviderEntityType.Invoke(null, new[] { entity });
820+
// GET mapping
821+
tableName = string.Concat("\"", sqlServer.TableName, "\"");
730822

731-
// GET mapping
732-
tableName = string.Concat("`", sqlServer.TableName, "`");
823+
List<Tuple<string, string>> propertyAndColumnName = new List<Tuple<string, string>>();
733824

734-
// GET keys mappings
735-
foreach (var propertyKey in entity.GetKeys().ToList()[0].Properties)
736-
{
737-
var mappingProperty = dynamicProviderProperty.Invoke(null, new[] { propertyKey });
738-
739-
var columnNameProperty = mappingProperty.GetType().GetProperty("ColumnName", BindingFlags.Public | BindingFlags.Instance);
740-
columnKeys.Add((string)columnNameProperty.GetValue(mappingProperty));
741-
}
825+
// GET keys mappings
826+
foreach (var propertyKey in entity.GetKeys().ToList()[0].Properties)
827+
{
828+
var mappingProperty = dynamicProviderProperty.Invoke(null, new[] { propertyKey });
742829

743-
primaryKeys = string.Join(Environment.NewLine + "AND ", columnKeys.Select(x => string.Concat("A.`", x, "` = B.`", x, "`")));
744-
}
745-
else if (isSQLite)
746-
{
747-
var sqlServer = (IRelationalEntityTypeAnnotations)dynamicProviderEntityType.Invoke(null, new[] { entity });
830+
var columnNameProperty = mappingProperty.GetType().GetProperty("ColumnName", BindingFlags.Public | BindingFlags.Instance);
831+
//columnKeys.Add((string)columnNameProperty.GetValue(mappingProperty));
832+
propertyAndColumnName.Add(new Tuple<string, string>(propertyKey.Name, (string)columnNameProperty.GetValue(mappingProperty)));
833+
}
748834

749-
// GET mapping
750-
tableName = string.Concat("\"", sqlServer.TableName, "\"");
835+
primaryKeys = propertyAndColumnName.Count > 1 ?
836+
string.Join(Environment.NewLine + "AND ", propertyAndColumnName.Select(x => string.Concat("\"", x.Item2, "\" = B.\"", x.Item1, "\""))):
837+
string.Join(Environment.NewLine + "AND ", propertyAndColumnName.Select(x => string.Concat("\"", x.Item2, "\" = B.\"", x.Item2, "\"")));
838+
}
839+
else if (isOracle || isDevOracle)
840+
{
841+
var sqlServer = (IRelationalEntityTypeAnnotations)dynamicProviderEntityType.Invoke(null, new[] { entity });
751842

752-
// GET keys mappings
753-
foreach (var propertyKey in entity.GetKeys().ToList()[0].Properties)
843+
if (devOracleDisableQuoting)
754844
{
755-
var mappingProperty = dynamicProviderProperty.Invoke(null, new[] { propertyKey });
756-
757-
var columnNameProperty = mappingProperty.GetType().GetProperty("ColumnName", BindingFlags.Public | BindingFlags.Instance);
758-
columnKeys.Add((string)columnNameProperty.GetValue(mappingProperty));
845+
// GET mapping
846+
tableName = string.IsNullOrEmpty(sqlServer.Schema) ? sqlServer.TableName : string.Concat(sqlServer.Schema, ".", sqlServer.TableName);
847+
}
848+
else
849+
{
850+
// GET mapping
851+
tableName = string.IsNullOrEmpty(sqlServer.Schema) ? string.Concat("\"", sqlServer.TableName, "\"") : string.Concat("\"", sqlServer.Schema, "\".\"", sqlServer.TableName, "\"");
759852
}
760853

761-
primaryKeys = string.Join(Environment.NewLine + "AND ", columnKeys.Select(x => string.Concat(tableName + "." + "\"" + x + "\"", " = B.\"", x, "\"")));
762-
}
763-
else if (isOracle || isDevOracle)
764-
{
765-
var sqlServer = (IRelationalEntityTypeAnnotations)dynamicProviderEntityType.Invoke(null, new[] { entity });
766854

767-
// GET mapping
768-
tableName = string.IsNullOrEmpty(sqlServer.Schema) ? string.Concat("\"", sqlServer.TableName, "\"") : string.Concat("\"", sqlServer.Schema, "\".\"", sqlServer.TableName, "\"");
855+
List<Tuple<string, string>> propertyAndColumnName = new List<Tuple<string, string>>();
769856

770-
// GET keys mappings
771-
foreach (var propertyKey in entity.GetKeys().ToList()[0].Properties)
772-
{
773-
var mappingProperty = dynamicProviderProperty.Invoke(null, new[] { propertyKey });
857+
// GET keys mappings
858+
foreach (var propertyKey in entity.GetKeys().ToList()[0].Properties)
859+
{
860+
var mappingProperty = dynamicProviderProperty.Invoke(null, new[] { propertyKey });
774861

775-
var columnNameProperty = mappingProperty.GetType().GetProperty("ColumnName", BindingFlags.Public | BindingFlags.Instance);
776-
columnKeys.Add((string)columnNameProperty.GetValue(mappingProperty));
777-
}
862+
var columnNameProperty = mappingProperty.GetType().GetProperty("ColumnName", BindingFlags.Public | BindingFlags.Instance);
863+
//columnKeys.Add((string)columnNameProperty.GetValue(mappingProperty));
864+
propertyAndColumnName.Add(new Tuple<string, string>(propertyKey.Name, (string)columnNameProperty.GetValue(mappingProperty)));
865+
}
778866

779-
// GET primary key join
780-
primaryKeys = string.Join(Environment.NewLine + "AND ", columnKeys.Select(x => string.Concat(tableName + ".\"", x, "\" = B.\"", x, "\"")));
781-
}
867+
if (devOracleDisableQuoting)
868+
{
869+
// GET primary key join
870+
primaryKeys = propertyAndColumnName.Count > 1 ?
871+
string.Join(Environment.NewLine + "AND ", propertyAndColumnName.Select(x => string.Concat("A.", x.Item2, " = B.", x.Item1))) :
872+
string.Join(Environment.NewLine + "AND ", propertyAndColumnName.Select(x => string.Concat("A.", x.Item2, " = B.", x.Item2)));
873+
}
874+
else
875+
{
876+
// GET primary key join
877+
primaryKeys = propertyAndColumnName.Count > 1 ?
878+
string.Join(Environment.NewLine + "AND ", propertyAndColumnName.Select(x => string.Concat("A.\"", x.Item2, "\" = B.\"", x.Item1, "\""))) :
879+
string.Join(Environment.NewLine + "AND ", propertyAndColumnName.Select(x => string.Concat("A.\"", x.Item2, "\" = B.\"", x.Item2, "\"")));
880+
}
881+
}
782882

783883
// GET command text template
784884
var commandTextTemplate = isPostgreSQL ?
@@ -788,7 +888,7 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity)
788888
isSQLite ?
789889
CommandTextSQLiteTemplate :
790890
( isOracle || isDevOracle) ?
791-
CommandTextOracleTemplate :
891+
CommandTextOracleTemplateCore :
792892
BatchSize > 0 ?
793893
BatchDelayInterval > 0 ?
794894
CommandTextWhileDelayTemplate :
@@ -831,7 +931,7 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity)
831931
}
832932
else
833933
{
834-
#endif
934+
#endif
835935
name = relationalParameter.InvariantName;
836936
#if NETSTANDARD2_0
837937
}
@@ -882,7 +982,7 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity)
882982
return command;
883983
}
884984
#endif
885-
public string EscapeName(string name, bool isMySql, bool isOracle, bool isHana)
985+
public string EscapeName(string name, bool isMySql, bool isOracle, bool isHana)
886986
{
887987
return isMySql ? string.Concat("`", name, "`") :
888988
isOracle || isHana ? string.Concat("\"", name, "\"") :

0 commit comments

Comments
 (0)