SqliteExtensions.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. //foreach (var query in queries)
  20. //{
  21. // conn.Execute(query);
  22. //}
  23. conn.ExecuteAll(string.Join(";", queries));
  24. });
  25. }
  26. public static byte[] ToGuidBlob(this string str)
  27. {
  28. return ToGuidBlob(new Guid(str));
  29. }
  30. public static byte[] ToGuidBlob(this Guid guid)
  31. {
  32. return guid.ToByteArray();
  33. }
  34. public static Guid ReadGuidFromBlob(this IResultSetValue result)
  35. {
  36. return new Guid(result.ToBlob());
  37. }
  38. public static string ToDateTimeParamValue(this DateTime dateValue)
  39. {
  40. var kind = DateTimeKind.Utc;
  41. return (dateValue.Kind == DateTimeKind.Unspecified)
  42. ? DateTime.SpecifyKind(dateValue, kind).ToString(
  43. GetDateTimeKindFormat(kind),
  44. CultureInfo.InvariantCulture)
  45. : dateValue.ToString(
  46. GetDateTimeKindFormat(dateValue.Kind),
  47. CultureInfo.InvariantCulture);
  48. }
  49. private static string GetDateTimeKindFormat(
  50. DateTimeKind kind)
  51. {
  52. return (kind == DateTimeKind.Utc) ? _datetimeFormatUtc : _datetimeFormatLocal;
  53. }
  54. /// <summary>
  55. /// An array of ISO-8601 DateTime formats that we support parsing.
  56. /// </summary>
  57. private static string[] _datetimeFormats = new string[] {
  58. "THHmmssK",
  59. "THHmmK",
  60. "HH:mm:ss.FFFFFFFK",
  61. "HH:mm:ssK",
  62. "HH:mmK",
  63. "yyyy-MM-dd HH:mm:ss.FFFFFFFK", /* NOTE: UTC default (5). */
  64. "yyyy-MM-dd HH:mm:ssK",
  65. "yyyy-MM-dd HH:mmK",
  66. "yyyy-MM-ddTHH:mm:ss.FFFFFFFK",
  67. "yyyy-MM-ddTHH:mmK",
  68. "yyyy-MM-ddTHH:mm:ssK",
  69. "yyyyMMddHHmmssK",
  70. "yyyyMMddHHmmK",
  71. "yyyyMMddTHHmmssFFFFFFFK",
  72. "THHmmss",
  73. "THHmm",
  74. "HH:mm:ss.FFFFFFF",
  75. "HH:mm:ss",
  76. "HH:mm",
  77. "yyyy-MM-dd HH:mm:ss.FFFFFFF", /* NOTE: Non-UTC default (19). */
  78. "yyyy-MM-dd HH:mm:ss",
  79. "yyyy-MM-dd HH:mm",
  80. "yyyy-MM-ddTHH:mm:ss.FFFFFFF",
  81. "yyyy-MM-ddTHH:mm",
  82. "yyyy-MM-ddTHH:mm:ss",
  83. "yyyyMMddHHmmss",
  84. "yyyyMMddHHmm",
  85. "yyyyMMddTHHmmssFFFFFFF",
  86. "yyyy-MM-dd",
  87. "yyyyMMdd",
  88. "yy-MM-dd"
  89. };
  90. private static string _datetimeFormatUtc = _datetimeFormats[5];
  91. private static string _datetimeFormatLocal = _datetimeFormats[19];
  92. public static DateTime ReadDateTime(this IResultSetValue result)
  93. {
  94. var dateText = result.ToString();
  95. return DateTime.ParseExact(
  96. dateText, _datetimeFormats,
  97. DateTimeFormatInfo.InvariantInfo,
  98. DateTimeStyles.None).ToUniversalTime();
  99. }
  100. public static DateTime? TryReadDateTime(this IResultSetValue result)
  101. {
  102. var dateText = result.ToString();
  103. if (DateTime.TryParseExact(dateText, _datetimeFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out var dateTimeResult))
  104. {
  105. return dateTimeResult.ToUniversalTime();
  106. }
  107. return null;
  108. }
  109. /// <summary>
  110. /// Serializes to bytes.
  111. /// </summary>
  112. /// <returns>System.Byte[][].</returns>
  113. /// <exception cref="ArgumentNullException">obj</exception>
  114. public static byte[] SerializeToBytes(this IJsonSerializer json, object obj)
  115. {
  116. if (obj == null)
  117. {
  118. throw new ArgumentNullException(nameof(obj));
  119. }
  120. using (var stream = new MemoryStream())
  121. {
  122. json.SerializeToStream(obj, stream);
  123. return stream.ToArray();
  124. }
  125. }
  126. public static void Attach(ManagedConnection db, string path, string alias)
  127. {
  128. var commandText = string.Format("attach @path as {0};", alias);
  129. using (var statement = db.PrepareStatement(commandText))
  130. {
  131. statement.TryBind("@path", path);
  132. statement.MoveNext();
  133. }
  134. }
  135. public static bool IsDBNull(this IReadOnlyList<IResultSetValue> result, int index)
  136. {
  137. return result[index].SQLiteType == SQLiteType.Null;
  138. }
  139. public static string GetString(this IReadOnlyList<IResultSetValue> result, int index)
  140. {
  141. return result[index].ToString();
  142. }
  143. public static bool GetBoolean(this IReadOnlyList<IResultSetValue> result, int index)
  144. {
  145. return result[index].ToBool();
  146. }
  147. public static int GetInt32(this IReadOnlyList<IResultSetValue> result, int index)
  148. {
  149. return result[index].ToInt();
  150. }
  151. public static long GetInt64(this IReadOnlyList<IResultSetValue> result, int index)
  152. {
  153. return result[index].ToInt64();
  154. }
  155. public static float GetFloat(this IReadOnlyList<IResultSetValue> result, int index)
  156. {
  157. return result[index].ToFloat();
  158. }
  159. public static Guid GetGuid(this IReadOnlyList<IResultSetValue> result, int index)
  160. {
  161. return result[index].ReadGuidFromBlob();
  162. }
  163. private static void CheckName(string name)
  164. {
  165. #if DEBUG
  166. //if (!name.IndexOf("@", StringComparison.OrdinalIgnoreCase) != 0)
  167. {
  168. throw new Exception("Invalid param name: " + name);
  169. }
  170. #endif
  171. }
  172. public static void TryBind(this IStatement statement, string name, double value)
  173. {
  174. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  175. {
  176. bindParam.Bind(value);
  177. }
  178. else
  179. {
  180. CheckName(name);
  181. }
  182. }
  183. public static void TryBind(this IStatement statement, string name, string value)
  184. {
  185. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  186. {
  187. if (value == null)
  188. {
  189. bindParam.BindNull();
  190. }
  191. else
  192. {
  193. bindParam.Bind(value);
  194. }
  195. }
  196. else
  197. {
  198. CheckName(name);
  199. }
  200. }
  201. public static void TryBind(this IStatement statement, string name, bool 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, float 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, int value)
  224. {
  225. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  226. {
  227. bindParam.Bind(value);
  228. }
  229. else
  230. {
  231. CheckName(name);
  232. }
  233. }
  234. public static void TryBind(this IStatement statement, string name, Guid value)
  235. {
  236. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  237. {
  238. bindParam.Bind(value.ToGuidBlob());
  239. }
  240. else
  241. {
  242. CheckName(name);
  243. }
  244. }
  245. public static void TryBind(this IStatement statement, string name, DateTime value)
  246. {
  247. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  248. {
  249. bindParam.Bind(value.ToDateTimeParamValue());
  250. }
  251. else
  252. {
  253. CheckName(name);
  254. }
  255. }
  256. public static void TryBind(this IStatement statement, string name, long 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 TryBind(this IStatement statement, string name, byte[] value)
  268. {
  269. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  270. {
  271. bindParam.Bind(value);
  272. }
  273. else
  274. {
  275. CheckName(name);
  276. }
  277. }
  278. public static void TryBindNull(this IStatement statement, string name)
  279. {
  280. if (statement.BindParameters.TryGetValue(name, out IBindParameter bindParam))
  281. {
  282. bindParam.BindNull();
  283. }
  284. else
  285. {
  286. CheckName(name);
  287. }
  288. }
  289. public static void TryBind(this IStatement statement, string name, DateTime? 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, Guid? 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, double? 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, int? 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, float? 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 void TryBind(this IStatement statement, string name, bool? value)
  345. {
  346. if (value.HasValue)
  347. {
  348. TryBind(statement, name, value.Value);
  349. }
  350. else
  351. {
  352. TryBindNull(statement, name);
  353. }
  354. }
  355. public static IEnumerable<IReadOnlyList<IResultSetValue>> ExecuteQuery(
  356. this IStatement This)
  357. {
  358. while (This.MoveNext())
  359. {
  360. yield return This.Current;
  361. }
  362. }
  363. }
  364. }