using Api.Framework.Model; using SqlSugar; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; namespace Api.Framework.Tools { /// /// 持久层线程池 /// public class SessionThreadLocal { private static ThreadLocal m_SessionLocal = new ThreadLocal(); /// /// 设置持久层到线程池 /// /// public static void Set(SqlSugarClient session) { m_SessionLocal.Value = session; } /// /// 从线程池获取持久层 /// /// public static SqlSugarClient Get() { return m_SessionLocal.Value; } /// /// 清空线程池 /// public static void Clear() { foreach (var item in m_SessionLocal.Values) { item.Close(); item.Dispose(); } m_SessionLocal.Value = null; } } /// /// 数据库链接配置 /// public class ConnectionConfig { /// /// 数据库连接信息 /// public string ConnectionString { get; set; } /// /// 数据库类型 /// public DatabaseType DatabaseType { get; set; } } /// /// 数据库类型枚举,需要扩展类型可在此添加 /// public enum DatabaseType : int { /// /// MSSQL数据库 /// [Description("MSSQL数据库")] SQLSERVER = 1, /// /// ORACLE数据库 /// [Description("ORACLE数据库")] ORACLE = 2, /// /// ACCESS数据库 /// [Description("ACCESS数据库")] ACCESS = 3, /// /// MYSQL数据库 /// [Description("MYSQL数据库")] MYSQL = 4, /// /// SQLITE数据库 /// [Description("SQLITE数据库")] SQLITE = 5 } /// /// 数据库基础类 /// public class base_model { [SugarColumn(IsPrimaryKey = true, IsIdentity = true), Browsable(false), ReadOnly(false)] public long id { get; set; } } /// /// 分页查询结果 /// /// 数据库表实体类 public class PageResult { /// /// 分页查询中总记录数 /// public int Total { get; set; } /// /// 分页查询中结果集合 /// public List DataList { get; set; } } public class ParamMap { public List SugarParameters { get; private set; } public bool ContainsKey(string name) { return this.SugarParameters.FirstOrDefault(f => f.ParameterName == name) != null ? true : false; } public ParamMap() { OrderBySQL = string.Empty; SugarParameters = new List(); } public void setParameter(string name, object value) { SugarParameters.Add(new SqlSugar.SugarParameter(name, value)); } public int Index { get; private set; } public int PageSize { get; private set; } public void setPageParamters(int index, int pagesize) { this.Index = index; this.PageSize = pagesize; } public string OrderBySQL { get; private set; } /// /// 设置排序字段及排序方式 /// /// 排序字段 /// 是否为递减排序 public void setOrderFields(string orderFields, bool isDesc) { OrderBySQL = $" Order by {orderFields} " + (isDesc ? "desc" : "asc") + " "; } } public static class SqlSugarExtend { public static PageResult FindPage(this SqlSugarClient session, string sql, ParamMap map) { string _temp = string.Empty; if (session.CurrentConnectionConfig.DbType == DbType.Sqlite) { _temp = $" limit {map.PageSize} offset {map.PageSize * (map.Index - 1)}"; } else if (session.CurrentConnectionConfig.DbType == DbType.MySql) { _temp = $" limit {map.PageSize * (map.Index - 1)},{map.PageSize}"; } //foreach (SugarParameter item in map.SugarParameters) //{ // sql = sql.Replace("@"+item.ParameterName,$""); //} PageResult result = new PageResult(); result.DataList = session.Ado.SqlQuery(sql + map.OrderBySQL + _temp, map.SugarParameters); //var reg = Regex.Matches(sql, @"(select [\w\W]+ from)"); //if (reg.Count > 0) //{ //} //var temp = Regex.Replace(sql, @"^select [\w\W]+ from", "select count(*) as count from"); var start_index = sql.IndexOf("from"); var temp_sql = "select count(*) as count " + sql.Substring(start_index); result.Total = int.Parse(session.FindRow(temp_sql, map.SugarParameters)["count"].ToString()); return result; } public static ParamMap NewParamMap(this SqlSugarClient session) { return new ParamMap(); } /// /// 创建表 /// /// public static void CreateTable(this SqlSugarClient session, Type type) { //session.CodeFirst.BackupTable().InitTables(type); //获取公共属性 PropertyInfo[] Propertys = type.GetProperties(); if (Propertys.Length == 1) throw new Exception("字段最少需要2个。"); StringBuilder sb = new StringBuilder(); sb.Append("CREATE TABLE "); sb.Append(type.Name); sb.Append("("); int number = 0; foreach (var item in Propertys) { string _name = $"`{item.Name}`"; if (_name == "interval") _name = $"`{_name}`"; string _type = string.Empty; bool _primary = false; var _unique = " "; var _attribute = item.GetCustomAttributes().FirstOrDefault(f => f.GetType() == typeof(SugarColumn)) as SugarColumn; if (_attribute != null) { _name = _attribute.ColumnName; //_type = _attribute.ty; _primary = _attribute.IsPrimaryKey; // _unique = _attribute.IsUnique ? " unique " : " "; } if (string.IsNullOrEmpty(_name)) _name = item.Name; number++; sb.Append(_name); if (item.Name.ToLower() == "id" || _primary) { //sb.Append(" integer(11) primary key 自增 not null "); sb.Append(" integer primary key 自增 not null "); } else if (!string.IsNullOrEmpty(_type)) { sb.Append(" " + _type + _unique); } else { sb.Append(" "); //if (item.PropertyType.FullName == typeof(Int32).ToString() || item.PropertyType.FullName == typeof(Int16).ToString() || item.PropertyType.FullName == typeof(UInt16).ToString() || item.PropertyType.FullName == typeof(Int64).ToString()) if (item.PropertyType.FullName == typeof(Int32).ToString() || item.PropertyType.FullName == typeof(Int16).ToString() || item.PropertyType.FullName == typeof(UInt16).ToString()) sb.Append(" integer(11) default 0 " + _unique); else if (item.PropertyType.FullName == typeof(Int64).ToString()) sb.Append(" bigint(11) default 0 " + _unique); else if (typeof(DateTime).ToString() == item.PropertyType.FullName) sb.Append(" datetime " + _unique); else if (typeof(String).ToString() == item.PropertyType.FullName) { if (_name.ToUpper() == "message".ToUpper() || _name.ToUpper() == "answer".ToUpper() || _name.ToUpper() == "token".ToUpper() || _name.ToUpper() == "dicvalue".ToUpper() || _name.ToUpper() == "ExtInfo".ToUpper() || _name.ToUpper() == "Signature".ToUpper() || _name.ToUpper() == "adzones".ToUpper() || _name.ToUpper() == "data".ToUpper() || _name.ToUpper() == "device_info".ToUpper() || _name.ToUpper().EndsWith("_text".ToUpper()) || _name.ToUpper().EndsWith("dic".ToUpper())) { if (session.CurrentConnectionConfig.DbType == DbType.MySql && _name.ToUpper().EndsWith("dic".ToUpper())) { sb.Append(" longtext " + _unique); } else sb.Append(" text " + _unique); } else sb.Append(" varchar(" + (_attribute == null ? 255 : (_attribute.Length == 0 ? 255 : _attribute.Length)) + ") default ''" + _unique); } else if (item.PropertyType.FullName == typeof(Char).ToString()) sb.Append(" varchar(2) default ''" + _unique); else if (item.PropertyType.FullName == typeof(Boolean).ToString()) sb.Append(" boolean " + _unique); else if (item.PropertyType.FullName == typeof(Double).ToString()) sb.Append(" double default 0.00" + _unique); else { if (item.PropertyType.BaseType.Name == "Enum") { sb.Append(" integer default 0 " + _unique); } else { throw new Exception("不支持" + item.PropertyType.FullName + "字段,请联系框架设计师处理。"); } } } if (number != Propertys.Length) { sb.Append(","); } } sb.Append(");"); string sql = sb.ToString(); if (sql.Contains("自增")) { if (session.CurrentConnectionConfig.DbType == DbType.Sqlite) { sql = sql.Replace("自增", "AUTOINCREMENT").Replace("AUTO_INCREMENT", "AUTOINCREMENT"); } else if (session.CurrentConnectionConfig.DbType == DbType.MySql) { sql = sql.Replace("自增", "AUTO_INCREMENT").Replace("AUTOINCREMENT", "AUTO_INCREMENT"); } } session.ExcuteSQL(sql); } public static void CreateTable(this SqlSugarClient session) { session.CreateTable(typeof(T)); } public static void Update(this SqlSugarClient session, base_model model, object param = null) { StringBuilder sb = new StringBuilder("update " + model.GetType().Name + " set "); ParamMap map = session.NewParamMap(); if (param != null) { PropertyInfo[] propertys = param.GetType().GetProperties(); int number = 0; foreach (System.Reflection.PropertyInfo p in propertys) { number++; sb.Append(p.Name + "=@" + p.Name); map.setParameter(p.Name, p.GetValue(param)); if (propertys.Length != number) sb.Append(","); } map.setParameter("id", model.id); sb.Append(" where id=@id;"); } } public static bool IndexExist(this SqlSugarClient session, params object[] values) { return IndexExist(session, typeof(T).Name, values); } public static bool IndexExist(this SqlSugarClient session, string tableName, params object[] values) { var indexName = tableName + "__" + string.Join("_", values); if (indexName.Length > 64) indexName = indexName.Substring(0, 64); if (session.CurrentConnectionConfig.DbType == DbType.MySql) { var db_name = string.Empty; var reg = Regex.Match(session.CurrentConnectionConfig.ConnectionString, "Data Source=(?.*?);Initial Catalog=(?.*?);Persist Security Info=True;User ID=(?.*?);Password=(?.*?);Port=(?.*?);"); if (reg.Success) db_name = reg.Groups["db_name"].ToString(); var row = session.FindRow("SELECT * FROM information_schema.statistics WHERE table_schema = @db_name and table_name = @tableName AND index_name = @indexName", new { db_name = db_name, tableName = tableName, indexName = indexName }); if (row != null) return true; } else if (session.CurrentConnectionConfig.DbType == DbType.Sqlite) { var row = session.FindRow("SELECT * FROM sqlite_master WHERE type = 'index' and tbl_name = @tableName and name = @indexName", new { tableName = tableName, indexName = indexName }); if (row != null) return true; } return false; } /// /// 索引是否存在 /// /// /// /// /// public static bool IndexExist(this SqlSugarClient session, string indexName) { return IndexExist(session, typeof(T).Name, indexName); } /// /// 索引是否存在 /// /// /// /// /// public static bool IndexExist(this SqlSugarClient session, string tableName, string indexName) { if (session.CurrentConnectionConfig.DbType == DbType.MySql) { var db_name = string.Empty; var reg = Regex.Match(session.CurrentConnectionConfig.ConnectionString, "Data Source=(?.*?);Initial Catalog=(?.*?);Persist Security Info=True;User ID=(?.*?);Password=(?.*?);Port=(?.*?);"); if (reg.Success) db_name = reg.Groups["db_name"].ToString(); var row = session.FindRow("SELECT * FROM information_schema.statistics WHERE table_schema = @db_name and table_name = @tableName AND index_name = @indexName", new { db_name = db_name, tableName = tableName, indexName = tableName+ "__" + indexName }); if (row != null) return true; } else if (session.CurrentConnectionConfig.DbType == DbType.Sqlite) { var row = session.FindRow("SELECT * FROM sqlite_master WHERE type = 'index' and tbl_name = @tableName and name = @indexName", new { tableName = tableName, indexName = indexName }); if (row != null) return true; } return false; } /// /// 添加索引 /// public static bool AddIndex(this SqlSugarClient session, params object[] values) { try { string tableName = typeof(T).Name; string sql = string.Empty; var indexName = tableName + "__" + string.Join("_", values); if (indexName.Length > 64) indexName = indexName.Substring(0, 64); if (session.CurrentConnectionConfig.DbType == DbType.Sqlite) sql = $" CREATE INDEX {indexName} ON {tableName} ({string.Join(",", values)});"; else if (session.CurrentConnectionConfig.DbType == DbType.MySql) sql = $"ALTER TABLE {tableName} ADD INDEX {indexName}({string.Join(",", values)});"; else throw new Exception("暂时不支持" + session.CurrentConnectionConfig.DbType + "数据库!"); session.ExcuteSQL(sql); return true; } catch (Exception ex) { Console.WriteLine("创建索引失败:" + ex.Message); } return false; } public static bool DeleteIndex(this SqlSugarClient session, string tableName, string indexName) { if (session.CurrentConnectionConfig.DbType == DbType.MySql) { session.ExcuteSQL($"DROP INDEX {indexName} ON {tableName}"); } else if (session.CurrentConnectionConfig.DbType == DbType.Sqlite) { session.ExcuteSQL($"DROP INDEX {indexName}"); } return !session.IndexExist(tableName, indexName); } /// /// 判断表是否存在 /// /// /// public static bool TableExist(this SqlSugarClient session) { return TableExist(session, typeof(T).Name); } /// /// 判断表是否存在 /// public static bool TableExist(this SqlSugarClient session, string tableName) { if (session.CurrentConnectionConfig.DbType == DbType.Sqlite) { var result = session.Ado.GetDataTable("SELECT * FROM sqlite_master where type='table' and name=@tableName;", new { tableName = tableName }); if (result.Rows.Count == 0) return false; } else if (session.CurrentConnectionConfig.DbType == DbType.MySql) { //var result = session.Ado.GetDataTable("select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA=@dbName and TABLE_NAME=@tableName ;", new { tableName = Regex.Match(session.CurrentConnectionConfig.ConnectionString, "Initial Catalog=(.*?);").Groups[1].Value }); var result = session.Ado.GetDataTable("select * from information_schema.TABLES where TABLE_SCHEMA=(select database()) and `table_name` =@tableName", new { tableName = tableName }); if (result.Rows.Count == 0) return false; else { //SELECT table_name FROM information_schema.TABLES WHERE table_name ='fl_plugin_pddrebate_pddtgw'; result = session.Ado.GetDataTable("SELECT table_name FROM information_schema.TABLES WHERE table_name = @tableName", new { tableName = tableName }); if (result.Rows.Count == 0) return false; } } else return false; return true; } /// /// 添加唯一约束 /// public static void AddUnique(this SqlSugarClient session, params object[] values) { AddUnique(session, typeof(T).Name, values); } /// /// 执行sql /// /// /// /// public static void ExcuteSQL(this SqlSugarClient session, string sql, object parameters = null) { session.Ado.ExecuteCommand(sql, parameters); } public static bool ExistUnique(this SqlSugarClient session, string tableName, params object[] values) { if (session.CurrentConnectionConfig.DbType == DbType.Sqlite) { return false; } else if (session.CurrentConnectionConfig.DbType == DbType.MySql) { //SELECT count(*) FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE where table_name = 'fl_statistics_record' and column_name = 'uid' //var result = session.Ado.GetDataTable("SELECT count(*) FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE where table_name = @tableName and column_name = @column_name", new { tableName = tableName, column_name = columnName }); StringBuilder strb = new StringBuilder($"SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE where table_name = @tableName and CONSTRAINT_NAME = '{values[0]}'"); //List ors = new List(); //for (int i = 0; i < values.Length; i++) //{ // ors.Add($"column_name = '{values[i]}'"); //} //strb.Append($"({string.Join(" or ", ors)});"); var result = session.Ado.GetDataTable(strb.ToString(), new { tableName = tableName }); var rows = result.Rows; if (rows.Count == 0) return false; List strs = new List(); foreach (System.Data.DataRow row in rows) { var item = row["column_name"]; strs.Add(item.ToString()); } string[] strs2 = new string[values.Length]; values.CopyTo(strs2, 0); var notchongfu = strs.Except(strs2.ToList()).ToList(); return notchongfu.Count == 0; } return true; } public static bool DeleteUnique(this SqlSugarClient session, string tableName, params object[] values) { try { if (values == null) throw new Exception("Unique 不能为空。"); StringBuilder sb = new StringBuilder(); if (session.CurrentConnectionConfig.DbType == DbType.Sqlite) { sb.Append(" drop index "); sb.Append(tableName); foreach (var item in values) { sb.Append("_"); sb.Append(item); sb.Append("_"); } } else if (session.CurrentConnectionConfig.DbType == DbType.MySql) { try { sb.Append("ALTER TABLE "); sb.Append(tableName); sb.Append(" drop index "); for (int i = 0; i < values.Length; i++) { sb.Append(values[i]); if ((i + 1) != values.Length) { sb.Append(","); } } sb.Append(";"); session.Ado.ExecuteCommand(sb.ToString()); return true; } catch (Exception) { sb.Length = 0; sb.Append("ALTER TABLE "); sb.Append(tableName); sb.Append(" drop index "); if (values.Length != 0) { sb.Append(values[0]); } sb.Append(";"); session.Ado.ExecuteCommand(sb.ToString()); return true; } } else throw new Exception("暂时不支持" + session.CurrentConnectionConfig.DbType + "数据库!"); session.Ado.ExecuteCommand(sb.ToString()); return true; } catch (Exception ex) { } return false; } /// /// 添加唯一约束 /// public static void AddUnique(this SqlSugarClient session, string tableName, params object[] values) { if (values == null) throw new Exception("Unique 不能为空。"); StringBuilder sb = new StringBuilder(); if (session.CurrentConnectionConfig.DbType == DbType.Sqlite) { sb.Append(" CREATE UNIQUE INDEX "); sb.Append(tableName); foreach (var item in values) { sb.Append("_"); sb.Append(item); sb.Append("_"); } sb.Append(" ON "); sb.Append(tableName); sb.Append("("); for (int i = 0; i < values.Length; i++) { sb.Append(values[i]); if ((i + 1) != values.Length) { sb.Append(","); } } sb.Append(");"); } else if (session.CurrentConnectionConfig.DbType == DbType.MySql) { if (session.ExistUnique(tableName, values)) return; sb.Append("ALTER TABLE "); sb.Append(tableName); sb.Append(" ADD UNIQUE KEY("); for (int i = 0; i < values.Length; i++) { sb.Append(values[i]); if ((i + 1) != values.Length) { sb.Append(","); } } sb.Append(");"); } else throw new Exception("暂时不支持" + session.CurrentConnectionConfig.DbType + "数据库!"); session.Ado.ExecuteCommand(sb.ToString()); } /// /// 检测列是否存在 /// /// /// /// /// public static bool ColumnExist(this SqlSugarClient session, string tableName, string columnName) { //IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'vrv_paw_rule' AND column_name = 'thresholdMin') THEN //ALTER TABLE vrv_paw_rule ADD COLUMN thresholdMin BIGINT; if (session.CurrentConnectionConfig.DbType == DbType.Sqlite) { var row = session.FindRow("select * from sqlite_master where name = @tableName and sql like @colunName;", new { tableName = tableName, colunName = "%" + columnName + "%" }); { if (row != null) { string sql = row["sql"].ToString().ToLower(); //var reg = Regex.Match(sql, @",?'?\s{0,}\[?""?" + columnName.ToLower() + @"""?\]?'?\s{1,}", RegexOptions.IgnoreCase); var reg = Regex.Match(sql, @",?'?\s{0,}\[?""?" + columnName.ToLower() + @"""?\]?'?\s{1,}", RegexOptions.IgnoreCase); if (reg.Success) return true; else return false; //return true; } } } else if (session.CurrentConnectionConfig.DbType == DbType.MySql) { var row = session.FindRow("select * from information_schema.columns where table_schema = DATABASE() and table_name = @tableName and column_name like @colunName;", new { tableName = tableName, colunName = "%" + columnName + "%" }); if (row != null) return true; } return false; } /// /// 增加列 /// /// /// /// /// public static void AddColumn(this SqlSugarClient session, string tableName, string columnName, string sqlType) { try { if (session.CurrentConnectionConfig.DbType == DbType.Sqlite) session.ExcuteSQL("alter table " + tableName + " add " + columnName + " " + sqlType); else if (session.CurrentConnectionConfig.DbType == DbType.MySql) session.ExcuteSQL("alter table " + tableName + " add column " + columnName + " " + sqlType); } catch (Exception ex) { EventClient.OnEvent("", $"增加字段异常:{ex.Message} - {tableName} - {columnName} - {sqlType}"); } } /// /// 修改字段类型 /// /// /// /// /// public static void UpdateColumnType(this SqlSugarClient session, string tableName, string cloumnName, string newType) { if (session.CurrentConnectionConfig.DbType == DbType.MySql) session.ExcuteSQL("alter table " + tableName + " modify column " + cloumnName + " " + newType + ";"); } /// /// 获得第一条数据 /// /// /// /// /// public static System.Data.DataRow FindRow(this SqlSugarClient session, string strSQL, Object obj) { if (session.CurrentConnectionConfig.DbType == SqlSugar.DbType.MySql) { strSQL = strSQL.Replace("[随机排序]", " order by rand() "); } else { strSQL = strSQL.Replace("[随机排序]", " order by RANDOM() "); } //获取公共属性 var table = session.Ado.GetDataTable(strSQL, obj); if (table.Rows.Count > 0) return table.Rows[0]; return null; } /// /// 获得Table /// /// /// SQL /// /// public static System.Data.DataTable FindTable(this SqlSugarClient session, string strSQL, object param = null) { return session.Ado.GetDataTable(strSQL, param); } /// /// 根据ID查数据、必须有为ID的字段 /// /// /// /// /// public static T FindById(this SqlSugarClient session, long id) where T : new() { return session.Queryable().InSingle(2); } public static int Insertable(this SqlSugarClient session, base_model obj) { var t = obj.GetType(); return session.Insertable(obj).ExecuteReturnIdentity(); } public static List Find(this SqlSugarClient session, string strSQL, Object param = null) where T : new() { var tablename = typeof(T).Name; if (!strSQL.StartsWith("select")) { StringBuilder sb = new StringBuilder(); sb.Append("select * from "); sb.Append(tablename); sb.Append(" where "); sb.Append(strSQL); strSQL = sb.ToString(); } if (session.CurrentConnectionConfig.DbType == DbType.MySql) { strSQL = strSQL.Replace("[随机排序]", " order by rand() "); } else { strSQL = strSQL.Replace("[随机排序]", " order by RANDOM() "); } return session.Ado.SqlQuery(strSQL, param); } public static T FindSingle(this SqlSugarClient session, string strSQL, Object param = null) where T : new() { for (int i = 0; i < 5; i++) { var tablename = typeof(T).Name; if (!strSQL.StartsWith("select", StringComparison.OrdinalIgnoreCase)) { StringBuilder sb = new StringBuilder(); sb.Append("select * from "); sb.Append(tablename); sb.Append(" where "); sb.Append(strSQL); strSQL = sb.ToString(); } if (session.CurrentConnectionConfig.DbType == DbType.MySql) { strSQL = strSQL.Replace("[随机排序]", " order by rand() "); } else { strSQL = strSQL.Replace("[随机排序]", " order by RANDOM() "); } return session.Ado.SqlQuerySingle(strSQL, param); } return default(T); } /// /// 删除表 /// /// /// public static void DropTable(this SqlSugarClient session, string tableName) { session.ExcuteSQL("DROP TABLE " + tableName); } /// /// 删除表 /// /// /// public static void DropTable(this SqlSugarClient session) { string table_name = typeof(T).Name; session.DropTable(table_name); } /// /// 开启事务 /// /// public static void BeginTransaction(this SqlSugarClient session) { session.Ado.BeginTran(); } /// /// 回滚 /// /// public static void Rollback(this SqlSugarClient session) { session.Ado.RollbackTran(); } /// /// 提交 /// /// public static void Commit(this SqlSugarClient session) { session.Ado.CommitTran(); } } }