70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
/// Using ticks is more accurate but less compatible with other viewers and utilities that access your database.
/// </summary>
Ticks = 0,
/// <summary>
/// The default format for this provider.
/// </summary>
ISO8601 = 1,
}
/// <summary>
/// Struct used internally to determine the datatype of a column in a resultset
/// </summary>
internal class SQLiteType
{
|
>
>
>
>
|
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
/// Using ticks is more accurate but less compatible with other viewers and utilities that access your database.
/// </summary>
Ticks = 0,
/// <summary>
/// The default format for this provider.
/// </summary>
ISO8601 = 1,
/// <summary>
/// JulianDay format, which is what SQLite uses internally
/// </summary>
JulianDay = 2
}
/// <summary>
/// Struct used internally to determine the datatype of a column in a resultset
/// </summary>
internal class SQLiteType
{
|
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/// </summary>
public abstract class SQLiteConvert
{
/// <summary>
/// An array of ISO8601 datetime formats we support conversion from
/// </summary>
private static string[] _datetimeFormats = new string[] {
"yyyy-MM-dd HH:mm:ss.fffffff",
"yyyy-MM-dd HH:mm:ss",
"yyyy-MM-dd HH:mm",
"yyyyMMddHHmmss",
"yyyyMMddHHmm",
"yyyyMMddTHHmmssfffffff",
"yyyy-MM-dd",
"yy-MM-dd",
"yyyyMMdd",
"HH:mm:ss",
"HH:mm",
"THHmmss",
"THHmm",
"yyyy-MM-dd HH:mm:ss.fff",
"yyyy-MM-ddTHH:mm",
"yyyy-MM-ddTHH:mm:ss",
"yyyy-MM-ddTHH:mm:ss.fff",
"yyyy-MM-ddTHH:mm:ss.ffffff",
"HH:mm:ss.fff"
};
/// <summary>
/// An UTF-8 Encoding instance, so we can convert strings to and from UTF-8
/// </summary>
private static Encoding _utf8 = new UTF8Encoding();
/// <summary>
|
<
<
<
|
|
<
<
<
<
|
|
>
|
|
|
|
|
|
>
>
>
>
|
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
/// </summary>
public abstract class SQLiteConvert
{
/// <summary>
/// An array of ISO8601 datetime formats we support conversion from
/// </summary>
private static string[] _datetimeFormats = new string[] {
"THHmmss",
"THHmm",
"HH:mm:ss",
"HH:mm",
"HH:mm:ss.FFFFFFF",
"yy-MM-dd",
"yyyy-MM-dd",
"yyyy-MM-dd HH:mm:ss.FFFFFFF",
"yyyy-MM-dd HH:mm:ss",
"yyyy-MM-dd HH:mm",
"yyyy-MM-ddTHH:mm:ss.FFFFFFF",
"yyyy-MM-ddTHH:mm",
"yyyy-MM-ddTHH:mm:ss",
"yyyyMMddHHmmss",
"yyyyMMddHHmm",
"yyyyMMddTHHmmssFFFFFFF",
"yyyyMMdd"
};
/// <summary>
/// An UTF-8 Encoding instance, so we can convert strings to and from UTF-8
/// </summary>
private static Encoding _utf8 = new UTF8Encoding();
/// <summary>
|
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/// yyyyMMddTHHmmssfffffff
/// yyyy-MM-dd
/// yy-MM-dd
/// yyyyMMdd
/// HH:mm:ss
/// THHmmss
/// </remarks>
/// <param name="dateText">The string containing either a Tick value or an ISO8601-format string</param>
/// <returns>A DateTime value</returns>
public DateTime ToDateTime(string dateText)
{
switch (_datetimeFormat)
{
case SQLiteDateFormats.Ticks:
return new DateTime(Convert.ToInt64(dateText, CultureInfo.InvariantCulture));
default:
return DateTime.ParseExact(dateText, _datetimeFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
}
}
/// <summary>
/// Converts a DateTime to a string value, using the current DateTimeFormat specified for the connection when it was opened.
/// </summary>
/// <param name="dateValue">The DateTime value to convert</param>
/// <returns>Either a string consisting of the tick count for DateTimeFormat.Ticks, or a date/time in ISO8601 format.</returns>
public string ToString(DateTime dateValue)
{
switch (_datetimeFormat)
{
case SQLiteDateFormats.Ticks:
return dateValue.Ticks.ToString(CultureInfo.InvariantCulture);
default:
return dateValue.ToString(_datetimeFormats[0], CultureInfo.InvariantCulture);
}
}
/// <summary>
/// Internal function to convert a UTF-8 encoded IntPtr of the specified length to a DateTime.
/// </summary>
/// <remarks>
/// This is a convenience function, which first calls ToString() on the IntPtr to convert it to a string, then calls
/// ToDateTime() on the string to return a DateTime.
/// </remarks>
/// <param name="ptr">A pointer to the UTF-8 encoded string</param>
/// <param name="len">The length in bytes of the string</param>
/// <returns>The parsed DateTime value</returns>
internal DateTime ToDateTime(IntPtr ptr, int len)
{
return ToDateTime(ToString(ptr, len));
}
#endregion
/// <summary>
/// Smart method of splitting a string. Skips quoted elements, removes the quotes.
/// </summary>
/// <remarks>
/// This split function works somewhat like the String.Split() function in that it breaks apart a string into
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
|
>
|
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/// yyyyMMddTHHmmssfffffff
/// yyyy-MM-dd
/// yy-MM-dd
/// yyyyMMdd
/// HH:mm:ss
/// THHmmss
/// </remarks>
/// <param name="dateText">The string containing either a Tick value, a JulianDay double, or an ISO8601-format string</param>
/// <returns>A DateTime value</returns>
public DateTime ToDateTime(string dateText)
{
switch (_datetimeFormat)
{
case SQLiteDateFormats.Ticks:
return new DateTime(Convert.ToInt64(dateText, CultureInfo.InvariantCulture));
case SQLiteDateFormats.JulianDay:
return ToDateTime(Convert.ToDouble(dateText, CultureInfo.InvariantCulture));
default:
return DateTime.ParseExact(dateText, _datetimeFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
}
}
/// <summary>
/// Converts a julianday value into a DateTime
/// </summary>
/// <param name="julianDay">The value to convert</param>
/// <returns>A .NET DateTime</returns>
public DateTime ToDateTime(double julianDay)
{
return DateTime.FromOADate(julianDay - 2415018.5);
}
/// <summary>
/// Converts a DateTime struct to a JulianDay double
/// </summary>
/// <param name="value">The DateTime to convert</param>
/// <returns>The JulianDay value the Datetime represents</returns>
public double ToJulianDay(DateTime value)
{
return value.ToOADate() + 2415018.5;
}
/// <summary>
/// Converts a DateTime to a string value, using the current DateTimeFormat specified for the connection when it was opened.
/// </summary>
/// <param name="dateValue">The DateTime value to convert</param>
/// <returns>Either a string consisting of the tick count for DateTimeFormat.Ticks, a JulianDay double, or a date/time in ISO8601 format.</returns>
public string ToString(DateTime dateValue)
{
switch (_datetimeFormat)
{
case SQLiteDateFormats.Ticks:
return dateValue.Ticks.ToString(CultureInfo.InvariantCulture);
case SQLiteDateFormats.JulianDay:
return ToJulianDay(dateValue).ToString(CultureInfo.InvariantCulture);
default:
return dateValue.ToString(_datetimeFormats[7], CultureInfo.InvariantCulture);
}
}
/// <summary>
/// Internal function to convert a UTF-8 encoded IntPtr of the specified length to a DateTime.
/// </summary>
/// <remarks>
/// This is a convenience function, which first calls ToString() on the IntPtr to convert it to a string, then calls
/// ToDateTime() on the string to return a DateTime.
/// </remarks>
/// <param name="ptr">A pointer to the UTF-8 encoded string</param>
/// <param name="len">The length in bytes of the string</param>
/// <returns>The parsed DateTime value</returns>
internal DateTime ToDateTime(IntPtr ptr, int len)
{
return ToDateTime(ToString(ptr, len));
}
#endregion
/// <summary>
/// Smart method of splitting a string. Skips quoted elements, removes the quotes.
/// </summary>
/// <remarks>
/// This split function works somewhat like the String.Split() function in that it breaks apart a string into
|
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
/// <summary>
/// Converts a SQLiteType to a .NET Type object
/// </summary>
/// <param name="t">The SQLiteType to convert</param>
/// <returns>Returns a .NET Type object</returns>
internal static Type SQLiteTypeToType(SQLiteType t)
{
return SQLiteConvert.DbTypeToType(t.Type);
}
/// <summary>
/// For a given intrinsic type, return a DbType
/// </summary>
/// <param name="typ">The native type to convert</param>
/// <returns>The corresponding (closest match) DbType</returns>
internal static DbType TypeToDbType(Type typ)
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
|
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
/// <summary>
/// Converts a SQLiteType to a .NET Type object
/// </summary>
/// <param name="t">The SQLiteType to convert</param>
/// <returns>Returns a .NET Type object</returns>
internal static Type SQLiteTypeToType(SQLiteType t)
{
if (t.Type == DbType.Object)
return _affinitytotype[(int)t.Affinity];
else
return SQLiteConvert.DbTypeToType(t.Type);
}
private static Type[] _affinitytotype = {
typeof(object),
typeof(Int64),
typeof(Double),
typeof(string),
typeof(byte[]),
typeof(object),
typeof(DateTime),
typeof(object)
};
/// <summary>
/// For a given intrinsic type, return a DbType
/// </summary>
/// <param name="typ">The native type to convert</param>
/// <returns>The corresponding (closest match) DbType</returns>
internal static DbType TypeToDbType(Type typ)
|