using System;
using System.Data;
using System.IO;
using System.Text;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
namespace Emby.Server.Core.Data
{
public static class DataExtensions
{
///
/// Determines whether the specified conn is open.
///
/// The conn.
/// true if the specified conn is open; otherwise, false.
public static bool IsOpen(this IDbConnection conn)
{
return conn.State == ConnectionState.Open;
}
public static IDataParameter GetParameter(this IDbCommand cmd, int index)
{
return (IDataParameter)cmd.Parameters[index];
}
public static IDataParameter Add(this IDataParameterCollection paramCollection, IDbCommand cmd, string name, DbType type)
{
var param = cmd.CreateParameter();
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);
return param;
}
///
/// Gets a stream from a DataReader at a given ordinal
///
/// Stream.
/// reader
public static Stream GetMemoryStream(this IDataReader reader, int ordinal, IMemoryStreamFactory streamProvider)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
var memoryStream = streamProvider.CreateNew();
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;
}
///
/// Runs the queries.
///
/// The connection.
/// The queries.
/// The logger.
/// true if XXXX, false otherwise
/// queries
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;
}
}
}
public static void Attach(IDbConnection db, string path, string alias)
{
using (var cmd = db.CreateCommand())
{
cmd.CommandText = string.Format("attach @dbPath as {0};", alias);
cmd.Parameters.Add(cmd, "@dbPath", DbType.String);
cmd.GetParameter(0).Value = path;
cmd.ExecuteNonQuery();
}
}
///
/// Serializes to bytes.
///
/// System.Byte[][].
/// obj
public static byte[] SerializeToBytes(this IJsonSerializer json, object obj, IMemoryStreamFactory streamProvider)
{
if (obj == null)
{
throw new ArgumentNullException("obj");
}
using (var stream = streamProvider.CreateNew())
{
json.SerializeToStream(obj, stream);
return stream.ToArray();
}
}
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);
}
}
}