SqliteExtensions.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. public static void RunQueries(this SQLiteDatabaseConnection connection, string[] queries)
  12. {
  13. if (queries == null)
  14. {
  15. throw new ArgumentNullException(nameof(queries));
  16. }
  17. connection.RunInTransaction(conn =>
  18. {
  19. conn.ExecuteAll(string.Join(";", queries));
  20. });
  21. }
  22. public static byte[] ToGuidBlob(this string str)
  23. {
  24. return ToGuidBlob(new Guid(str));
  25. }
  26. public static byte[] ToGuidBlob(this Guid guid)
  27. {
  28. return guid.ToByteArray();
  29. }
  30. public static Guid ReadGuidFromBlob(this IResultSetValue result)
  31. {
  32. // TODO: Remove ToArray when upgrading to netstandard2.1
  33. return new Guid(result.ToBlob().ToArray());
  34. }
  35. public static string ToDateTimeParamValue(this DateTime dateValue)
  36. {
  37. var kind = DateTimeKind.Utc;
  38. return (dateValue.Kind == DateTimeKind.Unspecified)
  39. ? DateTime.SpecifyKind(dateValue, kind).ToString(
  40. GetDateTimeKindFormat(kind),
  41. CultureInfo.InvariantCulture)
  42. : dateValue.ToString(
  43. GetDateTimeKindFormat(dateValue.Kind),
  44. CultureInfo.InvariantCulture);
  45. }
  46. private static string GetDateTimeKindFormat(
  47. DateTimeKind kind)
  48. {
  49. return (kind == DateTimeKind.Utc) ? _datetimeFormatUtc : _datetimeFormatLocal;
  50. }
  51. /// <summary>
  52. /// An array of ISO-8601 DateTime formats that we support parsing.
  53. /// </summary>
  54. private static string[] _datetimeFormats = new string[] {
  55. "THHmmssK",
  56. "THHmmK",
  57. "HH:mm:ss.FFFFFFFK",
  58. "HH:mm:ssK",
  59. "HH:mmK",
  60. "yyyy-MM-dd HH:mm:ss.FFFFFFFK", /* NOTE: UTC default (5). */
  61. "yyyy-MM-dd HH:mm:ssK",
  62. "yyyy-MM-dd HH:mmK",
  63. "yyyy-MM-ddTHH:mm:ss.FFFFFFFK",
  64. "yyyy-MM-ddTHH:mmK",
  65. "yyyy-MM-ddTHH:mm:ssK",
  66. "yyyyMMddHHmmssK",
  67. "yyyyMMddHHmmK",
  68. "yyyyMMddTHHmmssFFFFFFFK",
  69. "THHmmss",
  70. "THHmm",
  71. "HH:mm:ss.FFFFFFF",
  72. "HH:mm:ss",
  73. "HH:mm",
  74. "yyyy-MM-dd HH:mm:ss.FFFFFFF", /* NOTE: Non-UTC default (19). */
  75. "yyyy-MM-dd HH:mm:ss",
  76. "yyyy-MM-dd HH:mm",
  77. "yyyy-MM-ddTHH:mm:ss.FFFFFFF",
  78. "yyyy-MM-ddTHH:mm",
  79. "yyyy-MM-ddTHH:mm:ss",
  80. "yyyyMMddHHmmss",
  81. "yyyyMMddHHmm",
  82. "yyyyMMddTHHmmssFFFFFFF",
  83. "yyyy-MM-dd",
  84. "yyyyMMdd",
  85. "yy-MM-dd"
  86. };
  87. private static string _datetimeFormatUtc = _datetimeFormats[5];
  88. private static string _datetimeFormatLocal = _datetimeFormats[19];
  89. public static DateTime ReadDateTime(this IResultSetValue result)
  90. {
  91. var dateText = result.ToString();
  92. return DateTime.ParseExact(
  93. dateText, _datetimeFormats,
  94. DateTimeFormatInfo.InvariantInfo,
  95. DateTimeStyles.None).ToUniversalTime();
  96. }
  97. public static DateTime? TryReadDateTime(this IResultSetValue result)
  98. {
  99. var dateText = result.ToString();
  100. if (DateTime.TryParseExact(dateText, _datetimeFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out var dateTimeResult))
  101. {
  102. return dateTimeResult.ToUniversalTime();
  103. }
  104. return null;
  105. }
  106. /// <summary>
  107. /// Serializes to bytes.
  108. /// </summary>
  109. /// <returns>System.Byte[][].</returns>
  110. /// <exception cref="ArgumentNullException">obj</exception>
  111. public static byte[] SerializeToBytes(this IJsonSerializer json, object obj)
  112. {
  113. if (obj == null)
  114. {
  115. throw new ArgumentNullException(nameof(obj));
  116. }
  117. using (var stream = new MemoryStream())
  118. {
  119. json.SerializeToStream(obj, stream);
  120. return stream.ToArray();
  121. }
  122. }
  123. public static void Attach(SQLiteDatabaseConnection db, string path, string alias)
  124. {
  125. var commandText = string.Format("attach @path as {0};", alias);
  126. using (var statement = db.PrepareStatement(commandText))
  127. {
  128. statement.TryBind("@path", path);
  129. statement.MoveNext();
  130. }
  131. }
  132. public static bool IsDBNull(this IReadOnlyList<IResultSetValue> result, int index)
  133. {
  134. return result[index].SQLiteType == SQLiteType.Null;
  135. }
  136. public static string GetString(this IReadOnlyList<IResultSetValue> result, int index)
  137. {
  138. return result[index].ToString();
  139. }
  140. public static bool GetBoolean(this IReadOnlyList<IResultSetValue> result, int index)
  141. {
  142. return result[index].ToBool();
  143. }
  144. public static int GetInt32(this IReadOnlyList<IResultSetValue> result, int index)
  145. {
  146. return result[index].ToInt();
  147. }
  148. public static long GetInt64(this IReadOnlyList<IResultSetValue> result, int index)
  149. {
  150. return result[index].ToInt64();
  151. }
  152. public static float GetFloat(this IReadOnlyList<IResultSetValue> result, int index)
  153. {
  154. return result[index].ToFloat();
  155. }
  156. public static Guid GetGuid(this IReadOnlyList<IResultSetValue> result, int index)
  157. {
  158. return result[index].ReadGuidFromBlob();
  159. }
  160. private static void CheckName(string name)
  161. {
  162. #if DEBUG
  163. //if (!name.IndexOf("@", StringComparison.OrdinalIgnoreCase) != 0)
  164. {
  165. throw new Exception("Invalid param name: " + name);
  166. }
  167. #endif
  168. }
  169. public static void TryBind(this IStatement statement, string name, double 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, string value)
  181. {
  182. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  183. {
  184. if (value == null)
  185. {
  186. bindParam.BindNull();
  187. }
  188. else
  189. {
  190. bindParam.Bind(value);
  191. }
  192. }
  193. else
  194. {
  195. CheckName(name);
  196. }
  197. }
  198. public static void TryBind(this IStatement statement, string name, bool value)
  199. {
  200. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  201. {
  202. bindParam.Bind(value);
  203. }
  204. else
  205. {
  206. CheckName(name);
  207. }
  208. }
  209. public static void TryBind(this IStatement statement, string name, float value)
  210. {
  211. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  212. {
  213. bindParam.Bind(value);
  214. }
  215. else
  216. {
  217. CheckName(name);
  218. }
  219. }
  220. public static void TryBind(this IStatement statement, string name, int value)
  221. {
  222. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  223. {
  224. bindParam.Bind(value);
  225. }
  226. else
  227. {
  228. CheckName(name);
  229. }
  230. }
  231. public static void TryBind(this IStatement statement, string name, Guid value)
  232. {
  233. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  234. {
  235. bindParam.Bind(value.ToGuidBlob());
  236. }
  237. else
  238. {
  239. CheckName(name);
  240. }
  241. }
  242. public static void TryBind(this IStatement statement, string name, DateTime value)
  243. {
  244. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  245. {
  246. bindParam.Bind(value.ToDateTimeParamValue());
  247. }
  248. else
  249. {
  250. CheckName(name);
  251. }
  252. }
  253. public static void TryBind(this IStatement statement, string name, long value)
  254. {
  255. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  256. {
  257. bindParam.Bind(value);
  258. }
  259. else
  260. {
  261. CheckName(name);
  262. }
  263. }
  264. public static void TryBind(this IStatement statement, string name, byte[] value)
  265. {
  266. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  267. {
  268. bindParam.Bind(value);
  269. }
  270. else
  271. {
  272. CheckName(name);
  273. }
  274. }
  275. public static void TryBindNull(this IStatement statement, string name)
  276. {
  277. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  278. {
  279. bindParam.BindNull();
  280. }
  281. else
  282. {
  283. CheckName(name);
  284. }
  285. }
  286. public static void TryBind(this IStatement statement, string name, DateTime? value)
  287. {
  288. if (value.HasValue)
  289. {
  290. TryBind(statement, name, value.Value);
  291. }
  292. else
  293. {
  294. TryBindNull(statement, name);
  295. }
  296. }
  297. public static void TryBind(this IStatement statement, string name, Guid? value)
  298. {
  299. if (value.HasValue)
  300. {
  301. TryBind(statement, name, value.Value);
  302. }
  303. else
  304. {
  305. TryBindNull(statement, name);
  306. }
  307. }
  308. public static void TryBind(this IStatement statement, string name, double? value)
  309. {
  310. if (value.HasValue)
  311. {
  312. TryBind(statement, name, value.Value);
  313. }
  314. else
  315. {
  316. TryBindNull(statement, name);
  317. }
  318. }
  319. public static void TryBind(this IStatement statement, string name, int? value)
  320. {
  321. if (value.HasValue)
  322. {
  323. TryBind(statement, name, value.Value);
  324. }
  325. else
  326. {
  327. TryBindNull(statement, name);
  328. }
  329. }
  330. public static void TryBind(this IStatement statement, string name, float? value)
  331. {
  332. if (value.HasValue)
  333. {
  334. TryBind(statement, name, value.Value);
  335. }
  336. else
  337. {
  338. TryBindNull(statement, name);
  339. }
  340. }
  341. public static void TryBind(this IStatement statement, string name, bool? value)
  342. {
  343. if (value.HasValue)
  344. {
  345. TryBind(statement, name, value.Value);
  346. }
  347. else
  348. {
  349. TryBindNull(statement, name);
  350. }
  351. }
  352. public static IEnumerable<IReadOnlyList<IResultSetValue>> ExecuteQuery(
  353. this IStatement This)
  354. {
  355. while (This.MoveNext())
  356. {
  357. yield return This.Current;
  358. }
  359. }
  360. }
  361. }