Datei: ClassGenerator/Nodes/TableNode.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.Collections; |
| 25 | using System.Diagnostics; |
| 26 | using System.Windows.Forms; |
| 27 | using System.ComponentModel; |
| 28 | using System.Data; |
| 29 | using NDO; |
| 30 | using NDOInterfaces; |
| 31 | |
| 32 | namespace ClassGenerator |
| 33 | { |
| 34 | ····/// <summary> |
| 35 | ····/// Zusammenfassung für TableNode. |
| 36 | ····/// </summary> |
| 37 | ····[Serializable] |
| 38 | #if DEBUG |
| 39 | ····public class TableNode : NDOTreeNode |
| 40 | #else |
| 41 | ····internal class TableNode : NDOTreeNode |
| 42 | #endif |
| 43 | ····{ |
| 44 | ········public void MapIntermediateTable(Object sender, EventArgs e) |
| 45 | ········{ |
| 46 | ············ApplicationController.Instance.MapIntermediateTable(this); |
| 47 | ········} |
| 48 | ········public void MapIntermediateClass(Object sender, EventArgs e) |
| 49 | ········{ |
| 50 | ················if (this.Table.MappingType != TableMappingType.NotMapped) |
| 51 | ························return; |
| 52 | |
| 53 | ············MapClass(false); |
| 54 | ············ |
| 55 | ············if (ApplicationController.Instance.MapIntermediateClass(this)) |
| 56 | ············{ |
| 57 | ················this.Table.MappingType = TableMappingType.MappedAsIntermediateClass; |
| 58 | ················this.OnIsMappedChanged(null, EventArgs.Empty); |
| 59 | ············} |
| 60 | ············else |
| 61 | ················UnmapClass(null, EventArgs.Empty); |
| 62 | ········} |
| 63 | |
| 64 | ········public void UnmapIntermediateClass(Object sender, EventArgs e) |
| 65 | ········{ |
| 66 | ············ApplicationController.Instance.UnmapIntermediateClass(this); |
| 67 | ············this.Table.MappingType = TableMappingType.NotMapped; |
| 68 | ············UnmapClass(sender, e); |
| 69 | ········} |
| 70 | |
| 71 | ········private void MapClass(bool checkForPrimaryKey) |
| 72 | ········{ |
| 73 | ············this.Table.Namespace = ApplicationController.Instance.AssemblyNode.Assembly.RootNamespace; |
| 74 | |
| 75 | ············this.Table.MappingType = TableMappingType.MappedAsClass; |
| 76 | ············this.OnIsMappedChanged(null, EventArgs.Empty); |
| 77 | ············this.Table.PrimaryKey = string.Empty; |
| 78 | ············foreach(NDOTreeNode tn in this.Nodes) |
| 79 | ············{ |
| 80 | ················ColumnNode cn = tn as ColumnNode; |
| 81 | ················if (cn != null) |
| 82 | ················{ |
| 83 | ····················if (!cn.IsPrimary) |
| 84 | ························cn.IsMapped = true; |
| 85 | ····················else |
| 86 | ····················{ |
| 87 | ························this.Table.PrimaryKey = cn.Name; |
| 88 | ························if (cn.Column.Type == "System.String") |
| 89 | ························{ |
| 90 | ····························Assembly ass = ApplicationController.Instance.AssemblyNode.Assembly; |
| 91 | ····························if (ass.MapStringsAsGuids) |
| 92 | ····························{ |
| 93 | ································this.Table.NdoOidType = "Guid"; |
| 94 | ····························} |
| 95 | ····························else |
| 96 | ····························{ |
| 97 | ································if (ass.UseClassField) |
| 98 | ····································cn.IsMapped = true; |
| 99 | ································else |
| 100 | ····································this.Table.NdoOidType = "string"; |
| 101 | ····························} |
| 102 | ························} |
| 103 | ····················} |
| 104 | ················} |
| 105 | ············} |
| 106 | |
| 107 | ············if (checkForPrimaryKey && this.Table.PrimaryKey == string.Empty) |
| 108 | ············{ |
| 109 | ················ClassGenerator.MapClassWizard.MapClassWizard wiz = new ClassGenerator.MapClassWizard.MapClassWizard(this); |
| 110 | ················DialogResult dr = wiz.ShowDialog(); |
| 111 | ················if (dr == DialogResult.OK && wiz.Result != null) |
| 112 | ················{ |
| 113 | ····················if (wiz.Result.IsMapped) |
| 114 | ························wiz.Result.ChangeMapping(null, EventArgs.Empty); |
| 115 | ····················wiz.Result.ChangePrimary(null, EventArgs.Empty); |
| 116 | ················} |
| 117 | ············}············ |
| 118 | ············ApplicationController.Instance.MappingChanged(); |
| 119 | ········} |
| 120 | |
| 121 | ········public void MapClass(Object sender, EventArgs e) |
| 122 | ········{ |
| 123 | ············if (this.Table.MappingType != TableMappingType.NotMapped) |
| 124 | ····················return; |
| 125 | ············MapClass(true); |
| 126 | ········} |
| 127 | |
| 128 | ········void AddPrimaryKeyColumn(Object sender, EventArgs e) |
| 129 | ········{ |
| 130 | ············ApplicationController.Instance.AddPrimaryKeyColum(this); |
| 131 | ········} |
| 132 | |
| 133 | ········public void UnmapClass(Object sender, EventArgs e) |
| 134 | ········{ |
| 135 | ············if (this.Table.MappingType == TableMappingType.NotMapped) |
| 136 | ················return; |
| 137 | ············this.Table.MappingType = TableMappingType.NotMapped; |
| 138 | ············this.OnIsMappedChanged(null, EventArgs.Empty); |
| 139 | ············ArrayList nodesToDelete = new ArrayList(); |
| 140 | ············foreach(NDOTreeNode tn in this.Nodes) |
| 141 | ············{ |
| 142 | ················ColumnNode cn; |
| 143 | ················RelationNode rn; |
| 144 | ················if ((cn = tn as ColumnNode) != null) |
| 145 | ····················cn.IsMapped = false; |
| 146 | ················else if ((rn = tn as RelationNode) != null) |
| 147 | ····················nodesToDelete.Add(rn); |
| 148 | ············} |
| 149 | ············foreach(RelationNode rn2 in nodesToDelete) |
| 150 | ················ApplicationController.Instance.DeleteRelation(rn2); |
| 151 | ············ApplicationController.Instance.MappingChanged(); |
| 152 | ········} |
| 153 | |
| 154 | ········public void Skip( Object sender, EventArgs e ) |
| 155 | ········{ |
| 156 | ············this.Table.Skipped = true; |
| 157 | ········} |
| 158 | |
| 159 | ········public void Unskip( Object sender, EventArgs e ) |
| 160 | ········{ |
| 161 | ············this.Table.Skipped = false; |
| 162 | ········} |
| 163 | |
| 164 | |
| 165 | ········public override System.Windows.Forms.ContextMenu GetContextMenu() |
| 166 | ········{ |
| 167 | ············ContextMenu menu = new ContextMenu(); |
| 168 | |
| 169 | ············if (!(((TreeNode)this).Parent is DatabaseNode))··// This is the case, if the table is mapped as intermediate table. |
| 170 | ················return menu; |
| 171 | |
| 172 | ············if (this.Table.MappingType != TableMappingType.NotMapped) |
| 173 | ············{ |
| 174 | ················menu.MenuItems.Add(new MenuItem("Unmap Table", new EventHandler(this.UnmapClass))); |
| 175 | ············} |
| 176 | ············else |
| 177 | ············{ |
| 178 | ················menu.MenuItems.Add(new MenuItem("Add Primary Key Column", new EventHandler(this.AddPrimaryKeyColumn))); |
| 179 | ················menu.MenuItems.Add(new MenuItem("Map Table to Class", new EventHandler(this.MapClass))); |
| 180 | ················if (this.Nodes.Count >= 2 && ((DatabaseNode)this.Parent).Classes.Count >= 2) |
| 181 | ················{ |
| 182 | ····················menu.MenuItems.Add(new MenuItem("Define Table as Intermediate Table", new EventHandler(this.MapIntermediateTable))); |
| 183 | ····················menu.MenuItems.Add(new MenuItem("Map Table as Intermediate Class", new EventHandler(this.MapIntermediateClass))); |
| 184 | ················} |
| 185 | ············} |
| 186 | |
| 187 | ············if ( ((DatabaseNode) this.Parent).Database.IsXmlSchema ) |
| 188 | ············{ |
| 189 | ················if (this.Table.Skipped) |
| 190 | ····················menu.MenuItems.Add(new MenuItem("Don't skip this Xml Node", new EventHandler(this.Unskip))); |
| 191 | ················else |
| 192 | ····················menu.MenuItems.Add(new MenuItem("Skip this Xml Node", new EventHandler(this.Skip))); |
| 193 | ············} |
| 194 | ············return menu; |
| 195 | ········} |
| 196 | |
| 197 | ········private string GeneratePkName( DataTable dt ) |
| 198 | ········{ |
| 199 | ············int i = 0; |
| 200 | ············string columnName = dt.TableName + "_Id"; |
| 201 | ············while(dt.Columns.Contains(columnName)) |
| 202 | ············{ |
| 203 | ················i++; |
| 204 | ················columnName = dt.TableName + "_Id" + i; |
| 205 | ············} |
| 206 | ············return columnName; |
| 207 | ········} |
| 208 | |
| 209 | ········public TableNode(DataTable dt, DatabaseNode parent, string ownerName, IDbConnection conn, IProvider provider) : base(dt.TableName, parent) |
| 210 | ········{ |
| 211 | ············string tableName = dt.TableName; |
| 212 | |
| 213 | ············SetImageIndex(15); // unmapped |
| 214 | ············this.myObject = new Table(tableName, string.Empty, (string)dt.ExtendedProperties["summary"]); |
| 215 | ············this.Table.OnIsMappedChanged += new EventHandler(OnIsMappedChanged); |
| 216 | |
| 217 | #if DontUseDataSets |
| 218 | ············DataTable dt = null; |
| 219 | |
| 220 | ············if ( !parent.Database.IsXmlSchema ) |
| 221 | ············{ |
| 222 | ················string sql; |
| 223 | |
| 224 | ················if ( ownerName != null && ownerName.Trim() != "" ) |
| 225 | ················{ |
| 226 | ····················sql = "SELECT * FROM " + provider.GetQuotedName( ownerName ) + "." + provider.GetQuotedName( tableName ); |
| 227 | ················} |
| 228 | ················else |
| 229 | ················{ |
| 230 | ····················sql = "SELECT * FROM " + provider.GetQuotedName( tableName ); |
| 231 | ················} |
| 232 | |
| 233 | ················DataSet ds = new DataSet(); |
| 234 | ················IDbCommand cmd = provider.NewSqlCommand( conn ); |
| 235 | ················cmd.CommandText = sql; |
| 236 | ················IDataAdapter da = provider.NewDataAdapter( cmd, null, null, null ); |
| 237 | |
| 238 | ················da.FillSchema( ds, SchemaType.Source ); |
| 239 | ················dt = ds.Tables[0]; |
| 240 | ············} |
| 241 | ············else |
| 242 | ············{ |
| 243 | ················dt = parent.Database.DataSet.Tables[tableName]; |
| 244 | #endif |
| 245 | ············if ( parent.Database.IsXmlSchema && dt.PrimaryKey.Length··== 0 ) |
| 246 | ············{ |
| 247 | ················string pkColumnName = GeneratePkName( dt ); |
| 248 | ················DataColumn pkColumn = dt.Columns.Add( pkColumnName, typeof( int ) ); |
| 249 | ················dt.PrimaryKey = new DataColumn[] { pkColumn }; |
| 250 | ············} |
| 251 | ············ |
| 252 | ············foreach ( DataColumn column in dt.Columns ) |
| 253 | ············{ |
| 254 | ················bool isPrimary = false; |
| 255 | ················bool isAutoIncremented = false; |
| 256 | ················foreach ( DataColumn pkColumn in dt.PrimaryKey ) |
| 257 | ················{ |
| 258 | ····················if ( pkColumn == column ) |
| 259 | ····················{ |
| 260 | ························isPrimary = true; |
| 261 | ························isAutoIncremented = pkColumn.AutoIncrement; |
| 262 | ····················} |
| 263 | ················} |
| 264 | ················ColumnNode cn = new ColumnNode( column.ColumnName, isPrimary, isAutoIncremented, column.DataType.ToString(), (string)column.ExtendedProperties["summary"], this ); |
| 265 | ················if(parent.Database.IsXmlSchema && column.ExtendedProperties.Contains("schemaType")) |
| 266 | ····················cn.UserData.Add("schemaType", "Element"); |
| 267 | |
| 268 | ················cn.IsPrimaryChanged += new EventHandler( OnIsPrimaryChanged ); |
| 269 | ················this.Nodes.Add( cn ); |
| 270 | ············} |
| 271 | ········} |
| 272 | |
| 273 | |
| 274 | ········private void OnIsMappedChanged(object sender, EventArgs e) |
| 275 | ········{ |
| 276 | ············if (this.Table.MappingType == TableMappingType.MappedAsClass) |
| 277 | ················SetImageIndex(14); |
| 278 | ············else if (this.Table.MappingType == TableMappingType.MappedAsIntermediateClass) |
| 279 | ················SetImageIndex(19); |
| 280 | ············else |
| 281 | ················SetImageIndex(15); // Unmapped. IntermediateTable shouldn't occur |
| 282 | ········}········ |
| 283 | |
| 284 | |
| 285 | ········[Browsable(false)] |
| 286 | ········public bool HasPrimary |
| 287 | ········{ |
| 288 | ············get { return this.Table.PrimaryKey != string.Empty; } |
| 289 | ········} |
| 290 | |
| 291 | ········public Table Table |
| 292 | ········{ |
| 293 | ············get { return this.myObject as Table; } |
| 294 | ········} |
| 295 | |
| 296 | ········private void OnIsPrimaryChanged(object sender, EventArgs e) |
| 297 | ········{ |
| 298 | ············ColumnNode cn = sender as ColumnNode; |
| 299 | ············if (cn == null) |
| 300 | ················return; |
| 301 | ············if (cn.IsPrimary) |
| 302 | ············{ |
| 303 | ················this.Table.PrimaryKey = cn.Text; |
| 304 | ············} |
| 305 | ············if (cn.Name == this.Table.PrimaryKey) |
| 306 | ············{ |
| 307 | ················if (!cn.IsPrimary) |
| 308 | ················{ |
| 309 | ····················this.Table.PrimaryKey = string.Empty; |
| 310 | ················} |
| 311 | ············} |
| 312 | ········} |
| 313 | |
| 314 | ········public IList ColumnNodes |
| 315 | ········{ |
| 316 | ············get |
| 317 | ············{ |
| 318 | ················IList result = new ArrayList(); |
| 319 | ················foreach(NDOTreeNode tn in Nodes) |
| 320 | ················{ |
| 321 | ····················if (tn is ColumnNode) |
| 322 | ························result.Add(tn); |
| 323 | ················} |
| 324 | ················return result; |
| 325 | ············} |
| 326 | ········} |
| 327 | |
| 328 | ········public IList RelationNodes |
| 329 | ········{ |
| 330 | ············get |
| 331 | ············{ |
| 332 | ················IList result = new ArrayList(); |
| 333 | ················foreach(NDOTreeNode tn in Nodes) |
| 334 | ················{ |
| 335 | ····················if (tn is RelationNode) |
| 336 | ························result.Add(tn); |
| 337 | ················} |
| 338 | ················return result; |
| 339 | ············} |
| 340 | ········} |
| 341 | |
| 342 | ········public RelationNode FindRelationNode(string text, string relatedTableName) |
| 343 | ········{ |
| 344 | ············foreach(NDOTreeNode tn in Nodes) |
| 345 | ············{ |
| 346 | ················RelationNode rn = tn as RelationNode; |
| 347 | ················if (rn == null) |
| 348 | ····················continue; |
| 349 | ················if (rn.Text == text && rn.RelatedTableNode.Text == relatedTableName) |
| 350 | ····················return rn; |
| 351 | ············} |
| 352 | ············return null; |
| 353 | ········} |
| 354 | |
| 355 | ········public override string ToString() |
| 356 | ········{ |
| 357 | ············return this.Table.Name; |
| 358 | ········} |
| 359 | |
| 360 | ········string[] dualKeyRelations = new string[2]; |
| 361 | ········public string[] DualKeyRelations |
| 362 | ········{ |
| 363 | ············get { return dualKeyRelations; } |
| 364 | ········} |
| 365 | |
| 366 | ········protected override void CalculateIndex() |
| 367 | ········{ |
| 368 | ············OnIsMappedChanged(null, EventArgs.Empty); |
| 369 | ········} |
| 370 | |
| 371 | ········public override void ToXml(System.Xml.XmlElement element) |
| 372 | ········{ |
| 373 | ············base.ToXml (element); |
| 374 | ············for (int i = 0; i < 2; i++) |
| 375 | ············{ |
| 376 | ················element.SetAttribute("DualKeyRelations" + i, this.dualKeyRelations[i]); |
| 377 | ············} |
| 378 | ········} |
| 379 | |
| 380 | ········public override void FromXml(System.Xml.XmlElement element) |
| 381 | ········{ |
| 382 | ············base.FromXml (element); |
| 383 | ············for (int i = 0; i < 2; i++) |
| 384 | ············{ |
| 385 | ················this.dualKeyRelations[i] = element.Attributes["DualKeyRelations" + i].Value; |
| 386 | ············} |
| 387 | ············//TODO: Database refresh |
| 388 | ········} |
| 389 | |
| 390 | ········public ColumnNode this[string name] |
| 391 | ········{ |
| 392 | ············get |
| 393 | ············{ |
| 394 | ················foreach(ColumnNode cn in this.ColumnNodes) |
| 395 | ····················if (cn.Text == name) |
| 396 | ························return cn; |
| 397 | ················return null; |
| 398 | ············} |
| 399 | ········} |
| 400 | |
| 401 | |
| 402 | ········/// <summary> |
| 403 | ········/// Used for serialization only |
| 404 | ········/// </summary> |
| 405 | ········public TableNode() |
| 406 | ········{ |
| 407 | ········} |
| 408 | |
| 409 | |
| 410 | #if SoapSerialisation |
| 411 | ········protected TableNode(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) |
| 412 | ········{ |
| 413 | ········} |
| 414 | #endif |
| 415 | |
| 416 | ····} |
| 417 | } |
| 418 |
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.Collections; |
| 25 | using System.Diagnostics; |
| 26 | using System.Windows.Forms; |
| 27 | using System.ComponentModel; |
| 28 | using System.Data; |
| 29 | using NDO; |
| 30 | using NDOInterfaces; |
| 31 | |
| 32 | namespace ClassGenerator |
| 33 | { |
| 34 | ····/// <summary> |
| 35 | ····/// Zusammenfassung für TableNode. |
| 36 | ····/// </summary> |
| 37 | ····[Serializable] |
| 38 | #if DEBUG |
| 39 | ····public class TableNode : NDOTreeNode |
| 40 | #else |
| 41 | ····internal class TableNode : NDOTreeNode |
| 42 | #endif |
| 43 | ····{ |
| 44 | ········public void MapIntermediateTable(Object sender, EventArgs e) |
| 45 | ········{ |
| 46 | ············ApplicationController.Instance.MapIntermediateTable(this); |
| 47 | ········} |
| 48 | ········public void MapIntermediateClass(Object sender, EventArgs e) |
| 49 | ········{ |
| 50 | ················if (this.Table.MappingType != TableMappingType.NotMapped) |
| 51 | ························return; |
| 52 | |
| 53 | ············MapClass(false); |
| 54 | ············ |
| 55 | ············if (ApplicationController.Instance.MapIntermediateClass(this)) |
| 56 | ············{ |
| 57 | ················this.Table.MappingType = TableMappingType.MappedAsIntermediateClass; |
| 58 | ················this.OnIsMappedChanged(null, EventArgs.Empty); |
| 59 | ············} |
| 60 | ············else |
| 61 | ················UnmapClass(null, EventArgs.Empty); |
| 62 | ········} |
| 63 | |
| 64 | ········public void UnmapIntermediateClass(Object sender, EventArgs e) |
| 65 | ········{ |
| 66 | ············ApplicationController.Instance.UnmapIntermediateClass(this); |
| 67 | ············this.Table.MappingType = TableMappingType.NotMapped; |
| 68 | ············UnmapClass(sender, e); |
| 69 | ········} |
| 70 | |
| 71 | ········private void MapClass(bool checkForPrimaryKey) |
| 72 | ········{ |
| 73 | ············this.Table.Namespace = ApplicationController.Instance.AssemblyNode.Assembly.RootNamespace; |
| 74 | |
| 75 | ············this.Table.MappingType = TableMappingType.MappedAsClass; |
| 76 | ············this.OnIsMappedChanged(null, EventArgs.Empty); |
| 77 | ············this.Table.PrimaryKey = string.Empty; |
| 78 | ············foreach(NDOTreeNode tn in this.Nodes) |
| 79 | ············{ |
| 80 | ················ColumnNode cn = tn as ColumnNode; |
| 81 | ················if (cn != null) |
| 82 | ················{ |
| 83 | ····················if (!cn.IsPrimary) |
| 84 | ························cn.IsMapped = true; |
| 85 | ····················else |
| 86 | ····················{ |
| 87 | ························this.Table.PrimaryKey = cn.Name; |
| 88 | ························if (cn.Column.Type == "System.String") |
| 89 | ························{ |
| 90 | ····························Assembly ass = ApplicationController.Instance.AssemblyNode.Assembly; |
| 91 | ····························if (ass.MapStringsAsGuids) |
| 92 | ····························{ |
| 93 | ································this.Table.NdoOidType = "Guid"; |
| 94 | ····························} |
| 95 | ····························else |
| 96 | ····························{ |
| 97 | ································if (ass.UseClassField) |
| 98 | ····································cn.IsMapped = true; |
| 99 | ································else |
| 100 | ····································this.Table.NdoOidType = "string"; |
| 101 | ····························} |
| 102 | ························} |
| 103 | ····················} |
| 104 | ················} |
| 105 | ············} |
| 106 | |
| 107 | ············if (checkForPrimaryKey && this.Table.PrimaryKey == string.Empty) |
| 108 | ············{ |
| 109 | ················ClassGenerator.MapClassWizard.MapClassWizard wiz = new ClassGenerator.MapClassWizard.MapClassWizard(this); |
| 110 | ················DialogResult dr = wiz.ShowDialog(); |
| 111 | ················if (dr == DialogResult.OK && wiz.Result != null) |
| 112 | ················{ |
| 113 | ····················if (wiz.Result.IsMapped) |
| 114 | ························wiz.Result.ChangeMapping(null, EventArgs.Empty); |
| 115 | ····················wiz.Result.ChangePrimary(null, EventArgs.Empty); |
| 116 | ················} |
| 117 | ············}············ |
| 118 | ············ApplicationController.Instance.MappingChanged(); |
| 119 | ········} |
| 120 | |
| 121 | ········public void MapClass(Object sender, EventArgs e) |
| 122 | ········{ |
| 123 | ············if (this.Table.MappingType != TableMappingType.NotMapped) |
| 124 | ····················return; |
| 125 | ············MapClass(true); |
| 126 | ········} |
| 127 | |
| 128 | ········void AddPrimaryKeyColumn(Object sender, EventArgs e) |
| 129 | ········{ |
| 130 | ············ApplicationController.Instance.AddPrimaryKeyColum(this); |
| 131 | ········} |
| 132 | |
| 133 | ········public void UnmapClass(Object sender, EventArgs e) |
| 134 | ········{ |
| 135 | ············if (this.Table.MappingType == TableMappingType.NotMapped) |
| 136 | ················return; |
| 137 | ············this.Table.MappingType = TableMappingType.NotMapped; |
| 138 | ············this.OnIsMappedChanged(null, EventArgs.Empty); |
| 139 | ············ArrayList nodesToDelete = new ArrayList(); |
| 140 | ············foreach(NDOTreeNode tn in this.Nodes) |
| 141 | ············{ |
| 142 | ················ColumnNode cn; |
| 143 | ················RelationNode rn; |
| 144 | ················if ((cn = tn as ColumnNode) != null) |
| 145 | ····················cn.IsMapped = false; |
| 146 | ················else if ((rn = tn as RelationNode) != null) |
| 147 | ····················nodesToDelete.Add(rn); |
| 148 | ············} |
| 149 | ············foreach(RelationNode rn2 in nodesToDelete) |
| 150 | ················ApplicationController.Instance.DeleteRelation(rn2); |
| 151 | ············ApplicationController.Instance.MappingChanged(); |
| 152 | ········} |
| 153 | |
| 154 | ········public void Skip( Object sender, EventArgs e ) |
| 155 | ········{ |
| 156 | ············this.Table.Skipped = true; |
| 157 | ········} |
| 158 | |
| 159 | ········public void Unskip( Object sender, EventArgs e ) |
| 160 | ········{ |
| 161 | ············this.Table.Skipped = false; |
| 162 | ········} |
| 163 | |
| 164 | |
| 165 | ········public override System.Windows.Forms.ContextMenu GetContextMenu() |
| 166 | ········{ |
| 167 | ············ContextMenu menu = new ContextMenu(); |
| 168 | |
| 169 | ············if (!(((TreeNode)this).Parent is DatabaseNode))··// This is the case, if the table is mapped as intermediate table. |
| 170 | ················return menu; |
| 171 | |
| 172 | ············if (this.Table.MappingType != TableMappingType.NotMapped) |
| 173 | ············{ |
| 174 | ················menu.MenuItems.Add(new MenuItem("Unmap Table", new EventHandler(this.UnmapClass))); |
| 175 | ············} |
| 176 | ············else |
| 177 | ············{ |
| 178 | ················menu.MenuItems.Add(new MenuItem("Add Primary Key Column", new EventHandler(this.AddPrimaryKeyColumn))); |
| 179 | ················menu.MenuItems.Add(new MenuItem("Map Table to Class", new EventHandler(this.MapClass))); |
| 180 | ················if (this.Nodes.Count >= 2 && ((DatabaseNode)this.Parent).Classes.Count >= 2) |
| 181 | ················{ |
| 182 | ····················menu.MenuItems.Add(new MenuItem("Define Table as Intermediate Table", new EventHandler(this.MapIntermediateTable))); |
| 183 | ····················menu.MenuItems.Add(new MenuItem("Map Table as Intermediate Class", new EventHandler(this.MapIntermediateClass))); |
| 184 | ················} |
| 185 | ············} |
| 186 | |
| 187 | ············if ( ((DatabaseNode) this.Parent).Database.IsXmlSchema ) |
| 188 | ············{ |
| 189 | ················if (this.Table.Skipped) |
| 190 | ····················menu.MenuItems.Add(new MenuItem("Don't skip this Xml Node", new EventHandler(this.Unskip))); |
| 191 | ················else |
| 192 | ····················menu.MenuItems.Add(new MenuItem("Skip this Xml Node", new EventHandler(this.Skip))); |
| 193 | ············} |
| 194 | ············return menu; |
| 195 | ········} |
| 196 | |
| 197 | ········private string GeneratePkName( DataTable dt ) |
| 198 | ········{ |
| 199 | ············int i = 0; |
| 200 | ············string columnName = dt.TableName + "_Id"; |
| 201 | ············while(dt.Columns.Contains(columnName)) |
| 202 | ············{ |
| 203 | ················i++; |
| 204 | ················columnName = dt.TableName + "_Id" + i; |
| 205 | ············} |
| 206 | ············return columnName; |
| 207 | ········} |
| 208 | |
| 209 | ········public TableNode(DataTable dt, DatabaseNode parent, string ownerName, IDbConnection conn, IProvider provider) : base(dt.TableName, parent) |
| 210 | ········{ |
| 211 | ············string tableName = dt.TableName; |
| 212 | |
| 213 | ············SetImageIndex(15); // unmapped |
| 214 | ············this.myObject = new Table(tableName, string.Empty, (string)dt.ExtendedProperties["summary"]); |
| 215 | ············this.Table.OnIsMappedChanged += new EventHandler(OnIsMappedChanged); |
| 216 | |
| 217 | ············if ( parent.Database.IsXmlSchema && dt.PrimaryKey.Length··== 0 ) |
| 218 | ············{ |
| 219 | ················string pkColumnName = GeneratePkName( dt ); |
| 220 | ················DataColumn pkColumn = dt.Columns.Add( pkColumnName, typeof( int ) ); |
| 221 | ················dt.PrimaryKey = new DataColumn[] { pkColumn }; |
| 222 | ············} |
| 223 | ············ |
| 224 | ············foreach ( DataColumn column in dt.Columns ) |
| 225 | ············{ |
| 226 | ················bool isPrimary = false; |
| 227 | ················bool isAutoIncremented = false; |
| 228 | ················foreach ( DataColumn pkColumn in dt.PrimaryKey ) |
| 229 | ················{ |
| 230 | ····················if ( pkColumn == column ) |
| 231 | ····················{ |
| 232 | ························isPrimary = true; |
| 233 | ························isAutoIncremented = pkColumn.AutoIncrement; |
| 234 | ····················} |
| 235 | ················} |
| 236 | ················ColumnNode cn = new ColumnNode( column.ColumnName, isPrimary, isAutoIncremented, column.DataType.ToString(), (string)column.ExtendedProperties["summary"], this ); |
| 237 | ················if(parent.Database.IsXmlSchema && column.ExtendedProperties.Contains("schemaType")) |
| 238 | ····················cn.UserData.Add("schemaType", "Element"); |
| 239 | |
| 240 | ················cn.IsPrimaryChanged += new EventHandler( OnIsPrimaryChanged ); |
| 241 | ················this.Nodes.Add( cn ); |
| 242 | ············} |
| 243 | ········} |
| 244 | |
| 245 | |
| 246 | ········private void OnIsMappedChanged(object sender, EventArgs e) |
| 247 | ········{ |
| 248 | ············if (this.Table.MappingType == TableMappingType.MappedAsClass) |
| 249 | ················SetImageIndex(14); |
| 250 | ············else if (this.Table.MappingType == TableMappingType.MappedAsIntermediateClass) |
| 251 | ················SetImageIndex(19); |
| 252 | ············else |
| 253 | ················SetImageIndex(15); // Unmapped. IntermediateTable shouldn't occur |
| 254 | ········}········ |
| 255 | |
| 256 | |
| 257 | ········[Browsable(false)] |
| 258 | ········public bool HasPrimary |
| 259 | ········{ |
| 260 | ············get { return this.Table.PrimaryKey != string.Empty; } |
| 261 | ········} |
| 262 | |
| 263 | ········public Table Table |
| 264 | ········{ |
| 265 | ············get { return this.myObject as Table; } |
| 266 | ········} |
| 267 | |
| 268 | ········private void OnIsPrimaryChanged(object sender, EventArgs e) |
| 269 | ········{ |
| 270 | ············ColumnNode cn = sender as ColumnNode; |
| 271 | ············if (cn == null) |
| 272 | ················return; |
| 273 | ············if (cn.IsPrimary) |
| 274 | ············{ |
| 275 | ················this.Table.PrimaryKey = cn.Text; |
| 276 | ············} |
| 277 | ············if (cn.Name == this.Table.PrimaryKey) |
| 278 | ············{ |
| 279 | ················if (!cn.IsPrimary) |
| 280 | ················{ |
| 281 | ····················this.Table.PrimaryKey = string.Empty; |
| 282 | ················} |
| 283 | ············} |
| 284 | ········} |
| 285 | |
| 286 | ········public IList ColumnNodes |
| 287 | ········{ |
| 288 | ············get |
| 289 | ············{ |
| 290 | ················IList result = new ArrayList(); |
| 291 | ················foreach(NDOTreeNode tn in Nodes) |
| 292 | ················{ |
| 293 | ····················if (tn is ColumnNode) |
| 294 | ························result.Add(tn); |
| 295 | ················} |
| 296 | ················return result; |
| 297 | ············} |
| 298 | ········} |
| 299 | |
| 300 | ········public IList RelationNodes |
| 301 | ········{ |
| 302 | ············get |
| 303 | ············{ |
| 304 | ················IList result = new ArrayList(); |
| 305 | ················foreach(NDOTreeNode tn in Nodes) |
| 306 | ················{ |
| 307 | ····················if (tn is RelationNode) |
| 308 | ························result.Add(tn); |
| 309 | ················} |
| 310 | ················return result; |
| 311 | ············} |
| 312 | ········} |
| 313 | |
| 314 | ········public RelationNode FindRelationNode(string text, string relatedTableName) |
| 315 | ········{ |
| 316 | ············foreach(NDOTreeNode tn in Nodes) |
| 317 | ············{ |
| 318 | ················RelationNode rn = tn as RelationNode; |
| 319 | ················if (rn == null) |
| 320 | ····················continue; |
| 321 | ················if (rn.Text == text && rn.RelatedTableNode.Text == relatedTableName) |
| 322 | ····················return rn; |
| 323 | ············} |
| 324 | ············return null; |
| 325 | ········} |
| 326 | |
| 327 | ········public override string ToString() |
| 328 | ········{ |
| 329 | ············return this.Table.Name; |
| 330 | ········} |
| 331 | |
| 332 | ········string[] dualKeyRelations = new string[2]; |
| 333 | ········public string[] DualKeyRelations |
| 334 | ········{ |
| 335 | ············get { return dualKeyRelations; } |
| 336 | ········} |
| 337 | |
| 338 | ········protected override void CalculateIndex() |
| 339 | ········{ |
| 340 | ············OnIsMappedChanged(null, EventArgs.Empty); |
| 341 | ········} |
| 342 | |
| 343 | ········public override void ToXml(System.Xml.XmlElement element) |
| 344 | ········{ |
| 345 | ············base.ToXml (element); |
| 346 | ············for (int i = 0; i < 2; i++) |
| 347 | ············{ |
| 348 | ················element.SetAttribute("DualKeyRelations" + i, this.dualKeyRelations[i]); |
| 349 | ············} |
| 350 | ········} |
| 351 | |
| 352 | ········public override void FromXml(System.Xml.XmlElement element) |
| 353 | ········{ |
| 354 | ············base.FromXml (element); |
| 355 | ············for (int i = 0; i < 2; i++) |
| 356 | ············{ |
| 357 | ················this.dualKeyRelations[i] = element.Attributes["DualKeyRelations" + i].Value; |
| 358 | ············} |
| 359 | ············//TODO: Database refresh |
| 360 | ········} |
| 361 | |
| 362 | ········public ColumnNode this[string name] |
| 363 | ········{ |
| 364 | ············get |
| 365 | ············{ |
| 366 | ················foreach(ColumnNode cn in this.ColumnNodes) |
| 367 | ····················if (cn.Text == name) |
| 368 | ························return cn; |
| 369 | ················return null; |
| 370 | ············} |
| 371 | ········} |
| 372 | |
| 373 | |
| 374 | ········/// <summary> |
| 375 | ········/// Used for serialization only |
| 376 | ········/// </summary> |
| 377 | ········public TableNode() |
| 378 | ········{ |
| 379 | ········} |
| 380 | |
| 381 | |
| 382 | #if SoapSerialisation |
| 383 | ········protected TableNode(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) |
| 384 | ········{ |
| 385 | ········} |
| 386 | #endif |
| 387 | |
| 388 | ····} |
| 389 | } |
| 390 |