Esempio n. 1
0
 Task <IEnumerable <dynamic> > IDatabase.ExecuteEnumerableAsync(IQueryCommand queryCommand, IDataSegment segment, ParameterCollection parameters, CancellationToken cancellationToken)
 {
     return(_innerDatabase.ExecuteEnumerableAsync(queryCommand, segment, parameters, cancellationToken));
 }
Esempio n. 2
0
 DataTable IDatabase.ExecuteDataTable(IQueryCommand queryCommand, string tableName, IDataSegment segment, ParameterCollection parameters)
 {
     return(_innerDatabase.ExecuteDataTable(queryCommand, tableName, segment, parameters));
 }
Esempio n. 3
0
 Task <IDataReader> IDatabase.ExecuteReaderAsync(IQueryCommand queryCommand, IDataSegment segment, ParameterCollection parameters, CommandBehavior?behavior, CancellationToken cancellationToken)
 {
     return(_innerDatabase.ExecuteReaderAsync(queryCommand, segment, parameters, behavior, cancellationToken));
 }
Esempio n. 4
0
 Task <IEnumerable <T> > IDatabase.ExecuteEnumerableAsync <T>(IQueryCommand queryCommand, IDataSegment segment, ParameterCollection parameters, IDataRowMapper <T> rowMapper, CancellationToken cancellationToken)
 {
     return(_innerDatabase.ExecuteEnumerableAsync <T>(queryCommand, segment, parameters, rowMapper, cancellationToken));
 }
Esempio n. 5
0
        /// <summary>
        /// 执行查询文本并将结果以一个 <see cref="IEnumerable{T}"/> 的序列返回。
        /// </summary>
        /// <typeparam name="T">查询对象类型。</typeparam>
        /// <param name="queryCommand">查询命令。</param>
        /// <param name="segment">数据分段对象。</param>
        /// <param name="parameters">查询参数集合。</param>
        /// <param name="rowMapper">数据行映射器。</param>
        /// <returns>一个 <typeparamref name="T"/> 类型的对象的枚举器。</returns>
        public virtual IEnumerable <T> ExecuteEnumerable <T>(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null, IDataRowMapper <T> rowMapper = null)
        {
            Guard.ArgumentNull(queryCommand, nameof(queryCommand));

            rowMapper = rowMapper ?? RowMapperFactory.CreateRowMapper <T>();
            rowMapper.RecordWrapper = Provider.GetService <IRecordWrapper>();
            using (var reader = ExecuteReader(queryCommand, segment, parameters))
            {
                while (reader.Read())
                {
                    yield return(rowMapper.Map(this, reader));
                }
            }
        }
Esempio n. 6
0
 public string Segment(string commandText, IDataSegment segment)
 {
     throw new NotImplementedException();
 }
Esempio n. 7
0
 /// <summary>
 /// 对命令文本进行分段处理,使之能够返回小范围内的数据。
 /// </summary>
 /// <param name="commandText">命令文本。</param>
 /// <param name="segment">数据分段对象。</param>
 /// <returns>处理后的分段命令文本。</returns>
 /// <exception cref="SegmentNotSupportedException">当前数据库或版本不支持分段时,引发该异常。</exception>
 public string Segment(string commandText, IDataSegment segment)
 {
     throw new SegmentNotSupportedException();
 }
Esempio n. 8
0
 /// <summary>
 /// 对命令文本进行分段处理,使之能够返回小范围内的数据。
 /// </summary>
 /// <param name="commandText">命令文本。</param>
 /// <param name="segment">数据分段对象。</param>
 /// <returns>处理后的分段命令文本。</returns>
 public virtual string Segment(string commandText, IDataSegment segment)
 {
     return(segment.Start != null ?
            $"{commandText} ROWS {segment.Start} TO {segment.End - 1}" :
            $"{commandText} ROWS {segment.Length}");
 }
Esempio n. 9
0
        /// <summary>
        /// 异步执行查询文本并返回一个 <see cref="IDataReader"/>。
        /// </summary>
        /// <param name="queryCommand">查询命令。</param>
        /// <param name="segment">数据分段对象。</param>
        /// <param name="parameters">查询参数集合。</param>
        /// <param name="cancellationToken">取消操作的通知。</param>
        /// <returns>一个 <see cref="IDataReader"/> 对象。</returns>
        public async virtual Task <IDataReader> ExecuteReaderAsync(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null, CancellationToken cancellationToken = default)
        {
            Guard.ArgumentNull(queryCommand, nameof(queryCommand));
            return(await UsingConnection(connection =>
            {
                var command = CreateDbCommand(connection, queryCommand, parameters);
                try
                {
                    var context = new CommandContext(this, command, segment, parameters);
                    HandleSegmentCommand(context);

                    return HandleExecuteTask(HandleCommandExecute(command, () => command.ExecuteReaderAsync(cancellationToken)), command, parameters);
                }
                catch (DbException exp)
                {
                    HandleFailedLog(command, exp);
                    command.Dispose();

                    throw new CommandException(command, exp);
                }
            }, mode : DistributedMode.Slave));
        }
Esempio n. 10
0
        /// <summary>
        /// 异步执行查询文本并将结果以一个 <see cref="IEnumerable{T}"/> 的序列返回。
        /// </summary>
        /// <typeparam name="T">查询对象类型。</typeparam>
        /// <param name="queryCommand">查询命令。</param>
        /// <param name="segment">数据分段对象。</param>
        /// <param name="parameters">查询参数集合。</param>
        /// <param name="rowMapper">数据行映射器。</param>
        /// <param name="cancellationToken">取消操作的通知。</param>
        /// <returns>一个 <typeparamref name="T"/> 类型的对象的枚举器。</returns>
        public async virtual Task <IEnumerable <T> > ExecuteEnumerableAsync <T>(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null, IDataRowMapper <T> rowMapper = null, CancellationToken cancellationToken = default)
        {
            Guard.ArgumentNull(queryCommand, nameof(queryCommand));

            rowMapper = rowMapper ?? RowMapperFactory.CreateRowMapper <T>();
            rowMapper.RecordWrapper = Provider.GetService <IRecordWrapper>();

            var reader = await ExecuteReaderAsync(queryCommand, segment, parameters, cancellationToken);

            return(new AsyncEnumerable <T>(this, reader, rowMapper));
        }
Esempio n. 11
0
        /// <summary>
        /// 执行查询文本并将结果填充到指定的 <see cref="DataSet"/> 对象中。
        /// </summary>
        /// <param name="dataSet">要填充的 <see cref="DataSet"/>。</param>
        /// <param name="queryCommand">查询命令。</param>
        /// <param name="tableName">表的名称,多个表名称使用逗号分隔。</param>
        /// <param name="segment">数据分段对象。</param>
        /// <param name="parameters">查询参数集合。</param>
        public virtual void FillDataSet(DataSet dataSet, IQueryCommand queryCommand, string tableName = null, IDataSegment segment = null, ParameterCollection parameters = null)
        {
            Guard.ArgumentNull(queryCommand, nameof(queryCommand));
            var adapter = Provider.DbProviderFactory.CreateDataAdapter();

            if (adapter == null)
            {
                throw new NotSupportedException(nameof(DataAdapter));
            }

            UsingConnection(connection =>
            {
                using (var command = CreateDbCommand(connection, queryCommand, parameters))
                {
                    adapter.SelectCommand = command;

                    //如果要使用Update更新DataSet,则必须指定MissingSchemaAction.AddWithKey,
                    //但在Oracle使用分页时,却不能设置该属性,否则抛出“应为标识符或带引号的标识符”
                    //因此,如果要实现Update,只有手动添加DataSet的PrimaryKeys
                    //adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                    dataSet.EnforceConstraints = false;
                    HandleAdapterTableMapping(adapter, tableName);

                    try
                    {
                        var context = new CommandContext(this, command, segment, parameters);

                        //无法分页时才采用 adapter.Fill(dataSet, startRecord, maxRecords, "Table")
                        var handler = !HandleSegmentCommand(context) && segment != null ?
                                      new Func <int>(() => adapter.Fill(dataSet, segment.Start.Value, segment.Length, "Table")) :
                                      new Func <int>(() => adapter.Fill(dataSet));

                        HandleCommandExecute(command, handler);
                    }
                    catch (DbException exp)
                    {
                        HandleFailedLog(command, exp);

                        throw new CommandException(command, exp);
                    }
                }
            }, false, DistributedMode.Slave);
        }
Esempio n. 12
0
        public virtual IEnumerable <object> ExecuteEnumerable(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null)
#endif
        {
            Guard.ArgumentNull(queryCommand, nameof(queryCommand));

            using (var reader = ExecuteReader(queryCommand, segment, parameters))
            {
                var wrapper = Provider.GetService <IRecordWrapper>();
#if !NET35
                TypeDescriptorUtility.AddDefaultDynamicProvider();

                while (reader.Read())
                {
                    var expando    = new ExpandoObject();
                    var dictionary = (IDictionary <string, object>)expando;

                    for (var i = 0; i < reader.FieldCount; i++)
                    {
                        var name = wrapper.GetFieldName(reader, i); //34343424
                        if (name.Equals("ROW_NUM"))
                        {
                            continue;
                        }

                        dictionary.Add(wrapper.GetFieldName(reader, i), RecordWrapHelper.GetValue(wrapper, reader, i));
                    }

                    yield return(expando);
                }
#else
                var builder     = new DataReaderTypeBuilder(reader);
                var elementType = builder.CreateType();
                while (reader.Read())
                {
                    yield return(elementType.New(reader, wrapper));
                }
#endif
            }
        }
Esempio n. 13
0
        /// <summary>
        /// 根据自定义的SQL语句查询返回一组动态对象。
        /// </summary>
        /// <param name="queryCommand">查询命令。</param>
        /// <param name="segment">数据分段对象。</param>
        /// <param name="parameters">查询参数集合。</param>
        /// <returns>一个动态对象的枚举器。</returns>
#if !NET35
        public virtual IEnumerable <dynamic> ExecuteEnumerable(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null)
Esempio n. 14
0
 IEnumerable <T> IDatabase.ExecuteEnumerable <T>(IQueryCommand queryCommand, IDataSegment segment, ParameterCollection parameters, IDataRowMapper <T> rowMapper)
 {
     return(_innerDatabase.ExecuteEnumerable(queryCommand, segment, parameters, rowMapper));
 }
Esempio n. 15
0
        /// <summary>
        /// 执行一个查询,返回一个序列。
        /// </summary>
        /// <param name="database"></param>
        /// <param name="elementType"></param>
        /// <param name="queryCommand"></param>
        /// <param name="segment"></param>
        /// <param name="parameters"></param>
        /// <param name="initializer"></param>
        /// <param name="setter"></param>
        /// <returns></returns>
        internal static IEnumerable InternalExecuteEnumerable(this IDatabase database, Type elementType, IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null, Action <object> initializer = null, Action <IDataReader, object> setter = null)
        {
            var mapper = RowMapperFactory.CreateMapper(elementType);

            Guard.ArgumentNull(mapper, "mapper");

            mapper.RecordWrapper = database.Provider.GetService <IRecordWrapper>();
            mapper.Initializer   = initializer;

            using (var reader = database.ExecuteReader(queryCommand, segment, parameters))
            {
                while (reader.Read())
                {
                    var item = mapper.Map(reader);
                    if (setter != null)
                    {
                        setter(reader, item);
                    }

                    yield return(item);
                }
            }
        }
Esempio n. 16
0
 IEnumerable <object> IDatabase.ExecuteEnumerable(IQueryCommand queryCommand, IDataSegment segment, ParameterCollection parameters)
 {
     return(_innerDatabase.ExecuteEnumerable(queryCommand, segment, parameters));
 }
Esempio n. 17
0
 IDataReader IDatabase.ExecuteReader(IQueryCommand queryCommand, IDataSegment segment, ParameterCollection parameters, CommandBehavior?behavior)
 {
     return(_innerDatabase.ExecuteReader(queryCommand, segment, parameters, behavior));
 }
Esempio n. 18
0
        /// <summary>
        /// 执行查询文本并将结果填充到指定的 <see cref="DataTable"/> 对象中。
        /// </summary>
        /// <param name="queryCommand">查询命令。</param>
        /// <param name="tableName"><see cref="DataTable"/> 的名称。</param>
        /// <param name="segment">数据分段对象。</param>
        /// <param name="parameters">查询参数集合。</param>
        /// <returns>一个 <see cref="DataTable"/> 对象。</returns>
        public virtual DataTable ExecuteDataTable(IQueryCommand queryCommand, string tableName = null, IDataSegment segment = null, ParameterCollection parameters = null)
        {
            Guard.ArgumentNull(queryCommand, nameof(queryCommand));
            var ds = new DataSet();

            FillDataSet(ds, queryCommand, tableName, segment, parameters);
            return(ds.Tables.Count == 0 ? null : ds.Tables[0]);
        }
Esempio n. 19
0
 void IDatabase.FillDataSet(DataSet dataSet, IQueryCommand queryCommand, string tableName, IDataSegment segment, ParameterCollection parameters)
 {
     _innerDatabase.FillDataSet(dataSet, queryCommand, tableName, segment, parameters);
 }
Esempio n. 20
0
        /// <summary>
        /// 根据自定义的SQL语句查询返回一组动态对象。
        /// </summary>
        /// <param name="queryCommand">查询命令。</param>
        /// <param name="segment">数据分段对象。</param>
        /// <param name="parameters">查询参数集合。</param>
        /// <returns>一个动态对象的枚举器。</returns>
        public virtual IEnumerable <dynamic> ExecuteEnumerable(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null)
        {
            Guard.ArgumentNull(queryCommand, nameof(queryCommand));

            using (var reader = ExecuteReader(queryCommand, segment, parameters))
            {
                var wrapper = Provider.GetService <IRecordWrapper>();
                TypeDescriptorUtility.AddDefaultDynamicProvider();

                while (reader.Read())
                {
                    var expando    = new ExpandoObject();
                    var dictionary = (IDictionary <string, object>)expando;

                    for (var i = 0; i < reader.FieldCount; i++)
                    {
                        var name = wrapper.GetFieldName(reader, i);
                        if (name.Equals("ROW_NUM"))
                        {
                            continue;
                        }

                        dictionary.Add(wrapper.GetFieldName(reader, i), RecordWrapHelper.GetValue(wrapper, reader, i));
                    }

                    yield return(expando);
                }
            }
        }
Esempio n. 21
0
 /// <summary>
 /// 对命令文本进行分段处理,使之能够返回小范围内的数据。
 /// </summary>
 /// <param name="commandText">命令文本。</param>
 /// <param name="segment">数据分段对象。</param>
 /// <returns>处理后的分段命令文本。</returns>
 /// <exception cref="SegmentNotSupportedException">当前数据库或版本不支持分段时,引发该异常。</exception>
 public virtual string Segment(string commandText, IDataSegment segment)
 {
     throw new SyntaxParseException(MethodInfo.GetCurrentMethod().Name);
 }