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