Datei: Provider/SqliteProvider/NDO.Sqlite/Provider.cs
Last Commit (4b748a1)
| 1 | // |
| 2 | // Copyright (c) 2002-2019 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.Data; |
| 26 | using System.Data.Common; |
| 27 | using NDOInterfaces; |
| 28 | using System.Data.SQLite; |
| 29 | |
| 30 | namespace System.Data.SQLite |
| 31 | { |
| 32 | ····public enum SQLiteDbType |
| 33 | ····{ |
| 34 | ········Text, |
| 35 | ········Numeric, |
| 36 | ········Integer, |
| 37 | ········Decimal, |
| 38 | ········Int2, |
| 39 | ········Real, |
| 40 | ········Datetime, |
| 41 | ········Blob, |
| 42 | ········Guid |
| 43 | ····} |
| 44 | } |
| 45 | |
| 46 | namespace NDO.SqliteProvider |
| 47 | { |
| 48 | ····/// <summary> |
| 49 | ····/// Sample adapter class to connect new ADO.NET Providers with NDO. |
| 50 | ····/// This Adapter is based on the SQLite SQLite Data Connector |
| 51 | ····/// </summary> |
| 52 | ····public class Provider : NDOAbstractProvider |
| 53 | ····{ |
| 54 | ········public Provider() |
| 55 | ········{ |
| 56 | ········} |
| 57 | |
| 58 | ········// The following methods provide objects of provider classes |
| 59 | ········// which implement common interfaces in .NET: |
| 60 | ········// IDbConnection, IDbCommand, DbDataAdapter and the Parameter objects |
| 61 | ········#region Provide specialized type objects |
| 62 | public override INdoDbConnection NewConnection( string connectionString) |
| 63 | ········{ |
| 64 | ············return new NdoDbConnection( new SQLiteConnection(connectionString) ); |
| 65 | ········} |
| 66 | |
| 67 | public override IDbCommand NewSqlCommand( INdoDbConnection connection) |
| 68 | ········{ |
| 69 | ············SQLiteCommand command = new SQLiteCommand(); |
| 70 | command. Connection = ( SQLiteConnection) connection. InnerConnection; |
| 71 | ············return command; |
| 72 | ········} |
| 73 | |
| 74 | ········public override DbDataAdapter NewDataAdapter(IDbCommand select, IDbCommand update, IDbCommand insert, IDbCommand delete) |
| 75 | ········{ |
| 76 | ············SQLiteDataAdapter da = new SQLiteDataAdapter(); |
| 77 | ············da.SelectCommand = (SQLiteCommand)select; |
| 78 | ············da.UpdateCommand = (SQLiteCommand)update; |
| 79 | ············da.InsertCommand = (SQLiteCommand)insert; |
| 80 | ············da.DeleteCommand = (SQLiteCommand)delete; |
| 81 | ············return da; |
| 82 | ········} |
| 83 | |
| 84 | ········/// <summary> |
| 85 | ········/// See <see cref="IProvider"> IProvider interface </see> |
| 86 | ········/// </summary> |
| 87 | ········public override object NewCommandBuilder(DbDataAdapter dataAdapter) |
| 88 | ········{ |
| 89 | ············return new SQLiteCommandBuilder((SQLiteDataAdapter)dataAdapter); |
| 90 | ········} |
| 91 | |
| 92 | |
| 93 | ········public override IDataParameter AddParameter(IDbCommand command, string parameterName, object odbType, int size, string columnName) |
| 94 | ········{ |
| 95 | ············DbType dbType = GetDbTypeFromSqliteDbType( (SQLiteDbType) odbType ); |
| 96 | ············SQLiteParameter result = new SQLiteParameter( parameterName, dbType, size, columnName ); |
| 97 | ············((SQLiteCommand)command).Parameters.Add(result); |
| 98 | ············return result; |
| 99 | ········} |
| 100 | |
| 101 | ········public override IDataParameter AddParameter(IDbCommand command, string parameterName, object odbType, int size, ParameterDirection dir, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value) |
| 102 | ········{ |
| 103 | ············DbType dbType = GetDbTypeFromSqliteDbType( (SQLiteDbType) odbType ); |
| 104 | ············SQLiteParameter result = new SQLiteParameter( parameterName, dbType, size, dir, isNullable, precision, scale, srcColumn, srcVersion, value ); |
| 105 | ············((SQLiteCommand)command).Parameters.Add(result); |
| 106 | ············return result; |
| 107 | ········} |
| 108 | |
| 109 | ········private DbType GetDbTypeFromSqliteDbType( SQLiteDbType sqliteDbType ) |
| 110 | ········{ |
| 111 | ············switch ( sqliteDbType ) |
| 112 | ············{ |
| 113 | ················case SQLiteDbType.Text: |
| 114 | ····················return DbType.String; |
| 115 | ················case SQLiteDbType.Numeric: |
| 116 | ····················return DbType.VarNumeric; |
| 117 | ················case SQLiteDbType.Integer: |
| 118 | ····················return DbType.Int32; |
| 119 | ················case SQLiteDbType.Int2: |
| 120 | ····················return DbType.Int16; |
| 121 | ················case SQLiteDbType.Decimal: |
| 122 | ····················return DbType.Decimal; |
| 123 | ················case SQLiteDbType.Real: |
| 124 | ····················return DbType.Double; |
| 125 | ················case SQLiteDbType.Datetime: |
| 126 | ····················return DbType.DateTime; |
| 127 | ················case SQLiteDbType.Blob: |
| 128 | ····················return DbType.Binary; |
| 129 | ················case SQLiteDbType.Guid: |
| 130 | ····················return DbType.String; |
| 131 | ············} |
| 132 | ············return DbType.String; |
| 133 | ········} |
| 134 | |
| 135 | ········#endregion |
| 136 | |
| 137 | ········// The following method convert System.Type objects to SQLiteDbType-Members |
| 138 | ········// For your own adapter use members of the database type emumeration |
| 139 | ········// of your ADO.NET provider |
| 140 | ········#region Provide SQLiteDbType members |
| 141 | ········public override object GetDbType(Type t) |
| 142 | ········{ |
| 143 | ············t = base.ConvertNullableType(t); |
| 144 | ············return GetInternalDbType( t ); |
| 145 | ········} |
| 146 | |
| 147 | ········internal static SQLiteDbType GetInternalDbType( Type t ) |
| 148 | ········{ |
| 149 | ············if ( t == typeof( bool ) ) |
| 150 | ················return SQLiteDbType.Int2; |
| 151 | ············else if ( t == typeof( byte ) ) |
| 152 | ················return SQLiteDbType.Int2; |
| 153 | ············else if ( t == typeof( sbyte ) ) |
| 154 | ················return SQLiteDbType.Int2; |
| 155 | ············else if ( t == typeof( char ) ) |
| 156 | ················return SQLiteDbType.Int2; |
| 157 | ············else if ( t == typeof( short ) ) |
| 158 | ················return SQLiteDbType.Int2; |
| 159 | ············else if ( t == typeof( ushort ) ) |
| 160 | ················return SQLiteDbType.Int2; |
| 161 | ············else if ( t == typeof( int ) ) |
| 162 | ················return SQLiteDbType.Integer; |
| 163 | ············else if ( t == typeof( uint ) ) |
| 164 | ················return SQLiteDbType.Integer; |
| 165 | ············else if ( t == typeof( long ) ) |
| 166 | ················return SQLiteDbType.Numeric; |
| 167 | ············else if ( t == typeof( System.Guid ) ) |
| 168 | ················return SQLiteDbType.Guid; |
| 169 | ············else if ( t == typeof( ulong ) ) |
| 170 | ················return SQLiteDbType.Numeric; |
| 171 | ············else if ( t == typeof( float ) ) |
| 172 | ················return SQLiteDbType.Real; |
| 173 | ············else if ( t == typeof( double ) ) |
| 174 | ················return SQLiteDbType.Real; |
| 175 | ············else if ( t == typeof( string ) ) |
| 176 | ················return SQLiteDbType.Text; |
| 177 | ············else if ( t == typeof( byte[] ) ) |
| 178 | ················return SQLiteDbType.Blob; |
| 179 | ············else if ( t == typeof( decimal ) ) |
| 180 | ················return SQLiteDbType.Decimal; |
| 181 | ············else if ( t == typeof( System.DateTime ) ) |
| 182 | ················return SQLiteDbType.Datetime; |
| 183 | ············else if ( t.IsSubclassOf( typeof( System.Enum ) ) ) |
| 184 | ················return SQLiteDbType.Integer; |
| 185 | ············else |
| 186 | ················throw new NDOException( 27, "NDO.SQLiteProvider.GetDbType: Type " + t.Name + " can't be converted into SQLiteDbType." ); |
| 187 | ········} |
| 188 | |
| 189 | ········// The following method converts string representations of SQLiteDbType-Members |
| 190 | ········// into SQLiteDbType. |
| 191 | ········// For your own adapter use members of the database type emumeration of your |
| 192 | ········// ADO.NET provider and convert it to the respective enumeration type········ |
| 193 | ········public override object GetDbType(string typeName) |
| 194 | ········{ |
| 195 | ············SQLiteDbType result; |
| 196 | |
| 197 | ············if (!Enum.TryParse<SQLiteDbType>(typeName, true, out result)) |
| 198 | ············throw new NDOException(27, "NDO.SQLiteProvider.Provider.GetDbType: Typname " + typeName + " cannot be converted into a DbType."); |
| 199 | ············return result; |
| 200 | ········} |
| 201 | |
| 202 | ········public override int GetDefaultLength(System.Type t) |
| 203 | ········{ |
| 204 | ············t = base.ConvertNullableType(t); |
| 205 | ············if (t == typeof(bool)) |
| 206 | ················return 2; |
| 207 | ············else if ( t == typeof(byte) ) |
| 208 | ················return 2; |
| 209 | ············else if ( t == typeof(sbyte) ) |
| 210 | ················return 2; |
| 211 | ············else if ( t == typeof(char) ) |
| 212 | ················return 2; |
| 213 | ············else if ( t == typeof(short)) |
| 214 | ················return 2; |
| 215 | ············else if ( t == typeof(ushort)) |
| 216 | ················return 2; |
| 217 | ············else if ( t == typeof(int)) |
| 218 | ················return 4; |
| 219 | ············else if ( t == typeof(uint)) |
| 220 | ················return 4; |
| 221 | ············else if ( t == typeof(long)) |
| 222 | ················return 8; |
| 223 | ············else if ( t == typeof(System.Guid)) |
| 224 | ················return 36; |
| 225 | ············else if ( t == typeof(ulong)) |
| 226 | ················return 8; |
| 227 | ············else if ( t == typeof(float)) |
| 228 | ················return 4; |
| 229 | ············else if ( t == typeof(double)) |
| 230 | ················return 8; |
| 231 | ············else if ( t == typeof(string)) |
| 232 | ················return 255; |
| 233 | ············else if ( t == typeof(byte[])) |
| 234 | ················return 255; |
| 235 | ············else if ( t == typeof(decimal)) |
| 236 | ················return 10; |
| 237 | ············else if ( t == typeof(System.DateTime)) |
| 238 | ················return 0; |
| 239 | ············else if ( t.IsSubclassOf(typeof(System.Enum))) |
| 240 | ················return 4; |
| 241 | ············else |
| 242 | ················return 0;········ |
| 243 | ········} |
| 244 | |
| 245 | |
| 246 | ········public override string Wildcard |
| 247 | ········{ |
| 248 | ············get { return "%"; } |
| 249 | ········} |
| 250 | |
| 251 | ········public override bool UseNamedParams |
| 252 | ········{ |
| 253 | ············get { return true; } |
| 254 | ········} |
| 255 | |
| 256 | /* |
| 257 | ········public override bool UseStoredProcedure |
| 258 | ········{ |
| 259 | ············get { return false; } |
| 260 | ········} |
| 261 | */ |
| 262 | ········#endregion |
| 263 | ········ |
| 264 | ········//private Hashtable namedParameters = new Hashtable(); |
| 265 | ········public override string GetNamedParameter(string plainParameterName) |
| 266 | ········{ |
| 267 | ············return ":" + plainParameterName; |
| 268 | ········} |
| 269 | ···· |
| 270 | ········public override string GetQuotedName(string plainName) |
| 271 | ········{ |
| 272 | ············return "\"" + plainName + '"'; |
| 273 | ········} |
| 274 | ···· |
| 275 | |
| 276 | ········/// <summary> |
| 277 | ········/// Indicates whether the last automatically generated ID can be retrieved. |
| 278 | ········/// Returns true if a database provides automatically incremented IDs and its syntax has an expression |
| 279 | ········/// which retrieves the last generated ID; otherwise false. |
| 280 | ········/// </summary> |
| 281 | ········public override bool SupportsLastInsertedId |
| 282 | ········{ |
| 283 | ············get { return true; } |
| 284 | ········} |
| 285 | |
| 286 | |
| 287 | ········/// <summary> |
| 288 | ········/// Gets an expression in the SQL dialect of the database, which retrieves the ID of the last |
| 289 | ········/// inserted row, if the ID is automatically generated by the database. |
| 290 | ········/// </summary> |
| 291 | ········public override string GetLastInsertedId(string tableName, string columnName) |
| 292 | ········{ |
| 293 | ············return "(SELECT last_insert_rowid())"; |
| 294 | ········} |
| 295 | |
| 296 | ········/// <summary> |
| 297 | ········/// Determines whether a database supports bulk command strings. |
| 298 | ········/// </summary> |
| 299 | ········public override bool SupportsBulkCommands |
| 300 | ········{ |
| 301 | ············get { return false; } |
| 302 | ········} |
| 303 | ········ |
| 304 | |
| 305 | ········/// <summary> |
| 306 | ········/// Generate one big command string out of several partial commands to save roundtrips |
| 307 | ········/// to the server. |
| 308 | ········/// </summary> |
| 309 | ········/// <param name="commands"></param> |
| 310 | ········/// <returns></returns> |
| 311 | ········public override string GenerateBulkCommand(string[] commands) |
| 312 | ········{ |
| 313 | ············StringBuilder sb = new StringBuilder(commands.Length * 100); |
| 314 | ············foreach (string s in commands) |
| 315 | ············{ |
| 316 | ················sb.Append(s); |
| 317 | ················sb.Append(';'); |
| 318 | ············} |
| 319 | ············return sb.ToString(); |
| 320 | ········} |
| 321 | |
| 322 | ············ |
| 323 | public override string[] GetTableNames( INdoDbConnection conn, string owner) |
| 324 | ········{ |
| 325 | SQLiteDataAdapter a = new SQLiteDataAdapter( "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;", ( SQLiteConnection) conn. InnerConnection) ; |
| 326 | ············DataSet ds = new DataSet(); |
| 327 | ············a.Fill(ds); |
| 328 | ············DataTable dt = ds.Tables[0]; |
| 329 | ············string[] strresult = new string[dt.Rows.Count]; |
| 330 | |
| 331 | ············int i = 0; |
| 332 | ············foreach (DataRow dr in dt.Rows) |
| 333 | ············{ |
| 334 | ················strresult[i++] = (string)dr["name"]; |
| 335 | ············} |
| 336 | |
| 337 | ············return strresult; |
| 338 | ········} |
| 339 | ···· |
| 340 | ········public override string[] TypeNames |
| 341 | ········{ |
| 342 | ············get |
| 343 | ············{················ |
| 344 | ················return Enum.GetNames(typeof(SQLiteDbType)); |
| 345 | ············} |
| 346 | ········} |
| 347 | |
| 348 | ········public override string Name { get { return "Sqlite"; }··} |
| 349 | |
| 350 | ········public override bool SupportsInsertBatch |
| 351 | ········{ |
| 352 | ············get |
| 353 | ············{ |
| 354 | ················return true; |
| 355 | ············} |
| 356 | ········} |
| 357 | |
| 358 | ········public override bool SupportsNativeGuidType |
| 359 | ········{ |
| 360 | ············get { return true; } |
| 361 | ········} |
| 362 | |
| 363 | ········public override string FetchLimit( int skip, int take ) |
| 364 | ········{ |
| 365 | ············return "LIMIT " + skip + "," + take; |
| 366 | ········} |
| 367 | |
| 368 | |
| 369 | ········public override string CreateDatabase(string databaseName, string connectionString, object additionalData) |
| 370 | ········{ |
| 371 | ············if (connectionString == null ) |
| 372 | ················throw new ArgumentNullException( nameof(connectionString) ); |
| 373 | |
| 374 | ············string path = connectionString.Substring( connectionString.IndexOf( '=' ) + 1 ); |
| 375 | ············path = path.Trim(); |
| 376 | |
| 377 | ············SQLiteConnection.CreateFile( path ); |
| 378 | |
| 379 | ············return connectionString; |
| 380 | ········} |
| 381 | ····} |
| 382 | } |
| 383 |
New Commit (f856804)
| 1 | // |
| 2 | // Copyright (c) 2002-2019 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 NDOInterfaces; |
| 24 | using System; |
| 25 | using System. Collections. ObjectModel; |
| 26 | using System.Data; |
| 27 | using System.Data.Common; |
| 28 | using System.Data.SQLite; |
| 29 | using System.Text; |
| 30 | |
| 31 | namespace System.Data.SQLite |
| 32 | { |
| 33 | ····public enum SQLiteDbType |
| 34 | ····{ |
| 35 | ········Text, |
| 36 | ········Numeric, |
| 37 | ········Integer, |
| 38 | ········Decimal, |
| 39 | ········Int2, |
| 40 | ········Real, |
| 41 | ········Datetime, |
| 42 | ········Blob, |
| 43 | ········Guid |
| 44 | ····} |
| 45 | } |
| 46 | |
| 47 | namespace NDO.SqliteProvider |
| 48 | { |
| 49 | ····/// <summary> |
| 50 | ····/// Sample adapter class to connect new ADO.NET Providers with NDO. |
| 51 | ····/// This Adapter is based on the SQLite SQLite Data Connector |
| 52 | ····/// </summary> |
| 53 | ····public class Provider : NDOAbstractProvider |
| 54 | ····{ |
| 55 | ········public Provider() |
| 56 | ········{ |
| 57 | ········} |
| 58 | |
| 59 | ········// The following methods provide objects of provider classes |
| 60 | ········// which implement common interfaces in .NET: |
| 61 | ········// IDbConnection, IDbCommand, DbDataAdapter and the Parameter objects |
| 62 | ········#region Provide specialized type objects |
| 63 | public override IDbConnection NewConnection( string connectionString) |
| 64 | ········{ |
| 65 | ············return new NdoDbConnection( new SQLiteConnection(connectionString) ); |
| 66 | ········} |
| 67 | |
| 68 | public override IDbCommand NewSqlCommand( IDbConnection connection) |
| 69 | ········{ |
| 70 | ············SQLiteCommand command = new SQLiteCommand(); |
| 71 | command. Connection = ( SQLiteConnection) ( ( INdoDbConnection) connection ) . InnerConnection; |
| 72 | ············return command; |
| 73 | ········} |
| 74 | |
| 75 | ········public override DbDataAdapter NewDataAdapter(IDbCommand select, IDbCommand update, IDbCommand insert, IDbCommand delete) |
| 76 | ········{ |
| 77 | ············SQLiteDataAdapter da = new SQLiteDataAdapter(); |
| 78 | ············da.SelectCommand = (SQLiteCommand)select; |
| 79 | ············da.UpdateCommand = (SQLiteCommand)update; |
| 80 | ············da.InsertCommand = (SQLiteCommand)insert; |
| 81 | ············da.DeleteCommand = (SQLiteCommand)delete; |
| 82 | ············return da; |
| 83 | ········} |
| 84 | |
| 85 | ········/// <summary> |
| 86 | ········/// See <see cref="IProvider"> IProvider interface </see> |
| 87 | ········/// </summary> |
| 88 | ········public override object NewCommandBuilder(DbDataAdapter dataAdapter) |
| 89 | ········{ |
| 90 | ············return new SQLiteCommandBuilder((SQLiteDataAdapter)dataAdapter); |
| 91 | ········} |
| 92 | |
| 93 | |
| 94 | ········public override IDataParameter AddParameter(IDbCommand command, string parameterName, object odbType, int size, string columnName) |
| 95 | ········{ |
| 96 | ············DbType dbType = GetDbTypeFromSqliteDbType( (SQLiteDbType) odbType ); |
| 97 | ············SQLiteParameter result = new SQLiteParameter( parameterName, dbType, size, columnName ); |
| 98 | ············((SQLiteCommand)command).Parameters.Add(result); |
| 99 | ············return result; |
| 100 | ········} |
| 101 | |
| 102 | ········public override IDataParameter AddParameter(IDbCommand command, string parameterName, object odbType, int size, ParameterDirection dir, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value) |
| 103 | ········{ |
| 104 | ············DbType dbType = GetDbTypeFromSqliteDbType( (SQLiteDbType) odbType ); |
| 105 | ············SQLiteParameter result = new SQLiteParameter( parameterName, dbType, size, dir, isNullable, precision, scale, srcColumn, srcVersion, value ); |
| 106 | ············((SQLiteCommand)command).Parameters.Add(result); |
| 107 | ············return result; |
| 108 | ········} |
| 109 | |
| 110 | ········private DbType GetDbTypeFromSqliteDbType( SQLiteDbType sqliteDbType ) |
| 111 | ········{ |
| 112 | ············switch ( sqliteDbType ) |
| 113 | ············{ |
| 114 | ················case SQLiteDbType.Text: |
| 115 | ····················return DbType.String; |
| 116 | ················case SQLiteDbType.Numeric: |
| 117 | ····················return DbType.VarNumeric; |
| 118 | ················case SQLiteDbType.Integer: |
| 119 | ····················return DbType.Int32; |
| 120 | ················case SQLiteDbType.Int2: |
| 121 | ····················return DbType.Int16; |
| 122 | ················case SQLiteDbType.Decimal: |
| 123 | ····················return DbType.Decimal; |
| 124 | ················case SQLiteDbType.Real: |
| 125 | ····················return DbType.Double; |
| 126 | ················case SQLiteDbType.Datetime: |
| 127 | ····················return DbType.DateTime; |
| 128 | ················case SQLiteDbType.Blob: |
| 129 | ····················return DbType.Binary; |
| 130 | ················case SQLiteDbType.Guid: |
| 131 | ····················return DbType.String; |
| 132 | ············} |
| 133 | ············return DbType.String; |
| 134 | ········} |
| 135 | |
| 136 | ········#endregion |
| 137 | |
| 138 | ········// The following method convert System.Type objects to SQLiteDbType-Members |
| 139 | ········// For your own adapter use members of the database type emumeration |
| 140 | ········// of your ADO.NET provider |
| 141 | ········#region Provide SQLiteDbType members |
| 142 | ········public override object GetDbType(Type t) |
| 143 | ········{ |
| 144 | ············t = base.ConvertNullableType(t); |
| 145 | ············return GetInternalDbType( t ); |
| 146 | ········} |
| 147 | |
| 148 | ········internal static SQLiteDbType GetInternalDbType( Type t ) |
| 149 | ········{ |
| 150 | ············if ( t == typeof( bool ) ) |
| 151 | ················return SQLiteDbType.Int2; |
| 152 | ············else if ( t == typeof( byte ) ) |
| 153 | ················return SQLiteDbType.Int2; |
| 154 | ············else if ( t == typeof( sbyte ) ) |
| 155 | ················return SQLiteDbType.Int2; |
| 156 | ············else if ( t == typeof( char ) ) |
| 157 | ················return SQLiteDbType.Int2; |
| 158 | ············else if ( t == typeof( short ) ) |
| 159 | ················return SQLiteDbType.Int2; |
| 160 | ············else if ( t == typeof( ushort ) ) |
| 161 | ················return SQLiteDbType.Int2; |
| 162 | ············else if ( t == typeof( int ) ) |
| 163 | ················return SQLiteDbType.Integer; |
| 164 | ············else if ( t == typeof( uint ) ) |
| 165 | ················return SQLiteDbType.Integer; |
| 166 | ············else if ( t == typeof( long ) ) |
| 167 | ················return SQLiteDbType.Numeric; |
| 168 | ············else if ( t == typeof( System.Guid ) ) |
| 169 | ················return SQLiteDbType.Guid; |
| 170 | ············else if ( t == typeof( ulong ) ) |
| 171 | ················return SQLiteDbType.Numeric; |
| 172 | ············else if ( t == typeof( float ) ) |
| 173 | ················return SQLiteDbType.Real; |
| 174 | ············else if ( t == typeof( double ) ) |
| 175 | ················return SQLiteDbType.Real; |
| 176 | ············else if ( t == typeof( string ) ) |
| 177 | ················return SQLiteDbType.Text; |
| 178 | ············else if ( t == typeof( byte[] ) ) |
| 179 | ················return SQLiteDbType.Blob; |
| 180 | ············else if ( t == typeof( decimal ) ) |
| 181 | ················return SQLiteDbType.Decimal; |
| 182 | ············else if ( t == typeof( System.DateTime ) ) |
| 183 | ················return SQLiteDbType.Datetime; |
| 184 | ············else if ( t.IsSubclassOf( typeof( System.Enum ) ) ) |
| 185 | ················return SQLiteDbType.Integer; |
| 186 | ············else |
| 187 | ················throw new NDOException( 27, "NDO.SQLiteProvider.GetDbType: Type " + t.Name + " can't be converted into SQLiteDbType." ); |
| 188 | ········} |
| 189 | |
| 190 | ········// The following method converts string representations of SQLiteDbType-Members |
| 191 | ········// into SQLiteDbType. |
| 192 | ········// For your own adapter use members of the database type emumeration of your |
| 193 | ········// ADO.NET provider and convert it to the respective enumeration type········ |
| 194 | ········public override object GetDbType(string typeName) |
| 195 | ········{ |
| 196 | ············SQLiteDbType result; |
| 197 | |
| 198 | ············if (!Enum.TryParse<SQLiteDbType>(typeName, true, out result)) |
| 199 | ············throw new NDOException(27, "NDO.SQLiteProvider.Provider.GetDbType: Typname " + typeName + " cannot be converted into a DbType."); |
| 200 | ············return result; |
| 201 | ········} |
| 202 | |
| 203 | ········public override int GetDefaultLength(System.Type t) |
| 204 | ········{ |
| 205 | ············t = base.ConvertNullableType(t); |
| 206 | ············if (t == typeof(bool)) |
| 207 | ················return 2; |
| 208 | ············else if ( t == typeof(byte) ) |
| 209 | ················return 2; |
| 210 | ············else if ( t == typeof(sbyte) ) |
| 211 | ················return 2; |
| 212 | ············else if ( t == typeof(char) ) |
| 213 | ················return 2; |
| 214 | ············else if ( t == typeof(short)) |
| 215 | ················return 2; |
| 216 | ············else if ( t == typeof(ushort)) |
| 217 | ················return 2; |
| 218 | ············else if ( t == typeof(int)) |
| 219 | ················return 4; |
| 220 | ············else if ( t == typeof(uint)) |
| 221 | ················return 4; |
| 222 | ············else if ( t == typeof(long)) |
| 223 | ················return 8; |
| 224 | ············else if ( t == typeof(System.Guid)) |
| 225 | ················return 36; |
| 226 | ············else if ( t == typeof(ulong)) |
| 227 | ················return 8; |
| 228 | ············else if ( t == typeof(float)) |
| 229 | ················return 4; |
| 230 | ············else if ( t == typeof(double)) |
| 231 | ················return 8; |
| 232 | ············else if ( t == typeof(string)) |
| 233 | ················return 255; |
| 234 | ············else if ( t == typeof(byte[])) |
| 235 | ················return 255; |
| 236 | ············else if ( t == typeof(decimal)) |
| 237 | ················return 10; |
| 238 | ············else if ( t == typeof(System.DateTime)) |
| 239 | ················return 0; |
| 240 | ············else if ( t.IsSubclassOf(typeof(System.Enum))) |
| 241 | ················return 4; |
| 242 | ············else |
| 243 | ················return 0;········ |
| 244 | ········} |
| 245 | |
| 246 | |
| 247 | ········public override string Wildcard |
| 248 | ········{ |
| 249 | ············get { return "%"; } |
| 250 | ········} |
| 251 | |
| 252 | ········public override bool UseNamedParams |
| 253 | ········{ |
| 254 | ············get { return true; } |
| 255 | ········} |
| 256 | |
| 257 | /* |
| 258 | ········public override bool UseStoredProcedure |
| 259 | ········{ |
| 260 | ············get { return false; } |
| 261 | ········} |
| 262 | */ |
| 263 | ········#endregion |
| 264 | ········ |
| 265 | ········//private Hashtable namedParameters = new Hashtable(); |
| 266 | ········public override string GetNamedParameter(string plainParameterName) |
| 267 | ········{ |
| 268 | ············return ":" + plainParameterName; |
| 269 | ········} |
| 270 | ···· |
| 271 | ········public override string GetQuotedName(string plainName) |
| 272 | ········{ |
| 273 | ············return "\"" + plainName + '"'; |
| 274 | ········} |
| 275 | ···· |
| 276 | |
| 277 | ········/// <summary> |
| 278 | ········/// Indicates whether the last automatically generated ID can be retrieved. |
| 279 | ········/// Returns true if a database provides automatically incremented IDs and its syntax has an expression |
| 280 | ········/// which retrieves the last generated ID; otherwise false. |
| 281 | ········/// </summary> |
| 282 | ········public override bool SupportsLastInsertedId |
| 283 | ········{ |
| 284 | ············get { return true; } |
| 285 | ········} |
| 286 | |
| 287 | |
| 288 | ········/// <summary> |
| 289 | ········/// Gets an expression in the SQL dialect of the database, which retrieves the ID of the last |
| 290 | ········/// inserted row, if the ID is automatically generated by the database. |
| 291 | ········/// </summary> |
| 292 | ········public override string GetLastInsertedId(string tableName, string columnName) |
| 293 | ········{ |
| 294 | ············return "(SELECT last_insert_rowid())"; |
| 295 | ········} |
| 296 | |
| 297 | ········/// <summary> |
| 298 | ········/// Determines whether a database supports bulk command strings. |
| 299 | ········/// </summary> |
| 300 | ········public override bool SupportsBulkCommands |
| 301 | ········{ |
| 302 | ············get { return false; } |
| 303 | ········} |
| 304 | ········ |
| 305 | |
| 306 | ········/// <summary> |
| 307 | ········/// Generate one big command string out of several partial commands to save roundtrips |
| 308 | ········/// to the server. |
| 309 | ········/// </summary> |
| 310 | ········/// <param name="commands"></param> |
| 311 | ········/// <returns></returns> |
| 312 | ········public override string GenerateBulkCommand(string[] commands) |
| 313 | ········{ |
| 314 | ············StringBuilder sb = new StringBuilder(commands.Length * 100); |
| 315 | ············foreach (string s in commands) |
| 316 | ············{ |
| 317 | ················sb.Append(s); |
| 318 | ················sb.Append(';'); |
| 319 | ············} |
| 320 | ············return sb.ToString(); |
| 321 | ········} |
| 322 | |
| 323 | ············ |
| 324 | public override string[] GetTableNames( IDbConnection conn, string owner) |
| 325 | ········{ |
| 326 | SQLiteDataAdapter a = new SQLiteDataAdapter( "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;", ( SQLiteConnection) ( ( INdoDbConnection) conn ) . InnerConnection) ; |
| 327 | ············DataSet ds = new DataSet(); |
| 328 | ············a.Fill(ds); |
| 329 | ············DataTable dt = ds.Tables[0]; |
| 330 | ············string[] strresult = new string[dt.Rows.Count]; |
| 331 | |
| 332 | ············int i = 0; |
| 333 | ············foreach (DataRow dr in dt.Rows) |
| 334 | ············{ |
| 335 | ················strresult[i++] = (string)dr["name"]; |
| 336 | ············} |
| 337 | |
| 338 | ············return strresult; |
| 339 | ········} |
| 340 | ···· |
| 341 | ········public override string[] TypeNames |
| 342 | ········{ |
| 343 | ············get |
| 344 | ············{················ |
| 345 | ················return Enum.GetNames(typeof(SQLiteDbType)); |
| 346 | ············} |
| 347 | ········} |
| 348 | |
| 349 | ········public override string Name { get { return "Sqlite"; }··} |
| 350 | |
| 351 | ········public override bool SupportsInsertBatch |
| 352 | ········{ |
| 353 | ············get |
| 354 | ············{ |
| 355 | ················return true; |
| 356 | ············} |
| 357 | ········} |
| 358 | |
| 359 | ········public override bool SupportsNativeGuidType |
| 360 | ········{ |
| 361 | ············get { return true; } |
| 362 | ········} |
| 363 | |
| 364 | ········public override string FetchLimit( int skip, int take ) |
| 365 | ········{ |
| 366 | ············return "LIMIT " + skip + "," + take; |
| 367 | ········} |
| 368 | |
| 369 | |
| 370 | ········public override string CreateDatabase(string databaseName, string connectionString, object additionalData) |
| 371 | ········{ |
| 372 | ············if (connectionString == null ) |
| 373 | ················throw new ArgumentNullException( nameof(connectionString) ); |
| 374 | |
| 375 | ············string path = connectionString.Substring( connectionString.IndexOf( '=' ) + 1 ); |
| 376 | ············path = path.Trim(); |
| 377 | |
| 378 | ············SQLiteConnection.CreateFile( path ); |
| 379 | |
| 380 | ············return connectionString; |
| 381 | ········} |
| 382 | ····} |
| 383 | } |
| 384 |