SqliteExtensions.cs 11 KB

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