SqliteExtensions.cs 10 KB

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