namespace IStation
{
///
///
///
public class ConfigHelper
{
#region PostgreSql-ConnectionConfig
///
/// 默认连接配置
///
internal static ConnectionConfig DefaultConnectionConfig
{
get
{
//SnowFlakeSingle.WorkId = Settings.SqlSugar.SnowFlakeWorkId; 不同机器配置的唯一数字; // 单服务器不需要指定
return new ConnectionConfig()
{
DbType = SqlSugar.DbType.PostgreSQL,//数据库类型
ConnectionString = Settings.ParasHelper.DataBase.PostgreSql.ConnectString,
IsAutoCloseConnection = true,//是否自动关闭
MoreSettings = new ConnMoreSettings()
{
//PgSqlIsAutoToLower = false //数据库存在大写字段的 ,需要把这个设为false ,并且实体和字段名称要一样
},
AopEvents = new AopEvents
{
OnLogExecuting = (sql, p) =>
{
// var sqlString = UtilMethods.GetNativeSql(sql, p);
//LogHelper.Debug(sqlString);
// Console.WriteLine(sql);
}
}
};
}
}
///
/// PostgreSql连接配置
///
internal static ConnectionConfig PostgreSqlConnectionConfig
{
get
{
//SnowFlakeSingle.WorkId = Settings.SqlSugar.SnowFlakeWorkId; 不同机器配置的唯一数字; // 单服务器不需要指定
return new ConnectionConfig()
{
DbType = SqlSugar.DbType.PostgreSQL,//数据库类型
ConnectionString = Settings.ParasHelper.DataBase.PostgreSql.ConnectString,
IsAutoCloseConnection = true,//是否自动关闭
MoreSettings = new ConnMoreSettings()
{
//PgSqlIsAutoToLower = false //数据库存在大写字段的 ,需要把这个设为false ,并且实体和字段名称要一样
},
AopEvents = new AopEvents
{
OnLogExecuting = (sql, p) =>
{
// var sqlString = UtilMethods.GetNativeSql(sql, p);
//LogHelper.Debug(sqlString);
// Console.WriteLine(sql);
}
}
};
}
}
#endregion
#region SQLite-ConnectionConfig
///
///
///
///
///
private static string GetConnectionString(string dbFile)
{
var filePath = Settings.ParasHelper.LocalFile.DataFolderDirectory + "\\" + dbFile;
return $"DataSource={filePath}";
}
///
///
///
///
///
public static string DeleteDataBase(string dbFile)
{
var filePath = Settings.ParasHelper.LocalFile.DataFolderDirectory + "\\" + dbFile;
if (File.Exists(filePath))
File.Delete(filePath);
return filePath;
}
///
/// Analysis SQLite连接配置
///
internal static ConnectionConfig AnalysisConnectionConfig
{
get
{
//SnowFlakeSingle.WorkId = Settings.SqlSugar.SnowFlakeWorkId; 不同机器配置的唯一数字; // 单服务器不需要指定
return new ConnectionConfig()
{
DbType = SqlSugar.DbType.Sqlite,//数据库类型
ConnectionString = GetConnectionString(Settings.ParasHelper.DataBase.SQLite.AnalysisConnectString),
IsAutoCloseConnection = true,//是否自动关闭
MoreSettings = new ConnMoreSettings()
{
//PgSqlIsAutoToLower = false //数据库存在大写字段的 ,需要把这个设为false ,并且实体和字段名称要一样
},
AopEvents = new AopEvents
{
OnLogExecuting = (sql, p) =>
{
var sqlString = UtilMethods.GetNativeSql(sql, p);
//LogHelper.Debug(sqlString);
//Console.WriteLine(sql);
}
}
};
}
}
///
/// Schedule SQLite连接配置
///
internal static ConnectionConfig ScheduleConnectionConfig
{
get
{
//SnowFlakeSingle.WorkId = Settings.SqlSugar.SnowFlakeWorkId; 不同机器配置的唯一数字; // 单服务器不需要指定
return new ConnectionConfig()
{
DbType = SqlSugar.DbType.Sqlite,//数据库类型
ConnectionString = GetConnectionString(Settings.ParasHelper.DataBase.SQLite.ScheduleConnectString),
IsAutoCloseConnection = true,//是否自动关闭
MoreSettings = new ConnMoreSettings()
{
//PgSqlIsAutoToLower = false //数据库存在大写字段的 ,需要把这个设为false ,并且实体和字段名称要一样
},
AopEvents = new AopEvents
{
OnLogExecuting = (sql, p) =>
{
var sqlString = UtilMethods.GetNativeSql(sql, p);
//LogHelper.Debug(sqlString);
//Console.WriteLine(sql);
}
}
};
}
}
///
/// 初始化 Analysis 数据库
///
public static void InitAnalysisDB(bool backup = false)
{
DeleteDataBase(Settings.ParasHelper.DataBase.SQLite.AnalysisConnectString);
var sqlSugarClient = new SqlSugarClient(AnalysisConnectionConfig);
Type[] types = new Type[] {
typeof(Entity.AnalysisLog),
//typeof(Entity.AnalysisCombine),
//typeof(Entity.AnalysisConclusion),
};
sqlSugarClient.DbMaintenance.CreateDatabase();
if (backup)
{
sqlSugarClient.CodeFirst.BackupTable().InitTables(types);
}
else
{
sqlSugarClient.CodeFirst.InitTables(types);
}
}
///
/// 初始化 Schedule 数据库
///
public static void InitScheduleDB(bool backup = false)
{
var sqlSugarClient = new SqlSugarClient(ScheduleConnectionConfig);
Type[] types = new Type[] {
typeof(Entity.ScheduleRequest),
typeof(Entity.ScheduleScada),
typeof(Entity.ScheduleRule),
typeof(Entity.SchedulePump),
typeof(Entity.ScheduleConclusion),
};
sqlSugarClient.DbMaintenance.CreateDatabase();
if (backup)
{
sqlSugarClient.CodeFirst.BackupTable().InitTables(types);
}
else
{
sqlSugarClient.CodeFirst.InitTables(types);
}
}
#endregion
#region SQLite-Sql
/////
///// AnalysisCombine 表名前缀
/////
//internal static string AnalysisCombinePrefix = "AnalysisCombine_Cb_";
/////
///// AnalysisConclusion 表名前缀
/////
//internal static string AnalysisConclusionPrefix = "AnalysisConclusion_Cl_";
/////
///// 获取 AnalysisCombine建表 SQL
/////
///// 表名
/////
//internal static string GetAnalysisCombineCreateSql(string tableName)
//{
// var sql = $"CREATE TABLE {tableName} ( ID BIGINT NOT NULL PRIMARY KEY, RunCount INTEGER, Pump1 REAL, Pump2 REAL, CurveQH VARCHAR (255), CurveQP VARCHAR (255), MaxFlow REAL, MinFlow REAL, MaxHead REAL, MinHead REAL, AnaStatus BIT ); ";
// return sql;
//}
/////
///// 获取 AnalysisConclusion建表 SQL
/////
///// 表名
/////
//internal static string GetAnalysisConclusionCreateSql(string tableName)
//{
// var sql = $"CREATE TABLE {tableName} ( ID BIGINT NOT NULL PRIMARY KEY, CombineID BIGINT, Pump1 REAL, Pump2 REAL, Head REAL, Flow REAL, Power REAL, WP REAL, UWP REAL ); ";
// return sql;
//}
///
/// 获取 判断表是否存在 SQL
///
/// 表名
///
internal static string GetExistTableSql(string tableName)
{
var sql = $"select count(*) from sqlite_master where type = 'table' and name = '{tableName}';";
return sql;
}
#endregion
}
}