Datei: Provider/MySqlNdoProvider/NDO.MySql/MySqlProvider.cs
Last Commit (e404055)
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.Text; |
25 | using System.Text.RegularExpressions; |
26 | using System.Data; |
27 | using System.Data.Common; |
28 | using NDOInterfaces; |
29 | using System.Collections; |
30 | using MySql.Data.MySqlClient; |
31 | using MySql.Data.Types; |
32 | |
33 | namespace NDO.MySqlProvider |
34 | { |
35 | ····/// <summary> |
36 | ····/// Sample adapter class to connect new ADO.NET Providers with NDO. |
37 | ····/// This Adapter is based on the MySql MySql Data Connector |
38 | ····/// </summary> |
39 | ····public class Provider : NDOAbstractProvider |
40 | ····{ |
41 | ········// The following methods provide objects of provider classes |
42 | ········// which implement common interfaces in .NET: |
43 | ········// IDbConnection, IDbCommand, DbDataAdapter and the Parameter objects |
44 | ········#region Provide specialized type objects |
45 | ········public override System.Data.IDbConnection NewConnection(string connectionString) |
46 | ········{ |
47 | ············return new MySqlConnection(connectionString); |
48 | ········} |
49 | |
50 | ········public override System.Data.IDbCommand NewSqlCommand(System.Data.IDbConnection connection) |
51 | ········{ |
52 | ············MySqlCommand command = new MySqlCommand(); |
53 | ············command.Connection = (MySqlConnection)connection; |
54 | ············return command; |
55 | ········} |
56 | |
57 | ········public override DbDataAdapter NewDataAdapter(System.Data.IDbCommand select, System.Data.IDbCommand update, System.Data.IDbCommand insert, System.Data.IDbCommand delete) |
58 | ········{ |
59 | ············MySqlDataAdapter da = new MySqlDataAdapter(); |
60 | ············da.SelectCommand = (MySqlCommand)select; |
61 | ············da.UpdateCommand = (MySqlCommand)update; |
62 | ············da.InsertCommand = (MySqlCommand)insert; |
63 | ············da.DeleteCommand = (MySqlCommand)delete; |
64 | ············return da; |
65 | ········} |
66 | |
67 | ········/// <summary> |
68 | ········/// See <see cref="IProvider"> IProvider interface </see> |
69 | ········/// </summary> |
70 | ········public override object NewCommandBuilder(DbDataAdapter dataAdapter) |
71 | ········{ |
72 | ············return new MySqlCommandBuilder((MySqlDataAdapter)dataAdapter); |
73 | ········} |
74 | |
75 | |
76 | ········public override IDataParameter AddParameter(System.Data.IDbCommand command, string parameterName, object dbType, int size, string columnName) |
77 | ········{ |
78 | ············return ((MySqlCommand)command).Parameters.Add(new MySqlParameter(parameterName, (MySql.Data.MySqlClient.MySqlDbType)dbType, size, columnName));············ |
79 | ········} |
80 | |
81 | ········public override IDataParameter AddParameter(IDbCommand command, string parameterName, object dbType, int size, ParameterDirection dir, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value) |
82 | ········{ |
83 | ············return ((MySqlCommand)command).Parameters.Add(new MySqlParameter(parameterName, (MySql.Data.MySqlClient.MySqlDbType)dbType, size, dir, isNullable, precision, scale, srcColumn, srcVersion, value)); |
84 | ········} |
85 | ········#endregion |
86 | |
87 | ········// The following method convert System.Type objects to MySql.Data.MySqlClient.MySqlDbType-Members |
88 | ········// For your own adapter use members of the database type emumeration |
89 | ········// of your ADO.NET provider |
90 | ········#region Provide MySql.Data.MySqlClient.MySqlDbType members |
91 | ········public override object GetDbType(Type t) |
92 | ········{ |
93 | ············t = base.ConvertNullableType(t); |
94 | ············if ( t == typeof(bool) ) |
95 | ················return MySql.Data.MySqlClient.MySqlDbType.Byte; |
96 | ············else if ( t == typeof(byte) ) |
97 | ················return MySql.Data.MySqlClient.MySqlDbType.Byte; |
98 | ············else if ( t == typeof(sbyte) ) |
99 | ················return MySql.Data.MySqlClient.MySqlDbType.Byte; |
100 | ············else if ( t == typeof(char) ) |
101 | ················return MySql.Data.MySqlClient.MySqlDbType.Int16; |
102 | ············else if ( t == typeof(short)) |
103 | ················return MySql.Data.MySqlClient.MySqlDbType.Int16; |
104 | ············else if ( t == typeof(ushort)) |
105 | ················return MySql.Data.MySqlClient.MySqlDbType.Int16; |
106 | ············else if ( t == typeof(int)) |
107 | ················return MySql.Data.MySqlClient.MySqlDbType.Int32; |
108 | ············else if ( t == typeof(uint)) |
109 | ················return MySql.Data.MySqlClient.MySqlDbType.Int32; |
110 | ············else if ( t == typeof(long)) |
111 | ················return MySql.Data.MySqlClient.MySqlDbType.Int64; |
112 | ············else if ( t == typeof(System.Guid)) |
113 | ················return MySql.Data.MySqlClient.MySqlDbType.VarChar; |
114 | ············else if ( t == typeof(ulong)) |
115 | ················return MySql.Data.MySqlClient.MySqlDbType.Int64; |
116 | ············else if ( t == typeof(float)) |
117 | ················return MySql.Data.MySqlClient.MySqlDbType.Float; |
118 | ············else if ( t == typeof(double)) |
119 | ················return MySql.Data.MySqlClient.MySqlDbType.Double; |
120 | ············else if ( t == typeof(string)) |
121 | ················return MySql.Data.MySqlClient.MySqlDbType.VarChar; |
122 | ············else if ( t == typeof(byte[])) |
123 | ················return MySql.Data.MySqlClient.MySqlDbType.MediumBlob; |
124 | ············else if ( t == typeof(decimal)) |
125 | ················return MySql.Data.MySqlClient.MySqlDbType.Decimal; |
126 | ············else if ( t == typeof(System.DateTime)) |
127 | ················return MySql.Data.MySqlClient.MySqlDbType.DateTime; |
128 | ············else if ( t.IsSubclassOf(typeof(System.Enum))) |
129 | ················return MySql.Data.MySqlClient.MySqlDbType.Int32; |
130 | ············else |
131 | ················throw new NDOException(27, "NDO.MySqlProvider.GetDbType: Typ " + t.Name + " kann nicht in MySql.Data.MySqlClient.MySqlDbType konvertiert werden"); |
132 | ········} |
133 | |
134 | ········// The following method converts string representations of MySql.Data.MySqlClient.MySqlDbType-Members |
135 | ········// into MySql.Data.MySqlClient.MySqlDbType. |
136 | ········// For your own adapter use members of the database type emumeration of your |
137 | ········// ADO.NET provider and convert it to the respective enumeration type········ |
138 | ········public override object GetDbType(string typeName) |
139 | ········{ |
140 | ············if (typeName == "BigInt") |
141 | return MySql. Data. MySqlClient. MySqlDbType. Int64; |
142 | ············if (typeName == "Blob") |
143 | ················return MySql.Data.MySqlClient.MySqlDbType.Blob; |
144 | ············if (typeName == "Byte") |
145 | ················return MySql.Data.MySqlClient.MySqlDbType.Byte; |
146 | ············if (typeName == "Date") |
147 | ················return MySql.Data.MySqlClient.MySqlDbType.Date; |
148 | ············if (typeName == "Datetime") |
149 | return MySql. Data. MySqlClient. MySqlDbType. DateTime; |
150 | ············if (typeName == "Decimal") |
151 | ················return MySql.Data.MySqlClient.MySqlDbType.Decimal; |
152 | ············if (typeName == "Double") |
153 | ················return MySql.Data.MySqlClient.MySqlDbType.Double; |
154 | ············if (typeName == "Enum") |
155 | ················return MySql.Data.MySqlClient.MySqlDbType.Enum; |
156 | ············if (typeName == "Float") |
157 | ················return MySql.Data.MySqlClient.MySqlDbType.Float; |
158 | ············if (typeName == "Int32") |
159 | ················return MySql.Data.MySqlClient.MySqlDbType.Int32; |
160 | ············if (typeName == "Int24") |
161 | ················return MySql.Data.MySqlClient.MySqlDbType.Int24; |
162 | ············if (typeName == "Long") |
163 | return MySql. Data. MySqlClient. MySqlDbType. Int64; |
164 | ············if (typeName == "LongBlob") |
165 | ················return MySql.Data.MySqlClient.MySqlDbType.LongBlob; |
166 | ············if (typeName == "LongLong") |
167 | return MySql. Data. MySqlClient. MySqlDbType. Int64; |
168 | ············if (typeName == "MediumBlob") |
169 | ················return MySql.Data.MySqlClient.MySqlDbType.MediumBlob; |
170 | ············if (typeName == "Newdate") |
171 | ················return MySql.Data.MySqlClient.MySqlDbType.Newdate; |
172 | ············if (typeName == "Set") |
173 | ················return MySql.Data.MySqlClient.MySqlDbType.Set; |
174 | ············if (typeName == "Int16") |
175 | ················return MySql.Data.MySqlClient.MySqlDbType.Int16; |
176 | ············if (typeName == "String") |
177 | ················return MySql.Data.MySqlClient.MySqlDbType.String; |
178 | ············if (typeName == "Time") |
179 | ················return MySql.Data.MySqlClient.MySqlDbType.Time; |
180 | ············if (typeName == "Timestamp") |
181 | ················return MySql.Data.MySqlClient.MySqlDbType.Timestamp; |
182 | ············if (typeName == "TinyBlob") |
183 | ················return MySql.Data.MySqlClient.MySqlDbType.TinyBlob; |
184 | ············if (typeName == "VarChar") |
185 | ················return MySql.Data.MySqlClient.MySqlDbType.VarChar; |
186 | ············if (typeName == "Year") |
187 | ················return MySql.Data.MySqlClient.MySqlDbType.Year;············ |
188 | ············throw new NDOException(27, "NDOMySql.Provider.GetDbType: Typname " + typeName + " kann nicht in MySql.Data.MySqlClient.MySqlDbType konvertiert werden"); |
189 | ········} |
190 | |
191 | ········public override string GetDbTypeString( IDbDataParameter parameter ) |
192 | ········{ |
193 | ············return (((MySqlParameter)parameter).MySqlDbType).ToString(); |
194 | ········} |
195 | |
196 | ········private string GetDateExpression(System.DateTime dt) |
197 | ········{ |
198 | ············//'9999-12-31 23:59:59' |
199 | ············return "'" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "'"; |
200 | ········} |
201 | ···· |
202 | |
203 | ········public override int GetDefaultLength(System.Type t) |
204 | ········{ |
205 | ············t = base.ConvertNullableType(t); |
206 | ············if ( t == typeof(bool) ) |
207 | ················return 1; |
208 | ············else if ( t == typeof(byte) ) |
209 | ················return 1; |
210 | ············else if ( t == typeof(sbyte) ) |
211 | ················return 1; |
212 | ············else if ( t == typeof(char) ) |
213 | ················return 2; |
214 | ············else if ( t == typeof(short)) |
215 | ················return 2; |
216 | ············else if ( t == typeof(ushort)) |
217 | ················return 2; |
218 | ············else if ( t == typeof(int)) |
219 | ················return 4; |
220 | ············else if ( t == typeof(uint)) |
221 | ················return 4; |
222 | ············else if ( t == typeof(long)) |
223 | ················return 8; |
224 | ············else if ( t == typeof(System.Guid)) |
225 | ················return 36; |
226 | ············else if ( t == typeof(ulong)) |
227 | ················return 8; |
228 | ············else if ( t == typeof(float)) |
229 | ················return 4; |
230 | ············else if ( t == typeof(double)) |
231 | ················return 8; |
232 | ············else if ( t == typeof(string)) |
233 | ················return 255; |
234 | ············else if ( t == typeof(byte[])) |
235 | ················return 100000; |
236 | ············else if ( t == typeof(decimal)) |
237 | ················return 18; |
238 | ············else if ( t == typeof(System.DateTime)) |
239 | ················return 16; |
240 | ············else if ( t.IsSubclassOf(typeof(System.Enum))) |
241 | ················return 4; |
242 | ············else |
243 | ················return 0;········ |
244 | ········} |
245 | |
246 | |
247 | ········public override string Wildcard |
248 | ········{ |
249 | ············get { return "%"; } |
250 | ········} |
251 | |
252 | ········public override bool UseNamedParams |
253 | ········{ |
254 | ············get { return true; } |
255 | ········} |
256 | |
257 | |
258 | ········#endregion |
259 | ········ |
260 | ········//private Hashtable namedParameters = new Hashtable(); |
261 | ········public override string GetNamedParameter(string plainParameterName) |
262 | ········{ |
263 | ············return "?" + plainParameterName; |
264 | ········} |
265 | ···· |
266 | ········public override string GetQuotedName(string plainName) |
267 | ········{ |
268 | ············return "`" + plainName + "`"; |
269 | ········} |
270 | ···· |
271 | ········public override string GetSqlLiteral(object o) |
272 | ········{ |
273 | ············if (o is DateTime) |
274 | ················return this.GetDateExpression((DateTime)o); |
275 | ············return base.GetSqlLiteral (o); |
276 | ········} |
277 | ········ |
278 | |
279 | ········/// <summary> |
280 | ········/// Indicates whether the last automatically generated ID can be retrieved. |
281 | ········/// Returns true if a database provides automatically incremented IDs and its syntax has an expression |
282 | ········/// which retrieves the last generated ID; otherwise false. |
283 | ········/// </summary> |
284 | ········public override bool SupportsLastInsertedId |
285 | ········{ |
286 | ············get { return true; } |
287 | ········} |
288 | |
289 | |
290 | ········/// <summary> |
291 | ········/// Gets an expression in the SQL dialect of the database, which retrieves the ID of the last |
292 | ········/// inserted row, if the ID is automatically generated by the database. |
293 | ········/// </summary> |
294 | ········public override string GetLastInsertedId(string tableName, string columnName) |
295 | ········{ |
296 | ············return "LAST_INSERT_ID()"; |
297 | ········} |
298 | |
299 | ········/// <summary> |
300 | ········/// Determines whether a database supports bulk command strings. |
301 | ········/// </summary> |
302 | ········public override bool SupportsBulkCommands |
303 | ········{ |
304 | ············get { return true; } |
305 | ········} |
306 | ········ |
307 | |
308 | ········/// <summary> |
309 | ········/// Generate one big command string out of several partial commands to save roundtrips |
310 | ········/// to the server. |
311 | ········/// </summary> |
312 | ········/// <param name="commands"></param> |
313 | ········/// <returns></returns> |
314 | ········public override string GenerateBulkCommand(string[] commands) |
315 | ········{ |
316 | ············StringBuilder sb = new StringBuilder(commands.Length * 100); |
317 | ············foreach (string s in commands) |
318 | ············{ |
319 | ················sb.Append(s); |
320 | ················sb.Append(';'); |
321 | ············} |
322 | ············return sb.ToString(); |
323 | ········} |
324 | |
325 | ············ |
326 | ········public override string[] GetTableNames(IDbConnection conn, string owner) |
327 | ········{ |
328 | ············MySqlCommand cmd = new MySqlCommand("show tables", (MySqlConnection) conn); |
329 | ············bool wasOpen = true; |
330 | ············if (conn.State == ConnectionState.Closed) |
331 | ············{ |
332 | ················conn.Open(); |
333 | ················wasOpen = false; |
334 | ············} |
335 | ············MySqlDataReader dr = cmd.ExecuteReader(); |
336 | ············IList result = new ArrayList(); |
337 | ············while (dr.Read()) |
338 | ················result.Add(dr.GetString(0)); |
339 | ············dr.Close(); |
340 | ············if (!wasOpen) |
341 | ················conn.Close(); |
342 | ············string[] strresult = new string[result.Count]; |
343 | ············for (int i = 0; i < result.Count; i++) |
344 | ················strresult[i] = (string) result[i]; |
345 | ············return strresult; |
346 | ········} |
347 | ···· |
348 | ········public override string[] TypeNames |
349 | ········{ |
350 | ············get |
351 | ············{················ |
352 | ················return Enum.GetNames(typeof(MySql.Data.MySqlClient.MySqlDbType)); |
353 | ············} |
354 | ········} |
355 | |
356 | ········public override string Name { get { return "MySql"; }··} |
357 | |
358 | ········public override bool SupportsInsertBatch |
359 | ········{ |
360 | ············get |
361 | ············{ |
362 | ················return true; |
363 | ············} |
364 | ········} |
365 | |
366 | ········public override bool SupportsNativeGuidType |
367 | ········{ |
368 | ············get { return false; } |
369 | ········} |
370 | |
371 | ········public override string FetchLimit( int skip, int take ) |
372 | ········{ |
373 | ············return "LIMIT " + take + " OFFSET " + skip; |
374 | ········} |
375 | |
376 | |
377 | ········public override string CreateDatabase(string databaseName, string connectionString, object additionalData) |
378 | ········{ |
379 | ············base.CreateDatabase( databaseName, connectionString, additionalData ); |
380 | |
381 | ············Regex regex = new Regex(@"Database\s*=([^\;]*)"); |
382 | ············Match match = regex.Match( connectionString ); |
383 | ············if (match.Success) |
384 | ············{ |
385 | ················return connectionString.Substring(0, match.Groups[1].Index) + databaseName + connectionString.Substring(match.Index + match.Length); |
386 | ············} |
387 | ············ |
388 | ············if (!connectionString.EndsWith(";")) |
389 | ················connectionString = connectionString + ";"; |
390 | ············ |
391 | ············return connectionString + "Database=" + databaseName; |
392 | ········} |
393 | ····} |
394 | } |
395 |
New Commit (f90cc9c)
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.Text; |
25 | using System.Text.RegularExpressions; |
26 | using System.Data; |
27 | using System.Data.Common; |
28 | using NDOInterfaces; |
29 | using System.Collections; |
30 | using MySql.Data.MySqlClient; |
31 | using MySql.Data.Types; |
32 | |
33 | namespace NDO.MySqlProvider |
34 | { |
35 | ····/// <summary> |
36 | ····/// Sample adapter class to connect new ADO.NET Providers with NDO. |
37 | ····/// This Adapter is based on the MySql MySql Data Connector |
38 | ····/// </summary> |
39 | ····public class Provider : NDOAbstractProvider |
40 | ····{ |
41 | ········// The following methods provide objects of provider classes |
42 | ········// which implement common interfaces in .NET: |
43 | ········// IDbConnection, IDbCommand, DbDataAdapter and the Parameter objects |
44 | ········#region Provide specialized type objects |
45 | ········public override System.Data.IDbConnection NewConnection(string connectionString) |
46 | ········{ |
47 | ············return new MySqlConnection(connectionString); |
48 | ········} |
49 | |
50 | ········public override System.Data.IDbCommand NewSqlCommand(System.Data.IDbConnection connection) |
51 | ········{ |
52 | ············MySqlCommand command = new MySqlCommand(); |
53 | ············command.Connection = (MySqlConnection)connection; |
54 | ············return command; |
55 | ········} |
56 | |
57 | ········public override DbDataAdapter NewDataAdapter(System.Data.IDbCommand select, System.Data.IDbCommand update, System.Data.IDbCommand insert, System.Data.IDbCommand delete) |
58 | ········{ |
59 | ············MySqlDataAdapter da = new MySqlDataAdapter(); |
60 | ············da.SelectCommand = (MySqlCommand)select; |
61 | ············da.UpdateCommand = (MySqlCommand)update; |
62 | ············da.InsertCommand = (MySqlCommand)insert; |
63 | ············da.DeleteCommand = (MySqlCommand)delete; |
64 | ············return da; |
65 | ········} |
66 | |
67 | ········/// <summary> |
68 | ········/// See <see cref="IProvider"> IProvider interface </see> |
69 | ········/// </summary> |
70 | ········public override object NewCommandBuilder(DbDataAdapter dataAdapter) |
71 | ········{ |
72 | ············return new MySqlCommandBuilder((MySqlDataAdapter)dataAdapter); |
73 | ········} |
74 | |
75 | |
76 | ········public override IDataParameter AddParameter(System.Data.IDbCommand command, string parameterName, object dbType, int size, string columnName) |
77 | ········{ |
78 | ············return ((MySqlCommand)command).Parameters.Add(new MySqlParameter(parameterName, (MySql.Data.MySqlClient.MySqlDbType)dbType, size, columnName));············ |
79 | ········} |
80 | |
81 | ········public override IDataParameter AddParameter(IDbCommand command, string parameterName, object dbType, int size, ParameterDirection dir, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value) |
82 | ········{ |
83 | ············return ((MySqlCommand)command).Parameters.Add(new MySqlParameter(parameterName, (MySql.Data.MySqlClient.MySqlDbType)dbType, size, dir, isNullable, precision, scale, srcColumn, srcVersion, value)); |
84 | ········} |
85 | ········#endregion |
86 | |
87 | ········// The following method convert System.Type objects to MySql.Data.MySqlClient.MySqlDbType-Members |
88 | ········// For your own adapter use members of the database type emumeration |
89 | ········// of your ADO.NET provider |
90 | ········#region Provide MySql.Data.MySqlClient.MySqlDbType members |
91 | ········public override object GetDbType(Type t) |
92 | ········{ |
93 | ············t = base.ConvertNullableType(t); |
94 | ············if ( t == typeof(bool) ) |
95 | ················return MySql.Data.MySqlClient.MySqlDbType.Byte; |
96 | ············else if ( t == typeof(byte) ) |
97 | ················return MySql.Data.MySqlClient.MySqlDbType.Byte; |
98 | ············else if ( t == typeof(sbyte) ) |
99 | ················return MySql.Data.MySqlClient.MySqlDbType.Byte; |
100 | ············else if ( t == typeof(char) ) |
101 | ················return MySql.Data.MySqlClient.MySqlDbType.Int16; |
102 | ············else if ( t == typeof(short)) |
103 | ················return MySql.Data.MySqlClient.MySqlDbType.Int16; |
104 | ············else if ( t == typeof(ushort)) |
105 | ················return MySql.Data.MySqlClient.MySqlDbType.Int16; |
106 | ············else if ( t == typeof(int)) |
107 | ················return MySql.Data.MySqlClient.MySqlDbType.Int32; |
108 | ············else if ( t == typeof(uint)) |
109 | ················return MySql.Data.MySqlClient.MySqlDbType.Int32; |
110 | ············else if ( t == typeof(long)) |
111 | ················return MySql.Data.MySqlClient.MySqlDbType.Int64; |
112 | ············else if ( t == typeof(System.Guid)) |
113 | ················return MySql.Data.MySqlClient.MySqlDbType.VarChar; |
114 | ············else if ( t == typeof(ulong)) |
115 | ················return MySql.Data.MySqlClient.MySqlDbType.Int64; |
116 | ············else if ( t == typeof(float)) |
117 | ················return MySql.Data.MySqlClient.MySqlDbType.Float; |
118 | ············else if ( t == typeof(double)) |
119 | ················return MySql.Data.MySqlClient.MySqlDbType.Double; |
120 | ············else if ( t == typeof(string)) |
121 | ················return MySql.Data.MySqlClient.MySqlDbType.VarChar; |
122 | ············else if ( t == typeof(byte[])) |
123 | ················return MySql.Data.MySqlClient.MySqlDbType.MediumBlob; |
124 | ············else if ( t == typeof(decimal)) |
125 | ················return MySql.Data.MySqlClient.MySqlDbType.Decimal; |
126 | ············else if ( t == typeof(System.DateTime)) |
127 | ················return MySql.Data.MySqlClient.MySqlDbType.DateTime; |
128 | ············else if ( t.IsSubclassOf(typeof(System.Enum))) |
129 | ················return MySql.Data.MySqlClient.MySqlDbType.Int32; |
130 | ············else |
131 | ················throw new NDOException(27, "NDO.MySqlProvider.GetDbType: Typ " + t.Name + " kann nicht in MySql.Data.MySqlClient.MySqlDbType konvertiert werden"); |
132 | ········} |
133 | |
134 | ········// The following method converts string representations of MySql.Data.MySqlClient.MySqlDbType-Members |
135 | ········// into MySql.Data.MySqlClient.MySqlDbType. |
136 | ········// For your own adapter use members of the database type emumeration of your |
137 | ········// ADO.NET provider and convert it to the respective enumeration type········ |
138 | ········public override object GetDbType(string typeName) |
139 | ········{ |
140 | ············if (Enum.TryParse<MySqlDbType>( typeName, out var dbtype )) |
141 | ················return dbtype; |
142 | ············if (typeName == "BigInt") |
143 | return MySqlDbType. Int64; |
144 | ············if (typeName == "Datetime") |
145 | return MySqlDbType. DateTime; |
146 | ············if (typeName == "Long") |
147 | return MySqlDbType. Int64; |
148 | ············if (typeName == "LongLong") |
149 | return MySqlDbType. Int64; |
150 | ············throw new NDOException(27, "NDOMySql.Provider.GetDbType: Typname " + typeName + " kann nicht in MySql.Data.MySqlClient.MySqlDbType konvertiert werden"); |
151 | ········} |
152 | |
153 | ········public override string GetDbTypeString( IDbDataParameter parameter ) |
154 | ········{ |
155 | ············return (((MySqlParameter)parameter).MySqlDbType).ToString(); |
156 | ········} |
157 | |
158 | ········private string GetDateExpression(System.DateTime dt) |
159 | ········{ |
160 | ············//'9999-12-31 23:59:59' |
161 | ············return "'" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "'"; |
162 | ········} |
163 | ···· |
164 | |
165 | ········public override int GetDefaultLength(System.Type t) |
166 | ········{ |
167 | ············t = base.ConvertNullableType(t); |
168 | ············if ( t == typeof(bool) ) |
169 | ················return 1; |
170 | ············else if ( t == typeof(byte) ) |
171 | ················return 1; |
172 | ············else if ( t == typeof(sbyte) ) |
173 | ················return 1; |
174 | ············else if ( t == typeof(char) ) |
175 | ················return 2; |
176 | ············else if ( t == typeof(short)) |
177 | ················return 2; |
178 | ············else if ( t == typeof(ushort)) |
179 | ················return 2; |
180 | ············else if ( t == typeof(int)) |
181 | ················return 4; |
182 | ············else if ( t == typeof(uint)) |
183 | ················return 4; |
184 | ············else if ( t == typeof(long)) |
185 | ················return 8; |
186 | ············else if ( t == typeof(System.Guid)) |
187 | ················return 36; |
188 | ············else if ( t == typeof(ulong)) |
189 | ················return 8; |
190 | ············else if ( t == typeof(float)) |
191 | ················return 4; |
192 | ············else if ( t == typeof(double)) |
193 | ················return 8; |
194 | ············else if ( t == typeof(string)) |
195 | ················return 255; |
196 | ············else if ( t == typeof(byte[])) |
197 | ················return 100000; |
198 | ············else if ( t == typeof(decimal)) |
199 | ················return 18; |
200 | ············else if ( t == typeof(System.DateTime)) |
201 | ················return 16; |
202 | ············else if ( t.IsSubclassOf(typeof(System.Enum))) |
203 | ················return 4; |
204 | ············else |
205 | ················return 0;········ |
206 | ········} |
207 | |
208 | |
209 | ········public override string Wildcard |
210 | ········{ |
211 | ············get { return "%"; } |
212 | ········} |
213 | |
214 | ········public override bool UseNamedParams |
215 | ········{ |
216 | ············get { return true; } |
217 | ········} |
218 | |
219 | |
220 | ········#endregion |
221 | ········ |
222 | ········//private Hashtable namedParameters = new Hashtable(); |
223 | ········public override string GetNamedParameter(string plainParameterName) |
224 | ········{ |
225 | ············return "?" + plainParameterName; |
226 | ········} |
227 | ···· |
228 | ········public override string GetQuotedName(string plainName) |
229 | ········{ |
230 | ············return "`" + plainName + "`"; |
231 | ········} |
232 | ···· |
233 | ········public override string GetSqlLiteral(object o) |
234 | ········{ |
235 | ············if (o is DateTime) |
236 | ················return this.GetDateExpression((DateTime)o); |
237 | ············return base.GetSqlLiteral (o); |
238 | ········} |
239 | ········ |
240 | |
241 | ········/// <summary> |
242 | ········/// Indicates whether the last automatically generated ID can be retrieved. |
243 | ········/// Returns true if a database provides automatically incremented IDs and its syntax has an expression |
244 | ········/// which retrieves the last generated ID; otherwise false. |
245 | ········/// </summary> |
246 | ········public override bool SupportsLastInsertedId |
247 | ········{ |
248 | ············get { return true; } |
249 | ········} |
250 | |
251 | |
252 | ········/// <summary> |
253 | ········/// Gets an expression in the SQL dialect of the database, which retrieves the ID of the last |
254 | ········/// inserted row, if the ID is automatically generated by the database. |
255 | ········/// </summary> |
256 | ········public override string GetLastInsertedId(string tableName, string columnName) |
257 | ········{ |
258 | ············return "LAST_INSERT_ID()"; |
259 | ········} |
260 | |
261 | ········/// <summary> |
262 | ········/// Determines whether a database supports bulk command strings. |
263 | ········/// </summary> |
264 | ········public override bool SupportsBulkCommands |
265 | ········{ |
266 | ············get { return true; } |
267 | ········} |
268 | ········ |
269 | |
270 | ········/// <summary> |
271 | ········/// Generate one big command string out of several partial commands to save roundtrips |
272 | ········/// to the server. |
273 | ········/// </summary> |
274 | ········/// <param name="commands"></param> |
275 | ········/// <returns></returns> |
276 | ········public override string GenerateBulkCommand(string[] commands) |
277 | ········{ |
278 | ············StringBuilder sb = new StringBuilder(commands.Length * 100); |
279 | ············foreach (string s in commands) |
280 | ············{ |
281 | ················sb.Append(s); |
282 | ················sb.Append(';'); |
283 | ············} |
284 | ············return sb.ToString(); |
285 | ········} |
286 | |
287 | ············ |
288 | ········public override string[] GetTableNames(IDbConnection conn, string owner) |
289 | ········{ |
290 | ············MySqlCommand cmd = new MySqlCommand("show tables", (MySqlConnection) conn); |
291 | ············bool wasOpen = true; |
292 | ············if (conn.State == ConnectionState.Closed) |
293 | ············{ |
294 | ················conn.Open(); |
295 | ················wasOpen = false; |
296 | ············} |
297 | ············MySqlDataReader dr = cmd.ExecuteReader(); |
298 | ············IList result = new ArrayList(); |
299 | ············while (dr.Read()) |
300 | ················result.Add(dr.GetString(0)); |
301 | ············dr.Close(); |
302 | ············if (!wasOpen) |
303 | ················conn.Close(); |
304 | ············string[] strresult = new string[result.Count]; |
305 | ············for (int i = 0; i < result.Count; i++) |
306 | ················strresult[i] = (string) result[i]; |
307 | ············return strresult; |
308 | ········} |
309 | ···· |
310 | ········public override string[] TypeNames |
311 | ········{ |
312 | ············get |
313 | ············{················ |
314 | ················return Enum.GetNames(typeof(MySql.Data.MySqlClient.MySqlDbType)); |
315 | ············} |
316 | ········} |
317 | |
318 | ········public override string Name { get { return "MySql"; }··} |
319 | |
320 | ········public override bool SupportsInsertBatch |
321 | ········{ |
322 | ············get |
323 | ············{ |
324 | ················return true; |
325 | ············} |
326 | ········} |
327 | |
328 | ········public override bool SupportsNativeGuidType |
329 | ········{ |
330 | ············get { return false; } |
331 | ········} |
332 | |
333 | ········public override string FetchLimit( int skip, int take ) |
334 | ········{ |
335 | ············return "LIMIT " + take + " OFFSET " + skip; |
336 | ········} |
337 | |
338 | |
339 | ········public override string CreateDatabase(string databaseName, string connectionString, object additionalData) |
340 | ········{ |
341 | ············base.CreateDatabase( databaseName, connectionString, additionalData ); |
342 | |
343 | ············Regex regex = new Regex(@"Database\s*=([^\;]*)"); |
344 | ············Match match = regex.Match( connectionString ); |
345 | ············if (match.Success) |
346 | ············{ |
347 | ················return connectionString.Substring(0, match.Groups[1].Index) + databaseName + connectionString.Substring(match.Index + match.Length); |
348 | ············} |
349 | ············ |
350 | ············if (!connectionString.EndsWith(";")) |
351 | ················connectionString = connectionString + ";"; |
352 | ············ |
353 | ············return connectionString + "Database=" + databaseName; |
354 | ········} |
355 | ····} |
356 | } |
357 |