DBTypes to Config.Entities.Enums.cs

This commit is contained in:
Milan Hanajik 2015-12-31 18:20:03 +01:00
parent 35c449bd0c
commit 02b31634f8
7 changed files with 60 additions and 71 deletions

View File

@ -10,14 +10,14 @@ namespace Config
/// </summary>
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;

View File

@ -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()

View File

@ -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;
}

View File

@ -6,6 +6,15 @@ using System.Collections.Generic;
namespace Config.Entities
{
/// <summary> Identifies the type of a database </summary>
public enum DBType
{
MySql, /// MySQL database
SQLite, /// SQLite
DbTypesCount,
}
public enum ProcedureState
{
Active,

View File

@ -24,17 +24,6 @@ namespace Config
Count,
}
/// <summary>
/// Identifies the type of a database
/// </summary>
public enum DBType
{
SQLite, /// SQLite
MySql, /// MySQL database
DbTypesCount,
}
public static class FluentCommon
{
/// <summary>
@ -48,7 +37,7 @@ namespace Config
/// <returns>A database session</returns>
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;
}

View File

@ -10,17 +10,6 @@ using NHibernate.Tool.hbm2ddl;
namespace Results
{
/// <summary>
/// Identifies the type of a database
/// </summary>
public enum DBType
{
SQLite, /// SQLite
MySql, /// MySQL database
DbTypesCount,
}
public static class DB
{
/// <summary> Session factory for all regular sessions, not for CreateEmptyResultsDB(). </summary>
@ -32,37 +21,43 @@ namespace Results
public static string ConnectionString
{
get { return connectionString; }
set
{
connectionString = value;
SessionFactory = null;
}
set { connectionString = value; SessionFactory = null; }
}
/// <summary> Database type (MySQL or SQLite) for all sessions </summary>
private static Config.Entities.DBType dbType;
///
public static Config.Entities.DBType DbType
{
get { return dbType; }
set { dbType = value; SessionFactory = null; }
}
/// <summary>
/// NHibernate session factory (to create the database session 'SessionFactory')
/// </summary>
/// <returns>A database session</returns>
static ISessionFactory CreateSessionFactory(DBType dbType)
static ISessionFactory CreateSessionFactory()
{
return CreateSessionFactory(dbType, false);
return CreateSessionFactory(false);
}
/// <summary>
/// NHibernate session factory (to create the database session 'SessionFactory')
/// </summary>
/// <param name="createDB">true = Create a new DB, false = Regular DB</param>
/// <returns>A database session</returns>
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
}
}
/// <summary>
/// Create an empty users database.
/// Database contains only the user 'admin' and the control board component 'CB'.
@ -147,9 +138,9 @@ namespace Results
/// <param name="dbType">DBType.SQLite or DBType.MySql</param>
/// <param name="connectionString">Connection string</param>
/// <returns>true=success, false=error</returns>
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

View File

@ -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;
}