-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
SqliteValueBinder.cs
316 lines (290 loc) · 10.3 KB
/
SqliteValueBinder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
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
308
309
310
311
312
313
314
315
316
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.Data.Sqlite.Properties;
namespace Microsoft.Data.Sqlite
{
// TODO: Make generic
internal abstract class SqliteValueBinder
{
private readonly object? _value;
private readonly SqliteType? _sqliteType;
protected SqliteValueBinder(object? value)
: this(value, null)
{
}
protected SqliteValueBinder(object? value, SqliteType? sqliteType)
{
_value = value;
_sqliteType = sqliteType;
}
protected abstract void BindInt64(long value);
protected virtual void BindDouble(double value)
{
if (double.IsNaN(value))
{
throw new InvalidOperationException(Resources.CannotStoreNaN);
}
BindDoubleCore(value);
}
protected abstract void BindDoubleCore(double value);
protected abstract void BindText(string value);
protected abstract void BindBlob(byte[] value);
protected abstract void BindNull();
public virtual void Bind()
{
if (_value == null)
{
BindNull();
return;
}
var type = _value.GetType().UnwrapNullableType().UnwrapEnumType();
if (type == typeof(bool))
{
var value = (bool)_value ? 1L : 0;
BindInt64(value);
}
else if (type == typeof(byte))
{
var value = (long)(byte)_value;
BindInt64(value);
}
else if (type == typeof(byte[]))
{
var value = (byte[])_value;
BindBlob(value);
}
else if (type == typeof(char))
{
var chr = (char)_value;
if (_sqliteType != SqliteType.Integer)
{
var value = new string(chr, 1);
BindText(value);
}
else
{
var value = (long)chr;
BindInt64(value);
}
}
else if (type == typeof(DateTime))
{
var dateTime = (DateTime)_value;
if (_sqliteType == SqliteType.Real)
{
var value = ToJulianDate(dateTime);
BindDouble(value);
}
else
{
var value = dateTime.ToString(@"yyyy\-MM\-dd HH\:mm\:ss.FFFFFFF", CultureInfo.InvariantCulture);
BindText(value);
}
}
else if (type == typeof(DateTimeOffset))
{
var dateTimeOffset = (DateTimeOffset)_value;
if (_sqliteType == SqliteType.Real)
{
var value = ToJulianDate(dateTimeOffset.DateTime);
BindDouble(value);
}
else
{
var value = dateTimeOffset.ToString(@"yyyy\-MM\-dd HH\:mm\:ss.FFFFFFFzzz", CultureInfo.InvariantCulture);
BindText(value);
}
}
#if NET6_0_OR_GREATER
else if (type == typeof(DateOnly))
{
var dateOnly = (DateOnly)_value;
if (_sqliteType == SqliteType.Real)
{
var value = ToJulianDate(dateOnly.Year, dateOnly.Month, dateOnly.Day, 0, 0, 0, 0);
BindDouble(value);
}
else
{
var value = dateOnly.ToString(@"yyyy\-MM\-dd", CultureInfo.InvariantCulture);
BindText(value);
}
}
else if (type == typeof(TimeOnly))
{
var timeOnly = (TimeOnly)_value;
if (_sqliteType == SqliteType.Real)
{
var value = GetTotalDays(timeOnly.Hour, timeOnly.Minute, timeOnly.Second, timeOnly.Millisecond);
BindDouble(value);
}
else
{
var value = timeOnly.Ticks % 10000000 == 0
? timeOnly.ToString(@"HH:mm:ss", CultureInfo.InvariantCulture)
: timeOnly.ToString(@"HH:mm:ss.fffffff", CultureInfo.InvariantCulture);
BindText(value);
}
}
#endif
else if (type == typeof(DBNull))
{
BindNull();
}
else if (type == typeof(decimal))
{
var value = ((decimal)_value).ToString("0.0###########################", CultureInfo.InvariantCulture);
BindText(value);
}
else if (type == typeof(double))
{
var value = (double)_value;
BindDouble(value);
}
else if (type == typeof(float))
{
var value = (double)(float)_value;
BindDouble(value);
}
else if (type == typeof(Guid))
{
var guid = (Guid)_value;
if (_sqliteType != SqliteType.Blob)
{
var value = guid.ToString().ToUpperInvariant();
BindText(value);
}
else
{
var value = guid.ToByteArray();
BindBlob(value);
}
}
else if (type == typeof(int))
{
var value = (long)(int)_value;
BindInt64(value);
}
else if (type == typeof(long))
{
var value = (long)_value;
BindInt64(value);
}
else if (type == typeof(sbyte))
{
var value = (long)(sbyte)_value;
BindInt64(value);
}
else if (type == typeof(short))
{
var value = (long)(short)_value;
BindInt64(value);
}
else if (type == typeof(string))
{
var value = (string)_value;
BindText(value);
}
else if (type == typeof(TimeSpan))
{
var timeSpan = (TimeSpan)_value;
if (_sqliteType == SqliteType.Real)
{
var value = timeSpan.TotalDays;
BindDouble(value);
}
else
{
var value = timeSpan.ToString("c");
BindText(value);
}
}
else if (type == typeof(uint))
{
var value = (long)(uint)_value;
BindInt64(value);
}
else if (type == typeof(ulong))
{
var value = (long)(ulong)_value;
BindInt64(value);
}
else if (type == typeof(ushort))
{
var value = (long)(ushort)_value;
BindInt64(value);
}
else
{
throw new InvalidOperationException(Resources.UnknownDataType(type));
}
}
private static readonly Dictionary<Type, SqliteType> _sqliteTypeMapping =
new()
{
{ typeof(bool), SqliteType.Integer },
{ typeof(byte), SqliteType.Integer },
{ typeof(byte[]), SqliteType.Blob },
{ typeof(char), SqliteType.Text },
{ typeof(DateTime), SqliteType.Text },
{ typeof(DateTimeOffset), SqliteType.Text },
{ typeof(DBNull), SqliteType.Text },
{ typeof(decimal), SqliteType.Text },
{ typeof(double), SqliteType.Real },
{ typeof(float), SqliteType.Real },
{ typeof(Guid), SqliteType.Text },
{ typeof(int), SqliteType.Integer },
{ typeof(long), SqliteType.Integer },
{ typeof(sbyte), SqliteType.Integer },
{ typeof(short), SqliteType.Integer },
{ typeof(string), SqliteType.Integer },
{ typeof(TimeSpan), SqliteType.Text },
{ typeof(uint), SqliteType.Integer },
{ typeof(ulong), SqliteType.Integer },
{ typeof(ushort), SqliteType.Integer }
};
internal static SqliteType GetSqliteType(object? value)
{
if (value == null)
{
return SqliteType.Text;
}
var type = value.GetType().UnwrapNullableType().UnwrapEnumType();
if (_sqliteTypeMapping.TryGetValue(type, out var sqliteType))
{
return sqliteType;
}
throw new InvalidOperationException(Resources.UnknownDataType(type));
}
private static double ToJulianDate(DateTime dateTime)
=> ToJulianDate(
dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond);
private static double ToJulianDate(int year, int month, int day, int hour, int minute, int second, int millisecond)
{
// computeJD
var Y = year;
var M = month;
var D = day;
if (M <= 2)
{
Y--;
M += 12;
}
var A = Y / 100;
var B = 2 - A + (A / 4);
var X1 = 36525 * (Y + 4716) / 100;
var X2 = 306001 * (M + 1) / 10000;
var iJD = (long)((X1 + X2 + D + B - 1524.5) * 86400000);
iJD += hour * 3600000 + minute * 60000 + (long)((second + millisecond / 1000.0) * 1000);
return iJD / 86400000.0;
}
private static double GetTotalDays(int hour, int minute, int second, int millisecond)
{
var iJD = hour * 3600000 + minute * 60000 + (long)((second + millisecond / 1000.0) * 1000);
return iJD / 86400000.0;
}
}
}