cs
复制代码
using CommDB.Basic;
using CommDB.Exceptions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace CommDB
{
/// <summary>
/// 封装ORM
/// 使用方法基本类似于 SqlSugar ORM 的 SqlSugarClient
/// 1.封装单表查询、两表连接查询和三表连接查询
/// 2.封装常用的重载方式查询
/// 3.封装 sql 查询 调用Ado
/// </summary>
public class SqlClient
{
//这个ado封装了 sqlsugar 的ado
private readonly SqlSugarClient db; //私有变量
public readonly AdoBase Ado; //直接可以使用 执行sql 相关
public readonly AopProvider Apo;
#region 初始化函数
/// <summary>
///
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="dbType">数据库类型 "MySql"、 "SqlServer"、 "Sqlite" "Oracle";</param>
public SqlClient(string connectionString, DbType dbType = DbType.MySql)
{
db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = connectionString, //连接字符串
DbType = dbType, // 数据库类型
IsAutoCloseConnection = true, //是否自动关闭连接
//InitKeyType = InitKeyType.SystemTable
});
Ado = new AdoBase(db);
Apo = db.Aop;
}
/// <summary>
///
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="dbType">数据库类型 "MySql"、 "SqlServer"、 "Sqlite" "Oracle";</param>
/// <param name="isAutoCloseConnection">是否自动关闭数据库</param>
/// <param name="initKeyType">0使用系统主键 1 使用 属性中定义的主键</param>
public SqlClient(string connectionString, DbType dbType, bool isAutoCloseConnection = true, int initKeyType = 0)
{
ConnectionConfig config = new ConnectionConfig()
{
ConnectionString = connectionString, //连接字符串
DbType = dbType, // 数据库类型
IsAutoCloseConnection = isAutoCloseConnection, //是否自动关闭连接
};
if (initKeyType == 0)
{
config.InitKeyType = InitKeyType.SystemTable;
}
else if (initKeyType == 1)
{
config.InitKeyType = InitKeyType.Attribute;
}
db = new SqlSugarClient(config);
Ado = new AdoBase(db);
}
#endregion
#region 其他变量
/// <summary>
/// 临时变量 可以存一些额外的数据
/// </summary>
public Dictionary<string, object> TempItems
{
get
{
return db.TempItems;
}
set
{
db.TempItems = value;
}
}
#endregion
#region 查询
#region 递归实现查询
public QueryBase<T> Queryable<T>() where T : class, new()
{
return new QueryBase<T>(db).Queryable();
}
public QueryBase<T, T2> Queryable<T, T2>(Expression<Func<T, T2, object[]>> joinExpression) where T : class, new()
{
return new QueryBase<T, T2>(db).Queryable(joinExpression);
}
public QueryBase<T, T2, T3> Queryable<T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression) where T : class, new()
{
return new QueryBase<T, T2, T3>(db).Queryable(joinExpression);
}
#endregion
#region 单表查询 重载实现查询
public List<T> QueryOrderBy<T>(Expression<Func<T, bool>> where = null, Expression<Func<T, object>> orderBy = null, string ascOrDesc = "asc") where T : class, new()
{
return Query(@where, orderBy, ascOrDesc);
}
public List<T> QueryGroupBy<T>(Expression<Func<T, bool>> where = null, Expression<Func<T, object>> groupBy = null) where T : class, new()
{
return Query(@where, null, null, groupBy);
}
public List<T> QueryTop<T>(Expression<Func<T, bool>> where = null, int? top = null) where T : class, new()
{
return Query(@where, null, null, null, top);
}
public List<T> QueryPageList<T>(Expression<Func<T, bool>> where = null, int? pageSize = null, int? pageIndex = null) where T : class, new()
{
return Query(@where, null, null, null, null, pageSize, pageIndex);
}
public List<T> QueryPageList<T>(Expression<Func<T, bool>> where = null, Expression<Func<T, object>> orderBy = null, string ascOrDesc = "asc", int? pageSize = null, int? pageIndex = null) where T : class, new()
{
return Query(@where, orderBy, ascOrDesc, null, null, pageSize, pageIndex);
}
public List<T> QueryPageList<T>(Expression<Func<T, bool>> where = null, Expression<Func<T, object>> groupBy = null, int? pageSize = null, int? pageIndex = null) where T : class, new()
{
return Query(@where, null, null, groupBy, null, pageSize, pageIndex);
}
public List<T> Query<T>(Expression<Func<T, bool>> where = null, Expression<Func<T, object>> orderBy = null, string ascOrDesc = "asc", Expression<Func<T, object>> groupBy = null, int? top = null, int? pageSize = null, int? pageIndex = null) where T : class, new()
{
var isQueryable = db.Queryable<T>();
if (@where != null)
{
isQueryable = isQueryable.Where(@where);
}
if (orderBy != null)
{
if (ascOrDesc.ToLower() == "asc")
{
isQueryable.OrderBy(orderBy, OrderByType.Asc);
}
else
{
isQueryable.OrderBy(orderBy, OrderByType.Desc);
}
}
if (groupBy != null)
{
isQueryable.GroupBy(groupBy);
}
if (top != null)
{
isQueryable.Take(top.Value);
}
else if (pageIndex != null && pageSize != null)
{
isQueryable.ToPageList(pageSize.Value, pageIndex.Value);
}
return isQueryable.ToList();
}
#endregion
#region 两个表连接查询 重载实现
public List<TResult> QueryOrderBy<TResult, T, T2>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, bool>> where = null, Expression<Func<T, T2, object>> orderBy = null, string ascOrDesc = "asc")
where TResult : class, new()
where T : class, new()
{
return Query<TResult, T, T2>(joinExpression, @where, orderBy, ascOrDesc);
}
public List<TResult> QueryGroupBy<TResult, T, T2>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, bool>> where = null, Expression<Func<T, T2, object>> groupBy = null)
where TResult : class, new()
where T : class, new()
{
return Query<TResult, T, T2>(joinExpression, @where, null, null, groupBy);
}
public List<TResult> QueryTop<TResult, T, T2>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, bool>> where = null, int? top = null)
where TResult : class, new()
where T : class, new()
{
return Query<TResult, T, T2>(joinExpression, @where, null, null, null, top);
}
public List<TResult> QueryPageList<TResult, T, T2>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, bool>> where = null, int? pageSize = null, int? pageIndex = null)
where TResult : class, new()
where T : class, new()
{
return Query<TResult, T, T2>(joinExpression, @where, null, null, null, null, pageSize, pageIndex);
}
public List<TResult> QueryPageList<TResult, T, T2>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, bool>> where = null, Expression<Func<T, T2, object>> orderBy = null, string ascOrDesc = "asc", int? pageSize = null, int? pageIndex = null)
where TResult : class, new()
where T : class, new()
{
return Query<TResult, T, T2>(joinExpression, @where, orderBy, ascOrDesc, null, null, pageSize, pageIndex);
}
public List<TResult> QueryPageList<TResult, T, T2>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, bool>> where = null, Expression<Func<T, T2, object>> groupBy = null, int? pageSize = null, int? pageIndex = null)
where TResult : class, new()
where T : class, new()
{
return Query<TResult, T, T2>(joinExpression, @where, null, null, groupBy, null, pageSize, pageIndex);
}
public List<TResult> Query<TResult, T, T2>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, bool>> where = null, Expression<System.Func<T, T2, object>> orderBy = null, string ascOrDesc = "asc", Expression<System.Func<T, T2, object>> groupBy = null, int? top = null, int? pageSize = null, int? pageIndex = null)
where TResult : class, new()
where T : class, new()
{
if (joinExpression == null)
{
throw new CusCommException("连接表达式不能为空");
}
var isQueryable = db.Queryable<T, T2>(joinExpression);
if (@where != null)
{
isQueryable = isQueryable.Where(@where);
}
if (orderBy != null)
{
if (ascOrDesc.ToLower() == "asc")
{
isQueryable.OrderBy(orderBy, OrderByType.Asc);
}
else
{
isQueryable.OrderBy(orderBy, OrderByType.Desc);
}
}
if (groupBy != null)
{
isQueryable.GroupBy(groupBy);
}
if (top != null)
{
isQueryable.Take(top.Value);
}
else if (pageIndex != null && pageSize != null)
{
isQueryable.ToPageList(pageSize.Value, pageIndex.Value);
}
return isQueryable.Select((t, t1) => new TResult()).ToList();
}
#endregion
#region 三个表连接查询 重载实现
public List<TResult> QueryOrderBy<TResult, T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, bool>> where = null, Expression<Func<T, T2, T3, object>> orderBy = null, string ascOrDesc = "asc")
where TResult : class, new()
where T : class, new()
{
return Query<TResult, T, T2, T3>(joinExpression, where, orderBy, ascOrDesc);
}
public List<TResult> QueryGroupBy<TResult, T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, bool>> where = null, Expression<Func<T, T2, T3, object>> groupBy = null)
where TResult : class, new()
where T : class, new()
{
return Query<TResult, T, T2, T3>(joinExpression, @where, null, null, groupBy);
}
public List<TResult> QueryTop<TResult, T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, bool>> where = null, int? top = null)
where TResult : class, new()
where T : class, new()
{
return Query<TResult, T, T2, T3>(joinExpression, where, null, null, null, top);
}
public List<TResult> QueryPageList<TResult, T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, bool>> where = null, int? pageSize = null, int? pageIndex = null)
where TResult : class, new()
where T : class, new()
{
return Query<TResult, T, T2, T3>(joinExpression, @where, null, null, null, null, pageSize, pageIndex);
}
public List<TResult> QueryPageList<TResult, T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, bool>> where = null, Expression<Func<T, T2, T3, object>> orderBy = null, string ascOrDesc = "asc", int? pageSize = null, int? pageIndex = null)
where TResult : class, new()
where T : class, new()
{
return Query<TResult, T, T2, T3>(joinExpression, @where, orderBy, ascOrDesc, null, null, pageSize, pageIndex);
}
public List<TResult> QueryPageList<TResult, T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, bool>> where = null, Expression<Func<T, T2, T3, object>> groupBy = null, int? pageSize = null, int? pageIndex = null)
where TResult : class, new()
where T : class, new()
{
return Query<TResult, T, T2, T3>(joinExpression, where, null, null, groupBy, null, pageSize, pageIndex);
}
public List<TResult> Query<TResult, T, T2, T3>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, bool>> where = null, Expression<System.Func<T, T2, T3, object>> orderBy = null, string ascOrDesc = "asc", Expression<System.Func<T, T2, T3, object>> groupBy = null, int? top = null, int? pageSize = null, int? pageIndex = null)
where TResult : class, new()
where T : class, new()
{
if (joinExpression == null)
{
throw new Exception("连接表达式不能为空");
}
var isQueryable = db.Queryable<T, T2, T3>(joinExpression);
if (@where != null)
{
isQueryable = isQueryable.Where(@where);
}
if (orderBy != null)
{
if (ascOrDesc.ToLower() == "asc")
{
isQueryable.OrderBy(orderBy, OrderByType.Asc);
}
else
{
isQueryable.OrderBy(orderBy, OrderByType.Desc);
}
}
if (groupBy != null)
{
isQueryable.GroupBy(groupBy);
}
if (top != null)
{
isQueryable.Take(top.Value);
}
else if (pageIndex != null && pageSize != null)
{
isQueryable.ToPageList(pageSize.Value, pageIndex.Value);
}
return isQueryable.Select((t, t1) => new TResult()).ToList();
}
#endregion
#endregion
#region 插入
#region 直接调用
public virtual int Insertable<T>(List<T> insertObjs) where T : class, new()
{
if (insertObjs == null || insertObjs.Count == 0)
{
return 0;
}
Utity<T>.RemoveSpace(insertObjs);
return db.Insertable<T>(insertObjs).ExecuteCommand();
}
public virtual int Insertable<T>(T insertObj) where T : class, new()
{
Utity<T>.RemoveSpace(insertObj);
return this.Insertable(new List<T>() { insertObj });
}
public virtual T InsertableReturnEntity<T>(T insertObj) where T : class, new()
{
Utity<T>.RemoveSpace(insertObj);
return db.Insertable<T>(insertObj).ExecuteReturnEntity();
}
//插入不同类型的 对象 对象必须 可以 where T : class, new()
public virtual int Insertable(List<dynamic> insertObjs)
{
if (insertObjs == null || insertObjs.Count == 0)
{
return 0;
}
try
{
db.Ado.BeginTran();
foreach (var insertObj in insertObjs)
{
db.Insertable(insertObj).ExecuteCommand();
}
db.Ado.CommitTran();
return insertObjs.Count;
}
catch (Exception)
{
db.Ado.RollbackTran();
return 0;
}
}
#endregion
#region 递归调用
public virtual InsertBase<T> InsertableBase<T>(List<T> insertObjs) where T : class, new()
{
return new InsertBase<T>(db).Insertable(insertObjs);
}
public virtual InsertBase<T> InsertableBase<T>(T insertObj) where T : class, new()
{
return new InsertBase<T>(db).Insertable(insertObj);
}
public virtual InsertBase<T> InsertableBase<T>(dynamic insertDynamicObject) where T : class, new()
{
return new InsertBase<T>(db).Insertable(insertDynamicObject);
}
#endregion
#endregion
#region 更新
#region 直接调用
public virtual int Updateable<T>(List<T> UpdateObjs) where T : class, new()
{
if (UpdateObjs == null || UpdateObjs.Count == 0)
{
return 0;
}
Utity<T>.RemoveSpace(UpdateObjs);
return db.Updateable<T>(UpdateObjs).Where(true).ExecuteCommand();
}
/// <summary>
/// 更新单个字段
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="UpdataObj"></param>
/// <param name="columns"></param>
public virtual void UpdateColumns<T>(T UpdataObj, Expression<Func<T, object>> columns) where T : class, new()
{
Utity<T>.RemoveSpace(UpdataObj);
db.Updateable(UpdataObj).UpdateColumns(columns).ExecuteCommand();
}
public virtual int Updateable<T>(T UpdateObj) where T : class, new()
{
Utity<T>.RemoveSpace(UpdateObj);
return this.Updateable(new List<T>() { UpdateObj });
}
//更新不同类型的 对象 对象必须 可以 where T : class, new()
public virtual int Updateable(List<dynamic> UpdateObjs)
{
if (UpdateObjs == null || UpdateObjs.Count == 0)
{
return 0;
}
try
{
db.Ado.BeginTran();
foreach (var updateObj in UpdateObjs)
{
db.Updateable(updateObj).ExecuteCommand();
}
db.Ado.CommitTran();
return UpdateObjs.Count;
}
catch (Exception)
{
db.Ado.RollbackTran();
return 0;
}
}
#endregion
#region 递归调用
public virtual UpdateBase<T> UpdateableBase<T>() where T : class, new()
{
return new UpdateBase<T>(db).Updateable();
}
public virtual UpdateBase<T> UpdateableBase<T>(List<T> UpdateObjs) where T : class, new()
{
return new UpdateBase<T>(db).Updateable(UpdateObjs);
}
public virtual UpdateBase<T> UpdateableBase<T>(T[] UpdateObjs) where T : class, new()
{
return new UpdateBase<T>(db).Updateable(UpdateObjs);
}
public virtual UpdateBase<T> UpdateableBase<T>(T UpdateObj) where T : class, new()
{
return new UpdateBase<T>(db).Updateable(UpdateObj);
}
#endregion
#endregion
#region 删除
#region 直接调用
private IDeleteable<T> Deleteable<T>() where T : class, new()
{
return db.Deleteable<T>();
}
public virtual int Deleteable<T>(Expression<Func<T, bool>> expression) where T : class, new()
{
return this.Deleteable<T>().Where(expression).ExecuteCommand();
}
public virtual int Deleteable<T>(T deleteObj) where T : class, new()
{
return this.Deleteable<T>().Where(deleteObj).ExecuteCommand();
}
public virtual int Deleteable<T>(List<T> deleteObjs) where T : class, new()
{
return this.Deleteable<T>().Where(deleteObjs).ExecuteCommand();
}
//删除不同类型的 对象 对象必须 可以 where T : class, new()
public virtual int Deleteable(List<dynamic> deleteObjs)
{
if (deleteObjs == null || deleteObjs.Count == 0)
{
return 0;
}
try
{
db.Ado.BeginTran();
foreach (var deleteObj in deleteObjs)
{
db.Deleteable(deleteObj).ExecuteCommand();
}
db.Ado.CommitTran();
return deleteObjs.Count;
}
catch (Exception)
{
db.Ado.RollbackTran();
return 0;
}
}
#endregion
#region 递归调用
public virtual DeleteBase<T> DeleteableBase<T>() where T : class, new()
{
return new DeleteBase<T>(db).Deleteable();
}
public virtual DeleteBase<T> DeleteableBase<T>(Expression<Func<T, bool>> expression) where T : class, new()
{
return new DeleteBase<T>(db).Deleteable(expression);
}
public virtual DeleteBase<T> DeleteableBase<T>(T deleteObj) where T : class, new()
{
return new DeleteBase<T>(db).Deleteable(deleteObj);
}
public DeleteBase<T> DeleteableBase<T>(List<T> deleteObjs) where T : class, new()
{
return new DeleteBase<T>(db).Deleteable(deleteObjs);
}
#endregion
#endregion
}
}