Beispiel #1
0
        public override IEnumerable <Row> Execute(IEnumerable <Row> rows)
        {
            if (_entity.IsMaster())
            {
                new MasterEntityIndexBuilder(_process).Create();
            }

            //escape 1
            if (_entity.IsMaster() || (!_entity.HasForeignKeys() && _process.IsFirstRun))
            {
                return(rows);
            }

            //escape 2
            var entityChanged = _entity.Inserts + _entity.Updates > 0;
            var masterChanged = _process.MasterEntity.Inserts + _process.MasterEntity.Updates > 0;

            if (!entityChanged && !masterChanged)
            {
                return(rows);
            }

            var master = _process.OutputConnection.Enclose(_process.MasterEntity.OutputName());
            var entity = _process.OutputConnection.Enclose(_entity.OutputName());

            using (var cn = _process.OutputConnection.GetConnection()) {
                cn.Open();

                int records;
                string where;
                if (entityChanged && masterChanged)
                {
                    where = string.Format("WHERE {0}.TflBatchId = @TflBatchId OR {1}.TflBatchId = @MasterTflBatchId;", entity, master);
                    var sql = PrepareSql() + where;
                    Debug(sql);
                    records = cn.Execute(sql, new { _entity.TflBatchId, MasterTflBatchId = _process.MasterEntity.TflBatchId }, commandTimeout: 0);
                }
                else
                {
                    where = string.Format("WHERE {0}.TflBatchId = @TflBatchId;", entityChanged ? entity : master);
                    var sql = PrepareSql() + where;
                    Debug(sql);
                    records = cn.Execute(sql, new { TflBatchId = entityChanged ? _entity.TflBatchId : _process.MasterEntity.TflBatchId }, commandTimeout: 0);
                }
                Logger.EntityDebug(EntityName, "TflBatchId = {0}.", _entity.TflBatchId);
                Logger.EntityInfo(EntityName, "Processed {0} rows. Updated {1} with {2}.", records, _process.MasterEntity.Alias, _entity.Alias);
            }
            return(rows);
        }
 public SqlEntityDelete(AbstractConnection connection, Entity entity)
     : base(connection) {
     _name = Connection.Enclose(entity.OutputName());
     _isMaster = entity.IsMaster();
     BatchSize = connection.BatchSize;
     UseTransaction = true;
 }
 public SqlEntityKeysExtractAllFromOutput(AbstractConnection connection, Entity entity)
     : base(connection) {
     _entity = entity;
     EntityName = entity.Name;
     _keys = _entity.Delete && _entity.IsMaster() ?
         new List<string>(entity.PrimaryKey.Aliases()) { "TflDeleted", "TflKey" } :
         new List<string>(entity.PrimaryKey.Aliases()) { "TflKey" };
 }
 public SqlEntityDelete(AbstractConnection connection, Entity entity)
     : base(connection)
 {
     _name          = Connection.Enclose(entity.OutputName());
     _isMaster      = entity.IsMaster();
     BatchSize      = connection.BatchSize;
     UseTransaction = true;
 }
        public override void Create(AbstractConnection connection, Process process, Entity entity)
        {
            if (EntityExists != null && EntityExists.Exists(connection, entity))
            {
                process.Logger.EntityWarn(entity.Name, "Trying to create entity that already exists! {0}", entity.Name);
                return;
            }

            var keyType = entity.IsMaster() ? FieldType.MasterKey : FieldType.PrimaryKey;

            var writer = process.StarEnabled && keyType == FieldType.MasterKey ?
                         new FieldSqlWriter(entity.Fields, entity.CalculatedFields, process.CalculatedFields, GetRelationshipFields(process.Relationships, entity)) :
                         new FieldSqlWriter(entity.Fields, entity.CalculatedFields);

            var primaryKey = writer.FieldType(keyType).Alias(connection.L, connection.R).Asc().Values();
            var defs       = new List <string>();

            defs.AddRange(writer
                          .Reload()
                          .AddBatchId(entity.Index)
                          .AddDeleted(entity)
                          .AddSurrogateKey(entity.Index)
                          .Output()
                          .Alias(connection.L, connection.R)
                          .DataType(new SqlServerDataTypeService())
                          .AppendIf(" NOT NULL", keyType)
                          .Values());

            var rowVersion = entity.Fields.WithSimpleType("rowversion").WithoutInput().WithoutOutput();

            if (rowVersion.Any())
            {
                var alias = rowVersion.First().Alias;
                defs.Add(connection.Enclose(alias) + " [ROWVERSION] NOT NULL");
            }

            var createSql = connection.TableQueryWriter.CreateTable(entity.OutputName(), defs);

            _logger.EntityDebug(entity.Name, createSql);

            var indexSql = connection.TableQueryWriter.AddUniqueClusteredIndex(entity.OutputName());

            _logger.EntityDebug(entity.Name, indexSql);

            var keySql = connection.TableQueryWriter.AddPrimaryKey(entity.OutputName(), primaryKey);

            _logger.EntityDebug(entity.Name, keySql);

            using (var cn = connection.GetConnection()) {
                cn.Open();
                cn.Execute(createSql);
                cn.Execute(indexSql);
                cn.Execute(keySql);
                _logger.EntityInfo(entity.Name, "Initialized {0} in {1} on {2}.", entity.OutputName(), connection.Database, connection.Server);
            }
        }
        public override void Create(AbstractConnection connection, Process process, Entity entity) {

            if (EntityExists != null && EntityExists.Exists(connection, entity)) {
                process.Logger.EntityWarn(entity.Name,"Trying to create entity that already exists! {0}", entity.Name);
                return;
            }

            var keyType = entity.IsMaster() ? FieldType.MasterKey : FieldType.PrimaryKey;

            var writer = process.StarEnabled && keyType == FieldType.MasterKey ?
                new FieldSqlWriter(entity.Fields, entity.CalculatedFields, process.CalculatedFields, GetRelationshipFields(process.Relationships, entity)) :
                new FieldSqlWriter(entity.Fields, entity.CalculatedFields);

            var primaryKey = writer.FieldType(keyType).Alias(connection.L, connection.R).Asc().Values();
            var defs = new List<string>();
            defs.AddRange(writer
                .Reload()
                .AddBatchId(entity.Index)
                .AddDeleted(entity)
                .AddSurrogateKey(entity.Index)
                .Output()
                .Alias(connection.L, connection.R)
                .DataType(new SqlServerDataTypeService())
                .AppendIf(" NOT NULL", keyType)
                .Values());

            var rowVersion = entity.Fields.WithSimpleType("rowversion").WithoutInput().WithoutOutput();
            if (rowVersion.Any()) {
                var alias = rowVersion.First().Alias;
                defs.Add(connection.Enclose(alias) + " [ROWVERSION] NOT NULL");
            }

            var createSql = connection.TableQueryWriter.CreateTable(entity.OutputName(), defs);
            _logger.EntityDebug(entity.Name, createSql);

            var indexSql = connection.TableQueryWriter.AddUniqueClusteredIndex(entity.OutputName());
            _logger.EntityDebug(entity.Name, indexSql);

            var keySql = connection.TableQueryWriter.AddPrimaryKey(entity.OutputName(), primaryKey);
            _logger.EntityDebug(entity.Name, keySql);

            using (var cn = connection.GetConnection()) {
                cn.Open();
                cn.Execute(createSql);
                cn.Execute(indexSql);
                cn.Execute(keySql);
                _logger.EntityInfo(entity.Name, "Initialized {0} in {1} on {2}.", entity.OutputName(), connection.Database, connection.Server);
            }
        }
Beispiel #7
0
 public SqlEntityKeysExtractAllFromOutput(AbstractConnection connection, Entity entity)
     : base(connection)
 {
     _entity    = entity;
     EntityName = entity.Name;
     _keys      = _entity.Delete && _entity.IsMaster() ?
                  new List <string>(entity.PrimaryKey.Aliases())
     {
         "TflDeleted", "TflKey"
     } :
     new List <string>(entity.PrimaryKey.Aliases())
     {
         "TflKey"
     };
 }
        protected override void PrepareSchema()
        {
            NotifyBatchSize = (int)LogRows;
            BatchSize       = _batchSize;

            var fromFields = new Fields(_entity.Fields, _entity.CalculatedFields).WithOutput().AddBatchId(_entity.Index, false);

            if (_entity.IsMaster() && _entity.Delete)
            {
                fromFields.AddDeleted(_entity.Index, false);
            }

            foreach (var field in fromFields)
            {
                Schema[field.Alias] = field.SystemType;
            }

            var toFields = new SqlServerEntityAutoFieldReader().Read(Connection, _entity.ProcessName, _entity.Prefix, _entity.OutputName(), string.Empty, _entity.IsMaster());

            foreach (var from in fromFields)
            {
                if (toFields.HaveField(from.Alias))
                {
                    var to = toFields.Find(from.Alias).First();
                    if (!to.SimpleType.Equals(from.SimpleType))
                    {
                        if (!to.SimpleType.Equals("byte[]") && from.SimpleType.Equals("rowversion"))
                        {
                            throw new TransformalizeException(Logger, EntityName, "{0} has a matching {1} fields, but different types: {2} != {3}.", TargetTable, from.Alias, from.SimpleType, to.SimpleType);
                        }
                    }
                }
                else
                {
                    throw new TransformalizeException(Logger, EntityName, "{0} does not have a matching {1} field.", TargetTable, from.Alias);
                }
            }
        }
Beispiel #9
0
 public Fields Read(Entity entity, bool isMaster)
 {
     return(Read(entity.Input.First().Connection, entity.ProcessName, entity.Prefix, entity.Name, entity.Schema, entity.IsMaster()));
 }