using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using Foo.DataAccessLayer.DataTypes;
namespace Foo.DataAccessLayer
{
internal class dVersioningTables
{
///
/// Quick Check if there is Version Information available for this Table
///
/// a table name to check
/// true if the DB has version information for this Table
internal bool DoesTableHaveVersionEntry(string TableName)
{
// Check TableName Integrity
if (!DataTypeValidation.IsValidTableName(TableName))
return false;
object obj = null;
lock (this)
{
string sql = "SELECT [TableName] FROM VersioningTables WHERE [TableName] = @tablename";
SqlCeParameter[] sqlparams = new SqlCeParameter[] { new SqlCeParameter("@tablename", TableName) };
obj = DB.RunSQLCommandTextExecuteScalar(sql, sqlparams);
}
return (obj != null);
}
///
/// Retrieve the Version information for the specified Table
///
/// a table name to check
/// version information string, or empty.string if not found
internal string GetVersionInformationForTable(string TableName)
{
// Check TableName Integrity
if (!DataTypeValidation.IsValidTableName(TableName))
return String.Empty;
object obj = null;
lock (this)
{
string sql = "SELECT [Version] FROM VersioningTables WHERE [TableName] = @tablename";
SqlCeParameter[] sqlparams = new SqlCeParameter[] { new SqlCeParameter("@tablename", TableName) };
obj = DB.RunSQLCommandTextExecuteScalar(sql, sqlparams);
if (obj != null)
return obj.ToString();
}
return String.Empty;
}
///
/// Use this most often to insert/update the version information of a table into the VersioningTables Table
///
/// The table name for which to insert version information
/// the version information in the format of n.n.nnnn
/// true if successfully inserted or updated, false otherwise
internal bool AddUpdateVersionInformationForSpecifiedTable(string TableName, string VersionInformation)
{
// Check TableName Integrity
if (!DataTypeValidation.IsValidTableName(TableName))
return false;
// Check Version Integrity
if (!DataTypeValidation.IsValidVersionInformation(VersionInformation))
return false;
lock (this)
{
// First Determine if we need to update or insert
bool bUpdate = DoesTableHaveVersionEntry(TableName);
if (!bUpdate) // we need to insert
{
string sql = "INSERT INTO VersioningTables ([TableName],[Version]) VALUES (@tablename,@version)";
SqlCeParameter[] sqlparams = new SqlCeParameter[]
{
new SqlCeParameter("@tablename", TableName),
new SqlCeParameter("@version", VersionInformation),
};
int nResult = DB.RunSQLCommandTextExecuteNonQuery(sql, sqlparams);
return (nResult == 1);
}
else // we need to update
{
string sql2 = "UPDATE VersioningTables SET [Version]=@version WHERE [TableName]=@tablename";
SqlCeParameter[] sqlparams2 = new SqlCeParameter[]
{
new SqlCeParameter("@tablename", TableName),
new SqlCeParameter("@version", VersionInformation),
};
int nResult2 = DB.RunSQLCommandTextExecuteNonQuery(sql2, sqlparams2);
return (nResult2 == 1);
}
}
}
}
}