Beispiel #1
0
        // 执行SQL 语句,并返回单个实体对象
        async Task <T> ExecuteAsync <T>(IDbCommand cmd, DbCommandDefinition_Select definition)
        {
            IDataReader   reader = null;
            IDbConnection conn   = null;

            try
            {
                reader = await this.ExecuteReaderAsync(cmd);

                conn = cmd != null ? cmd.Connection : null;
                TypeDeserializer deserializer = new TypeDeserializer(reader, definition);
                List <T>         result       = await deserializer.DeserializeAsync <T>();

                return(result.FirstOrDefault());
            }
            finally
            {
                if (cmd != null)
                {
                    cmd.Dispose();
                }
                if (reader != null)
                {
                    reader.Dispose();
                }
                this.InternalDispose();
            }
        }
Beispiel #2
0
        // 执行SQL 语句,并返回单个实体对象
        protected virtual async Task <T> ExecuteAsync <T>(IDbCommand command, IMapper map)
        {
            IDataReader   reader = null;
            IDbConnection conn   = null;

            try
            {
                reader = await this.ExecuteReaderAsync(command);

                conn = command != null ? command.Connection : null;
                TypeDeserializer deserializer = new TypeDeserializer(this, reader, map);
                List <T>         result       = await deserializer.DeserializeAsync <T>();

                return(result.FirstOrDefault());
            }
            finally
            {
                if (command != null)
                {
                    command.Dispose();
                }
                if (reader != null)
                {
                    reader.Dispose();
                }
                this.InternalDispose();
            }
        }
Beispiel #3
0
        /// <summary>
        /// 异步执行SQL 语句,并返回 <see cref="IEnumerable"/> 对象
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="command">SQL 命令</param>
        /// <param name="map">命令定义对象,用于解析实体的外键</param>
        /// <returns></returns>
        protected virtual async Task <List <T> > ExecuteListAsync <T>(IDbCommand command, IMapper map)
        {
            IDataReader reader  = null;
            List <T>    objList = new List <T>();

            try
            {
                reader = await this.ExecuteReaderAsync(command);

                TypeDeserializer deserializer = new TypeDeserializer(this, reader, map);
                objList = await deserializer.DeserializeAsync <T>();
            }
            finally
            {
                if (command != null)
                {
                    command.Dispose();
                }
                if (reader != null)
                {
                    reader.Dispose();
                }
                this.InternalDispose();
            }
            return(objList);
        }
Beispiel #4
0
        // 执行SQL 语句,并返回 <see cref="IEnumerable"/> 对象
        List <T> ExecuteList <T>(IDbCommand command, IMapping map)
        {
            IDataReader reader  = null;
            List <T>    objList = new List <T>();

            try
            {
                reader = this.ExecuteReader(command);
                TypeDeserializer deserializer = new TypeDeserializer(reader, map);
                objList = deserializer.Deserialize <T>();
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
                if (command != null)
                {
                    command.Dispose();
                }
                this.InternalDispose();
            }

            return(objList);
        }
Beispiel #5
0
        /// <summary>
        /// 异步执行 SQL 语句,并返回两个实体集合
        /// </summary>
        /// <param name="query1">SQL 命令</param>
        /// <param name="query2">SQL 命令</param>
        /// <param name="query3">SQL 命令</param>
        public override async Task <Tuple <List <T1>, List <T2>, List <T3> > > ExecuteMultipleAsync <T1, T2, T3>(IDbQueryable <T1> query1, IDbQueryable <T2> query2, IDbQueryable <T3> query3)
        {
            List <T1>      q1      = null;
            List <T2>      q2      = null;
            List <T3>      q3      = null;
            IDataReader    reader  = null;
            List <Command> sqlList = query1.Provider.Resolve(new List <object> {
                query1, query2, query3
            });
            List <IMapping> maps = sqlList.ToList(x => x as IMapping, x => x is IMapping);

            TypeDeserializer deserializer1 = null;
            TypeDeserializer deserializer2 = null;
            TypeDeserializer deserializer3 = null;

            Func <DbCommand, Task <object> > doExecute = async cmd =>
            {
                reader = await base.ExecuteReaderAsync(cmd);

                do
                {
                    if (q1 == null)
                    {
                        deserializer1 = new TypeDeserializer(reader, maps.Count > 0 ? maps[0] : null);
                        q1            = deserializer1.Deserialize <T1>();
                    }
                    else if (q2 == null)
                    {
                        deserializer2 = new TypeDeserializer(reader, maps.Count > 1 ? maps[1] : null);
                        q2            = deserializer2.Deserialize <T2>();
                    }
                    else if (q3 == null)
                    {
                        deserializer3 = new TypeDeserializer(reader, maps.Count > 2 ? maps[2] : null);
                        q3            = deserializer3.Deserialize <T3>();
                    }
                }while (reader.NextResult());

                // 释放当前的reader
                if (reader != null)
                {
                    reader.Dispose();
                }
                return(null);
            };

            try
            {
                await base.DoExecuteAsync <object>(sqlList, doExecute);

                return(new Tuple <List <T1>, List <T2>, List <T3> >(q1, q2, q3));
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
            }
        }
Beispiel #6
0
        // 执行SQL 语句,并返回 <see cref="IEnumerable"/> 对象
        List <T> ExecuteList <T>(IDbCommand cmd, DbCommandDefinition_Select definition)
        {
            IDataReader reader  = null;
            List <T>    objList = new List <T>();

            try
            {
                reader = this.ExecuteReader(cmd);
                TypeDeserializer deserializer = new TypeDeserializer(reader, definition);
                objList = deserializer.Deserialize <T>();
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
                if (cmd != null)
                {
                    cmd.Dispose();
                }
                this.InternalDispose();
            }

            return(objList);
        }
Beispiel #7
0
        /// <summary>
        /// 反序列化实体集合
        /// <para>
        /// 适用于自增列与结果集混合输出的场景
        /// </para>
        /// </summary>
        public List <T> Deserialize <T>(out List <int> identitys)
        {
            bool     isThisLine = false;
            object   prevLine   = null;
            List <T> collection = null;

            identitys = null;
            TypeDeserializer <T>   deserializer  = null;
            TypeDeserializer <int> deserializer2 = null;
            bool isAutoIncrement = false;
            bool readedName      = false;


            while (_reader.Read())
            {
                if (!isAutoIncrement && !readedName)
                {
                    isAutoIncrement = _reader.GetName(0) == Constant.AUTOINCREMENTNAME;
                    readedName      = true;
                }

                if (isAutoIncrement)
                {
                    // 输出自增列
                    if (deserializer2 == null)
                    {
                        deserializer2 = new TypeDeserializer <int>(_reader, null);
                    }
                    if (identitys == null)
                    {
                        identitys = new List <int>(1);
                    }
                    int model = deserializer2.Deserialize(prevLine, out isThisLine);
                    identitys.Add(model);
                }
                else
                {
                    // 输出指定类型实体
                    if (deserializer == null)
                    {
                        deserializer = new TypeDeserializer <T>(_reader, _map);
                    }
                    T model = deserializer.Deserialize(prevLine, out isThisLine);
                    if (!isThisLine)
                    {
                        if (collection == null)
                        {
                            collection = new List <T>();
                        }
                        collection.Add(model);
                        prevLine = model;
                    }
                }
            }

            // 返回结果
            return(collection);
        }
Beispiel #8
0
        /// <summary>
        /// 执行 SQL 语句,并返回两个实体集合
        /// </summary>
        /// <param name="query1">SQL 命令</param>
        /// <param name="query2">SQL 命令</param>
        /// <param name="query3">SQL 命令</param>
        /// <param name="trans">事务对象</param>
        public override Tuple <List <T1>, List <T2>, List <T3> > ExecuteMultiple <T1, T2, T3>(IDbQueryable <T1> query1, IDbQueryable <T2> query2, IDbQueryable <T3> query3)
        {
            List <T1>   q1     = null;
            List <T2>   q2     = null;
            List <T3>   q3     = null;
            IDataReader reader = null;
            List <DbCommandDefinition> sqlList = query1.Provider.Resolve(new List <object> {
                query1, query2, query3
            });
            List <DbCommandDefinition_Select> defines = sqlList.ToList(x => x as DbCommandDefinition_Select, x => x is DbCommandDefinition_Select);

            TypeDeserializer deserializer1 = null;
            TypeDeserializer deserializer2 = null;
            TypeDeserializer deserializer3 = null;

            Func <IDbCommand, object> doExecute = cmd =>
            {
                reader = base.ExecuteReader(cmd);
                do
                {
                    if (q1 == null)
                    {
                        deserializer1 = new TypeDeserializer(reader, defines.Count > 0 ? defines[0] : null);
                        q1            = deserializer1.Deserialize <T1>();
                    }
                    else if (q2 == null)
                    {
                        deserializer2 = new TypeDeserializer(reader, defines.Count > 1 ? defines[1] : null);
                        q2            = deserializer2.Deserialize <T2>();
                    }
                    else if (q3 == null)
                    {
                        deserializer3 = new TypeDeserializer(reader, defines.Count > 2 ? defines[2] : null);
                        q3            = deserializer3.Deserialize <T3>();
                    }
                }while (reader.NextResult());

                // 释放当前的reader
                if (reader != null)
                {
                    reader.Dispose();
                }
                return(null);
            };

            try
            {
                base.DoExecute <object>(sqlList, doExecute);
                return(new Tuple <List <T1>, List <T2>, List <T3> >(q1, q2, q3));
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
            }
        }
Beispiel #9
0
        /// <summary>
        /// 计算要插入、更新或删除的已修改对象的集,并执行相应命令以实现对数据库的更改
        /// </summary>
        /// <returns></returns>
        public virtual int SubmitChanges()
        {
            int rowCount = _dbQueryables.Count;

            if (rowCount == 0)
            {
                return(0);
            }

            IDataReader    reader    = null;
            List <int>     identitys = null;
            List <Command> sqlList   = this.Provider.Resolve(_dbQueryables);

            Func <IDbCommand, object> doExecute = cmd =>
            {
                reader = this.Database.ExecuteReader(cmd);
                TypeDeserializer deserializer = new TypeDeserializer(this.Database, reader, null);
                do
                {
                    List <int> autoIncrements = null;
                    deserializer.Deserialize <object>(out autoIncrements);
                    if (autoIncrements != null && autoIncrements.Count > 0)
                    {
                        if (identitys == null)
                        {
                            identitys = new List <int>();
                        }
                        identitys.AddRange(autoIncrements);
                    }
                }while (reader.NextResult());

                // 释放当前的reader
                if (reader != null)
                {
                    reader.Dispose();
                }
                return(null);
            };

            try
            {
                this.Database.Execute <object>(sqlList, doExecute);
                this.SetAutoIncrementValue(_dbQueryables, identitys);
                return(rowCount);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
                this.InternalDispose();
            }
        }
Beispiel #10
0
        /// <summary>
        /// 计算要插入、更新或删除的已修改对象的集,并执行相应命令以实现对数据库的更改
        /// </summary>
        /// <returns></returns>
        public virtual async Task <int> SubmitChangesAsync()
        {
            int rowCount = _dbQueryables.Count;

            if (rowCount == 0)
            {
                return(0);
            }

            List <Command> sqlList   = this.Provider.Resolve(_dbQueryables);
            List <int>     identitys = new List <int>();
            IDataReader    reader    = null;

            try
            {
                Func <IDbCommand, Task <object> > func = async cmd =>
                {
                    reader = await this.Database.ExecuteReaderAsync(cmd);

                    TypeDeserializer deserializer = new TypeDeserializer(this.Database, reader, null);
                    do
                    {
                        List <int> result = null;
                        deserializer.Deserialize <int>(out result);
                        if (result != null && result.Count > 0)
                        {
                            identitys.AddRange(result);
                        }
                    }while (reader.NextResult());

                    // 释放当前的reader
                    if (reader != null)
                    {
                        reader.Dispose();
                    }

                    return(null);
                };

                await this.Database.ExecuteAsync <object>(sqlList, func);

                this.SetAutoIncrementValue(_dbQueryables, identitys);
                return(rowCount);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
                this.InternalDispose();
            }
        }
Beispiel #11
0
        /// <summary>
        /// 异步反序列化实体集合
        /// </summary>
        public async Task <List <T> > DeserializeAsync <T>()
        {
            bool     isThisLine = false;
            object   prevLine   = null;
            List <T> collection = new List <T>();

            TypeDeserializer <T> deserializer = new TypeDeserializer <T>(_reader, _map);

            while (await(_reader as DbDataReader).ReadAsync())
            {
                T model = deserializer.Deserialize(prevLine, out isThisLine);
                if (!isThisLine)
                {
                    collection.Add(model);
                    prevLine = model;
                }
            }

            // 返回结果
            return(collection);
        }
Beispiel #12
0
        /// <summary>
        /// 反序列化实体集合
        /// <para>
        /// 适用于单一结果集的场景
        /// </para>
        /// </summary>
        public List <T> Deserialize <T>()
        {
            bool     isThisLine = false;
            object   prevLine   = null;
            List <T> collection = new List <T>();

            TypeDeserializer <T> deserializer = new TypeDeserializer <T>(_reader, _map);

            while (_reader.Read())
            {
                T model = deserializer.Deserialize(prevLine, out isThisLine);
                if (!isThisLine)
                {
                    collection.Add(model);
                    prevLine = model;
                }
            }

            // 返回结果
            return(collection);
        }
Beispiel #13
0
        // datareader 转实体
        T GetResult <T>(IDataReader reader, IMapping map)
        {
            T result = default(T);

            if (typeof(T) == typeof(DataTable))
            {
                DataTable table = new DataTable();
                table.Load(reader);
                result = (T)(object)table;
            }
            else if (typeof(T) == typeof(DataSet))
            {
                InternalDataSet data = new InternalDataSet();
                data.Load(reader, LoadOption.OverwriteChanges, null, new DataTable[] { });
                result = (T)(object)data;
            }
            else
            {
                TypeDeserializer deserializer = new TypeDeserializer(this, reader, map);
                result = deserializer.Deserialize <T>();
            }
            return(result);
        }
Beispiel #14
0
        // 执行SQL 语句,并返回单个实体对象
        protected virtual T Execute <T>(IDbCommand command, IMapper map)
        {
            IDataReader reader = null;

            try
            {
                reader = this.ExecuteReader(command);
                TypeDeserializer deserializer = new TypeDeserializer(this, reader, map);
                List <T>         result       = deserializer.Deserialize <T>();
                return(result.FirstOrDefault());
            }
            finally
            {
                if (command != null)
                {
                    command.Dispose();
                }
                if (reader != null)
                {
                    reader.Dispose();
                }
                this.InternalDispose();
            }
        }
Beispiel #15
0
        // 执行SQL 语句,并返回单个实体对象
        T Execute <T>(IDbCommand cmd, DbCommandDefinition_Select definition)
        {
            IDataReader reader = null;

            try
            {
                reader = this.ExecuteReader(cmd);
                TypeDeserializer deserializer = new TypeDeserializer(reader, definition);
                List <T>         result       = deserializer.Deserialize <T>();
                return(result.FirstOrDefault());
            }
            finally
            {
                if (cmd != null)
                {
                    cmd.Dispose();
                }
                if (reader != null)
                {
                    reader.Dispose();
                }
                this.InternalDispose();
            }
        }
Beispiel #16
0
        /// <summary>
        /// 执行 SQL 语句,并返回多个实体集合
        /// </summary>
        /// <typeparam name="T1">第一个列表的元素类型</typeparam>
        /// <typeparam name="T2">第二个列表的元素类型</typeparam>
        /// <typeparam name="T3">第三个列表的元素类型</typeparam>
        /// <typeparam name="T4">第四个列表的元素类型</typeparam>
        /// <typeparam name="T5">第五个列表的元素类型</typeparam>
        /// <typeparam name="T6">第六个列表的元素类型</typeparam>
        /// <typeparam name="T7">第七个列表的元素类型</typeparam>
        /// <param name="command">SQL 命令</param>
        /// <param name="maps">实体映射描述列表</param>
        /// <returns></returns>
        protected virtual async Task <Tuple <List <T1>, List <T2>, List <T3>, List <T4>, List <T5>, List <T6>, List <T7> > > ExecuteAsync <T1, T2, T3, T4, T5, T6, T7>(IDbCommand command, List <IMapping> maps = null)
        {
            IDataReader   reader = null;
            IDbConnection conn   = null;
            List <T1>     q1     = null;
            List <T2>     q2     = null;
            List <T3>     q3     = null;
            List <T4>     q4     = null;
            List <T5>     q5     = null;
            List <T6>     q6     = null;
            List <T7>     q7     = null;

            TypeDeserializer deserializer1 = null;
            TypeDeserializer deserializer2 = null;
            TypeDeserializer deserializer3 = null;
            TypeDeserializer deserializer4 = null;
            TypeDeserializer deserializer5 = null;
            TypeDeserializer deserializer6 = null;
            TypeDeserializer deserializer7 = null;

            try
            {
                int i = 0;
                reader = await this.ExecuteReaderAsync(command);

                conn = command != null ? command.Connection : null;

                do
                {
                    i += 1;
                    switch (i)
                    {
                        #region 元组赋值

                    case 1:
                        if (deserializer1 == null)
                        {
                            deserializer1 = new TypeDeserializer(this, reader, maps != null && maps.Count > i - 1 ? maps[i - 1] : null);
                        }
                        q1 = deserializer1.Deserialize <List <T1> >();

                        break;

                    case 2:
                        if (deserializer2 == null)
                        {
                            deserializer2 = new TypeDeserializer(this, reader, maps != null && maps.Count > i - 1 ? maps[i - 1] : null);
                        }
                        q2 = deserializer2.Deserialize <List <T2> >();

                        break;

                    case 3:
                        if (deserializer3 == null)
                        {
                            deserializer3 = new TypeDeserializer(this, reader, maps != null && maps.Count > i - 1 ? maps[i - 1] : null);
                        }
                        q3 = deserializer3.Deserialize <List <T3> >();

                        break;

                    case 4:
                        if (deserializer4 == null)
                        {
                            deserializer4 = new TypeDeserializer(this, reader, maps != null && maps.Count > i - 1 ? maps[i - 1] : null);
                        }
                        q4 = deserializer4.Deserialize <List <T4> >();

                        break;

                    case 5:
                        if (deserializer5 == null)
                        {
                            deserializer5 = new TypeDeserializer(this, reader, maps != null && maps.Count > i - 1 ? maps[i - 1] : null);
                        }
                        q5 = deserializer5.Deserialize <List <T5> >();

                        break;

                    case 6:
                        if (deserializer6 == null)
                        {
                            deserializer6 = new TypeDeserializer(this, reader, maps != null && maps.Count > i - 1 ? maps[i - 1] : null);
                        }
                        q6 = deserializer6.Deserialize <List <T6> >();

                        break;

                    case 7:
                        if (deserializer7 == null)
                        {
                            deserializer7 = new TypeDeserializer(this, reader, maps != null && maps.Count > i - 1 ? maps[i - 1] : null);
                        }
                        q7 = deserializer7.Deserialize <List <T7> >();

                        break;

                        #endregion
                    }
                }while (reader.NextResult());
            }
            finally
            {
                if (command != null)
                {
                    command.Dispose();
                }
                if (reader != null)
                {
                    reader.Dispose();
                }
                this.InternalDispose();
            }

            return(new Tuple <List <T1>, List <T2>, List <T3>, List <T4>, List <T5>, List <T6>, List <T7> >(q1 ?? new List <T1>(), q2 ?? new List <T2>(), q3 ?? new List <T3>(), q4 ?? new List <T4>(), q5 ?? new List <T5>(), q6 ?? new List <T6>(), q7 ?? new List <T7>()));
        }
Beispiel #17
0
        /// <summary>
        /// 计算要插入、更新或删除的已修改对象的集,并执行相应命令以实现对数据库的更改
        /// </summary>
        /// <typeparam name="T1">T</typeparam>
        /// <typeparam name="T2">T</typeparam>
        /// <param name="result1">提交更改并查询数据</param>
        /// <returns></returns>
        public virtual int SubmitChanges <T1, T2>(out List <T1> result1, out List <T2> result2)
        {
            result1 = new List <T1>();
            result2 = new List <T2>();
            int rowCount = _dbQueryables.Count;

            if (rowCount == 0)
            {
                return(0);
            }

            List <T1>       q1        = null;
            List <T2>       q2        = null;
            IDataReader     reader    = null;
            List <int>      identitys = null;
            List <Command>  sqlList   = this.Provider.Resolve(_dbQueryables);
            List <IMapping> maps      = sqlList.ToList(x => x as IMapping, x => x is IMapping);

            Func <IDbCommand, object> doExecute = cmd =>
            {
                reader = this.Database.ExecuteReader(cmd);
                TypeDeserializer deserializer1 = null;
                TypeDeserializer deserializer2 = null;
                do
                {
                    if (q1 == null)
                    {
                        // 先查第一个类型集合
                        List <int> autoIncrements = null;
                        if (deserializer1 == null)
                        {
                            deserializer1 = new TypeDeserializer(this.Database, reader, maps.Count > 0 ? maps[0] : null);
                        }
                        var collection = deserializer1.Deserialize <T1>(out autoIncrements);

                        if (autoIncrements != null)
                        {
                            if (identitys == null)
                            {
                                identitys = new List <int>();
                            }
                            identitys.AddRange(autoIncrements);
                        }
                        else if (collection != null)
                        {
                            q1 = collection;
                        }
                    }
                    else
                    {
                        // 再查第二个类型集合
                        List <int> autoIncrements = null;
                        if (deserializer2 == null)
                        {
                            deserializer2 = new TypeDeserializer(this.Database, reader, maps.Count > 1 ? maps[1] : null);
                        }
                        var collection = deserializer2.Deserialize <T2>(out autoIncrements);

                        if (autoIncrements != null)
                        {
                            if (identitys == null)
                            {
                                identitys = new List <int>();
                            }
                            identitys.AddRange(autoIncrements);
                        }
                        else if (collection != null)
                        {
                            if (q2 == null)
                            {
                                q2 = collection;
                            }
                        }
                    }
                }while (reader.NextResult());

                // 释放当前的reader
                if (reader != null)
                {
                    reader.Dispose();
                }
                return(null);
            };

            try
            {
                this.Database.Execute <object>(sqlList, doExecute);
                result1 = q1 ?? new List <T1>(0);
                result2 = q2 ?? new List <T2>(0);
                this.SetAutoIncrementValue(_dbQueryables, identitys);
                return(rowCount);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
                this.InternalDispose();
            }
        }
Beispiel #18
0
        // 执行 SQL 语句,并返回多个实体集合
        protected override async Task <Tuple <List <T1>, List <T2>, List <T3>, List <T4>, List <T5>, List <T6>, List <T7> > > ExecuteMultipleAsync <T1, T2, T3, T4, T5, T6, T7>(IDbCommand command, List <IMapping> maps = null)
        {
            List <T1>      q1     = null;
            List <T2>      q2     = null;
            List <T3>      q3     = null;
            List <T4>      q4     = null;
            List <T5>      q5     = null;
            List <T6>      q6     = null;
            List <T7>      q7     = null;
            IDataReader    reader = null;
            List <Command> myList = this.TrySeparate(command.CommandText, command.Parameters, command.CommandType);

            if (myList == null)
            {
                q1 = await this.ExecuteListAsync <T1>(command, maps != null && maps.Count > 0?maps[0] : null);
            }
            else
            {
                TypeDeserializer deserializer1 = null;
                TypeDeserializer deserializer2 = null;
                TypeDeserializer deserializer3 = null;
                TypeDeserializer deserializer4 = null;
                TypeDeserializer deserializer5 = null;
                TypeDeserializer deserializer6 = null;
                TypeDeserializer deserializer7 = null;

                Func <IDbCommand, Task <object> > doExecute = async cmd =>
                {
                    reader = await base.ExecuteReaderAsync(cmd);

                    do
                    {
                        if (q1 == null)
                        {
                            deserializer1 = new TypeDeserializer(this, reader, maps != null && maps.Count > 0 ? maps[0] : null);
                            q1            = deserializer1.Deserialize <T1>();
                        }
                        else if (q2 == null)
                        {
                            deserializer2 = new TypeDeserializer(this, reader, maps != null && maps.Count > 1 ? maps[1] : null);
                            q2            = deserializer2.Deserialize <T2>();
                        }
                        else if (q3 == null)
                        {
                            deserializer3 = new TypeDeserializer(this, reader, maps != null && maps.Count > 2 ? maps[2] : null);
                            q3            = deserializer3.Deserialize <T3>();
                        }
                        else if (q4 == null)
                        {
                            deserializer4 = new TypeDeserializer(this, reader, maps != null && maps.Count > 3 ? maps[3] : null);
                            q4            = deserializer4.Deserialize <T4>();
                        }
                        else if (q5 == null)
                        {
                            deserializer5 = new TypeDeserializer(this, reader, maps != null && maps.Count > 4 ? maps[4] : null);
                            q5            = deserializer5.Deserialize <T5>();
                        }
                        else if (q6 == null)
                        {
                            deserializer6 = new TypeDeserializer(this, reader, maps != null && maps.Count > 5 ? maps[5] : null);
                            q6            = deserializer6.Deserialize <T6>();
                        }
                        else if (q7 == null)
                        {
                            deserializer7 = new TypeDeserializer(this, reader, maps != null && maps.Count > 6 ? maps[6] : null);
                            q7            = deserializer7.Deserialize <T7>();
                        }
                    }while (reader.NextResult());

                    // 释放当前的reader
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                    return(null);
                };

                try
                {
                    await base.DoExecuteAsync <object>(myList, doExecute);
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                }
            }

            return(new Tuple <List <T1>, List <T2>, List <T3>, List <T4>, List <T5>, List <T6>, List <T7> >(
                       q1 ?? new List <T1>(), q2 ?? new List <T2>(), q3 ?? new List <T3>(), q4 ?? new List <T4>(), q5 ?? new List <T5>(), q6 ?? new List <T6>(), q7 ?? new List <T7>()));
        }
Beispiel #19
0
        /// <summary>
        /// 计算要插入、更新或删除的已修改对象的集,并执行相应命令以实现对数据库的更改
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <param name="result">提交更改并查询数据</param>
        /// <returns></returns>
        public virtual int SubmitChanges <T>(out List <T> result)
        {
            result = new List <T>();
            int rowCount = _dbQueryables.Count;

            if (rowCount == 0)
            {
                return(0);
            }

            List <T>          q1        = null;
            IDataReader       reader    = null;
            List <int>        identitys = null;
            List <RawCommand> sqlList   = this.Resolve();

            Func <IDbCommand, object> doExecute = cmd =>
            {
                reader = this.Database.ExecuteReader(cmd);
                TypeDeserializer deserializer = new TypeDeserializer(this.Database, reader, null);
                do
                {
                    List <int> autoIncrements = null;
                    var        collection     = deserializer.Deserialize <T>(out autoIncrements);
                    if (autoIncrements != null)
                    {
                        if (identitys == null)
                        {
                            identitys = new List <int>();
                        }
                        identitys.AddRange(autoIncrements);
                    }
                    else if (collection != null)
                    {
                        if (q1 == null)
                        {
                            q1 = collection;
                        }
                    }
                }while (reader.NextResult());

                // 释放当前的reader
                if (reader != null)
                {
                    reader.Dispose();
                }
                return(null);
            };

            try
            {
                this.Database.Execute <object>(sqlList, doExecute);
                result = q1 ?? new List <T>(0);
                this.SetIdentityValue(_dbQueryables, identitys);
                return(rowCount);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
                this.InternalDispose();
            }
        }
Beispiel #20
0
        // 执行 SQL 语句,并返回多个实体集合
        Tuple <List <T1>, List <T2>, List <T3>, List <T4>, List <T5>, List <T6>, List <T7> > ExecuteMultiple <T1, T2, T3, T4, T5, T6, T7>(IDbCommand cmd, List <DbCommandDefinition_Select> defines = null)
        {
            IDataReader reader = null;
            List <T1>   q1     = null;
            List <T2>   q2     = null;
            List <T3>   q3     = null;
            List <T4>   q4     = null;
            List <T5>   q5     = null;
            List <T6>   q6     = null;
            List <T7>   q7     = null;

            TypeDeserializer deserializer1 = null;
            TypeDeserializer deserializer2 = null;
            TypeDeserializer deserializer3 = null;
            TypeDeserializer deserializer4 = null;
            TypeDeserializer deserializer5 = null;
            TypeDeserializer deserializer6 = null;
            TypeDeserializer deserializer7 = null;

            try
            {
                int i = 0;
                reader = this.ExecuteReader(cmd);

                do
                {
                    i += 1;

                    switch (i)
                    {
                        #region 元组赋值

                    case 1:
                        if (deserializer1 == null)
                        {
                            deserializer1 = new TypeDeserializer(reader, defines != null ? defines[i - 1] : null);
                        }
                        q1 = deserializer1.Deserialize <T1>();

                        break;

                    case 2:
                        if (deserializer2 == null)
                        {
                            deserializer2 = new TypeDeserializer(reader, defines != null ? defines[i - 1] : null);
                        }
                        q2 = deserializer2.Deserialize <T2>();

                        break;

                    case 3:
                        if (deserializer3 == null)
                        {
                            deserializer3 = new TypeDeserializer(reader, defines != null ? defines[i - 1] : null);
                        }
                        q3 = deserializer3.Deserialize <T3>();

                        break;

                    case 4:
                        if (deserializer4 == null)
                        {
                            deserializer4 = new TypeDeserializer(reader, defines != null ? defines[i - 1] : null);
                        }
                        q4 = deserializer4.Deserialize <T4>();

                        break;

                    case 5:
                        if (deserializer5 == null)
                        {
                            deserializer5 = new TypeDeserializer(reader, defines != null ? defines[i - 1] : null);
                        }
                        q5 = deserializer5.Deserialize <T5>();

                        break;

                    case 6:
                        if (deserializer6 == null)
                        {
                            deserializer6 = new TypeDeserializer(reader, defines != null ? defines[i - 1] : null);
                        }
                        q6 = deserializer6.Deserialize <T6>();

                        break;

                    case 7:
                        if (deserializer7 == null)
                        {
                            deserializer7 = new TypeDeserializer(reader, defines != null ? defines[i - 1] : null);
                        }
                        q7 = deserializer7.Deserialize <T7>();

                        break;

                        #endregion
                    }
                }while (reader.NextResult());
            }
            finally
            {
                if (cmd != null)
                {
                    cmd.Dispose();
                }
                if (reader != null)
                {
                    reader.Dispose();
                }
                this.InternalDispose();
            }

            return(new Tuple <List <T1>, List <T2>, List <T3>, List <T4>, List <T5>, List <T6>, List <T7> >(q1 ?? new List <T1>(), q2 ?? new List <T2>(), q3 ?? new List <T3>(), q4 ?? new List <T4>(), q5 ?? new List <T5>(), q6 ?? new List <T6>(), q7 ?? new List <T7>()));
        }