SqliteExtensions.cs 12 KB

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