using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using FluentNHibernate.Cfg; using FluentNHibernate.Cfg.Db; using NHibernate; using NHibernate.Cfg; using NHibernate.Tool.hbm2ddl; using Common; using Events; namespace EventViewer { public static class EViewerDB { /// Session factory for all regular sessions, not for CreateEmptyResultsDB(). public static ISessionFactory SessionFactory; /// Connection string for all sessions static string connectionString; /// public static string ConnectionString { get { return connectionString; } set { if (value != connectionString) { connectionString = value; SessionFactory = null; /// Clear SessionFactory on connection string change } } } /// Database type (MySQL or SQLite) for all sessions private static DBType dbType; /// public static 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() { 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(bool createDB) { FluentConfiguration cfg = Fluently.Configure(); switch (dbType) { default: case DBType.MySql: cfg = cfg.Database(MySQLConfiguration.Standard.ConnectionString(connectionString)); break; case DBType.SQLite: cfg = cfg.Database(SQLiteConfiguration.Standard.UsingFile(connectionString)); break; } cfg = cfg.Mappings(m => m.FluentMappings.AddFromAssemblyOf()); if (createDB) { return cfg.ExposeConfiguration(BuildSchemaCreate) .BuildConfiguration() .SetProperty("hibernate.connection.connect_timeout", Const.MySqlConnectTimeoutSec) .BuildSessionFactory(); } else { return cfg.ExposeConfiguration(BuildSchema) .BuildConfiguration() .SetProperty("hibernate.connection.connect_timeout", Const.MySqlConnectTimeoutSec) .BuildSessionFactory(); } } static void BuildSchema(Configuration config) { /// 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 and exports a database schema new SchemaExport(config).Create(true, true); } /// Create a NHibernate session for the given database public static ISession CreateSession() { if (string.IsNullOrEmpty(connectionString)) { throw new Exception("Connection string was not specified"); } if (SessionFactory == null) SessionFactory = CreateSessionFactory(); return SessionFactory.OpenSession(); } /// /// Create an empty users database. /// Database contains only the user 'admin' and the control board component 'CB'. /// /// DBType.SQLite or DBType.MySql /// Connection string /// true=success, false=error public static bool CreateEmptyDB() { ISessionFactory sessionFactory = CreateSessionFactory(true); if (sessionFactory == null) return false; /// Populate the database using (var session = sessionFactory.OpenSession()) { using (var transaction = session.BeginTransaction()) { transaction.Commit(); } } return true; } } }