SqliteExtensions.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Globalization;
  7. using SQLitePCL.pretty;
  8. namespace Emby.Server.Implementations.Data
  9. {
  10. public static class SqliteExtensions
  11. {
  12. private const string DatetimeFormatUtc = "yyyy-MM-dd HH:mm:ss.FFFFFFFK";
  13. private const string DatetimeFormatLocal = "yyyy-MM-dd HH:mm:ss.FFFFFFF";
  14. /// <summary>
  15. /// An array of ISO-8601 DateTime formats that we support parsing.
  16. /// </summary>
  17. private static readonly string[] _datetimeFormats = new string[]
  18. {
  19. "THHmmssK",
  20. "THHmmK",
  21. "HH:mm:ss.FFFFFFFK",
  22. "HH:mm:ssK",
  23. "HH:mmK",
  24. DatetimeFormatUtc,
  25. "yyyy-MM-dd HH:mm:ssK",
  26. "yyyy-MM-dd HH:mmK",
  27. "yyyy-MM-ddTHH:mm:ss.FFFFFFFK",
  28. "yyyy-MM-ddTHH:mmK",
  29. "yyyy-MM-ddTHH:mm:ssK",
  30. "yyyyMMddHHmmssK",
  31. "yyyyMMddHHmmK",
  32. "yyyyMMddTHHmmssFFFFFFFK",
  33. "THHmmss",
  34. "THHmm",
  35. "HH:mm:ss.FFFFFFF",
  36. "HH:mm:ss",
  37. "HH:mm",
  38. DatetimeFormatLocal,
  39. "yyyy-MM-dd HH:mm:ss",
  40. "yyyy-MM-dd HH:mm",
  41. "yyyy-MM-ddTHH:mm:ss.FFFFFFF",
  42. "yyyy-MM-ddTHH:mm",
  43. "yyyy-MM-ddTHH:mm:ss",
  44. "yyyyMMddHHmmss",
  45. "yyyyMMddHHmm",
  46. "yyyyMMddTHHmmssFFFFFFF",
  47. "yyyy-MM-dd",
  48. "yyyyMMdd",
  49. "yy-MM-dd"
  50. };
  51. public static void RunQueries(this SQLiteDatabaseConnection connection, string[] queries)
  52. {
  53. if (queries == null)
  54. {
  55. throw new ArgumentNullException(nameof(queries));
  56. }
  57. connection.RunInTransaction(conn =>
  58. {
  59. conn.ExecuteAll(string.Join(';', queries));
  60. });
  61. }
  62. public static Guid ReadGuidFromBlob(this IResultSetValue result)
  63. {
  64. return new Guid(result.ToBlob());
  65. }
  66. public static string ToDateTimeParamValue(this DateTime dateValue)
  67. {
  68. var kind = DateTimeKind.Utc;
  69. return (dateValue.Kind == DateTimeKind.Unspecified)
  70. ? DateTime.SpecifyKind(dateValue, kind).ToString(
  71. GetDateTimeKindFormat(kind),
  72. CultureInfo.InvariantCulture)
  73. : dateValue.ToString(
  74. GetDateTimeKindFormat(dateValue.Kind),
  75. CultureInfo.InvariantCulture);
  76. }
  77. private static string GetDateTimeKindFormat(DateTimeKind kind)
  78. => (kind == DateTimeKind.Utc) ? DatetimeFormatUtc : DatetimeFormatLocal;
  79. public static DateTime ReadDateTime(this IResultSetValue result)
  80. {
  81. var dateText = result.ToString();
  82. return DateTime.ParseExact(
  83. dateText,
  84. _datetimeFormats,
  85. DateTimeFormatInfo.InvariantInfo,
  86. DateTimeStyles.None).ToUniversalTime();
  87. }
  88. public static bool TryReadDateTime(this IReadOnlyList<IResultSetValue> reader, int index, [NotNullWhen(true)] out DateTime? result)
  89. {
  90. result = null;
  91. var item = reader[index];
  92. if (item.IsDbNull())
  93. {
  94. return false;
  95. }
  96. var dateText = item.ToString();
  97. if (DateTime.TryParseExact(dateText, _datetimeFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out var dateTimeResult))
  98. {
  99. result = dateTimeResult.ToUniversalTime();
  100. return true;
  101. }
  102. return false;
  103. }
  104. public static bool TryGetGuid(this IReadOnlyList<IResultSetValue> reader, int index, [NotNullWhen(true)] out Guid? result)
  105. {
  106. result = null;
  107. var item = reader[index];
  108. if (item.IsDbNull())
  109. {
  110. return false;
  111. }
  112. result = item.ReadGuidFromBlob();
  113. return true;
  114. }
  115. private static bool IsDbNull(this IResultSetValue result)
  116. {
  117. return result.SQLiteType == SQLiteType.Null;
  118. }
  119. public static string GetString(this IReadOnlyList<IResultSetValue> result, int index)
  120. {
  121. return result[index].ToString();
  122. }
  123. public static bool TryGetString(this IReadOnlyList<IResultSetValue> reader, int index, out string result)
  124. {
  125. result = null;
  126. var item = reader[index];
  127. if (item.IsDbNull())
  128. {
  129. return false;
  130. }
  131. result = item.ToString();
  132. return true;
  133. }
  134. public static bool GetBoolean(this IReadOnlyList<IResultSetValue> result, int index)
  135. {
  136. return result[index].ToBool();
  137. }
  138. public static bool TryGetBoolean(this IReadOnlyList<IResultSetValue> reader, int index, [NotNullWhen(true)] out bool? result)
  139. {
  140. result = null;
  141. var item = reader[index];
  142. if (item.IsDbNull())
  143. {
  144. return false;
  145. }
  146. result = item.ToBool();
  147. return true;
  148. }
  149. public static bool TryGetInt(this IReadOnlyList<IResultSetValue> reader, int index, out int? result)
  150. {
  151. result = null;
  152. var item = reader[index];
  153. if (item.IsDbNull())
  154. {
  155. return false;
  156. }
  157. result = item.ToInt();
  158. return true;
  159. }
  160. public static long GetInt64(this IReadOnlyList<IResultSetValue> result, int index)
  161. {
  162. return result[index].ToInt64();
  163. }
  164. public static bool TryGetLong(this IReadOnlyList<IResultSetValue> reader, int index, out long? result)
  165. {
  166. result = null;
  167. var item = reader[index];
  168. if (item.IsDbNull())
  169. {
  170. return false;
  171. }
  172. result = item.ToInt64();
  173. return true;
  174. }
  175. public static bool TryGetFloat(this IReadOnlyList<IResultSetValue> reader, int index, out float? result)
  176. {
  177. result = null;
  178. var item = reader[index];
  179. if (item.IsDbNull())
  180. {
  181. return false;
  182. }
  183. result = item.ToFloat();
  184. return true;
  185. }
  186. public static bool TryGetDouble(this IReadOnlyList<IResultSetValue> reader, int index, out double? result)
  187. {
  188. result = null;
  189. var item = reader[index];
  190. if (item.IsDbNull())
  191. {
  192. return false;
  193. }
  194. result = item.ToDouble();
  195. return true;
  196. }
  197. public static Guid GetGuid(this IReadOnlyList<IResultSetValue> result, int index)
  198. {
  199. return result[index].ReadGuidFromBlob();
  200. }
  201. [Conditional("DEBUG")]
  202. private static void CheckName(string name)
  203. {
  204. throw new ArgumentException("Invalid param name: " + name, nameof(name));
  205. }
  206. public static void TryBind(this IStatement statement, string name, double value)
  207. {
  208. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  209. {
  210. bindParam.Bind(value);
  211. }
  212. else
  213. {
  214. CheckName(name);
  215. }
  216. }
  217. public static void TryBind(this IStatement statement, string name, string value)
  218. {
  219. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  220. {
  221. if (value == null)
  222. {
  223. bindParam.BindNull();
  224. }
  225. else
  226. {
  227. bindParam.Bind(value);
  228. }
  229. }
  230. else
  231. {
  232. CheckName(name);
  233. }
  234. }
  235. public static void TryBind(this IStatement statement, string name, bool value)
  236. {
  237. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  238. {
  239. bindParam.Bind(value);
  240. }
  241. else
  242. {
  243. CheckName(name);
  244. }
  245. }
  246. public static void TryBind(this IStatement statement, string name, float value)
  247. {
  248. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  249. {
  250. bindParam.Bind(value);
  251. }
  252. else
  253. {
  254. CheckName(name);
  255. }
  256. }
  257. public static void TryBind(this IStatement statement, string name, int value)
  258. {
  259. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  260. {
  261. bindParam.Bind(value);
  262. }
  263. else
  264. {
  265. CheckName(name);
  266. }
  267. }
  268. public static void TryBind(this IStatement statement, string name, Guid value)
  269. {
  270. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  271. {
  272. Span<byte> byteValue = stackalloc byte[16];
  273. value.TryWriteBytes(byteValue);
  274. bindParam.Bind(byteValue);
  275. }
  276. else
  277. {
  278. CheckName(name);
  279. }
  280. }
  281. public static void TryBind(this IStatement statement, string name, DateTime value)
  282. {
  283. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  284. {
  285. bindParam.Bind(value.ToDateTimeParamValue());
  286. }
  287. else
  288. {
  289. CheckName(name);
  290. }
  291. }
  292. public static void TryBind(this IStatement statement, string name, long value)
  293. {
  294. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  295. {
  296. bindParam.Bind(value);
  297. }
  298. else
  299. {
  300. CheckName(name);
  301. }
  302. }
  303. public static void TryBind(this IStatement statement, string name, ReadOnlySpan<byte> value)
  304. {
  305. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  306. {
  307. bindParam.Bind(value);
  308. }
  309. else
  310. {
  311. CheckName(name);
  312. }
  313. }
  314. public static void TryBindNull(this IStatement statement, string name)
  315. {
  316. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  317. {
  318. bindParam.BindNull();
  319. }
  320. else
  321. {
  322. CheckName(name);
  323. }
  324. }
  325. public static void TryBind(this IStatement statement, string name, DateTime? value)
  326. {
  327. if (value.HasValue)
  328. {
  329. TryBind(statement, name, value.Value);
  330. }
  331. else
  332. {
  333. TryBindNull(statement, name);
  334. }
  335. }
  336. public static void TryBind(this IStatement statement, string name, Guid? value)
  337. {
  338. if (value.HasValue)
  339. {
  340. TryBind(statement, name, value.Value);
  341. }
  342. else
  343. {
  344. TryBindNull(statement, name);
  345. }
  346. }
  347. public static void TryBind(this IStatement statement, string name, double? value)
  348. {
  349. if (value.HasValue)
  350. {
  351. TryBind(statement, name, value.Value);
  352. }
  353. else
  354. {
  355. TryBindNull(statement, name);
  356. }
  357. }
  358. public static void TryBind(this IStatement statement, string name, int? value)
  359. {
  360. if (value.HasValue)
  361. {
  362. TryBind(statement, name, value.Value);
  363. }
  364. else
  365. {
  366. TryBindNull(statement, name);
  367. }
  368. }
  369. public static void TryBind(this IStatement statement, string name, float? value)
  370. {
  371. if (value.HasValue)
  372. {
  373. TryBind(statement, name, value.Value);
  374. }
  375. else
  376. {
  377. TryBindNull(statement, name);
  378. }
  379. }
  380. public static void TryBind(this IStatement statement, string name, bool? value)
  381. {
  382. if (value.HasValue)
  383. {
  384. TryBind(statement, name, value.Value);
  385. }
  386. else
  387. {
  388. TryBindNull(statement, name);
  389. }
  390. }
  391. public static IEnumerable<IReadOnlyList<IResultSetValue>> ExecuteQuery(this IStatement statement)
  392. {
  393. while (statement.MoveNext())
  394. {
  395. yield return statement.Current;
  396. }
  397. }
  398. }
  399. }