Still needs a work over in terms of error handling, progress reporting, warning output, refactoring etc. Also misses the volume reuse/reclaim algorithms. But it passes the unit tests now, with and without a local database. git-svn-id: https://duplicati.googlecode.com/svn/sandboxes/Kenneth/ForestHash@1526 59da171f-624f-0410-aa54-27559c288bec
105 lines
3.3 KiB
C#
105 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace Duplicati.Library.Main.ForestHash.Database
|
|
{
|
|
public static class ExtensionMethods
|
|
{
|
|
public static void AddParameters(this System.Data.IDbCommand self, int count)
|
|
{
|
|
for(var i = 0; i < count; i++)
|
|
self.Parameters.Add(self.CreateParameter());
|
|
}
|
|
|
|
public static void AddParameter(this System.Data.IDbCommand self)
|
|
{
|
|
self.Parameters.Add(self.CreateParameter());
|
|
}
|
|
|
|
public static void AddParameter<T>(this System.Data.IDbCommand self, T value)
|
|
{
|
|
var p = self.CreateParameter();
|
|
p.Value = value;
|
|
self.Parameters.Add(p);
|
|
}
|
|
|
|
public static void SetParameterValue<T>(this System.Data.IDbCommand self, int index, T value)
|
|
{
|
|
((System.Data.IDataParameter)self.Parameters[index]).Value = value;
|
|
}
|
|
|
|
public static long ExecuteNonQuery(this System.Data.IDbCommand self, string cmd, params object[] values)
|
|
{
|
|
if (cmd != null)
|
|
self.CommandText = cmd;
|
|
|
|
if (values != null && values.Length > 0)
|
|
{
|
|
self.Parameters.Clear();
|
|
foreach (var n in values)
|
|
self.AddParameter(n);
|
|
}
|
|
|
|
return self.ExecuteNonQuery();
|
|
}
|
|
|
|
public static object ExecuteScalar(this System.Data.IDbCommand self, string cmd, params object[] values)
|
|
{
|
|
if (cmd != null)
|
|
self.CommandText = cmd;
|
|
|
|
if (values != null && values.Length > 0)
|
|
{
|
|
self.Parameters.Clear();
|
|
foreach (var n in values)
|
|
self.AddParameter(n);
|
|
}
|
|
|
|
return self.ExecuteScalar();
|
|
}
|
|
|
|
public static long GetLastRowID(this System.Data.IDbConnection self, System.Data.IDbTransaction tr = null)
|
|
{
|
|
using (var c = self.CreateCommand())
|
|
{
|
|
if (tr != null)
|
|
c.Transaction = tr;
|
|
c.CommandText = "SELECT last_insert_rowid()";
|
|
return Convert.ToInt64(c.ExecuteScalar());
|
|
}
|
|
|
|
}
|
|
|
|
public static void DumpSQL(this System.Data.IDbConnection self, string sql, params object[] parameters)
|
|
{
|
|
using (var c = self.CreateCommand())
|
|
{
|
|
c.CommandText = sql;
|
|
if (parameters != null)
|
|
foreach (var p in parameters)
|
|
c.AddParameter(p);
|
|
|
|
using (var rd = c.ExecuteReader())
|
|
{
|
|
for (int i = 0; i < rd.FieldCount; i++)
|
|
Console.Write((i == 0 ? "" : "\t") + rd.GetName(i));
|
|
Console.WriteLine();
|
|
|
|
long n = 0;
|
|
while (rd.Read())
|
|
{
|
|
for (int i = 0; i < rd.FieldCount; i++)
|
|
Console.Write(string.Format((i == 0 ? "{0}" : "\t{0}"), rd.GetValue(i)));
|
|
Console.WriteLine();
|
|
n++;
|
|
}
|
|
Console.WriteLine("{0} records", n);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|