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
|
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using Microsoft.Data.Sqlite;
namespace Emby.Server.Implementations.Data
{
public static class SqliteExtensions
{
private const string DatetimeFormatUtc = "yyyy-MM-dd HH:mm:ss.FFFFFFFK";
private const string DatetimeFormatLocal = "yyyy-MM-dd HH:mm:ss.FFFFFFF";
/// <summary>
/// An array of ISO-8601 DateTime formats that we support parsing.
/// </summary>
private static readonly string[] _datetimeFormats = new string[]
{
"THHmmssK",
"THHmmK",
"HH:mm:ss.FFFFFFFK",
"HH:mm:ssK",
"HH:mmK",
DatetimeFormatUtc,
"yyyy-MM-dd HH:mm:ssK",
"yyyy-MM-dd HH:mmK",
"yyyy-MM-ddTHH:mm:ss.FFFFFFFK",
"yyyy-MM-ddTHH:mmK",
"yyyy-MM-ddTHH:mm:ssK",
"yyyyMMddHHmmssK",
"yyyyMMddHHmmK",
"yyyyMMddTHHmmssFFFFFFFK",
"THHmmss",
"THHmm",
"HH:mm:ss.FFFFFFF",
"HH:mm:ss",
"HH:mm",
DatetimeFormatLocal,
"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",
"yyyy-MM-dd",
"yyyyMMdd",
"yy-MM-dd"
};
public static IEnumerable<SqliteDataReader> Query(this SqliteConnection sqliteConnection, string commandText)
{
if (sqliteConnection.State != ConnectionState.Open)
{
sqliteConnection.Open();
}
using var command = sqliteConnection.CreateCommand();
command.CommandText = commandText;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
yield return reader;
}
}
}
public static void Execute(this SqliteConnection sqliteConnection, string commandText)
{
using var command = sqliteConnection.CreateCommand();
command.CommandText = commandText;
command.ExecuteNonQuery();
}
public static string ToDateTimeParamValue(this DateTime dateValue)
{
var kind = DateTimeKind.Utc;
return (dateValue.Kind == DateTimeKind.Unspecified)
? DateTime.SpecifyKind(dateValue, kind).ToString(
GetDateTimeKindFormat(kind),
CultureInfo.InvariantCulture)
: dateValue.ToString(
GetDateTimeKindFormat(dateValue.Kind),
CultureInfo.InvariantCulture);
}
private static string GetDateTimeKindFormat(DateTimeKind kind)
=> (kind == DateTimeKind.Utc) ? DatetimeFormatUtc : DatetimeFormatLocal;
public static bool TryReadDateTime(this SqliteDataReader reader, int index, out DateTime result)
{
if (reader.IsDBNull(index))
{
result = default;
return false;
}
var dateText = reader.GetString(index);
if (DateTime.TryParseExact(dateText, _datetimeFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal, out var dateTimeResult))
{
// If the resulting DateTimeKind is Unspecified it is actually Utc.
// This is required downstream for the Json serializer.
if (dateTimeResult.Kind == DateTimeKind.Unspecified)
{
dateTimeResult = DateTime.SpecifyKind(dateTimeResult, DateTimeKind.Utc);
}
result = dateTimeResult;
return true;
}
result = default;
return false;
}
public static bool TryGetGuid(this SqliteDataReader reader, int index, out Guid result)
{
if (reader.IsDBNull(index))
{
result = default;
return false;
}
try
{
result = reader.GetGuid(index);
return true;
}
catch
{
result = Guid.Empty;
return false;
}
}
public static bool TryGetString(this SqliteDataReader reader, int index, out string result)
{
result = string.Empty;
if (reader.IsDBNull(index))
{
return false;
}
result = reader.GetString(index);
return true;
}
public static bool TryGetBoolean(this SqliteDataReader reader, int index, out bool result)
{
if (reader.IsDBNull(index))
{
result = default;
return false;
}
result = reader.GetBoolean(index);
return true;
}
public static bool TryGetInt32(this SqliteDataReader reader, int index, out int result)
{
if (reader.IsDBNull(index))
{
result = default;
return false;
}
result = reader.GetInt32(index);
return true;
}
public static bool TryGetInt64(this SqliteDataReader reader, int index, out long result)
{
if (reader.IsDBNull(index))
{
result = default;
return false;
}
result = reader.GetInt64(index);
return true;
}
public static bool TryGetSingle(this SqliteDataReader reader, int index, out float result)
{
if (reader.IsDBNull(index))
{
result = default;
return false;
}
result = reader.GetFloat(index);
return true;
}
public static bool TryGetDouble(this SqliteDataReader reader, int index, out double result)
{
if (reader.IsDBNull(index))
{
result = default;
return false;
}
result = reader.GetDouble(index);
return true;
}
public static void TryBind(this SqliteCommand statement, string name, Guid value)
{
statement.TryBind(name, value, true);
}
public static void TryBind(this SqliteCommand statement, string name, object? value, bool isBlob = false)
{
var preparedValue = value ?? DBNull.Value;
if (statement.Parameters.Contains(name))
{
statement.Parameters[name].Value = preparedValue;
}
else
{
// Blobs aren't always detected automatically
if (isBlob)
{
statement.Parameters.Add(new SqliteParameter(name, SqliteType.Blob) { Value = value });
}
else
{
statement.Parameters.AddWithValue(name, preparedValue);
}
}
}
public static void TryBindNull(this SqliteCommand statement, string name)
{
statement.TryBind(name, DBNull.Value);
}
public static IEnumerable<SqliteDataReader> ExecuteQuery(this SqliteCommand command)
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
yield return reader;
}
}
}
public static int SelectScalarInt(this SqliteCommand command)
{
var result = command.ExecuteScalar();
// Can't be null since the method is used to retrieve Count
return Convert.ToInt32(result!, CultureInfo.InvariantCulture);
}
public static SqliteCommand PrepareStatement(this SqliteConnection sqliteConnection, string sql)
{
var command = sqliteConnection.CreateCommand();
command.CommandText = sql;
return command;
}
}
}
|