public static DbCommand CreateBulkQueryCommand(
            DbConnection connection,
            string sqlStatement,
            IEnumerable parameters,
            bool paramsAsList,
            SourceAdapterBuilderDelegate sourceAdapterBuilder,
            TargetAdapterBuilderDelegate adapterBuilder)
        {
            ColumnMetadataList metadata = null;

            // ReSharper disable once LoopCanBeConvertedToQuery
            foreach (var param in parameters)
            {
                metadata = new ColumnMetadataList {
                    new ColumnMetadata
                    {
                        ColumnName = "P",
                        DataType   = param.GetType()
                    }
                };
                break;
            }
            if (metadata == null)
            {
                throw new InvalidOperationException("parameters enumerable is empty");
            }

            return(CreateBulkQueryCommand(connection, sqlStatement, metadata, parameters, paramsAsList, sourceAdapterBuilder, adapterBuilder));
        }
示例#2
0
        public virtual void Prepare()
        {
            if (_prepared)
            {
                throw new InvalidOperationException("DataReplicator already prepared.");
            }
            try
            {
                var sqlBuilder = (IClassSqlBuilder)typeof(TSourceAdapter);
                _sourceCmds = new List <DbCommand>();
                foreach (var sqlStatement in _sourceTables)
                {
                    _sourceCmds.Add(sqlStatement.Item4 ?? sqlBuilder.BuildCommand(sqlStatement.Item2, null));
                    sqlStatement.Item4 = null;
                }

                _sourceConnections  = new DbConnection[_sourceCmds.Count];
                _targetConnections  = new TTargetConn[_sourceCmds.Count];
                ColumnMetadataLists = new ColumnMetadataList[_sourceCmds.Count];
                _readers            = new List <DbDataReader> {
                    Capacity = _sourceCmds.Count
                };
                for (var i = 0; i < _sourceCmds.Count; i++)
                {
                    _readers.Add(null);
                }

                _parallelRunner = new BoundedParallel(1, ParallelismLevel);
                _parallelRunner.For(0, _sourceCmds.Count, i =>
                {
                    if (_sourceTables[i].Item4 != null)
                    {
                        _sourceConnections[i] = _sourceTables[i].Item4.Connection;
                    }
                    else
                    {
                        _sourceConnections[i] = sqlBuilder.BuildConnection(_sourceConnStr);
                    }
                    _sourceConnections[i].Open();

                    _sourceCmds[i].Connection = _sourceConnections[i];
                    var reader  = _sourceCmds[i].ExecuteReader();
                    _readers[i] = reader;

                    ColumnMetadataLists[i] = new ColumnMetadataListFromDbReader(reader);

                    _targetConnections[i] = GenericObjectBuilder.Build <TTargetConn>(_targetConnStr);
                    _targetConnections[i].Open();
                });
                _prepared = true;
            }
            catch (Exception)
            {
                UnPrepare(true);
                throw;
            }
        }
示例#3
0
 public static SqlCommand CreateBulkQueryCommand(
     this SqlConnection connection,
     string sqlStatement,
     ColumnMetadataList metadata,
     IEnumerable parameters,
     bool paramsAsList = false)
 {
     return((SqlCommand)DbConnectionExtensions.CreateBulkQueryCommand(connection, sqlStatement, metadata, parameters, paramsAsList, SourceAdapterBuilder, TargetAdapterBuilder));
 }
示例#4
0
        private static TargetAdapterSql TargetAdapterBuilder(string sqlStatement, ColumnMetadataList metadata,
                                                             DbConnection connection, bool paramsAsList)
        {
            var columnNames = metadata.Select(meta => meta.ColumnName).ToList();

            return(new SqlClientAdapterBulkCommand(sqlStatement, columnNames, (SqlConnection)connection)
            {
                ParamsAsList = paramsAsList
            });
        }
示例#5
0
 public void Pump(
     Stream source,
     ColumnMetadataList sourceMetadatas,
     IEnumerable <ITargetAdapter <PoolEntry <object[]> > > dataPipelineTargetAdapters,
     string delimiter     = DelimitedTextSourceAdapter.DefaultDelimiter,
     int rowsPoolCapacity = TextSourceAdapter.DefaultRowsPoolCapacity)
 {
     using var reader = new StreamReader(source);
     Pump(reader, sourceMetadatas, dataPipelineTargetAdapters, delimiter, rowsPoolCapacity);
 }
示例#6
0
 public void Pump(
     string sourceFileName,
     ColumnMetadataList sourceMetadatas,
     IEnumerable <ITargetAdapter <PoolEntry <object[]> > > dataPipelineTargetAdapters,
     string delimiter     = DelimitedTextSourceAdapter.DefaultDelimiter,
     int rowsPoolCapacity = TextSourceAdapter.DefaultRowsPoolCapacity)
 {
     using var stream = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read);
     using var reader = new StreamReader(stream);
     Pump(reader, sourceMetadatas, dataPipelineTargetAdapters, delimiter, rowsPoolCapacity);
 }
        public void Pump(
            TextReader source,
            ColumnMetadataList sourceMetadatas,
            ITargetAdapter <PoolEntry <object[]> > targetAdapter,
            int rowsPoolCapacity = TextSourceAdapter.DefaultRowsPoolCapacity)
        {
            var sourceAdapter = new FixedLengthTextSourceAdapter(source)
            {
                ColumnMetadatas = sourceMetadatas,
                RowsPoolSize    = rowsPoolCapacity
            };

            base.Pump(sourceAdapter, targetAdapter);
        }
        public static DbCommand CreateBulkQueryCommand(
            DbConnection connection,
            string sqlStatement,
            ColumnMetadataList metadata,
            IEnumerable parameters,
            bool paramsAsList,
            SourceAdapterBuilderDelegate sourceAdapterBuilder,
            TargetAdapterBuilderDelegate adapterBuilder)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (metadata == null)
            {
                throw new ArgumentNullException(nameof(metadata));
            }
            if (sourceAdapterBuilder == null)
            {
                throw new ArgumentNullException(nameof(sourceAdapterBuilder));
            }
            if (adapterBuilder == null)
            {
                throw new ArgumentNullException(nameof(adapterBuilder));
            }

            var sourceAdapter = sourceAdapterBuilder(parameters, metadata);
            var adapter       = adapterBuilder(sqlStatement, metadata, connection, paramsAsList);

            adapter.Prepare(sourceAdapter);
            try
            {
                foreach (var row in sourceAdapter.RowsEnumerable)
                {
                    adapter.Process(row);
                }
                adapter.BindParameters();
                return(adapter.TakeCommand());
            }
            finally
            {
                adapter.UnPrepare();
            }
        }
示例#9
0
        protected override string BuildCreateTableStatement(string tableName, ColumnMetadataList metadatas)
        {
            const string defDelimiter = ",\r\n";

            var statement = $"CREATE TABLE {tableName} (\r\n";

            // ReSharper disable once ForeachCanBeConvertedToQueryUsingAnotherGetEnumerator
            foreach (var colDef in metadatas)
            {
                statement += $"{colDef.ColumnName} {_typeToExprMap[colDef.DataType]} {(colDef.IsIdentity ?? false ? "PRIMARY KEY" : "")}{defDelimiter}";
            }
            statement  = statement.Remove(statement.Length - defDelimiter.Length, defDelimiter.Length);
            statement += ")";

            return(statement);
        }
示例#10
0
        public void Pump(
            TextReader source,
            ColumnMetadataList sourceMetadatas,
            ITargetAdapter <PoolEntry <object[]> > targetAdapter,
            string delimiter     = DelimitedTextSourceAdapter.DefaultDelimiter,
            int rowsPoolCapacity = TextSourceAdapter.DefaultRowsPoolCapacity)
        {
            var sourceAdapter = new DelimitedTextSourceAdapter(source)
            {
                ColumnMetadatas = sourceMetadatas,
                RowsPoolSize    = rowsPoolCapacity,
                Delimiter       = delimiter
            };

            base.Pump(sourceAdapter, targetAdapter);
        }
示例#11
0
        protected override string BuildCreateTableStatement(string tableName, ColumnMetadataList metadatas)
        {
            const string defDelimiter = ",\r\n";

            var statement = $"CREATE TABLE {tableName} (\r\n";

            // ReSharper disable once ForeachCanBeConvertedToQueryUsingAnotherGetEnumerator
            foreach (var colDef in metadatas)
            {
                statement += @$ "{colDef.ColumnName} 
                                {(colDef.DataTypeName != string.Empty && typeof(TSourceAdapter) == typeof(OracleSourceAdapter) 
                                    ? colDef.DataTypeName 
                                    : _typeToExprMap[colDef.DataType])} 
                                {(colDef.DataType == typeof(string) ? " (" + Math.Min(colDef.ColumnSize ?? MaxNVARCHAR2Size, MaxNVARCHAR2Size) + ") " : " ")}
                                {(colDef.DataType == typeof(decimal) ? " (" + colDef.NumericPrecision + ", " + colDef.NumericScale + ") " : " ")}
                                {(colDef.IsIdentity ?? false ? " PRIMARY KEY " : " ")}{defDelimiter}";
            }
示例#12
0
        protected override string BuildCreateTableStatement(string tableName, ColumnMetadataList metadatas)
        {
            const string defDelimiter = ",\r\n";

            var statement = $"CREATE TABLE {tableName} (\r\n";

            // ReSharper disable once ForeachCanBeConvertedToQueryUsingAnotherGetEnumerator
            foreach (var colDef in metadatas)
            {
                statement += @$ "{colDef.ColumnName} 
                                {(colDef.DataTypeName != string.Empty && typeof(TSourceAdapter) == typeof(SqlClientSourceAdapter) ? colDef.DataTypeName : _typeToExprMap[colDef.DataType])} 
                                {((colDef.DataType == typeof(string) || colDef.DataType == typeof(byte[])) && colDef.DataTypeName != " text " 
                                    ? " (" + (colDef.ColumnSize == int.MaxValue 
                                        ? " MAX " 
                                        : colDef.ColumnSize.ToString()) + ") " 
                                    : " ")}
                                {(colDef.DataType == typeof(decimal) ? " (" + colDef.NumericPrecision + ", " + colDef.NumericScale + ") " : " ")}
                                {(colDef.IsIdentity ?? false ? " PRIMARY KEY " : " ")}{defDelimiter}";
            }
示例#13
0
 protected abstract string BuildCreateTableStatement(string tableName, ColumnMetadataList metadatas);
示例#14
0
 private static ISourceAdapter <PoolEntry <object[]> > SourceAdapterBuilder(IEnumerable parameters, ColumnMetadataList metadata)
 {
     return(new BlockingQueueSourceAdapter(parameters)
     {
         ColumnMetadatas = metadata
     });
 }