221 lines
6.7 KiB
C#
221 lines
6.7 KiB
C#
///
|
|
/// Copyright (c) 2020 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using FluentNHibernate.Cfg;
|
|
using FluentNHibernate.Cfg.Db;
|
|
using NHibernate;
|
|
using NHibernate.Cfg;
|
|
using NHibernate.Tool.hbm2ddl;
|
|
using Common;
|
|
using Events.Entities;
|
|
|
|
namespace Events
|
|
{
|
|
public static class DB
|
|
{
|
|
/// <summary> Session factory for all regular sessions, not for CreateEmptyResultsDB(). </summary>
|
|
public static ISessionFactory SessionFactory;
|
|
|
|
/// <summary> Connection string for all sessions </summary>
|
|
static string connectionString;
|
|
///
|
|
public static string ConnectionString
|
|
{
|
|
get { return connectionString; }
|
|
set
|
|
{
|
|
if (value != connectionString)
|
|
{
|
|
connectionString = value;
|
|
SessionFactory = null; /// Clear SessionFactory on connection string change
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary> Database type (MySQL or SQLite) for all sessions </summary>
|
|
private static DBType dbType;
|
|
///
|
|
public static 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()
|
|
{
|
|
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(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<Entities.Event>());
|
|
|
|
if (createDB)
|
|
{
|
|
return cfg.ExposeConfiguration(BuildSchemaCreate).BuildSessionFactory();
|
|
}
|
|
else
|
|
{
|
|
return cfg.ExposeConfiguration(BuildSchema).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();
|
|
}
|
|
|
|
|
|
public static void SaveObject(object obj)
|
|
{
|
|
SaveObject(CreateSession(), obj);
|
|
}
|
|
///
|
|
public static void SaveObject(ISession session, object obj)
|
|
{
|
|
using (var transaction = session.BeginTransaction())
|
|
{
|
|
session.SaveOrUpdate(obj);
|
|
try { transaction.Commit(); }
|
|
catch { }
|
|
}
|
|
}
|
|
|
|
|
|
public static void DeleteObject(object obj)
|
|
{
|
|
DeleteObject(CreateSession(), obj);
|
|
}
|
|
///
|
|
public static void DeleteObject(ISession session, object obj)
|
|
{
|
|
using (var transaction = session.BeginTransaction())
|
|
{
|
|
session.Delete(obj);
|
|
transaction.Commit();
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Create an empty users database.
|
|
/// Database contains only the user 'admin' and the control board component 'CB'.
|
|
/// </summary>
|
|
/// <param name="dbType">DBType.SQLite or DBType.MySql</param>
|
|
/// <param name="connectionString">Connection string</param>
|
|
/// <returns>true=success, false=error</returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Shared data (initially empty)
|
|
/// </summary>
|
|
public static string BenchName = "undefined";
|
|
public static IList<Entities.Event> RecentEvents = null;
|
|
static IList<Entities.Subscriber> allSubscribers = null;
|
|
|
|
/// <summary>
|
|
/// Set test bench name used in Events.Entities.Event constructors
|
|
/// </summary>
|
|
public static void SetBenchName(string benchName)
|
|
{
|
|
DB.BenchName = benchName;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Loads shared data from the database
|
|
/// </summary>
|
|
public static void LoadSubscribers(ISession session)
|
|
{
|
|
allSubscribers = session.QueryOver<Subscriber>().List();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Loads shared data from the database
|
|
/// </summary>
|
|
public static void LoadRecentEvents(ISession session, string benchName, int days)
|
|
{
|
|
RecentEvents = session.QueryOver<Event>()
|
|
.Where(e => (e.Bench == benchName))
|
|
.And(e => (e.TimeStamp >= DateTime.Now - new TimeSpan(days, 0, 0, 0)))
|
|
.List();
|
|
}
|
|
|
|
public static void SaveEvent(ISession session, Event evnt, SubscriberGroup groups)
|
|
{
|
|
if (allSubscribers == null) return;
|
|
IList<Subscriber> thisEventSubscribers = new List<Subscriber>();
|
|
foreach (var s in allSubscribers)
|
|
{
|
|
if ((s.Groups & (long)groups) != 0)
|
|
{
|
|
/// Subscriber 's' belongs to at least one of SubscriberGroup(s) in 'groups' bit field
|
|
thisEventSubscribers.Add(s);
|
|
}
|
|
}
|
|
evnt.Bench = BenchName;
|
|
evnt.Subscribers = thisEventSubscribers;
|
|
session.SaveOrUpdate(evnt);
|
|
session.Flush();
|
|
}
|
|
}
|
|
}
|