Example #1
0
        /// <summary>
        /// 返回首列结果
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="query"></param>
        /// <returns></returns>
        public override dynamic QueryScalar <TModel>(LambdaQuery <TModel> query)
        {
            query.TakeNum = 1;
            //using (var reader = GetQueryDynamicReader(query))
            //{
            //    var a = reader.Read();
            //    if (!a)
            //    {
            //        return null;
            //    }
            //    var result = reader[0];
            //    return result;
            //}
            var list = SqlStopWatch.ReturnData(() =>
            {
                return(GetQueryDynamicReader(query));
            }, (r) =>
            {
                var reader = r.reader;
                var a      = reader.Read();
                if (!a)
                {
                    return(null);
                }
                var result = reader[0];
                reader.Close();
                return(new object[] { result });
            });

            if (list == null)
            {
                return(null);
            }
            return(list.FirstOrDefault());
        }
Example #2
0
        /// <summary>
        /// 指定替换对象查询,并返回对象列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="types"></param>
        /// <returns></returns>
        public override List <T> ExecList <T>(string sql, params Type[] types)
        {
            var list = SqlStopWatch.ReturnData(() =>
            {
                return(GetDataReader(sql, types));
            }, (r) =>
            {
                var pro     = TypeCache.GetTable(typeof(T)).Fields;
                var mapping = pro.Select(b => new Attribute.FieldMapping()
                {
                    ResultName = b.MemberName, QueryField = b.MemberName, PropertyType = b.PropertyType
                });
                var queryInfo = new LambdaQuery.Mapping.QueryInfo <T>(false, sql, mapping);
                return(ObjectConvert.DataReaderToSpecifiedList <T>(r.reader, queryInfo));
            });

            return(list);
            ////var reader = GetDataReader(sql, types);
            ////double runTime;
            ////return ObjectConvert.DataReaderToList<T>(reader, out runTime);
            //var pro = TypeCache.GetTable(typeof(T)).Fields;
            //var mapping = pro.Select(b => new Attribute.FieldMapping() { MappingName = b.MemberName, QueryName = b.MemberName }).ToList();
            //var queryInfo = new LambdaQuery.Mapping.QueryInfo<T>(false, sql, mapping);
            //var list = ObjectConvert.DataReaderToSpecifiedList<T>(reader, queryInfo);
        }
Example #3
0
        /// <summary>
        /// 获取插入语法
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override object InsertObject <T>(DbContext dbContext, T obj)
        {
            Type type   = obj.GetType();
            var  helper = dbContext.DBHelper;
            var  table  = TypeCache.GetTable(type);

            var    primaryKey = table.PrimaryKey;
            object id;

            if (primaryKey.KeepIdentity)
            {
                id = primaryKey.GetValue(obj);
            }
            else
            {
                string sequenceName = string.Format("{0}_sequence", table.TableName);
                var    sqlGetIndex  = string.Format("select {0}.nextval from dual", sequenceName);//oracle不能同时执行多条语句
                id = SqlStopWatch.ExecScalar(helper, sqlGetIndex);
                primaryKey.SetValue(obj, Convert.ChangeType(id, primaryKey.PropertyType));
            }

            var sql = GetInsertSql(dbContext, table, obj);

            //helper.SetParam(primaryKey.MapingName, id);
            SqlStopWatch.Execute(helper, sql);
            //var helper2 = helper as OracleHelper;
            //int id = helper2.Insert(sql,sequenceName);
            return(id);
        }
Example #4
0
        /// <summary>
        /// 获取插入语法
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override object InsertObject(DbContext dbContext, IModel obj)
        {
            var    helper   = dbContext.DBHelper;
            Type   type     = obj.GetType();
            string table    = TypeCache.GetTableName(type, dbContext);
            var    typeArry = TypeCache.GetProperties(type, true).Values;

            Attribute.FieldAttribute primaryKey = null;
            string sql  = string.Format("insert into `{0}`(", table);
            string sql1 = "";
            string sql2 = "";

            foreach (Attribute.FieldAttribute info in typeArry)
            {
                //if (info.FieldType != Attribute.FieldType.数据库字段)
                //{
                //    continue;
                //}
                string name = info.MapingName;
                if (info.IsPrimaryKey)
                {
                    primaryKey = info;
                }
                if (info.IsPrimaryKey && !info.KeepIdentity)
                {
                    continue;
                }
                //if (!string.IsNullOrEmpty(info.VirtualField))
                //{
                //    continue;
                //}
                object value = info.GetValue(obj);
                if (info.PropertyType.FullName.StartsWith("System.Nullable"))//Nullable<T>类型为空值不插入
                {
                    if (value == null)
                    {
                        continue;
                    }
                }
                value = ObjectConvert.CheckNullValue(value, info.PropertyType);
                sql1 += string.Format("{0},", FieldNameFormat(info));
                sql2 += string.Format("?{0},", name);
                helper.AddParam(name, value);
            }
            sql1 = sql1.Substring(0, sql1.Length - 1);
            sql2 = sql2.Substring(0, sql2.Length - 1);
            sql += sql1 + ") values( " + sql2 + ") ; ";
            sql  = SqlFormat(sql);
            if (primaryKey.KeepIdentity)
            {
                helper.Execute(sql);
                return(primaryKey.GetValue(obj));
            }
            else
            {
                sql += "SELECT LAST_INSERT_ID();";
                return(SqlStopWatch.ExecScalar(helper, sql));
            }
        }
Example #5
0
        /// <summary>
        /// 指定替换对象返回单个结果
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="types">格式化SQL语句的关键类型</param>
        /// <returns></returns>
        public override object ExecScalar(string sql, params Type[] types)
        {
            sql = AutoFormat(sql, types);
            sql = _DBAdapter.SqlFormat(sql);
            var    db  = GetDBHelper(AccessType.Read);
            object obj = SqlStopWatch.ExecScalar(db, sql);

            ClearParame();
            return(obj);
        }
Example #6
0
        /// <summary>
        /// 指定替换对象更新
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="types"></param>
        /// <returns></returns>
        public override int Execute(string sql, params Type[] types)
        {
            sql = AutoFormat(sql, types);
            sql = _DBAdapter.SqlFormat(sql);
            var db    = GetDBHelper();
            int count = SqlStopWatch.Execute(db, sql);

            ClearParame();
            return(count);
        }
Example #7
0
        /// <summary>
        /// 使用完整的LamadaQuery查询
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="query"></param>
        /// <param name="cacheKey">cacheKey</param>
        /// <returns></returns>
        public override List <TModel> QueryOrFromCache <TModel>(LambdaQuery <TModel> query, out string cacheKey)
        {
            cacheKey = "";
            CheckTableCreated <TModel>();
            List <TModel> list = new List <TModel>();

            if (query.SkipPage > 0)//按分页
            {
                list = QueryResult <TModel>(query);
                if (SettingConfig.AutoTrackingModel && query.__TrackingModel)
                {
                    SetOriginClone(list);
                }
                return(list);
            }
            cacheKey = "";
            System.Data.Common.DbDataReader reader;
            query.FillParames(this);
            var    sql       = query.GetQuery();
            var    cacheTime = query.__ExpireMinute;
            var    compileSp = query.__CompileSp;
            double runTime   = 0;
            var    db        = GetDBHelper(AccessType.Read);

            if (cacheTime <= 0)
            {
                list = SqlStopWatch.ReturnList(() =>
                {
                    if (!compileSp)
                    {
                        reader = db.ExecDataReader(sql);
                    }
                    else//生成储过程
                    {
                        string sp = CompileSqlToSp(_DBAdapter.TemplateSp, sql);
                        reader    = db.RunDataReader(sp);
                    }
                    query.ExecuteTime += db.ExecuteTime;
                    var queryInfo      = new LambdaQuery.Mapping.QueryInfo <TModel>(false, query.GetQueryFieldString(), query.GetFieldMapping());
                    return(ObjectConvert.DataReaderToSpecifiedList <TModel>(reader, queryInfo));
                }, sql);
                query.MapingTime += runTime;
            }
            else
            {
                list = MemoryDataCache.CacheService.GetCacheList <TModel>(sql, query.GetFieldMapping(), cacheTime, db, out cacheKey).Values.ToList();
            }
            ClearParame();
            query.RowCount = list.Count;
            if (SettingConfig.AutoTrackingModel && query.__TrackingModel)
            {
                SetOriginClone(list);
            }
            return(list);
        }
Example #8
0
        /// <summary>
        /// 指定替换对象返回单个结果
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="types">格式化SQL语句的关键类型</param>
        /// <returns></returns>
        public override object ExecScalar(string sql)
        {
            sql = _DBAdapter.SqlFormat(sql);
            var db = GetDBHelper(DataAccessType.Read);

            sql = _DBAdapter.ReplaceParameter(db, sql);
            object obj = SqlStopWatch.ExecScalar(db, sql);

            ClearParame();
            return(obj);
        }
Example #9
0
        /// <summary>
        /// 指定替换对象更新
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public override int Execute(string sql)
        {
            sql = _DBAdapter.SqlFormat(sql);
            var db = GetDBHelper();

            sql = _DBAdapter.ReplaceParameter(db, sql);
            int count = SqlStopWatch.Execute(db, sql);

            ClearParame();
            return(count);
        }
Example #10
0
        /// <summary>
        /// 获取插入语法
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override object InsertObject(DbContext dbContext, IModel obj)
        {
            var    helper   = dbContext.DBHelper;
            Type   type     = obj.GetType();
            string table    = TypeCache.GetTableName(type, dbContext);
            var    typeArry = TypeCache.GetProperties(type, true).Values;
            string sql      = string.Format("insert into {0}(", table);
            string sql1     = "";
            string sql2     = "";

            string sequenceName = string.Format("{0}_sequence", table);
            var    sqlGetIndex  = string.Format("select {0}.nextval from dual", sequenceName);//oracle不能同时执行多条语句
            int    id           = Convert.ToInt32(SqlStopWatch.ExecScalar(helper, sqlGetIndex));

            foreach (Attribute.FieldAttribute info in typeArry)
            {
                //if (info.FieldType != Attribute.FieldType.数据库字段)
                //{
                //    continue;
                //}
                string name = info.MapingName;
                if (info.IsPrimaryKey && !info.KeepIdentity)
                {
                    //continue;//手动插入ID
                }
                //if (!string.IsNullOrEmpty(info.VirtualField))
                //{
                //    continue;
                //}
                object value = info.GetValue(obj);
                if (info.PropertyType.FullName.StartsWith("System.Nullable"))//Nullable<T>类型为空值不插入
                {
                    if (value == null)
                    {
                        continue;
                    }
                }
                value = ObjectConvert.CheckNullValue(value, info.PropertyType);
                sql1 += string.Format("{0},", info.MapingName);
                sql2 += string.Format("@{0},", info.MapingName);
                helper.AddParam(info.MapingName, value);
            }
            sql1 = sql1.Substring(0, sql1.Length - 1);
            sql2 = sql2.Substring(0, sql2.Length - 1);
            sql += sql1 + ") values( " + sql2 + ")";
            sql  = SqlFormat(sql);
            var primaryKey = TypeCache.GetTable(obj.GetType()).PrimaryKey;

            helper.SetParam(primaryKey.MapingName, id);
            helper.Execute(sql);
            //var helper2 = helper as CoreHelper.OracleHelper;
            //int id = helper2.Insert(sql,sequenceName);
            return(id);
        }
Example #11
0
        public override Dictionary <TKey, TValue> ToDictionary <TModel, TKey, TValue>(LambdaQuery <TModel> query)
        {
            var dic = SqlStopWatch.ReturnData(() =>
            {
                return(GetQueryDynamicReader(query));
            }, (r) =>
            {
                return(ObjectConvert.DataReadToDictionary <TKey, TValue>(r.reader));
            });

            return(dic);
        }
Example #12
0
        /// <summary>
        /// 按条件删除
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="where"></param>
        /// <returns></returns>
        internal int Delete <TModel>(string where) where TModel : IModel, new()
        {
            CheckTableCreated <TModel>();
            string table = TypeCache.GetTableName(typeof(TModel), dbContext);
            string sql   = _DBAdapter.GetDeleteSql(table, where);

            sql = _DBAdapter.SqlFormat(sql);
            var db = GetDBHelper();
            var n  = SqlStopWatch.Execute(db, sql);

            ClearParame();
            return(n);
        }
Example #13
0
        /// <summary>
        /// 返回dynamic集合
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="types"></param>
        /// <returns></returns>
        public override List <dynamic> ExecDynamicList(string sql)
        {
            var list = SqlStopWatch.ReturnData(() =>
            {
                return(GetDataReader(sql));
            }, (r) =>
            {
                double runTime;
                return(Dynamic.DynamicObjConvert.DataReaderToDynamic(r.reader, out runTime));
            });

            return(list);
        }
Example #14
0
        public override Dictionary <TKey, TValue> ExecDictionary <TKey, TValue>(string sql, params Type[] types)
        {
            //var reader = GetDataReader(sql, types);
            //return ObjectConvert.DataReadToDictionary<TKey, TValue>(reader);

            var dic = SqlStopWatch.ReturnData(() =>
            {
                return(GetDataReader(sql, types));
            }, (r) =>
            {
                return(ObjectConvert.DataReadToDictionary <TKey, TValue>(r.reader));
            });

            return(dic);
        }
Example #15
0
        /// <summary>
        /// 返回dynamic集合
        /// </summary>
        /// <param name="sp"></param>
        /// <returns></returns>
        public override List <dynamic> RunDynamicList(string sp)
        {
            double runTime;
            //var reader = SqlStopWatch.RunDataReader(__DbHelper,sp);
            //ClearParame();
            //return Dynamic.DynamicObjConvert.DataReaderToDynamic(reader,out runTime);
            var list = SqlStopWatch.ReturnList(() =>
            {
                var db     = GetDBHelper(DataAccessType.Read);
                var reader = db.RunDataReader(sp);
                return(Dynamic.DynamicObjConvert.DataReaderToDynamic(reader, out runTime));
            }, sp);

            return(list);
        }
Example #16
0
        /// <summary>
        /// 指定拼接条件更新
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="setValue"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        internal int Update <TModel>(ParameCollection setValue, string where) where TModel : IModel, new()
        {
            CheckTableCreated <TModel>();
            Type   type      = typeof(TModel);
            string table     = TypeCache.GetTableName(type, dbContext);
            string setString = ForamtSetValue <TModel>(setValue);
            string sql       = _DBAdapter.GetUpdateSql(table, setString, where);

            sql = _DBAdapter.SqlFormat(sql);
            var db = GetDBHelper();
            var n  = SqlStopWatch.Execute(db, sql);

            ClearParame();
            return(n);
        }
Example #17
0
        /// <summary>
        /// 返回动态对象
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public override List <dynamic> QueryDynamic(LambdaQueryBase query)
        {
            if (query.SkipPage > 0)
            {
                //int count;
                //var reader = GetPageReader(query);
                //var list = reader.GetDataDynamic(out count);
                //query.MapingTime += reader.runTime;
                //query.RowCount = count;
                //return list;

                var list = SqlStopWatch.ReturnData(() =>
                {
                    return(GetPageReader(query));
                }, (r) =>
                {
                    int count;
                    var list2         = r.GetDataDynamic(out count);
                    query.MapingTime += r.runTime;
                    query.__RowCount  = count;
                    return(list2);
                });
                return(list);
            }
            else
            {
                //var reader = GetQueryDynamicReader(query);
                //double runTime;
                //var list = Dynamic.DynamicObjConvert.DataReaderToDynamic(reader, out runTime);
                //query.MapingTime += runTime;
                //query.RowCount = list.Count;
                //return list;

                var list = SqlStopWatch.ReturnData(() =>
                {
                    return(GetQueryDynamicReader(query));
                }, (r) =>
                {
                    double runTime;
                    var list2         = Dynamic.DynamicObjConvert.DataReaderToDynamic(r.reader, out runTime);
                    query.MapingTime += runTime;
                    query.__RowCount  = list2.Count;
                    return(list2);
                });
                return(list);
            }
        }
Example #18
0
        /// <summary>
        /// 返回dynamic集合
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="types"></param>
        /// <returns></returns>
        public override List <dynamic> ExecDynamicList(string sql, params Type[] types)
        {
            //var reader = GetDataReader(sql, types);
            //double runTime;
            //return Dynamic.DynamicObjConvert.DataReaderToDynamic(reader, out runTime);

            var list = SqlStopWatch.ReturnData(() =>
            {
                return(GetDataReader(sql, types));
            }, (r) =>
            {
                double runTime;
                return(Dynamic.DynamicObjConvert.DataReaderToDynamic(r.reader, out runTime));
            });

            return(list);
        }
Example #19
0
        /// <summary>
        /// 执行存储过程返回结果集
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sp"></param>
        /// <returns></returns>
        public override List <T> RunList <T>(string sp)
        {
            var list = SqlStopWatch.ReturnList(() =>
            {
                var db     = GetDBHelper(DataAccessType.Read);
                var reader = db.RunDataReader(sp);
                ClearParame();
                var pro     = TypeCache.GetTable(typeof(T)).Fields;
                var mapping = pro.Select(b => new Attribute.FieldMapping()
                {
                    ResultName = b.MemberName, QueryField = b.MemberName, PropertyType = b.PropertyType
                }).ToList();
                var queryInfo = new LambdaQuery.Mapping.QueryInfo <T>(false, sp, mapping);
                return(ObjectConvert.DataReaderToSpecifiedList <T>(reader, queryInfo));
            }, sp);

            return(list);
        }
Example #20
0
        /// <summary>
        /// 获取插入语法
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override object InsertObject <T>(DbContext dbContext, T obj)
        {
            Type type       = obj.GetType();
            var  helper     = dbContext.DBHelper;
            var  table      = TypeCache.GetTable(type);
            var  primaryKey = table.PrimaryKey;
            var  sql        = GetInsertSql(dbContext, table, obj);

            if (primaryKey.KeepIdentity)
            {
                SqlStopWatch.Execute(helper, sql);
                return(primaryKey.GetValue(obj));
            }
            else
            {
                sql += ";SELECT LAST_INSERT_ID();";
                return(SqlStopWatch.ExecScalar(helper, sql));
            }
        }
Example #21
0
        /// <summary>
        /// 按select返回指定类型
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="query"></param>
        /// <returns></returns>
        public override List <TResult> QueryResult <TResult>(LambdaQueryBase query)
        {
            var queryInfo = new LambdaQuery.Mapping.QueryInfo <TResult>(false, query.GetQueryFieldString(), query.GetFieldMapping());

            if (query.SkipPage > 0)
            {
                //var reader = GetPageReader(query);
                //int count;
                //var list = reader.GetDataTResult<TResult>(queryInfo, out count);
                //query.RowCount = count;
                //return list;

                var list = SqlStopWatch.ReturnData(() =>
                {
                    return(GetPageReader(query));
                }, (r) =>
                {
                    int count;
                    var list2        = r.GetDataTResult <TResult>(queryInfo, out count);
                    query.__RowCount = count;
                    return(list2);
                });
                return(list);
            }
            else
            {
                //var reader = GetQueryDynamicReader(query);
                //var list = ObjectConvert.DataReaderToSpecifiedList<TResult>(reader, queryInfo);
                //query.RowCount = list.Count;
                //return list;
                var list = SqlStopWatch.ReturnData(() =>
                {
                    return(GetQueryDynamicReader(query));
                }, (r) =>
                {
                    var list2        = ObjectConvert.DataReaderToSpecifiedList <TResult>(r.reader, queryInfo);
                    query.__RowCount = list2.Count;
                    return(list2);
                });
                return(list);
            }
        }
Example #22
0
        /// <summary>
        /// 按匿名对象
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="query"></param>
        /// <param name="newExpression"></param>
        /// <returns></returns>
        public override List <TResult> QueryResult <TResult>(LambdaQueryBase query, NewExpression newExpression)
        {
            List <TResult> list;
            var            queryInfo = new LambdaQuery.Mapping.QueryInfo <TResult>(true, query.GetQueryFieldString(), null, newExpression.Constructor);

            if (query.SkipPage > 0)
            {
                //var reader = GetPageReader(query);
                //int count;
                //list = reader.GetDataTResult<TResult>(queryInfo, out count);
                //query.RowCount = count;
                list = SqlStopWatch.ReturnData(() =>
                {
                    return(GetPageReader(query));
                }, (r) =>
                {
                    int count;
                    var list2      = r.GetDataTResult <TResult>(queryInfo, out count);
                    query.RowCount = count;
                    return(list2);
                });
            }
            else
            {
                //var reader = GetQueryDynamicReader(query);
                //list = ObjectConvert.DataReaderToSpecifiedList<TResult>(reader, queryInfo);
                list = SqlStopWatch.ReturnData(() =>
                {
                    return(GetQueryDynamicReader(query));
                }, (r) =>
                {
                    var list2 = ObjectConvert.DataReaderToSpecifiedList <TResult>(r.reader, queryInfo);
                    return(list2);
                });
            }
            return(list);
        }
Example #23
0
        /// <summary>
        /// 插入对象,并返回主键
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override object InsertObject <T>(DbContext dbContext, T obj)
        {
            Type type       = obj.GetType();
            var  helper     = dbContext.DBHelper;
            var  table      = TypeCache.GetTable(type);
            var  primaryKey = table.PrimaryKey;
            //string table = TypeCache.GetTableName(type, dbContext);
            //var typeArry = TypeCache.GetProperties(type, true).Values;
            //Attribute.FieldAttribute primaryKey = null;
            //string sql = string.Format("insert into [{0}](", table);
            //string sql1 = "";
            //string sql2 = "";
            //foreach (Attribute.FieldAttribute info in typeArry)
            //{
            //    //if (info.FieldType != Attribute.FieldType.数据库字段)
            //    //{
            //    //    continue;
            //    //}
            //    string name = info.MapingName;
            //    if (info.IsPrimaryKey)
            //    {
            //        primaryKey = info;
            //    }
            //    if (info.IsPrimaryKey && !info.KeepIdentity)
            //    {
            //        continue;
            //    }
            //    //if (!string.IsNullOrEmpty(info.VirtualField))
            //    //{
            //    //    continue;
            //    //}
            //    object value = info.GetValue(obj);
            //    if (info.PropertyType.FullName.StartsWith("System.Nullable"))//Nullable<T>类型为空值不插入
            //    {
            //        if (value == null)
            //        {
            //            continue;
            //        }
            //    }
            //    value = ObjectConvert.CheckNullValue(value, info.PropertyType);
            //    sql1 += string.Format("{0},", FieldNameFormat(info));
            //    sql2 += string.Format("@{0},", name);
            //    helper.AddParam(name, value);
            //}
            //sql1 = sql1.Substring(0, sql1.Length - 1);
            //sql2 = sql2.Substring(0, sql2.Length - 1);
            //sql += sql1 + ") values( " + sql2 + ") ; ";
            //sql = SqlFormat(sql);
            var sql = GetInsertSql(dbContext, table, obj);

            if (primaryKey.KeepIdentity)
            {
                SqlStopWatch.Execute(helper, sql);
                return(primaryKey.GetValue(obj));
            }
            else
            {
                sql += ";SELECT scope_identity() ;";
                return(SqlStopWatch.ExecScalar(helper, sql));
            }
        }
Example #24
0
        /// <summary>
        /// 使用完整的LamadaQuery查询
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="iQuery"></param>
        /// <param name="cacheKey">cacheKey</param>
        /// <returns></returns>
        public override List <TModel> QueryOrFromCache <TModel>(ILambdaQuery <TModel> iQuery, out string cacheKey)
        {
            var query = iQuery as LambdaQueryBase;

            cacheKey = "";
            CheckTableCreated <TModel>();
            List <TModel> list = new List <TModel>();

            if (query.SkipPage > 0)//按分页
            {
                list = QueryResult <TModel>(query);
                //分页不创建Clone
                //if (SettingConfig.AutoTrackingModel && query.__TrackingModel)
                //{
                //    SetOriginClone(list);
                //}
                return(list);
            }
            cacheKey = "";
            System.Data.Common.DbDataReader reader;
            query.FillParames(this);
            var sql = query.GetQuery();

            sql = _DBAdapter.SqlFormat(sql);
            var    cacheTime = query.__ExpireMinute;
            var    compileSp = query.__CompileSp;
            double runTime   = 0;
            var    db        = GetDBHelper(DataAccessType.Read);

            if (cacheTime <= 0)
            {
                list = SqlStopWatch.ReturnList(() =>
                {
                    if (!compileSp || !_DBAdapter.CanCompileSP)
                    {
                        reader = db.ExecDataReader(sql);
                    }
                    else//生成储过程
                    {
                        string sp = CompileSqlToSp(_DBAdapter.TemplateSp, sql);
                        reader    = db.RunDataReader(sp);
                    }
                    query.ExecuteTime += db.ExecuteTime;
                    var queryInfo      = new LambdaQuery.Mapping.QueryInfo <TModel>(false, query.GetQueryFieldString(), query.GetFieldMapping());
                    return(ObjectConvert.DataReaderToSpecifiedList <TModel>(reader, queryInfo));
                }, sql);
                query.MapingTime += runTime;
                //if(!string.IsNullOrEmpty(query.__RemoveInJionBatchNo))
                //{
                //    Delete<InJoin>(b => b.BatchNo == query.__RemoveInJionBatchNo);
                //    query.__RemoveInJionBatchNo = "";
                //}
            }
            else
            {
                list = MemoryDataCache.CacheService.GetCacheList <TModel>(sql, query.GetFieldMapping(), cacheTime, db, out cacheKey).Values.ToList();
            }
            ClearParame();
            query.__RowCount = list.Count;
            //query = null;
            return(list);
        }
Example #25
0
        /// <summary>
        /// 按是否能编译
        /// </summary>
        /// <param name="query1"></param>
        /// <returns></returns>
        CallBackDataReader GetGroupPageReader(LambdaQueryBase query1)
        {
            //var query1 = query as RelationLambdaQuery<TModel>;
            if (_DBAdapter.CanCompileSP && query1.__CompileSp)
            {
                return(GetSpGroupPageReader(query1));
            }
            CheckTableCreated(query1.__MainType);
            var sb = new StringBuilder();

            query1.GetQueryConditions(sb);
            var condition = sb.ToString();
            var fields    = query1.GetQueryFieldString();

            if (!condition.Contains("group"))
            {
                throw new CRLException("缺少group语法");
            }
            var rowOver = query1.GetOrder();

            if (string.IsNullOrEmpty(rowOver))
            {
                throw new CRLException("Group分页需指定Group排序字段");
                //var table = TypeCache.GetTable(typeof(T));
                //rowOver = string.Format("t1.{0} desc", table.PrimaryKey.Name);
            }
            var sort1 = System.Text.RegularExpressions.Regex.Replace(rowOver, @"t\d\.", "");

            condition = _DBAdapter.SqlFormat(condition);

            query1.FillParames(this);
            var pageIndex = query1.SkipPage;
            var pageSize  = query1.TakeNum;

            pageIndex = pageIndex == 0 ? 1 : pageIndex;
            pageSize  = pageSize == 0 ? 15 : pageSize;

            string countSql = string.Format("select count(*)  from (select count(*) as a from {0}) t", condition);
            var    db       = GetDBHelper(AccessType.Read);
            int    count    = Convert.ToInt32(SqlStopWatch.ExecScalar(db, countSql));

            query1.ExecuteTime += db.ExecuteTime;
            query1.RowCount     = count;
            //if (count == 0)
            //{
            //    return null;
            //}
            int pageCount = (count + pageSize - 1) / pageSize;

            if (pageIndex > pageCount)
            {
                pageIndex = pageCount;
            }

            var    start = pageSize * (pageIndex - 1) + 1;
            var    end   = start + pageSize - 1;
            string sql   = _DBAdapter.PageSqlFormat(fields, rowOver, condition, start, end, "");
            //System.Data.Common.DbDataReader reader;
            //reader = dbHelper.ExecDataReader(sql);
            var reader = new CallBackDataReader(db.ExecDataReader(sql), () =>
            {
                return(count);
            }, sql);

            query1.ExecuteTime += db.ExecuteTime;
            ClearParame();
            return(reader);
        }
Example #26
0
        /// <summary>
        /// GROUP和是否编译判断
        /// </summary>
        /// <param name="query1"></param>
        /// <returns></returns>
        internal CallBackDataReader GetPageReader(LambdaQueryBase query1)
        {
            if (query1.__GroupFields != null)
            {
                return(GetGroupPageReader(query1));
            }
            if (_DBAdapter.CanCompileSP && query1.__CompileSp)
            {
                return(GetSpPageReader(query1));
            }

            CheckTableCreated(query1.__MainType);
            //var fields = query.GetQueryFieldString(b => b.Length > 500 || b.PropertyType == typeof(byte[]));
            var fields  = query1.GetQueryFieldString();
            var rowOver = query1.GetOrder();

            if (string.IsNullOrEmpty(rowOver))
            {
                var table = TypeCache.GetTable(query1.__MainType);
                rowOver = string.Format("t1.{0} desc", table.PrimaryKey.MapingName);
            }
            var orderBy = System.Text.RegularExpressions.Regex.Replace(rowOver, @"t\d\.", "t.");
            var sb      = new StringBuilder();

            query1.GetQueryConditions(sb);
            var condition = sb.ToString();

            condition = _DBAdapter.SqlFormat(condition);
            query1.FillParames(this);

            var pageIndex = query1.SkipPage;
            var pageSize  = query1.TakeNum;

            pageIndex = pageIndex == 0 ? 1 : pageIndex;
            pageSize  = pageSize == 0 ? 15 : pageSize;
            string countSql = string.Format("select count(*) from {0}", condition);
            var    db       = GetDBHelper(AccessType.Read);
            int    count    = Convert.ToInt32(SqlStopWatch.ExecScalar(db, countSql));

            query1.ExecuteTime += db.ExecuteTime;
            query1.RowCount     = count;
            //if (count == 0)
            //{
            //    return null;
            //}
            int pageCount = (count + pageSize - 1) / pageSize;

            if (pageIndex > pageCount)
            {
                pageIndex = pageCount;
            }

            var    start  = pageSize * (pageIndex - 1) + 1;
            var    end    = start + pageSize - 1;
            string sql    = _DBAdapter.PageSqlFormat(fields, rowOver, condition, start, end, orderBy);
            var    reader = new CallBackDataReader(db.ExecDataReader(sql), () =>
            {
                return(count);
            }, sql);

            query1.ExecuteTime += db.ExecuteTime;
            ClearParame();
            return(reader);
        }