SqliteExtensions.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. DateTime dateTimeResult;
  104. if (DateTime.TryParseExact(dateText, _datetimeFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dateTimeResult))
  105. {
  106. return dateTimeResult.ToUniversalTime();
  107. }
  108. return null;
  109. }
  110. /// <summary>
  111. /// Serializes to bytes.
  112. /// </summary>
  113. /// <returns>System.Byte[][].</returns>
  114. /// <exception cref="System.ArgumentNullException">obj</exception>
  115. public static byte[] SerializeToBytes(this IJsonSerializer json, object obj)
  116. {
  117. if (obj == null)
  118. {
  119. throw new ArgumentNullException(nameof(obj));
  120. }
  121. using (var stream = new MemoryStream())
  122. {
  123. json.SerializeToStream(obj, stream);
  124. return stream.ToArray();
  125. }
  126. }
  127. public static void Attach(ManagedConnection db, string path, string alias)
  128. {
  129. var commandText = string.Format("attach @path as {0};", alias);
  130. using (var statement = db.PrepareStatement(commandText))
  131. {
  132. statement.TryBind("@path", path);
  133. statement.MoveNext();
  134. }
  135. }
  136. public static bool IsDBNull(this IReadOnlyList<IResultSetValue> result, int index)
  137. {
  138. return result[index].SQLiteType == SQLiteType.Null;
  139. }
  140. public static string GetString(this IReadOnlyList<IResultSetValue> result, int index)
  141. {
  142. return result[index].ToString();
  143. }
  144. public static bool GetBoolean(this IReadOnlyList<IResultSetValue> result, int index)
  145. {
  146. return result[index].ToBool();
  147. }
  148. public static int GetInt32(this IReadOnlyList<IResultSetValue> result, int index)
  149. {
  150. return result[index].ToInt();
  151. }
  152. public static long GetInt64(this IReadOnlyList<IResultSetValue> result, int index)
  153. {
  154. return result[index].ToInt64();
  155. }
  156. public static float GetFloat(this IReadOnlyList<IResultSetValue> result, int index)
  157. {
  158. return result[index].ToFloat();
  159. }
  160. public static Guid GetGuid(this IReadOnlyList<IResultSetValue> result, int index)
  161. {
  162. return result[index].ReadGuidFromBlob();
  163. }
  164. private static void CheckName(string name)
  165. {
  166. #if DEBUG
  167. //if (!name.IndexOf("@", StringComparison.OrdinalIgnoreCase) != 0)
  168. {
  169. throw new Exception("Invalid param name: " + name);
  170. }
  171. #endif
  172. }
  173. public static void TryBind(this IStatement statement, string name, double value)
  174. {
  175. IBindParameter bindParam;
  176. if (statement.BindParameters.TryGetValue(name, out bindParam))
  177. {
  178. bindParam.Bind(value);
  179. }
  180. else
  181. {
  182. CheckName(name);
  183. }
  184. }
  185. public static void TryBind(this IStatement statement, string name, string value)
  186. {
  187. IBindParameter bindParam;
  188. if (statement.BindParameters.TryGetValue(name, out bindParam))
  189. {
  190. if (value == null)
  191. {
  192. bindParam.BindNull();
  193. }
  194. else
  195. {
  196. bindParam.Bind(value);
  197. }
  198. }
  199. else
  200. {
  201. CheckName(name);
  202. }
  203. }
  204. public static void TryBind(this IStatement statement, string name, bool value)
  205. {
  206. IBindParameter bindParam;
  207. if (statement.BindParameters.TryGetValue(name, out bindParam))
  208. {
  209. bindParam.Bind(value);
  210. }
  211. else
  212. {
  213. CheckName(name);
  214. }
  215. }
  216. public static void TryBind(this IStatement statement, string name, float value)
  217. {
  218. IBindParameter bindParam;
  219. if (statement.BindParameters.TryGetValue(name, out bindParam))
  220. {
  221. bindParam.Bind(value);
  222. }
  223. else
  224. {
  225. CheckName(name);
  226. }
  227. }
  228. public static void TryBind(this IStatement statement, string name, int value)
  229. {
  230. IBindParameter bindParam;
  231. if (statement.BindParameters.TryGetValue(name, out bindParam))
  232. {
  233. bindParam.Bind(value);
  234. }
  235. else
  236. {
  237. CheckName(name);
  238. }
  239. }
  240. public static void TryBind(this IStatement statement, string name, Guid value)
  241. {
  242. IBindParameter bindParam;
  243. if (statement.BindParameters.TryGetValue(name, out bindParam))
  244. {
  245. bindParam.Bind(value.ToGuidBlob());
  246. }
  247. else
  248. {
  249. CheckName(name);
  250. }
  251. }
  252. public static void TryBind(this IStatement statement, string name, DateTime value)
  253. {
  254. IBindParameter bindParam;
  255. if (statement.BindParameters.TryGetValue(name, out bindParam))
  256. {
  257. bindParam.Bind(value.ToDateTimeParamValue());
  258. }
  259. else
  260. {
  261. CheckName(name);
  262. }
  263. }
  264. public static void TryBind(this IStatement statement, string name, long value)
  265. {
  266. IBindParameter bindParam;
  267. if (statement.BindParameters.TryGetValue(name, out bindParam))
  268. {
  269. bindParam.Bind(value);
  270. }
  271. else
  272. {
  273. CheckName(name);
  274. }
  275. }
  276. public static void TryBind(this IStatement statement, string name, byte[] value)
  277. {
  278. IBindParameter bindParam;
  279. if (statement.BindParameters.TryGetValue(name, out bindParam))
  280. {
  281. bindParam.Bind(value);
  282. }
  283. else
  284. {
  285. CheckName(name);
  286. }
  287. }
  288. public static void TryBindNull(this IStatement statement, string name)
  289. {
  290. IBindParameter bindParam;
  291. if (statement.BindParameters.TryGetValue(name, out bindParam))
  292. {
  293. bindParam.BindNull();
  294. }
  295. else
  296. {
  297. CheckName(name);
  298. }
  299. }
  300. public static void TryBind(this IStatement statement, string name, DateTime? 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, Guid? 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, double? 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, int? 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, float? 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 void TryBind(this IStatement statement, string name, bool? value)
  356. {
  357. if (value.HasValue)
  358. {
  359. TryBind(statement, name, value.Value);
  360. }
  361. else
  362. {
  363. TryBindNull(statement, name);
  364. }
  365. }
  366. public static IEnumerable<IReadOnlyList<IResultSetValue>> ExecuteQuery(
  367. this IStatement This)
  368. {
  369. while (This.MoveNext())
  370. {
  371. yield return This.Current;
  372. }
  373. }
  374. }
  375. }