SqliteExtensions.cs 11 KB

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