SqliteExtensions.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. public static IEnumerable<SqliteDataReader> Query(this SqliteConnection sqliteConnection, string commandText)
  53. {
  54. if (sqliteConnection.State != ConnectionState.Open)
  55. {
  56. sqliteConnection.Open();
  57. }
  58. using var command = sqliteConnection.CreateCommand();
  59. command.CommandText = commandText;
  60. using (var reader = command.ExecuteReader())
  61. {
  62. while (reader.Read())
  63. {
  64. yield return reader;
  65. }
  66. }
  67. }
  68. public static void Execute(this SqliteConnection sqliteConnection, string commandText)
  69. {
  70. using var command = sqliteConnection.CreateCommand();
  71. command.CommandText = commandText;
  72. command.ExecuteNonQuery();
  73. }
  74. public static string ToDateTimeParamValue(this DateTime dateValue)
  75. {
  76. var kind = DateTimeKind.Utc;
  77. return (dateValue.Kind == DateTimeKind.Unspecified)
  78. ? DateTime.SpecifyKind(dateValue, kind).ToString(
  79. GetDateTimeKindFormat(kind),
  80. CultureInfo.InvariantCulture)
  81. : dateValue.ToString(
  82. GetDateTimeKindFormat(dateValue.Kind),
  83. CultureInfo.InvariantCulture);
  84. }
  85. private static string GetDateTimeKindFormat(DateTimeKind kind)
  86. => (kind == DateTimeKind.Utc) ? DatetimeFormatUtc : DatetimeFormatLocal;
  87. public static bool TryReadDateTime(this SqliteDataReader reader, int index, out DateTime result)
  88. {
  89. if (reader.IsDBNull(index))
  90. {
  91. result = default;
  92. return false;
  93. }
  94. var dateText = reader.GetString(index);
  95. if (DateTime.TryParseExact(dateText, _datetimeFormats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal, out var dateTimeResult))
  96. {
  97. result = dateTimeResult;
  98. return true;
  99. }
  100. result = default;
  101. return false;
  102. }
  103. public static bool TryGetGuid(this SqliteDataReader reader, int index, out Guid result)
  104. {
  105. if (reader.IsDBNull(index))
  106. {
  107. result = default;
  108. return false;
  109. }
  110. result = reader.GetGuid(index);
  111. return true;
  112. }
  113. public static bool TryGetString(this SqliteDataReader reader, int index, out string result)
  114. {
  115. result = string.Empty;
  116. if (reader.IsDBNull(index))
  117. {
  118. return false;
  119. }
  120. result = reader.GetString(index);
  121. return true;
  122. }
  123. public static bool TryGetBoolean(this SqliteDataReader reader, int index, out bool result)
  124. {
  125. if (reader.IsDBNull(index))
  126. {
  127. result = default;
  128. return false;
  129. }
  130. result = reader.GetBoolean(index);
  131. return true;
  132. }
  133. public static bool TryGetInt32(this SqliteDataReader reader, int index, out int result)
  134. {
  135. if (reader.IsDBNull(index))
  136. {
  137. result = default;
  138. return false;
  139. }
  140. result = reader.GetInt32(index);
  141. return true;
  142. }
  143. public static bool TryGetInt64(this SqliteDataReader reader, int index, out long result)
  144. {
  145. if (reader.IsDBNull(index))
  146. {
  147. result = default;
  148. return false;
  149. }
  150. result = reader.GetInt64(index);
  151. return true;
  152. }
  153. public static bool TryGetSingle(this SqliteDataReader reader, int index, out float result)
  154. {
  155. if (reader.IsDBNull(index))
  156. {
  157. result = default;
  158. return false;
  159. }
  160. result = reader.GetFloat(index);
  161. return true;
  162. }
  163. public static bool TryGetDouble(this SqliteDataReader reader, int index, out double result)
  164. {
  165. if (reader.IsDBNull(index))
  166. {
  167. result = default;
  168. return false;
  169. }
  170. result = reader.GetDouble(index);
  171. return true;
  172. }
  173. public static void TryBind(this SqliteCommand statement, string name, Guid value)
  174. {
  175. statement.TryBind(name, value, true);
  176. }
  177. public static void TryBind(this SqliteCommand statement, string name, object? value, bool isBlob = false)
  178. {
  179. var preparedValue = value ?? DBNull.Value;
  180. if (statement.Parameters.Contains(name))
  181. {
  182. statement.Parameters[name].Value = preparedValue;
  183. }
  184. else
  185. {
  186. // Blobs aren't always detected automatically
  187. if (isBlob)
  188. {
  189. statement.Parameters.Add(new SqliteParameter(name, SqliteType.Blob) { Value = value });
  190. }
  191. else
  192. {
  193. statement.Parameters.AddWithValue(name, preparedValue);
  194. }
  195. }
  196. }
  197. public static void TryBindNull(this SqliteCommand statement, string name)
  198. {
  199. statement.TryBind(name, DBNull.Value);
  200. }
  201. public static IEnumerable<SqliteDataReader> ExecuteQuery(this SqliteCommand command)
  202. {
  203. using (var reader = command.ExecuteReader())
  204. {
  205. while (reader.Read())
  206. {
  207. yield return reader;
  208. }
  209. }
  210. }
  211. public static int SelectScalarInt(this SqliteCommand command)
  212. {
  213. var result = command.ExecuteScalar();
  214. return Convert.ToInt32(result!, CultureInfo.InvariantCulture);
  215. }
  216. public static SqliteCommand PrepareStatement(this SqliteConnection sqliteConnection, string sql)
  217. {
  218. var command = sqliteConnection.CreateCommand();
  219. command.CommandText = sql;
  220. return command;
  221. }
  222. }
  223. }