jellyfin/MediaBrowser.Server.Implementations/Persistence/DataExtensions.cs

185 lines
6.0 KiB
C#
Raw Normal View History

2015-03-14 18:42:09 -07:00
using System.Text;
using MediaBrowser.Model.Logging;
2014-11-07 15:37:10 -07:00
using MediaBrowser.Model.Serialization;
2013-08-09 18:16:31 -07:00
using System;
2013-06-18 02:43:07 -07:00
using System.Data;
2013-06-18 12:16:27 -07:00
using System.IO;
using System.Threading.Tasks;
2013-06-18 02:43:07 -07:00
namespace MediaBrowser.Server.Implementations.Persistence
{
2016-05-01 14:48:37 -07:00
static class DataExtensions
2013-06-18 02:43:07 -07:00
{
/// <summary>
/// Determines whether the specified conn is open.
/// </summary>
/// <param name="conn">The conn.</param>
/// <returns><c>true</c> if the specified conn is open; otherwise, <c>false</c>.</returns>
2013-07-03 17:11:11 -07:00
public static bool IsOpen(this IDbConnection conn)
2013-06-18 02:43:07 -07:00
{
return conn.State == ConnectionState.Open;
}
2013-06-18 12:16:27 -07:00
2013-07-03 17:11:11 -07:00
public static IDataParameter GetParameter(this IDbCommand cmd, int index)
{
return (IDataParameter)cmd.Parameters[index];
}
2016-05-01 14:48:37 -07:00
2013-07-03 17:11:11 -07:00
public static IDataParameter Add(this IDataParameterCollection paramCollection, IDbCommand cmd, string name, DbType type)
{
var param = cmd.CreateParameter();
2016-05-01 14:48:37 -07:00
2013-07-03 17:11:11 -07:00
param.ParameterName = name;
param.DbType = type;
paramCollection.Add(param);
return param;
}
public static IDataParameter Add(this IDataParameterCollection paramCollection, IDbCommand cmd, string name)
{
var param = cmd.CreateParameter();
param.ParameterName = name;
paramCollection.Add(param);
2016-05-01 14:48:37 -07:00
2013-07-03 17:11:11 -07:00
return param;
}
2016-05-01 14:48:37 -07:00
2013-06-18 12:16:27 -07:00
/// <summary>
/// Gets a stream from a DataReader at a given ordinal
/// </summary>
/// <param name="reader">The reader.</param>
/// <param name="ordinal">The ordinal.</param>
/// <returns>Stream.</returns>
/// <exception cref="System.ArgumentNullException">reader</exception>
public static Stream GetMemoryStream(this IDataReader reader, int ordinal)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
var memoryStream = new MemoryStream();
var num = 0L;
var array = new byte[4096];
long bytes;
do
{
bytes = reader.GetBytes(ordinal, num, array, 0, array.Length);
memoryStream.Write(array, 0, (int)bytes);
num += bytes;
}
while (bytes > 0L);
memoryStream.Position = 0;
return memoryStream;
}
/// <summary>
/// Runs the queries.
/// </summary>
/// <param name="connection">The connection.</param>
/// <param name="queries">The queries.</param>
/// <param name="logger">The logger.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
/// <exception cref="System.ArgumentNullException">queries</exception>
public static void RunQueries(this IDbConnection connection, string[] queries, ILogger logger)
{
if (queries == null)
{
throw new ArgumentNullException("queries");
}
using (var tran = connection.BeginTransaction())
{
try
{
using (var cmd = connection.CreateCommand())
{
foreach (var query in queries)
{
cmd.Transaction = tran;
cmd.CommandText = query;
cmd.ExecuteNonQuery();
}
}
tran.Commit();
}
catch (Exception e)
{
logger.ErrorException("Error running queries", e);
tran.Rollback();
throw;
}
}
}
2015-10-13 09:52:27 -07:00
public static void Attach(IDbConnection db, string path, string alias)
{
using (var cmd = db.CreateCommand())
{
2016-05-24 11:28:58 -07:00
cmd.CommandText = string.Format("attach @dbPath as {0};", alias);
cmd.Parameters.Add(cmd, "@dbPath", DbType.String);
cmd.GetParameter(0).Value = path;
2015-10-13 09:52:27 -07:00
cmd.ExecuteNonQuery();
}
}
2014-08-04 20:41:56 -07:00
/// <summary>
/// Serializes to bytes.
/// </summary>
/// <param name="json">The json.</param>
/// <param name="obj">The obj.</param>
/// <returns>System.Byte[][].</returns>
/// <exception cref="System.ArgumentNullException">obj</exception>
public static byte[] SerializeToBytes(this IJsonSerializer json, object obj)
{
if (obj == null)
{
throw new ArgumentNullException("obj");
}
using (var stream = new MemoryStream())
{
json.SerializeToStream(obj, stream);
return stream.ToArray();
}
}
2015-03-14 18:42:09 -07:00
public static void AddColumn(this IDbConnection connection, ILogger logger, string table, string columnName, string type)
{
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = "PRAGMA table_info(" + table + ")";
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
{
while (reader.Read())
{
if (!reader.IsDBNull(1))
{
var name = reader.GetString(1);
if (string.Equals(name, columnName, StringComparison.OrdinalIgnoreCase))
{
return;
}
}
}
}
}
var builder = new StringBuilder();
builder.AppendLine("alter table " + table);
builder.AppendLine("add column " + columnName + " " + type);
connection.RunQueries(new[] { builder.ToString() }, logger);
}
2013-06-18 02:43:07 -07:00
}
}