2
0

SqliteExtensions.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Data;
  3. using System.Data.SQLite;
  4. using System.IO;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Model.Logging;
  7. namespace MediaBrowser.Server.Implementations.Persistence
  8. {
  9. /// <summary>
  10. /// Class SQLiteExtensions
  11. /// </summary>
  12. static class SqliteExtensions
  13. {
  14. /// <summary>
  15. /// Adds the param.
  16. /// </summary>
  17. /// <param name="cmd">The CMD.</param>
  18. /// <param name="param">The param.</param>
  19. /// <returns>SQLiteParameter.</returns>
  20. /// <exception cref="System.ArgumentNullException"></exception>
  21. public static SQLiteParameter AddParam(this SQLiteCommand cmd, string param)
  22. {
  23. if (string.IsNullOrEmpty(param))
  24. {
  25. throw new ArgumentNullException();
  26. }
  27. var sqliteParam = new SQLiteParameter(param);
  28. cmd.Parameters.Add(sqliteParam);
  29. return sqliteParam;
  30. }
  31. /// <summary>
  32. /// Adds the param.
  33. /// </summary>
  34. /// <param name="cmd">The CMD.</param>
  35. /// <param name="param">The param.</param>
  36. /// <param name="data">The data.</param>
  37. /// <returns>SQLiteParameter.</returns>
  38. /// <exception cref="System.ArgumentNullException"></exception>
  39. public static SQLiteParameter AddParam(this SQLiteCommand cmd, string param, object data)
  40. {
  41. if (string.IsNullOrEmpty(param))
  42. {
  43. throw new ArgumentNullException();
  44. }
  45. var sqliteParam = AddParam(cmd, param);
  46. sqliteParam.Value = data;
  47. return sqliteParam;
  48. }
  49. /// <summary>
  50. /// Determines whether the specified conn is open.
  51. /// </summary>
  52. /// <param name="conn">The conn.</param>
  53. /// <returns><c>true</c> if the specified conn is open; otherwise, <c>false</c>.</returns>
  54. public static bool IsOpen(this SQLiteConnection conn)
  55. {
  56. return conn.State == ConnectionState.Open;
  57. }
  58. /// <summary>
  59. /// Gets a stream from a DataReader at a given ordinal
  60. /// </summary>
  61. /// <param name="reader">The reader.</param>
  62. /// <param name="ordinal">The ordinal.</param>
  63. /// <returns>Stream.</returns>
  64. /// <exception cref="System.ArgumentNullException">reader</exception>
  65. public static Stream GetMemoryStream(this IDataReader reader, int ordinal)
  66. {
  67. if (reader == null)
  68. {
  69. throw new ArgumentNullException("reader");
  70. }
  71. var memoryStream = new MemoryStream();
  72. var num = 0L;
  73. var array = new byte[4096];
  74. long bytes;
  75. do
  76. {
  77. bytes = reader.GetBytes(ordinal, num, array, 0, array.Length);
  78. memoryStream.Write(array, 0, (int)bytes);
  79. num += bytes;
  80. }
  81. while (bytes > 0L);
  82. memoryStream.Position = 0;
  83. return memoryStream;
  84. }
  85. /// <summary>
  86. /// Runs the queries.
  87. /// </summary>
  88. /// <param name="connection">The connection.</param>
  89. /// <param name="queries">The queries.</param>
  90. /// <param name="logger">The logger.</param>
  91. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  92. /// <exception cref="System.ArgumentNullException">queries</exception>
  93. public static void RunQueries(this IDbConnection connection, string[] queries, ILogger logger)
  94. {
  95. if (queries == null)
  96. {
  97. throw new ArgumentNullException("queries");
  98. }
  99. using (var tran = connection.BeginTransaction())
  100. {
  101. try
  102. {
  103. using (var cmd = connection.CreateCommand())
  104. {
  105. foreach (var query in queries)
  106. {
  107. cmd.Transaction = tran;
  108. cmd.CommandText = query;
  109. cmd.ExecuteNonQuery();
  110. }
  111. }
  112. tran.Commit();
  113. }
  114. catch (Exception e)
  115. {
  116. logger.ErrorException("Error running queries", e);
  117. tran.Rollback();
  118. throw;
  119. }
  120. }
  121. }
  122. /// <summary>
  123. /// Connects to db.
  124. /// </summary>
  125. /// <param name="dbPath">The db path.</param>
  126. /// <returns>Task{IDbConnection}.</returns>
  127. /// <exception cref="System.ArgumentNullException">dbPath</exception>
  128. public static async Task<SQLiteConnection> ConnectToDb(string dbPath)
  129. {
  130. if (string.IsNullOrEmpty(dbPath))
  131. {
  132. throw new ArgumentNullException("dbPath");
  133. }
  134. var connectionstr = new SQLiteConnectionStringBuilder
  135. {
  136. PageSize = 4096,
  137. CacheSize = 4096,
  138. SyncMode = SynchronizationModes.Off,
  139. DataSource = dbPath,
  140. JournalMode = SQLiteJournalModeEnum.Wal
  141. };
  142. var connection = new SQLiteConnection(connectionstr.ConnectionString);
  143. await connection.OpenAsync().ConfigureAwait(false);
  144. return connection;
  145. }
  146. }
  147. }