2
0

SqliteExtensions.cs 11 KB

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