public void UpdateOptionalityFk(TableModel sourceTable, kindOptionality optionality)
 {
     Model.UpdateOptionalityFk (sourceTable, optionality);
 }
        public void RefreshRelationships(bool refresh, bool changeConnection, TableFigure notifier, kindOptionality optionality, bool changeDatatype, ColumnSchema notifierColumn)
        {
            if (refresh && changeConnection)
                throw new NotImplementedException ();

            if (NotifyChanged != null) {
                NotifyChanged (refresh, changeConnection, notifier, optionality, changeDatatype, notifierColumn);
            }
        }
 //This functions works as dispatcher to correct one functions
 //TODO: improve this implementation of data interchange to propagate changes
 public void Update(bool refresh, bool changeConnection, TableFigure notifier, kindOptionality optionality, bool changeDatatype, ColumnSchema notifierColumn)
 {
     if (refresh) {
         Console.WriteLine ("La tabla " + this.Model.Name + " ya fue Avisada de REFRESCAR!!!!!!!!!!!!!!!!!!!!!!!!!!! de la tabla:" + notifier.Model.Name);
         refreshForeignKeys (notifier, optionality);
     } else if (changeConnection)
         Console.WriteLine ("La tabla " + this.Model.Name + " ya fue Avisada de CAMBIO DE CONEXION!!!!!!!!!!!!!!!!!!!!!!!!!!! de la tabla:" + notifier.Model.Name); else if (changeDatatype) {
         changeFkColumnDatatype (notifierColumn);
         Console.WriteLine ("La tabla " + this.Model.Name + " ya fue Avisada de Cambio de tipo de dato en columna !!!!!!!!!!!!!!!!!!!!!!!!!!!" + notifierColumn.Name);
     } else if (!changeConnection && !refresh && !changeDatatype) {
         Console.WriteLine ("La tabla " + this.Model.Name + " ya fue Avisada de ELIMINAR FK !!!!!!!!!!!!!!!!!!!!!!!!!!! de la tabla:" + notifier.Model.Name);
         removeAllForeignKeysFrom(notifier);
     }
 }
 public void AddFkConstraintColumn(ColumnSchema sourceCol, kindOptionality optionality)
 {
     AbstractColumnFigure tmp = Model.addFkConstraintColumn (sourceCol, optionality);
     this.Add (tmp);
     OnFigureChanged (new FigureEventArgs (this, DisplayBox));
 }
        public void refreshForeignKeys(TableFigure sourceFk, kindOptionality optionality)
        {
            PrimaryKeyConstraintSchema fkConsColumns = null;
            //Lookup for pk at table level at reference table
            foreach (ConstraintSchema cs in sourceFk.Model.TableSchema.Constraints) {
                if (cs is PrimaryKeyConstraintSchema) {
                    fkConsColumns = cs as PrimaryKeyConstraintSchema;
                    break;
                }
            }

            //Lookup for pk at column level at reference table
            if (fkConsColumns == null) {
                foreach (ColumnSchema col in sourceFk.Model.TableSchema.Columns) {
                    fkConsColumns = col.Constraints.GetConstraint (ConstraintType.PrimaryKey) as PrimaryKeyConstraintSchema;
                    if (fkConsColumns != null)
                        break;
                }
            }

            //Add new fk(s) column to table
            if (fkConsColumns != null) {
                foreach (ColumnSchema colfk in fkConsColumns.Columns) {
                    bool exists = false;
                    Console.WriteLine ("comienzo a buscar :" + colfk.Parent.Name + "." + colfk.Name);
                    foreach (AbstractColumnFigure cf in Model.columns) {
                        if (cf is ColumnFkFigure) {
                            ColumnFkFigure cfk = cf as ColumnFkFigure;
                            Console.WriteLine ("\t\tNO JODA 666 COMPARO:" + cfk.originalTableName + "." + cfk.originalColumnName + " CON " + colfk.Parent.Name + "." + colfk.Name);
                            if (cfk.sameForeignKey (colfk.Parent.Name, colfk.Name)) {
                                exists = true;
                                Console.WriteLine (" \t\t!!!!!!!!! MATCHES: " + colfk.Name);
                            }
                        }
                    }
                    if (!exists) {
                        this.AddFkConstraintColumn (colfk, optionality);
                    }
                }
            }

            //Remove not existing fk(s) to table
            bool removeFk = true;
            ColumnFkFigure removeCfk = null;
            ColumnFkFigure colFigFk = null;
            foreach (AbstractColumnFigure cf in Model.columns) {
                if (cf is ColumnFkFigure) {
                    Console.WriteLine ("Busco si elimino a: " + cf.ColumnModel.Name);
                    colFigFk = cf as ColumnFkFigure;
                    removeFk = true;
                    if (fkConsColumns != null) {
                        foreach (ColumnSchema colfk in fkConsColumns.Columns) {
                            Console.WriteLine ("\t\tComparo con: " + colfk.Name);
                            if (colFigFk.sameForeignKey (colfk.Parent.Name, colfk.Name)) {
                                removeFk = false;
                                Console.WriteLine ("\t\t\tNo la debo remover tiene columna en el fkconstraint " + colfk.Name);
                            }
                        }
                    }
                    if (removeFk) {
                        removeCfk = colFigFk;
                        Console.WriteLine ("PORQ ENTRO AQUI CON: " + colFigFk.originalColumnName + "   " + colFigFk.ColumnModel.Name);
                    }
                }
            }

            if (removeCfk != null) {
                Console.WriteLine ("Mando a eliminar " + removeCfk.originalTableName + "." + removeCfk.originalColumnName);
                this.removeFkConstraintColumn (removeCfk);
            }
        }
 //add foreign key to this table figure
 public void addFkConstraint(RelationshipFigure r, kindOptionality optionality)
 {
     //TODO: implement more than one fk between same tables [multiple times same fk column is used].
     List<ColumnFkFigure> tmp = Model.addFkConstraint (r.StartTable.Model, optionality);
     foreach (ColumnFkFigure c in tmp) {
         this.Add (c);
     }
     OnFigureChanged (new FigureEventArgs (this, DisplayBox));
 }
 private void UpdateOptionalityFk(TableModel sourceTable, kindOptionality optionality)
 {
     if(figEnd!=null){
         figEnd.UpdateOptionalityFk(sourceTable, optionality);
     }
 }
 public void UpdateOptionalityFk(TableModel sourceTable, kindOptionality optionality)
 {
     foreach(ConstraintSchema cs in TableSchema.Constraints){
         if(cs is ForeignKeyConstraintSchema && (cs as ForeignKeyConstraintSchema).ReferenceTableName==sourceTable.Name){
             ForeignKeyConstraintSchema fkc = (cs as ForeignKeyConstraintSchema);
             foreach(ColumnSchema fkCol in fkc.Columns){
                 if(optionality==kindOptionality.optional)
                     fkCol.IsNullable=false;
                 else
                     fkCol.IsNullable=true;
             }
         }
     }
 }
 public AbstractColumnFigure addFkConstraintColumn(ColumnSchema sourceCol, kindOptionality optionality)
 {
     if(sourceCol.Parent is TableSchema){
         ForeignKeyConstraintSchema fkc = null;
         //Add this column to a constraint or create a new one
         foreach(ConstraintSchema cs in TableSchema.Constraints){
             if(cs is ForeignKeyConstraintSchema && (cs as ForeignKeyConstraintSchema).ReferenceTableName==(sourceCol.Parent as TableSchema).Name){
                 fkc = (cs as ForeignKeyConstraintSchema);
             }
         }
         if(fkc==null){
            	fkc = new ForeignKeyConstraintSchema ((sourceCol.Parent as TableSchema).SchemaProvider);
             fkc.ReferenceTableName = (sourceCol.Parent as TableSchema).Name;
             fkc.ReferenceTable = (sourceCol.Parent as TableSchema);
             fkc.Name = (sourceCol.Parent as TableSchema).Name + "_" + TableSchema.Name + "_fk";
             TableSchema.Constraints.Add(fkc);
         }
         ColumnSchema fkCol = new ColumnSchema(sourceCol);
         if(optionality==kindOptionality.optional)
             fkCol.IsNullable=false;
         else
             fkCol.IsNullable=true;
         //Remove column level pk if any
             ConstraintSchema tmp=null;
             foreach(ConstraintSchema cs in fkCol.Constraints)
                 if(cs is PrimaryKeyConstraintSchema)
                     tmp=cs;
             if(tmp!=null)
                 fkCol.Constraints.Remove(tmp);
         fkCol.Name = fkCol.Name + "_" + (sourceCol.Parent as TableSchema).Name + "_fk"; //TODO: should be checked that this name doesn't exists at table yet
         fkCol.Parent = TableSchema;
         fkc.Columns.Add (fkCol);
         TableSchema.Columns.Add (fkCol);
         fkc.ReferenceColumns.Add (sourceCol);
         ColumnFkFigure fk = new ColumnFkFigure (fkCol, FigureOwner, sourceCol.Name, (sourceCol.Parent as TableSchema).Name);
         this.columns.Add (fk);
         return fk;
     }
     return null;
 }
 //TODO: change for IEnumerable?
 public List<ColumnFkFigure> addFkConstraint(TableModel source, kindOptionality optionality)
 {
     List<ColumnFkFigure> items = new List<ColumnFkFigure> ();
     ForeignKeyConstraintSchema fkc = new ForeignKeyConstraintSchema (source.TableSchema.SchemaProvider);
     fkc.ReferenceTableName = source.Name;
     fkc.ReferenceTable = source.TableSchema;
     fkc.Name = source.Name + "_" + TableSchema.Name + "_fk";
     foreach (ColumnFigure col in source.columns) {
         if (col.PrimaryKey) {
             ColumnSchema fkCol = new ColumnSchema (col.ColumnModel);
             //Remove column level pk if any
             ConstraintSchema tmp=null;
             foreach(ConstraintSchema cs in fkCol.Constraints)
                 if(cs is PrimaryKeyConstraintSchema)
                     tmp=cs;
             if(tmp!=null)
                 fkCol.Constraints.Remove(tmp);
             //TODO: create a function that just get three letters from table name using a standard
             fkCol.Name = fkCol.Name + "_" + (col.ColumnModel.Parent as TableSchema).Name + "_fk"; //TODO: should be checked that this name doesn't exists at table yet
             fkCol.Parent = TableSchema;
             if(optionality==kindOptionality.optional)
                 fkCol.IsNullable=false;
             else
                 fkCol.IsNullable=true;
             fkc.Columns.Add (fkCol);
             TableSchema.Columns.Add (fkCol);
             fkc.ReferenceColumns.Add (col.ColumnModel);
             Console.WriteLine("NO JODA 555 666 CREE Fk figure con:" + " Tabla: "+(col.ColumnModel.Parent as TableSchema).Name +"." +col.ColumnModel.Name);
             ColumnFkFigure fk = new ColumnFkFigure (fkCol, FigureOwner, col.ColumnModel.Name , (col.ColumnModel.Parent as TableSchema).Name);
             this.columns.Add (fk);
             items.Add (fk);
         }
     }
     if(fkc.Columns.Count > 0){
         TableSchema.Constraints.Add (fkc);
         return items;
     }else
         return null;
 }