예제 #1
0
        public ColumnsWithValue BuildDerivativePk(TableMetadata derivativeTable, object[] sourceRow)
        {
            var colPkSrc = new ColumnsWithValue();
            var colPkDst = new ColumnsWithValue();

            for (var j = 0; j < ColumnsDefinition.Count; j++)
            {
                if (ColumnsDefinition[j].IsPrimary)
                {
                    colPkSrc.Add(ColumnsDefinition[j].Name, sourceRow[j]);
                }
            }

            //FK qui pointent vers la table courante
            foreach (var fk in derivativeTable.ForeignKeys.Where(k => k.TableTo == Name))
            {
                //Toutes les colonnes doivent correspondre
                if (!fk.Columns.Any(c => !colPkSrc.ContainsKey(c.NameTo)))
                {
                    foreach (var col in fk.Columns)
                    {
                        colPkDst.Add(col.NameFrom, colPkSrc[col.NameTo]);
                    }
                    break;
                }
            }
            if (!colPkDst.Any())
            {
                throw new Exception($"A problem append with the metadata. The derivative table '{derivativeTable.Name}' dosen't have a foreign key to the table '{Name}'.");
            }
            return(colPkDst);
        }
예제 #2
0
        private static List <RowIdentifier> ParseClonedRows(Dictionary <string, ExecutionPlan> exucutionPlanByServer)
        {
            var clonedRows = new List <RowIdentifier>();

            foreach (var server in exucutionPlanByServer)
            {
                foreach (var row in server.Value.InsertSteps.Where(s => s.Depth == 0))
                {
                    var pkTemp = row.TableMetadata.BuildPkFromDataRow(row.Datarow);

                    //Clone for new reference
                    var clonedPk = new ColumnsWithValue();
                    foreach (var col in pkTemp)
                    {
                        var sqlVar = col.Value as SqlVariable;
                        clonedPk.Add(col.Key, sqlVar != null ? sqlVar.Value : col.Value);
                    }

                    var riReturn = new RowIdentifier
                    {
                        ServerId = row.DestinationTable.ServerId,
                        Database = row.DestinationTable.Database,
                        Schema   = row.DestinationTable.Schema,
                        Table    = row.DestinationTable.Table,
                        Columns  = clonedPk
                    };

                    //Construit la pk de retour
                    clonedRows.Add(riReturn);
                }
            }

            return(clonedRows);
        }
예제 #3
0
        public ColumnsWithValue BuildKeyFromFkDataRow(ForeignKey fk, object[] row)
        {
            var fkValues = new ColumnsWithValue();

            foreach (var col in fk.Columns)
            {
                var posFkTable = ColumnsDefinition.IndexOf(c => c.Name == col.NameTo);
                fkValues.Add(col.NameFrom, row[posFkTable]);
            }
            return(fkValues);
        }
예제 #4
0
        public ColumnsWithValue BuildKeyFromDerivativeDataRow(ForeignKey fk, object[] row)
        {
            var fkValues = new ColumnsWithValue();

            foreach (var t in fk.Columns)
            {
                var posTblSource = ColumnsDefinition.IndexOf(c => c.Name == t.NameFrom);
                fkValues.Add(t.NameTo, row[posTblSource]);
            }
            return(fkValues);
        }
예제 #5
0
        public ColumnsWithValue BuildPkFromDataRow(object[] row)
        {
            var pk = new ColumnsWithValue();

            for (var i = 0; i < ColumnsDefinition.Count; i++)
            {
                if (ColumnsDefinition[i].IsPrimary)
                {
                    pk.Add(ColumnsDefinition[i].Name, row[i]);
                }
            }
            return(pk);
        }
예제 #6
0
        public ColumnsWithValue BuildPkFromRawKey(object[] key)
        {
            var pkColumns = ColumnsDefinition.Where(c => c.IsPrimary).ToArray();

            if (key.Length != pkColumns.Length)
            {
                throw new Exception("The key doesn't correspond to table defenition.");
            }

            var pk = new ColumnsWithValue();

            for (var i = 0; i < pkColumns.Count(); i++)
            {
                pk.Add(pkColumns[i].Name, key[i]);
            }
            return(pk);
        }
예제 #7
0
        private void BuildCircularReferencesPlan()
        {
            foreach (var job in _circularKeyJobs)
            {
                var baseTable           = Metadatas.GetTable(job.SourceBaseRowStartPoint);
                var fkTable             = Metadatas.GetTable(job.SourceFkRowStartPoint);
                var pkDestinationRow    = _keyRelationships.GetKey(job.SourceBaseRowStartPoint);
                var keyDestinationFkRow = _keyRelationships.GetKey(job.SourceFkRowStartPoint);

                var serverDstBaseTable = ExecutionContext.Map[new SehemaIdentifier
                                                              {
                                                                  ServerId = job.SourceBaseRowStartPoint.ServerId,
                                                                  Database = job.SourceBaseRowStartPoint.Database,
                                                                  Schema = job.SourceBaseRowStartPoint.Schema
                                                              }];

                var serverDstFkTable = ExecutionContext.Map[new SehemaIdentifier
                                                            {
                                                                ServerId = job.SourceFkRowStartPoint.ServerId,
                                                                Database = job.SourceFkRowStartPoint.Database,
                                                                Schema = job.SourceFkRowStartPoint.Schema
                                                            }];

                if (job.ForeignKey.Columns.Count != keyDestinationFkRow.Length)
                {
                    throw new Exception("The foreign key defenition is not matching with the values.");
                }

                var fk = new ColumnsWithValue();
                for (var i = 0; i < job.ForeignKey.Columns.Count; i++)
                {
                    var colName = job.ForeignKey.Columns[i].NameTo;
                    var value   = keyDestinationFkRow[i];

                    fk.Add(colName, value);
                }

                var riBaseDestination = new RowIdentifier
                {
                    ServerId = serverDstBaseTable.ServerId,
                    Database = serverDstBaseTable.Database,
                    Schema   = serverDstBaseTable.Schema,
                    Table    = job.SourceBaseRowStartPoint.Table,
                    Columns  = baseTable.BuildPkFromRawKey(pkDestinationRow)
                };

                var riFkDestination = new RowIdentifier
                {
                    ServerId = serverDstFkTable.ServerId,
                    Database = serverDstFkTable.Database,
                    Schema   = serverDstFkTable.Schema,
                    Table    = job.SourceFkRowStartPoint.Table,
                    Columns  = fk
                };

                var fkDestinationDataRow = GetDataRow(riFkDestination);
                var modifiedFk           = fkTable.BuildKeyFromFkDataRow(job.ForeignKey, fkDestinationDataRow);

                var step = new UpdateStep()
                {
                    StepId           = _nextStepId++,
                    DestinationRow   = riBaseDestination,
                    ForeignKey       = modifiedFk,
                    DestinationTable = riFkDestination
                };

                AddUpdateStep(step);
            }
            _circularKeyJobs.Clear();
        }