SqliteExtensions.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 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. var sqliteParam = AddParam(cmd, param);
  42. sqliteParam.Value = data;
  43. return sqliteParam;
  44. }
  45. /// <summary>
  46. /// Determines whether the specified conn is open.
  47. /// </summary>
  48. /// <param name="conn">The conn.</param>
  49. /// <returns><c>true</c> if the specified conn is open; otherwise, <c>false</c>.</returns>
  50. public static bool IsOpen(this IDbConnection conn)
  51. {
  52. return conn.State == ConnectionState.Open;
  53. }
  54. public static IDataParameter GetParameter(this IDbCommand cmd, int index)
  55. {
  56. return (IDataParameter)cmd.Parameters[index];
  57. }
  58. public static IDataParameter Add(this IDataParameterCollection paramCollection, IDbCommand cmd, string name, DbType type)
  59. {
  60. var param = cmd.CreateParameter();
  61. param.ParameterName = name;
  62. param.DbType = type;
  63. paramCollection.Add(param);
  64. return param;
  65. }
  66. public static IDataParameter Add(this IDataParameterCollection paramCollection, IDbCommand cmd, string name)
  67. {
  68. var param = cmd.CreateParameter();
  69. param.ParameterName = name;
  70. paramCollection.Add(param);
  71. return param;
  72. }
  73. /// <summary>
  74. /// Gets a stream from a DataReader at a given ordinal
  75. /// </summary>
  76. /// <param name="reader">The reader.</param>
  77. /// <param name="ordinal">The ordinal.</param>
  78. /// <returns>Stream.</returns>
  79. /// <exception cref="System.ArgumentNullException">reader</exception>
  80. public static Stream GetMemoryStream(this IDataReader reader, int ordinal)
  81. {
  82. if (reader == null)
  83. {
  84. throw new ArgumentNullException("reader");
  85. }
  86. var memoryStream = new MemoryStream();
  87. var num = 0L;
  88. var array = new byte[4096];
  89. long bytes;
  90. do
  91. {
  92. bytes = reader.GetBytes(ordinal, num, array, 0, array.Length);
  93. memoryStream.Write(array, 0, (int)bytes);
  94. num += bytes;
  95. }
  96. while (bytes > 0L);
  97. memoryStream.Position = 0;
  98. return memoryStream;
  99. }
  100. /// <summary>
  101. /// Runs the queries.
  102. /// </summary>
  103. /// <param name="connection">The connection.</param>
  104. /// <param name="queries">The queries.</param>
  105. /// <param name="logger">The logger.</param>
  106. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  107. /// <exception cref="System.ArgumentNullException">queries</exception>
  108. public static void RunQueries(this IDbConnection connection, string[] queries, ILogger logger)
  109. {
  110. if (queries == null)
  111. {
  112. throw new ArgumentNullException("queries");
  113. }
  114. using (var tran = connection.BeginTransaction())
  115. {
  116. try
  117. {
  118. using (var cmd = connection.CreateCommand())
  119. {
  120. foreach (var query in queries)
  121. {
  122. cmd.Transaction = tran;
  123. cmd.CommandText = query;
  124. cmd.ExecuteNonQuery();
  125. }
  126. }
  127. tran.Commit();
  128. }
  129. catch (Exception e)
  130. {
  131. logger.ErrorException("Error running queries", e);
  132. tran.Rollback();
  133. throw;
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// Connects to db.
  139. /// </summary>
  140. /// <param name="dbPath">The db path.</param>
  141. /// <returns>Task{IDbConnection}.</returns>
  142. /// <exception cref="System.ArgumentNullException">dbPath</exception>
  143. public static async Task<SQLiteConnection> ConnectToDb(string dbPath)
  144. {
  145. if (string.IsNullOrEmpty(dbPath))
  146. {
  147. throw new ArgumentNullException("dbPath");
  148. }
  149. var connectionstr = new SQLiteConnectionStringBuilder
  150. {
  151. PageSize = 4096,
  152. CacheSize = 4096,
  153. SyncMode = SynchronizationModes.Normal,
  154. DataSource = dbPath,
  155. JournalMode = SQLiteJournalModeEnum.Wal
  156. };
  157. var connection = new SQLiteConnection(connectionstr.ConnectionString);
  158. await connection.OpenAsync().ConfigureAwait(false);
  159. return connection;
  160. }
  161. }
  162. }