Datei: Provider/MySqlConnectorProvider/NDO.MySqlConnector/MySqlProvider.cs

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