2
0

SqliteExtensions.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using SQLitePCL.pretty;
  6. namespace Emby.Server.Implementations.Data
  7. {
  8. public static class SqliteExtensions
  9. {
  10. private const string DatetimeFormatUtc = "yyyy-MM-dd HH:mm:ss.FFFFFFFK";
  11. private const string DatetimeFormatLocal = "yyyy-MM-dd HH:mm:ss.FFFFFFF";
  12. /// <summary>
  13. /// An array of ISO-8601 DateTime formats that we support parsing.
  14. /// </summary>
  15. private static readonly string[] _datetimeFormats = new string[]
  16. {
  17. "THHmmssK",
  18. "THHmmK",
  19. "HH:mm:ss.FFFFFFFK",
  20. "HH:mm:ssK",
  21. "HH:mmK",
  22. DatetimeFormatUtc,
  23. "yyyy-MM-dd HH:mm:ssK",
  24. "yyyy-MM-dd HH:mmK",
  25. "yyyy-MM-ddTHH:mm:ss.FFFFFFFK",
  26. "yyyy-MM-ddTHH:mmK",
  27. "yyyy-MM-ddTHH:mm:ssK",
  28. "yyyyMMddHHmmssK",
  29. "yyyyMMddHHmmK",
  30. "yyyyMMddTHHmmssFFFFFFFK",
  31. "THHmmss",
  32. "THHmm",
  33. "HH:mm:ss.FFFFFFF",
  34. "HH:mm:ss",
  35. "HH:mm",
  36. DatetimeFormatLocal,
  37. "yyyy-MM-dd HH:mm:ss",
  38. "yyyy-MM-dd HH:mm",
  39. "yyyy-MM-ddTHH:mm:ss.FFFFFFF",
  40. "yyyy-MM-ddTHH:mm",
  41. "yyyy-MM-ddTHH:mm:ss",
  42. "yyyyMMddHHmmss",
  43. "yyyyMMddHHmm",
  44. "yyyyMMddTHHmmssFFFFFFF",
  45. "yyyy-MM-dd",
  46. "yyyyMMdd",
  47. "yy-MM-dd"
  48. };
  49. public static void RunQueries(this SQLiteDatabaseConnection connection, string[] queries)
  50. {
  51. if (queries == null)
  52. {
  53. throw new ArgumentNullException(nameof(queries));
  54. }
  55. connection.RunInTransaction(conn =>
  56. {
  57. conn.ExecuteAll(string.Join(";", queries));
  58. });
  59. }
  60. public static Guid ReadGuidFromBlob(this IResultSetValue result)
  61. {
  62. return new Guid(result.ToBlob());
  63. }
  64. public static string ToDateTimeParamValue(this DateTime dateValue)
  65. {
  66. var kind = DateTimeKind.Utc;
  67. return (dateValue.Kind == DateTimeKind.Unspecified)
  68. ? DateTime.SpecifyKind(dateValue, kind).ToString(
  69. GetDateTimeKindFormat(kind),
  70. CultureInfo.InvariantCulture)
  71. : dateValue.ToString(
  72. GetDateTimeKindFormat(dateValue.Kind),
  73. CultureInfo.InvariantCulture);
  74. }
  75. private static string GetDateTimeKindFormat(DateTimeKind kind)
  76. => (kind == DateTimeKind.Utc) ? DatetimeFormatUtc : DatetimeFormatLocal;
  77. public static DateTime ReadDateTime(this IResultSetValue result)
  78. {
  79. var dateText = result.ToString();
  80. return DateTime.ParseExact(
  81. dateText,
  82. _datetimeFormats,
  83. DateTimeFormatInfo.InvariantInfo,
  84. DateTimeStyles.None).ToUniversalTime();
  85. }
  86. public static DateTime? TryReadDateTime(this IResultSetValue result)
  87. {
  88. var dateText = result.ToString();
  89. if (DateTime.TryParseExact(dateText, _datetimeFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out var dateTimeResult))
  90. {
  91. return dateTimeResult.ToUniversalTime();
  92. }
  93. return null;
  94. }
  95. public static bool IsDBNull(this IReadOnlyList<IResultSetValue> result, int index)
  96. {
  97. return result[index].SQLiteType == SQLiteType.Null;
  98. }
  99. public static string GetString(this IReadOnlyList<IResultSetValue> result, int index)
  100. {
  101. return result[index].ToString();
  102. }
  103. public static bool GetBoolean(this IReadOnlyList<IResultSetValue> result, int index)
  104. {
  105. return result[index].ToBool();
  106. }
  107. public static int GetInt32(this IReadOnlyList<IResultSetValue> result, int index)
  108. {
  109. return result[index].ToInt();
  110. }
  111. public static long GetInt64(this IReadOnlyList<IResultSetValue> result, int index)
  112. {
  113. return result[index].ToInt64();
  114. }
  115. public static float GetFloat(this IReadOnlyList<IResultSetValue> result, int index)
  116. {
  117. return result[index].ToFloat();
  118. }
  119. public static Guid GetGuid(this IReadOnlyList<IResultSetValue> result, int index)
  120. {
  121. return result[index].ReadGuidFromBlob();
  122. }
  123. private static void CheckName(string name)
  124. {
  125. #if DEBUG
  126. throw new ArgumentException("Invalid param name: " + name, nameof(name));
  127. #endif
  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. }