Skip to content

Commit 5af1a45

Browse files
author
zzzprojects
committed
Add SQLite support to BatchDelete && BatchUpdate
Add SQLite support to BatchDelete && BatchUpdate
1 parent 53aa9db commit 5af1a45

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ WHERE EXISTS ( SELECT 1 FROM ({Select}) AS B
5656
internal const string CommandTextOracleTemplate = @"
5757
DELETE
5858
FROM {TableName}
59+
WHERE EXISTS ( SELECT 1 FROM ({Select}) B
60+
WHERE {PrimaryKeys}
61+
)
62+
";
63+
64+
internal const string CommandTextSQLiteTemplate = @"
65+
DELETE
66+
FROM {TableName}
5967
WHERE EXISTS ( SELECT 1 FROM ({Select}) B
6068
WHERE {PrimaryKeys}
6169
)
@@ -253,7 +261,7 @@ public int Execute<T>(IQueryable<T> query) where T : class
253261
int totalRowAffecteds = DbInterception.Dispatch.Command.NonQuery(command, interceptionContext);
254262
return totalRowAffecteds;
255263
}
256-
else if (command.Connection.GetType().Name.Contains("Oracle"))
264+
else if (command.Connection.GetType().Name.Contains("Oracle") || command.Connection.GetType().Name.Contains("SQLite"))
257265
{
258266
int totalRowAffecteds = DbInterception.Dispatch.Command.NonQuery(command, interceptionContext);
259267
return totalRowAffecteds;
@@ -355,6 +363,7 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
355363
bool isMySql = command.GetType().FullName.Contains("MySql");
356364
var isSqlCe = command.GetType().Name == "SqlCeCommand";
357365
var isOracle = command.GetType().Namespace.Contains("Oracle");
366+
var isSQLite = command.GetType().Namespace.Contains("SQLite");
358367

359368
// Oracle BindByName
360369
if (isOracle)
@@ -380,6 +389,10 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
380389
{
381390
tableName = string.Concat("[", store.Table, "]");
382391
}
392+
else if (isSQLite)
393+
{
394+
tableName = string.Concat("\"", store.Table, "\"");
395+
}
383396
else if (isOracle)
384397
{
385398
tableName = string.IsNullOrEmpty(store.Schema) || store.Schema == "dbo" ?
@@ -416,6 +429,8 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
416429
CommandTextTemplate_MySql :
417430
isSqlCe ?
418431
CommandTextSqlCeTemplate :
432+
isSQLite ?
433+
CommandTextSQLiteTemplate :
419434
BatchSize > 0 ?
420435
BatchDelayInterval > 0 ?
421436
CommandTextWhileDelayTemplate :
@@ -434,7 +449,7 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
434449

435450
// GET primary key join
436451
string primaryKeys;
437-
if (isSqlCe || isOracle)
452+
if (isSqlCe || isOracle || isSQLite)
438453
{
439454
primaryKeys = string.Join(Environment.NewLine + "AND ", columnKeys.Select(x => string.Concat(tableName + ".", EscapeName(x, isMySql, isOracle), " = B.", EscapeName(x, isMySql, isOracle), "")));
440455
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ WHERE EXISTS ( SELECT 1 FROM ({Select}) B
6363
internal const string CommandTextTemplate_PostgreSQL = @"
6464
UPDATE {TableName}
6565
SET {SetValue}
66+
WHERE EXISTS ( SELECT 1 FROM ({Select}) B
67+
WHERE {PrimaryKeys}
68+
)
69+
";
70+
71+
internal const string CommandTextTemplate_SQLite = @"
72+
UPDATE {TableName}
73+
SET {SetValue}
6674
WHERE EXISTS ( SELECT 1 FROM ({Select}) B
6775
WHERE {PrimaryKeys}
6876
)
@@ -316,6 +324,7 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
316324
var isSqlCe = command.GetType().Name == "SqlCeCommand";
317325
var isOracle = command.GetType().Namespace.Contains("Oracle");
318326
var isPostgreSQL = command.GetType().Name == "NpgsqlCommand";
327+
var isSQLite = command.GetType().Namespace.Contains("SQLite");
319328

320329
// Oracle BindByName
321330
if (isOracle)
@@ -353,6 +362,10 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
353362
string.Concat("\"", store.Table, "\"") :
354363
string.Concat("\"", store.Schema, "\".\"", store.Table, "\"");
355364
}
365+
else if (isSQLite)
366+
{
367+
tableName = string.Concat("\"", store.Table, "\"");
368+
}
356369
else
357370
{
358371
tableName = string.IsNullOrEmpty(store.Schema) ?
@@ -388,6 +401,7 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
388401
isOracle ? CommandTextOracleTemplate :
389402
isMySql ? CommandTextTemplate_MySQL :
390403
isSqlCe ? CommandTextTemplateSqlCe :
404+
isSQLite ? CommandTextTemplate_SQLite :
391405
CommandTextTemplate;
392406

393407
// GET inner query
@@ -421,6 +435,15 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
421435
string.Concat(EscapeName(x.Item1, isMySql, isOracle, isPostgreSQL), " = ", ((ConstantExpression)x.Item2).Value) :
422436
string.Concat(EscapeName(x.Item1, isMySql, isOracle, isPostgreSQL), " = :zzz_BatchUpdate_", i)));
423437
}
438+
else if (isSQLite)
439+
{
440+
primaryKeys = string.Join(Environment.NewLine + "AND ", columnKeys.Select(x => string.Concat(EscapeName(x, isMySql, isOracle, isPostgreSQL), " = B.", EscapeName(x, isMySql, isOracle, isPostgreSQL), "")));
441+
442+
// GET updateSetValues
443+
setValues = string.Join("," + Environment.NewLine, values.Select((x, i) => x.Item2 is ConstantExpression ?
444+
string.Concat(EscapeName(x.Item1, isMySql, isOracle, isPostgreSQL), " = ", ((ConstantExpression)x.Item2).Value) :
445+
string.Concat(EscapeName(x.Item1, isMySql, isOracle, isPostgreSQL), " = @zzz_BatchUpdate_", i)));
446+
}
424447
else
425448
{
426449
primaryKeys = string.Join(Environment.NewLine + "AND ", columnKeys.Select(x => string.Concat("A.", EscapeName(x, isMySql, isOracle, isPostgreSQL), " = B.", EscapeName(x, isMySql, isOracle, isPostgreSQL), "")));

version.txt

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

0 commit comments

Comments
 (0)