예제 #1
0
        public IEnumerable <IRow> Read()
        {
            var batches    = _parent.Entities.Select(e => e.BatchId).ToArray();
            var minBatchId = batches.Min();
            var maxBatchId = batches.Max();

            _output.Info("Batch Range: {0} to {1}.", minBatchId, maxBatchId);

            var sql = $"SELECT {string.Join(",", _output.Entity.Fields.Where(f=>f.Output).Select(f => _cf.Enclose(f.Alias)))} FROM {_cf.Enclose(_output.Process.Star)} {(_cf.AdoProvider == AdoProvider.SqlServer ? "WITH (NOLOCK)" : string.Empty)} WHERE {_cf.Enclose(Constants.TflBatchId)} BETWEEN @MinBatchId AND @MaxBatchId;";

            _output.Debug(() => sql);

            using (var cn = _cf.GetConnection()) {
                cn.Open();

                var cmd = cn.CreateCommand();

                cmd.CommandTimeout = 0;
                cmd.CommandType    = CommandType.Text;
                cmd.CommandText    = sql;

                var min = cmd.CreateParameter();
                min.ParameterName = "@MinBatchId";
                min.Value         = minBatchId;
                min.Direction     = ParameterDirection.Input;
                min.DbType        = DbType.Int32;

                var max = cmd.CreateParameter();
                max.ParameterName = "@MaxBatchId";
                max.Value         = maxBatchId;
                max.Direction     = ParameterDirection.Input;
                max.DbType        = DbType.Int32;

                cmd.Parameters.Add(min);
                cmd.Parameters.Add(max);

                var reader     = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
                var rowCount   = 0;
                var fieldArray = _output.Entity.Fields.ToArray();
                while (reader.Read())
                {
                    rowCount++;
                    _output.Increment();
                    yield return(_rowCreator.Create(reader, fieldArray));
                }
                _output.Info("{0} from {1}", rowCount, _output.Connection.Name);
            }
        }
예제 #2
0
        public IEnumerable <IRow> Read(IEnumerable <IRow> input)
        {
            using (var cn = _cf.GetConnection()) {
                cn.Open();
                var trans = cn.BeginTransaction();

                cn.Execute(_create, null, trans);

                var keys = input.Select(r => r.ToExpandoObject(_keys));
                cn.Execute(_insert, keys, trans, 0, System.Data.CommandType.Text);

                using (var reader = cn.ExecuteReader(_query, null, trans, 0, System.Data.CommandType.Text)) {
                    while (reader.Read())
                    {
                        yield return(_rowCreator.Create(reader, _input.InputFields));
                    }
                }

                cn.Execute(_drop, null, trans);
                trans.Commit();
            }
        }
        public IEnumerable <IRow> Read(IEnumerable <IRow> input)
        {
            var results = new List <IRow>();

            using (var cn = _cf.GetConnection()) {
                cn.Open();
                _context.Debug(() => "begin transaction");
                var trans = cn.BeginTransaction();

                try {
                    var createSql = SqlCreateKeysTable(_tempTable);
                    cn.Execute(createSql, null, trans);

                    var keys      = input.Select(r => r.ToExpandoObject(_keys));
                    var insertSql = SqlInsertTemplate(_context, _tempTable, _keys);
                    cn.Execute(insertSql, keys, trans, 0, System.Data.CommandType.Text);

                    using (var reader = cn.ExecuteReader(SqlQuery(), null, trans, 0, System.Data.CommandType.Text)) {
                        while (reader.Read())
                        {
                            var row = _rowCreator.Create(reader, _fields);
                            results.Add(row);
                        }
                    }

                    var sqlDrop = SqlDrop(_tempTable, _cf);
                    cn.Execute(sqlDrop, null, trans);

                    _context.Debug(() => "commit transaction");
                    trans.Commit();
                } catch (Exception ex) {
                    _context.Error(ex.Message);
                    _context.Warn("rollback transaction");
                    trans.Rollback();
                }
            }
            return(results);
        }
예제 #4
0
        public IEnumerable <IRow> Read()
        {
            using (var cn = _cf.GetConnection()) {
                cn.Open();
                var cmd = cn.CreateCommand();

                cmd.CommandTimeout = 0;
                cmd.CommandType    = CommandType.Text;
                cmd.CommandText    = $@"
                    SELECT {string.Join(",", _fields.Select(f => _readFrom == ReadFrom.Output ? _cf.Enclose(f.FieldName()) : _cf.Enclose(f.Name)))} 
                    FROM {_schemaPrefix}{_cf.Enclose(_tableOrView)} {(_connection.Provider == "sqlserver" && _context.Entity.NoLock ? "WITH (NOLOCK)" : string.Empty)}
                    {_filter};";
                _context.Debug(() => cmd.CommandText);

                var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);

                while (reader.Read())
                {
                    _rowCount++;
                    yield return(_rowCreator.Create(reader, _fields));
                }
                _context.Info("{0} from {1}", _rowCount, _connection.Name);
            }
        }
예제 #5
0
        public IEnumerable <IRow> Read()
        {
            using (var cn = _factory.GetConnection()) {
                cn.Open();
                var cmd = cn.CreateCommand();

                if (string.IsNullOrEmpty(_input.Entity.Query))
                {
                    if (_input.Entity.MinVersion == null)
                    {
                        cmd.CommandText = _input.SqlSelectInput(_fields, _factory, _input.ResolveOrder);

                        if (_input.Entity.IsPageRequest())
                        {
                            var start = (_input.Entity.Page * _input.Entity.PageSize) - _input.Entity.PageSize;
                            var end   = start + _input.Entity.PageSize;
                            switch (_factory.AdoProvider)
                            {
                            case AdoProvider.SqlServer:
                                cmd.CommandText = $"WITH p AS ({cmd.CommandText}) SELECT {string.Join(",", _fields.Select(f => _factory.Enclose(f.Name)))} FROM p WHERE TflRow BETWEEN {start + 1} AND {end}";
                                break;

                            case AdoProvider.PostgreSql:
                                cmd.CommandText += $" LIMIT {_input.Entity.PageSize} OFFSET {start}";
                                break;

                            default:
                                cmd.CommandText += $" LIMIT {start},{_input.Entity.PageSize}";
                                break;
                            }
                        }

                        _input.Debug(() => cmd.CommandText);
                    }
                    else
                    {
                        cmd.CommandText = _input.SqlSelectInputWithMinVersion(_fields, _factory, _input.ResolveOrder);
                        _input.Debug(() => cmd.CommandText);

                        var parameter = cmd.CreateParameter();
                        parameter.ParameterName = "@MinVersion";
                        parameter.Direction     = ParameterDirection.Input;
                        parameter.Value         = _input.Entity.MinVersion;
                        cmd.Parameters.Add(parameter);
                    }

                    if (_input.Entity.IsPageRequest())
                    {
                        var sql = $"SELECT COUNT(*) FROM {_input.SqlInputName(_factory)} {(_factory.AdoProvider == AdoProvider.SqlServer ? "WITH (NOLOCK)" : string.Empty)} {(_input.Entity.Filter.Any() ? " WHERE " + _input.ResolveFilter(_factory) : string.Empty)}";
                        _input.Debug(() => sql);
                        _input.Entity.Hits = cn.ExecuteScalar <int>(sql);
                    }
                    _input.Entity.Query = cmd.CommandText;
                }
                else
                {
                    cmd.CommandText = _input.Entity.Query;
                }

                cmd.CommandType    = CommandType.Text;
                cmd.CommandTimeout = 0;

                using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)) {
                    // transform types if sqlite
                    if (_factory.AdoProvider == AdoProvider.SqLite)
                    {
                        while (reader.Read())
                        {
                            _rowCount++;
                            _input.Increment();
                            var row = _rowCreator.Create(reader, _fields);
                            _typeTransform.Transform(row);
                            yield return(row);
                        }
                    }
                    else
                    {
                        // check the first one's types
                        if (reader.Read())
                        {
                            var row = _rowCreator.Create(reader, _fields);
                            foreach (var field in _fields)
                            {
                                var expected = Constants.TypeSystem()[field.Type];
                                var actual   = row[field] == null ? expected : row[field].GetType();
                                if (expected != actual)
                                {
                                    _input.Warn($"The {field.Alias} field in {_input.Entity.Alias} expects a {expected}, but is reading a {actual}.");
                                }
                            }
                            _rowCount++;
                            _input.Increment();
                            yield return(row);
                        }

                        // just read
                        while (reader.Read())
                        {
                            _rowCount++;
                            _input.Increment();
                            yield return(_rowCreator.Create(reader, _fields));
                        }
                    }
                }

                _input.Info("{0} from {1}", _rowCount, _input.Connection.Name);
            }
        }