SqliteExtensions.cs 11 KB

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