Datei: NDOEnhancer/NDOEnhancer/Enhancer/Generator/GenericSqlGeneratorBase.cs

Last Commit (2a2e7ba)
1 //
2 // Copyright (c) 2002-2022 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.Text;
25 using System.Data;
26 using NDO.Mapping;
27 using NDO;
28 using NDOInterfaces;
29
30 namespace NDOEnhancer
31 {
32 ····/// <summary>
33 ····/// Zusammenfassung für GenericSqlGeneratorBase.
34 ····/// </summary>
35 ····internal class GenericSqlGeneratorBase
36 ····{
37 ········protected ISqlGenerator concreteGenerator;
38 ········protected MessageAdapter messages;
39 ········protected NDOMapping mappings;
40
41 ········public GenericSqlGeneratorBase(ISqlGenerator concreteGenerator, MessageAdapter messages, NDOMapping mappings)
42 ········{
43 ············this.concreteGenerator = concreteGenerator;
44 ············this.messages = messages;
45 ············this.mappings = mappings;
46 ········}
47
48 ········protected string CreateTable(DataTable dt)
49 ········{
50 ············StringBuilder sb = new StringBuilder();
51 ············IProvider provider = NDOProviderFactory.Instance[concreteGenerator.ProviderName];
52 ············if (provider == null)
53 ················throw new Exception("Can't find NDO provider '" + concreteGenerator.ProviderName + "'.");
54
55 ············string tableName = QualifiedTableName.Get(dt.TableName, provider);
56
57 ············Class cl = FindClass(dt.TableName, mappings);
58
59 ············if (cl != null)
60 ············{
61 ················Connection conn = mappings.FindConnection(cl);
62 ················if (conn != null)
63 ····················concreteGenerator.ConnectToDatabase(conn.Name);
64 ············}
65
66 ············sb.Append(concreteGenerator.BeginnTable(tableName));
67 ············sb.Append('\n');
68
69 ············int vorletzterIndex = dt.Columns.Count - 1;
70 ············DataColumn[] primaryKeyColumns = dt.PrimaryKey;
71 ············bool hasPrimaryKeyColumns = primaryKeyColumns.Length > 0;
72
73 ············for (int i = 0; i < dt.Columns.Count; i++)
74 ············{
75 ················System.Data.DataColumn dc = dt.Columns[i];
76
77 ················bool isPrimary = false;
78 ················foreach (DataColumn pkc in primaryKeyColumns)
79 ················{
80 ····················if (pkc.ColumnName == dc.ColumnName)
81 ····················{
82 ························isPrimary = true;
83 ························break;
84 ····················}
85 ················}
86
87 ················sb.Append(CreateColumn(dc, cl, provider, isPrimary));
88 ················if (i < vorletzterIndex)
89 ················{
90 ····················sb.Append(",");
91 ····················sb.Append('\n');
92 ················}
93 ············}
94
95 ············//············vorletzterIndex = dt.ParentRelations.Count - 1;
96 ············//············if (vorletzterIndex > -1)
97 ············//················sb.Append(",");
98 ············//
99 ············//············for (int i = 0; i < dt.ParentRelations.Count; i++)
100 ············//············{
101 ············//················DataRelation dr = dt.ParentRelations[i];
102 ············//················sb.Append(concreteGenerator.CreateForeignKeyConstraint(dr.ChildColumns, dr.ParentColumns, provider.GetQuotedName(dr.RelationName), dr.ParentTable.TableName));
103 ············//················if (i < vorletzterIndex)
104 ············//····················sb.Append(",");
105 ············//············}
106
107 ············if(concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable
108 ················&& hasPrimaryKeyColumns)
109 ············{
110 ················sb.Append(",");
111 ················sb.Append('\n');
112 ············}
113
114 ············if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable)
115 ············{
116 ················sb.Append(CreatePrimaryKeyConstraint(primaryKeyColumns, dt, provider));
117 ············}
118 ············else
119 ············{
120 ················sb.Append('\n');
121 ············}
122 ············sb.Append(concreteGenerator.EndTable(tableName));
123 ············sb.Append('\n');
124
125 ············//············CreateIndex(primaryKeyColumns, sb, dt, provider);
126
127 ············if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.AfterTable)
128 ············{
129 ················sb.Append(CreatePrimaryKeyConstraint(primaryKeyColumns, dt, provider));
130 ············}
131
132 ············sb.Append('\n');
133
134 ············return sb.ToString();
135 ········}
136
137 ········protected NDO.Mapping.Class FindClass(string tableName, NDOMapping mappings)
138 ········{
139 ············Class result = null;
140 ············foreach(Class cl in mappings.Classes)
141 ············{
142 ················if (cl.TableName == tableName)
143 ················{
144 ····················result = cl;
145 ····················break;
146 ················}
147 ············}
148 ············return result;
149 ········}
150
151 ········protected string CreateColumn(DataColumn dc, Class cl, IProvider provider, bool isPrimary)
152 ········{
153 ············string name = provider.GetQuotedName(dc.ColumnName);
154 ············string columnType = null;
155 ············string width = null;
156 ············string precision = null;
157 ············bool autoIncrement = false;
158 ············StringBuilder sb = new StringBuilder();
159 ············bool allowNull = true;
160 ············int size = 0;
161
162 ············if (cl != null)
163 ············{
164 ················Field field = FindField(dc.ColumnName, cl);
165 ················if (null != field)
166 ················{
167 ····················if (null != field.Column.DbType)
168 ························columnType = field.Column.DbType;
169 ····················size = field.Column.Size;
170 ····················var defaultDbType = concreteGenerator.DbTypeFromType(dc.DataType, size);
171 ····················bool ignoreColumnSize = field.Column.IgnoreColumnSizeInDDL;
172 ····················if (0 != size && !ignoreColumnSize)
173 ····················{
174 ························int dl = field.Column.Size;
175 ························if (dl == -1 && String.Compare(defaultDbType, "nvarchar", true) == 0 && concreteGenerator.ProviderName == "SqlServer" )
176 ····························width = "max";
177 ························else
178 ····························width = dl.ToString();
179 ····················}
180 ····················if (0 != field.Column.Precision && !ignoreColumnSize)
181 ························precision = field.Column.Precision.ToString();
182 ····················allowNull = field.Column.AllowDbNull;
183 ················}
184 ················else if (cl.TimeStampColumn == dc.ColumnName)
185 ················{
186 ····················if (!provider.SupportsNativeGuidType)
187 ························width = "36";
188 ················}
189 ················else if (isPrimary && dc.AutoIncrement)
190 ················{
191 ····················autoIncrement = true;
192 ················}
193 ············}
194 ············if (null == columnType)
195 ············{
196 ················try
197 ················{
198 ····················columnType = concreteGenerator.DbTypeFromType(dc.DataType, size);
199 ················}
200 ················catch
201 ················{
202 ····················System.Diagnostics.Debug.Write("");
203 ················}
204 ············}
205
206 ············if (null == width)
207 ············{································
208 ················int dl = provider.GetDefaultLength(dc.DataType);
209 ················if (dl != 0)
210 ················{
211 ····················width = dl.ToString();
212 ················}
213 ············}
214 ············
215 ············// Because there is no GetDefaultPrecision in the provider...
216 ············// We assume the field to represent currency data
217 ············if (precision == null && dc.DataType == typeof(decimal))
218 ············{
219 ················precision = "2";
220 ············}
221
222 ············if (columnType != null)
223 ············{
224 ················if (!concreteGenerator.LengthAllowed(columnType))
225 ····················width = null;
226 ············}
227 ············else
228 ············{
229 ················if (!concreteGenerator.LengthAllowed(dc.DataType))
230 ····················width = null;
231 ············}
232
233
234 ············if (autoIncrement && concreteGenerator.HasSpecialAutoIncrementColumnFormat)
235 sb. Append( concreteGenerator. AutoIncrementColumn( name, dc. DataType, columnType, width) ) ;
236 ············else if(isPrimary && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InColumn)
237 ················sb.Append(concreteGenerator.PrimaryKeyColumn(name, dc.DataType, columnType, width));
238 ············else if (width != null && precision != null)
239 ················sb.Append(name + " " + columnType + "(" + width + "," + precision + ")");
240 ············else if (width != null)
241 ················sb.Append(name + " " + columnType + "(" + width + ")");············
242 ············else
243 ················sb.Append(name + " " + columnType);
244
245 ············sb.Append(" ");
246
247 ············sb.Append(concreteGenerator.NullExpression(allowNull && dc.AllowDBNull));
248 ············return sb.ToString();
249 ········}
250
251 ········protected string CreatePrimaryKeyConstraint(DataColumn[] primaryKeyColumns, DataTable dt, IProvider provider)
252 ········{
253 ············if (primaryKeyColumns.Length == 0)
254 ················return string.Empty;
255 ············string[] strArr = dt.TableName.Split('.');
256 ············string constraintName = provider.GetQuotedName("PK_" + strArr[strArr.Length - 1]);
257 ············return concreteGenerator.CreatePrimaryKeyConstraint(primaryKeyColumns, constraintName, QualifiedTableName.Get(dt.TableName, provider)) + '\n';
258 ········}
259
260
261 ········protected Field FindField (string columnName, Class cl)
262 ········{
263 ············Field result = null;
264 ············foreach (Field field in cl.Fields)
265 ············{
266 ················if (field.Column.Name == columnName)
267 ················{
268 ····················result = field;
269 ····················break;
270 ················}
271 ············}
272 ············return result;
273 ········}
274
275
276 ········protected Class GetClassForTable(string tableName, NDOMapping mapping)
277 ········{
278 ············foreach(Class cl in mapping.Classes)
279 ················if (cl.TableName == tableName)
280 ····················return cl;
281 ············return null;
282 ········}
283
284
285 ····}
286 }
287
New Commit (1e68dc7)
1 //
2 // Copyright (c) 2002-2022 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.Text;
25 using System.Data;
26 using NDO.Mapping;
27 using NDO;
28 using NDOInterfaces;
29
30 namespace NDOEnhancer
31 {
32 ····/// <summary>
33 ····/// Zusammenfassung für GenericSqlGeneratorBase.
34 ····/// </summary>
35 ····internal class GenericSqlGeneratorBase
36 ····{
37 ········protected ISqlGenerator concreteGenerator;
38 ········protected MessageAdapter messages;
39 ········protected NDOMapping mappings;
40
41 ········public GenericSqlGeneratorBase(ISqlGenerator concreteGenerator, MessageAdapter messages, NDOMapping mappings)
42 ········{
43 ············this.concreteGenerator = concreteGenerator;
44 ············this.messages = messages;
45 ············this.mappings = mappings;
46 ········}
47
48 ········protected string CreateTable(DataTable dt)
49 ········{
50 ············StringBuilder sb = new StringBuilder();
51 ············IProvider provider = NDOProviderFactory.Instance[concreteGenerator.ProviderName];
52 ············if (provider == null)
53 ················throw new Exception("Can't find NDO provider '" + concreteGenerator.ProviderName + "'.");
54
55 ············string tableName = QualifiedTableName.Get(dt.TableName, provider);
56
57 ············Class cl = FindClass(dt.TableName, mappings);
58
59 ············if (cl != null)
60 ············{
61 ················Connection conn = mappings.FindConnection(cl);
62 ················if (conn != null)
63 ····················concreteGenerator.ConnectToDatabase(conn.Name);
64 ············}
65
66 ············sb.Append(concreteGenerator.BeginnTable(tableName));
67 ············sb.Append('\n');
68
69 ············int vorletzterIndex = dt.Columns.Count - 1;
70 ············DataColumn[] primaryKeyColumns = dt.PrimaryKey;
71 ············bool hasPrimaryKeyColumns = primaryKeyColumns.Length > 0;
72
73 ············for (int i = 0; i < dt.Columns.Count; i++)
74 ············{
75 ················System.Data.DataColumn dc = dt.Columns[i];
76
77 ················bool isPrimary = false;
78 ················foreach (DataColumn pkc in primaryKeyColumns)
79 ················{
80 ····················if (pkc.ColumnName == dc.ColumnName)
81 ····················{
82 ························isPrimary = true;
83 ························break;
84 ····················}
85 ················}
86
87 ················sb.Append(CreateColumn(dc, cl, provider, isPrimary));
88 ················if (i < vorletzterIndex)
89 ················{
90 ····················sb.Append(",");
91 ····················sb.Append('\n');
92 ················}
93 ············}
94
95 ············//············vorletzterIndex = dt.ParentRelations.Count - 1;
96 ············//············if (vorletzterIndex > -1)
97 ············//················sb.Append(",");
98 ············//
99 ············//············for (int i = 0; i < dt.ParentRelations.Count; i++)
100 ············//············{
101 ············//················DataRelation dr = dt.ParentRelations[i];
102 ············//················sb.Append(concreteGenerator.CreateForeignKeyConstraint(dr.ChildColumns, dr.ParentColumns, provider.GetQuotedName(dr.RelationName), dr.ParentTable.TableName));
103 ············//················if (i < vorletzterIndex)
104 ············//····················sb.Append(",");
105 ············//············}
106
107 ············if(concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable
108 ················&& hasPrimaryKeyColumns)
109 ············{
110 ················sb.Append(",");
111 ················sb.Append('\n');
112 ············}
113
114 ············if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable)
115 ············{
116 ················sb.Append(CreatePrimaryKeyConstraint(primaryKeyColumns, dt, provider));
117 ············}
118 ············else
119 ············{
120 ················sb.Append('\n');
121 ············}
122 ············sb.Append(concreteGenerator.EndTable(tableName));
123 ············sb.Append('\n');
124
125 ············//············CreateIndex(primaryKeyColumns, sb, dt, provider);
126
127 ············if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.AfterTable)
128 ············{
129 ················sb.Append(CreatePrimaryKeyConstraint(primaryKeyColumns, dt, provider));
130 ············}
131
132 ············sb.Append('\n');
133
134 ············return sb.ToString();
135 ········}
136
137 ········protected NDO.Mapping.Class FindClass(string tableName, NDOMapping mappings)
138 ········{
139 ············Class result = null;
140 ············foreach(Class cl in mappings.Classes)
141 ············{
142 ················if (cl.TableName == tableName)
143 ················{
144 ····················result = cl;
145 ····················break;
146 ················}
147 ············}
148 ············return result;
149 ········}
150
151 ········protected string CreateColumn(DataColumn dc, Class cl, IProvider provider, bool isPrimary)
152 ········{
153 ············string name = provider.GetQuotedName(dc.ColumnName);
154 ············string columnType = null;
155 ············string width = null;
156 ············string precision = null;
157 ············bool autoIncrement = false;
158 ············StringBuilder sb = new StringBuilder();
159 ············bool allowNull = true;
160 ············int size = 0;
161
162 ············if (cl != null)
163 ············{
164 ················Field field = FindField(dc.ColumnName, cl);
165 ················if (null != field)
166 ················{
167 ····················if (null != field.Column.DbType)
168 ························columnType = field.Column.DbType;
169 ····················size = field.Column.Size;
170 ····················var defaultDbType = concreteGenerator.DbTypeFromType(dc.DataType, size);
171 ····················bool ignoreColumnSize = field.Column.IgnoreColumnSizeInDDL;
172 ····················if (0 != size && !ignoreColumnSize)
173 ····················{
174 ························int dl = field.Column.Size;
175 ························if (dl == -1 && String.Compare(defaultDbType, "nvarchar", true) == 0 && concreteGenerator.ProviderName == "SqlServer" )
176 ····························width = "max";
177 ························else
178 ····························width = dl.ToString();
179 ····················}
180 ····················if (0 != field.Column.Precision && !ignoreColumnSize)
181 ························precision = field.Column.Precision.ToString();
182 ····················allowNull = field.Column.AllowDbNull;
183 ················}
184 ················else if (cl.TimeStampColumn == dc.ColumnName)
185 ················{
186 ····················if (!provider.SupportsNativeGuidType)
187 ························width = "36";
188 ················}
189 ················else if (isPrimary && dc.AutoIncrement)
190 ················{
191 ····················autoIncrement = true;
192 ················}
193 ············}
194 ············if (null == columnType)
195 ············{
196 ················try
197 ················{
198 ····················columnType = concreteGenerator.DbTypeFromType(dc.DataType, size);
199 ················}
200 ················catch
201 ················{
202 ····················System.Diagnostics.Debug.Write("");
203 ················}
204 ············}
205
206 ············if (null == width)
207 ············{································
208 ················int dl = provider.GetDefaultLength(dc.DataType);
209 ················if (dl != 0)
210 ················{
211 ····················width = dl.ToString();
212 ················}
213 ············}
214 ············
215 ············// Because there is no GetDefaultPrecision in the provider...
216 ············// We assume the field to represent currency data
217 ············if (precision == null && dc.DataType == typeof(decimal))
218 ············{
219 ················precision = "2";
220 ············}
221
222 ············if (columnType != null)
223 ············{
224 ················if (!concreteGenerator.LengthAllowed(columnType))
225 ····················width = null;
226 ············}
227 ············else
228 ············{
229 ················if (!concreteGenerator.LengthAllowed(dc.DataType))
230 ····················width = null;
231 ············}
232
233
234 ············if (autoIncrement && concreteGenerator.HasSpecialAutoIncrementColumnFormat)
235 sb. Append( concreteGenerator. AutoIncrementColumn( name, dc. DataType, columnType, width, isPrimary) ) ;
236 ············else if(isPrimary && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InColumn)
237 ················sb.Append(concreteGenerator.PrimaryKeyColumn(name, dc.DataType, columnType, width));
238 ············else if (width != null && precision != null)
239 ················sb.Append(name + " " + columnType + "(" + width + "," + precision + ")");
240 ············else if (width != null)
241 ················sb.Append(name + " " + columnType + "(" + width + ")");············
242 ············else
243 ················sb.Append(name + " " + columnType);
244
245 ············sb.Append(" ");
246
247 ············sb.Append(concreteGenerator.NullExpression(allowNull && dc.AllowDBNull));
248 ············return sb.ToString();
249 ········}
250
251 ········protected string CreatePrimaryKeyConstraint(DataColumn[] primaryKeyColumns, DataTable dt, IProvider provider)
252 ········{
253 ············if (primaryKeyColumns.Length == 0)
254 ················return string.Empty;
255 ············string[] strArr = dt.TableName.Split('.');
256 ············string constraintName = provider.GetQuotedName("PK_" + strArr[strArr.Length - 1]);
257 ············return concreteGenerator.CreatePrimaryKeyConstraint(primaryKeyColumns, constraintName, QualifiedTableName.Get(dt.TableName, provider)) + '\n';
258 ········}
259
260
261 ········protected Field FindField (string columnName, Class cl)
262 ········{
263 ············Field result = null;
264 ············foreach (Field field in cl.Fields)
265 ············{
266 ················if (field.Column.Name == columnName)
267 ················{
268 ····················result = field;
269 ····················break;
270 ················}
271 ············}
272 ············return result;
273 ········}
274
275
276 ········protected Class GetClassForTable(string tableName, NDOMapping mapping)
277 ········{
278 ············foreach(Class cl in mapping.Classes)
279 ················if (cl.TableName == tableName)
280 ····················return cl;
281 ············return null;
282 ········}
283
284
285 ····}
286 }
287