using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Linq; using System.Linq.Expressions; using Dapper; using Learun.Util; namespace Learun.DataBase.Repository { /// /// 仓储类。 /// public class Repository : IRepository { /// /// 有点类似于全局的一个where条件 /// private DbWhere dbWhere = (DbWhere)WebHelper.GetHttpItems("DataAhthorCondition"); public IDatabase db; public Repository(IDatabase idatabase) { this.db = idatabase; } public DbConnection getDbConnection() { return this.db.getDbConnection(); } public IRepository BeginTrans() { this.db.BeginTrans(); return this; } public void Commit() { this.db.Commit(); } public void Rollback() { this.db.Rollback(); } public int ExecuteBySql(string strSql) { return this.db.ExecuteBySql(strSql); } public int ExecuteBySql(string strSql, object dbParameter) { return this.db.ExecuteBySql(strSql, dbParameter); } public int ExecuteByProc(string procName) { return this.db.ExecuteByProc(procName); } public int ExecuteByProc(string procName, object dbParameter) { return this.db.ExecuteByProc(procName, dbParameter); } public T ExecuteByProc(string procName) where T : class { return this.db.ExecuteByProc(procName); } public T ExecuteByProc(string procName, object dbParameter) where T : class { return this.db.ExecuteByProc(procName, dbParameter); } public IEnumerable QueryByProc(string procName) where T : class { return this.db.QueryByProc(procName); } public IEnumerable QueryByProc(string procName, object dbParameter) where T : class { return this.db.QueryByProc(procName, dbParameter); } public int Insert(T entity) where T : class { return this.db.Insert(entity); } public int InsertByNoMap(T entity, string tablename) where T : class { return this.db.InsertByNoMap(entity, tablename); } public int InsertByNoMap(List entities, string tablename) where T : class { return this.db.InsertByNoMap(entities, tablename); } public int Insert(List entity) where T : class { return this.db.Insert(entity); } public int Delete(T entity) where T : class { return this.db.Delete(entity); } public int Delete(List entity) where T : class { return this.db.Delete(entity); } public int Delete(Expression> condition) where T : class, new() { return this.db.Delete(condition); } public int Update(T entity) where T : class { return this.db.Update(entity); } public int UpdateByNoMap(T entity, string tablename, string pkcolumn) where T : class { return this.db.UpdateByNoMap(entity, tablename, pkcolumn); } public int UpdateByNoMap(List entities, string tablename, string pkcolumn) where T : class { return this.db.UpdateByNoMap(entities, tablename, pkcolumn); } public int UpdateEx(T entity) where T : class { return this.db.UpdateEx(entity); } public int Update(List entity) where T : class { return this.db.Update(entity); } /// /// 仓储下的方法。根据主键找一个实体 /// /// /// /// public T FindEntity(object keyValue) where T : class { return this.db.FindEntity(keyValue); } public T FindEntity(Expression> condition) where T : class, new() { return this.db.FindEntity(condition); } /// /// 仓储下的方法。 /// /// /// /// /// public T FindEntity(string strSql, object dbParameter) where T : class, new() { if (this.dbWhere != null) { int num = strSql.ToUpper().IndexOf("ORDER BY"); if (num > 0) { strSql = strSql.Substring(0, num); string arg = strSql.Substring(num); strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, this.dbWhere.sql, arg); } else { strSql = string.Format(" select * From ({0})t Where {1} ", strSql, this.dbWhere.sql); } DynamicParameters dynamicParameters = SqlHelper.FieldValueParamToParameter(this.dbWhere.dbParameters); dynamicParameters.AddDynamicParams(dbParameter); return this.db.FindEntity(strSql, dynamicParameters); } return this.db.FindEntity(strSql, dbParameter); } public IQueryable IQueryable() where T : class, new() { return this.db.IQueryable(); } public IQueryable IQueryable(Expression> condition) where T : class, new() { return this.db.IQueryable(condition); } public IEnumerable FindList() where T : class, new() { return this.db.FindList(); } public IEnumerable FindList(string strSql) where T : class { if (this.dbWhere != null) { int num = strSql.ToUpper().IndexOf("ORDER BY"); if (num > 0) { strSql = strSql.Substring(0, num); string arg = strSql.Substring(num); strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, this.dbWhere.sql, arg); } else { strSql = string.Format(" select * From ({0})t Where {1} ", strSql, this.dbWhere.sql); } DynamicParameters dbParameter = SqlHelper.FieldValueParamToParameter(this.dbWhere.dbParameters); return this.db.FindList(strSql, dbParameter); } return this.db.FindList(strSql); } public IEnumerable FindList(string strSql, object dbParameter) where T : class { if (this.dbWhere != null) { int num = strSql.ToUpper().IndexOf("ORDER BY"); if (num > 0) { strSql = strSql.Substring(0, num); string arg = strSql.Substring(num); strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, this.dbWhere.sql, arg); } else { strSql = string.Format(" select * From ({0})t Where {1} ", strSql, this.dbWhere.sql); } DynamicParameters dynamicParameters = SqlHelper.FieldValueParamToParameter(this.dbWhere.dbParameters); dynamicParameters.AddDynamicParams(dbParameter); return this.db.FindList(strSql, dynamicParameters); } return this.db.FindList(strSql, dbParameter); } public IEnumerable FindList(Pagination pagination) where T : class, new() { int records = pagination.records; if (string.IsNullOrEmpty(pagination.sidx)) { pagination.sidx = ""; pagination.sord = "asc"; } IEnumerable arg_6B_0 = this.db.FindList(pagination.sidx, pagination.sord.ToLower() == "asc", pagination.rows, pagination.page, out records); pagination.records = records; return arg_6B_0; } public IEnumerable FindList(Expression> condition, Pagination pagination) where T : class, new() { int records = pagination.records; if (string.IsNullOrEmpty(pagination.sidx)) { pagination.sidx = ""; pagination.sord = "asc"; } IEnumerable arg_6C_0 = this.db.FindList(condition, pagination.sidx, pagination.sord.ToLower() == "asc", pagination.rows, pagination.page, out records); pagination.records = records; return arg_6C_0; } public IEnumerable FindList(Expression> condition) where T : class, new() { return this.db.FindList(condition); } public IEnumerable FindList(string strSql, Pagination pagination) where T : class { int records = pagination.records; if (string.IsNullOrEmpty(pagination.sidx)) { pagination.sidx = ""; pagination.sord = "asc"; } if (this.dbWhere != null) { int num = strSql.ToUpper().IndexOf("ORDER BY"); if (num > 0) { strSql = strSql.Substring(0, num); string arg = strSql.Substring(num); strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, this.dbWhere.sql, arg); } else { strSql = string.Format(" select * From ({0})t Where {1} ", strSql, this.dbWhere.sql); } DynamicParameters dbParameter = SqlHelper.FieldValueParamToParameter(this.dbWhere.dbParameters); IEnumerable arg_E3_0 = this.db.FindList(strSql, dbParameter, pagination.sidx, pagination.sord.ToLower() == "asc", pagination.rows, pagination.page, out records); pagination.records = records; return arg_E3_0; } IEnumerable arg_126_0 = this.db.FindList(strSql, pagination.sidx, pagination.sord.ToLower() == "asc", pagination.rows, pagination.page, out records); pagination.records = records; return arg_126_0; } public IEnumerable FindList(string strSql, object dbParameter, Pagination pagination) where T : class { int records = pagination.records; if (string.IsNullOrEmpty(pagination.sidx)) { pagination.sidx = ""; pagination.sord = "asc"; } if (this.dbWhere != null) { int num = strSql.ToUpper().IndexOf("ORDER BY"); if (num > 0) { strSql = strSql.Substring(0, num); string arg = strSql.Substring(num); strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, this.dbWhere.sql, arg); } else { strSql = string.Format(" select * From ({0})t Where {1} ", strSql, this.dbWhere.sql); } DynamicParameters dynamicParameters = SqlHelper.FieldValueParamToParameter(this.dbWhere.dbParameters); dynamicParameters.AddDynamicParams(dbParameter); IEnumerable arg_EA_0 = this.db.FindList(strSql, dynamicParameters, pagination.sidx, pagination.sord.ToLower() == "asc", pagination.rows, pagination.page, out records); pagination.records = records; return arg_EA_0; } IEnumerable arg_12E_0 = this.db.FindList(strSql, dbParameter, pagination.sidx, pagination.sord.ToLower() == "asc", pagination.rows, pagination.page, out records); pagination.records = records; return arg_12E_0; } public DataTable FindTable(string strSql) { if (this.dbWhere != null) { int num = strSql.ToUpper().IndexOf("ORDER BY"); if (num > 0) { strSql = strSql.Substring(0, num); string arg = strSql.Substring(num); strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, this.dbWhere.sql, arg); } else { strSql = string.Format(" select * From ({0})t Where {1} ", strSql, this.dbWhere.sql); } return this.db.FindTable(strSql, SqlHelper.FieldValueParamToParameter(this.dbWhere.dbParameters)); } return this.db.FindTable(strSql); } public DataTable FindTable(string strSql, object dbParameter) { if (this.dbWhere != null) { int num = strSql.ToUpper().IndexOf("ORDER BY"); if (num > 0) { strSql = strSql.Substring(0, num); string arg = strSql.Substring(num); strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, this.dbWhere.sql, arg); } else { strSql = string.Format(" select * From ({0})t Where {1} ", strSql, this.dbWhere.sql); } DynamicParameters dynamicParameters = SqlHelper.FieldValueParamToParameter(this.dbWhere.dbParameters); dynamicParameters.AddDynamicParams(dbParameter); return this.db.FindTable(strSql, dynamicParameters); } return this.db.FindTable(strSql, dbParameter); } public DataTable FindTable(string strSql, Pagination pagination) { int records = pagination.records; DataTable result; if (this.dbWhere != null) { int num = strSql.ToUpper().IndexOf("ORDER BY"); if (num > 0) { strSql = strSql.Substring(0, num); string arg = strSql.Substring(num); strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, this.dbWhere.sql, arg); } else { strSql = string.Format(" select * From ({0})t Where {1} ", strSql, this.dbWhere.sql); } DynamicParameters dbParameter = SqlHelper.FieldValueParamToParameter(this.dbWhere.dbParameters); result = this.db.FindTable(strSql, dbParameter, pagination.sidx, pagination.sord.ToLower() == "asc", pagination.rows, pagination.page, out records); } else { result = this.db.FindTable(strSql, pagination.sidx, pagination.sord.ToLower() == "asc", pagination.rows, pagination.page, out records); } pagination.records = records; return result; } public DataTable FindTable(string strSql, object dbParameter, Pagination pagination) { int records = pagination.records; DataTable result; if (this.dbWhere != null) { int num = strSql.ToUpper().IndexOf("ORDER BY"); if (num > 0) { strSql = strSql.Substring(0, num); string arg = strSql.Substring(num); strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, this.dbWhere.sql, arg); } else { strSql = string.Format(" select * From ({0})t Where {1} ", strSql, this.dbWhere.sql); } DynamicParameters dynamicParameters = SqlHelper.FieldValueParamToParameter(this.dbWhere.dbParameters); dynamicParameters.AddDynamicParams(dbParameter); result = this.db.FindTable(strSql, dynamicParameters, pagination.sidx, pagination.sord.ToLower() == "asc", pagination.rows, pagination.page, out records); } else { result = this.db.FindTable(strSql, dbParameter, pagination.sidx, pagination.sord.ToLower() == "asc", pagination.rows, pagination.page, out records); } pagination.records = records; return result; } public object FindObject(string strSql) { if (this.dbWhere != null) { int num = strSql.ToUpper().IndexOf("ORDER BY"); if (num > 0) { strSql = strSql.Substring(0, num); string arg = strSql.Substring(num); strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, this.dbWhere.sql, arg); } else { strSql = string.Format(" select * From ({0})t Where {1} ", strSql, this.dbWhere.sql); } DynamicParameters dbParameter = SqlHelper.FieldValueParamToParameter(this.dbWhere.dbParameters); return this.db.FindObject(strSql, dbParameter); } return this.db.FindObject(strSql); } public object FindObject(string strSql, object dbParameter) { if (this.dbWhere != null) { int num = strSql.ToUpper().IndexOf("ORDER BY"); if (num > 0) { strSql = strSql.Substring(0, num); string arg = strSql.Substring(num); strSql = string.Format(" select * From ({0})t Where {1} {2} ", strSql, this.dbWhere.sql, arg); } else { strSql = string.Format(" select * From ({0})t Where {1} ", strSql, this.dbWhere.sql); } DynamicParameters dynamicParameters = SqlHelper.FieldValueParamToParameter(this.dbWhere.dbParameters); dynamicParameters.AddDynamicParams(dbParameter); return this.db.FindObject(strSql, dynamicParameters); } return this.db.FindObject(strSql, dbParameter); } public IEnumerable GetDBTable() where T : class, new() { return this.db.GetDBTable(); } public IEnumerable GetDBTableFields(string tableName) where T : class, new() { return this.db.GetDBTableFields(tableName); } } }