SqliteRepository.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using MediaBrowser.Model.Logging;
  2. using System;
  3. using System.Data;
  4. using System.Data.SQLite;
  5. using System.IO;
  6. using System.Threading.Tasks;
  7. namespace MediaBrowser.Server.Implementations.Persistence
  8. {
  9. /// <summary>
  10. /// Class SqliteRepository
  11. /// </summary>
  12. public abstract class SqliteRepository : IDisposable
  13. {
  14. /// <summary>
  15. /// The db file name
  16. /// </summary>
  17. protected string DbFileName;
  18. /// <summary>
  19. /// The connection
  20. /// </summary>
  21. protected SQLiteConnection Connection;
  22. /// <summary>
  23. /// Gets the logger.
  24. /// </summary>
  25. /// <value>The logger.</value>
  26. protected ILogger Logger { get; private set; }
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="SqliteRepository" /> class.
  29. /// </summary>
  30. /// <param name="logManager">The log manager.</param>
  31. /// <exception cref="System.ArgumentNullException">logger</exception>
  32. protected SqliteRepository(ILogManager logManager)
  33. {
  34. if (logManager == null)
  35. {
  36. throw new ArgumentNullException("logManager");
  37. }
  38. Logger = logManager.GetLogger(GetType().Name);
  39. }
  40. /// <summary>
  41. /// Connects to DB.
  42. /// </summary>
  43. /// <param name="dbPath">The db path.</param>
  44. /// <returns>Task{System.Boolean}.</returns>
  45. /// <exception cref="System.ArgumentNullException">dbPath</exception>
  46. protected Task ConnectToDb(string dbPath)
  47. {
  48. if (string.IsNullOrEmpty(dbPath))
  49. {
  50. throw new ArgumentNullException("dbPath");
  51. }
  52. DbFileName = dbPath;
  53. var connectionstr = new SQLiteConnectionStringBuilder
  54. {
  55. PageSize = 4096,
  56. CacheSize = 40960,
  57. SyncMode = SynchronizationModes.Off,
  58. DataSource = dbPath,
  59. JournalMode = SQLiteJournalModeEnum.Wal
  60. };
  61. Connection = new SQLiteConnection(connectionstr.ConnectionString);
  62. return Connection.OpenAsync();
  63. }
  64. /// <summary>
  65. /// Runs the queries.
  66. /// </summary>
  67. /// <param name="queries">The queries.</param>
  68. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  69. /// <exception cref="System.ArgumentNullException">queries</exception>
  70. protected void RunQueries(string[] queries)
  71. {
  72. if (queries == null)
  73. {
  74. throw new ArgumentNullException("queries");
  75. }
  76. using (var tran = Connection.BeginTransaction())
  77. {
  78. try
  79. {
  80. using (var cmd = Connection.CreateCommand())
  81. {
  82. foreach (var query in queries)
  83. {
  84. cmd.Transaction = tran;
  85. cmd.CommandText = query;
  86. cmd.ExecuteNonQuery();
  87. }
  88. }
  89. tran.Commit();
  90. }
  91. catch (Exception e)
  92. {
  93. Logger.ErrorException("Error running queries", e);
  94. tran.Rollback();
  95. throw;
  96. }
  97. }
  98. }
  99. /// <summary>
  100. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  101. /// </summary>
  102. public void Dispose()
  103. {
  104. Dispose(true);
  105. GC.SuppressFinalize(this);
  106. }
  107. private readonly object _disposeLock = new object();
  108. /// <summary>
  109. /// Releases unmanaged and - optionally - managed resources.
  110. /// </summary>
  111. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  112. protected virtual void Dispose(bool dispose)
  113. {
  114. if (dispose)
  115. {
  116. try
  117. {
  118. lock (_disposeLock)
  119. {
  120. if (Connection != null)
  121. {
  122. if (Connection.IsOpen())
  123. {
  124. Connection.Close();
  125. }
  126. Connection.Dispose();
  127. Connection = null;
  128. }
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. Logger.ErrorException("Error disposing database", ex);
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// Gets a stream from a DataReader at a given ordinal
  139. /// </summary>
  140. /// <param name="reader">The reader.</param>
  141. /// <param name="ordinal">The ordinal.</param>
  142. /// <returns>Stream.</returns>
  143. /// <exception cref="System.ArgumentNullException">reader</exception>
  144. protected static Stream GetStream(IDataReader reader, int ordinal)
  145. {
  146. if (reader == null)
  147. {
  148. throw new ArgumentNullException("reader");
  149. }
  150. var memoryStream = new MemoryStream();
  151. var num = 0L;
  152. var array = new byte[4096];
  153. long bytes;
  154. do
  155. {
  156. bytes = reader.GetBytes(ordinal, num, array, 0, array.Length);
  157. memoryStream.Write(array, 0, (int)bytes);
  158. num += bytes;
  159. }
  160. while (bytes > 0L);
  161. memoryStream.Position = 0;
  162. return memoryStream;
  163. }
  164. }
  165. }