public TargetRefColumnSqlModel(TargetReference fk, TargetReferenceColumn col, TargetEntitySqlModel targetSqlModel, ColumnInfo colinfo)
     : base(colinfo)
 {
     _col = col;
     _fk = fk;
     _targetSqlModel = targetSqlModel;
 }
 internal override void CreateLifetimeConditions(DmlfCommandBase cmd, string targetEntityAlias, TargetEntitySqlModel targetEntityModel)
 {
     cmd.AddAndCondition(new DmlfIsNullCondition
     {
         Expr = DmlfColumnRefExpression.Create(targetEntityAlias, ValidToColumn),
     });
 }
 internal override void CreateSetUpdatedUpdateFields(DmlfUpdate cmd, string targetEntityAlias, TargetEntitySqlModel targetEntityModel)
 {
     cmd.Columns.Add(new DmlfUpdateField
     {
         TargetColumn = ValidToColumn,
         Expr = SqlScriptCompiler.ImportDateTimeExpression
     });
 }
 internal override void CreateReliveConditions(DmlfCommandBase cmd, string targetEntityAlias, TargetEntitySqlModel targetEntityModel)
 {
     if (!String.IsNullOrEmpty(DeletedDateColumn))
     {
         cmd.AddAndCondition(new DmlfIsNotNullCondition
         {
             Expr = DmlfColumnRefExpression.Create(targetEntityAlias, DeletedDateColumn),
         });
     }
     if (!String.IsNullOrEmpty(IsDeletedColumn))
     {
         cmd.AddAndCondition(new DmlfEqualCondition
         {
             LeftExpr = DmlfColumnRefExpression.Create(targetEntityAlias, DeletedDateColumn),
             RightExpr = new DmlfLiteralExpression { Value = 1 },
         });
     }
 }
 internal override void CreateReliveUpdateFields(DmlfUpdate cmd, string targetEntityAlias, TargetEntitySqlModel targetEntityModel)
 {
     if (!String.IsNullOrEmpty(DeletedDateColumn))
     {
         cmd.Columns.Add(new DmlfUpdateField
         {
             TargetColumn = DeletedDateColumn,
             Expr = new DmlfLiteralExpression { Value = null }
         });
     }
     if (!String.IsNullOrEmpty(IsDeletedColumn))
     {
         cmd.Columns.Add(new DmlfUpdateField
         {
             TargetColumn = IsDeletedColumn,
             Expr = new DmlfLiteralExpression { Value = 0 }
         });
     }
 }
        public void FindReferences(TargetEntitySqlModel entity, string baseAlias)
        {
            foreach (var fkPair in entity.RefEntities)
            {
                if (!fkPair.Key.IsRestriction) continue;

                string refAlias = "refrestr_" + _queue.Count;

                _queue.Add(new QueueItem
                {
                    Fk = fkPair.Key,
                    BaseEntity = entity,
                    RefEntity = fkPair.Value,
                    BaseAlias = baseAlias,
                    RefAlias = refAlias,
                });

                if (_queue.Any(x => x.BaseEntity == fkPair.Value)) throw new Exception("DBSH-00000 cycle in references");
                FindReferences(fkPair.Value, refAlias);
            }
        }
Exemple #7
0
        public void PutLogMessage(TargetEntitySqlModel entity, LogOperationType operation, string message, string durationName)
        {
            Put("&nset @rows = cast(@@ROWCOUNT as nvarchar);&n");
            if (message == null)
            {
                Put("set @msg = cast(ERROR_MESSAGE() as nvarchar(max))"
                                + " + ' Severity:' + cast(ERROR_SEVERITY() as nvarchar)"
                                + " + ' State:' + cast(ERROR_STATE() as nvarchar)"
                                + " + ' Line:' + cast(ERROR_LINE() as nvarchar)"
                                + " + ' Num:' + cast(ERROR_NUMBER() as nvarchar);&n");
            }
            else
            {
                if (entity != null) message = $"{entity.TargetTable}: {message}";

                if (message.Contains("@rows"))
                {
                    Put($"set @msg = REPLACE('{message}','@rows',@rows);&n");
                }
                else
                {
                    Put($"set @msg = '{message}';&n");
                }
                for (int i = 0; i < 9; i++)
                {
                    string argname = "@arg" + i;
                    if (message.Contains(argname))
                    {
                        Put($"set @msg = REPLACE(@msg,'{argname}',{argname}); &n");
                    }
                }
            }
            if (durationName != null)
            {
                if (!_startedDurations.Contains(durationName))
                    throw new Exception("DBSH-00218 Duration name not defined: " + durationName);
                Put($"set @lastLogDiff = DATEDIFF(second, @lastLogTime_{durationName}, GETDATE());&n");
            }
            else
            {
                Put("set @lastLogDiff = NULL;&n");
            }

            Put($"INSERT INTO @messages (Message, Duration, Operation, TargetEntity, Rows) VALUES (@msg, @lastLogDiff, '{operation}', %v, @rows);&n", entity?.LogName);
            Put("RAISERROR(@msg, 0, 1) WITH NOWAIT; &n");

            //Put("PRINT @msg; &n");
            _datasync.Dbsh.LogHandlers.ForEach(x => x.PutLogMessage(
                Dumper,
                $"'{operation}'",
                entity == null ? "NULL" : $"'{entity.LogName}'",
                "@msg",
                "@lastLogDiff",
                _procName == null ? "''" : $"'{_procName}'",
                "@rows",
                _context));

            //sb.AppendFormat(
            //    "INSERT INTO ImportLog ([Procedure], SourceRowSet, TargetEntity, [RowCount], [Operation], ImportDate, Message, DurationSeconds) "
            //    + "VALUES ('{0}', '{1}', '{2}', @rows, '{3}', @nowDateTime, @msg, @lastLogDiff);\n",
            //    Procedure.Name,
            //    entity == null || entity.Source == null ? "" : entity.Source.Name,
            //    entity == null ? "" : entity.Name,
            //    operation.ToString().ToUpper());
        }
Exemple #8
0
 public void PutEndTryCatch(TargetEntitySqlModel entity, bool useTransaction)
 {
     Put("^end ^try&n");
     Put("^begin ^catch&n");
     Put("&>");
     PutLogMessage(entity, LogOperationType.Error, null, null);
     Put("&<");
     if (useTransaction)
     {
         Put("^if (@useTransaction = 1) begin SELECT @CatchErrorMessage = ERROR_MESSAGE(),@CatchErrorSeverity = ERROR_SEVERITY(), @CatchErrorState = ERROR_STATE();RAISERROR (@CatchErrorMessage, @CatchErrorSeverity, @CatchErrorState);^end;");
     }
     Put("^end ^catch&n");
 }
Exemple #9
0
 public void PutBeginTryCatch(TargetEntitySqlModel entity)
 {
     Put("^begin ^try&n");
     Put("IF NULL = NULL SELECT NULL;&n");
 }
 internal virtual void AddTargetColumns(TargetEntitySqlModel targetEntityModel)
 {
 }
 internal virtual void CreateSetUpdatedUpdateFields(DmlfUpdate cmd, string targetEntityAlias, TargetEntitySqlModel targetEntityModel)
 {
 }
 internal virtual void CreateReliveConditions(DmlfCommandBase cmd, string targetEntityAlias, TargetEntitySqlModel targetEntityModel)
 {
 }
 internal override void CreateSetDeletedUpdateFields(DmlfUpdate cmd, string targetEntityAlias, TargetEntitySqlModel targetEntityModel)
 {
     if (!String.IsNullOrEmpty(DeletedDateColumn))
     {
         cmd.Columns.Add(new DmlfUpdateField
         {
             TargetColumn = DeletedDateColumn,
             Expr = SqlScriptCompiler.ImportDateTimeExpression
         });
     }
     if (!String.IsNullOrEmpty(IsDeletedColumn))
     {
         cmd.Columns.Add(new DmlfUpdateField
         {
             TargetColumn = IsDeletedColumn,
             Expr = new DmlfLiteralExpression { Value = 1 }
         });
     }
 }
Exemple #14
0
        public SourceJoinSqlModel(TargetEntitySqlModel targetEntitySqlModel, SourceGraphSqlModel sourceGraph)
        {
            _targetEntitySqlModel = targetEntitySqlModel;
            _sourceGraph = sourceGraph;

            SourceToRefsJoin = new DmlfFromItem();


            //foreach (var column in _targetEntitySqlModel.RequiredSourceColumns)
            //{
            //    if (Columns.ContainsKey(column.Alias)) continue;

            //    Columns[column.Alias] = new SourceColumnSqlModel
            //    {
            //        Alias = column.Alias,
            //    };

            //    bool addedEntity = false;
            //    foreach (var ent in column.Entities)
            //    {
            //        if (ent.SingleKeyColumn == column.Alias)
            //        {
            //            AddEntity(ent);
            //            addedEntity = true;
            //            break;
            //        }
            //    }
            //    if (addedEntity) continue;

            //    AddEntity(column.Entities.First());
            //}

            if (_sourceGraph == null)
            {
                // flat variant
                PrimarySource = _targetEntitySqlModel.DataSync.FlatSources.FirstOrDefault(x => x.Match(_targetEntitySqlModel.Dbsh.PrimarySource));
                if (PrimarySource == null)
                {
                    throw new IncorrectRdsDefinitionException($"DBSH-00000 Source not found for entity {_targetEntitySqlModel.LogName}");
                }
                SourceToRefsJoin.Source = PrimarySource.QuerySource;
                foreach (var col in PrimarySource.Columns)
                {
                    Columns[col.Alias] = col;
                }
            }
            else
            {
                _entityQueue.AddRange(_sourceGraph.Entities);

                DetectPrimarySource();

                DetectUnusedEntities();

                RebuildEntityList();
                //queue.Add(_targetEntitySqlModel.KeySourceColumns.First().Entities.First());
                //foreach (var ent in _targetEntitySqlModel.RequiredSourceColumns.SelectMany(x => x.Entities))
                //{
                //    if (queue.Contains(ent)) continue;
                //    queue.Add(ent);
                //}

                if (!_entityQueue.Any() && targetEntitySqlModel.Dbsh.Columns.Any(x => x.IsKey))
                {
                    throw new IncorrectRdsDefinitionException($"LGM-00000 None of source entities is used in {_targetEntitySqlModel.LogName} (try to set source column)");
                }

                if (_entityQueue.Any())
                {
                    CreateSourceJoin();
                    AddRefsToJoin();
                }

                foreach (var col in Columns.Values) col.CompileFilter();
            }
        }
        public void AddReference(TargetReference fk, TargetEntitySqlModel targetEntity)
        {
            RefEntities[fk] = targetEntity;

            foreach (var col in fk.Columns)
            {
                TargetColumns.Add(new TargetRefColumnSqlModel(fk, col, targetEntity, FindColumnInfo(col.BaseName)));
            }

            foreach (var col in targetEntity.KeySourceColumns)
            {
                RequiredSourceColumns.Add(col);
                if (fk.IsKey) KeySourceColumns.Add(col);
            }
        }
 internal override void AddTargetColumns(TargetEntitySqlModel targetEntityModel)
 {
     targetEntityModel.TargetColumns.Add(new TargetValidFromColumnSqlModel(ValidFromColumn, targetEntityModel.FindColumnInfo(ValidFromColumn)));
 }