SqliteExtensions.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  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 ResultSetValue 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 ResultSetValue 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<ResultSetValue> reader, int index, out DateTime result)
  89. {
  90. var item = reader[index];
  91. if (item.IsDbNull())
  92. {
  93. result = default;
  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. result = default;
  103. return false;
  104. }
  105. public static bool TryGetGuid(this IReadOnlyList<ResultSetValue> reader, int index, out Guid result)
  106. {
  107. var item = reader[index];
  108. if (item.IsDbNull())
  109. {
  110. result = default;
  111. return false;
  112. }
  113. result = item.ReadGuidFromBlob();
  114. return true;
  115. }
  116. public static bool IsDbNull(this ResultSetValue result)
  117. {
  118. return result.SQLiteType == SQLiteType.Null;
  119. }
  120. public static string GetString(this IReadOnlyList<ResultSetValue> result, int index)
  121. {
  122. return result[index].ToString();
  123. }
  124. public static bool TryGetString(this IReadOnlyList<ResultSetValue> reader, int index, out string result)
  125. {
  126. result = null;
  127. var item = reader[index];
  128. if (item.IsDbNull())
  129. {
  130. return false;
  131. }
  132. result = item.ToString();
  133. return true;
  134. }
  135. public static bool GetBoolean(this IReadOnlyList<ResultSetValue> result, int index)
  136. {
  137. return result[index].ToBool();
  138. }
  139. public static bool TryGetBoolean(this IReadOnlyList<ResultSetValue> reader, int index, out bool result)
  140. {
  141. var item = reader[index];
  142. if (item.IsDbNull())
  143. {
  144. result = default;
  145. return false;
  146. }
  147. result = item.ToBool();
  148. return true;
  149. }
  150. public static bool TryGetInt32(this IReadOnlyList<ResultSetValue> reader, int index, out int result)
  151. {
  152. var item = reader[index];
  153. if (item.IsDbNull())
  154. {
  155. result = default;
  156. return false;
  157. }
  158. result = item.ToInt();
  159. return true;
  160. }
  161. public static long GetInt64(this IReadOnlyList<ResultSetValue> result, int index)
  162. {
  163. return result[index].ToInt64();
  164. }
  165. public static bool TryGetInt64(this IReadOnlyList<ResultSetValue> reader, int index, out long result)
  166. {
  167. var item = reader[index];
  168. if (item.IsDbNull())
  169. {
  170. result = default;
  171. return false;
  172. }
  173. result = item.ToInt64();
  174. return true;
  175. }
  176. public static bool TryGetSingle(this IReadOnlyList<ResultSetValue> reader, int index, out float result)
  177. {
  178. var item = reader[index];
  179. if (item.IsDbNull())
  180. {
  181. result = default;
  182. return false;
  183. }
  184. result = item.ToFloat();
  185. return true;
  186. }
  187. public static bool TryGetDouble(this IReadOnlyList<ResultSetValue> reader, int index, out double result)
  188. {
  189. var item = reader[index];
  190. if (item.IsDbNull())
  191. {
  192. result = default;
  193. return false;
  194. }
  195. result = item.ToDouble();
  196. return true;
  197. }
  198. public static Guid GetGuid(this IReadOnlyList<ResultSetValue> result, int index)
  199. {
  200. return result[index].ReadGuidFromBlob();
  201. }
  202. [Conditional("DEBUG")]
  203. private static void CheckName(string name)
  204. {
  205. throw new ArgumentException("Invalid param name: " + name, nameof(name));
  206. }
  207. public static void TryBind(this IStatement statement, string name, double value)
  208. {
  209. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  210. {
  211. bindParam.Bind(value);
  212. }
  213. else
  214. {
  215. CheckName(name);
  216. }
  217. }
  218. public static void TryBind(this IStatement statement, string name, string value)
  219. {
  220. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  221. {
  222. if (value == null)
  223. {
  224. bindParam.BindNull();
  225. }
  226. else
  227. {
  228. bindParam.Bind(value);
  229. }
  230. }
  231. else
  232. {
  233. CheckName(name);
  234. }
  235. }
  236. public static void TryBind(this IStatement statement, string name, bool value)
  237. {
  238. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  239. {
  240. bindParam.Bind(value);
  241. }
  242. else
  243. {
  244. CheckName(name);
  245. }
  246. }
  247. public static void TryBind(this IStatement statement, string name, float value)
  248. {
  249. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  250. {
  251. bindParam.Bind(value);
  252. }
  253. else
  254. {
  255. CheckName(name);
  256. }
  257. }
  258. public static void TryBind(this IStatement statement, string name, int value)
  259. {
  260. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  261. {
  262. bindParam.Bind(value);
  263. }
  264. else
  265. {
  266. CheckName(name);
  267. }
  268. }
  269. public static void TryBind(this IStatement statement, string name, Guid value)
  270. {
  271. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  272. {
  273. Span<byte> byteValue = stackalloc byte[16];
  274. value.TryWriteBytes(byteValue);
  275. bindParam.Bind(byteValue);
  276. }
  277. else
  278. {
  279. CheckName(name);
  280. }
  281. }
  282. public static void TryBind(this IStatement statement, string name, DateTime value)
  283. {
  284. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  285. {
  286. bindParam.Bind(value.ToDateTimeParamValue());
  287. }
  288. else
  289. {
  290. CheckName(name);
  291. }
  292. }
  293. public static void TryBind(this IStatement statement, string name, long value)
  294. {
  295. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  296. {
  297. bindParam.Bind(value);
  298. }
  299. else
  300. {
  301. CheckName(name);
  302. }
  303. }
  304. public static void TryBind(this IStatement statement, string name, ReadOnlySpan<byte> value)
  305. {
  306. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  307. {
  308. bindParam.Bind(value);
  309. }
  310. else
  311. {
  312. CheckName(name);
  313. }
  314. }
  315. public static void TryBindNull(this IStatement statement, string name)
  316. {
  317. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  318. {
  319. bindParam.BindNull();
  320. }
  321. else
  322. {
  323. CheckName(name);
  324. }
  325. }
  326. public static void TryBind(this IStatement statement, string name, DateTime? value)
  327. {
  328. if (value.HasValue)
  329. {
  330. TryBind(statement, name, value.Value);
  331. }
  332. else
  333. {
  334. TryBindNull(statement, name);
  335. }
  336. }
  337. public static void TryBind(this IStatement statement, string name, Guid? value)
  338. {
  339. if (value.HasValue)
  340. {
  341. TryBind(statement, name, value.Value);
  342. }
  343. else
  344. {
  345. TryBindNull(statement, name);
  346. }
  347. }
  348. public static void TryBind(this IStatement statement, string name, double? value)
  349. {
  350. if (value.HasValue)
  351. {
  352. TryBind(statement, name, value.Value);
  353. }
  354. else
  355. {
  356. TryBindNull(statement, name);
  357. }
  358. }
  359. public static void TryBind(this IStatement statement, string name, int? value)
  360. {
  361. if (value.HasValue)
  362. {
  363. TryBind(statement, name, value.Value);
  364. }
  365. else
  366. {
  367. TryBindNull(statement, name);
  368. }
  369. }
  370. public static void TryBind(this IStatement statement, string name, float? value)
  371. {
  372. if (value.HasValue)
  373. {
  374. TryBind(statement, name, value.Value);
  375. }
  376. else
  377. {
  378. TryBindNull(statement, name);
  379. }
  380. }
  381. public static void TryBind(this IStatement statement, string name, bool? value)
  382. {
  383. if (value.HasValue)
  384. {
  385. TryBind(statement, name, value.Value);
  386. }
  387. else
  388. {
  389. TryBindNull(statement, name);
  390. }
  391. }
  392. public static IEnumerable<IReadOnlyList<ResultSetValue>> ExecuteQuery(this IStatement statement)
  393. {
  394. while (statement.MoveNext())
  395. {
  396. yield return statement.Current;
  397. }
  398. }
  399. }
  400. }