Datei: ClassGenerator/Nodes/DatabaseNode.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.Collections.Generic;
26 using System.ComponentModel;
27 using NDO;
28 using NDOInterfaces;
29 using System.Data;
30 using System.Windows.Forms;
31 using ClassGenerator.AssemblyWizard;
32 using System.Diagnostics;
33
34 namespace ClassGenerator
35 {
36 ····[Serializable]
37 #if DEBUG····
38 ····public class DatabaseNode : NDOTreeNode
39 #else
40 ····internal class DatabaseNode : NDOTreeNode
41 #endif
42 ····{
43 ········public ArrayList TableNodes
44 ········{
45 ············get
46 ············{
47 ················ArrayList al = new ArrayList();
48 ················foreach(NDOTreeNode trn in this.Nodes)
49 ················{
50 ····················TableNode tn = trn as TableNode;
51 ····················if (tn == null)
52 ························continue;
53 ····················al.Add(tn);
54 ················}
55 ················return al;
56 ············}
57 ········}
58
59 ········public ArrayList UnmappedTableNodes
60 ········{
61 ············get
62 ············{
63 ················ArrayList al = new ArrayList();
64 ················foreach(NDOTreeNode trn in this.Nodes)
65 ················{
66 ····················TableNode tn = trn as TableNode;
67 ····················if (tn == null)
68 ························continue;
69 ····················if (tn.Table.MappingType == TableMappingType.NotMapped)
70 ························al.Add(tn);
71 ················}
72 ················return al;
73 ············}
74 ········}
75
76 ········public ArrayList Classes
77 ········{
78 ············get
79 ············{
80 ················ArrayList al = new ArrayList();
81 ················foreach(NDOTreeNode trn in this.Nodes)
82 ················{
83 ····················TableNode tn = trn as TableNode;
84 ····················if (tn == null)
85 ························continue;
86 ····················if (tn.Table.MappingType == TableMappingType.MappedAsClass)
87 ························al.Add(tn);
88 ················}
89 ················return al;
90 ············}
91 ········}
92
93 ········public DatabaseNode(AssemblyWizModel model) : base("Database", null)
94 ········{
95 ············this.myObject = new Database(model);············
96 ············Init();
97 ········}
98
99 ········public DatabaseNode(Database db) : base("Database", null)
100 ········{
101 ············this.myObject = db;
102 ············Init();
103 ········}
104
105 ········void Init()
106 ········{
107 #if DontUseDataSets
108 ············if ( !this.Database.IsXmlSchema )
109 ············{
110 ················IProvider provider = NDOProviderFactory.Instance[Database.ConnectionType];
111 ················IDbConnection conn = null;
112 ················try
113 ················{
114 ····················conn = provider.NewConnection( Database.ConnectionString );
115 ················}
116 ················catch
117 ················{
118 ····················conn = null;//MessageBox.Show("Can't open connection '" + Database.ConnectionString + '\'');
119 ················}
120 ················if ( conn == null ) // can be null out of several reasons
121 ················{
122 ····················MessageBox.Show( "Can't open connection '" + Database.ConnectionString + '\'' );
123 ····················return;
124 ················}
125 ················string[] tnames;
126 ················try
127 ················{
128 ····················conn.Open();
129 ················}
130 ················catch ( Exception ex )
131 ················{
132 ····················MessageBox.Show( "Can't open connection '" + Database.ConnectionString + "'.\n" + ex.Message );
133 ····················return;
134 ················}
135 ················try
136 ················{
137 ····················if ( Database.OwnerName != null && Database.OwnerName != string.Empty )
138 ························tnames = provider.GetTableNames( conn, Database.OwnerName );
139 ····················else
140 ························tnames = provider.GetTableNames( conn );
141 ····················foreach ( string s in tnames )
142 ····················{
143 ························this.Nodes.Add( new TableNode( s, this, Database.OwnerName, conn, provider ) );
144 ····················}
145 ················}
146 ················catch ( Exception ex )
147 ················{
148 ····················MessageBox.Show( "Error while collecting database schema information: " + ex.Message );
149 ················}
150 ················if ( conn.State != ConnectionState.Closed )
151 ····················conn.Close();
152 ············}
153 ············else
154 ············{
155 #endif
156
157 ················List<TableNode> l = new List<TableNode>();
158 ················foreach ( DataTable dt in this.Database.DataSet.Tables )
159 ················{
160 ····················l.Add( new TableNode( dt, this, string.Empty, null, null) );
161 ················}
162 ················l.Sort(delegate(TableNode tn1, TableNode tn2)
163 ················{
164 ····················return tn1.Name.CompareTo(tn2.Name);
165 ················});
166 ················foreach (TableNode tn in l)
167 ····················this.Nodes.Add(tn);
168 ················// Store the parent nodes, so that we can navigate through the schema hierarchy.
169 ················// We need this to generate the correct XPath, if we skip an Xml Element
170 ················//foreach ( DataRelation dr in this.Database.DataSet.Relations )
171 ················//{
172 ················//····TableNode parentNode = FindTableNode(dr.ParentTable.TableName, true);
173 ················//····TableNode childNode = FindTableNode(dr.ChildTable.TableName, true);
174 ················//····childNode.UserData.Add( "parentnode", parentNode );
175 ················//}
176 //············}
177 ············CalculateIndex();
178 ········}
179
180 ········public TableNode this[string name]
181 ········{
182 ············get
183 ············{
184 ················foreach(TableNode tn in this.TableNodes)
185 ····················if (tn.Text == name)
186 ························return tn;
187 ················return null;
188 ············}
189 ········}
190
191
192 ········public void MapXmlSchema()
193 ········{
194 ············foreach(TableNode tn in this.TableNodes)
195 ················tn.MapClass(null, EventArgs.Empty);
196
197 ············DataSet ds = this.Database.DataSet;
198 ············foreach ( DataRelation dr in ds.Relations )
199 ············{
200 ················string xpath = (string)dr.ExtendedProperties["xpath"];
201 ················ApplicationController.Instance.MakeForeignKey(dr);
202 ············}
203 ········}
204
205 ········public TableNode FindTableNode(string tableName, bool throwIfNotFound)
206 ········{
207 ············foreach(NDOTreeNode trn in this.Nodes)
208 ············{
209 ················TableNode tn = trn as TableNode;
210 ················if (tn == null)
211 ····················continue;
212 ················if (tn.ToString() == tableName)
213 ····················return tn;
214 ············}
215 ············if (throwIfNotFound)
216 ················throw new Exception("FindTableNode: Can't find table '" + tableName + "'.");
217 ············return null;
218 ········}
219
220 ········public Database Database
221 ········{
222 ············get { return this.myObject as Database; }
223 ········}
224
225 ········protected override void CalculateIndex()
226 ········{············
227 ············SetImageIndex(12);
228 ········}
229 ········/// <summary>
230 ········/// Used by serialization only
231 ········/// </summary>
232 ········public DatabaseNode()
233 ········{
234 ········}
235
236 ········public override void FromXml(System.Xml.XmlElement element)
237 ········{
238 ············base.FromXml (element);
239 ············//TODO: Db-Refresh
240 ········}
241
242
243 ····}
244 }
245
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.Collections.Generic;
26 using System.ComponentModel;
27 using NDO;
28 using NDOInterfaces;
29 using System.Data;
30 using System.Windows.Forms;
31 using ClassGenerator.AssemblyWizard;
32 using System.Diagnostics;
33
34 namespace ClassGenerator
35 {
36 ····[Serializable]
37 #if DEBUG····
38 ····public class DatabaseNode : NDOTreeNode
39 #else
40 ····internal class DatabaseNode : NDOTreeNode
41 #endif
42 ····{
43 ········public ArrayList TableNodes
44 ········{
45 ············get
46 ············{
47 ················ArrayList al = new ArrayList();
48 ················foreach(NDOTreeNode trn in this.Nodes)
49 ················{
50 ····················TableNode tn = trn as TableNode;
51 ····················if (tn == null)
52 ························continue;
53 ····················al.Add(tn);
54 ················}
55 ················return al;
56 ············}
57 ········}
58
59 ········public ArrayList UnmappedTableNodes
60 ········{
61 ············get
62 ············{
63 ················ArrayList al = new ArrayList();
64 ················foreach(NDOTreeNode trn in this.Nodes)
65 ················{
66 ····················TableNode tn = trn as TableNode;
67 ····················if (tn == null)
68 ························continue;
69 ····················if (tn.Table.MappingType == TableMappingType.NotMapped)
70 ························al.Add(tn);
71 ················}
72 ················return al;
73 ············}
74 ········}
75
76 ········public ArrayList Classes
77 ········{
78 ············get
79 ············{
80 ················ArrayList al = new ArrayList();
81 ················foreach(NDOTreeNode trn in this.Nodes)
82 ················{
83 ····················TableNode tn = trn as TableNode;
84 ····················if (tn == null)
85 ························continue;
86 ····················if (tn.Table.MappingType == TableMappingType.MappedAsClass)
87 ························al.Add(tn);
88 ················}
89 ················return al;
90 ············}
91 ········}
92
93 ········public DatabaseNode(AssemblyWizModel model) : base("Database", null)
94 ········{
95 ············this.myObject = new Database(model);············
96 ············Init();
97 ········}
98
99 ········public DatabaseNode(Database db) : base("Database", null)
100 ········{
101 ············this.myObject = db;
102 ············Init();
103 ········}
104
105 ········void Init()
106 ········{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
108 ················List<TableNode> l = new List<TableNode>();
109 ················foreach ( DataTable dt in this.Database.DataSet.Tables )
110 ················{
111 ····················l.Add( new TableNode( dt, this, string.Empty, null, null) );
112 ················}
113 ················l.Sort(delegate(TableNode tn1, TableNode tn2)
114 ················{
115 ····················return tn1.Name.CompareTo(tn2.Name);
116 ················});
117 ················foreach (TableNode tn in l)
118 ····················this.Nodes.Add(tn);
119 ················// Store the parent nodes, so that we can navigate through the schema hierarchy.
120 ················// We need this to generate the correct XPath, if we skip an Xml Element
121 ················//foreach ( DataRelation dr in this.Database.DataSet.Relations )
122 ················//{
123 ················//····TableNode parentNode = FindTableNode(dr.ParentTable.TableName, true);
124 ················//····TableNode childNode = FindTableNode(dr.ChildTable.TableName, true);
125 ················//····childNode.UserData.Add( "parentnode", parentNode );
126 ················//}
127 //············}
128 ············CalculateIndex();
129 ········}
130
131 ········public TableNode this[string name]
132 ········{
133 ············get
134 ············{
135 ················foreach(TableNode tn in this.TableNodes)
136 ····················if (tn.Text == name)
137 ························return tn;
138 ················return null;
139 ············}
140 ········}
141
142
143 ········public void MapXmlSchema()
144 ········{
145 ············foreach(TableNode tn in this.TableNodes)
146 ················tn.MapClass(null, EventArgs.Empty);
147
148 ············DataSet ds = this.Database.DataSet;
149 ············foreach ( DataRelation dr in ds.Relations )
150 ············{
151 ················string xpath = (string)dr.ExtendedProperties["xpath"];
152 ················ApplicationController.Instance.MakeForeignKey(dr);
153 ············}
154 ········}
155
156 ········public TableNode FindTableNode(string tableName, bool throwIfNotFound)
157 ········{
158 ············foreach(NDOTreeNode trn in this.Nodes)
159 ············{
160 ················TableNode tn = trn as TableNode;
161 ················if (tn == null)
162 ····················continue;
163 ················if (tn.ToString() == tableName)
164 ····················return tn;
165 ············}
166 ············if (throwIfNotFound)
167 ················throw new Exception("FindTableNode: Can't find table '" + tableName + "'.");
168 ············return null;
169 ········}
170
171 ········public Database Database
172 ········{
173 ············get { return this.myObject as Database; }
174 ········}
175
176 ········protected override void CalculateIndex()
177 ········{············
178 ············SetImageIndex(12);
179 ········}
180 ········/// <summary>
181 ········/// Used by serialization only
182 ········/// </summary>
183 ········public DatabaseNode()
184 ········{
185 ········}
186
187 ········public override void FromXml(System.Xml.XmlElement element)
188 ········{
189 ············base.FromXml (element);
190 ············//TODO: Db-Refresh
191 ········}
192
193
194 ····}
195 }
196