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
+27 -36
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