From 02b31634f8dd96fce2ca307238fa3d9f9c0524b8 Mon Sep 17 00:00:00 2001 From: Milan Hanajik Date: Thu, 31 Dec 2015 18:20:03 +0100 Subject: [PATCH] DBTypes to Config.Entities.Enums.cs --- Config/DBSettings.cs | 4 +- Config/DatabaseSettings.cs | 10 ++--- Config/DatabaseSettingsDlg.cs | 14 +++--- Config/Entities/Enums.cs | 9 ++++ Config/FluentCommon.cs | 17 ++------ Results/DB.cs | 63 ++++++++++++--------------- ResultsBrowser/DatabaseSettingsDlg.cs | 14 +++--- 7 files changed, 60 insertions(+), 71 deletions(-) diff --git a/Config/DBSettings.cs b/Config/DBSettings.cs index 903f55643..1e53f62e7 100644 --- a/Config/DBSettings.cs +++ b/Config/DBSettings.cs @@ -10,14 +10,14 @@ namespace Config /// public class DBSettings : ICloneable { - public DBType DbType; + public Entities.DBType DbType; public string ConnectionString; public DBSettings() { } - public DBSettings(DBType dbType, string connectionString) + public DBSettings(Entities.DBType dbType, string connectionString) { this.DbType = dbType; this.ConnectionString = connectionString; diff --git a/Config/DatabaseSettings.cs b/Config/DatabaseSettings.cs index 17f3c6aac..3f2d204ce 100644 --- a/Config/DatabaseSettings.cs +++ b/Config/DatabaseSettings.cs @@ -26,11 +26,11 @@ namespace Config { BenchName = String.Empty; /// Empty string to avoid null IsRealBench = false; - UsersDBSettings = new DBSettings(DBType.MySql, "SERVER=localhost; DATABASE=tbf_users; UID=root; PASSWORD=;"); - BenchDBSettings = new DBSettings(DBType.MySql, "SERVER=localhost; DATABASE=tbf_bench; UID=root; PASSWORD=;"); - ProceduresDBSettings = new DBSettings(DBType.MySql, "SERVER=localhost; DATABASE=tbf_procedures; UID=root; PASSWORD=;"); - WMTypesDBSettings = new DBSettings(DBType.MySql, "SERVER=localhost; DATABASE=tbf_wmtypes; UID=root; PASSWORD=;"); - WaterMetersDBSettings = new DBSettings(DBType.MySql, "SERVER=localhost; DATABASE=tbf_watermeters; UID=root; PASSWORD=;"); + UsersDBSettings = new DBSettings(Entities.DBType.MySql, "SERVER=localhost; DATABASE=tbf_users; UID=root; PASSWORD=;"); + BenchDBSettings = new DBSettings(Entities.DBType.MySql, "SERVER=localhost; DATABASE=tbf_bench; UID=root; PASSWORD=;"); + ProceduresDBSettings = new DBSettings(Entities.DBType.MySql, "SERVER=localhost; DATABASE=tbf_procedures; UID=root; PASSWORD=;"); + WMTypesDBSettings = new DBSettings(Entities.DBType.MySql, "SERVER=localhost; DATABASE=tbf_wmtypes; UID=root; PASSWORD=;"); + WaterMetersDBSettings = new DBSettings(Entities.DBType.MySql, "SERVER=localhost; DATABASE=tbf_watermeters; UID=root; PASSWORD=;"); } public object Clone() diff --git a/Config/DatabaseSettingsDlg.cs b/Config/DatabaseSettingsDlg.cs index 746eb6640..937bcbf28 100644 --- a/Config/DatabaseSettingsDlg.cs +++ b/Config/DatabaseSettingsDlg.cs @@ -62,9 +62,9 @@ namespace Config realBenchCheckBox.Checked = bench.IsRealBench; /// Initialize 'Database type' combo-boxes - for (int i = 0; i < (int)DBType.DbTypesCount; i++) + for (int i = 0; i < (int)Entities.DBType.DbTypesCount; i++) { - DBType dbType = (DBType)i; + Entities.DBType dbType = (Entities.DBType)i; usersDbTypeComboBox.Items.Add(dbType); benchDbTypeComboBox.Items.Add(dbType); proceduresDbTypeComboBox.Items.Add(dbType); @@ -107,11 +107,11 @@ namespace Config bench.WMTypesDBSettings.ConnectionString = wmTypesConnectionStringTextBox.Text; bench.WaterMetersDBSettings.ConnectionString = watermetersConnectionStringTextBox.Text; - bench.UsersDBSettings.DbType = (DBType)usersDbTypeComboBox.SelectedIndex; - bench.BenchDBSettings.DbType = (DBType)benchDbTypeComboBox.SelectedIndex; - bench.ProceduresDBSettings.DbType = (DBType)proceduresDbTypeComboBox.SelectedIndex; - bench.WMTypesDBSettings.DbType = (DBType)wmTypesDbTypeComboBox.SelectedIndex; - bench.WaterMetersDBSettings.DbType = (DBType)watermetersDbTypeComboBox.SelectedIndex; + bench.UsersDBSettings.DbType = (Entities.DBType)usersDbTypeComboBox.SelectedIndex; + bench.BenchDBSettings.DbType = (Entities.DBType)benchDbTypeComboBox.SelectedIndex; + bench.ProceduresDBSettings.DbType = (Entities.DBType)proceduresDbTypeComboBox.SelectedIndex; + bench.WMTypesDBSettings.DbType = (Entities.DBType)wmTypesDbTypeComboBox.SelectedIndex; + bench.WaterMetersDBSettings.DbType = (Entities.DBType)watermetersDbTypeComboBox.SelectedIndex; return true; } diff --git a/Config/Entities/Enums.cs b/Config/Entities/Enums.cs index 34f60e57c..22d6d84bf 100644 --- a/Config/Entities/Enums.cs +++ b/Config/Entities/Enums.cs @@ -6,6 +6,15 @@ using System.Collections.Generic; namespace Config.Entities { + /// Identifies the type of a database + public enum DBType + { + MySql, /// MySQL database + SQLite, /// SQLite + DbTypesCount, + } + + public enum ProcedureState { Active, diff --git a/Config/FluentCommon.cs b/Config/FluentCommon.cs index 25af9610c..41abe0dfb 100644 --- a/Config/FluentCommon.cs +++ b/Config/FluentCommon.cs @@ -24,17 +24,6 @@ namespace Config Count, } - /// - /// Identifies the type of a database - /// - public enum DBType - { - SQLite, /// SQLite - MySql, /// MySQL database - DbTypesCount, - } - - public static class FluentCommon { /// @@ -48,7 +37,7 @@ namespace Config /// A database session static ISessionFactory CreateSessionFactory(Database database) { - DBType dbType; + Entities.DBType dbType; string connectionString; switch (database) @@ -88,10 +77,10 @@ namespace Config switch (dbSettings.DbType) { default: - case DBType.SQLite: + case Entities.DBType.SQLite: cfg = cfg.Database(SQLiteConfiguration.Standard.UsingFile(dbSettings.ConnectionString)); break; - case DBType.MySql: + case Entities.DBType.MySql: cfg = cfg.Database(MySQLConfiguration.Standard.ConnectionString(dbSettings.ConnectionString)); break; } diff --git a/Results/DB.cs b/Results/DB.cs index eb33d1bb7..1ce72ed02 100644 --- a/Results/DB.cs +++ b/Results/DB.cs @@ -10,17 +10,6 @@ using NHibernate.Tool.hbm2ddl; namespace Results { - /// - /// Identifies the type of a database - /// - public enum DBType - { - SQLite, /// SQLite - MySql, /// MySQL database - DbTypesCount, - } - - public static class DB { /// Session factory for all regular sessions, not for CreateEmptyResultsDB(). @@ -32,37 +21,43 @@ namespace Results public static string ConnectionString { get { return connectionString; } - set - { - connectionString = value; - SessionFactory = null; - } + set { connectionString = value; SessionFactory = null; } + } + + /// Database type (MySQL or SQLite) for all sessions + private static Config.Entities.DBType dbType; + /// + public static Config.Entities.DBType DbType + { + get { return dbType; } + set { dbType = value; SessionFactory = null; } } /// /// NHibernate session factory (to create the database session 'SessionFactory') /// /// A database session - static ISessionFactory CreateSessionFactory(DBType dbType) + static ISessionFactory CreateSessionFactory() { - return CreateSessionFactory(dbType, false); + return CreateSessionFactory(false); } /// /// NHibernate session factory (to create the database session 'SessionFactory') /// + /// true = Create a new DB, false = Regular DB /// A database session - public static ISessionFactory CreateSessionFactory(DBType dbType, bool createDB) + public static ISessionFactory CreateSessionFactory(bool createDB) { FluentConfiguration cfg = Fluently.Configure(); switch (dbType) { default: - case DBType.SQLite: + case Config.Entities.DBType.SQLite: cfg = cfg.Database(SQLiteConfiguration.Standard.UsingFile(connectionString)); break; - case DBType.MySql: + case Config.Entities.DBType.MySql: cfg = cfg.Database(MySQLConfiguration.Standard.ConnectionString(connectionString)); break; } @@ -79,20 +74,16 @@ namespace Results } } - public delegate void BuildSchemaDlgt(Configuration config); - static void BuildSchema(Configuration config) { - /// This NHibernate tool takes a configuration (with mapping info in) - /// and exports a database schema from it + /// This NHibernate tool takes a configuration with mapping info and exports a database schema new SchemaExport(config).SetOutputFile("db_schema"); } static void BuildSchemaCreate(Configuration config) { - /// This NHibernate tool takes a configuration (with mapping info in) - /// and exports a database schema from it - new SchemaExport(config).Create(true, true); + /// This NHibernate tool takes a configuration with mapping info and exports a database schema + new SchemaExport(config).Create(true, true); } /// Create a NHibernate session for the given database @@ -103,19 +94,17 @@ namespace Results throw new Exception("Connection string was not specified"); } - if (SessionFactory == null) - { - SessionFactory = CreateSessionFactory(DBType.MySql); - } + if (SessionFactory == null) SessionFactory = CreateSessionFactory(); return SessionFactory.OpenSession(); } + public static void SaveObject(object obj) { SaveObject(CreateSession(), obj); } - + /// public static void SaveObject(ISession session, object obj) { using (var transaction = session.BeginTransaction()) @@ -126,11 +115,12 @@ namespace Results } } + public static void DeleteObject(object obj) { DeleteObject(CreateSession(), obj); } - + /// public static void DeleteObject(ISession session, object obj) { using (var transaction = session.BeginTransaction()) @@ -140,6 +130,7 @@ namespace Results } } + /// /// Create an empty users database. /// Database contains only the user 'admin' and the control board component 'CB'. @@ -147,9 +138,9 @@ namespace Results /// DBType.SQLite or DBType.MySql /// Connection string /// true=success, false=error - public static bool CreateEmptyResultsDB(DBType dbType) + public static bool CreateEmptyResultsDB(Config.Entities.DBType dbType) { - ISessionFactory sessionFactory = CreateSessionFactory(dbType, true); + ISessionFactory sessionFactory = CreateSessionFactory(true); if (sessionFactory == null) return false; /// Populate the database diff --git a/ResultsBrowser/DatabaseSettingsDlg.cs b/ResultsBrowser/DatabaseSettingsDlg.cs index 73e1807c9..af6effe5f 100644 --- a/ResultsBrowser/DatabaseSettingsDlg.cs +++ b/ResultsBrowser/DatabaseSettingsDlg.cs @@ -67,9 +67,9 @@ namespace ResultsBrowser realBenchCheckBox.Checked = bench.IsRealBench; /// Initialize 'Database type' combo-boxes - for (int i = 0; i < (int)Config.DBType.DbTypesCount; i++) + for (int i = 0; i < (int)Config.Entities.DBType.DbTypesCount; i++) { - Config.DBType dbType = (Config.DBType)i; + Config.Entities.DBType dbType = (Config.Entities.DBType)i; usersDbTypeComboBox.Items.Add(dbType); benchDbTypeComboBox.Items.Add(dbType); proceduresDbTypeComboBox.Items.Add(dbType); @@ -111,11 +111,11 @@ namespace ResultsBrowser bench.WMTypesDBSettings.ConnectionString = wmTypesConnectionStringTextBox.Text; bench.WaterMetersDBSettings.ConnectionString = watermetersConnectionStringTextBox.Text; - bench.UsersDBSettings.DbType = (Config.DBType)usersDbTypeComboBox.SelectedIndex; - bench.BenchDBSettings.DbType = (Config.DBType)benchDbTypeComboBox.SelectedIndex; - bench.ProceduresDBSettings.DbType = (Config.DBType)proceduresDbTypeComboBox.SelectedIndex; - bench.WMTypesDBSettings.DbType = (Config.DBType)wmTypesDbTypeComboBox.SelectedIndex; - bench.WaterMetersDBSettings.DbType = (Config.DBType)watermetersDbTypeComboBox.SelectedIndex; + bench.UsersDBSettings.DbType = (Config.Entities.DBType)usersDbTypeComboBox.SelectedIndex; + bench.BenchDBSettings.DbType = (Config.Entities.DBType)benchDbTypeComboBox.SelectedIndex; + bench.ProceduresDBSettings.DbType = (Config.Entities.DBType)proceduresDbTypeComboBox.SelectedIndex; + bench.WMTypesDBSettings.DbType = (Config.Entities.DBType)wmTypesDbTypeComboBox.SelectedIndex; + bench.WaterMetersDBSettings.DbType = (Config.Entities.DBType)watermetersDbTypeComboBox.SelectedIndex; return true; }