SqliteExtensions.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 void Attach(SQLiteDatabaseConnection db, string path, string alias)
  96. {
  97. var commandText = string.Format(
  98. CultureInfo.InvariantCulture,
  99. "attach @path as {0};",
  100. alias);
  101. using (var statement = db.PrepareStatement(commandText))
  102. {
  103. statement.TryBind("@path", path);
  104. statement.MoveNext();
  105. }
  106. }
  107. public static bool IsDBNull(this IReadOnlyList<IResultSetValue> result, int index)
  108. {
  109. return result[index].SQLiteType == SQLiteType.Null;
  110. }
  111. public static string GetString(this IReadOnlyList<IResultSetValue> result, int index)
  112. {
  113. return result[index].ToString();
  114. }
  115. public static bool GetBoolean(this IReadOnlyList<IResultSetValue> result, int index)
  116. {
  117. return result[index].ToBool();
  118. }
  119. public static int GetInt32(this IReadOnlyList<IResultSetValue> result, int index)
  120. {
  121. return result[index].ToInt();
  122. }
  123. public static long GetInt64(this IReadOnlyList<IResultSetValue> result, int index)
  124. {
  125. return result[index].ToInt64();
  126. }
  127. public static float GetFloat(this IReadOnlyList<IResultSetValue> result, int index)
  128. {
  129. return result[index].ToFloat();
  130. }
  131. public static Guid GetGuid(this IReadOnlyList<IResultSetValue> result, int index)
  132. {
  133. return result[index].ReadGuidFromBlob();
  134. }
  135. private static void CheckName(string name)
  136. {
  137. #if DEBUG
  138. throw new ArgumentException("Invalid param name: " + name, nameof(name));
  139. #endif
  140. }
  141. public static void TryBind(this IStatement statement, string name, double value)
  142. {
  143. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  144. {
  145. bindParam.Bind(value);
  146. }
  147. else
  148. {
  149. CheckName(name);
  150. }
  151. }
  152. public static void TryBind(this IStatement statement, string name, string value)
  153. {
  154. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  155. {
  156. if (value == null)
  157. {
  158. bindParam.BindNull();
  159. }
  160. else
  161. {
  162. bindParam.Bind(value);
  163. }
  164. }
  165. else
  166. {
  167. CheckName(name);
  168. }
  169. }
  170. public static void TryBind(this IStatement statement, string name, bool value)
  171. {
  172. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  173. {
  174. bindParam.Bind(value);
  175. }
  176. else
  177. {
  178. CheckName(name);
  179. }
  180. }
  181. public static void TryBind(this IStatement statement, string name, float value)
  182. {
  183. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  184. {
  185. bindParam.Bind(value);
  186. }
  187. else
  188. {
  189. CheckName(name);
  190. }
  191. }
  192. public static void TryBind(this IStatement statement, string name, int value)
  193. {
  194. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  195. {
  196. bindParam.Bind(value);
  197. }
  198. else
  199. {
  200. CheckName(name);
  201. }
  202. }
  203. public static void TryBind(this IStatement statement, string name, Guid value)
  204. {
  205. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  206. {
  207. Span<byte> byteValue = stackalloc byte[16];
  208. value.TryWriteBytes(byteValue);
  209. bindParam.Bind(byteValue);
  210. }
  211. else
  212. {
  213. CheckName(name);
  214. }
  215. }
  216. public static void TryBind(this IStatement statement, string name, DateTime value)
  217. {
  218. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  219. {
  220. bindParam.Bind(value.ToDateTimeParamValue());
  221. }
  222. else
  223. {
  224. CheckName(name);
  225. }
  226. }
  227. public static void TryBind(this IStatement statement, string name, long value)
  228. {
  229. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  230. {
  231. bindParam.Bind(value);
  232. }
  233. else
  234. {
  235. CheckName(name);
  236. }
  237. }
  238. public static void TryBind(this IStatement statement, string name, ReadOnlySpan<byte> value)
  239. {
  240. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  241. {
  242. bindParam.Bind(value);
  243. }
  244. else
  245. {
  246. CheckName(name);
  247. }
  248. }
  249. public static void TryBindNull(this IStatement statement, string name)
  250. {
  251. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  252. {
  253. bindParam.BindNull();
  254. }
  255. else
  256. {
  257. CheckName(name);
  258. }
  259. }
  260. public static void TryBind(this IStatement statement, string name, DateTime? value)
  261. {
  262. if (value.HasValue)
  263. {
  264. TryBind(statement, name, value.Value);
  265. }
  266. else
  267. {
  268. TryBindNull(statement, name);
  269. }
  270. }
  271. public static void TryBind(this IStatement statement, string name, Guid? value)
  272. {
  273. if (value.HasValue)
  274. {
  275. TryBind(statement, name, value.Value);
  276. }
  277. else
  278. {
  279. TryBindNull(statement, name);
  280. }
  281. }
  282. public static void TryBind(this IStatement statement, string name, double? value)
  283. {
  284. if (value.HasValue)
  285. {
  286. TryBind(statement, name, value.Value);
  287. }
  288. else
  289. {
  290. TryBindNull(statement, name);
  291. }
  292. }
  293. public static void TryBind(this IStatement statement, string name, int? value)
  294. {
  295. if (value.HasValue)
  296. {
  297. TryBind(statement, name, value.Value);
  298. }
  299. else
  300. {
  301. TryBindNull(statement, name);
  302. }
  303. }
  304. public static void TryBind(this IStatement statement, string name, float? value)
  305. {
  306. if (value.HasValue)
  307. {
  308. TryBind(statement, name, value.Value);
  309. }
  310. else
  311. {
  312. TryBindNull(statement, name);
  313. }
  314. }
  315. public static void TryBind(this IStatement statement, string name, bool? value)
  316. {
  317. if (value.HasValue)
  318. {
  319. TryBind(statement, name, value.Value);
  320. }
  321. else
  322. {
  323. TryBindNull(statement, name);
  324. }
  325. }
  326. public static IEnumerable<IReadOnlyList<IResultSetValue>> ExecuteQuery(this IStatement statement)
  327. {
  328. while (statement.MoveNext())
  329. {
  330. yield return statement.Current;
  331. }
  332. }
  333. }
  334. }