private string GetRelationTypeName(TableRelation relation) { if (relation is ParentTableRelation) { return("ParentChild"); } else if (relation is ChildTableRelation) { return("ChildParent"); } else if (relation is ManyToManyTableRelation) { return("ManyToMany"); } return(string.Empty); }
public static List <ImportedRelation> ImportRelations(string filePath) { List <ImportedRelation> listRelations = null; listRelations = new List <ImportedRelation>(); string[] lines = File.ReadAllLines(filePath); foreach (string line in lines) { try { //skip comments if (line.StartsWith("//") || line == string.Empty) { continue; } //get all the parts string[] parts = line.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); TableRelation relation = null; string tableName = string.Empty; ParseRelation(parts, ref relation, ref tableName); if (relation != null && (!string.IsNullOrEmpty(tableName))) { listRelations.Add(new ImportedRelation(tableName, relation)); } } catch { continue; } } return(listRelations); }
private void buttonOK_Click(object sender, EventArgs e) { //do some checks try { switch (comboBoxRelationType.SelectedIndex) { case 0: if (CheckParentChildRelation()) { //create the relation ParentTableRelation relation = new ParentTableRelation(); this.relation = relation; relation.CascadeDeleteChildren = checkBoxEnableCascadeDelete.Checked; if (comboBoxParentChildRelationCardinality.SelectedIndex == 0) { relation.RelationCardinality = RelationCardinality.OneToOne; } else { relation.RelationCardinality = RelationCardinality.OneToMany; } relation.RelatedTableName = comboBoxParentChildRelatedTable.SelectedItem.ToString(); relation.ForeignKeyName = comboBoxParentChildFields.SelectedItem.ToString(); DialogResult = DialogResult.OK; this.relation = (TableRelation)relation; Close(); } break; case 1: if (CheckChildParentRelation()) { ChildTableRelation relation = new ChildTableRelation(); relation.RelationCardinality = RelationCardinality.OneToOne; relation.RelatedTableName = comboBoxChildParentRelatedTable.SelectedItem.ToString(); relation.RelatedTableKeyName = comboBoxChildParentPrimaryKeyFields.SelectedItem.ToString(); relation.ForeignKeyName = comboBoxChildParentForeignKeyFields.SelectedItem.ToString(); DialogResult = DialogResult.OK; this.relation = (TableRelation)relation; Close(); } break; case 2: if (CheckManyToManyRelation()) { ManyToManyTableRelation relation = new ManyToManyTableRelation(); relation.RelationCardinality = RelationCardinality.ManyToMany; relation.RelatedTableName = comboBoxManyToManyRelatedTable.SelectedItem.ToString(); relation.IntermediaryTableName = comboBoxManyToManyIntermediaryTable.SelectedItem.ToString(); relation.IntermediaryKeyFieldFromChildTable = comboBoxManyToManyIntermediaryForeignKeyRelatedEntity.SelectedItem.ToString(); relation.IntermediaryKeyFieldFromParentTable = comboBoxManyToManyIntermediaryForeignKeyOurEntity.SelectedItem.ToString(); this.relation = (TableRelation)relation; DialogResult = DialogResult.OK; Close(); } break; } } catch (Exception ex) { MessageBox.Show("Error \n" + ex.Message, "DataBlock Modeler", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
internal static void ParseRelation(string[] parts, ref TableRelation relation, ref string tableName) { try { Dictionary <string, string> entries = new Dictionary <string, string>(); foreach (string part in parts) { try { string[] internalPieces = part.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); if (internalPieces.Length != 2) { continue; } entries.Add(internalPieces[0].Trim().ToLower(), internalPieces[1].Trim()); } catch { continue; } } //check the type string entry; entries.TryGetValue(TYPE, out entry); if (string.IsNullOrEmpty(entry)) { return; } if (entry == CHILD_PARENT) { ChildTableRelation childTableRelation = new ChildTableRelation(); childTableRelation.ForeignKeyName = entries[FOREIGN_KEY_FIELD_NAME]; childTableRelation.RelatedTableName = entries[RELATED_TABLE_NAME]; childTableRelation.RelationCardinality = (RelationCardinality)Enum.Parse(typeof(RelationCardinality), entries[CARDINALITY], true); childTableRelation.RelatedTableKeyName = entries[PRIMARY_KEY_FIELD_NAME]; tableName = entries[TABLE]; relation = childTableRelation; } else if (entry == PARENT_CHILD) { ParentTableRelation parentTableRelation = new ParentTableRelation(); parentTableRelation.RelatedTableName = entries[RELATED_TABLE_NAME]; parentTableRelation.ForeignKeyName = entries[FOREIGN_KEY_FIELD_NAME]; parentTableRelation.RelationCardinality = (RelationCardinality)Enum.Parse(typeof(RelationCardinality), entries[CARDINALITY], true); tableName = entries[TABLE]; parentTableRelation.CascadeDeleteChildren = Convert.ToBoolean(entries[CASCADE_DELETE_CHILDREN]); relation = parentTableRelation; } else if (entry == MANY_TO_MANY) { ManyToManyTableRelation manyTableRelation = new ManyToManyTableRelation(); manyTableRelation.RelatedTableName = entries[RELATED_TABLE_NAME]; manyTableRelation.IntermediaryTableName = entries[INTERMEDIARY_TABLE_NAME]; manyTableRelation.IntermediaryKeyFieldFromParentTable = entries[INTERMEDIARY_KEY_FIELD_FROM_PARENT_TABLE]; manyTableRelation.IntermediaryKeyFieldFromChildTable = entries[INTERMEDIARY_KEY_FIELD_FROM_CHILD_TABLE]; manyTableRelation.RelationCardinality = RelationCardinality.ManyToMany; tableName = entries[TABLE]; relation = manyTableRelation; } } catch { relation = null; } }
public ImportedRelation(string tableName, TableRelation relation) { this.tableName = tableName; this.relation = relation; }
private void AspnetCodeGenerationToolStripMenuItem_Click(object sender, EventArgs e) { AspnetCodeGenerationDialog dialog = null; try { //generate mappings menuItemGenerateMappingFiles_Click(null, null); //is something selected if (listView.CheckedItems.Count != 1) { MessageBox.Show("Please choose a single entity to generate ASP.NET code", "DataBlock Modeler", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (CheckForPrimaryKeys() == false) { return; } dialog = new AspnetCodeGenerationDialog(); if (dialog.ShowDialog() == DialogResult.OK) { string entityName = string.Empty; string tableName = string.Empty; ArrayList alColumns = new ArrayList(); ArrayList alTableRelations = new ArrayList(); TableRelation[] relations = new TableRelation[0]; int current = -1; //get the entity names for (int i = 0; i < listView.Items.Count; i++) { ++current; if (listView.Items[i].Checked) { entityName = Utilies.RemoveEmptySpaces(Context.databaseTables[i].EntityName); tableName = Context.databaseTables[i].TableName; relations = Context.databaseTables[i].Relations; break; } } string currentFilePath = string.Empty; AspnetCodeGenerator c = new AspnetCodeGenerator(); c.Generate(Context.databaseTables[current].Columns, dialog.NamespaceName, entityName, dialog.GenerateContentPlaceholder, Application.StartupPath + @"\Output", relations); MessageBox.Show("Files were successfully generated.", "DataBlock Modeler", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } catch (Exception ex) { MessageBox.Show("Error occurred while generating the files " + "\n" + ex.Message, "DataBlock Modeler", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (dialog != null) { dialog.Dispose(); } } }