SqliteExtensions.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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[] ToGuidParamValue(this string str)
  27. {
  28. return ToGuidParamValue(new Guid(str));
  29. }
  30. public static byte[] ToGuidParamValue(this Guid guid)
  31. {
  32. return guid.ToByteArray();
  33. }
  34. public static Guid ReadGuid(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(IDatabaseConnection db, string path, string alias)
  118. {
  119. var commandText = string.Format("attach ? as {0};", alias);
  120. var paramList = new List<object>();
  121. paramList.Add(path);
  122. db.Execute(commandText, paramList.ToArray());
  123. }
  124. public static bool IsDBNull(this IReadOnlyList<IResultSetValue> result, int index)
  125. {
  126. return result[index].SQLiteType == SQLiteType.Null;
  127. }
  128. public static string GetString(this IReadOnlyList<IResultSetValue> result, int index)
  129. {
  130. return result[index].ToString();
  131. }
  132. public static bool GetBoolean(this IReadOnlyList<IResultSetValue> result, int index)
  133. {
  134. return result[index].ToBool();
  135. }
  136. public static int GetInt32(this IReadOnlyList<IResultSetValue> result, int index)
  137. {
  138. return result[index].ToInt();
  139. }
  140. public static long GetInt64(this IReadOnlyList<IResultSetValue> result, int index)
  141. {
  142. return result[index].ToInt64();
  143. }
  144. public static float GetFloat(this IReadOnlyList<IResultSetValue> result, int index)
  145. {
  146. return result[index].ToFloat();
  147. }
  148. public static Guid GetGuid(this IReadOnlyList<IResultSetValue> result, int index)
  149. {
  150. return result[index].ReadGuid();
  151. }
  152. private static void CheckName(string name)
  153. {
  154. #if DEBUG
  155. //if (!name.IndexOf("@", StringComparison.OrdinalIgnoreCase) != 0)
  156. {
  157. throw new Exception("Invalid param name: " + name);
  158. }
  159. #endif
  160. }
  161. public static void TryBind(this IStatement statement, string name, double value)
  162. {
  163. IBindParameter bindParam;
  164. if (statement.BindParameters.TryGetValue(name, out bindParam))
  165. {
  166. bindParam.Bind(value);
  167. }
  168. else
  169. {
  170. CheckName(name);
  171. }
  172. }
  173. public static void TryBind(this IStatement statement, string name, string value)
  174. {
  175. IBindParameter bindParam;
  176. if (statement.BindParameters.TryGetValue(name, out bindParam))
  177. {
  178. if (value == null)
  179. {
  180. bindParam.BindNull();
  181. }
  182. else
  183. {
  184. bindParam.Bind(value);
  185. }
  186. }
  187. else
  188. {
  189. CheckName(name);
  190. }
  191. }
  192. public static void TryBind(this IStatement statement, string name, bool value)
  193. {
  194. IBindParameter bindParam;
  195. if (statement.BindParameters.TryGetValue(name, out bindParam))
  196. {
  197. bindParam.Bind(value);
  198. }
  199. else
  200. {
  201. CheckName(name);
  202. }
  203. }
  204. public static void TryBind(this IStatement statement, string name, float 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, int 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, Guid value)
  229. {
  230. IBindParameter bindParam;
  231. if (statement.BindParameters.TryGetValue(name, out bindParam))
  232. {
  233. bindParam.Bind(value.ToGuidParamValue());
  234. }
  235. else
  236. {
  237. CheckName(name);
  238. }
  239. }
  240. public static void TryBind(this IStatement statement, string name, DateTime value)
  241. {
  242. IBindParameter bindParam;
  243. if (statement.BindParameters.TryGetValue(name, out bindParam))
  244. {
  245. bindParam.Bind(value.ToDateTimeParamValue());
  246. }
  247. else
  248. {
  249. CheckName(name);
  250. }
  251. }
  252. public static void TryBind(this IStatement statement, string name, long value)
  253. {
  254. IBindParameter bindParam;
  255. if (statement.BindParameters.TryGetValue(name, out bindParam))
  256. {
  257. bindParam.Bind(value);
  258. }
  259. else
  260. {
  261. CheckName(name);
  262. }
  263. }
  264. public static void TryBind(this IStatement statement, string name, byte[] 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 TryBindNull(this IStatement statement, string name)
  277. {
  278. IBindParameter bindParam;
  279. if (statement.BindParameters.TryGetValue(name, out bindParam))
  280. {
  281. bindParam.BindNull();
  282. }
  283. else
  284. {
  285. CheckName(name);
  286. }
  287. }
  288. public static void TryBind(this IStatement statement, string name, DateTime? 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, Guid? 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(
  344. this IStatement This)
  345. {
  346. while (This.MoveNext())
  347. {
  348. yield return This.Current;
  349. }
  350. }
  351. }
  352. }