Datei: ClassGenerator/ApplicationController.cs
Last Commit (50beab8)
| 1 | // |
| 2 | // Copyright (c) 2002-2016 Mirko Matytschak |
| 3 | // (www.netdataobjects.de) |
| 4 | // |
| 5 | // Author: Mirko Matytschak |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 11 | // conditions: |
| 12 | |
| 13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 14 | // of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | // DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | |
| 23 | using System; |
| 24 | using System.Data; |
| 25 | using System.Text; |
| 26 | using System.IO; |
| 27 | using System.Diagnostics; |
| 28 | using System.Collections; |
| 29 | using System.Collections.Generic; |
| 30 | using System.Drawing; |
| 31 | using System.Windows.Forms; |
| 32 | using ClassGenerator.ForeignKeyWizard; |
| 33 | using ClassGenerator.AssemblyWizard; |
| 34 | using ClassGenerator.IntermediateTableWizard; |
| 35 | using ClassGenerator.IntermediateClassWizard; |
| 36 | using ClassGenerator.AddPrimaryKeyWizard; |
| 37 | using WizardBase; |
| 38 | using Generator; |
| 39 | using NDO; |
| 40 | using NDOInterfaces; |
| 41 | |
| 42 | namespace ClassGenerator |
| 43 | { |
| 44 | ····/// <summary> |
| 45 | ····/// Zusammenfassung für Controller. |
| 46 | ····/// </summary> |
| 47 | #if DEBUG |
| 48 | ····public class ApplicationController |
| 49 | #else |
| 50 | ····internal class ApplicationController |
| 51 | #endif |
| 52 | ····{ |
| 53 | ········static ApplicationController instance = new ApplicationController(); |
| 54 | ········public static ApplicationController Instance |
| 55 | ········{ |
| 56 | ············get { return instance; } |
| 57 | ········} |
| 58 | |
| 59 | ········static WizardControllerFactory wizardControllerFactory = new WizardControllerFactory(typeof(ApplicationController).Assembly, ""); |
| 60 | ········public static WizardControllerFactory WizardControllerFactory |
| 61 | ········{ |
| 62 | ············get { return wizardControllerFactory; } |
| 63 | ············set { wizardControllerFactory = value; } |
| 64 | ········} |
| 65 | |
| 66 | ········DatabaseNode databaseNode; |
| 67 | ········public DatabaseNode DatabaseNode |
| 68 | ········{ |
| 69 | ············get { return databaseNode; } |
| 70 | ············set { databaseNode = value; } |
| 71 | ········} |
| 72 | ········AssemblyNode assemblyNode; |
| 73 | ········public AssemblyNode AssemblyNode |
| 74 | ········{ |
| 75 | ············get { return assemblyNode; } |
| 76 | ············set { assemblyNode = value; } |
| 77 | ········} |
| 78 | |
| 79 | ········public void MappingChanged() |
| 80 | ········{ |
| 81 | ············this.assemblyNode.Refresh(); |
| 82 | ········} |
| 83 | |
| 84 | ········string MakeCamelCase( string input ) |
| 85 | ········{ |
| 86 | ············string prefix = this.assemblyNode.Assembly.TargetLanguage == TargetLanguage.VB ? "_" : string.Empty; |
| 87 | ············return prefix + input.Substring(0,1).ToLower() + input.Substring(1); |
| 88 | ········} |
| 89 | |
| 90 | ········string MakePascalCase(string input) |
| 91 | ········{ |
| 92 | ············return input.Substring(0, 1).ToUpper() + input.Substring(1); |
| 93 | ········} |
| 94 | |
| 95 | ········public void MakeForeignKey( DataRelation dataRelation ) |
| 96 | ········{ |
| 97 | ············// The FkRelation is owned by the child table, because it holds |
| 98 | ············// the foreign key. |
| 99 | ············// Therefore the XPath has to be stored in the ForeignFkRelation. |
| 100 | ············string xpath = (string)dataRelation.ExtendedProperties["xpath"]; |
| 101 | |
| 102 | ············TableNode ownTableNode = this.databaseNode[dataRelation.ChildTable.TableName]; |
| 103 | ············TableNode foreignTableNode = this.databaseNode[dataRelation.ParentTable.TableName]; |
| 104 | ············ColumnNode cn = ownTableNode[dataRelation.ChildColumns[0].ColumnName]; |
| 105 | ············FkRelation fkRelation = new FkRelation(cn.Text); |
| 106 | ············RelationNode relationNode = new RelationNode(fkRelation, cn.Parent); |
| 107 | ············ForeignKeyWizModel model = new ForeignKeyWizModel(relationNode, null); |
| 108 | ············fkRelation.CodingStyle = CodingStyle.ArrayList; |
| 109 | ············fkRelation.FieldName = MakeCamelCase(foreignTableNode.Text); |
| 110 | ············fkRelation.RelationDirection = RelationDirection.DirectedToMe;// Bidirectional; |
| 111 | ············relationNode.RelatedTableNode = foreignTableNode; |
| 112 | ············fkRelation.RelatedTable = dataRelation.ParentTable.TableName; |
| 113 | ············fkRelation.RelatedType = relationNode.RelatedTableNode.Table.ClassName; |
| 114 | ············fkRelation.IsComposite = false; |
| 115 | |
| 116 | ············string singularFieldName = null; |
| 117 | ············if (xpath != null) |
| 118 | ················singularFieldName = MakePascalCase(xpath.Substring(xpath.LastIndexOf('/') + 1)); |
| 119 | ············else |
| 120 | ················singularFieldName = MakePascalCase(ownTableNode.Text); |
| 121 | |
| 122 | ············singularFieldName = singularFieldName.Substring(singularFieldName.IndexOf(':') + 1); |
| 123 | |
| 124 | ············string foreignFieldName = MakeCamelCase(singularFieldName); |
| 125 | ············if (foreignFieldName.EndsWith("y")) |
| 126 | ············{ |
| 127 | ················char c = char.ToLower(foreignFieldName[foreignFieldName.Length - 2]);················ |
| 128 | ················if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') |
| 129 | ····················foreignFieldName += "s"; |
| 130 | ················else |
| 131 | ····················foreignFieldName = foreignFieldName.Substring(0, foreignFieldName.Length - 1) + "ies"; |
| 132 | ············} |
| 133 | ············else if (foreignFieldName.EndsWith("s")) |
| 134 | ················foreignFieldName += "es"; |
| 135 | ············else if (foreignFieldName.EndsWith("f")) |
| 136 | ················foreignFieldName = foreignFieldName.Substring(0, foreignFieldName.Length - 1) + "ves"; |
| 137 | ············else |
| 138 | ················foreignFieldName += "s"; |
| 139 | ············fkRelation.ForeignFieldName = foreignFieldName; |
| 140 | |
| 141 | ············fkRelation.ForeignIsComposite = true; |
| 142 | ············fkRelation.ForeignCodingStyle = CodingStyle.ArrayList; |
| 143 | ············fkRelation.RelationName = string.Empty; |
| 144 | ············MakeForeignKey(cn, model); |
| 145 | ············fkRelation.ForeignRelation.SingularFieldName = MakePascalCase(singularFieldName); |
| 146 | ············fkRelation.ForeignRelation.XPath = xpath; |
| 147 | ········} |
| 148 | |
| 149 | ········public void MakeForeignKey(ColumnNode columnNode) |
| 150 | ········{ |
| 151 | ············MakeForeignKey(columnNode, null); |
| 152 | ········} |
| 153 | |
| 154 | ········public void MakeForeignKey(ColumnNode columnNode, ForeignKeyWizModel model) |
| 155 | ········{ |
| 156 | ············FkRelation fkRelation = null; |
| 157 | ············RelationNode relationNode = null; |
| 158 | ············if ( model == null ) |
| 159 | ············{ |
| 160 | ················fkRelation = new FkRelation( columnNode.Name ); |
| 161 | ················relationNode = new RelationNode( fkRelation, columnNode.Parent ); |
| 162 | ············} |
| 163 | ············else |
| 164 | ············{ |
| 165 | ················fkRelation = (FkRelation) model.RelationNode.Relation; |
| 166 | ················relationNode = model.RelationNode; |
| 167 | ············} |
| 168 | ············fkRelation.OwningTable = ((TableNode) relationNode.Parent).Table.Name; |
| 169 | ············fkRelation.OwningType = ((TableNode) relationNode.Parent).Table.ClassName; |
| 170 | ············IList tableNodes = new ArrayList(); |
| 171 | ············if ( model == null ) |
| 172 | ············{ |
| 173 | ················foreach(TableNode tnode in ((DatabaseNode)columnNode.Parent.Parent).TableNodes) |
| 174 | ················{ |
| 175 | ····················if (tnode.Table.MappingType == TableMappingType.MappedAsClass) |
| 176 | ························tableNodes.Add(tnode); |
| 177 | ················} |
| 178 | ················model = new ForeignKeyWizModel( relationNode, tableNodes ); |
| 179 | ················IWizardController controller = ApplicationController.wizardControllerFactory.Create |
| 180 | ····················( "ForeignKeyWizController", "ForeignKeyWiz", "Foreign Key Wizard" ); |
| 181 | ················//controller.FrameSize = new Size( 544, 408 ); |
| 182 | ················DialogResult r = controller.Run( model ); |
| 183 | ················if ( r == DialogResult.OK ) |
| 184 | ················{ |
| 185 | ····················MakeForeignKeyRelation( relationNode, columnNode, model ); |
| 186 | ················} |
| 187 | ············} |
| 188 | ············else |
| 189 | ············{ |
| 190 | ····················MakeForeignKeyRelation( relationNode, columnNode, model ); |
| 191 | ············} |
| 192 | ········} |
| 193 | |
| 194 | |
| 195 | ········private void MakeForeignKeyRelation(RelationNode relationNode, ColumnNode columnNode, ForeignKeyWizModel model) |
| 196 | ········{ |
| 197 | ············FkRelation fkRelation = (FkRelation) relationNode.Relation; |
| 198 | ············fkRelation.RelatedTable = relationNode.RelatedTableNode.Table.Name; |
| 199 | ············fkRelation.RelatedType = relationNode.RelatedTableNode.Table.ClassName; |
| 200 | ············TableNode tn = (TableNode) columnNode.Parent; |
| 201 | ············//tn.Nodes. Remove(columnNode); |
| 202 | ············columnNode.Remove(); |
| 203 | ············tn.Nodes.Add(relationNode); |
| 204 | ············relationNode.OriginalColumnNode = columnNode; |
| 205 | ············this.assemblyNode.Refresh(); |
| 206 | ············if (fkRelation.RelationDirection == RelationDirection.DirectedToMe |
| 207 | ················|| fkRelation.RelationDirection == RelationDirection.Bidirectional) |
| 208 | ············{ |
| 209 | ················tn = databaseNode.FindTableNode(fkRelation.RelatedTable, true);···················· |
| 210 | ················RelationNode rn = new RelationNode(fkRelation.ForeignRelation, tn); |
| 211 | ················rn.RelatedTableNode = (TableNode) columnNode.Parent; |
| 212 | ················tn.Nodes.Add(rn); |
| 213 | ············} |
| 214 | ········} |
| 215 | |
| 216 | |
| 217 | ········public void DeleteRelation(RelationNode relationNode) |
| 218 | ········{ |
| 219 | ············FkRelation fkRelation = relationNode.Relation as FkRelation; |
| 220 | ············TableNode parentNode = null; |
| 221 | ············if (fkRelation == null) |
| 222 | ············{ |
| 223 | ················ForeignFkRelation ffkrel = relationNode.Relation as ForeignFkRelation; |
| 224 | ················if (ffkrel == null) |
| 225 | ····················return; |
| 226 | ················parentNode = (TableNode) this.databaseNode.FindNode(ffkrel.RelatedTable, typeof(TableNode)); |
| 227 | ················relationNode = parentNode.FindRelationNode(relationNode.Text, relationNode.Parent.Text);//(RelationNode) parentNode.FindNode(relationNode.Text, typeof(RelationNode)); |
| 228 | ················fkRelation = (FkRelation) relationNode.Relation; |
| 229 | ················if (parentNode.Table.MappingType == TableMappingType.MappedAsIntermediateClass) |
| 230 | ················{ |
| 231 | ····················parentNode.UnmapIntermediateClass(null, EventArgs.Empty); |
| 232 | ····················return; |
| 233 | ················} |
| 234 | ············} |
| 235 | ············else |
| 236 | ············{ |
| 237 | ················parentNode = (TableNode) relationNode.Parent; |
| 238 | ············} |
| 239 | |
| 240 | ············//parentNode.Nodes. Remove(relationNode); |
| 241 | ············relationNode.Remove(); |
| 242 | ············parentNode.Nodes.Add(relationNode.OriginalColumnNode); |
| 243 | ············ |
| 244 | ············Debug.Assert(fkRelation != null); |
| 245 | ············if (fkRelation != null) |
| 246 | ············{ |
| 247 | ················if (fkRelation.RelationDirection != RelationDirection.DirectedFromMe) |
| 248 | ················{ |
| 249 | ····················TableNode tn = databaseNode.FindTableNode(fkRelation.RelatedTable, true); |
| 250 | ····················RelationNode nodeToRemove = tn.FindRelationNode(relationNode.Text, parentNode.Text); |
| 251 | ····················if (nodeToRemove != null) |
| 252 | ························nodeToRemove.Remove(); |
| 253 | ················} |
| 254 | ············} |
| 255 | ········} |
| 256 | |
| 257 | ········public void GenerateAssembly() |
| 258 | ········{ |
| 259 | ············Assembly ass = assemblyNode.Assembly; |
| 260 | ············string fullPath = Path.GetFullPath(ass.TargetDir); |
| 261 | ············DirectoryInfo di = new DirectoryInfo(Path.GetPathRoot(fullPath)); |
| 262 | ············di.CreateSubdirectory(fullPath.Substring(3)); |
| 263 | ············string projExtension = ass.TargetLanguage == TargetLanguage.VB ? ".vbproj" : ".csproj"; |
| 264 | ············string projFile = Path.Combine(ass.TargetDir, ass.ProjectName + projExtension); |
| 265 | ············string ndoProjFile = Path.ChangeExtension(projFile, ".ndoproj"); |
| 266 | ············VsProject vsProject = new VsProject(ass.TargetLanguage, projFile, ass.ProjectName, ass.TargetDir, ass.RootNamespace, databaseNode.Database.ConnectionType); |
| 267 | ············NdoProject ndoProject = new NdoProject(ndoProjFile, |
| 268 | ················Path.Combine(ass.TargetDir, "bin\\debug\\" + ass.ProjectName + ".dll"), |
| 269 | ················Path.Combine(ass.TargetDir, "obj\\debug\\"), |
| 270 | ················ass.TargetDir); |
| 271 | ············ndoProject.Save(); |
| 272 | ············AssemblyGenerator assGen = new AssemblyGenerator(databaseNode, vsProject); |
| 273 | ············assGen.GenerateCode(); |
| 274 | ············string filename = ass.TargetDir + "\\NDOMapping.xml"; |
| 275 | ············vsProject.AddXmlFile("NDOMapping.xml"); |
| 276 | ············new MappingGenerator(filename, databaseNode, assemblyNode).GenerateCode(); |
| 277 | ············vsProject.Save(); |
| 278 | ············List<string> filesWithConflicts = assGen.FilesWithConflicts; |
| 279 | |
| 280 | ············if (vsProject.HasConflicts) |
| 281 | ················filesWithConflicts.Insert(0, projFile); |
| 282 | ············if ( filesWithConflicts.Count > 0 ) |
| 283 | ············{ |
| 284 | ················string strConflicts = "Files with conflicts:\n\n"; |
| 285 | ················foreach ( string s in filesWithConflicts ) |
| 286 | ····················strConflicts += s + '\n'; |
| 287 | ················strConflicts += "\nPlease resolve the conflicts, otherwise the project might not compile."; |
| 288 | ················MessageBox.Show(strConflicts, "NDO Class Generator"); |
| 289 | ············} |
| 290 | |
| 291 | ············foreach (string oldFile in Directory.GetFiles(ass.TargetDir, "*.old")) |
| 292 | ················File.Delete(oldFile); |
| 293 | ········} |
| 294 | |
| 295 | ········public void MapIntermediateTable(TableNode tn) |
| 296 | ········{ |
| 297 | ············IntermediateTable it = new IntermediateTable(tn.Text); |
| 298 | ············IntermediateTableNode itn = new IntermediateTableNode(it, tn); |
| 299 | |
| 300 | ············IList tableNodes = databaseNode.TableNodes; |
| 301 | ············tableNodes.Remove(tn); |
| 302 | |
| 303 | ············IntermediateTableWizardModel model = new IntermediateTableWizardModel(itn, tableNodes); |
| 304 | |
| 305 | ············IWizardController controller = ApplicationController.wizardControllerFactory.Create |
| 306 | ················("IntermediateTableWizController", "IntTblWiz", "Intermediate Table Wizard"); |
| 307 | ············//controller.FrameSize = new Size(544, 416); |
| 308 | ············DialogResult r = controller.Run(model); |
| 309 | ············if (r == DialogResult.OK) |
| 310 | ············{ |
| 311 | ················DatabaseNode parent = (DatabaseNode) tn.Parent; |
| 312 | ················//tn.Parent.Nodes. Remove(tn); |
| 313 | ················int index = parent.Nodes.IndexOf(tn); |
| 314 | ················tn.Remove(); |
| 315 | ················tn.Parent.Nodes.Insert(index, itn); |
| 316 | ················itn.TreeView.SelectedNode = itn; |
| 317 | ················itn.Nodes.Add(tn);··// This ensures persisting the column nodes with file/save. |
| 318 | ················RelatedTableInfo rti0 = it[0]; |
| 319 | ················RelatedTableInfo rti1 = it[1]; |
| 320 | ················TableNode relTn0 = null; |
| 321 | ················TableNode relTn1 = null; |
| 322 | ················RelationNode relNode0 = null; |
| 323 | ················RelationNode relNode1 = null; |
| 324 | ················foreach(TableNode loopTn in tableNodes) |
| 325 | ················{ |
| 326 | ····················if (loopTn.Text == rti0.Table) |
| 327 | ····················{ |
| 328 | ························relTn0 = loopTn; |
| 329 | ························if (rti0.RelationDirection != RelationDirection.DirectedToMe) |
| 330 | ························{ |
| 331 | ····························ForeignIntermediateTableRelation fitr = new ForeignIntermediateTableRelation(rti0, tn.Text); |
| 332 | ····························relNode0 = new RelationNode(fitr, relTn0); |
| 333 | ····························relTn0.Nodes.Add(relNode0); |
| 334 | ························} |
| 335 | ····················} |
| 336 | ····················if (loopTn.Text == rti1.Table) |
| 337 | ····················{ |
| 338 | ························relTn1 = loopTn; |
| 339 | ························if (rti1.RelationDirection != RelationDirection.DirectedToMe) |
| 340 | ························{ |
| 341 | ····························ForeignIntermediateTableRelation fitr = new ForeignIntermediateTableRelation(rti1, tn.Text); |
| 342 | ····························relNode1 = new RelationNode(fitr, relTn1); |
| 343 | ····························relTn1.Nodes.Add(relNode1); |
| 344 | ························} |
| 345 | ····················} |
| 346 | ················} |
| 347 | ················if (relNode0 != null) |
| 348 | ····················relNode0.RelatedTableNode = relTn1; |
| 349 | ················if (relNode1 != null) |
| 350 | ····················relNode1.RelatedTableNode = relTn0; |
| 351 | ············} |
| 352 | ········} |
| 353 | |
| 354 | ········public bool MapIntermediateClass(TableNode tn) |
| 355 | ········{ |
| 356 | //············IntermediateClass ic = new IntermediateClass(); |
| 357 | //············IntermediateClassNode icn = new IntermediateClassNode(ic, tn); |
| 358 | |
| 359 | ············IList tableNodes = new ArrayList(); |
| 360 | |
| 361 | ············foreach(TableNode tnode in databaseNode.TableNodes) |
| 362 | ············{ |
| 363 | ················if (tn.Text == tnode.Text) |
| 364 | ····················continue; |
| 365 | ················if (tnode.Table.MappingType == TableMappingType.MappedAsClass) |
| 366 | ····················tableNodes.Add(tnode); |
| 367 | ············} |
| 368 | |
| 369 | ············IntermediateClassWizardModel model = new IntermediateClassWizardModel(tn, tableNodes); |
| 370 | |
| 371 | ············IWizardController controller = ApplicationController.wizardControllerFactory.Create |
| 372 | ················("IntermediateClassWizController", "IntClassWiz", "Intermediate Class Wizard"); |
| 373 | ············//controller.FrameSize = new Size(544, 500); |
| 374 | ············model[0].RelationDirection = RelationDirection.Bidirectional; |
| 375 | ············model[1].RelationDirection = RelationDirection.Bidirectional; |
| 376 | ············DialogResult r = controller.Run(model); |
| 377 | ············if (r == DialogResult.OK) |
| 378 | ············{ |
| 379 | ················DatabaseNode parent = (DatabaseNode) tn.Parent; |
| 380 | ················// Nothing to remove, because we use the original table node |
| 381 | ················//················tn.Remove(); |
| 382 | ················//················tn.Parent.Nodes.Add(icn); |
| 383 | ················for(int i = 0; i < 2; i++) |
| 384 | ················{ |
| 385 | ····················ColumnNode columnNode = (ColumnNode) tn.FindNode(model[i].ForeignKeyColumnName, typeof(ColumnNode)); |
| 386 | ····················FkRelation fkr = new FkRelation(columnNode.Text); |
| 387 | ····················IntermediateClassInfo intermClInfo = model[i]; |
| 388 | ····················fkr.FieldName = intermClInfo.OwnFieldName; |
| 389 | ····················fkr.ForeignCodingStyle = intermClInfo.CodingStyle; |
| 390 | ····················fkr.ForeignFieldName = intermClInfo.ForeignFieldName; |
| 391 | ····················fkr.ForeignIsComposite = false; |
| 392 | ····················fkr.IsComposite = false; |
| 393 | ····················fkr.OwningTable = tn.Text; |
| 394 | ····················fkr.OwningType = tn.Table.ClassName; |
| 395 | ····················fkr.RelatedTable = intermClInfo.Table; |
| 396 | ····················fkr.RelatedType = intermClInfo.Type; |
| 397 | ····················fkr.RelationDirection = intermClInfo.RelationDirection; |
| 398 | ····················fkr.RelationName = string.Empty; |
| 399 | ····················//ForeignFkRelation ffkr = fkr.ForeignRelation; |
| 400 | ····················RelationNode relationNode = new RelationNode(fkr, tn); |
| 401 | ····················relationNode.RelatedTableNode = (TableNode) databaseNode.FindNode(intermClInfo.Table); |
| 402 | ····················ForeignKeyWizModel fkwizModel = new ForeignKeyWizModel(relationNode, new ArrayList()); |
| 403 | |
| 404 | ····················tn.DualKeyRelations[i] = intermClInfo.OwnFieldName; |
| 405 | |
| 406 | ····················MakeForeignKeyRelation(relationNode, columnNode, fkwizModel); |
| 407 | ················} |
| 408 | ················return true; |
| 409 | ············} |
| 410 | ············return false; |
| 411 | ········} |
| 412 | |
| 413 | ········void RemoveRelationNodeFromTable(string tableName, string nodeName) |
| 414 | ········{ |
| 415 | ············TableNode tn = (TableNode) databaseNode.FindNode(tableName); |
| 416 | |
| 417 | ············if (tn != null) |
| 418 | ············{ |
| 419 | ················RelationNode relNode = (RelationNode) tn.FindNode(nodeName, typeof(RelationNode)); |
| 420 | ················if (relNode != null) |
| 421 | ····················relNode.Remove(); |
| 422 | ····················//tn.Nodes. Remove(relNode); |
| 423 | ············} |
| 424 | ········} |
| 425 | |
| 426 | ········public void UnmapIntermediateTable(IntermediateTableNode itn) |
| 427 | ········{ |
| 428 | ············TableNode tn = itn.OriginalTableNode; |
| 429 | ············int index = databaseNode.Nodes.IndexOf(itn); |
| 430 | ············tn.Remove(); // Was inserted as node in itn. |
| 431 | ············itn.Remove(); |
| 432 | ············databaseNode.Nodes.Insert(index, tn); |
| 433 | ············tn.TreeView.SelectedNode = tn; |
| 434 | ············for (int tableNr = 0; tableNr < 2; tableNr++) |
| 435 | ············{ |
| 436 | ················RelatedTableInfo rti = itn.IntermediateTable[tableNr]; |
| 437 | ················RemoveRelationNodeFromTable(rti.Table, rti.ForeignKeyColumnName); |
| 438 | ············} |
| 439 | ········} |
| 440 | |
| 441 | ········public void UnmapIntermediateClass(TableNode tn) |
| 442 | ········{ |
| 443 | ············foreach(NDOTreeNode trn in tn.Nodes) |
| 444 | ············{ |
| 445 | ················RelationNode rn = trn as RelationNode; |
| 446 | ················if (rn == null) |
| 447 | ····················continue; |
| 448 | ················rn.Remove(); |
| 449 | ················FkRelation fkRelation = rn.Relation as FkRelation; |
| 450 | ················if (fkRelation != null) |
| 451 | ················{ |
| 452 | ····················if (fkRelation.RelationDirection != RelationDirection.DirectedFromMe) |
| 453 | ····················{ |
| 454 | ························TableNode relTn = databaseNode.FindTableNode(fkRelation.RelatedTable, true); |
| 455 | ························RelationNode nodeToRemove = relTn.FindRelationNode(rn.Text, tn.Text); |
| 456 | ························if (nodeToRemove != null) |
| 457 | ····························nodeToRemove.Remove(); |
| 458 | ····················} |
| 459 | ················} |
| 460 | ············} |
| 461 | ········} |
| 462 | |
| 463 | ········public void AddPrimaryKeyColum(TableNode tn) |
| 464 | ········{ |
| 465 | ············//try |
| 466 | ············//{ |
| 467 | ················AddPrimaryKeyWiz wiz = new AddPrimaryKeyWiz(); |
| 468 | ················if (wiz.ShowDialog() != DialogResult.OK) |
| 469 | ····················return; |
| 470 | ················foreach (ColumnNode cn in tn.ColumnNodes) |
| 471 | ················{ |
| 472 | ····················if (cn.IsPrimary) |
| 473 | ························cn.ChangePrimary(null, EventArgs.Empty); |
| 474 | ················} |
| 475 | ················ISqlGenerator generator = (ISqlGenerator)NDOProviderFactory.Instance.Generators[this.databaseNode.Database.ConnectionType]; |
| 476 | ················IProvider provider = NDOProviderFactory.Instance[generator.ProviderName]; |
| 477 | ················generator.Provider = provider; |
| 478 | ················string columnString = CreateColumn(generator, provider, wiz.ColumnName, wiz.ColumnType, wiz.IsAutoNumbered); |
| 479 | ················string alter = "ALTER TABLE " + generator.AddColumn() + " " + columnString; |
| 480 | IDbConnection conn = provider. NewConnection( databaseNode. Database. ConnectionString) ; |
| 481 | IDbCommand cmd = provider. NewSqlCommand( conn) ; |
| 482 | ················cmd.CommandText = alter; |
| 483 | ················MessageBox.Show(alter); |
| 484 | ················//cmd.ExecuteNonQuery(); |
| 485 | ············//} |
| 486 | ············//catch (Exception ex) |
| 487 | ············//{ |
| 488 | ············//····MessageBox.Show(ex.Message, "Error"); |
| 489 | ············//} |
| 490 | ············//············sb.Append(generator.NullExpression(false)); |
| 491 | |
| 492 | ········} |
| 493 | |
| 494 | |
| 495 | ········protected string CreateColumn(ISqlGenerator generator, IProvider provider, string columnName, Type type, bool autoIncrement) |
| 496 | ········{ |
| 497 | ············string name = provider.GetQuotedName(columnName); |
| 498 | ············string columnType = null; |
| 499 | ············string width = null; |
| 500 | ············StringBuilder sb = new StringBuilder(); |
| 501 | |
| 502 | ············columnType = generator.DbTypeFromType(type); |
| 503 | |
| 504 | ············int dl = provider.GetDefaultLength(type); |
| 505 | ············if (dl != 0) |
| 506 | ················width = dl.ToString(); |
| 507 | |
| 508 | ············if (!generator.LengthAllowed(type)) |
| 509 | ················width = null; |
| 510 | |
| 511 | ············if (autoIncrement && generator.HasSpecialAutoIncrementColumnFormat) |
| 512 | ················sb.Append(generator.AutoIncrementColumn(name, type, columnType, width)); |
| 513 | ············else if (generator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InColumn) |
| 514 | ················sb.Append(generator.PrimaryKeyColumn(name, type, columnType, width)); |
| 515 | ············else if (width != null) |
| 516 | ················sb.Append(name + " " + columnType + "(" + width + ")"); |
| 517 | ············else |
| 518 | ················sb.Append(name + " " + columnType); |
| 519 | |
| 520 | ············sb.Append(" "); |
| 521 | |
| 522 | ············return sb.ToString(); |
| 523 | ········} |
| 524 | |
| 525 | |
| 526 | |
| 527 | ········public AssemblyWizModel ReCreateModel() |
| 528 | ········{ |
| 529 | ············AssemblyWizModel model = new AssemblyWizModel(); |
| 530 | ············Database db = this.databaseNode.Database; |
| 531 | ············model.ConnectionString = db.ConnectionString; |
| 532 | ············model.ConnectionType = db.ConnectionType; |
| 533 | ············model.IsXmlSchema = db.IsXmlSchema; |
| 534 | ············model.OwnerName = db.OwnerName; |
| 535 | ············model.XmlSchemaFile = db.XmlSchemaFile; |
| 536 | |
| 537 | ············Assembly ass = this.assemblyNode.Assembly; |
| 538 | ············model.DefaultNamespace = ass.RootNamespace; |
| 539 | ············model.ProjectDirectory = ass.TargetDir; |
| 540 | ············model.ProjectName = ass.ProjectName; |
| 541 | ············model.TargetLanguage = ass.TargetLanguage; |
| 542 | ············model.UseClassField = ass.UseClassField; |
| 543 | ············return model; |
| 544 | ········} |
| 545 | |
| 546 | ········private ApplicationController() |
| 547 | ········{ |
| 548 | ········} |
| 549 | ····} |
| 550 | } |
| 551 |
New Commit (74c26c3)
| 1 | // |
| 2 | // Copyright (c) 2002-2016 Mirko Matytschak |
| 3 | // (www.netdataobjects.de) |
| 4 | // |
| 5 | // Author: Mirko Matytschak |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 11 | // conditions: |
| 12 | |
| 13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 14 | // of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | // DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | |
| 23 | using System; |
| 24 | using System.Data; |
| 25 | using System.Data.Common; |
| 26 | using System.Text; |
| 27 | using System.IO; |
| 28 | using System.Diagnostics; |
| 29 | using System.Collections; |
| 30 | using System.Collections.Generic; |
| 31 | using System.Drawing; |
| 32 | using System.Windows.Forms; |
| 33 | using ClassGenerator.ForeignKeyWizard; |
| 34 | using ClassGenerator.AssemblyWizard; |
| 35 | using ClassGenerator.IntermediateTableWizard; |
| 36 | using ClassGenerator.IntermediateClassWizard; |
| 37 | using ClassGenerator.AddPrimaryKeyWizard; |
| 38 | using WizardBase; |
| 39 | using Generator; |
| 40 | using NDO; |
| 41 | using NDOInterfaces; |
| 42 | |
| 43 | namespace ClassGenerator |
| 44 | { |
| 45 | ····/// <summary> |
| 46 | ····/// Zusammenfassung für Controller. |
| 47 | ····/// </summary> |
| 48 | #if DEBUG |
| 49 | ····public class ApplicationController |
| 50 | #else |
| 51 | ····internal class ApplicationController |
| 52 | #endif |
| 53 | ····{ |
| 54 | ········static ApplicationController instance = new ApplicationController(); |
| 55 | ········public static ApplicationController Instance |
| 56 | ········{ |
| 57 | ············get { return instance; } |
| 58 | ········} |
| 59 | |
| 60 | ········static WizardControllerFactory wizardControllerFactory = new WizardControllerFactory(typeof(ApplicationController).Assembly, ""); |
| 61 | ········public static WizardControllerFactory WizardControllerFactory |
| 62 | ········{ |
| 63 | ············get { return wizardControllerFactory; } |
| 64 | ············set { wizardControllerFactory = value; } |
| 65 | ········} |
| 66 | |
| 67 | ········DatabaseNode databaseNode; |
| 68 | ········public DatabaseNode DatabaseNode |
| 69 | ········{ |
| 70 | ············get { return databaseNode; } |
| 71 | ············set { databaseNode = value; } |
| 72 | ········} |
| 73 | ········AssemblyNode assemblyNode; |
| 74 | ········public AssemblyNode AssemblyNode |
| 75 | ········{ |
| 76 | ············get { return assemblyNode; } |
| 77 | ············set { assemblyNode = value; } |
| 78 | ········} |
| 79 | |
| 80 | ········public void MappingChanged() |
| 81 | ········{ |
| 82 | ············this.assemblyNode.Refresh(); |
| 83 | ········} |
| 84 | |
| 85 | ········string MakeCamelCase( string input ) |
| 86 | ········{ |
| 87 | ············string prefix = this.assemblyNode.Assembly.TargetLanguage == TargetLanguage.VB ? "_" : string.Empty; |
| 88 | ············return prefix + input.Substring(0,1).ToLower() + input.Substring(1); |
| 89 | ········} |
| 90 | |
| 91 | ········string MakePascalCase(string input) |
| 92 | ········{ |
| 93 | ············return input.Substring(0, 1).ToUpper() + input.Substring(1); |
| 94 | ········} |
| 95 | |
| 96 | ········public void MakeForeignKey( DataRelation dataRelation ) |
| 97 | ········{ |
| 98 | ············// The FkRelation is owned by the child table, because it holds |
| 99 | ············// the foreign key. |
| 100 | ············// Therefore the XPath has to be stored in the ForeignFkRelation. |
| 101 | ············string xpath = (string)dataRelation.ExtendedProperties["xpath"]; |
| 102 | |
| 103 | ············TableNode ownTableNode = this.databaseNode[dataRelation.ChildTable.TableName]; |
| 104 | ············TableNode foreignTableNode = this.databaseNode[dataRelation.ParentTable.TableName]; |
| 105 | ············ColumnNode cn = ownTableNode[dataRelation.ChildColumns[0].ColumnName]; |
| 106 | ············FkRelation fkRelation = new FkRelation(cn.Text); |
| 107 | ············RelationNode relationNode = new RelationNode(fkRelation, cn.Parent); |
| 108 | ············ForeignKeyWizModel model = new ForeignKeyWizModel(relationNode, null); |
| 109 | ············fkRelation.CodingStyle = CodingStyle.ArrayList; |
| 110 | ············fkRelation.FieldName = MakeCamelCase(foreignTableNode.Text); |
| 111 | ············fkRelation.RelationDirection = RelationDirection.DirectedToMe;// Bidirectional; |
| 112 | ············relationNode.RelatedTableNode = foreignTableNode; |
| 113 | ············fkRelation.RelatedTable = dataRelation.ParentTable.TableName; |
| 114 | ············fkRelation.RelatedType = relationNode.RelatedTableNode.Table.ClassName; |
| 115 | ············fkRelation.IsComposite = false; |
| 116 | |
| 117 | ············string singularFieldName = null; |
| 118 | ············if (xpath != null) |
| 119 | ················singularFieldName = MakePascalCase(xpath.Substring(xpath.LastIndexOf('/') + 1)); |
| 120 | ············else |
| 121 | ················singularFieldName = MakePascalCase(ownTableNode.Text); |
| 122 | |
| 123 | ············singularFieldName = singularFieldName.Substring(singularFieldName.IndexOf(':') + 1); |
| 124 | |
| 125 | ············string foreignFieldName = MakeCamelCase(singularFieldName); |
| 126 | ············if (foreignFieldName.EndsWith("y")) |
| 127 | ············{ |
| 128 | ················char c = char.ToLower(foreignFieldName[foreignFieldName.Length - 2]);················ |
| 129 | ················if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') |
| 130 | ····················foreignFieldName += "s"; |
| 131 | ················else |
| 132 | ····················foreignFieldName = foreignFieldName.Substring(0, foreignFieldName.Length - 1) + "ies"; |
| 133 | ············} |
| 134 | ············else if (foreignFieldName.EndsWith("s")) |
| 135 | ················foreignFieldName += "es"; |
| 136 | ············else if (foreignFieldName.EndsWith("f")) |
| 137 | ················foreignFieldName = foreignFieldName.Substring(0, foreignFieldName.Length - 1) + "ves"; |
| 138 | ············else |
| 139 | ················foreignFieldName += "s"; |
| 140 | ············fkRelation.ForeignFieldName = foreignFieldName; |
| 141 | |
| 142 | ············fkRelation.ForeignIsComposite = true; |
| 143 | ············fkRelation.ForeignCodingStyle = CodingStyle.ArrayList; |
| 144 | ············fkRelation.RelationName = string.Empty; |
| 145 | ············MakeForeignKey(cn, model); |
| 146 | ············fkRelation.ForeignRelation.SingularFieldName = MakePascalCase(singularFieldName); |
| 147 | ············fkRelation.ForeignRelation.XPath = xpath; |
| 148 | ········} |
| 149 | |
| 150 | ········public void MakeForeignKey(ColumnNode columnNode) |
| 151 | ········{ |
| 152 | ············MakeForeignKey(columnNode, null); |
| 153 | ········} |
| 154 | |
| 155 | ········public void MakeForeignKey(ColumnNode columnNode, ForeignKeyWizModel model) |
| 156 | ········{ |
| 157 | ············FkRelation fkRelation = null; |
| 158 | ············RelationNode relationNode = null; |
| 159 | ············if ( model == null ) |
| 160 | ············{ |
| 161 | ················fkRelation = new FkRelation( columnNode.Name ); |
| 162 | ················relationNode = new RelationNode( fkRelation, columnNode.Parent ); |
| 163 | ············} |
| 164 | ············else |
| 165 | ············{ |
| 166 | ················fkRelation = (FkRelation) model.RelationNode.Relation; |
| 167 | ················relationNode = model.RelationNode; |
| 168 | ············} |
| 169 | ············fkRelation.OwningTable = ((TableNode) relationNode.Parent).Table.Name; |
| 170 | ············fkRelation.OwningType = ((TableNode) relationNode.Parent).Table.ClassName; |
| 171 | ············IList tableNodes = new ArrayList(); |
| 172 | ············if ( model == null ) |
| 173 | ············{ |
| 174 | ················foreach(TableNode tnode in ((DatabaseNode)columnNode.Parent.Parent).TableNodes) |
| 175 | ················{ |
| 176 | ····················if (tnode.Table.MappingType == TableMappingType.MappedAsClass) |
| 177 | ························tableNodes.Add(tnode); |
| 178 | ················} |
| 179 | ················model = new ForeignKeyWizModel( relationNode, tableNodes ); |
| 180 | ················IWizardController controller = ApplicationController.wizardControllerFactory.Create |
| 181 | ····················( "ForeignKeyWizController", "ForeignKeyWiz", "Foreign Key Wizard" ); |
| 182 | ················//controller.FrameSize = new Size( 544, 408 ); |
| 183 | ················DialogResult r = controller.Run( model ); |
| 184 | ················if ( r == DialogResult.OK ) |
| 185 | ················{ |
| 186 | ····················MakeForeignKeyRelation( relationNode, columnNode, model ); |
| 187 | ················} |
| 188 | ············} |
| 189 | ············else |
| 190 | ············{ |
| 191 | ····················MakeForeignKeyRelation( relationNode, columnNode, model ); |
| 192 | ············} |
| 193 | ········} |
| 194 | |
| 195 | |
| 196 | ········private void MakeForeignKeyRelation(RelationNode relationNode, ColumnNode columnNode, ForeignKeyWizModel model) |
| 197 | ········{ |
| 198 | ············FkRelation fkRelation = (FkRelation) relationNode.Relation; |
| 199 | ············fkRelation.RelatedTable = relationNode.RelatedTableNode.Table.Name; |
| 200 | ············fkRelation.RelatedType = relationNode.RelatedTableNode.Table.ClassName; |
| 201 | ············TableNode tn = (TableNode) columnNode.Parent; |
| 202 | ············//tn.Nodes. Remove(columnNode); |
| 203 | ············columnNode.Remove(); |
| 204 | ············tn.Nodes.Add(relationNode); |
| 205 | ············relationNode.OriginalColumnNode = columnNode; |
| 206 | ············this.assemblyNode.Refresh(); |
| 207 | ············if (fkRelation.RelationDirection == RelationDirection.DirectedToMe |
| 208 | ················|| fkRelation.RelationDirection == RelationDirection.Bidirectional) |
| 209 | ············{ |
| 210 | ················tn = databaseNode.FindTableNode(fkRelation.RelatedTable, true);···················· |
| 211 | ················RelationNode rn = new RelationNode(fkRelation.ForeignRelation, tn); |
| 212 | ················rn.RelatedTableNode = (TableNode) columnNode.Parent; |
| 213 | ················tn.Nodes.Add(rn); |
| 214 | ············} |
| 215 | ········} |
| 216 | |
| 217 | |
| 218 | ········public void DeleteRelation(RelationNode relationNode) |
| 219 | ········{ |
| 220 | ············FkRelation fkRelation = relationNode.Relation as FkRelation; |
| 221 | ············TableNode parentNode = null; |
| 222 | ············if (fkRelation == null) |
| 223 | ············{ |
| 224 | ················ForeignFkRelation ffkrel = relationNode.Relation as ForeignFkRelation; |
| 225 | ················if (ffkrel == null) |
| 226 | ····················return; |
| 227 | ················parentNode = (TableNode) this.databaseNode.FindNode(ffkrel.RelatedTable, typeof(TableNode)); |
| 228 | ················relationNode = parentNode.FindRelationNode(relationNode.Text, relationNode.Parent.Text);//(RelationNode) parentNode.FindNode(relationNode.Text, typeof(RelationNode)); |
| 229 | ················fkRelation = (FkRelation) relationNode.Relation; |
| 230 | ················if (parentNode.Table.MappingType == TableMappingType.MappedAsIntermediateClass) |
| 231 | ················{ |
| 232 | ····················parentNode.UnmapIntermediateClass(null, EventArgs.Empty); |
| 233 | ····················return; |
| 234 | ················} |
| 235 | ············} |
| 236 | ············else |
| 237 | ············{ |
| 238 | ················parentNode = (TableNode) relationNode.Parent; |
| 239 | ············} |
| 240 | |
| 241 | ············//parentNode.Nodes. Remove(relationNode); |
| 242 | ············relationNode.Remove(); |
| 243 | ············parentNode.Nodes.Add(relationNode.OriginalColumnNode); |
| 244 | ············ |
| 245 | ············Debug.Assert(fkRelation != null); |
| 246 | ············if (fkRelation != null) |
| 247 | ············{ |
| 248 | ················if (fkRelation.RelationDirection != RelationDirection.DirectedFromMe) |
| 249 | ················{ |
| 250 | ····················TableNode tn = databaseNode.FindTableNode(fkRelation.RelatedTable, true); |
| 251 | ····················RelationNode nodeToRemove = tn.FindRelationNode(relationNode.Text, parentNode.Text); |
| 252 | ····················if (nodeToRemove != null) |
| 253 | ························nodeToRemove.Remove(); |
| 254 | ················} |
| 255 | ············} |
| 256 | ········} |
| 257 | |
| 258 | ········public void GenerateAssembly() |
| 259 | ········{ |
| 260 | ············Assembly ass = assemblyNode.Assembly; |
| 261 | ············string fullPath = Path.GetFullPath(ass.TargetDir); |
| 262 | ············DirectoryInfo di = new DirectoryInfo(Path.GetPathRoot(fullPath)); |
| 263 | ············di.CreateSubdirectory(fullPath.Substring(3)); |
| 264 | ············string projExtension = ass.TargetLanguage == TargetLanguage.VB ? ".vbproj" : ".csproj"; |
| 265 | ············string projFile = Path.Combine(ass.TargetDir, ass.ProjectName + projExtension); |
| 266 | ············string ndoProjFile = Path.ChangeExtension(projFile, ".ndoproj"); |
| 267 | ············VsProject vsProject = new VsProject(ass.TargetLanguage, projFile, ass.ProjectName, ass.TargetDir, ass.RootNamespace, databaseNode.Database.ConnectionType); |
| 268 | ············NdoProject ndoProject = new NdoProject(ndoProjFile, |
| 269 | ················Path.Combine(ass.TargetDir, "bin\\debug\\" + ass.ProjectName + ".dll"), |
| 270 | ················Path.Combine(ass.TargetDir, "obj\\debug\\"), |
| 271 | ················ass.TargetDir); |
| 272 | ············ndoProject.Save(); |
| 273 | ············AssemblyGenerator assGen = new AssemblyGenerator(databaseNode, vsProject); |
| 274 | ············assGen.GenerateCode(); |
| 275 | ············string filename = ass.TargetDir + "\\NDOMapping.xml"; |
| 276 | ············vsProject.AddXmlFile("NDOMapping.xml"); |
| 277 | ············new MappingGenerator(filename, databaseNode, assemblyNode).GenerateCode(); |
| 278 | ············vsProject.Save(); |
| 279 | ············List<string> filesWithConflicts = assGen.FilesWithConflicts; |
| 280 | |
| 281 | ············if (vsProject.HasConflicts) |
| 282 | ················filesWithConflicts.Insert(0, projFile); |
| 283 | ············if ( filesWithConflicts.Count > 0 ) |
| 284 | ············{ |
| 285 | ················string strConflicts = "Files with conflicts:\n\n"; |
| 286 | ················foreach ( string s in filesWithConflicts ) |
| 287 | ····················strConflicts += s + '\n'; |
| 288 | ················strConflicts += "\nPlease resolve the conflicts, otherwise the project might not compile."; |
| 289 | ················MessageBox.Show(strConflicts, "NDO Class Generator"); |
| 290 | ············} |
| 291 | |
| 292 | ············foreach (string oldFile in Directory.GetFiles(ass.TargetDir, "*.old")) |
| 293 | ················File.Delete(oldFile); |
| 294 | ········} |
| 295 | |
| 296 | ········public void MapIntermediateTable(TableNode tn) |
| 297 | ········{ |
| 298 | ············IntermediateTable it = new IntermediateTable(tn.Text); |
| 299 | ············IntermediateTableNode itn = new IntermediateTableNode(it, tn); |
| 300 | |
| 301 | ············IList tableNodes = databaseNode.TableNodes; |
| 302 | ············tableNodes.Remove(tn); |
| 303 | |
| 304 | ············IntermediateTableWizardModel model = new IntermediateTableWizardModel(itn, tableNodes); |
| 305 | |
| 306 | ············IWizardController controller = ApplicationController.wizardControllerFactory.Create |
| 307 | ················("IntermediateTableWizController", "IntTblWiz", "Intermediate Table Wizard"); |
| 308 | ············//controller.FrameSize = new Size(544, 416); |
| 309 | ············DialogResult r = controller.Run(model); |
| 310 | ············if (r == DialogResult.OK) |
| 311 | ············{ |
| 312 | ················DatabaseNode parent = (DatabaseNode) tn.Parent; |
| 313 | ················//tn.Parent.Nodes. Remove(tn); |
| 314 | ················int index = parent.Nodes.IndexOf(tn); |
| 315 | ················tn.Remove(); |
| 316 | ················tn.Parent.Nodes.Insert(index, itn); |
| 317 | ················itn.TreeView.SelectedNode = itn; |
| 318 | ················itn.Nodes.Add(tn);··// This ensures persisting the column nodes with file/save. |
| 319 | ················RelatedTableInfo rti0 = it[0]; |
| 320 | ················RelatedTableInfo rti1 = it[1]; |
| 321 | ················TableNode relTn0 = null; |
| 322 | ················TableNode relTn1 = null; |
| 323 | ················RelationNode relNode0 = null; |
| 324 | ················RelationNode relNode1 = null; |
| 325 | ················foreach(TableNode loopTn in tableNodes) |
| 326 | ················{ |
| 327 | ····················if (loopTn.Text == rti0.Table) |
| 328 | ····················{ |
| 329 | ························relTn0 = loopTn; |
| 330 | ························if (rti0.RelationDirection != RelationDirection.DirectedToMe) |
| 331 | ························{ |
| 332 | ····························ForeignIntermediateTableRelation fitr = new ForeignIntermediateTableRelation(rti0, tn.Text); |
| 333 | ····························relNode0 = new RelationNode(fitr, relTn0); |
| 334 | ····························relTn0.Nodes.Add(relNode0); |
| 335 | ························} |
| 336 | ····················} |
| 337 | ····················if (loopTn.Text == rti1.Table) |
| 338 | ····················{ |
| 339 | ························relTn1 = loopTn; |
| 340 | ························if (rti1.RelationDirection != RelationDirection.DirectedToMe) |
| 341 | ························{ |
| 342 | ····························ForeignIntermediateTableRelation fitr = new ForeignIntermediateTableRelation(rti1, tn.Text); |
| 343 | ····························relNode1 = new RelationNode(fitr, relTn1); |
| 344 | ····························relTn1.Nodes.Add(relNode1); |
| 345 | ························} |
| 346 | ····················} |
| 347 | ················} |
| 348 | ················if (relNode0 != null) |
| 349 | ····················relNode0.RelatedTableNode = relTn1; |
| 350 | ················if (relNode1 != null) |
| 351 | ····················relNode1.RelatedTableNode = relTn0; |
| 352 | ············} |
| 353 | ········} |
| 354 | |
| 355 | ········public bool MapIntermediateClass(TableNode tn) |
| 356 | ········{ |
| 357 | //············IntermediateClass ic = new IntermediateClass(); |
| 358 | //············IntermediateClassNode icn = new IntermediateClassNode(ic, tn); |
| 359 | |
| 360 | ············IList tableNodes = new ArrayList(); |
| 361 | |
| 362 | ············foreach(TableNode tnode in databaseNode.TableNodes) |
| 363 | ············{ |
| 364 | ················if (tn.Text == tnode.Text) |
| 365 | ····················continue; |
| 366 | ················if (tnode.Table.MappingType == TableMappingType.MappedAsClass) |
| 367 | ····················tableNodes.Add(tnode); |
| 368 | ············} |
| 369 | |
| 370 | ············IntermediateClassWizardModel model = new IntermediateClassWizardModel(tn, tableNodes); |
| 371 | |
| 372 | ············IWizardController controller = ApplicationController.wizardControllerFactory.Create |
| 373 | ················("IntermediateClassWizController", "IntClassWiz", "Intermediate Class Wizard"); |
| 374 | ············//controller.FrameSize = new Size(544, 500); |
| 375 | ············model[0].RelationDirection = RelationDirection.Bidirectional; |
| 376 | ············model[1].RelationDirection = RelationDirection.Bidirectional; |
| 377 | ············DialogResult r = controller.Run(model); |
| 378 | ············if (r == DialogResult.OK) |
| 379 | ············{ |
| 380 | ················DatabaseNode parent = (DatabaseNode) tn.Parent; |
| 381 | ················// Nothing to remove, because we use the original table node |
| 382 | ················//················tn.Remove(); |
| 383 | ················//················tn.Parent.Nodes.Add(icn); |
| 384 | ················for(int i = 0; i < 2; i++) |
| 385 | ················{ |
| 386 | ····················ColumnNode columnNode = (ColumnNode) tn.FindNode(model[i].ForeignKeyColumnName, typeof(ColumnNode)); |
| 387 | ····················FkRelation fkr = new FkRelation(columnNode.Text); |
| 388 | ····················IntermediateClassInfo intermClInfo = model[i]; |
| 389 | ····················fkr.FieldName = intermClInfo.OwnFieldName; |
| 390 | ····················fkr.ForeignCodingStyle = intermClInfo.CodingStyle; |
| 391 | ····················fkr.ForeignFieldName = intermClInfo.ForeignFieldName; |
| 392 | ····················fkr.ForeignIsComposite = false; |
| 393 | ····················fkr.IsComposite = false; |
| 394 | ····················fkr.OwningTable = tn.Text; |
| 395 | ····················fkr.OwningType = tn.Table.ClassName; |
| 396 | ····················fkr.RelatedTable = intermClInfo.Table; |
| 397 | ····················fkr.RelatedType = intermClInfo.Type; |
| 398 | ····················fkr.RelationDirection = intermClInfo.RelationDirection; |
| 399 | ····················fkr.RelationName = string.Empty; |
| 400 | ····················//ForeignFkRelation ffkr = fkr.ForeignRelation; |
| 401 | ····················RelationNode relationNode = new RelationNode(fkr, tn); |
| 402 | ····················relationNode.RelatedTableNode = (TableNode) databaseNode.FindNode(intermClInfo.Table); |
| 403 | ····················ForeignKeyWizModel fkwizModel = new ForeignKeyWizModel(relationNode, new ArrayList()); |
| 404 | |
| 405 | ····················tn.DualKeyRelations[i] = intermClInfo.OwnFieldName; |
| 406 | |
| 407 | ····················MakeForeignKeyRelation(relationNode, columnNode, fkwizModel); |
| 408 | ················} |
| 409 | ················return true; |
| 410 | ············} |
| 411 | ············return false; |
| 412 | ········} |
| 413 | |
| 414 | ········void RemoveRelationNodeFromTable(string tableName, string nodeName) |
| 415 | ········{ |
| 416 | ············TableNode tn = (TableNode) databaseNode.FindNode(tableName); |
| 417 | |
| 418 | ············if (tn != null) |
| 419 | ············{ |
| 420 | ················RelationNode relNode = (RelationNode) tn.FindNode(nodeName, typeof(RelationNode)); |
| 421 | ················if (relNode != null) |
| 422 | ····················relNode.Remove(); |
| 423 | ····················//tn.Nodes. Remove(relNode); |
| 424 | ············} |
| 425 | ········} |
| 426 | |
| 427 | ········public void UnmapIntermediateTable(IntermediateTableNode itn) |
| 428 | ········{ |
| 429 | ············TableNode tn = itn.OriginalTableNode; |
| 430 | ············int index = databaseNode.Nodes.IndexOf(itn); |
| 431 | ············tn.Remove(); // Was inserted as node in itn. |
| 432 | ············itn.Remove(); |
| 433 | ············databaseNode.Nodes.Insert(index, tn); |
| 434 | ············tn.TreeView.SelectedNode = tn; |
| 435 | ············for (int tableNr = 0; tableNr < 2; tableNr++) |
| 436 | ············{ |
| 437 | ················RelatedTableInfo rti = itn.IntermediateTable[tableNr]; |
| 438 | ················RemoveRelationNodeFromTable(rti.Table, rti.ForeignKeyColumnName); |
| 439 | ············} |
| 440 | ········} |
| 441 | |
| 442 | ········public void UnmapIntermediateClass(TableNode tn) |
| 443 | ········{ |
| 444 | ············foreach(NDOTreeNode trn in tn.Nodes) |
| 445 | ············{ |
| 446 | ················RelationNode rn = trn as RelationNode; |
| 447 | ················if (rn == null) |
| 448 | ····················continue; |
| 449 | ················rn.Remove(); |
| 450 | ················FkRelation fkRelation = rn.Relation as FkRelation; |
| 451 | ················if (fkRelation != null) |
| 452 | ················{ |
| 453 | ····················if (fkRelation.RelationDirection != RelationDirection.DirectedFromMe) |
| 454 | ····················{ |
| 455 | ························TableNode relTn = databaseNode.FindTableNode(fkRelation.RelatedTable, true); |
| 456 | ························RelationNode nodeToRemove = relTn.FindRelationNode(rn.Text, tn.Text); |
| 457 | ························if (nodeToRemove != null) |
| 458 | ····························nodeToRemove.Remove(); |
| 459 | ····················} |
| 460 | ················} |
| 461 | ············} |
| 462 | ········} |
| 463 | |
| 464 | ········public void AddPrimaryKeyColum(TableNode tn) |
| 465 | ········{ |
| 466 | ············//try |
| 467 | ············//{ |
| 468 | ················AddPrimaryKeyWiz wiz = new AddPrimaryKeyWiz(); |
| 469 | ················if (wiz.ShowDialog() != DialogResult.OK) |
| 470 | ····················return; |
| 471 | ················foreach (ColumnNode cn in tn.ColumnNodes) |
| 472 | ················{ |
| 473 | ····················if (cn.IsPrimary) |
| 474 | ························cn.ChangePrimary(null, EventArgs.Empty); |
| 475 | ················} |
| 476 | ················ISqlGenerator generator = (ISqlGenerator)NDOProviderFactory.Instance.Generators[this.databaseNode.Database.ConnectionType]; |
| 477 | ················IProvider provider = NDOProviderFactory.Instance[generator.ProviderName]; |
| 478 | ················generator.Provider = provider; |
| 479 | ················string columnString = CreateColumn(generator, provider, wiz.ColumnName, wiz.ColumnType, wiz.IsAutoNumbered); |
| 480 | ················string alter = "ALTER TABLE " + generator.AddColumn() + " " + columnString; |
| 481 | DbConnection conn = provider. NewConnection( databaseNode. Database. ConnectionString) ; |
| 482 | DbCommand cmd = provider. NewSqlCommand( conn) ; |
| 483 | ················cmd.CommandText = alter; |
| 484 | ················MessageBox.Show(alter); |
| 485 | ················//cmd.ExecuteNonQuery(); |
| 486 | ············//} |
| 487 | ············//catch (Exception ex) |
| 488 | ············//{ |
| 489 | ············//····MessageBox.Show(ex.Message, "Error"); |
| 490 | ············//} |
| 491 | ············//············sb.Append(generator.NullExpression(false)); |
| 492 | |
| 493 | ········} |
| 494 | |
| 495 | |
| 496 | ········protected string CreateColumn(ISqlGenerator generator, IProvider provider, string columnName, Type type, bool autoIncrement) |
| 497 | ········{ |
| 498 | ············string name = provider.GetQuotedName(columnName); |
| 499 | ············string columnType = null; |
| 500 | ············string width = null; |
| 501 | ············StringBuilder sb = new StringBuilder(); |
| 502 | |
| 503 | ············columnType = generator.DbTypeFromType(type); |
| 504 | |
| 505 | ············int dl = provider.GetDefaultLength(type); |
| 506 | ············if (dl != 0) |
| 507 | ················width = dl.ToString(); |
| 508 | |
| 509 | ············if (!generator.LengthAllowed(type)) |
| 510 | ················width = null; |
| 511 | |
| 512 | ············if (autoIncrement && generator.HasSpecialAutoIncrementColumnFormat) |
| 513 | ················sb.Append(generator.AutoIncrementColumn(name, type, columnType, width)); |
| 514 | ············else if (generator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InColumn) |
| 515 | ················sb.Append(generator.PrimaryKeyColumn(name, type, columnType, width)); |
| 516 | ············else if (width != null) |
| 517 | ················sb.Append(name + " " + columnType + "(" + width + ")"); |
| 518 | ············else |
| 519 | ················sb.Append(name + " " + columnType); |
| 520 | |
| 521 | ············sb.Append(" "); |
| 522 | |
| 523 | ············return sb.ToString(); |
| 524 | ········} |
| 525 | |
| 526 | |
| 527 | |
| 528 | ········public AssemblyWizModel ReCreateModel() |
| 529 | ········{ |
| 530 | ············AssemblyWizModel model = new AssemblyWizModel(); |
| 531 | ············Database db = this.databaseNode.Database; |
| 532 | ············model.ConnectionString = db.ConnectionString; |
| 533 | ············model.ConnectionType = db.ConnectionType; |
| 534 | ············model.IsXmlSchema = db.IsXmlSchema; |
| 535 | ············model.OwnerName = db.OwnerName; |
| 536 | ············model.XmlSchemaFile = db.XmlSchemaFile; |
| 537 | |
| 538 | ············Assembly ass = this.assemblyNode.Assembly; |
| 539 | ············model.DefaultNamespace = ass.RootNamespace; |
| 540 | ············model.ProjectDirectory = ass.TargetDir; |
| 541 | ············model.ProjectName = ass.ProjectName; |
| 542 | ············model.TargetLanguage = ass.TargetLanguage; |
| 543 | ············model.UseClassField = ass.UseClassField; |
| 544 | ············return model; |
| 545 | ········} |
| 546 | |
| 547 | ········private ApplicationController() |
| 548 | ········{ |
| 549 | ········} |
| 550 | ····} |
| 551 | } |
| 552 |