DataExtensions.cs 6.0 KB

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