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

Last Commit (118b681)
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 ( Enum. TryParse<MySqlDbType>( typeName, out var dbtype ) )
140 ················return dbtype;
141 if ( typeName == "BigInt")
142 ················return MySqlDbType.Int64;
143 if ( typeName == "Datetime")
144 ················return MySqlDbType.DateTime;
145 if ( typeName == "Long")
146 ················return MySqlDbType.Int64;
147 if ( typeName == "LongLong")
148 ················return MySqlDbType.Int64;
149 throw new NDOException( 27, "MySqlConnector. Provider. GetDbType: Typname " + typeName + " kann nicht in MySqlDbType konvertiert werden") ;
150 ········}
151
152 ········public override string GetDbTypeString( IDbDataParameter parameter )
153 ········{
154 ············return (((MySqlParameter)parameter).MySqlDbType).ToString();
155 ········}
156
157 ········private string GetDateExpression(System.DateTime dt)
158 ········{
159 ············//'9999-12-31 23:59:59'
160 ············return "'" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "'";
161 ········}
162 ····
163
164 ········public override int GetDefaultLength(System.Type t)
165 ········{
166 ············t = base.ConvertNullableType(t);
167 ············if ( t == typeof(bool) )
168 ················return 1;
169 ············else if ( t == typeof(byte) )
170 ················return 1;
171 ············else if ( t == typeof(sbyte) )
172 ················return 1;
173 ············else if ( t == typeof(char) )
174 ················return 2;
175 ············else if ( t == typeof(short))
176 ················return 2;
177 ············else if ( t == typeof(ushort))
178 ················return 2;
179 ············else if ( t == typeof(int))
180 ················return 4;
181 ············else if ( t == typeof(uint))
182 ················return 4;
183 ············else if ( t == typeof(long))
184 ················return 8;
185 ············else if ( t == typeof(System.Guid))
186 ················return 36;
187 ············else if ( t == typeof(ulong))
188 ················return 8;
189 ············else if ( t == typeof(float))
190 ················return 4;
191 ············else if ( t == typeof(double))
192 ················return 8;
193 ············else if ( t == typeof(string))
194 ················return 255;
195 ············else if ( t == typeof(byte[]))
196 ················return 100000;
197 ············else if ( t == typeof(decimal))
198 ················return 18;
199 ············else if ( t == typeof(System.DateTime))
200 ················return 16;
201 ············else if ( t.IsSubclassOf(typeof(System.Enum)))
202 ················return 4;
203 ············else
204 ················return 0;········
205 ········}
206
207
208 ········public override string Wildcard
209 ········{
210 ············get { return "%"; }
211 ········}
212
213 ········public override bool UseNamedParams
214 ········{
215 ············get { return true; }
216 ········}
217
218
219 ········#endregion
220 ········
221 ········//private Hashtable namedParameters = new Hashtable();
222 ········public override string GetNamedParameter(string plainParameterName)
223 ········{
224 ············return "?" + plainParameterName;
225 ········}
226 ····
227 ········public override string GetQuotedName(string plainName)
228 ········{
229 ············return "`" + plainName + "`";
230 ········}
231 ····
232 ········public override string GetSqlLiteral(object o)
233 ········{
234 ············if (o is DateTime)
235 ················return this.GetDateExpression((DateTime)o);
236 ············return base.GetSqlLiteral (o);
237 ········}
238 ········
239
240 ········/// <summary>
241 ········/// Indicates whether the last automatically generated ID can be retrieved.
242 ········/// Returns true if a database provides automatically incremented IDs and its syntax has an expression
243 ········/// which retrieves the last generated ID; otherwise false.
244 ········/// </summary>
245 ········public override bool SupportsLastInsertedId
246 ········{
247 ············get { return true; }
248 ········}
249
250
251 ········/// <summary>
252 ········/// Gets an expression in the SQL dialect of the database, which retrieves the ID of the last
253 ········/// inserted row, if the ID is automatically generated by the database.
254 ········/// </summary>
255 ········public override string GetLastInsertedId(string tableName, string columnName)
256 ········{
257 ············return "LAST_INSERT_ID()";
258 ········}
259
260 ········/// <summary>
261 ········/// Determines whether a database supports bulk command strings.
262 ········/// </summary>
263 ········public override bool SupportsBulkCommands
264 ········{
265 ············get { return true; }
266 ········}
267 ········
268
269 ········/// <summary>
270 ········/// Generate one big command string out of several partial commands to save roundtrips
271 ········/// to the server.
272 ········/// </summary>
273 ········/// <param name="commands"></param>
274 ········/// <returns></returns>
275 ········public override string GenerateBulkCommand(string[] commands)
276 ········{
277 ············StringBuilder sb = new StringBuilder(commands.Length * 100);
278 ············foreach (string s in commands)
279 ············{
280 ················sb.Append(s);
281 ················sb.Append(';');
282 ············}
283 ············return sb.ToString();
284 ········}
285
286 ············
287 ········public override string[] GetTableNames(IDbConnection conn, string owner)
288 ········{
289 ············MySqlCommand cmd = new MySqlCommand("show tables", (MySqlConnection) conn);
290 ············bool wasOpen = true;
291 ············if (conn.State == ConnectionState.Closed)
292 ············{
293 ················conn.Open();
294 ················wasOpen = false;
295 ············}
296 ············MySqlDataReader dr = cmd.ExecuteReader();
297 ············IList result = new ArrayList();
298 ············while (dr.Read())
299 ················result.Add(dr.GetString(0));
300 ············dr.Close();
301 ············if (!wasOpen)
302 ················conn.Close();
303 ············string[] strresult = new string[result.Count];
304 ············for (int i = 0; i < result.Count; i++)
305 ················strresult[i] = (string) result[i];
306 ············return strresult;
307 ········}
308 ····
309 ········public override string[] TypeNames
310 ········{
311 ············get
312 ············{················
313 ················return Enum.GetNames(typeof(MySqlDbType));
314 ············}
315 ········}
316
317 ········public override string Name { get { return "MySqlConnector"; }··}
318
319 ········public override bool SupportsInsertBatch
320 ········{
321 ············get
322 ············{
323 ················return true;
324 ············}
325 ········}
326
327 ········public override bool SupportsNativeGuidType
328 ········{
329 ············get { return false; }
330 ········}
331
332 ········public override string FetchLimit( int skip, int take )
333 ········{
334 ············return "LIMIT " + take + " OFFSET " + skip;
335 ········}
336
337
338 ········public override string CreateDatabase(string databaseName, string connectionString, object additionalData)
339 ········{
340 ············base.CreateDatabase( databaseName, connectionString, additionalData );
341
342 ············Regex regex = new Regex(@"Database\s*=([^\;]*)");
343 ············Match match = regex.Match( connectionString );
344 ············if (match.Success)
345 ············{
346 ················return connectionString.Substring(0, match.Groups[1].Index) + databaseName + connectionString.Substring(match.Index + match.Length);
347 ············}
348 ············
349 ············if (!connectionString.EndsWith(";"))
350 ················connectionString = connectionString + ";";
351 ············
352 ············return connectionString + "Database=" + databaseName;
353 ········}
354 ····}
355 }
356
New Commit (48a2b1a)
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 ( Enum. TryParse<MySqlDbType>( typeName, true, out var dbtype ) )
140 ················return dbtype;
141 if ( typeName. Equals( "BigInt", StringComparison. InvariantCultureIgnoreCase ) )
142 ················return MySqlDbType.Int64;
143 if ( typeName. Equals( "Datetime", StringComparison. InvariantCultureIgnoreCase ) )
144 ················return MySqlDbType.DateTime;
145 if ( typeName. Equals( "Long", StringComparison. InvariantCultureIgnoreCase ) )
146 ················return MySqlDbType.Int64;
147 if ( typeName. Equals( "LongLong", StringComparison. InvariantCultureIgnoreCase ) )
148 ················return MySqlDbType.Int64;
149 throw new NDOException( 27, "MySqlConnector. Provider. GetDbType: Typname " + typeName + " kann nicht in MySqlDbType konvertiert werden" ) ;
150 ········}
151
152 ········public override string GetDbTypeString( IDbDataParameter parameter )
153 ········{
154 ············return (((MySqlParameter)parameter).MySqlDbType).ToString();
155 ········}
156
157 ········private string GetDateExpression(System.DateTime dt)
158 ········{
159 ············//'9999-12-31 23:59:59'
160 ············return "'" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "'";
161 ········}
162 ····
163
164 ········public override int GetDefaultLength(System.Type t)
165 ········{
166 ············t = base.ConvertNullableType(t);
167 ············if ( t == typeof(bool) )
168 ················return 1;
169 ············else if ( t == typeof(byte) )
170 ················return 1;
171 ············else if ( t == typeof(sbyte) )
172 ················return 1;
173 ············else if ( t == typeof(char) )
174 ················return 2;
175 ············else if ( t == typeof(short))
176 ················return 2;
177 ············else if ( t == typeof(ushort))
178 ················return 2;
179 ············else if ( t == typeof(int))
180 ················return 4;
181 ············else if ( t == typeof(uint))
182 ················return 4;
183 ············else if ( t == typeof(long))
184 ················return 8;
185 ············else if ( t == typeof(System.Guid))
186 ················return 36;
187 ············else if ( t == typeof(ulong))
188 ················return 8;
189 ············else if ( t == typeof(float))
190 ················return 4;
191 ············else if ( t == typeof(double))
192 ················return 8;
193 ············else if ( t == typeof(string))
194 ················return 255;
195 ············else if ( t == typeof(byte[]))
196 ················return 100000;
197 ············else if ( t == typeof(decimal))
198 ················return 18;
199 ············else if ( t == typeof(System.DateTime))
200 ················return 16;
201 ············else if ( t.IsSubclassOf(typeof(System.Enum)))
202 ················return 4;
203 ············else
204 ················return 0;········
205 ········}
206
207
208 ········public override string Wildcard
209 ········{
210 ············get { return "%"; }
211 ········}
212
213 ········public override bool UseNamedParams
214 ········{
215 ············get { return true; }
216 ········}
217
218
219 ········#endregion
220 ········
221 ········//private Hashtable namedParameters = new Hashtable();
222 ········public override string GetNamedParameter(string plainParameterName)
223 ········{
224 ············return "?" + plainParameterName;
225 ········}
226 ····
227 ········public override string GetQuotedName(string plainName)
228 ········{
229 ············return "`" + plainName + "`";
230 ········}
231 ····
232 ········public override string GetSqlLiteral(object o)
233 ········{
234 ············if (o is DateTime)
235 ················return this.GetDateExpression((DateTime)o);
236 ············return base.GetSqlLiteral (o);
237 ········}
238 ········
239
240 ········/// <summary>
241 ········/// Indicates whether the last automatically generated ID can be retrieved.
242 ········/// Returns true if a database provides automatically incremented IDs and its syntax has an expression
243 ········/// which retrieves the last generated ID; otherwise false.
244 ········/// </summary>
245 ········public override bool SupportsLastInsertedId
246 ········{
247 ············get { return true; }
248 ········}
249
250
251 ········/// <summary>
252 ········/// Gets an expression in the SQL dialect of the database, which retrieves the ID of the last
253 ········/// inserted row, if the ID is automatically generated by the database.
254 ········/// </summary>
255 ········public override string GetLastInsertedId(string tableName, string columnName)
256 ········{
257 ············return "LAST_INSERT_ID()";
258 ········}
259
260 ········/// <summary>
261 ········/// Determines whether a database supports bulk command strings.
262 ········/// </summary>
263 ········public override bool SupportsBulkCommands
264 ········{
265 ············get { return true; }
266 ········}
267 ········
268
269 ········/// <summary>
270 ········/// Generate one big command string out of several partial commands to save roundtrips
271 ········/// to the server.
272 ········/// </summary>
273 ········/// <param name="commands"></param>
274 ········/// <returns></returns>
275 ········public override string GenerateBulkCommand(string[] commands)
276 ········{
277 ············StringBuilder sb = new StringBuilder(commands.Length * 100);
278 ············foreach (string s in commands)
279 ············{
280 ················sb.Append(s);
281 ················sb.Append(';');
282 ············}
283 ············return sb.ToString();
284 ········}
285
286 ············
287 ········public override string[] GetTableNames(IDbConnection conn, string owner)
288 ········{
289 ············MySqlCommand cmd = new MySqlCommand("show tables", (MySqlConnection) conn);
290 ············bool wasOpen = true;
291 ············if (conn.State == ConnectionState.Closed)
292 ············{
293 ················conn.Open();
294 ················wasOpen = false;
295 ············}
296 ············MySqlDataReader dr = cmd.ExecuteReader();
297 ············IList result = new ArrayList();
298 ············while (dr.Read())
299 ················result.Add(dr.GetString(0));
300 ············dr.Close();
301 ············if (!wasOpen)
302 ················conn.Close();
303 ············string[] strresult = new string[result.Count];
304 ············for (int i = 0; i < result.Count; i++)
305 ················strresult[i] = (string) result[i];
306 ············return strresult;
307 ········}
308 ····
309 ········public override string[] TypeNames
310 ········{
311 ············get
312 ············{················
313 ················return Enum.GetNames(typeof(MySqlDbType));
314 ············}
315 ········}
316
317 ········public override string Name { get { return "MySqlConnector"; }··}
318
319 ········public override bool SupportsInsertBatch
320 ········{
321 ············get
322 ············{
323 ················return true;
324 ············}
325 ········}
326
327 ········public override bool SupportsNativeGuidType
328 ········{
329 ············get { return false; }
330 ········}
331
332 ········public override string FetchLimit( int skip, int take )
333 ········{
334 ············return "LIMIT " + take + " OFFSET " + skip;
335 ········}
336
337
338 ········public override string CreateDatabase(string databaseName, string connectionString, object additionalData)
339 ········{
340 ············base.CreateDatabase( databaseName, connectionString, additionalData );
341
342 ············Regex regex = new Regex(@"Database\s*=([^\;]*)");
343 ············Match match = regex.Match( connectionString );
344 ············if (match.Success)
345 ············{
346 ················return connectionString.Substring(0, match.Groups[1].Index) + databaseName + connectionString.Substring(match.Index + match.Length);
347 ············}
348 ············
349 ············if (!connectionString.EndsWith(";"))
350 ················connectionString = connectionString + ";";
351 ············
352 ············return connectionString + "Database=" + databaseName;
353 ········}
354 ····}
355 }
356