SqliteExtensions.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #nullable enable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Globalization;
  8. using Microsoft.Data.Sqlite;
  9. namespace Emby.Server.Implementations.Data
  10. {
  11. public static class SqliteExtensions
  12. {
  13. private const string DatetimeFormatUtc = "yyyy-MM-dd HH:mm:ss.FFFFFFFK";
  14. private const string DatetimeFormatLocal = "yyyy-MM-dd HH:mm:ss.FFFFFFF";
  15. /// <summary>
  16. /// An array of ISO-8601 DateTime formats that we support parsing.
  17. /// </summary>
  18. private static readonly string[] _datetimeFormats = new string[]
  19. {
  20. "THHmmssK",
  21. "THHmmK",
  22. "HH:mm:ss.FFFFFFFK",
  23. "HH:mm:ssK",
  24. "HH:mmK",
  25. DatetimeFormatUtc,
  26. "yyyy-MM-dd HH:mm:ssK",
  27. "yyyy-MM-dd HH:mmK",
  28. "yyyy-MM-ddTHH:mm:ss.FFFFFFFK",
  29. "yyyy-MM-ddTHH:mmK",
  30. "yyyy-MM-ddTHH:mm:ssK",
  31. "yyyyMMddHHmmssK",
  32. "yyyyMMddHHmmK",
  33. "yyyyMMddTHHmmssFFFFFFFK",
  34. "THHmmss",
  35. "THHmm",
  36. "HH:mm:ss.FFFFFFF",
  37. "HH:mm:ss",
  38. "HH:mm",
  39. DatetimeFormatLocal,
  40. "yyyy-MM-dd HH:mm:ss",
  41. "yyyy-MM-dd HH:mm",
  42. "yyyy-MM-ddTHH:mm:ss.FFFFFFF",
  43. "yyyy-MM-ddTHH:mm",
  44. "yyyy-MM-ddTHH:mm:ss",
  45. "yyyyMMddHHmmss",
  46. "yyyyMMddHHmm",
  47. "yyyyMMddTHHmmssFFFFFFF",
  48. "yyyy-MM-dd",
  49. "yyyyMMdd",
  50. "yy-MM-dd"
  51. };
  52. private static void EnsureOpen(this SqliteConnection sqliteConnection)
  53. {
  54. if (sqliteConnection.State == ConnectionState.Closed)
  55. {
  56. sqliteConnection.Open();
  57. }
  58. }
  59. public static IEnumerable<SqliteDataReader> Query(this SqliteConnection sqliteConnection, string commandText)
  60. {
  61. if (sqliteConnection.State != ConnectionState.Open)
  62. {
  63. sqliteConnection.Open();
  64. }
  65. using var command = sqliteConnection.CreateCommand();
  66. command.CommandText = commandText;
  67. using (var reader = command.ExecuteReader())
  68. {
  69. while (reader.Read())
  70. {
  71. yield return reader;
  72. }
  73. }
  74. }
  75. public static void Execute(this SqliteConnection sqliteConnection, string commandText)
  76. {
  77. sqliteConnection.EnsureOpen();
  78. using var command = sqliteConnection.CreateCommand();
  79. command.CommandText = commandText;
  80. command.ExecuteNonQuery();
  81. }
  82. public static void ExecuteAll(this SqliteConnection sqliteConnection, string commandText)
  83. {
  84. sqliteConnection.EnsureOpen();
  85. using var command = sqliteConnection.CreateCommand();
  86. command.CommandText = commandText;
  87. command.ExecuteNonQuery();
  88. }
  89. public static void RunQueries(this SqliteConnection connection, string[] queries)
  90. {
  91. ArgumentNullException.ThrowIfNull(queries);
  92. using var transaction = connection.BeginTransaction();
  93. connection.ExecuteAll(string.Join(';', queries));
  94. transaction.Commit();
  95. }
  96. public static string ToDateTimeParamValue(this DateTime dateValue)
  97. {
  98. var kind = DateTimeKind.Utc;
  99. return (dateValue.Kind == DateTimeKind.Unspecified)
  100. ? DateTime.SpecifyKind(dateValue, kind).ToString(
  101. GetDateTimeKindFormat(kind),
  102. CultureInfo.InvariantCulture)
  103. : dateValue.ToString(
  104. GetDateTimeKindFormat(dateValue.Kind),
  105. CultureInfo.InvariantCulture);
  106. }
  107. private static string GetDateTimeKindFormat(DateTimeKind kind)
  108. => (kind == DateTimeKind.Utc) ? DatetimeFormatUtc : DatetimeFormatLocal;
  109. public static bool TryReadDateTime(this SqliteDataReader reader, int index, out DateTime result)
  110. {
  111. if (reader.IsDBNull(index))
  112. {
  113. result = default;
  114. return false;
  115. }
  116. var dateText = reader.GetString(index);
  117. if (DateTime.TryParseExact(dateText, _datetimeFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal, out var dateTimeResult))
  118. {
  119. result = dateTimeResult;
  120. return true;
  121. }
  122. result = default;
  123. return false;
  124. }
  125. public static bool TryGetGuid(this SqliteDataReader reader, int index, out Guid result)
  126. {
  127. if (reader.IsDBNull(index))
  128. {
  129. result = default;
  130. return false;
  131. }
  132. result = reader.GetGuid(index);
  133. return true;
  134. }
  135. public static bool TryGetString(this SqliteDataReader reader, int index, out string result)
  136. {
  137. result = string.Empty;
  138. if (reader.IsDBNull(index))
  139. {
  140. return false;
  141. }
  142. result = reader.GetString(index);
  143. return true;
  144. }
  145. public static bool TryGetBoolean(this SqliteDataReader reader, int index, out bool result)
  146. {
  147. if (reader.IsDBNull(index))
  148. {
  149. result = default;
  150. return false;
  151. }
  152. result = reader.GetBoolean(index);
  153. return true;
  154. }
  155. public static bool TryGetInt32(this SqliteDataReader reader, int index, out int result)
  156. {
  157. if (reader.IsDBNull(index))
  158. {
  159. result = default;
  160. return false;
  161. }
  162. result = reader.GetInt32(index);
  163. return true;
  164. }
  165. public static bool TryGetInt64(this SqliteDataReader reader, int index, out long result)
  166. {
  167. if (reader.IsDBNull(index))
  168. {
  169. result = default;
  170. return false;
  171. }
  172. result = reader.GetInt64(index);
  173. return true;
  174. }
  175. public static bool TryGetSingle(this SqliteDataReader reader, int index, out float result)
  176. {
  177. if (reader.IsDBNull(index))
  178. {
  179. result = default;
  180. return false;
  181. }
  182. result = reader.GetFloat(index);
  183. return true;
  184. }
  185. public static bool TryGetDouble(this SqliteDataReader reader, int index, out double result)
  186. {
  187. if (reader.IsDBNull(index))
  188. {
  189. result = default;
  190. return false;
  191. }
  192. result = reader.GetDouble(index);
  193. return true;
  194. }
  195. public static void TryBind(this SqliteCommand statement, string name, Guid value)
  196. {
  197. statement.TryBind(name, value, true);
  198. }
  199. public static void TryBind(this SqliteCommand statement, string name, object? value, bool isBlob = false)
  200. {
  201. var preparedValue = value ?? DBNull.Value;
  202. if (statement.Parameters.Contains(name))
  203. {
  204. statement.Parameters[name].Value = preparedValue;
  205. }
  206. else
  207. {
  208. if (isBlob)
  209. {
  210. statement.Parameters.Add(new SqliteParameter(name, SqliteType.Blob) { Value = value });
  211. }
  212. else
  213. {
  214. statement.Parameters.AddWithValue(name, preparedValue);
  215. }
  216. }
  217. }
  218. public static void TryBind(this SqliteCommand statement, string name, byte[] value)
  219. {
  220. if (statement.Parameters.Contains(name))
  221. {
  222. statement.Parameters[name].Value = value;
  223. }
  224. else
  225. {
  226. statement.Parameters.Add(new SqliteParameter(name, SqliteType.Blob, value.Length) { Value = value });
  227. }
  228. }
  229. public static void TryBindNull(this SqliteCommand statement, string name)
  230. {
  231. statement.TryBind(name, DBNull.Value);
  232. }
  233. public static IEnumerable<SqliteDataReader> ExecuteQuery(this SqliteCommand command)
  234. {
  235. using (var reader = command.ExecuteReader())
  236. {
  237. while (reader.Read())
  238. {
  239. yield return reader;
  240. }
  241. }
  242. }
  243. public static int SelectScalarInt(this SqliteCommand command)
  244. {
  245. var result = command.ExecuteScalar();
  246. return Convert.ToInt32(result!, CultureInfo.InvariantCulture);
  247. }
  248. public static SqliteCommand PrepareStatement(this SqliteConnection sqliteConnection, string sql)
  249. {
  250. sqliteConnection.EnsureOpen();
  251. var command = sqliteConnection.CreateCommand();
  252. command.CommandText = sql;
  253. return command;
  254. }
  255. }
  256. }