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

Last Commit (ab290a4)
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 ········private readonly IProvider provider;
38 ········protected ISqlGenerator concreteGenerator;
39 ········protected MessageAdapter messages;
40 ········protected NDOMapping mappings;
41
42 ········public GenericSqlGeneratorBase(IProvider provider, ISqlGenerator concreteGenerator, MessageAdapter messages, NDOMapping mappings)
43 ········{
44 ············this.provider = provider;
45 ············this.concreteGenerator = concreteGenerator;
46 ············this.messages = messages;
47 ············this.mappings = mappings;
48 ········}
49
50 ········protected string CreateTable(DataTable dt)
51 ········{
52 ············StringBuilder sb = new StringBuilder();
53
54 ············string tableName = QualifiedTableName.Get(dt.TableName, provider);
55
56 ············Class cl = FindClass(dt.TableName, mappings);
57
58 ············if (cl != null)
59 ············{
60 ················Connection conn = mappings.FindConnection(cl);
61 ················if (conn != null)
62 ····················concreteGenerator.ConnectToDatabase(conn.Name);
63 ············}
64
65 ············sb.Append(concreteGenerator.BeginnTable(tableName));
66 ············sb.Append('\n');
67
68 ············int vorletzterIndex = dt.Columns.Count - 1;
69 ············DataColumn[] primaryKeyColumns = dt.PrimaryKey;
70 ············bool hasPrimaryKeyColumns = primaryKeyColumns.Length > 0;
71
72 ············for (int i = 0; i < dt.Columns.Count; i++)
73 ············{
74 ················System.Data.DataColumn dc = dt.Columns[i];
75
76 ················bool isPrimary = false;
77 ················foreach (DataColumn pkc in primaryKeyColumns)
78 ················{
79 ····················if (pkc.ColumnName == dc.ColumnName)
80 ····················{
81 ························isPrimary = true;
82 ························break;
83 ····················}
84 ················}
85
86 ················sb.Append(CreateColumn(dc, cl, provider, isPrimary));
87 ················if (i < vorletzterIndex)
88 ················{
89 ····················sb.Append(",");
90 ····················sb.Append('\n');
91 ················}
92 ············}
93
94 ············//············vorletzterIndex = dt.ParentRelations.Count - 1;
95 ············//············if (vorletzterIndex > -1)
96 ············//················sb.Append(",");
97 ············//
98 ············//············for (int i = 0; i < dt.ParentRelations.Count; i++)
99 ············//············{
100 ············//················DataRelation dr = dt.ParentRelations[i];
101 ············//················sb.Append(concreteGenerator.CreateForeignKeyConstraint(dr.ChildColumns, dr.ParentColumns, provider.GetQuotedName(dr.RelationName), dr.ParentTable.TableName));
102 ············//················if (i < vorletzterIndex)
103 ············//····················sb.Append(",");
104 ············//············}
105
106 ············if(concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable
107 ················&& hasPrimaryKeyColumns)
108 ············{
109 ················sb.Append(",");
110 ················sb.Append('\n');
111 ············}
112
113 ············if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable)
114 ············{
115 ················sb.Append(CreatePrimaryKeyConstraint(primaryKeyColumns, dt, provider));
116 ············}
117 ············else
118 ············{
119 ················sb.Append('\n');
120 ············}
121 ············sb.Append(concreteGenerator.EndTable(tableName));
122 ············sb.Append('\n');
123
124 ············//············CreateIndex(primaryKeyColumns, sb, dt, provider);
125
126 ············if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.AfterTable)
127 ············{
128 ················sb.Append(CreatePrimaryKeyConstraint(primaryKeyColumns, dt, provider));
129 ············}
130
131 ············sb.Append('\n');
132
133 ············return sb.ToString();
134 ········}
135
136 ········protected NDO.Mapping.Class FindClass(string tableName, NDOMapping mappings)
137 ········{
138 ············Class result = null;
139 ············foreach(Class cl in mappings.Classes)
140 ············{
141 ················if (cl.TableName == tableName)
142 ················{
143 ····················result = cl;
144 ····················break;
145 ················}
146 ············}
147 ············return result;
148 ········}
149
150 ········protected string CreateColumn(DataColumn dc, Class cl, IProvider provider, bool isPrimary)
151 ········{
152 ············string name = provider.GetQuotedName(dc.ColumnName);
153 ············string columnType = null;
154 ············string width = null;
155 ············string precision = null;
156 ············bool autoIncrement = false;
157 ············StringBuilder sb = new StringBuilder();
158 ············bool allowNull = true;
159 ············int size = 0;
160
161 ············if (cl != null)
162 ············{
163 ················Field field = FindField(dc.ColumnName, cl);
164 ················if (null != field)
165 ················{
166 ····················if (null != field.Column.DbType)
167 ························columnType = field.Column.DbType;
168 ····················size = field.Column.Size;
169 ····················var defaultDbType = concreteGenerator.DbTypeFromType(dc.DataType, size);
170 ····················bool ignoreColumnSize = field.Column.IgnoreColumnSizeInDDL;
171 ····················if (0 != size && !ignoreColumnSize)
172 ····················{
173 ························int dl = field.Column.Size;
174 ························if (dl == -1 && String.Compare(defaultDbType, "nvarchar", true) == 0 && concreteGenerator.ProviderName == "SqlServer" )
175 ····························width = "max";
176 ························else
177 ····························width = dl.ToString();
178 ····················}
179 ····················if (0 != field.Column.Precision && !ignoreColumnSize)
180 ························precision = field.Column.Precision.ToString();
181 ····················allowNull = field.Column.AllowDbNull;
182 ················}
183 ················else if (cl.TimeStampColumn == dc.ColumnName)
184 ················{
185 ····················if (!provider.SupportsNativeGuidType)
186 ························width = "36";
187 ················}
188 ················else if (isPrimary && dc.AutoIncrement)
189 ················{
190 ····················autoIncrement = true;
191 ················}
192 ············}
193 ············if (null == columnType)
194 ············{
195 ················try
196 ················{
197 ····················columnType = concreteGenerator.DbTypeFromType(dc.DataType, size);
198 ················}
199 ················catch
200 ················{
201 ····················System.Diagnostics.Debug.Write("");
202 ················}
203 ············}
204
205 ············if (null == width)
206 ············{································
207 ················int dl = provider.GetDefaultLength(dc.DataType);
208 ················if (dl != 0)
209 ················{
210 ····················width = dl.ToString();
211 ················}
212 ············}
213 ············
214 ············// Because there is no GetDefaultPrecision in the provider...
215 ············// We assume the field to represent currency data
216 ············if (precision == null && dc.DataType == typeof(decimal))
217 ············{
218 ················precision = "2";
219 ············}
220
221 ············if (columnType != null)
222 ············{
223 ················if (!concreteGenerator.LengthAllowed(columnType))
224 ····················width = null;
225 ············}
226 ············else
227 ············{
228 ················if (!concreteGenerator.LengthAllowed(dc.DataType))
229 ····················width = null;
230 ············}
231
232
233 ············if (autoIncrement && concreteGenerator.HasSpecialAutoIncrementColumnFormat)
234 ················sb.Append(concreteGenerator.AutoIncrementColumn(name, dc.DataType, columnType, width, isPrimary));
235 ············else if(isPrimary && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InColumn)
236 ················sb.Append(concreteGenerator.PrimaryKeyColumn(name, dc.DataType, columnType, width));
237 ············else if (width != null && precision != null)
238 ················sb.Append(name + " " + columnType + "(" + width + "," + precision + ")");
239 ············else if (width != null)
240 ················sb.Append(name + " " + columnType + "(" + width + ")");············
241 ············else
242 ················sb.Append(name + " " + columnType);
243
244 ············sb.Append(" ");
245
246 ············sb.Append(concreteGenerator.NullExpression(allowNull && dc.AllowDBNull));
247 ············return sb.ToString();
248 ········}
249
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250 ········protected string CreatePrimaryKeyConstraint(DataColumn[] primaryKeyColumns, DataTable dt, IProvider provider)
251 ········{
252 ············if (primaryKeyColumns.Length == 0)
253 ················return string.Empty;
254 ············string[] strArr = dt.TableName.Split('.');
255 ············string constraintName = provider.GetQuotedName("PK_" + strArr[strArr.Length - 1]);
256 ············return concreteGenerator.CreatePrimaryKeyConstraint(primaryKeyColumns, constraintName, QualifiedTableName.Get(dt.TableName, provider)) + '\n';
257 ········}
258
259
260 ········protected Field FindField (string columnName, Class cl)
261 ········{
262 ············Field result = null;
263 ············foreach (Field field in cl.Fields)
264 ············{
265 ················if (field.Column.Name == columnName)
266 ················{
267 ····················result = field;
268 ····················break;
269 ················}
270 ············}
271 ············return result;
272 ········}
273
274
275 ········protected Class GetClassForTable(string tableName, NDOMapping mapping)
276 ········{
277 ············foreach(Class cl in mapping.Classes)
278 ················if (cl.TableName == tableName)
279 ····················return cl;
280 ············return null;
281 ········}
282
283
284 ····}
285 }
286
New Commit (f5f4fd8)
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 ········private readonly IProvider provider;
38 ········protected ISqlGenerator concreteGenerator;
39 ········protected MessageAdapter messages;
40 ········protected NDOMapping mappings;
41
42 ········public GenericSqlGeneratorBase(IProvider provider, ISqlGenerator concreteGenerator, MessageAdapter messages, NDOMapping mappings)
43 ········{
44 ············this.provider = provider;
45 ············this.concreteGenerator = concreteGenerator;
46 ············this.messages = messages;
47 ············this.mappings = mappings;
48 ········}
49
50 ········protected string CreateTable(DataTable dt)
51 ········{
52 ············StringBuilder sb = new StringBuilder();
53
54 ············string tableName = QualifiedTableName.Get(dt.TableName, provider);
55
56 ············Class cl = FindClass(dt.TableName, mappings);
57
58 ············if (cl != null)
59 ············{
60 ················Connection conn = mappings.FindConnection(cl);
61 ················if (conn != null)
62 ····················concreteGenerator.ConnectToDatabase(conn.Name);
63 ············}
64
65 ············sb.Append(concreteGenerator.BeginnTable(tableName));
66 ············sb.Append('\n');
67
68 ············int vorletzterIndex = dt.Columns.Count - 1;
69 ············DataColumn[] primaryKeyColumns = dt.PrimaryKey;
70 ············bool hasPrimaryKeyColumns = primaryKeyColumns.Length > 0;
71
72 ············for (int i = 0; i < dt.Columns.Count; i++)
73 ············{
74 ················System.Data.DataColumn dc = dt.Columns[i];
75
76 ················bool isPrimary = false;
77 ················foreach (DataColumn pkc in primaryKeyColumns)
78 ················{
79 ····················if (pkc.ColumnName == dc.ColumnName)
80 ····················{
81 ························isPrimary = true;
82 ························break;
83 ····················}
84 ················}
85
86 ················sb.Append(CreateColumn(dc, cl, provider, isPrimary));
87 ················if (i < vorletzterIndex)
88 ················{
89 ····················sb.Append(",");
90 ····················sb.Append('\n');
91 ················}
92 ············}
93
94 ············//············vorletzterIndex = dt.ParentRelations.Count - 1;
95 ············//············if (vorletzterIndex > -1)
96 ············//················sb.Append(",");
97 ············//
98 ············//············for (int i = 0; i < dt.ParentRelations.Count; i++)
99 ············//············{
100 ············//················DataRelation dr = dt.ParentRelations[i];
101 ············//················sb.Append(concreteGenerator.CreateForeignKeyConstraint(dr.ChildColumns, dr.ParentColumns, provider.GetQuotedName(dr.RelationName), dr.ParentTable.TableName));
102 ············//················if (i < vorletzterIndex)
103 ············//····················sb.Append(",");
104 ············//············}
105
106 ············if(concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable
107 ················&& hasPrimaryKeyColumns)
108 ············{
109 ················sb.Append(",");
110 ················sb.Append('\n');
111 ············}
112
113 ············if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InTable)
114 ············{
115 ················sb.Append(CreatePrimaryKeyConstraint(primaryKeyColumns, dt, provider));
116 ············}
117 ············else
118 ············{
119 ················sb.Append('\n');
120 ············}
121 ············sb.Append(concreteGenerator.EndTable(tableName));
122 ············sb.Append('\n');
123
124 ············//············CreateIndex(primaryKeyColumns, sb, dt, provider);
125
126 ············if (hasPrimaryKeyColumns && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.AfterTable)
127 ············{
128 ················sb.Append(CreatePrimaryKeyConstraint(primaryKeyColumns, dt, provider));
129 ············}
130
131 ············sb.Append('\n');
132
133 ············return sb.ToString();
134 ········}
135
136 ········protected NDO.Mapping.Class FindClass(string tableName, NDOMapping mappings)
137 ········{
138 ············Class result = null;
139 ············foreach(Class cl in mappings.Classes)
140 ············{
141 ················if (cl.TableName == tableName)
142 ················{
143 ····················result = cl;
144 ····················break;
145 ················}
146 ············}
147 ············return result;
148 ········}
149
150 ········protected string CreateColumn(DataColumn dc, Class cl, IProvider provider, bool isPrimary)
151 ········{
152 ············string name = provider.GetQuotedName(dc.ColumnName);
153 ············string columnType = null;
154 ············string width = null;
155 ············string precision = null;
156 ············bool autoIncrement = false;
157 ············StringBuilder sb = new StringBuilder();
158 ············bool allowNull = true;
159 ············int size = 0;
160
161 ············if (cl != null)
162 ············{
163 ················Field field = FindField(dc.ColumnName, cl);
164 ················if (null != field)
165 ················{
166 ····················if (null != field.Column.DbType)
167 ························columnType = field.Column.DbType;
168 ····················size = field.Column.Size;
169 ····················var defaultDbType = concreteGenerator.DbTypeFromType(dc.DataType, size);
170 ····················bool ignoreColumnSize = field.Column.IgnoreColumnSizeInDDL;
171 ····················if (0 != size && !ignoreColumnSize)
172 ····················{
173 ························int dl = field.Column.Size;
174 ························if (dl == -1 && String.Compare(defaultDbType, "nvarchar", true) == 0 && concreteGenerator.ProviderName == "SqlServer" )
175 ····························width = "max";
176 ························else
177 ····························width = dl.ToString();
178 ····················}
179 ····················if (0 != field.Column.Precision && !ignoreColumnSize)
180 ························precision = field.Column.Precision.ToString();
181 ····················allowNull = field.Column.AllowDbNull;
182 ················}
183 ················else if (cl.TimeStampColumn == dc.ColumnName)
184 ················{
185 ····················if (!provider.SupportsNativeGuidType)
186 ························width = "36";
187 ················}
188 ················else if (isPrimary && dc.AutoIncrement)
189 ················{
190 ····················autoIncrement = true;
191 ················}
192 ············}
193 ············if (null == columnType)
194 ············{
195 ················try
196 ················{
197 ····················columnType = concreteGenerator.DbTypeFromType(dc.DataType, size);
198 ················}
199 ················catch
200 ················{
201 ····················System.Diagnostics.Debug.Write("");
202 ················}
203 ············}
204
205 ············if (null == width)
206 ············{································
207 ················int dl = provider.GetDefaultLength(dc.DataType);
208 ················if (dl != 0)
209 ················{
210 ····················width = dl.ToString();
211 ················}
212 ············}
213 ············
214 ············// Because there is no GetDefaultPrecision in the provider...
215 ············// We assume the field to represent currency data
216 ············if (precision == null && dc.DataType == typeof(decimal))
217 ············{
218 ················precision = "2";
219 ············}
220
221 ············if (columnType != null)
222 ············{
223 ················if (!concreteGenerator.LengthAllowed(columnType))
224 ····················width = null;
225 ············}
226 ············else
227 ············{
228 ················if (!concreteGenerator.LengthAllowed(dc.DataType))
229 ····················width = null;
230 ············}
231
232
233 ············if (autoIncrement && concreteGenerator.HasSpecialAutoIncrementColumnFormat)
234 ················sb.Append(concreteGenerator.AutoIncrementColumn(name, dc.DataType, columnType, width, isPrimary));
235 ············else if(isPrimary && concreteGenerator.PrimaryConstraintPlacement == PrimaryConstraintPlacement.InColumn)
236 ················sb.Append(concreteGenerator.PrimaryKeyColumn(name, dc.DataType, columnType, width));
237 ············else if (width != null && precision != null)
238 ················sb.Append(name + " " + columnType + "(" + width + "," + precision + ")");
239 ············else if (width != null)
240 ················sb.Append(name + " " + columnType + "(" + width + ")");············
241 ············else
242 ················sb.Append(name + " " + columnType);
243
244 ············sb.Append(" ");
245
246 ············sb.Append(concreteGenerator.NullExpression(allowNull && dc.AllowDBNull));
247 ············return sb.ToString();
248 ········}
249
250 ········/// <summary>
251 ········/// Creates the indexes of a table, see <see cref="IndexDefinition"/>.
252 ········/// </summary>
253 ········protected string CreateIndexes(DataTable dt)
254 ········{
255 ············StringBuilder sb = new StringBuilder();
256 ············foreach (IndexDefinition index in IndexDefinition.ForTable(dt, mappings))
257 ············{
258 ················string sql = concreteGenerator.CreateIndex(provider.GetQuotedName(index.Name), QualifiedTableName.Get(dt.TableName, provider), index.Columns, index.IsUnique);
259 ················if (!String.IsNullOrEmpty(sql))
260 ················{
261 ····················sb.Append(sql);
262 ····················sb.Append(";\n");
263 ················}
264 ············}
265 ············return sb.ToString();
266 ········}
267
268 ········/// <summary>
269 ········/// Turns a CREATE INDEX statement into a CREATE UNIQUE INDEX statement.
270 ········/// </summary>
271 ········/// <remarks>
272 ········/// ISqlGenerator.CreateIndex doesn't know unique indexes. Providers override CreateIndex only to shorten the
273 ········/// index name, so we change the statement they generate instead of extending the interface of all providers.
274 ········/// </remarks>
275 ········string MakeUnique(string sql, string indexName)
276 ········{
277 ············const string createIndex = "CREATE INDEX ";
278 ············if (sql.StartsWith(createIndex, StringComparison.OrdinalIgnoreCase))
279 ················return "CREATE UNIQUE INDEX " + sql.Substring(createIndex.Length);
280 ············messages.WriteLine("Warning: Can't create the index " + indexName + " as unique index, because the SQL generator for '" + provider.Name + "' uses an unknown syntax.");
281 ············return sql;
282 ········}
283
284 ········protected string CreatePrimaryKeyConstraint(DataColumn[] primaryKeyColumns, DataTable dt, IProvider provider)
285 ········{
286 ············if (primaryKeyColumns.Length == 0)
287 ················return string.Empty;
288 ············string[] strArr = dt.TableName.Split('.');
289 ············string constraintName = provider.GetQuotedName("PK_" + strArr[strArr.Length - 1]);
290 ············return concreteGenerator.CreatePrimaryKeyConstraint(primaryKeyColumns, constraintName, QualifiedTableName.Get(dt.TableName, provider)) + '\n';
291 ········}
292
293
294 ········protected Field FindField (string columnName, Class cl)
295 ········{
296 ············Field result = null;
297 ············foreach (Field field in cl.Fields)
298 ············{
299 ················if (field.Column.Name == columnName)
300 ················{
301 ····················result = field;
302 ····················break;
303 ················}
304 ············}
305 ············return result;
306 ········}
307
308
309 ········protected Class GetClassForTable(string tableName, NDOMapping mapping)
310 ········{
311 ············foreach(Class cl in mapping.Classes)
312 ················if (cl.TableName == tableName)
313 ····················return cl;
314 ············return null;
315 ········}
316
317
318 ····}
319 }
320