-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathDatabase.cs
More file actions
409 lines (371 loc) · 15.7 KB
/
Database.cs
File metadata and controls
409 lines (371 loc) · 15.7 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
using System;
using Microsoft.Data.SqlClient;
using RepoDb.SqlServer;
namespace RepoDb.IntegrationTests.Setup
{
/// <summary>
/// A class used as a startup setup for for RepoDb test database.
/// </summary>
public static class Database
{
/// <summary>
/// Initialize the creation of the database.
/// </summary>
public static void Initialize()
{
// Get the connection string
var connectionStringForMaster = Environment.GetEnvironmentVariable("REPODB_CONSTR_MASTER", EnvironmentVariableTarget.Process);
var connectionString = Environment.GetEnvironmentVariable("REPODB_CONSTR", EnvironmentVariableTarget.Process);
//// Master connection
//ConnectionStringForMaster = (connectionStringForMaster ?? @"Server=(local);Database=master;Integrated Security=False;User Id=michael;Password=Password123;");
//// RepoDb connection
//ConnectionStringForRepoDb = (connectionString ?? @"Server=(local);Database=RepoDb;Integrated Security=False;User Id=michael;Password=Password123;");
// Master connection
ConnectionStringForMaster = (connectionStringForMaster ?? @"Data Source=CHRISWA;Initial Catalog=master;Trusted_Connection=True;");
// RepoDb connection
ConnectionStringForRepoDb = (connectionString ?? @"Data Source=CHRISWA;Initial Catalog=RepoDb;Trusted_Connection=True;");
// Set the proper values for type mapper
TypeMapper.Add(typeof(DateTime), System.Data.DbType.DateTime2, true);
// Initialize the SqlServer
SqlServerBootstrap.Initialize();
// Create the database first
CreateDatabase();
// Create the schemas
CreateSchemas();
// Create the tables
CreateTables();
// Create the stored procedures
CreateStoredProcedures();
}
/// <summary>
/// Gets the connection string for master.
/// </summary>
public static string ConnectionStringForMaster { get; private set; }
/// <summary>
/// Gets the connection string for RepoDb.
/// </summary>
public static string ConnectionStringForRepoDb { get; private set; }
#region Methods
/// <summary>
/// Creates a test database for RepoDb.
/// </summary>
public static void CreateDatabase()
{
var commandText = @"IF (NOT EXISTS(SELECT * FROM sys.databases WHERE name = 'RepoDb'))
BEGIN
CREATE DATABASE [RepoDb];
END";
using (var connection = new SqlConnection(ConnectionStringForMaster).EnsureOpen())
{
connection.ExecuteNonQuery(commandText);
}
}
/// <summary>
/// Create the necessary schemas for testing.
/// </summary>
public static void CreateSchemas()
{
CreateScSchema();
}
/// <summary>
/// Create the necessary tables for testing.
/// </summary>
public static void CreateTables()
{
CreateCompleteTable();
CreateIdentityTable();
CreateNonIdentityTable();
CreateUnorganizedTable();
CreatePropertyHandlerTable();
}
/// <summary>
/// Create the necessary stored procedures for testing.
/// </summary>
public static void CreateStoredProcedures()
{
CreateGetIdentityTablesStoredProcedure();
CreateGetIdentityTableByIdStoredProcedure();
CreateMultiplyStoredProcedure();
CreateGetDatabaseDateTimeStoredProcedure();
}
/// <summary>
/// Clean up all the table.
/// </summary>
public static void Cleanup()
{
using (var connection = new SqlConnection(ConnectionStringForRepoDb))
{
connection.Truncate("[dbo].[CompleteTable]");
connection.Truncate("[sc].[IdentityTable]");
connection.Truncate("[dbo].[NonIdentityTable]");
connection.Truncate("[dbo].[Unorganized Table]");
connection.Truncate("[dbo].[PropertyHandler]");
}
}
#endregion
#region CreateSchemas
/// <summary>
/// Creates the 'sc' schema.
/// </summary>
public static void CreateScSchema()
{
using (var connection = new SqlConnection(ConnectionStringForRepoDb).EnsureOpen())
{
var exists = connection.ExecuteScalar("SELECT 1 FROM [sys].[schemas] WHERE name = 'sc';");
if (exists == null)
{
connection.ExecuteNonQuery("CREATE SCHEMA [sc];");
}
}
}
#endregion
#region CreateTables
/// <summary>
/// Creates an identity table that has some important fields. All fields are nullable.
/// </summary>
public static void CreateIdentityTable()
{
var commandText = @"IF (NOT EXISTS(SELECT 1 FROM [sys].[objects] WHERE type = 'U' AND name = 'IdentityTable'))
BEGIN
CREATE TABLE [sc].[IdentityTable]
(
[Id] BIGINT NOT NULL IDENTITY(1, 1),
[RowGuid] UNIQUEIDENTIFIER NOT NULL,
[ColumnBit] BIT NULL,
[ColumnDateTime] DATETIME NULL,
[ColumnDateTime2] DATETIME2(7) NULL,
[ColumnDecimal] DECIMAL(18, 2) NULL,
[ColumnFloat] FLOAT NULL,
[ColumnInt] INT NULL,
[ColumnNVarChar] NVARCHAR(MAX) NULL,
) ON [PRIMARY];
END";
using (var connection = new SqlConnection(ConnectionStringForRepoDb).EnsureOpen())
{
connection.ExecuteNonQuery(commandText);
}
}
/// <summary>
/// Creates an non-identity table that has some important fields. All fields are nullable.
/// </summary>
public static void CreateNonIdentityTable()
{
var commandText = @"IF (NOT EXISTS(SELECT 1 FROM [sys].[objects] WHERE type = 'U' AND name = 'NonIdentityTable'))
BEGIN
CREATE TABLE [dbo].[NonIdentityTable]
(
[Id] UNIQUEIDENTIFIER NOT NULL,
[ColumnBit] BIT NULL,
[ColumnDateTime] DATETIME NULL,
[ColumnDateTime2] DATETIME2(7) NULL,
[ColumnDecimal] DECIMAL(18, 2) NULL,
[ColumnFloat] FLOAT NULL,
[ColumnInt] INT NULL,
[ColumnNVarChar] NVARCHAR(MAX) NULL,
CONSTRAINT [NonIdentityTable_$Id] PRIMARY KEY
(
[Id] ASC
)
) ON [PRIMARY];
END";
using (var connection = new SqlConnection(ConnectionStringForRepoDb).EnsureOpen())
{
connection.ExecuteNonQuery(commandText);
}
}
/// <summary>
/// Creates an unorganized table that has some non-alphanumeric fields. All fields are nullable.
/// </summary>
public static void CreateUnorganizedTable()
{
var commandText = @"IF (NOT EXISTS(SELECT 1 FROM [sys].[objects] WHERE type = 'U' AND name = 'Unorganized Table'))
BEGIN
CREATE TABLE [dbo].[Unorganized Table]
(
[Id] BIGINT NOT NULL IDENTITY(1, 1),
[Session Id] UNIQUEIDENTIFIER NOT NULL,
[Column Int] INT NULL,
[Column/NVarChar] NVARCHAR(128) NULL,
[Column.DateTime] DATETIME2(7) NULL
) ON [PRIMARY];
END";
using (var connection = new SqlConnection(ConnectionStringForRepoDb).EnsureOpen())
{
connection.ExecuteNonQuery(commandText);
}
}
/// <summary>
/// Creates a table that has a complete fields. All fields are nullable.
/// </summary>
public static void CreateCompleteTable()
{
var commandText = @"IF (NOT EXISTS(SELECT 1 FROM [sys].[objects] WHERE type = 'U' AND name = 'CompleteTable'))
BEGIN
CREATE TABLE [dbo].[CompleteTable]
(
[SessionId] UNIQUEIDENTIFIER NOT NULL,
[ColumnBigInt] BIGINT NULL,
[ColumnBinary] BINARY(4000) NULL,
[ColumnBit] BIT NULL,
[ColumnChar] CHAR(32) NULL,
[ColumnDate] DATE NULL,
[ColumnDateTime] DATETIME NULL,
[ColumnDateTime2] DATETIME2(7) NULL,
[ColumnDateTimeOffset] DATETIMEOFFSET(7) NULL,
[ColumnDecimal] DECIMAL(18, 2) NULL,
[ColumnFloat] FLOAT NULL,
[ColumnGeography] GEOGRAPHY NULL,
[ColumnGeometry] GEOMETRY NULL,
[ColumnHierarchyId] HIERARCHYID NULL,
[ColumnImage] IMAGE NULL,
[ColumnInt] INT NULL,
[ColumnMoney] MONEY NULL,
[ColumnNChar] NCHAR(32) NULL,
[ColumnNText] NTEXT NULL,
[ColumnNumeric] NUMERIC(18, 2) NULL,
[ColumnNVarChar] NVARCHAR(MAX) NULL,
[ColumnReal] REAL NULL,
[ColumnSmallDateTime] SMALLDATETIME NULL,
[ColumnSmallInt] SMALLINT NULL,
[ColumnSmallMoney] SMALLMONEY NULL,
[ColumnSqlVariant] SQL_VARIANT NULL,
[ColumnText] TEXT NULL,
[ColumnTime] TIME(7) NULL,
[ColumnTimestamp] TIMESTAMP NULL,
[ColumnTinyInt] TINYINT NULL,
[ColumnUniqueIdentifier] UNIQUEIDENTIFIER NULL,
[ColumnVarBinary] VARBINARY(MAX) NULL,
[ColumnVarChar] VARCHAR(MAX) NULL,
[ColumnXml] XML NULL,
CONSTRAINT [SessionId] PRIMARY KEY
(
[SessionId] ASC
)
) ON [PRIMARY];
ALTER TABLE [dbo].[CompleteTable] ADD CONSTRAINT [DF_CompleteTable_SessionId] DEFAULT (NEWID()) FOR [SessionId];
END";
using (var connection = new SqlConnection(ConnectionStringForRepoDb).EnsureOpen())
{
connection.ExecuteNonQuery(commandText);
}
}
/// <summary>
/// Creates a table that has a complete fields. All fields are nullable.
/// </summary>
public static void CreatePropertyHandlerTable()
{
var commandText = @"IF (NOT EXISTS(SELECT 1 FROM [sys].[objects] WHERE type = 'U' AND name = 'PropertyHandler'))
BEGIN
CREATE TABLE [dbo].[PropertyHandler]
(
[Id] BIGINT NOT NULL IDENTITY(1, 1),
[ColumnNVarChar] NVARCHAR(MAX) NULL,
[ColumnInt] INT NULL,
[ColumnIntNotNull] INT NOT NULL,
[ColumnDecimal] DECIMAL(18, 2) NULL,
[ColumnDecimalNotNull] DECIMAL(18, 2) NOT NULL,
[ColumnFloat] FLOAT NULL,
[ColumnFloatNotNull] FLOAT NOT NULL,
[ColumnDateTime] DATETIME NULL,
[ColumnDateTimeNotNull] DATETIME NOT NULL,
[ColumnDateTime2] DATETIME2(7) NULL,
[ColumnDateTime2NotNull] DATETIME2(7) NOT NULL,
CONSTRAINT [pk_PropertyHandler] PRIMARY KEY CLUSTERED
(
[Id] ASC
)
) ON [PRIMARY];
END";
using (var connection = new SqlConnection(ConnectionStringForRepoDb).EnsureOpen())
{
connection.ExecuteNonQuery(commandText);
}
}
#endregion
#region CreateStoredProcedures
/// <summary>
/// Create a stored procedure that is used to return all records from IdentityTable.
/// </summary>
public static void CreateGetIdentityTablesStoredProcedure()
{
using (var connection = new SqlConnection(ConnectionStringForRepoDb).EnsureOpen())
{
var exists = connection.ExecuteScalar("SELECT 1 FROM [sys].[objects] WHERE type = 'P' AND name = 'sp_get_identity_tables';");
if (exists == null)
{
var commandText = @"CREATE PROCEDURE [dbo].[sp_get_identity_tables]
AS
BEGIN
SELECT * FROM [sc].[IdentityTable];
END";
connection.ExecuteNonQuery(commandText);
}
}
}
/// <summary>
/// Create a stored procedure that is used to return a IdentityTable record by id.
/// </summary>
public static void CreateGetIdentityTableByIdStoredProcedure()
{
using (var connection = new SqlConnection(ConnectionStringForRepoDb).EnsureOpen())
{
var exists = connection.ExecuteScalar("SELECT 1 FROM [sys].[objects] WHERE type = 'P' AND name = 'sp_get_identity_table_by_id';");
if (exists == null)
{
var commandText = @"CREATE PROCEDURE [dbo].[sp_get_identity_table_by_id]
(
@Id INT
)
AS
BEGIN
SELECT * FROM [sc].[IdentityTable] WHERE Id = @Id;
END";
connection.ExecuteNonQuery(commandText);
}
}
}
/// <summary>
/// Create a stored procedure that will return a scalar value of date time.
/// </summary>
public static void CreateGetDatabaseDateTimeStoredProcedure()
{
using (var connection = new SqlConnection(ConnectionStringForRepoDb).EnsureOpen())
{
var exists = connection.ExecuteScalar("SELECT 1 FROM [sys].[objects] WHERE type = 'P' AND name = 'sp_get_database_date_time';");
if (exists == null)
{
var commandText = @"CREATE PROCEDURE [dbo].[sp_get_database_date_time]
AS
BEGIN
SELECT GETUTCDATE() AS [Value];
END";
connection.ExecuteNonQuery(commandText);
}
}
}
/// <summary>
/// Create a stored procedure that is used for multiplication.
/// </summary>
public static void CreateMultiplyStoredProcedure()
{
using (var connection = new SqlConnection(ConnectionStringForRepoDb).EnsureOpen())
{
var exists = connection.ExecuteScalar("SELECT 1 FROM [sys].[objects] WHERE type = 'P' AND name = 'sp_multiply';");
if (exists == null)
{
var commandText = @"CREATE PROCEDURE [dbo].[sp_multiply]
(
@Value1 INT,
@Value2 INT
)
AS
BEGIN
SELECT @Value1 * @Value2 AS [Value];
END";
connection.ExecuteNonQuery(commandText);
}
}
}
#endregion
}
}