Exemple #1
0
 private void BuildRefConstraints()
 {
     foreach (var table in _dbModel.Tables)
     {
         if (table.Kind != EntityKind.Table)
         {
             continue;
         }
         var entity = table.Entity;
         foreach (var member in entity.Members)
         {
             if (member.Kind != MemberKind.EntityRef)
             {
                 continue;
             }
             var refInfo = member.ReferenceInfo;
             if (!IsActive(refInfo.ToKey.Entity.Area))
             {
                 continue; //target entity/table is not in this db model; so no foreign key link
             }
             var fromDbKey     = _dbModel.LookupDbObject <DbKeyInfo>(refInfo.FromKey);
             var toDbKey       = _dbModel.LookupDbObject <DbKeyInfo>(refInfo.ToKey);
             var cascadeDelete = member.Flags.IsSet(EntityMemberFlags.CascadeDelete);
             var dbRC          = new DbRefConstraintInfo(_dbModel, fromDbKey, toDbKey, cascadeDelete, refInfo);
             table.RefConstraints.Add(dbRC);
         }
     }//foreach entityInfo
     CheckErrors();
 }
Exemple #2
0
 //                
 public virtual void BuildRefConstraintDropSql(DbObjectChange change, DbRefConstraintInfo dbRefConstraint)
 {
     BuildTableConstraintDropSql(change, dbRefConstraint.FromKey);
 }
Exemple #3
0
 private void BuildRefConstraints()
 {
     foreach (var table in _dbModel.Tables) {
     if (table.Kind != EntityKind.Table)
       continue;
     var entity = table.Entity;
     foreach (var member in entity.Members) {
       if (member.Kind != MemberKind.EntityRef)
     continue;
       var refInfo = member.ReferenceInfo;
       if (!IsActive(refInfo.ToKey.Entity.Area))
     continue; //target entity/table is not in this db model; so no foreign key link
       var fromDbKey = _dbModel.LookupDbObject<DbKeyInfo>(refInfo.FromKey);
       var toDbKey = _dbModel.LookupDbObject<DbKeyInfo>(refInfo.ToKey);
       var cascadeDelete = member.Flags.IsSet(EntityMemberFlags.CascadeDelete);
       var dbRC = new DbRefConstraintInfo(_dbModel, fromDbKey, toDbKey, cascadeDelete, refInfo);
       table.RefConstraints.Add(dbRC);
     }
       }//foreach entityInfo
       CheckErrors();
 }
 //not supported
 public override void BuildRefConstraintDropSql(DbObjectChange change, DbRefConstraintInfo dbRefConstraint)
 {
 }
Exemple #5
0
 public virtual void BuildRefConstraintAddSql(DbObjectChange change, DbRefConstraintInfo refConstraint)
 {
     const string sqlTemplate = "ALTER TABLE {0} ADD CONSTRAINT \"{1}\" FOREIGN KEY ({2}) REFERENCES {3} ({4}) {5};";
       var srcTable = refConstraint.FromKey.Table;
       var targetTable = refConstraint.ToKey.Table;
       var fullSrcTableRef = srcTable.FullName;
       var fullTargetTableRef = targetTable.FullName;
       var srcCols = refConstraint.FromKey.KeyColumns.GetSqlNameList();
       var targetCols = refConstraint.ToKey.KeyColumns.GetSqlNameList();
       bool cascade = refConstraint.OwnerReference.FromMember.Flags.IsSet(EntityMemberFlags.CascadeDelete);
       var onDeleteClause = cascade ? "ON DELETE CASCADE" : string.Empty;
       change.AddScript(DbScriptType.RefConstraintAdd, sqlTemplate, fullSrcTableRef, refConstraint.FromKey.Name, srcCols, fullTargetTableRef, targetCols, onDeleteClause);
 }
 public override void BuildRefConstraintAddSql(DbObjectChange change, DbRefConstraintInfo refConstraint)
 {
 }
Exemple #7
0
        protected virtual void LoadReferentialConstraints()
        {
            var data = GetReferentialConstraintsExt();
              foreach (DataRow row in data.Rows) {
            //Load names for Foreign key and Unique key
            var fkSchema = row.GetAsString("CONSTRAINT_SCHEMA");
            var toSchema = row.GetAsString("UNIQUE_CONSTRAINT_SCHEMA");
            if (!IncludeSchema(toSchema) || !IncludeSchema(fkSchema))
              continue;
            var fkTableName = row.GetAsString("C_TABLE");
            var fkName = row.GetAsString("CONSTRAINT_NAME");
            var fromKey = FindKey(fkSchema, fkTableName, fkName);
            Util.Check(fromKey != null, "Key {0} for table {1} not found.", fkName, fkTableName);

            var toTableName = row.GetAsString("U_TABLE");
            var toTable = Model.GetTable(toSchema, toTableName);
            Util.Check(toTable != null, "Target table {0}.{1} not found in DB Model.", toSchema, toTableName);
            var toKey = toTable.PrimaryKey;
            /*
            if (toKey == null) {
              //catch special case - foreign key to non-key set of fields
              if (fromKey != null && fromKey.KeyType.IsSet(KeyType.ForeignKey))
            fromKey.NotSupported = true;
              continue; //it is not ref constraint
            }
             */
            bool cascadeDelete = row.GetAsString("DELETE_RULE") == "CASCADE";
            var refConstraint = new DbRefConstraintInfo(Model, fromKey, toKey, cascadeDelete);
            fromKey.Table.RefConstraints.Add(refConstraint);
              }
        }
Exemple #8
0
 private bool RefConstraintKeysChanged(DbRefConstraintInfo constraint)
 {
     return _changedKeys.Contains(constraint.FromKey) || _changedKeys.Contains(constraint.ToKey);
 }
Exemple #9
0
 private DbRefConstraintInfo FindOldRefConstraint(DbTableInfo oldTable, DbRefConstraintInfo newRefConstraint)
 {
     var newFrom = newRefConstraint.FromKey;
       var newTo = newRefConstraint.ToKey;
       foreach (var oldRc in oldTable.RefConstraints)
     if (oldRc.FromKey.Peer == newFrom && oldRc.ToKey.Peer == newTo && oldRc.CascadeDelete == newRefConstraint.CascadeDelete)
       return oldRc;
       return null;
 }
Exemple #10
0
 public override void BuildRefConstraintDropSql(DbObjectChange change, DbRefConstraintInfo dbRefConstraint)
 {
     var fromKey = dbRefConstraint.FromKey;
       change.AddScript(DbScriptType.RefConstraintDrop, "ALTER TABLE {0} DROP FOREIGN KEY {1};", fromKey.Table.FullName, fromKey.Name);
 }