DataExtensions.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System.Text;
  2. using MediaBrowser.Model.Logging;
  3. using MediaBrowser.Model.Serialization;
  4. using System;
  5. using System.Data;
  6. using System.IO;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Server.Implementations.Persistence
  9. {
  10. static class DataExtensions
  11. {
  12. /// <summary>
  13. /// Determines whether the specified conn is open.
  14. /// </summary>
  15. /// <param name="conn">The conn.</param>
  16. /// <returns><c>true</c> if the specified conn is open; otherwise, <c>false</c>.</returns>
  17. public static bool IsOpen(this IDbConnection conn)
  18. {
  19. return conn.State == ConnectionState.Open;
  20. }
  21. public static IDataParameter GetParameter(this IDbCommand cmd, int index)
  22. {
  23. return (IDataParameter)cmd.Parameters[index];
  24. }
  25. public static IDataParameter Add(this IDataParameterCollection paramCollection, IDbCommand cmd, string name, DbType type)
  26. {
  27. var param = cmd.CreateParameter();
  28. param.ParameterName = name;
  29. param.DbType = type;
  30. paramCollection.Add(param);
  31. return param;
  32. }
  33. public static IDataParameter Add(this IDataParameterCollection paramCollection, IDbCommand cmd, string name)
  34. {
  35. var param = cmd.CreateParameter();
  36. param.ParameterName = name;
  37. paramCollection.Add(param);
  38. return param;
  39. }
  40. /// <summary>
  41. /// Gets a stream from a DataReader at a given ordinal
  42. /// </summary>
  43. /// <param name="reader">The reader.</param>
  44. /// <param name="ordinal">The ordinal.</param>
  45. /// <returns>Stream.</returns>
  46. /// <exception cref="System.ArgumentNullException">reader</exception>
  47. public static Stream GetMemoryStream(this IDataReader reader, int ordinal)
  48. {
  49. if (reader == null)
  50. {
  51. throw new ArgumentNullException("reader");
  52. }
  53. var memoryStream = new MemoryStream();
  54. var num = 0L;
  55. var array = new byte[4096];
  56. long bytes;
  57. do
  58. {
  59. bytes = reader.GetBytes(ordinal, num, array, 0, array.Length);
  60. memoryStream.Write(array, 0, (int)bytes);
  61. num += bytes;
  62. }
  63. while (bytes > 0L);
  64. memoryStream.Position = 0;
  65. return memoryStream;
  66. }
  67. /// <summary>
  68. /// Runs the queries.
  69. /// </summary>
  70. /// <param name="connection">The connection.</param>
  71. /// <param name="queries">The queries.</param>
  72. /// <param name="logger">The logger.</param>
  73. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  74. /// <exception cref="System.ArgumentNullException">queries</exception>
  75. public static void RunQueries(this IDbConnection connection, string[] queries, ILogger logger)
  76. {
  77. if (queries == null)
  78. {
  79. throw new ArgumentNullException("queries");
  80. }
  81. using (var tran = connection.BeginTransaction())
  82. {
  83. try
  84. {
  85. using (var cmd = connection.CreateCommand())
  86. {
  87. foreach (var query in queries)
  88. {
  89. cmd.Transaction = tran;
  90. cmd.CommandText = query;
  91. cmd.ExecuteNonQuery();
  92. }
  93. }
  94. tran.Commit();
  95. }
  96. catch (Exception e)
  97. {
  98. logger.ErrorException("Error running queries", e);
  99. tran.Rollback();
  100. throw;
  101. }
  102. }
  103. }
  104. public static void Attach(IDbConnection db, string path, string alias)
  105. {
  106. using (var cmd = db.CreateCommand())
  107. {
  108. cmd.CommandText = string.Format("attach '{0}' as {1};", path, alias);
  109. cmd.ExecuteNonQuery();
  110. }
  111. }
  112. /// <summary>
  113. /// Serializes to bytes.
  114. /// </summary>
  115. /// <param name="json">The json.</param>
  116. /// <param name="obj">The obj.</param>
  117. /// <returns>System.Byte[][].</returns>
  118. /// <exception cref="System.ArgumentNullException">obj</exception>
  119. public static byte[] SerializeToBytes(this IJsonSerializer json, object obj)
  120. {
  121. if (obj == null)
  122. {
  123. throw new ArgumentNullException("obj");
  124. }
  125. using (var stream = new MemoryStream())
  126. {
  127. json.SerializeToStream(obj, stream);
  128. return stream.ToArray();
  129. }
  130. }
  131. public static void AddColumn(this IDbConnection connection, ILogger logger, string table, string columnName, string type)
  132. {
  133. using (var cmd = connection.CreateCommand())
  134. {
  135. cmd.CommandText = "PRAGMA table_info(" + table + ")";
  136. using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
  137. {
  138. while (reader.Read())
  139. {
  140. if (!reader.IsDBNull(1))
  141. {
  142. var name = reader.GetString(1);
  143. if (string.Equals(name, columnName, StringComparison.OrdinalIgnoreCase))
  144. {
  145. return;
  146. }
  147. }
  148. }
  149. }
  150. }
  151. var builder = new StringBuilder();
  152. builder.AppendLine("alter table " + table);
  153. builder.AppendLine("add column " + columnName + " " + type);
  154. connection.RunQueries(new[] { builder.ToString() }, logger);
  155. }
  156. }
  157. }