Datei: NDOInterfaces/NDOAbstractProvider.cs
Last Commit (bbf1d57)
| 1 | // |
| 2 | // Copyright (c) 2002-2024 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.Data; |
| 25 | using System.Data.Common; |
| 26 | using System.Text; |
| 27 | using System.Collections; |
| 28 | using NDOInterfaces; |
| 29 | using NDO; |
| 30 | using System.Globalization; |
| 31 | |
| 32 | namespace NDOInterfaces |
| 33 | { |
| 34 | ····/// <summary> |
| 35 | ····/// Contains standard implementation for providers. |
| 36 | ····/// </summary> |
| 37 | ····public abstract class NDOAbstractProvider : IProvider |
| 38 | ····{ |
| 39 | ········ |
| 40 | ···· |
| 41 | ········#region Implementation of IProvider |
| 42 | |
| 43 | |
| 44 | ········/// <summary> |
| 45 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 46 | ········/// </summary> |
| 47 | ········public abstract DbConnection NewConnection(string parameters); |
| 48 | |
| 49 | ········/// <summary> |
| 50 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 51 | ········/// </summary> |
| 52 | ········public abstract DbCommand NewSqlCommand(DbConnection connection); |
| 53 | |
| 54 | ········/// <summary> |
| 55 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 56 | ········/// </summary> |
| 57 | ········public abstract IDataParameter AddParameter(DbCommand command, string parameterName, object dbType, int size, string columnName); |
| 58 | |
| 59 | ········/// <summary> |
| 60 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 61 | ········/// </summary> |
| 62 | ········public abstract IDataParameter AddParameter(DbCommand command, string parameterName, object dbType, int size, System.Data.ParameterDirection dir, bool isNullable, byte precision, byte scale, string srcColumn, System.Data.DataRowVersion srcVersion, object value); |
| 63 | ········ |
| 64 | ········/// <summary> |
| 65 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 66 | ········/// </summary> |
| 67 | ········public abstract object GetDbType(System.Type t); |
| 68 | |
| 69 | ········/// <summary> |
| 70 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 71 | ········/// </summary> |
| 72 | ········public abstract object GetDbType(string dbTypeName); |
| 73 | |
| 74 | ········/// <summary> |
| 75 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 76 | ········/// </summary> |
| 77 | ········public virtual int GetDefaultLength(System.Type t) |
| 78 | ········{ |
| 79 | ············if ( t == typeof(bool) ) |
| 80 | ················return 4; |
| 81 | ············else if ( t == typeof(byte) ) |
| 82 | ················return 1; |
| 83 | ············else if ( t == typeof(sbyte) ) |
| 84 | ················return 1; |
| 85 | ············else if ( t == typeof(char) ) |
| 86 | ················return 2; |
| 87 | ············else if ( t == typeof(short)) |
| 88 | ················return 2; |
| 89 | ············else if ( t == typeof(ushort)) |
| 90 | ················return 2; |
| 91 | ············else if ( t == typeof(int)) |
| 92 | ················return 4; |
| 93 | ············else if ( t == typeof(uint)) |
| 94 | ················return 4; |
| 95 | ············else if ( t == typeof(long)) |
| 96 | ················return 8; |
| 97 | ············else if ( t == typeof(ulong)) |
| 98 | ················return 8; |
| 99 | ············else if ( t == typeof(float)) |
| 100 | ················return 8; |
| 101 | ············else if ( t == typeof(double)) |
| 102 | ················return 8; |
| 103 | ············else if ( t == typeof(string)) |
| 104 | ················return 255; |
| 105 | ············else if ( t == typeof(decimal)) |
| 106 | ················return 11; |
| 107 | ············else if ( t == typeof(DateTime)) |
| 108 | ················return 4; |
| 109 | ············else if ( t == typeof(Guid)) |
| 110 | ················return 16; |
| 111 | ············else if ( t == typeof(Byte[])) |
| 112 | ················return 1024; |
| 113 | ············else if ( t.IsSubclassOf(typeof(System.Enum))) |
| 114 | ················return 4; |
| 115 | ············else |
| 116 | ················return 0; |
| 117 | ········} |
| 118 | |
| 119 | ········/// <summary> |
| 120 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 121 | ········/// </summary> |
| 122 | ········public virtual string Wildcard |
| 123 | ········{ |
| 124 | ············get { return "%"; } |
| 125 | ········} |
| 126 | |
| 127 | ········/// <summary> |
| 128 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 129 | ········/// </summary> |
| 130 | ········public virtual bool UseNamedParams |
| 131 | ········{ |
| 132 | ············get { return false; } |
| 133 | ········} |
| 134 | |
| 135 | |
| 136 | ········/// <summary> |
| 137 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 138 | ········/// </summary> |
| 139 | ········public virtual string NamedParamPrefix |
| 140 | ········{ |
| 141 | ············get { return string.Empty; } |
| 142 | ········} |
| 143 | |
| 144 | ········/// <summary> |
| 145 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 146 | ········/// </summary> |
| 147 | ········public virtual bool UseStoredProcedure |
| 148 | ········{ |
| 149 | ············get { return false; } |
| 150 | ········} |
| 151 | |
| 152 | ···· |
| 153 | ········/// <summary> |
| 154 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 155 | ········/// </summary> |
| 156 | ········public virtual string GetNamedParameter(string plainParameterName) |
| 157 | ········{ |
| 158 | ············return plainParameterName; |
| 159 | ········} |
| 160 | ···· |
| 161 | ········/// <summary> |
| 162 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 163 | ········/// </summary> |
| 164 | ········public virtual string GetQuotedName(string plainName) |
| 165 | ········{ |
| 166 | ············return plainName; |
| 167 | ········} |
| 168 | |
| 169 | ········/// <summary> |
| 170 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 171 | ········/// </summary> |
| 172 | ········public virtual string GetSqlLiteral(object o) |
| 173 | ········{ |
| 174 | ············if (o == null) |
| 175 | ················return "NULL"; |
| 176 | ············if (o is string || o is Guid) |
| 177 | ················return "'" + o.ToString() + "'"; |
| 178 | |
| 179 | ············if (o is byte[]) |
| 180 | ············{ |
| 181 | ················StringBuilder sb = new StringBuilder(((byte[])o).Length * 2 + 2); |
| 182 | ················sb.Append('\''); |
| 183 | ················foreach (byte b in (byte[])o) |
| 184 | ················{ |
| 185 | ····················sb.Append(b.ToString("X2")); |
| 186 | ················} |
| 187 | ················sb.Append('\''); |
| 188 | ················return sb.ToString(); |
| 189 | ············} |
| 190 | |
| 191 | ············var ci = CultureInfo.InvariantCulture; |
| 192 | |
| 193 | ············if (o is DateTime dt) |
| 194 | ················return "'" + dt.ToString( "yyyy-MM-dd HH:mm:ss" ) + "'"; |
| 195 | ············if (o is double d) |
| 196 | ················return d.ToString( ci ); |
| 197 | ············if (o is float f) |
| 198 | ················return f.ToString( ci ); |
| 199 | ············if (o is decimal dc) |
| 200 | ················return dc.ToString( ci ); |
| 201 | |
| 202 | ············return o.ToString(); |
| 203 | ········} |
| 204 | |
| 205 | |
| 206 | ········/// <summary> |
| 207 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 208 | ········/// </summary> |
| 209 | ········public virtual bool SupportsLastInsertedId |
| 210 | ········{ |
| 211 | ············get {return false;} |
| 212 | ········} |
| 213 | |
| 214 | ········/// <summary> |
| 215 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 216 | ········/// </summary> |
| 217 | ········public virtual string GetLastInsertedId(string tableName, string columnName) |
| 218 | ········{ |
| 219 | ············return null; |
| 220 | ········} |
| 221 | |
| 222 | ········/// <summary> |
| 223 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 224 | ········/// </summary> |
| 225 | ········public virtual bool SupportsBulkCommands |
| 226 | ········{ |
| 227 | ············get { return false; } |
| 228 | ········} |
| 229 | ········ |
| 230 | |
| 231 | ········/// <summary> |
| 232 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 233 | ········/// </summary> |
| 234 | ········public virtual string GenerateBulkCommand(string[] commands) |
| 235 | ········{ |
| 236 | ············return null; |
| 237 | ········} |
| 238 | |
| 239 | |
| 240 | ········/// <summary> |
| 241 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 242 | ········/// </summary> |
| 243 | ········public virtual int MaxBulkCommandLength |
| 244 | ········{ |
| 245 | ············get { return 0; } |
| 246 | ········} |
| 247 | |
| 248 | |
| 249 | ········/// <summary> |
| 250 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 251 | ········/// </summary> |
| 252 | ········public abstract string[] TypeNames { get; } |
| 253 | |
| 254 | ········/// <summary> |
| 255 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 256 | ········/// </summary> |
| 257 | ········public string[] GetTableNames (DbConnection conn) |
| 258 | ········{ |
| 259 | ············return GetTableNames(conn, null); |
| 260 | ········} |
| 261 | |
| 262 | ········/// <summary> |
| 263 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 264 | ········/// </summary> |
| 265 | ········public abstract string[] GetTableNames(DbConnection conn, string owner); |
| 266 | |
| 267 | |
| 268 | ········/// <summary> |
| 269 | ········/// Generates a DataSet with exactly the same structure elements as the database. |
| 270 | ········/// It maps all tables, columns, primary keys and foreign key constraints of the database |
| 271 | ········/// to the DataSet. |
| 272 | ········/// </summary> |
| 273 | ········/// <remarks> |
| 274 | ········/// This implementation fetches only the tables and columns. |
| 275 | ········/// </remarks> |
| 276 | ········public virtual DataSet GetDatabaseStructure( DbConnection conn, string ownerName ) |
| 277 | ········{ |
| 278 | ············bool wasOpen = conn.State == ConnectionState.Open; |
| 279 | ············if (!wasOpen) |
| 280 | ················conn.Open(); |
| 281 | |
| 282 | ············DataSet ds = new DataSet(); |
| 283 | ············try |
| 284 | ············{ |
| 285 | ················foreach (string tableName in this.GetTableNames( conn, ownerName )) |
| 286 | ················{ |
| 287 | ····················string sql; |
| 288 | |
| 289 | ····················if ( ownerName != null && ownerName.Trim() != "" ) |
| 290 | ····················{ |
| 291 | ························sql = "SELECT * FROM " + this.GetQuotedName( ownerName ) + "." + this.GetQuotedName( tableName ); |
| 292 | ····················} |
| 293 | ····················else |
| 294 | ····················{ |
| 295 | ························sql = "SELECT * FROM " + this.GetQuotedName( tableName ); |
| 296 | ····················} |
| 297 | |
| 298 | ····················DbCommand cmd = this.NewSqlCommand( conn ); |
| 299 | ····················cmd.CommandText = sql; |
| 300 | |
| 301 | ····················// DataTable.Load builds the schema from reader.GetSchemaTable() using the same |
| 302 | ····················// schema mapping as DbDataAdapter.FillSchema. SchemaOnly delivers no rows. |
| 303 | ····················DataTable dt = new DataTable( tableName ); |
| 304 | ····················using (DbDataReader reader = cmd.ExecuteReader( CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo )) |
| 305 | ····················{ |
| 306 | ························dt.Load( reader ); |
| 307 | ····················} |
| 308 | ····················ds.Tables.Add( dt ); |
| 309 | ················} |
| 310 | ············} |
| 311 | ············finally |
| 312 | ············{ |
| 313 | ················if (!wasOpen) |
| 314 | ····················conn.Close(); |
| 315 | ············} |
| 316 | |
| 317 | ············return ds; |
| 318 | ········} |
| 319 | |
| 320 | |
| 321 | ········/// <summary> |
| 322 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 323 | ········/// </summary> |
| 324 | ········public abstract string Name { get; } |
| 325 | |
| 326 | ········/// <summary> |
| 327 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 328 | ········/// </summary> |
| 329 | ········public virtual bool SupportsInsertBatch |
| 330 | ········{ |
| 331 | ············get { return false; } |
| 332 | ········} |
| 333 | |
| 334 | ········/// <summary> |
| 335 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 336 | ········/// </summary> |
| 337 | ········public abstract bool SupportsNativeGuidType { get; } |
| 338 | |
| 339 | ········/// <summary> |
| 340 | ········/// Fetch limits are used for paging. If the database supports paging, it provides an addition to OrderBy which |
| 341 | ········/// allows to specify a count of rows to skip and a maximum count of rows to take. |
| 342 | ········/// </summary> |
| 343 | ········public virtual bool SupportsFetchLimit |
| 344 | ········{ |
| 345 | ············get { return true; } |
| 346 | ········} |
| 347 | |
| 348 | ········/// <summary> |
| 349 | ········/// Fetch limits are used for paging. If the database supports paging, it provides an addition to OrderBy which |
| 350 | ········/// allows to specify a count of rows to skip and a maximum count of rows to take. |
| 351 | ········/// </summary> |
| 352 | ········/// <param name="skip"></param> |
| 353 | ········/// <param name="take"></param> |
| 354 | ········/// <returns>A string, which is concatenated to the OrderBy clause</returns> |
| 355 | ········public virtual string FetchLimit( int skip, int take ) |
| 356 | ········{ |
| 357 | ············if (skip == 0 && take == 0) |
| 358 | ················return string.Empty; |
| 359 | ············// Standard Sql implementation |
| 360 | ············string fetch = String.Empty; |
| 361 | ············if (take != 0) |
| 362 | ················fetch = String.Format( " FETCH NEXT {0} ROWS ONLY", take ); |
| 363 | ············return String.Format("OFFSET {0} ROWS{1}", skip, fetch); |
| 364 | ········} |
| 365 | |
| 366 | ········/// <summary> |
| 367 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 368 | ········/// </summary> |
| 369 | ········public virtual string CreateDatabase(string databaseName, string connectionString, object additionalData = null) |
| 370 | ········{ |
| 371 | ············if (databaseName == null) |
| 372 | ················throw new ArgumentException( nameof( databaseName ) ); |
| 373 | ············if (connectionString == null) |
| 374 | ················throw new ArgumentException( nameof( connectionString ) ); |
| 375 | |
| 376 | ············string dbName = this.GetQuotedName(databaseName); |
| 377 | ············try |
| 378 | ············{ |
| 379 | ················var conn = this.NewConnection(connectionString); |
| 380 | ················DbCommand cmd = this.NewSqlCommand(conn); |
| 381 | ················cmd.CommandText = "CREATE DATABASE " + dbName; |
| 382 | ················bool wasOpen = true; |
| 383 | ················if (conn.State == ConnectionState.Closed) |
| 384 | ················{ |
| 385 | ····················conn.Open(); |
| 386 | ····················wasOpen = false; |
| 387 | ················} |
| 388 | ················cmd.ExecuteNonQuery(); |
| 389 | ················if (!wasOpen) |
| 390 | ····················conn.Close(); |
| 391 | ············} |
| 392 | ············catch (Exception ex) |
| 393 | ············{ |
| 394 | throw new NDOException( 19, "Error while attempting to create a database: Exception Type: " + ex. GetType( ) . Name + " Message: " + ex. Message) ; |
| 395 | ············} |
| 396 | |
| 397 | ············return string.Empty; |
| 398 | ········} |
| 399 | |
| 400 | |
| 401 | ········#endregion |
| 402 | |
| 403 | ········/// <summary> |
| 404 | ········/// Called by the providers to get the generic argument type of a Nullable. |
| 405 | ········/// </summary> |
| 406 | ········/// <param name="t">The type to convert.</param> |
| 407 | ········/// <returns>If t is a Nullable, the generic argument type is returned. Else t is returned.</returns> |
| 408 | ········public Type ConvertNullableType(Type t) |
| 409 | ········{ |
| 410 | ············if (t.IsGenericParameter) |
| 411 | ················return typeof(string); |
| 412 | ············if (t.FullName.StartsWith("System.Nullable`1")) |
| 413 | ················t = t.GetGenericArguments()[0]; |
| 414 | ············if (t.IsEnum) |
| 415 | ················return Enum.GetUnderlyingType(t); |
| 416 | ············return t; |
| 417 | ········} |
| 418 | |
| 419 | ········/// <summary> |
| 420 | ········/// Gets the string representation of the provider specific parameter type. |
| 421 | ········/// </summary> |
| 422 | ········/// <param name="parameter">An IDbDataParameter implementation.</param> |
| 423 | ········/// <returns>The string Representation of the parameter.</returns> |
| 424 | ········/// <remarks> |
| 425 | ········/// Each ADO.NET implementation uses it's own parameter types, e.g. SqlDbType for SqlParameter. |
| 426 | ········/// We need the names of this parameters to support the Loggin. |
| 427 | ········/// </remarks> |
| 428 | ········public virtual string GetDbTypeString( IDbDataParameter parameter ) |
| 429 | ········{ |
| 430 | ············return parameter.DbType.ToString(); |
| 431 | ········} |
| 432 | |
| 433 | ········/// <inheritdoc/> |
| 434 | ········public virtual object GetConnectionId( DbConnection connection ) |
| 435 | ········{ |
| 436 | ············return ConnectionIdProvider.Get( connection ); |
| 437 | ········} |
| 438 | ····} |
| 439 | } |
| 440 |
New Commit (4a40b5b)
| 1 | // |
| 2 | // Copyright (c) 2002-2024 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.Data; |
| 25 | using System.Data.Common; |
| 26 | using System.Text; |
| 27 | using System.Collections; |
| 28 | using NDOInterfaces; |
| 29 | using NDO; |
| 30 | using System.Globalization; |
| 31 | |
| 32 | namespace NDOInterfaces |
| 33 | { |
| 34 | ····/// <summary> |
| 35 | ····/// Contains standard implementation for providers. |
| 36 | ····/// </summary> |
| 37 | ····public abstract class NDOAbstractProvider : IProvider |
| 38 | ····{ |
| 39 | ········ |
| 40 | ···· |
| 41 | ········#region Implementation of IProvider |
| 42 | |
| 43 | |
| 44 | ········/// <summary> |
| 45 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 46 | ········/// </summary> |
| 47 | ········public abstract DbConnection NewConnection(string parameters); |
| 48 | |
| 49 | ········/// <summary> |
| 50 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 51 | ········/// </summary> |
| 52 | ········public abstract DbCommand NewSqlCommand(DbConnection connection); |
| 53 | |
| 54 | ········/// <summary> |
| 55 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 56 | ········/// </summary> |
| 57 | ········public abstract IDataParameter AddParameter(DbCommand command, string parameterName, object dbType, int size, string columnName); |
| 58 | |
| 59 | ········/// <summary> |
| 60 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 61 | ········/// </summary> |
| 62 | ········public abstract IDataParameter AddParameter(DbCommand command, string parameterName, object dbType, int size, System.Data.ParameterDirection dir, bool isNullable, byte precision, byte scale, string srcColumn, System.Data.DataRowVersion srcVersion, object value); |
| 63 | ········ |
| 64 | ········/// <summary> |
| 65 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 66 | ········/// </summary> |
| 67 | ········public abstract object GetDbType(System.Type t); |
| 68 | |
| 69 | ········/// <summary> |
| 70 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 71 | ········/// </summary> |
| 72 | ········public abstract object GetDbType(string dbTypeName); |
| 73 | |
| 74 | ········/// <summary> |
| 75 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 76 | ········/// </summary> |
| 77 | ········public virtual int GetDefaultLength(System.Type t) |
| 78 | ········{ |
| 79 | ············if ( t == typeof(bool) ) |
| 80 | ················return 4; |
| 81 | ············else if ( t == typeof(byte) ) |
| 82 | ················return 1; |
| 83 | ············else if ( t == typeof(sbyte) ) |
| 84 | ················return 1; |
| 85 | ············else if ( t == typeof(char) ) |
| 86 | ················return 2; |
| 87 | ············else if ( t == typeof(short)) |
| 88 | ················return 2; |
| 89 | ············else if ( t == typeof(ushort)) |
| 90 | ················return 2; |
| 91 | ············else if ( t == typeof(int)) |
| 92 | ················return 4; |
| 93 | ············else if ( t == typeof(uint)) |
| 94 | ················return 4; |
| 95 | ············else if ( t == typeof(long)) |
| 96 | ················return 8; |
| 97 | ············else if ( t == typeof(ulong)) |
| 98 | ················return 8; |
| 99 | ············else if ( t == typeof(float)) |
| 100 | ················return 8; |
| 101 | ············else if ( t == typeof(double)) |
| 102 | ················return 8; |
| 103 | ············else if ( t == typeof(string)) |
| 104 | ················return 255; |
| 105 | ············else if ( t == typeof(decimal)) |
| 106 | ················return 11; |
| 107 | ············else if ( t == typeof(DateTime)) |
| 108 | ················return 4; |
| 109 | ············else if ( t == typeof(Guid)) |
| 110 | ················return 16; |
| 111 | ············else if ( t == typeof(Byte[])) |
| 112 | ················return 1024; |
| 113 | ············else if ( t.IsSubclassOf(typeof(System.Enum))) |
| 114 | ················return 4; |
| 115 | ············else |
| 116 | ················return 0; |
| 117 | ········} |
| 118 | |
| 119 | ········/// <summary> |
| 120 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 121 | ········/// </summary> |
| 122 | ········public virtual string Wildcard |
| 123 | ········{ |
| 124 | ············get { return "%"; } |
| 125 | ········} |
| 126 | |
| 127 | ········/// <summary> |
| 128 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 129 | ········/// </summary> |
| 130 | ········public virtual bool UseNamedParams |
| 131 | ········{ |
| 132 | ············get { return false; } |
| 133 | ········} |
| 134 | |
| 135 | |
| 136 | ········/// <summary> |
| 137 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 138 | ········/// </summary> |
| 139 | ········public virtual string NamedParamPrefix |
| 140 | ········{ |
| 141 | ············get { return string.Empty; } |
| 142 | ········} |
| 143 | |
| 144 | ········/// <summary> |
| 145 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 146 | ········/// </summary> |
| 147 | ········public virtual bool UseStoredProcedure |
| 148 | ········{ |
| 149 | ············get { return false; } |
| 150 | ········} |
| 151 | |
| 152 | ···· |
| 153 | ········/// <summary> |
| 154 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 155 | ········/// </summary> |
| 156 | ········public virtual string GetNamedParameter(string plainParameterName) |
| 157 | ········{ |
| 158 | ············return plainParameterName; |
| 159 | ········} |
| 160 | ···· |
| 161 | ········/// <summary> |
| 162 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 163 | ········/// </summary> |
| 164 | ········public virtual string GetQuotedName(string plainName) |
| 165 | ········{ |
| 166 | ············return plainName; |
| 167 | ········} |
| 168 | |
| 169 | ········/// <summary> |
| 170 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 171 | ········/// </summary> |
| 172 | ········public virtual string GetSqlLiteral(object o) |
| 173 | ········{ |
| 174 | ············if (o == null) |
| 175 | ················return "NULL"; |
| 176 | ············if (o is string || o is Guid) |
| 177 | ················return "'" + o.ToString() + "'"; |
| 178 | |
| 179 | ············if (o is byte[]) |
| 180 | ············{ |
| 181 | ················StringBuilder sb = new StringBuilder(((byte[])o).Length * 2 + 2); |
| 182 | ················sb.Append('\''); |
| 183 | ················foreach (byte b in (byte[])o) |
| 184 | ················{ |
| 185 | ····················sb.Append(b.ToString("X2")); |
| 186 | ················} |
| 187 | ················sb.Append('\''); |
| 188 | ················return sb.ToString(); |
| 189 | ············} |
| 190 | |
| 191 | ············var ci = CultureInfo.InvariantCulture; |
| 192 | |
| 193 | ············if (o is DateTime dt) |
| 194 | ················return "'" + dt.ToString( "yyyy-MM-dd HH:mm:ss" ) + "'"; |
| 195 | ············if (o is double d) |
| 196 | ················return d.ToString( ci ); |
| 197 | ············if (o is float f) |
| 198 | ················return f.ToString( ci ); |
| 199 | ············if (o is decimal dc) |
| 200 | ················return dc.ToString( ci ); |
| 201 | |
| 202 | ············return o.ToString(); |
| 203 | ········} |
| 204 | |
| 205 | |
| 206 | ········/// <summary> |
| 207 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 208 | ········/// </summary> |
| 209 | ········public virtual bool SupportsLastInsertedId |
| 210 | ········{ |
| 211 | ············get {return false;} |
| 212 | ········} |
| 213 | |
| 214 | ········/// <summary> |
| 215 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 216 | ········/// </summary> |
| 217 | ········public virtual string GetLastInsertedId(string tableName, string columnName) |
| 218 | ········{ |
| 219 | ············return null; |
| 220 | ········} |
| 221 | |
| 222 | ········/// <summary> |
| 223 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 224 | ········/// </summary> |
| 225 | ········public virtual bool SupportsBulkCommands |
| 226 | ········{ |
| 227 | ············get { return false; } |
| 228 | ········} |
| 229 | ········ |
| 230 | |
| 231 | ········/// <summary> |
| 232 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 233 | ········/// </summary> |
| 234 | ········public virtual string GenerateBulkCommand(string[] commands) |
| 235 | ········{ |
| 236 | ············return null; |
| 237 | ········} |
| 238 | |
| 239 | |
| 240 | ········/// <summary> |
| 241 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 242 | ········/// </summary> |
| 243 | ········public virtual int MaxBulkCommandLength |
| 244 | ········{ |
| 245 | ············get { return 0; } |
| 246 | ········} |
| 247 | |
| 248 | |
| 249 | ········/// <summary> |
| 250 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 251 | ········/// </summary> |
| 252 | ········public abstract string[] TypeNames { get; } |
| 253 | |
| 254 | ········/// <summary> |
| 255 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 256 | ········/// </summary> |
| 257 | ········public string[] GetTableNames (DbConnection conn) |
| 258 | ········{ |
| 259 | ············return GetTableNames(conn, null); |
| 260 | ········} |
| 261 | |
| 262 | ········/// <summary> |
| 263 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 264 | ········/// </summary> |
| 265 | ········public abstract string[] GetTableNames(DbConnection conn, string owner); |
| 266 | |
| 267 | |
| 268 | ········/// <summary> |
| 269 | ········/// Generates a DataSet with exactly the same structure elements as the database. |
| 270 | ········/// It maps all tables, columns, primary keys and foreign key constraints of the database |
| 271 | ········/// to the DataSet. |
| 272 | ········/// </summary> |
| 273 | ········/// <remarks> |
| 274 | ········/// This implementation fetches only the tables and columns. |
| 275 | ········/// </remarks> |
| 276 | ········public virtual DataSet GetDatabaseStructure( DbConnection conn, string ownerName ) |
| 277 | ········{ |
| 278 | ············bool wasOpen = conn.State == ConnectionState.Open; |
| 279 | ············if (!wasOpen) |
| 280 | ················conn.Open(); |
| 281 | |
| 282 | ············DataSet ds = new DataSet(); |
| 283 | ············try |
| 284 | ············{ |
| 285 | ················foreach (string tableName in this.GetTableNames( conn, ownerName )) |
| 286 | ················{ |
| 287 | ····················string sql; |
| 288 | |
| 289 | ····················if ( ownerName != null && ownerName.Trim() != "" ) |
| 290 | ····················{ |
| 291 | ························sql = "SELECT * FROM " + this.GetQuotedName( ownerName ) + "." + this.GetQuotedName( tableName ); |
| 292 | ····················} |
| 293 | ····················else |
| 294 | ····················{ |
| 295 | ························sql = "SELECT * FROM " + this.GetQuotedName( tableName ); |
| 296 | ····················} |
| 297 | |
| 298 | ····················DbCommand cmd = this.NewSqlCommand( conn ); |
| 299 | ····················cmd.CommandText = sql; |
| 300 | |
| 301 | ····················// DataTable.Load builds the schema from reader.GetSchemaTable() using the same |
| 302 | ····················// schema mapping as DbDataAdapter.FillSchema. SchemaOnly delivers no rows. |
| 303 | ····················DataTable dt = new DataTable( tableName ); |
| 304 | ····················using (DbDataReader reader = cmd.ExecuteReader( CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo )) |
| 305 | ····················{ |
| 306 | ························dt.Load( reader ); |
| 307 | ····················} |
| 308 | ····················ds.Tables.Add( dt ); |
| 309 | ················} |
| 310 | ············} |
| 311 | ············finally |
| 312 | ············{ |
| 313 | ················if (!wasOpen) |
| 314 | ····················conn.Close(); |
| 315 | ············} |
| 316 | |
| 317 | ············return ds; |
| 318 | ········} |
| 319 | |
| 320 | |
| 321 | ········/// <summary> |
| 322 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 323 | ········/// </summary> |
| 324 | ········public abstract string Name { get; } |
| 325 | |
| 326 | ········/// <summary> |
| 327 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 328 | ········/// </summary> |
| 329 | ········public virtual bool SupportsInsertBatch |
| 330 | ········{ |
| 331 | ············get { return false; } |
| 332 | ········} |
| 333 | |
| 334 | ········/// <summary> |
| 335 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 336 | ········/// </summary> |
| 337 | ········public abstract bool SupportsNativeGuidType { get; } |
| 338 | |
| 339 | ········/// <summary> |
| 340 | ········/// Fetch limits are used for paging. If the database supports paging, it provides an addition to OrderBy which |
| 341 | ········/// allows to specify a count of rows to skip and a maximum count of rows to take. |
| 342 | ········/// </summary> |
| 343 | ········public virtual bool SupportsFetchLimit |
| 344 | ········{ |
| 345 | ············get { return true; } |
| 346 | ········} |
| 347 | |
| 348 | ········/// <summary> |
| 349 | ········/// Fetch limits are used for paging. If the database supports paging, it provides an addition to OrderBy which |
| 350 | ········/// allows to specify a count of rows to skip and a maximum count of rows to take. |
| 351 | ········/// </summary> |
| 352 | ········/// <param name="skip"></param> |
| 353 | ········/// <param name="take"></param> |
| 354 | ········/// <returns>A string, which is concatenated to the OrderBy clause</returns> |
| 355 | ········public virtual string FetchLimit( int skip, int take ) |
| 356 | ········{ |
| 357 | ············if (skip == 0 && take == 0) |
| 358 | ················return string.Empty; |
| 359 | ············// Standard Sql implementation |
| 360 | ············string fetch = String.Empty; |
| 361 | ············if (take != 0) |
| 362 | ················fetch = String.Format( " FETCH NEXT {0} ROWS ONLY", take ); |
| 363 | ············return String.Format("OFFSET {0} ROWS{1}", skip, fetch); |
| 364 | ········} |
| 365 | |
| 366 | ········/// <summary> |
| 367 | ········/// See <see cref="IProvider">IProvider interface</see>. |
| 368 | ········/// </summary> |
| 369 | ········public virtual string CreateDatabase(string databaseName, string connectionString, object additionalData = null) |
| 370 | ········{ |
| 371 | ············if (databaseName == null) |
| 372 | ················throw new ArgumentException( nameof( databaseName ) ); |
| 373 | ············if (connectionString == null) |
| 374 | ················throw new ArgumentException( nameof( connectionString ) ); |
| 375 | |
| 376 | ············string dbName = this.GetQuotedName(databaseName); |
| 377 | ············try |
| 378 | ············{ |
| 379 | ················var conn = this.NewConnection(connectionString); |
| 380 | ················DbCommand cmd = this.NewSqlCommand(conn); |
| 381 | ················cmd.CommandText = "CREATE DATABASE " + dbName; |
| 382 | ················bool wasOpen = true; |
| 383 | ················if (conn.State == ConnectionState.Closed) |
| 384 | ················{ |
| 385 | ····················conn.Open(); |
| 386 | ····················wasOpen = false; |
| 387 | ················} |
| 388 | ················cmd.ExecuteNonQuery(); |
| 389 | ················if (!wasOpen) |
| 390 | ····················conn.Close(); |
| 391 | ············} |
| 392 | ············catch (Exception ex) |
| 393 | ············{ |
| 394 | throw new NDOException( 19, "Error while attempting to create a database. ", ex) ; |
| 395 | ············} |
| 396 | |
| 397 | ············return string.Empty; |
| 398 | ········} |
| 399 | |
| 400 | |
| 401 | ········#endregion |
| 402 | |
| 403 | ········/// <summary> |
| 404 | ········/// Called by the providers to get the generic argument type of a Nullable. |
| 405 | ········/// </summary> |
| 406 | ········/// <param name="t">The type to convert.</param> |
| 407 | ········/// <returns>If t is a Nullable, the generic argument type is returned. Else t is returned.</returns> |
| 408 | ········public Type ConvertNullableType(Type t) |
| 409 | ········{ |
| 410 | ············if (t.IsGenericParameter) |
| 411 | ················return typeof(string); |
| 412 | ············if (t.FullName.StartsWith("System.Nullable`1")) |
| 413 | ················t = t.GetGenericArguments()[0]; |
| 414 | ············if (t.IsEnum) |
| 415 | ················return Enum.GetUnderlyingType(t); |
| 416 | ············return t; |
| 417 | ········} |
| 418 | |
| 419 | ········/// <summary> |
| 420 | ········/// Gets the string representation of the provider specific parameter type. |
| 421 | ········/// </summary> |
| 422 | ········/// <param name="parameter">An IDbDataParameter implementation.</param> |
| 423 | ········/// <returns>The string Representation of the parameter.</returns> |
| 424 | ········/// <remarks> |
| 425 | ········/// Each ADO.NET implementation uses it's own parameter types, e.g. SqlDbType for SqlParameter. |
| 426 | ········/// We need the names of this parameters to support the Loggin. |
| 427 | ········/// </remarks> |
| 428 | ········public virtual string GetDbTypeString( IDbDataParameter parameter ) |
| 429 | ········{ |
| 430 | ············return parameter.DbType.ToString(); |
| 431 | ········} |
| 432 | |
| 433 | ········/// <inheritdoc/> |
| 434 | ········public virtual object GetConnectionId( DbConnection connection ) |
| 435 | ········{ |
| 436 | ············return ConnectionIdProvider.Get( connection ); |
| 437 | ········} |
| 438 | ····} |
| 439 | } |
| 440 |