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