/// <summary> /// 创建单表查询对象 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public Queryable <T> Queryable <T>() where T : new() { var queryable = new Queryable <T>() { DB = this }; //别名表 if (_mappingTableList.IsValuable()) { string name = typeof(T).Name; if (_mappingTableList.Any(it => it.Key == name)) { queryable.TableName = _mappingTableList.First(it => it.Key == name).Value; } } //全局过滤器 if (CurrentFilterKey.IsValuable()) { if (_filterFuns.IsValuable() && _filterFuns.ContainsKey(CurrentFilterKey)) { var filterInfo = _filterFuns[CurrentFilterKey]; var filterValue = filterInfo(); string whereStr = string.Format(" AND {0} ", filterValue.Key); queryable.Where.Add(whereStr); if (filterValue.Value != null) { queryable.Params.AddRange(SqlSugarTool.GetParameters(filterValue.Value)); } return(queryable); } } return(queryable); }
/// <summary> /// 根据SQL语句将结果集映射到List《T》 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="reader"></param> /// <param name="pars"></param> /// <returns></returns> public List <T> SqlQuery <T>(string sql, List <MySqlParameter> pars) { MySqlDataReader reader = null; //全局过滤器 if (CurrentFilterKey.IsValuable()) { if (_filterFuns.IsValuable() && _filterFuns.ContainsKey(CurrentFilterKey)) { var filterInfo = _filterFuns[CurrentFilterKey]; var filterValue = filterInfo(); sql += string.Format(" AND {0} ", filterValue.Key); if (filterValue.Value != null) { pars.AddRange(SqlSugarTool.GetParameters(filterValue.Value)); } } } var type = typeof(T); sql = string.Format(@"/*{0}*/ {1}", type.Name, sql); reader = GetReader(sql, pars.ToArray()); string fields = sql; if (sql.Length > 101) { fields = sql.Substring(0, 100); } var reval = SqlSugarTool.DataReaderToList <T>(type, reader, fields); fields = null; sql = null; return(reval); }
/// <summary> /// 创建多表查询对象 /// </summary> public Sqlable Sqlable() { var sqlable = new Sqlable() { DB = this }; //全局过滤器 if (CurrentFilterKey.IsValuable()) { if (_filterFuns.IsValuable() && _filterFuns.ContainsKey(CurrentFilterKey)) { var filterInfo = _filterFuns[CurrentFilterKey]; var filterVlue = filterInfo(); string whereStr = string.Format(" AND {0} ", filterVlue.Key); sqlable.Where.Add(whereStr); if (filterVlue.Value != null) { sqlable.Params.AddRange(SqlSugarTool.GetParameters(filterVlue.Value)); } return(sqlable); } } return(sqlable); }
/// <summary> /// 条件筛选 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queryable"></param> /// <param name="whereString"></param> /// <returns></returns> public static Queryable <T> Where <T>(this Queryable <T> queryable, string whereString, object whereObj = null) { var type = queryable.Type; string whereStr = string.Format(" AND {0} ", whereString); queryable.Where.Add(whereStr); if (whereObj != null) { queryable.Params.AddRange(SqlSugarTool.GetParameters(whereObj)); } return(queryable); }
/// <summary> /// 获取页面参数 /// </summary> /// <param name="sqlable"></param> /// <param name="whereObj"></param> /// <returns></returns> private static MySqlParameter[] GetAllParas(Sqlable sqlable, object whereObj) { List <MySqlParameter> allParams = new List <MySqlParameter>(); var selectParas = SqlSugarTool.GetParameters(whereObj).ToList(); if (selectParas.IsValuable()) { allParams.AddRange(selectParas); } if (sqlable.Params.IsValuable()) { allParams.AddRange(sqlable.Params); } return(allParams.ToArray()); }
/// <summary> /// 联表查询根据字符串 /// </summary> /// <typeparam name="T">第一个表的对象</typeparam> /// <param name="queryable">查询对象</param> /// <param name="tableName">表名(可是以表或也可以是SQL语句加括号)</param> /// <param name="shortName">表名简写</param> /// <param name="onWhere">on后面的条件</param> /// <param name="whereObj">匿名参数(例如:new{id=1,name="张三"})</param> /// <param name="type">Join的类型</param> /// <returns>Queryable</returns> public static Queryable <T> JoinTable <T>(this Queryable <T> queryable, string tableName, string shortName, string onWhere, object whereObj, JoinType type = JoinType.LEFT) { queryable.WhereIndex = queryable.WhereIndex + 100;; string joinType = type.ToString(); string joinStr = string.Format(" {0} JOIN {1} {2} ON {3} ", /*0*/ joinType, /*1*/ tableName, /*2*/ shortName, /*3*/ onWhere ); queryable.JoinTableValue.Add(joinStr); if (whereObj != null) { queryable.Params.AddRange(SqlSugarTool.GetParameters(whereObj)); } return(queryable); }
/// <summary> /// 根据SQL语句将结果集映射到List《T》 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="whereObj"></param> /// <returns></returns> public List <T> SqlQuery <T>(string sql, object whereObj = null) { var pars = SqlSugarTool.GetParameters(whereObj).ToList(); return(SqlQuery <T>(sql, pars)); }
/// <summary> /// 获取DataSet /// </summary> /// <param name="sql"></param> /// <param name="pars"></param> /// <returns></returns> public virtual DataSet GetDataSetAll(string sql, object pars) { return(GetDataSetAll(sql, SqlSugarTool.GetParameters(pars))); }
/// <summary> /// 根据SQL获取T /// </summary> /// <typeparam name="T">可以是int、string等,也可以是类或者数组、字典</typeparam> /// <param name="sql"></param> /// <param name="pars">匿名参数(例如:new{id=1,name="张三"})</param> /// <returns></returns> public virtual T GetSingle <T>(string sql, object pars) { return(GetSingle <T>(sql, SqlSugarTool.GetParameters(pars))); }
/// <summary> /// 获取第一行第一列 /// </summary> /// <param name="sql"></param> /// <param name="pars">匿名参数(例如:new{id=1,name="张三"})</param> /// <returns></returns> public string GetString(string sql, object pars) { return(GetString(sql, SqlSugarTool.GetParameters(pars))); }
/// <summary> /// 获取DataReader /// </summary> /// <param name="sql"></param> /// <param name="pars">匿名参数(例如:new{id=1,name="张三"})</param> /// <returns></returns> public virtual MySqlDataReader GetReader(string sql, object pars) { return(GetReader(sql, SqlSugarTool.GetParameters(pars))); }
/// <summary> /// 执行SQL返回受影响行数 /// </summary> /// <param name="sql"></param> /// <param name="pars">匿名参数(例如:new{id=1,name="张三"})</param> /// <returns></returns> public virtual int ExecuteCommand(string sql, object pars) { return(ExecuteCommand(sql, SqlSugarTool.GetParameters(pars))); }
/// <summary> /// 获取第一行第一列 /// </summary> /// <param name="sql"></param> /// <param name="pars">匿名参数(例如:new{id=1,name="张三"})</param> /// <returns></returns> public virtual object GetScalar(string sql, object pars) { return(GetScalar(sql, SqlSugarTool.GetParameters(pars))); }
/// <summary> /// 获取第一行第一列 /// </summary> /// <param name="sql"></param> /// <param name="pars">匿名参数(例如:new{id=1,name="张三"})</param> /// <returns></returns> public virtual int GetInt(string sql, object pars) { return(GetInt(sql, SqlSugarTool.GetParameters(pars))); }
/// <summary> /// 获取DataTable /// </summary> /// <param name="sql"></param> /// <param name="pars">匿名参数(例如:new{id=1,name="张三"})</param> /// <returns></returns> public DataTable GetDataTable(string sql, object pars) { return(GetDataTable(sql, SqlSugarTool.GetParameters(pars))); }
/// <summary> /// 更新 /// 注意:rowObj为T类型将更新该实体的非主键所有列,如果rowObj类型为匿名类将更新指定列 /// 使用说明:sqlSugar.Update《T》(rowObj,whereObj); /// </summary> /// <typeparam name="T"></typeparam> /// <param name="rowObj">new T(){name="张三",sex="男"}或者new {name="张三",sex="男"}</param> /// <param name="expression">it.id=100</param> /// <returns></returns> public bool Update <T>(object rowObj, Expression <Func <T, bool> > expression) where T : class { if (rowObj == null) { throw new ArgumentNullException("SqlSugarClient.Update.rowObj"); } if (expression == null) { throw new ArgumentNullException("SqlSugarClient.Update.expression"); } Type type = typeof(T); string typeName = type.Name; typeName = GetTableNameByClassType(typeName); StringBuilder sbSql = new StringBuilder(string.Format(" UPDATE {0} SET ", typeName)); var rows = SqlSugarTool.GetParameters(rowObj); string pkName = SqlSugarTool.GetPrimaryKeyByTableName(this, typeName); foreach (var r in rows) { if (pkName == r.ParameterName.TrimStart('@')) { if (rowObj.GetType() == type) { continue; } } sbSql.Append(string.Format(" `{0}` =@{0} ,", r.ParameterName.TrimStart('@'))); } sbSql.Remove(sbSql.Length - 1, 1); sbSql.Append(" WHERE 1=1 "); ResolveExpress re = new ResolveExpress(); re.ResolveExpression(re, expression); sbSql.Append(re.SqlWhere);; List <MySqlParameter> parsList = new List <MySqlParameter>(); parsList.AddRange(re.Paras); var pars = rows; if (pars != null) { foreach (var par in pars) { parsList.Add(par); } } try { var updateRowCount = ExecuteCommand(sbSql.ToString(), parsList.ToArray()); return(updateRowCount > 0); } catch (Exception ex) { var cacheManager = CacheManager <string> .GetInstance(); cacheManager.RemoveAll(it => it.Contains("KeyBy")); throw new Exception("sql:" + sbSql.ToString() + "\n" + ex.Message); } }
/// <summary> /// 根据SQL获取T的集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="pars">匿名参数(例如:new{id=1,name="张三"})</param> /// <returns></returns> public virtual List <T> GetList <T>(string sql, object pars) { return(GetList <T>(sql, SqlSugarTool.GetParameters(pars))); }