/
InMemoryDatabase.cs
64 lines (49 loc) · 1.95 KB
/
InMemoryDatabase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using NPoco;
namespace NPoco.Tests.Common
{
public class InMemoryDatabase : TestDatabase
{
public InMemoryDatabase()
{
DbType = DatabaseType.SQLite;
ConnectionString = "Data Source=:memory:;Version=3;";
ProviderName = DatabaseType.SQLite.GetProviderName();
RecreateDataBase();
EnsureSharedConnectionConfigured();
}
public override void EnsureSharedConnectionConfigured()
{
if (Connection != null) return;
lock (_syncRoot)
{
Connection = new Microsoft.Data.Sqlite.SqliteConnection(ConnectionString);
Connection.Open();
}
}
public override void RecreateDataBase()
{
Console.WriteLine("----------------------------");
Console.WriteLine("Using SQLite In-Memory DB ");
Console.WriteLine("----------------------------");
base.RecreateDataBase();
var cmd = Connection.CreateCommand();
cmd.CommandText = "CREATE TABLE Users(UserId INTEGER PRIMARY KEY, Name nvarchar(200), Age int, DateOfBirth datetime, Savings Decimal(10,5));";
cmd.ExecuteNonQuery();
cmd.CommandText = "CREATE TABLE ExtraUserInfos(ExtraUserInfoId INTEGER PRIMARY KEY, UserId int, Email nvarchar(200), Children int);";
cmd.ExecuteNonQuery();
cmd.Dispose();
}
public override void CleanupDataBase()
{
base.CleanupDataBase();
if (Connection == null) return;
var cmd = Connection.CreateCommand();
cmd.CommandText = "DROP TABLE Users;";
cmd.ExecuteNonQuery();
cmd.CommandText = "DROP TABLE ExtraUserInfos;";
cmd.ExecuteNonQuery();
cmd.Dispose();
}
}
}