コード例 #1
0
        private object ExecuteScalar <TResult>(QuerySection <TResult> query)
            where TResult : Entity
        {
            try
            {
                string queryString = query.QueryString;
                string cacheKey    = GetCacheKey(queryString, query.Parameters);
                object obj         = GetCache <TResult>("GetObject", cacheKey);
                if (obj != null)
                {
                    return(obj);
                }

                //添加参数到Command中
                queryCommand = dbProvider.CreateSqlCommand(queryString, query.Parameters);

                object newobj = dbProvider.ExecuteScalar(queryCommand, dbTran);

                SetCache <TResult>("GetObject", cacheKey, newobj);

                return(newobj);
            }
            catch
            {
                throw;
            }
        }
コード例 #2
0
        private SourceTable ExecuteDataTable <TResult>(QuerySection <TResult> query)
            where TResult : Entity
        {
            try
            {
                string queryString = query.QueryString;
                string cacheKey    = GetCacheKey(queryString, query.Parameters);
                object obj         = GetCache <TResult>("DataTable", cacheKey);
                if (obj != null)
                {
                    return((SourceTable)obj);
                }

                //添加参数到Command中
                queryCommand = dbProvider.CreateSqlCommand(queryString, query.Parameters);

                using (DataTable dataTable = dbProvider.ExecuteDataTable(queryCommand, dbTran))
                {
                    dataTable.TableName = typeof(TResult).Name;
                    SourceTable table = new SourceTable(dataTable);

                    SetCache <TResult>("DataTable", cacheKey, table);

                    return(table);
                }
            }
            catch
            {
                throw;
            }
        }
コード例 #3
0
ファイル: DbTrans.cs プロジェクト: windygu/mysoftsolution
        /// <summary>
        /// 返回一个Query节
        /// </summary>
        /// <returns></returns>
        public QuerySection From(QueryCreator creator)
        {
            if (creator.Table == null)
            {
                throw new DataException("用创建器操作时,表不能为null!");
            }

            FromSection <ViewEntity> f = this.From <ViewEntity>(creator.Table);

            if (creator.IsRelation)
            {
                foreach (TableJoin join in creator.Relations.Values)
                {
                    if (join.Type == JoinType.LeftJoin)
                    {
                        f.LeftJoin <ViewEntity>(join.Table, join.Where);
                    }
                    else if (join.Type == JoinType.RightJoin)
                    {
                        f.RightJoin <ViewEntity>(join.Table, join.Where);
                    }
                    else
                    {
                        f.InnerJoin <ViewEntity>(join.Table, join.Where);
                    }
                }
            }

            QuerySection <ViewEntity> query = f.Select(creator.Fields).Where(creator.Where)
                                              .OrderBy(creator.OrderBy);

            return(new QuerySection(query));
        }
コード例 #4
0
ファイル: FromSection.cs プロジェクト: windygu/MySoft.Data
 internal FromSection(Table table)
     : this()
 {
     this.tableName = table == null ? fromTable.Name : table.Name;
     fromTable.As(this.tableName);
     this.query = new QuerySection <T>(this);
 }
コード例 #5
0
ファイル: FromSection.cs プロジェクト: windygu/MySoft.Data
 internal FromSection(string tableName, string relation, IList <Entity> list)
 {
     this.tableName = tableName;
     this.relation  = relation;
     this.entityList.AddRange(list);
     this.query = new QuerySection <T>(this);
 }
コード例 #6
0
        private DataSet ExecuteDataSet <TResult>(QuerySection <TResult> query)
            where TResult : Entity
        {
            try
            {
                string queryString = query.QueryString;
                string cacheKey    = GetCacheKey(queryString, query.Parameters);
                object obj         = GetCache <TResult>("DataTable", cacheKey);
                if (obj != null)
                {
                    return((DataSet)obj);
                }

                //添加参数到Command中
                queryCommand = dbProvider.CreateSqlCommand(queryString, query.Parameters);

                using (DataSet dataSet = dbProvider.ExecuteDataSet(queryCommand, dbTran))
                {
                    SetCache <TResult>("DataSet", cacheKey, dataSet);

                    return(dataSet);
                }
            }
            catch
            {
                throw;
            }
        }
コード例 #7
0
 /// <summary>
 /// 联合查询
 /// </summary>
 /// <param name="query"></param>
 /// <returns></returns>
 public override QuerySection <T> UnionAll(QuerySection <T> query)
 {
     this.queries.Add(new UnionItem <T> {
         Query = query, IsUnionAll = true
     });
     return(this);
 }
コード例 #8
0
ファイル: DbProvider.cs プロジェクト: windygu/MySoft.Data
        /// <summary>
        /// 创建分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="itemCount"></param>
        /// <param name="skipCount"></param>
        /// <returns></returns>
        protected internal virtual QuerySection <T> CreatePageQuery <T>(QuerySection <T> query, int itemCount, int skipCount)
            where T : Entity
        {
            if (skipCount == 0)
            {
                ((IPaging)query).Prefix("top " + itemCount);
                return(query);
            }
            else
            {
                ((IPaging)query).Prefix("top " + itemCount);

                Field pagingField = query.PagingField;

                if ((IField)pagingField == null)
                {
                    throw new MySoftException("SqlServer2000或Access请使用SetPagingField设定分页主键!");
                }

                QuerySection <T> jquery = query.CreateQuery <T>();
                ((IPaging)jquery).Prefix("top " + skipCount);
                jquery.Select(pagingField);

                //如果是联合查询,则需要符值整个QueryString
                if (query.UnionQuery)
                {
                    jquery.QueryString = query.QueryString;
                }

                query.PageWhere = !pagingField.In(jquery);

                return(query);
            }
        }
コード例 #9
0
        private SourceReader ExcuteDataReader <TResult>(QuerySection <TResult> query, bool all)
            where TResult : Entity
        {
            try
            {
                if (all)
                {
                    prefixString = null;
                    endString    = null;
                }

                string queryString = query.QueryString;
                if (this.unionQuery && all)
                {
                    queryString += OrderString;
                }

                //添加参数到Command中
                queryCommand = dbProvider.CreateSqlCommand(queryString, query.Parameters);

                return(dbProvider.ExecuteReader(queryCommand, dbTran));
            }
            catch
            {
                throw;
            }
        }
コード例 #10
0
        private int GetCount(QuerySection <T> query)
        {
            string countString = query.CountString;

            if (query.unionQuery)
            {
                countString = "select sum(row_count) as total_row_count from (" + countString + ") tmp_table";
            }

            string cacheKey = GetCacheKey(countString, this.Parameters);
            object obj      = GetCache <T>("Count", cacheKey);

            if (obj != null)
            {
                return(DataUtils.ConvertValue <int>(obj));
            }

            //添加参数到Command中
            queryCommand = dbProvider.CreateSqlCommand(countString, query.Parameters);

            object value = dbProvider.ExecuteScalar(queryCommand, dbTran);

            int ret = DataUtils.ConvertValue <int>(value);

            SetCache <T>("Count", cacheKey, ret);

            return(ret);
        }
コード例 #11
0
 /// <summary>
 /// UnionQuery构造函数
 /// </summary>
 /// <param name="query1"></param>
 /// <param name="query2"></param>
 /// <param name="dbProvider"></param>
 /// <param name="dbTran"></param>
 /// <param name="isUnionAll"></param>
 public UnionQuery(QuerySection <T> query1, QuerySection <T> query2, DbProvider dbProvider, DbTrans dbTran, bool isUnionAll)
     : base(query1.FromSection, dbProvider, dbTran)
 {
     this.queries.Add(new UnionItem <T> {
         Query = query1, IsUnionAll = isUnionAll
     });
     this.queries.Add(new UnionItem <T> {
         Query = query2, IsUnionAll = isUnionAll
     });
 }
コード例 #12
0
        private ArrayList <TResult> ExcuteDataListResult <TResult>(QuerySection <T> query, bool all)
        {
            try
            {
                string queryString = query.QueryString;
                if (this.unionQuery && all)
                {
                    queryString += OrderString;
                }

                string cacheKey = GetCacheKey(queryString, this.Parameters);
                object obj      = GetCache <T>("ListObject", cacheKey);
                if (obj != null)
                {
                    return((SourceList <TResult>)obj);
                }

                using (SourceReader reader = ExcuteDataReader(query, all))
                {
                    ArrayList <TResult> list = new ArrayList <TResult>();

                    if (typeof(TResult) == typeof(object[]))
                    {
                        while (reader.Read())
                        {
                            List <object> objs = new List <object>();
                            for (int row = 0; row < reader.FieldCount; row++)
                            {
                                objs.Add(reader.GetValue(row));
                            }

                            TResult result = (TResult)(objs.ToArray() as object);
                            list.Add(result);
                        }
                    }
                    else
                    {
                        while (reader.Read())
                        {
                            list.Add(reader.GetValue <TResult>(0));
                        }
                    }

                    SetCache <T>("ListObject", cacheKey, list);

                    reader.Close();

                    return(list);
                }
            }
            catch
            {
                throw;
            }
        }
コード例 #13
0
        /// <summary>
        /// 返回一个分页处理的Page节
        /// </summary>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageSection <T> GetPage(int pageSize)
        {
            QuerySection <T> query = this;

            if (this.unionQuery)
            {
                query = SubQuery();
                query.OrderBy(this.orderBy);
            }
            return(new PageSection <T>(query, pageSize));
        }
コード例 #14
0
ファイル: FromSection.cs プロジェクト: windygu/MySoft.Data
        internal FromSection(DbProvider dbProvider, DbTrans dbTran, Table table)
            : this()
        {
            this.entityList.Add(this.fromEntity);
            //从传入的表信息中获取表名
            this.tableName = table == null ? fromTable.Name : table.Name;
            fromTable.As(this.tableName);
            Field pagingField = fromEntity.PagingField;

            this.query = new QuerySection <T>(this, dbProvider, dbTran, pagingField);
        }
コード例 #15
0
ファイル: FromSection.cs プロジェクト: windygu/MySoft.Data
 internal FromSection(string tableName, string aliasName)
     : this()
 {
     fromTable.As(aliasName);
     this.entityList.Add(this.fromEntity);
     this.tableName = tableName;
     if (aliasName != null)
     {
         this.tableName += " {0}" + aliasName + "{1}";
     }
     this.query = new QuerySection <T>(this);
 }
コード例 #16
0
        /// <summary>
        /// 返回一个分页处理的Page节
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageSection <TEntity> GetPage <TEntity>(int pageSize)
            where TEntity : Entity
        {
            QuerySection <TEntity> query = CreateQuery <TEntity>();

            if (this.unionQuery)
            {
                query = SubQuery <TEntity>();
                query.OrderBy(this.orderBy);
            }
            return(new PageSection <TEntity>(query, pageSize));
        }
コード例 #17
0
        /// <summary>
        /// 返回一个列表
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <returns></returns>
        public virtual SourceList <TEntity> ToList <TEntity>()
            where TEntity : Entity
        {
            QuerySection <TEntity> query = CreateQuery <TEntity>();

            if (this.unionQuery)
            {
                query = SubQuery <TEntity>();
                query.OrderBy(this.orderBy);
            }

            return(ExcuteDataList <TEntity>(query, true));
        }
コード例 #18
0
        /// <summary>
        /// 创建一个跟当前相同的子查询
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <returns></returns>
        internal QuerySection <TResult> CreateQuery <TResult>()
            where TResult : Entity
        {
            QuerySection <TResult> query = new QuerySection <TResult>(new FromSection <TResult>(fromSection.TableName, fromSection.Relation, fromSection.EntityList), dbProvider, dbTran, pagingField);

            query.Where(queryWhere).OrderBy(orderBy).GroupBy(groupBy).Having(havingWhere);
            query.Parameters = this.Parameters;

            if (fieldSelect)
            {
                query.Select(fieldList.ToArray());
            }
            return(query);
        }
コード例 #19
0
        /// <summary>
        /// 生成一个子查询
        /// </summary>
        /// <returns></returns>
        public QuerySection <TSub> SubQuery <TSub>(string aliasName)
            where TSub : Entity
        {
            TSub   entity             = DataUtils.CreateInstance <TSub>();
            string tableName          = entity.GetTable().Name;
            QuerySection <TSub> query = new QuerySection <TSub>(new FromSection <TSub>(tableName, aliasName), dbProvider, dbTran, pagingField);

            query.SqlString  = "(" + QueryString + ") " + (aliasName != null ? "{0}" + aliasName + "{1}" : tableName);
            query.Parameters = this.Parameters;

            if ((IField)this.pagingField == null)
            {
                query.pagingField = entity.PagingField;
            }

            List <Field> flist = new List <Field>();

            if (aliasName != null)
            {
                if ((IField)query.pagingField != null)
                {
                    query.pagingField = query.pagingField.At(aliasName);
                }

                fieldList.ForEach(field =>
                {
                    flist.Add(field.At(aliasName));
                });
            }
            else
            {
                if ((IField)query.pagingField != null)
                {
                    query.pagingField = query.pagingField.At(tableName);
                }

                fieldList.ForEach(field =>
                {
                    flist.Add(field.At(tableName));
                });
            }

            //如果当前子查询与上一级子查询类型相同,则直接使用上一级的字段列表
            if (typeof(TSub) == typeof(T))
            {
                query.Select(flist.ToArray());
            }

            return(query);
        }
コード例 #20
0
ファイル: DbTrans.cs プロジェクト: windygu/mysoftsolution
        /// <summary>
        /// 返回一个Query节
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public QuerySection <T> From <T>(TableRelation <T> relation)
            where T : Entity
        {
            //重新实例化一个Query
            var query = new QuerySection <T>(relation.GetFromSection(), dbProvider, this);

            //处理前n条
            if (relation.GetTopSize() > 0)
            {
                query = query.GetTop(relation.GetTopSize());
            }

            return(query);
        }
コード例 #21
0
        /// <summary>
        /// 返回一个列表
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <returns></returns>
        public SourceList <TEntity> ToList <TEntity>(int startIndex, int endIndex)
            where TEntity : Entity
        {
            if (startIndex <= 0)
            {
                startIndex = 1;
            }
            int topItem = endIndex - startIndex + 1;
            QuerySection <TEntity> query = CreateQuery <TEntity>();

            if (this.unionQuery)
            {
                query = SubQuery <TEntity>();
                query.OrderBy(this.orderBy);
            }
            return(GetList <TEntity>(query, topItem, endIndex - topItem));
        }
コード例 #22
0
        /// <summary>
        /// 进行Union操作
        /// </summary>
        /// <param name="query"></param>
        /// <param name="isUnionAll"></param>
        /// <returns></returns>
        private QuerySection <T> Union(QuerySection <T> query, bool isUnionAll)
        {
            QuerySection <T> q = CreateQuery <T>();

            q.QueryString  = this.QueryString;
            q.CountString  = this.CountString;
            q.QueryString += " union " + (isUnionAll ? "all " : "") + query.QueryString;
            q.CountString += " union " + (isUnionAll ? "all " : "") + query.CountString;
            q.unionQuery   = true;

            //将排序进行合并
            OrderByClip order = this.orderBy && query.orderBy;

            q.Parameters = query.Parameters;

            return(q.OrderBy(order));
        }
コード例 #23
0
        private SourceReader ExecuteDataReader <TResult>(QuerySection <TResult> query)
            where TResult : Entity
        {
            try
            {
                string queryString = query.QueryString;

                //添加参数到Command中
                queryCommand = dbProvider.CreateSqlCommand(queryString, query.Parameters);

                return(dbProvider.ExecuteReader(queryCommand, dbTran));
            }
            catch
            {
                throw;
            }
        }
コード例 #24
0
        private SourceList <TResult> ExcuteDataList <TResult>(QuerySection <TResult> query, bool all)
            where TResult : Entity
        {
            try
            {
                string queryString = query.QueryString;
                if (this.unionQuery && all)
                {
                    queryString += OrderString;
                }

                string cacheKey = GetCacheKey(queryString, this.Parameters);
                object obj      = GetCache <TResult>("ListEntity", cacheKey);
                if (obj != null)
                {
                    return((SourceList <TResult>)obj);
                }

                using (SourceReader reader = ExcuteDataReader(query, all))
                {
                    SourceList <TResult> list = new SourceList <TResult>();

                    FastCreateInstanceHandler creator = DataUtils.GetFastInstanceCreator(typeof(TResult));

                    while (reader.Read())
                    {
                        TResult entity = (TResult)creator();
                        entity.SetAllValues(reader);
                        entity.Attach();
                        list.Add(entity);
                    }

                    SetCache <TResult>("ListEntity", cacheKey, list);

                    reader.Close();

                    return(list);
                }
            }
            catch
            {
                throw;
            }
        }
コード例 #25
0
        /// <summary>
        /// 生成一个子查询
        /// </summary>
        /// <returns></returns>
        public virtual QuerySection <TSub> SubQuery <TSub>(string aliasName)
            where TSub : Entity
        {
            TSub entity    = CoreHelper.CreateInstance <TSub>();
            var  tableName = entity.GetTable().Name;

            if (aliasName != null)
            {
                tableName = string.Format("__[{0}]__", aliasName);
            }

            QuerySection <TSub> query = new QuerySection <TSub>(new FromSection <TSub>(entity.GetTable(), aliasName), dbProvider, dbTran);

            query.SqlString  = string.Format("({0}) {1}", QueryString, tableName);
            query.Parameters = this.Parameters;
            query.Select(Field.All.At(entity.GetTable().As(aliasName)));

            return(query);
        }
コード例 #26
0
ファイル: FromSection.cs プロジェクト: windygu/MySoft.Data
        internal FromSection(DbProvider dbProvider, DbTrans dbTran, string aliasName)
            : this()
        {
            fromTable.As(aliasName);
            this.entityList.Add(this.fromEntity);
            this.tableName = fromTable.Name;
            Field pagingField = fromEntity.PagingField;

            if (aliasName != null)
            {
                if ((IField)pagingField != null)
                {
                    pagingField = pagingField.At(aliasName);
                }

                this.tableName += " {0}" + aliasName + "{1}";
            }
            this.query = new QuerySection <T>(this, dbProvider, dbTran, pagingField);
        }
コード例 #27
0
        private int GetCount(QuerySection <T> query)
        {
            string countString = query.CountString;
            string cacheKey    = GetCacheKey(countString, this.Parameters);
            object obj         = GetCache <T>("Count", cacheKey);

            if (obj != null)
            {
                return(CoreHelper.ConvertValue <int>(obj));
            }

            //添加参数到Command中
            queryCommand = dbProvider.CreateSqlCommand(countString, query.Parameters);

            object value = dbProvider.ExecuteScalar(queryCommand, dbTran);

            int ret = CoreHelper.ConvertValue <int>(value);

            SetCache <T>("Count", cacheKey, ret);

            return(ret);
        }
コード例 #28
0
        private SourceList <TResult> ExecuteDataList <TResult>(QuerySection <TResult> query)
            where TResult : Entity
        {
            try
            {
                string queryString = query.QueryString;
                string cacheKey    = GetCacheKey(queryString, query.Parameters);
                object obj         = GetCache <TResult>("ListEntity", cacheKey);
                if (obj != null)
                {
                    return((SourceList <TResult>)obj);
                }

                using (SourceReader reader = ExecuteDataReader(queryString, query.Parameters))
                {
                    SourceList <TResult> list = new SourceList <TResult>();

                    FastCreateInstanceHandler creator = CoreHelper.GetFastInstanceCreator(typeof(TResult));

                    while (reader.Read())
                    {
                        TResult entity = (TResult)creator();
                        entity.SetDbValues(reader);
                        entity.Attach();
                        list.Add(entity);
                    }

                    SetCache <TResult>("ListEntity", cacheKey, list);

                    reader.Close();

                    return(list);
                }
            }
            catch
            {
                throw;
            }
        }
コード例 #29
0
        /// <summary>
        /// 返回一个实体
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <returns></returns>
        public TEntity ToSingle <TEntity>()
            where TEntity : Entity
        {
            QuerySection <TEntity> query = CreateQuery <TEntity>();

            if (this.unionQuery)
            {
                query = SubQuery <TEntity>();
                query.OrderBy(this.orderBy);
            }

            ISourceList <TEntity> list = GetList <TEntity>(query, 1, 0);

            if (list.Count == 0)
            {
                return(default(TEntity));
            }
            else
            {
                return(list[0]);
            }
        }
コード例 #30
0
        private object ExcuteScalar <TResult>(QuerySection <TResult> query, bool all)
            where TResult : Entity
        {
            try
            {
                if (all)
                {
                    prefixString = null;
                    endString    = null;
                }

                string queryString = query.QueryString;
                if (this.unionQuery && all)
                {
                    queryString += OrderString;
                }

                string cacheKey = GetCacheKey(queryString, this.Parameters);
                object obj      = GetCache <TResult>("GetObject", cacheKey);
                if (obj != null)
                {
                    return(obj);
                }

                //添加参数到Command中
                queryCommand = dbProvider.CreateSqlCommand(queryString, query.Parameters);

                object newobj = dbProvider.ExecuteScalar(queryCommand, dbTran);

                SetCache <TResult>("GetObject", cacheKey, newobj);

                return(newobj);
            }
            catch
            {
                throw;
            }
        }