예제 #1
0
        /// <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);
        }
예제 #2
0
        /// <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 <SQLiteParameter> pars)
        {
            SQLiteDataReader 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);
        }
예제 #3
0
        /// <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);
        }
예제 #4
0
        /// <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);
        }
예제 #5
0
        /// <summary>
        /// 获取页面参数
        /// </summary>
        /// <param name="sqlable"></param>
        /// <param name="whereObj"></param>
        /// <returns></returns>
        private static SQLiteParameter[] GetAllParas(Sqlable sqlable, object whereObj)
        {
            List <SQLiteParameter> allParams = new List <SQLiteParameter>();
            var selectParas = SqlSugarTool.GetParameters(whereObj).ToList();

            if (selectParas.IsValuable())
            {
                allParams.AddRange(selectParas);
            }
            if (sqlable.Params.IsValuable())
            {
                allParams.AddRange(sqlable.Params);
            }
            return(allParams.ToArray());
        }
예제 #6
0
        /// <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 <SQLiteParameter> parsList = new List <SQLiteParameter>();

            parsList.AddRange(re.Paras);
            var pars = rows;

            if (pars != null)
            {
                foreach (var par in pars)
                {
                    //if (par.SqlDbType == SqlDbType.Udt)
                    //{
                    //    par.UdtTypeName = "HIERARCHYID";
                    //}
                    SqlSugarTool.SetParSize(par);
                    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);
            }
        }
예제 #7
0
        /// <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));
        }
예제 #8
0
 public string GetString(string sql, object pars)
 {
     return(GetString(sql, SqlSugarTool.GetParameters(pars)));
 }
예제 #9
0
 public int GetInt(string sql, object pars)
 {
     return(GetInt(sql, SqlSugarTool.GetParameters(pars)));
 }
예제 #10
0
 public DataTable GetDataTable(string sql, object pars)
 {
     return(GetDataTable(sql, SqlSugarTool.GetParameters(pars)));
 }
예제 #11
0
 public DataSet GetDataSetAll(string sql, object pars)
 {
     return(GetDataSetAll(sql, SqlSugarTool.GetParameters(pars)));
 }
예제 #12
0
 public T GetSingle <T>(string sql, object[] pars)
 {
     return(GetSingle <T>(sql, SqlSugarTool.GetParameters(pars)));
 }
예제 #13
0
 public List <T> GetList <T>(string sql, object pars)
 {
     return(GetList <T>(sql, SqlSugarTool.GetParameters(pars)));
 }
예제 #14
0
 public SQLiteDataReader GetReader(string sql, object pars)
 {
     return(GetReader(sql, SqlSugarTool.GetParameters(pars)));
 }
예제 #15
0
 public int ExecuteCommand(string sql, object pars)
 {
     return(ExecuteCommand(sql, SqlSugarTool.GetParameters(pars)));
 }
예제 #16
0
 public object GetScalar(string sql, object pars)
 {
     return(GetScalar(sql, SqlSugarTool.GetParameters(pars)));
 }