Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit f13b737

Browse files
committed
Drop tables support. Closes #55
1 parent d9ccae8 commit f13b737

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

‎DataCore.Database/Database.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,12 @@ public int CreateTablesIfNotExists(IEnumerable<Type> tables, bool createReferenc
263263

264264
public int DropTable<T>()
265265
{
266-
var tableDefinition = GetTableDefinition(typeof(T));
266+
return DropTable(typeof(T));
267+
}
268+
269+
public int DropTable(Type table)
270+
{
271+
var tableDefinition = GetTableDefinition(table);
267272

268273
var queries = Translator.GetDropTableQuery(tableDefinition.Name);
269274

@@ -275,9 +280,24 @@ public int DropTable<T>()
275280
return 0;
276281
}
277282

283+
public int DropTables(params Type[] tables)
284+
{
285+
foreach (var table in tables)
286+
{
287+
DropTable(table);
288+
}
289+
290+
return 0;
291+
}
292+
278293
public int DropTableIfExists<T>()
279294
{
280-
var tableDefinition = GetTableDefinition(typeof(T));
295+
return DropTableIfExists(typeof(T));
296+
}
297+
298+
public int DropTableIfExists(Type table)
299+
{
300+
var tableDefinition = GetTableDefinition(table);
281301

282302
var queries = Translator.GetDropTableIfExistsQuery(tableDefinition.Name);
283303

@@ -289,6 +309,16 @@ public int DropTableIfExists<T>()
289309
return 0;
290310
}
291311

312+
public int DropTablesIfExists(params Type[] tables)
313+
{
314+
foreach (var table in tables)
315+
{
316+
DropTableIfExists(table);
317+
}
318+
319+
return 0;
320+
}
321+
292322
public int CreateColumn<T>(Expression<Func<T, dynamic>> clause)
293323
{
294324
var arguments = ExpressionHelper.GetExpressionsFromDynamic(clause);

‎DataCore.Database/IDatabase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ public interface IDatabase
2020
int CreateTableIfNotExists(Type table, bool createReferences = false);
2121
int CreateTablesIfNotExists(params Type[] tables);
2222
int CreateTablesIfNotExists(IEnumerable<Type> tables, bool createReferences = false);
23+
2324
int DropTable<T>();
25+
int DropTable(Type table);
26+
int DropTables(params Type[] tables);
2427
int DropTableIfExists<T>();
28+
int DropTableIfExists(Type table);
29+
int DropTablesIfExists(params Type[] tables);
2530

2631
int CreateColumn<T>(Expression<Func<T, dynamic>> clause);
2732
int CreateColumnIfNotExists<T>(Expression<Func<T, dynamic>> clause);

‎DataCore.Test/DatabaseCheckedTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,24 @@ public void CanDropTable(TestHelper.DatabaseType dbType, string connectionString
165165
}
166166
}
167167

168+
[Test, TestCaseSource(typeof(SqlTestDataFactory), nameof(SqlTestDataFactory.TestCases))]
169+
public void CanDropTables(TestHelper.DatabaseType dbType, string connectionString)
170+
{
171+
using (var connection = TestHelper.GetConnectionFor(dbType, connectionString))
172+
{
173+
var database = TestHelper.GetDatabaseFor(dbType, connection);
174+
175+
database.CreateTablesIfNotExists(typeof(TestClass), typeof(TestClass2));
176+
177+
database.Select(database.From<TestClass>().Where(t => t.Id == 1));
178+
database.Select(database.From<TestClass2>().Where(t => t.Id == 1));
179+
180+
database.DropTablesIfExists(typeof(TestClass), typeof(TestClass2));
181+
182+
connection.Close();
183+
}
184+
}
185+
168186
[Test, TestCaseSource(typeof(SqlTestDataFactory), nameof(SqlTestDataFactory.TestCases))]
169187
public void CanCreateColumn(TestHelper.DatabaseType dbType, string connectionString)
170188
{

‎DataCore.Test/DatabaseTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,24 @@ public void CanDropTable(TestHelper.DatabaseType dbType, string connectionString
147147
}
148148
}
149149

150+
[Test, TestCaseSource(typeof(SqlTestDataFactory), nameof(SqlTestDataFactory.TestCases))]
151+
public void CanDropTables(TestHelper.DatabaseType dbType, string connectionString)
152+
{
153+
using (var connection = TestHelper.GetConnectionFor(dbType, connectionString))
154+
{
155+
var database = TestHelper.GetDatabaseFor(dbType, connection);
156+
157+
database.CreateTables(typeof(TestClass), typeof(TestClass2));
158+
159+
database.Select(database.From<TestClass>().Where(t => t.Id == 1));
160+
database.Select(database.From<TestClass2>().Where(t => t.Id == 1));
161+
162+
database.DropTables(typeof(TestClass), typeof(TestClass2));
163+
164+
connection.Close();
165+
}
166+
}
167+
150168
[Test, TestCaseSource(typeof(SqlTestDataFactory), nameof(SqlTestDataFactory.TestCases))]
151169
public void CanCreateColumn(TestHelper.DatabaseType dbType, string connectionString)
152170
{

0 commit comments

Comments
 (0)