[3943] | 1 | using System;
|
---|
[3970] | 2 | using System.Diagnostics;
|
---|
[3971] | 3 | using System.Data.Common;
|
---|
[3943] | 4 |
|
---|
| 5 | namespace Persistence {
|
---|
[3940] | 6 | /// <summary>
|
---|
[3943] | 7 | /// combines multiple used static methods into one class
|
---|
[3940] | 8 | /// </summary>
|
---|
| 9 | public class DatabaseUtil {
|
---|
[3970] | 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 |
|
---|
[3940] | 22 | /// <summary>
|
---|
[3943] | 23 | /// creates and returns a database connection, if possible
|
---|
[3940] | 24 | /// </summary>
|
---|
[3943] | 25 | /// <returns>database connection (could be null)</returns>
|
---|
[3940] | 26 | public static DataClassesDataContext createDataClassesDataContext() {
|
---|
[3970] | 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 | }
|
---|
[3940] | 32 | }
|
---|
[3943] | 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) {
|
---|
[3970] | 39 |
|
---|
[3943] | 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();
|
---|
[3971] | 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();
|
---|
[3943] | 57 | Console.WriteLine("Created new database!");
|
---|
| 58 | }
|
---|
[3940] | 59 | }
|
---|
| 60 | }
|
---|