public void removeFkConstraintColumn(ColumnFkFigure column) { Console.WriteLine ("Eliminando FkFigure: " + column.originalTableName + "." + column.originalColumnName); Model.removeFkConstraintColumn (column); this.Remove (column); OnFigureChanged (new FigureEventArgs (this, DisplayBox)); }
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; }
//column.originalTableName,column.originalColumnName public void removeFkConstraintColumn(ColumnFkFigure fkFigure) { ForeignKeyConstraintSchema fkc = null; ColumnSchema colFk=null; //Lookup for constraint containing column to remove from FK int pos = -1; foreach(ConstraintSchema cs in TableSchema.Constraints){ if(cs is ForeignKeyConstraintSchema && (cs as ForeignKeyConstraintSchema).ReferenceTableName==fkFigure.originalTableName){ fkc = (cs as ForeignKeyConstraintSchema); Console.WriteLine("Tengo reference Table: " + fkc.ReferenceTableName); foreach(ColumnSchema col in fkc.ReferenceColumns){ Console.WriteLine("Busco: "+fkFigure.originalTableName+"."+ fkFigure.originalColumnName+" Tengo " +(cs as ForeignKeyConstraintSchema).ReferenceTableName +"."+col.Name); if(col.Name==fkFigure.originalColumnName){ pos = fkc.ReferenceColumns.IndexOf(col); colFk = fkc.Columns[pos]; Console.WriteLine("Eliminando: "+fkFigure.originalTableName+"."+ fkFigure.originalColumnName); } } } } if(pos>=0){ fkc.Columns.RemoveAt(pos); fkc.ReferenceColumns.RemoveAt(pos); TableSchema.Columns.Remove (colFk); this.columns.Remove(fkFigure); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Mande a quitar la columna que era fk: " + fkFigure.Text); foreach(AbstractColumnFigure cf in this.columns){ if(cf is ColumnFkFigure ) Console.WriteLine(" %%%%%%%%%%%%%% REVISO Columns en modelo de la tabla: " + cf.ColumnModel.Name + " o "+ (cf as ColumnFkFigure).originalColumnName ); } } else throw new NotImplementedException (); if(fkc.Columns.Count==0) TableSchema.Constraints.Remove(fkc); System.Console.WriteLine (SchemaProvider.GetTableCreateStatement (tableSchema)); }
//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; }