SqliteExtensions.cs 6.2 KB

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