123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using FluentNHibernate.Cfg;
- using NHibernate;
- namespace IOTransmissionAbutment
- {
- public class ModelHelper
- {
- static private ISessionFactory m_factory = null;
- static private object obj = new object();
- static private string _connectionString;
- static public ISession Session
- {
- get
- {
- if (m_factory == null)
- {
- lock (obj)
- {
- try
- {
- if (m_factory == null) m_factory = createSessionFactory();
- }
- catch { throw; }
- }
- }
- return m_factory.OpenSession();
- }
- }
- static public void initialize(string path)
- {
- _connectionString = "Data Source =" + path;
- try
- {
- if (m_factory != null) m_factory.Dispose();
- m_factory = createSessionFactory();
- }
- catch (Exception ex) { Console.Write(ex.ToString()); throw; }
- }
- static public void close()
- {
- if (m_factory != null) { m_factory.Close(); m_factory = null; }
- }
- static private ISessionFactory createSessionFactory()
- {
- try
- {
- string connectionString = _connectionString;
- FluentConfiguration config = Fluently.Configure().Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008 .ConnectionString(connectionString));
- Assembly curAssembly = Assembly.GetExecutingAssembly();
- NHibernate.Cfg.Configuration cfg = config.Mappings(m => m.FluentMappings.AddFromAssembly(curAssembly)).ExposeConfiguration(f => f.SetInterceptor(new SqlStatementInterceptor())).BuildConfiguration();
- return cfg.BuildSessionFactory();
- }
- catch
- { throw; }
- }
- public static void DeleteAllWareHouseCallStatus()
- {
- try
- {
- using (var session = Session)
- {
- string sql = @"delete from WareHouseCallStatus";
- ISQLQuery qry = session.CreateSQLQuery(sql);
- qry.ExecuteUpdate();
- }
- }
- catch (Exception ex)
- {
- }
- }
- public static CallBoxSetting GetCallBoxSetting(int callbox)
- {
- CallBoxSetting whcs = null;
- try
- {
- using (var session = Session)
- {
- var result = session.QueryOver<CallBoxSetting>().Where(p => p.CallBoxNo == callbox).List();
- if (result.Count > 0)
- {
- whcs = result.First();
- }
- }
- }
- catch
- {
- }
- return whcs;
- }
- public static int GetCallBoxSettingNo(string warehouse)
- {
- int whcs = 0;
- try
- {
- using (var session = Session)
- {
- var result = session.QueryOver<CallBoxSetting>().Where(p => p.CallBoxType==2&&p.WareHouseID==warehouse).List();
- if (result.Count > 0)
- {
- whcs = result.First().CallBoxNo;
- }
- }
- }
- catch
- { }
- return whcs;
- }
- public static WareHouseCallStatus GetWareHouseCallStatus(string warehouseid)
- {
- WareHouseCallStatus whcs = null;
- try
- {
- using (var session = Session)
- {
- var result = session.QueryOver<WareHouseCallStatus>().Where(p => p.WareHouseID == warehouseid).List();
- if (result.Count > 0)
- {
- whcs = result.First();
- }
- }
- }
- catch
- {
- }
- return whcs;
- }
- public static ElevatorChoice GetElevatorChoice()
- {
- ElevatorChoice whcs = null;
- try
- {
- using (var session = Session)
- {
- var result = session.QueryOver<ElevatorChoice>().List();
- if (result.Count > 0)
- {
- whcs = result.First();
- }
- }
- }
- catch { }
- return whcs;
- }
- public class SqlStatementInterceptor : EmptyInterceptor
- {
- public override NHibernate.SqlCommand.SqlString OnPrepareStatement(NHibernate.SqlCommand.SqlString sql)
- {
- //#if DEBUG
- try
- {
- //File.AppendAllText("c:\\log.txt", string.Format("{0}\n", sql.ToString()));
- }
- catch
- {
- }
- //#endif
- return sql;
- }
- }
- /// <summary>
- /// 创建重新匹配数据库表
- /// </summary>
- /// <param name="dataBasePath"></param>
- internal static void CreateTables(string path, string mapSign = "")
- {
- //DataHelper dh = new DataHelper();
- initialize(path);
- using (var session = Session)
- {
- using (var transaction = session.BeginTransaction())
- {
- try
- {
- string sql = @"CREATE TABLE ElevatorDevice (
- Name STRING NOT NULL,
- ID INTEGER NOT NULL
- PRIMARY KEY,
- IPAddress STRING NOT NULL,
- FloorCount INTEGER NOT NULL,
- CreationTime DATETIME NOT NULL,
- ChangingTime DATETIME NOT NULL,
- Remark STRING NOT NULL,
- LiftingVertex INTEGER NOT NULL
- );
- CREATE TABLE ElevatorFloor (
- ID INTEGER PRIMARY KEY
- NOT NULL,
- ElevatorID INTEGER NOT NULL,
- Floor INTEGER NOT NULL,
- Address INTEGER NOT NULL,
- ClosingEdges STRING NOT NULL,
- ThroughEdges STRING NOT NULL,
- OpeningEdges STRING NOT NULL
- );
- CREATE TABLE GateDevice (
- ID INTEGER PRIMARY KEY
- NOT NULL,
- Address INTEGER NOT NULL,
- ClosingEdges STRING NOT NULL,
- IPAddress STRING NOT NULL,
- Name STRING NOT NULL,
- CreationTime DATETIME NOT NULL,
- ChangingTime DATETIME NOT NULL,
- Remark STRING NOT NULL,
- ThroughEdges STRING NOT NULL,
- OpeningEdges STRING NOT NULL
- );
- CREATE TABLE PowerStationDevice (
- ID INTEGER PRIMARY KEY
- NOT NULL,
- Name STRING NOT NULL,
- IPAddress STRING NOT NULL,
- CreationTime DATETIME NOT NULL,
- ChangingTime DATETIME NOT NULL,
- Remark STRING NOT NULL,
- CarrierType INTEGER NOT NULL,
- Occupied BOOLEAN NOT NULL,
- StationID INTEGER NOT NULL,
- Carrier INTEGER NOT NULL,
- Region STRING NOT NULL,
- Address INTEGER,
- ThroughEdges STRING
- );
- CREATE TABLE General (
- ID INTEGER PRIMARY KEY
- NOT NULL,
- Name VARCHAR NOT NULL,
- Content VARCHAR NOT NULL
- );
- CREATE TABLE OtherWayPowerStationDevice (
- ID INTEGER PRIMARY KEY
- NOT NULL,
- Name STRING NOT NULL,
- IPAddress STRING,
- CreationTime DATETIME NOT NULL,
- ChangingTime DATETIME NOT NULL,
- Remark STRING NOT NULL,
- CarrierType INTEGER NOT NULL,
- Occupied BOOLEAN NOT NULL,
- StationID INTEGER NOT NULL,
- Carrier INTEGER NOT NULL,
- Region STRING NOT NULL,
- Address INTEGER,
- ThroughEdges STRING
- );";
- ISQLQuery qry = session.CreateSQLQuery(sql);
- qry.ExecuteUpdate();
- transaction.Commit();
- }
- catch (Exception ex)
- {
- transaction.Rollback();
- throw ex;
- }
- }
- }
- }
- }
- }
|