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

Commit d9ccae8

Browse files
committed
Create tables support. Closes #54
1 parent c5b146c commit d9ccae8

File tree

4 files changed

+88
-4
lines changed

4 files changed

+88
-4
lines changed

‎DataCore.Database/Database.cs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ public void DeleteById<T>(params object[] ids)
157157

158158
public int CreateTable<T>(bool createReferences = false)
159159
{
160-
var type = typeof(T);
160+
return CreateTable(typeof(T), createReferences);
161+
}
162+
163+
public int CreateTable(Type table, bool createReferences = false)
164+
{
165+
var type = table;
161166

162167
var tableDefinition = GetTableDefinition(type);
163168

@@ -188,9 +193,29 @@ public int CreateTable<T>(bool createReferences = false)
188193
return 0;
189194
}
190195

196+
public int CreateTables(params Type[] tables)
197+
{
198+
return CreateTables(tables, false);
199+
}
200+
201+
public int CreateTables(IEnumerable<Type> tables, bool createReferences = false)
202+
{
203+
foreach (var type in tables)
204+
{
205+
CreateTable(type, createReferences);
206+
}
207+
208+
return 0;
209+
}
210+
191211
public int CreateTableIfNotExists<T>(bool createReferences = false)
192212
{
193-
var type = typeof(T);
213+
return CreateTableIfNotExists(typeof(T), createReferences);
214+
}
215+
216+
public int CreateTableIfNotExists(Type table, bool createReferences = false)
217+
{
218+
var type = table;
194219

195220
var tableDefinition = GetTableDefinition(type);
196221

@@ -199,7 +224,7 @@ public int CreateTableIfNotExists<T>(bool createReferences = false)
199224
var queries = Translator.GetCreateTableIfNotExistsQuery(tableDefinition.Name, fields);
200225
foreach (var query in queries)
201226
{
202-
Execute(query);
227+
Execute(query);
203228
}
204229

205230
foreach (var field in fields.Where(f => f.HasIndex))
@@ -215,7 +240,22 @@ public int CreateTableIfNotExists<T>(bool createReferences = false)
215240
var idColumnTo = referencedTable.IdField;
216241

217242
CreateForeignKeyIfNotExists(field.ReferenceName, tableDefinition.Name, referencedTable.Name, field.Name, idColumnTo.Name);
218-
}
243+
}
244+
}
245+
246+
return 0;
247+
}
248+
249+
public int CreateTablesIfNotExists(params Type[] tables)
250+
{
251+
return CreateTablesIfNotExists(tables, false);
252+
}
253+
254+
public int CreateTablesIfNotExists(IEnumerable<Type> tables, bool createReferences = false)
255+
{
256+
foreach (var type in tables)
257+
{
258+
CreateTableIfNotExists(type, createReferences);
219259
}
220260

221261
return 0;

‎DataCore.Database/IDatabase.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ public interface IDatabase
1313
void DeleteById<T>(params object[] ids);
1414

1515
int CreateTable<T>(bool createReferences = false);
16+
int CreateTable(Type table, bool createReferences = false);
17+
int CreateTables(params Type[] tables);
18+
int CreateTables(IEnumerable<Type> tables, bool createReferences = false);
1619
int CreateTableIfNotExists<T>(bool createReferences = false);
20+
int CreateTableIfNotExists(Type table, bool createReferences = false);
21+
int CreateTablesIfNotExists(params Type[] tables);
22+
int CreateTablesIfNotExists(IEnumerable<Type> tables, bool createReferences = false);
1723
int DropTable<T>();
1824
int DropTableIfExists<T>();
1925

‎DataCore.Test/DatabaseCheckedTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ public void CanCreateTable(TestHelper.DatabaseType dbType, string connectionStri
2525
}
2626
}
2727

28+
[Test, TestCaseSource(typeof(SqlTestDataFactory), nameof(SqlTestDataFactory.TestCases))]
29+
public void CanCreateTables(TestHelper.DatabaseType dbType, string connectionString)
30+
{
31+
using (var connection = TestHelper.GetConnectionFor(dbType, connectionString))
32+
{
33+
var database = TestHelper.GetDatabaseFor(dbType, connection);
34+
35+
database.CreateTablesIfNotExists(typeof(TestClass), typeof(TestClass2));
36+
37+
var query = database.From<TestClass>().Where(t => t.Id == 1);
38+
database.Select(query);
39+
40+
var query2 = database.From<TestClass2>().Where(t => t.Id == 1);
41+
database.Select(query2);
42+
43+
connection.Close();
44+
}
45+
}
46+
2847
[Test, TestCaseSource(typeof(SqlTestDataFactory), nameof(SqlTestDataFactory.TestCases))]
2948
public void CanCreateTableWithOverridedName(TestHelper.DatabaseType dbType, string connectionString)
3049
{

‎DataCore.Test/DatabaseTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ public void CanCreateTable(TestHelper.DatabaseType dbType, string connectionStri
2525
}
2626
}
2727

28+
[Test, TestCaseSource(typeof(SqlTestDataFactory), nameof(SqlTestDataFactory.TestCases))]
29+
public void CanCreateTables(TestHelper.DatabaseType dbType, string connectionString)
30+
{
31+
using (var connection = TestHelper.GetConnectionFor(dbType, connectionString))
32+
{
33+
var database = TestHelper.GetDatabaseFor(dbType, connection);
34+
35+
database.CreateTables(typeof(TestClass), typeof(TestClass2));
36+
37+
var query = database.From<TestClass>().Where(t => t.Id == 1);
38+
database.Select(query);
39+
40+
var query2 = database.From<TestClass2>().Where(t => t.Id == 1);
41+
database.Select(query2);
42+
43+
connection.Close();
44+
}
45+
}
46+
2847
[Test, TestCaseSource(typeof(SqlTestDataFactory), nameof(SqlTestDataFactory.TestCases))]
2948
public void CanCreateTableWithOverridedName(TestHelper.DatabaseType dbType, string connectionString)
3049
{

0 commit comments

Comments
 (0)