1 | using System;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using System.Data.Common;
|
---|
4 |
|
---|
5 | namespace Persistence {
|
---|
6 | /// <summary>
|
---|
7 | /// combines multiple used static methods into one class
|
---|
8 | /// </summary>
|
---|
9 | public class DatabaseUtil {
|
---|
10 |
|
---|
11 | protected static bool productionDatabase = true;
|
---|
12 |
|
---|
13 | public static bool ProductionDatabase {
|
---|
14 | get {
|
---|
15 | return DatabaseUtil.productionDatabase;
|
---|
16 | }
|
---|
17 | set {
|
---|
18 | DatabaseUtil.productionDatabase = value;
|
---|
19 | }
|
---|
20 | }
|
---|
21 |
|
---|
22 | /// <summary>
|
---|
23 | /// creates and returns a database connection, if possible
|
---|
24 | /// </summary>
|
---|
25 | /// <returns>database connection (could be null)</returns>
|
---|
26 | public static DataClassesDataContext createDataClassesDataContext() {
|
---|
27 | if (productionDatabase) {
|
---|
28 | return new Persistence.DataClassesDataContext(new Persistence.Properties.Settings().DatabaseConnectionString);
|
---|
29 | } else {
|
---|
30 | return new Persistence.DataClassesDataContext(new Persistence.Properties.Settings().DatabaseConnectionStringTesting);
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | /// <summary>
|
---|
35 | /// creates a new database out of the LINQ to SQL classes
|
---|
36 | /// </summary>
|
---|
37 | /// <param name="db">DataClassesDataContext</param>
|
---|
38 | public static void createDatabase(DataClassesDataContext db) {
|
---|
39 |
|
---|
40 | if (db.DatabaseExists()) {
|
---|
41 | Console.WriteLine("Deleting old database...");
|
---|
42 | db.DeleteDatabase();
|
---|
43 | Console.WriteLine("Deleted old database!");
|
---|
44 | }
|
---|
45 |
|
---|
46 | Console.WriteLine("Creating new database...");
|
---|
47 | db.CreateDatabase();
|
---|
48 |
|
---|
49 | DbCommand command = db.Connection.CreateCommand();
|
---|
50 | command.CommandText = "CREATE UNIQUE NONCLUSTERED INDEX [IDXRoleName] ON [dbo].[HeuristicLabRole]( [roleName] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]";
|
---|
51 | command.ExecuteNonQuery();
|
---|
52 |
|
---|
53 | command.CommandText = "CREATE UNIQUE NONCLUSTERED INDEX [IDXUserName] ON [dbo].[HeuristicLabUser]( [UserName] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]";
|
---|
54 | command.ExecuteNonQuery();
|
---|
55 | command.CommandText = "CREATE UNIQUE NONCLUSTERED INDEX [IDXUserEmail] ON [dbo].[HeuristicLabUser]( [Email] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]";
|
---|
56 | command.ExecuteNonQuery();
|
---|
57 | Console.WriteLine("Created new database!");
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|