2
0

DataExtensions.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Data;
  3. using System.IO;
  4. using System.Text;
  5. using MediaBrowser.Model.IO;
  6. using MediaBrowser.Model.Logging;
  7. using MediaBrowser.Model.Serialization;
  8. namespace Emby.Server.Core.Data
  9. {
  10. public 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. /// <returns>Stream.</returns>
  44. /// <exception cref="System.ArgumentNullException">reader</exception>
  45. public static Stream GetMemoryStream(this IDataReader reader, int ordinal, IMemoryStreamFactory streamProvider)
  46. {
  47. if (reader == null)
  48. {
  49. throw new ArgumentNullException("reader");
  50. }
  51. var memoryStream = streamProvider.CreateNew();
  52. var num = 0L;
  53. var array = new byte[4096];
  54. long bytes;
  55. do
  56. {
  57. bytes = reader.GetBytes(ordinal, num, array, 0, array.Length);
  58. memoryStream.Write(array, 0, (int)bytes);
  59. num += bytes;
  60. }
  61. while (bytes > 0L);
  62. memoryStream.Position = 0;
  63. return memoryStream;
  64. }
  65. /// <summary>
  66. /// Runs the queries.
  67. /// </summary>
  68. /// <param name="connection">The connection.</param>
  69. /// <param name="queries">The queries.</param>
  70. /// <param name="logger">The logger.</param>
  71. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  72. /// <exception cref="System.ArgumentNullException">queries</exception>
  73. public static void RunQueries(this IDbConnection connection, string[] queries, ILogger logger)
  74. {
  75. if (queries == null)
  76. {
  77. throw new ArgumentNullException("queries");
  78. }
  79. using (var tran = connection.BeginTransaction())
  80. {
  81. try
  82. {
  83. using (var cmd = connection.CreateCommand())
  84. {
  85. foreach (var query in queries)
  86. {
  87. cmd.Transaction = tran;
  88. cmd.CommandText = query;
  89. cmd.ExecuteNonQuery();
  90. }
  91. }
  92. tran.Commit();
  93. }
  94. catch (Exception e)
  95. {
  96. logger.ErrorException("Error running queries", e);
  97. tran.Rollback();
  98. throw;
  99. }
  100. }
  101. }
  102. public static void Attach(IDbConnection db, string path, string alias)
  103. {
  104. using (var cmd = db.CreateCommand())
  105. {
  106. cmd.CommandText = string.Format("attach @dbPath as {0};", alias);
  107. cmd.Parameters.Add(cmd, "@dbPath", DbType.String);
  108. cmd.GetParameter(0).Value = path;
  109. cmd.ExecuteNonQuery();
  110. }
  111. }
  112. /// <summary>
  113. /// Serializes to bytes.
  114. /// </summary>
  115. /// <returns>System.Byte[][].</returns>
  116. /// <exception cref="System.ArgumentNullException">obj</exception>
  117. public static byte[] SerializeToBytes(this IJsonSerializer json, object obj, IMemoryStreamFactory streamProvider)
  118. {
  119. if (obj == null)
  120. {
  121. throw new ArgumentNullException("obj");
  122. }
  123. using (var stream = streamProvider.CreateNew())
  124. {
  125. json.SerializeToStream(obj, stream);
  126. return stream.ToArray();
  127. }
  128. }
  129. public static void AddColumn(this IDbConnection connection, ILogger logger, string table, string columnName, string type)
  130. {
  131. using (var cmd = connection.CreateCommand())
  132. {
  133. cmd.CommandText = "PRAGMA table_info(" + table + ")";
  134. using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
  135. {
  136. while (reader.Read())
  137. {
  138. if (!reader.IsDBNull(1))
  139. {
  140. var name = reader.GetString(1);
  141. if (string.Equals(name, columnName, StringComparison.OrdinalIgnoreCase))
  142. {
  143. return;
  144. }
  145. }
  146. }
  147. }
  148. }
  149. var builder = new StringBuilder();
  150. builder.AppendLine("alter table " + table);
  151. builder.AppendLine("add column " + columnName + " " + type);
  152. connection.RunQueries(new[] { builder.ToString() }, logger);
  153. }
  154. }
  155. }