Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 80263de

Browse files
committed
Implement Disable/EnableForeignKeyCheck for MySql + SQL Server
1 parent 035157b commit 80263de

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

src/ServiceStack.OrmLite.MySql/MySqlDialectProviderBase.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,13 @@ public override string SqlCast(object fieldOrValue, string castAs) =>
554554

555555
public override string SqlBool(bool value) => value ? "1" : "0";
556556

557+
public override void EnableForeignKeysCheck(IDbCommand cmd) => cmd.ExecNonQuery("SET FOREIGN_KEY_CHECKS=1;");
558+
public override Task EnableForeignKeysCheckAsync(IDbCommand cmd, CancellationToken token = default) =>
559+
cmd.ExecNonQueryAsync("SET FOREIGN_KEY_CHECKS=1;", null, token);
560+
public override void DisableForeignKeysCheck(IDbCommand cmd) => cmd.ExecNonQuery("SET FOREIGN_KEY_CHECKS=0;");
561+
public override Task DisableForeignKeysCheckAsync(IDbCommand cmd, CancellationToken token = default) =>
562+
cmd.ExecNonQueryAsync("SET FOREIGN_KEY_CHECKS=0;", null, token);
563+
557564
protected DbConnection Unwrap(IDbConnection db)
558565
{
559566
return (DbConnection)db.ToDbConnection();

src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,13 @@ public override string SqlCast(object fieldOrValue, string castAs) =>
685685
: $"CAST({fieldOrValue} AS {castAs})";
686686

687687
public override string SqlRandom => "NEWID()";
688+
689+
public override void EnableForeignKeysCheck(IDbCommand cmd) => cmd.ExecNonQuery("EXEC sp_msforeachtable \"ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all\"");
690+
public override Task EnableForeignKeysCheckAsync(IDbCommand cmd, CancellationToken token = default) =>
691+
cmd.ExecNonQueryAsync("EXEC sp_msforeachtable \"ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all\"", null, token);
692+
public override void DisableForeignKeysCheck(IDbCommand cmd) => cmd.ExecNonQuery("EXEC sp_msforeachtable \"ALTER TABLE ? NOCHECK CONSTRAINT all\"");
693+
public override Task DisableForeignKeysCheckAsync(IDbCommand cmd, CancellationToken token = default) =>
694+
cmd.ExecNonQueryAsync("EXEC sp_msforeachtable \"ALTER TABLE ? NOCHECK CONSTRAINT all\"", null, token);
688695

689696
protected SqlConnection Unwrap(IDbConnection db) => (SqlConnection)db.ToDbConnection();
690697

src/ServiceStack.OrmLite.Sqlite/SqliteOrmLiteDialectProviderBase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,13 @@ public override string SqlConflict(string sql, string conflictResolution)
248248

249249
public override string SqlRandom => "random()";
250250

251-
public override void EnableForeignKeysCheck(IDbCommand cmd) => cmd.ExecNonQuery($"PRAGMA foreign_keys = ON;");
251+
public override void EnableForeignKeysCheck(IDbCommand cmd) => cmd.ExecNonQuery("PRAGMA foreign_keys = ON;");
252252
public override Task EnableForeignKeysCheckAsync(IDbCommand cmd, CancellationToken token = default) =>
253-
cmd.ExecNonQueryAsync($"PRAGMA foreign_keys = ON;", null, token);
253+
cmd.ExecNonQueryAsync("PRAGMA foreign_keys = ON;", null, token);
254254

255-
public override void DisableForeignKeysCheck(IDbCommand cmd) => cmd.ExecNonQuery($"PRAGMA foreign_keys = OFF;");
255+
public override void DisableForeignKeysCheck(IDbCommand cmd) => cmd.ExecNonQuery("PRAGMA foreign_keys = OFF;");
256256
public override Task DisableForeignKeysCheckAsync(IDbCommand cmd, CancellationToken token = default) =>
257-
cmd.ExecNonQueryAsync($"PRAGMA foreign_keys = OFF;", null, token);
257+
cmd.ExecNonQueryAsync("PRAGMA foreign_keys = OFF;", null, token);
258258
}
259259

260260
public static class SqliteExtensions

tests/ServiceStack.OrmLite.Tests/Issues/LoadReferenceIssueWithCyclicalForeignKeys.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace ServiceStack.OrmLite.Tests.Issues
88
{
9-
[TestFixtureOrmLiteDialects(Dialect.Sqlite)]
9+
[TestFixtureOrmLiteDialects(Dialect.Sqlite | Dialect.MySql)]
1010
public class LoadReferenceIssueWithCyclicalForeignKeys : OrmLiteProvidersTestBase
1111
{
1212
public LoadReferenceIssueWithCyclicalForeignKeys(DialectContext context) : base(context) { }
@@ -44,8 +44,10 @@ public class NameEntity : BaseEntity
4444
private void RecreateTables(IDbConnection db)
4545
{
4646
db.DisableForeignKeysCheck();
47-
db.DropAndCreateTable<NameEntity>();
48-
db.DropAndCreateTable<ResearchEntity>();
47+
db.DropTable<NameEntity>();
48+
db.DropTable<ResearchEntity>();
49+
db.CreateTable<NameEntity>();
50+
db.CreateTable<ResearchEntity>();
4951
db.EnableForeignKeysCheck();
5052
}
5153

0 commit comments

Comments
 (0)