namespace IStation.DAL.SQLite
|
{
|
/// <summary>
|
///
|
/// </summary>
|
public partial class AnalysisPump : Yw.DAL.SQLite.BaseDAL<Entity.AnalysisPump>, IAnalysisPump
|
{
|
/// <summary>
|
///
|
/// </summary>
|
public override ConnectionConfig ConnectionConfig
|
{
|
get { return ConfigHelper.AnalysisConnectionConfig; }
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
private string TableNamePrefix
|
{
|
get { return "AnalysisPump_"; }
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="runFlag"></param>
|
/// <returns></returns>
|
public string GetTableName(string runFlag)
|
{
|
return $"{this.TableNamePrefix}{runFlag}";
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="tableName"></param>
|
/// <returns></returns>
|
private string GetCreateTableSql(string tableName)
|
{
|
var sql = $"CREATE TABLE {tableName} (ID BIGINT NOT NULL\r\n PRIMARY KEY,\r\n Flag INTEGER,\r\n Hz REAL,\r\n CurveQH VARCHAR (255),\r\n CurveQP VARCHAR (255),\r\n MaxFlow REAL,\r\n MinFlow REAL,\r\n MaxHead REAL,\r\n MinHead REAL\r\n);";
|
return sql;
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="tableName"></param>
|
/// <returns></returns>
|
private string GetExistTableSql(string tableName)
|
{
|
return ConfigHelper.GetExistTableSql(tableName);
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="tableName"></param>
|
/// <returns></returns>
|
public string GetRunFlag(string tableName)
|
{
|
return tableName.Replace(this.TableNamePrefix, null);
|
}
|
|
/// <summary>
|
/// 获取全部表名
|
/// </summary>
|
public List<string> GetAllTableName()
|
{
|
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
|
{
|
var list = new List<string>();
|
var sql = $"select name from sqlite_master where type='table' and name like '%{this.TableNamePrefix}%';";
|
using (var reader = db.Ado.GetDataReader(sql))
|
{
|
while (reader.Read())
|
{
|
var tableName = reader["name"].ToString();
|
list.Add(tableName);
|
}
|
}
|
return list;
|
}
|
}
|
|
/// <summary>
|
/// 获取全部表
|
/// </summary>
|
public Dictionary<string, List<Entity.AnalysisPump>> GetAllTable()
|
{
|
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
|
{
|
var tableNames = new List<string>();
|
var sql_sel_table = $"select name from sqlite_master where type='table' and name like '%{this.TableNamePrefix}%';";
|
var reader = db.Ado.GetDataReader(sql_sel_table);
|
while (reader.Read())
|
{
|
var tableName = reader["name"].ToString();
|
tableNames.Add(tableName);
|
}
|
if (tableNames == null || !tableNames.Any())
|
return default;
|
|
var dict = new Dictionary<string, List<Entity.AnalysisPump>>();
|
foreach (var tableName in tableNames)
|
{
|
var sql = $"select * from {tableName}";
|
var list = db.Ado.SqlQuery<Entity.AnalysisPump>(sql);
|
if (list == null || !list.Any())
|
continue;
|
dict.Add(tableName, list);
|
}
|
return dict;
|
}
|
}
|
|
/// <summary>
|
/// 通过表名获取全部
|
/// </summary>
|
public List<Entity.AnalysisPump> GetAllByTableName(string tableName)
|
{
|
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
|
{
|
var sql = $"select * from {tableName}";
|
var list = db.Ado.SqlQuery<Entity.AnalysisPump>(sql);
|
return list;
|
}
|
}
|
|
/// <summary>
|
/// 大批量插入(表不存在就新建)
|
/// </summary>
|
public bool BulkInsertsEx(string runFlag, List<Entity.AnalysisPump> list)
|
{
|
if (list == null || list.Count < 1)
|
return default;
|
var tableName = GetTableName(runFlag);
|
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
|
{
|
var exist_sql = GetExistTableSql(tableName);
|
if (db.Ado.GetInt(exist_sql) < 1)
|
{
|
var sql_create_table = GetCreateTableSql(tableName);
|
db.Ado.ExecuteCommand(sql_create_table);
|
}
|
//大数据写入
|
return db.Fastest<Entity.AnalysisPump>().AS(tableName).BulkCopy(list) > 0;
|
}
|
}
|
|
|
|
/// <summary>
|
/// 判断数据库是否存在表
|
/// </summary>
|
public bool ExistTable(string runFlag)
|
{
|
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
|
{
|
var tableName = GetTableName(runFlag);
|
var sql = GetExistTableSql(tableName);
|
var bol = db.Ado.ExecuteCommand(sql) > 0;
|
return bol;
|
}
|
}
|
|
/// <summary>
|
/// 删除所有表
|
/// </summary>
|
public bool DeleteAllTable()
|
{
|
var tables = GetAllTableName();
|
if (tables == null || !tables.Any())
|
return false;
|
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
|
{
|
db.BeginTran();
|
foreach (var table in tables)
|
{
|
var sql = $"drop table {table};";
|
db.Ado.ExecuteCommand(sql);
|
}
|
db.CommitTran();
|
}
|
return true;
|
}
|
|
}
|
}
|