95 lines
3.5 KiB
C#
95 lines
3.5 KiB
C#
|
|
using System.Collections.Generic;
|
|||
|
|
using System.ComponentModel;
|
|||
|
|
using System.Data;
|
|||
|
|
|
|||
|
|
namespace DatabaseImport
|
|||
|
|
{
|
|||
|
|
public abstract class DbTimeStampBase : IDbTimeStampAware, INotifyPropertyChanged
|
|||
|
|
{
|
|||
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|||
|
|
|
|||
|
|
protected bool SetProperty<T>(ref T storage, T value, string propertyName = null)
|
|||
|
|
{
|
|||
|
|
if (Equals(storage, value)) return false;
|
|||
|
|
|
|||
|
|
storage = value;
|
|||
|
|
OnPropertyChanged(propertyName);
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
protected void OnPropertyChanged(string propertyName = null)
|
|||
|
|
{
|
|||
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected long DbTimeStamp;
|
|||
|
|
public long GetTimeStampMemory()
|
|||
|
|
{
|
|||
|
|
return DbTimeStamp;
|
|||
|
|
}
|
|||
|
|
public void SetTimeStampMemory(long value) { DbTimeStamp = value; }
|
|||
|
|
public void SetTimeStampMemory(DataRow row)
|
|||
|
|
{
|
|||
|
|
DbTimeStamp = 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void SetTimeStampMemory(IDataReader reader)
|
|||
|
|
{
|
|||
|
|
DbTimeStamp = 0;
|
|||
|
|
}
|
|||
|
|
public long GetTimeStampDb(Dictionary<string, long> lookup)
|
|||
|
|
{
|
|||
|
|
//var constraints = GetConstraints();
|
|||
|
|
return 0; //lookup.ContainsKey(constraints[0].DbValue.ToString()) ? lookup[constraints[0].DbValue.ToString()] : GetTimeStampDb();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public long GetTimeStampDb()
|
|||
|
|
{
|
|||
|
|
return 0;
|
|||
|
|
//using (var sql = DbOperations.GetSQLCommand())
|
|||
|
|
//{
|
|||
|
|
// var sb = new StringBuilder(50);
|
|||
|
|
// sb.AppendFormat("SELECT DbTimeStamp as A FROM {0} ", LookupTable);
|
|||
|
|
|
|||
|
|
// var constraints = GetConstraints();
|
|||
|
|
// for (var i = 0; i < constraints.Length; i++)
|
|||
|
|
// {
|
|||
|
|
// sb.Append(0 == i ? "WHERE " : " AND ");
|
|||
|
|
// sb.AppendFormat("{0}=@{1}", constraints[i].ColumnName, i);
|
|||
|
|
// DbOperations.CreateParam(sql, string.Format("@{0}", i), constraints[i].DbType, constraints[i].DbValue);
|
|||
|
|
// }
|
|||
|
|
// sql.CommandText = sb.ToString();
|
|||
|
|
// using (var ds = DbOperations.Connection.QueryDataSet(sql))
|
|||
|
|
// {
|
|||
|
|
// if (ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0) return 0;
|
|||
|
|
// if (DBNull.Value.Equals(ds.Tables[0].Rows[0]["A"])) return 0;
|
|||
|
|
// try
|
|||
|
|
// {
|
|||
|
|
// var res = BitConverter.ToInt64(ds.Tables[0].Rows[0]["A"] as byte[], 0);
|
|||
|
|
// System.Diagnostics.Trace.WriteLine(string.Format("Db: {0}", res));
|
|||
|
|
// return res;
|
|||
|
|
// }
|
|||
|
|
// catch (Exception ex) { APILogger.Log(ex); return 0; }
|
|||
|
|
// }
|
|||
|
|
//}
|
|||
|
|
}
|
|||
|
|
public bool IsOutOfDate()
|
|||
|
|
{
|
|||
|
|
var db = GetTimeStampDb();
|
|||
|
|
var mem = GetTimeStampMemory();
|
|||
|
|
//if there's no record in the db, don't mark as out of date
|
|||
|
|
if (db == 0) { return false; }
|
|||
|
|
//if is in db, but in memory is new, allow overwrite...
|
|||
|
|
if (mem == 0 && db != 0) { mem = db; SetTimeStampMemory(db); }
|
|||
|
|
return db != mem;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public interface IDbTimeStampAware
|
|||
|
|
{
|
|||
|
|
long GetTimeStampMemory();
|
|||
|
|
void SetTimeStampMemory(long value);
|
|||
|
|
long GetTimeStampDb();
|
|||
|
|
bool IsOutOfDate();
|
|||
|
|
}
|
|||
|
|
}
|