SqliteExtensions.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using MediaBrowser.Model.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("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. /// <summary>
  101. /// Serializes to bytes.
  102. /// </summary>
  103. /// <returns>System.Byte[][].</returns>
  104. /// <exception cref="System.ArgumentNullException">obj</exception>
  105. public static byte[] SerializeToBytes(this IJsonSerializer json, object obj, IMemoryStreamFactory streamProvider)
  106. {
  107. if (obj == null)
  108. {
  109. throw new ArgumentNullException("obj");
  110. }
  111. using (var stream = streamProvider.CreateNew())
  112. {
  113. json.SerializeToStream(obj, stream);
  114. return stream.ToArray();
  115. }
  116. }
  117. public static void Attach(ManagedConnection db, string path, string alias)
  118. {
  119. var commandText = string.Format("attach @path as {0};", 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. //if (!name.IndexOf("@", StringComparison.OrdinalIgnoreCase) != 0)
  158. {
  159. throw new Exception("Invalid param name: " + name);
  160. }
  161. #endif
  162. }
  163. public static void TryBind(this IStatement statement, string name, double value)
  164. {
  165. IBindParameter bindParam;
  166. if (statement.BindParameters.TryGetValue(name, out bindParam))
  167. {
  168. bindParam.Bind(value);
  169. }
  170. else
  171. {
  172. CheckName(name);
  173. }
  174. }
  175. public static void TryBind(this IStatement statement, string name, string value)
  176. {
  177. IBindParameter bindParam;
  178. if (statement.BindParameters.TryGetValue(name, out bindParam))
  179. {
  180. if (value == null)
  181. {
  182. bindParam.BindNull();
  183. }
  184. else
  185. {
  186. bindParam.Bind(value);
  187. }
  188. }
  189. else
  190. {
  191. CheckName(name);
  192. }
  193. }
  194. public static void TryBind(this IStatement statement, string name, bool value)
  195. {
  196. IBindParameter bindParam;
  197. if (statement.BindParameters.TryGetValue(name, out bindParam))
  198. {
  199. bindParam.Bind(value);
  200. }
  201. else
  202. {
  203. CheckName(name);
  204. }
  205. }
  206. public static void TryBind(this IStatement statement, string name, float value)
  207. {
  208. IBindParameter bindParam;
  209. if (statement.BindParameters.TryGetValue(name, out bindParam))
  210. {
  211. bindParam.Bind(value);
  212. }
  213. else
  214. {
  215. CheckName(name);
  216. }
  217. }
  218. public static void TryBind(this IStatement statement, string name, int value)
  219. {
  220. IBindParameter bindParam;
  221. if (statement.BindParameters.TryGetValue(name, out 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. IBindParameter bindParam;
  233. if (statement.BindParameters.TryGetValue(name, out bindParam))
  234. {
  235. bindParam.Bind(value.ToGuidBlob());
  236. }
  237. else
  238. {
  239. CheckName(name);
  240. }
  241. }
  242. public static void TryBind(this IStatement statement, string name, DateTime value)
  243. {
  244. IBindParameter bindParam;
  245. if (statement.BindParameters.TryGetValue(name, out bindParam))
  246. {
  247. bindParam.Bind(value.ToDateTimeParamValue());
  248. }
  249. else
  250. {
  251. CheckName(name);
  252. }
  253. }
  254. public static void TryBind(this IStatement statement, string name, long value)
  255. {
  256. IBindParameter bindParam;
  257. if (statement.BindParameters.TryGetValue(name, out bindParam))
  258. {
  259. bindParam.Bind(value);
  260. }
  261. else
  262. {
  263. CheckName(name);
  264. }
  265. }
  266. public static void TryBind(this IStatement statement, string name, byte[] value)
  267. {
  268. IBindParameter bindParam;
  269. if (statement.BindParameters.TryGetValue(name, out 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. IBindParameter bindParam;
  281. if (statement.BindParameters.TryGetValue(name, out bindParam))
  282. {
  283. bindParam.BindNull();
  284. }
  285. else
  286. {
  287. CheckName(name);
  288. }
  289. }
  290. public static void TryBind(this IStatement statement, string name, DateTime? value)
  291. {
  292. if (value.HasValue)
  293. {
  294. TryBind(statement, name, value.Value);
  295. }
  296. else
  297. {
  298. TryBindNull(statement, name);
  299. }
  300. }
  301. public static void TryBind(this IStatement statement, string name, Guid? value)
  302. {
  303. if (value.HasValue)
  304. {
  305. TryBind(statement, name, value.Value);
  306. }
  307. else
  308. {
  309. TryBindNull(statement, name);
  310. }
  311. }
  312. public static void TryBind(this IStatement statement, string name, double? value)
  313. {
  314. if (value.HasValue)
  315. {
  316. TryBind(statement, name, value.Value);
  317. }
  318. else
  319. {
  320. TryBindNull(statement, name);
  321. }
  322. }
  323. public static void TryBind(this IStatement statement, string name, int? value)
  324. {
  325. if (value.HasValue)
  326. {
  327. TryBind(statement, name, value.Value);
  328. }
  329. else
  330. {
  331. TryBindNull(statement, name);
  332. }
  333. }
  334. public static void TryBind(this IStatement statement, string name, float? value)
  335. {
  336. if (value.HasValue)
  337. {
  338. TryBind(statement, name, value.Value);
  339. }
  340. else
  341. {
  342. TryBindNull(statement, name);
  343. }
  344. }
  345. public static void TryBind(this IStatement statement, string name, bool? value)
  346. {
  347. if (value.HasValue)
  348. {
  349. TryBind(statement, name, value.Value);
  350. }
  351. else
  352. {
  353. TryBindNull(statement, name);
  354. }
  355. }
  356. public static IEnumerable<IReadOnlyList<IResultSetValue>> ExecuteQuery(
  357. this IStatement This)
  358. {
  359. while (This.MoveNext())
  360. {
  361. yield return This.Current;
  362. }
  363. }
  364. }
  365. }