908 lines
35 KiB
C#
908 lines
35 KiB
C#
|
|
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;
|
|
using Api.Framework.Utils;
|
|
|
|
namespace Api.Framework.Tools
|
|
{
|
|
/// <summary>
|
|
/// 持久层线程池
|
|
/// </summary>
|
|
public class SessionThreadLocal
|
|
{
|
|
private static ThreadLocal<SqlSugarClient> m_SessionLocal = new ThreadLocal<SqlSugarClient>();
|
|
|
|
/// <summary>
|
|
/// 设置持久层到线程池
|
|
/// </summary>
|
|
/// <param name="session"></param>
|
|
public static void Set(SqlSugarClient session)
|
|
{
|
|
m_SessionLocal.Value = session;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从线程池获取持久层
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static SqlSugarClient Get()
|
|
{
|
|
return m_SessionLocal.Value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清空线程池
|
|
/// </summary>
|
|
public static void Clear()
|
|
{
|
|
foreach (var item in m_SessionLocal.Values)
|
|
{
|
|
item.Close();
|
|
item.Dispose();
|
|
}
|
|
m_SessionLocal.Value = null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据库链接配置
|
|
/// </summary>
|
|
public class ConnectionConfig
|
|
{
|
|
/// <summary>
|
|
/// 数据库连接信息
|
|
/// </summary>
|
|
public string ConnectionString { get; set; }
|
|
/// <summary>
|
|
/// 数据库类型
|
|
/// </summary>
|
|
public DatabaseType DatabaseType { get; set; }
|
|
|
|
}
|
|
/// <summary>
|
|
/// 数据库类型枚举,需要扩展类型可在此添加
|
|
/// </summary>
|
|
public enum DatabaseType : int
|
|
{
|
|
/// <summary>
|
|
/// MSSQL数据库
|
|
/// </summary>
|
|
[Description("MSSQL数据库")]
|
|
SQLSERVER = 1,
|
|
/// <summary>
|
|
/// ORACLE数据库
|
|
/// </summary>
|
|
[Description("ORACLE数据库")]
|
|
ORACLE = 2,
|
|
/// <summary>
|
|
/// ACCESS数据库
|
|
/// </summary>
|
|
[Description("ACCESS数据库")]
|
|
ACCESS = 3,
|
|
/// <summary>
|
|
/// MYSQL数据库
|
|
/// </summary>
|
|
[Description("MYSQL数据库")]
|
|
MYSQL = 4,
|
|
/// <summary>
|
|
/// SQLITE数据库
|
|
/// </summary>
|
|
[Description("SQLITE数据库")]
|
|
SQLITE = 5
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据库基础类
|
|
/// </summary>
|
|
public class base_model
|
|
{
|
|
[SugarColumn(IsPrimaryKey = true, IsIdentity = true), Browsable(false), ReadOnly(false)]
|
|
public long id { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页查询结果
|
|
/// </summary>
|
|
/// <typeparam name="T">数据库表实体类</typeparam>
|
|
public class PageResult<T>
|
|
{
|
|
/// <summary>
|
|
/// 分页查询中总记录数
|
|
/// </summary>
|
|
public int Total { get; set; }
|
|
|
|
/// <summary>
|
|
/// 分页查询中结果集合
|
|
/// </summary>
|
|
public List<T> DataList { get; set; }
|
|
}
|
|
|
|
|
|
public class ParamMap
|
|
{
|
|
public List<SugarParameter> 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<SugarParameter>();
|
|
}
|
|
|
|
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; }
|
|
/// <summary>
|
|
/// 设置排序字段及排序方式
|
|
/// </summary>
|
|
/// <param name="orderFields">排序字段</param>
|
|
/// <param name="isDesc">是否为递减排序</param>
|
|
public void setOrderFields(string orderFields, bool isDesc)
|
|
{
|
|
OrderBySQL = $" Order by {orderFields} " + (isDesc ? "desc" : "asc") + " ";
|
|
}
|
|
}
|
|
public static class SqlSugarExtend
|
|
{
|
|
|
|
public static PageResult<T> FindPage<T>(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<T> result = new PageResult<T>();
|
|
|
|
result.DataList = session.Ado.SqlQuery<T>(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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建表
|
|
/// </summary>
|
|
/// <param name="type"></param>
|
|
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<T>(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<T>(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=(?<db_host>.*?);Initial Catalog=(?<db_name>.*?);Persist Security Info=True;User ID=(?<db_user>.*?);Password=(?<db_pass>.*?);Port=(?<db_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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 索引是否存在
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="session"></param>
|
|
/// <param name="indexName"></param>
|
|
/// <returns></returns>
|
|
public static bool IndexExist<T>(this SqlSugarClient session, string indexName)
|
|
{
|
|
return IndexExist(session, typeof(T).Name, indexName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 索引是否存在
|
|
/// </summary>
|
|
/// <param name="session"></param>
|
|
/// <param name="tableName"></param>
|
|
/// <param name="indexName"></param>
|
|
/// <returns></returns>
|
|
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=(?<db_host>.*?);Initial Catalog=(?<db_name>.*?);Persist Security Info=True;User ID=(?<db_user>.*?);Password=(?<db_pass>.*?);Port=(?<db_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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加索引
|
|
/// </summary>
|
|
public static bool AddIndex<T>(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);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 判断表是否存在
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static bool TableExist<T>(this SqlSugarClient session)
|
|
{
|
|
return TableExist(session, typeof(T).Name);
|
|
}
|
|
/// <summary>
|
|
/// 判断表是否存在
|
|
/// </summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加唯一约束
|
|
/// </summary>
|
|
public static void AddUnique<T>(this SqlSugarClient session, params object[] values)
|
|
{
|
|
AddUnique(session, typeof(T).Name, values);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行sql
|
|
/// </summary>
|
|
/// <param name="session"></param>
|
|
/// <param name="sql"></param>
|
|
/// <param name="parameters"></param>
|
|
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<string> ors = new List<string>();
|
|
//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<string> strs = new List<string>();
|
|
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加唯一约束
|
|
/// </summary>
|
|
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());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检测列是否存在
|
|
/// </summary>
|
|
/// <param name="session"></param>
|
|
/// <param name="tableName"></param>
|
|
/// <param name="columnName"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 增加列
|
|
/// </summary>
|
|
/// <param name="session"></param>
|
|
/// <param name="tableName"></param>
|
|
/// <param name="columnName"></param>
|
|
/// <param name="sqlType"></param>
|
|
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)
|
|
{
|
|
LogHelper.GetSingleObj().Error("启动表字段检测", $"增加字段异常:{ex.Message} - {tableName} - {columnName} - {sqlType}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改字段类型
|
|
/// </summary>
|
|
/// <param name="session"></param>
|
|
/// <param name="tableName"></param>
|
|
/// <param name="cloumnName"></param>
|
|
/// <param name="newType"></param>
|
|
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 + ";");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获得第一条数据
|
|
/// </summary>
|
|
/// <param name="session"></param>
|
|
/// <param name="strSQL"></param>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获得Table
|
|
/// </summary>
|
|
/// <param name="session"></param>
|
|
/// <param name="strSQL">SQL</param>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
public static System.Data.DataTable FindTable(this SqlSugarClient session, string strSQL, object param = null)
|
|
{
|
|
return session.Ado.GetDataTable(strSQL, param);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ID查数据、必须有为ID的字段
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="session"></param>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public static T FindById<T>(this SqlSugarClient session, long id) where T : new()
|
|
{
|
|
return session.Queryable<T>().InSingle(2);
|
|
}
|
|
|
|
public static int Insertable(this SqlSugarClient session, base_model obj)
|
|
{
|
|
var t = obj.GetType();
|
|
|
|
return session.Insertable(obj).ExecuteReturnIdentity();
|
|
}
|
|
|
|
public static List<T> Find<T>(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<T>(strSQL, param);
|
|
}
|
|
|
|
public static T FindSingle<T>(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<T>(strSQL, param);
|
|
}
|
|
return default(T);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除表
|
|
/// </summary>
|
|
/// <param name="session"></param>
|
|
/// <param name="tableName"></param>
|
|
public static void DropTable(this SqlSugarClient session, string tableName)
|
|
{
|
|
session.ExcuteSQL("DROP TABLE " + tableName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除表
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="session"></param>
|
|
public static void DropTable<T>(this SqlSugarClient session)
|
|
{
|
|
string table_name = typeof(T).Name;
|
|
session.DropTable(table_name);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 开启事务
|
|
/// </summary>
|
|
/// <param name="session"></param>
|
|
public static void BeginTransaction(this SqlSugarClient session)
|
|
{
|
|
session.Ado.BeginTran();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 回滚
|
|
/// </summary>
|
|
/// <param name="session"></param>
|
|
public static void Rollback(this SqlSugarClient session)
|
|
{
|
|
session.Ado.RollbackTran();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 提交
|
|
/// </summary>
|
|
/// <param name="session"></param>
|
|
public static void Commit(this SqlSugarClient session)
|
|
{
|
|
session.Ado.CommitTran();
|
|
}
|
|
}
|
|
|
|
}
|