Start modernization

This commit is contained in:
2023-03-10 12:18:03 -05:00
parent a0214b98f1
commit f480a6c373
57 changed files with 661 additions and 2921 deletions

View File

@@ -12,12 +12,15 @@ namespace FeedCenter.Data
{
#region Static database settings
public static string DatabaseFile;
public static string DatabasePath;
#endregion
#region File version
public static FeedCenterEntities Entities;
private enum SqlServerCeFileVersion
{
Unknown,
@@ -27,6 +30,11 @@ namespace FeedCenter.Data
Version40,
}
public static void Load()
{
Entities = new FeedCenterEntities();
}
private static SqlServerCeFileVersion GetFileVersion(string databasePath)
{
// Create a mapping of version numbers to the version enumeration
@@ -43,18 +51,15 @@ namespace FeedCenter.Data
try
{
// Open the database file
using (var stream = new FileStream(databasePath, FileMode.Open, FileAccess.Read))
{
// Read the file using the binary reader
var reader = new BinaryReader(stream);
using var stream = new FileStream(databasePath, FileMode.Open, FileAccess.Read);
// Read the file using the binary reader
var reader = new BinaryReader(stream);
// Seek to the version signature
stream.Seek(16, SeekOrigin.Begin);
// Seek to the version signature
stream.Seek(16, SeekOrigin.Begin);
// Read the version signature
signature = reader.ReadInt32();
}
// Read the version signature
signature = reader.ReadInt32();
}
catch (Exception exception)
{
@@ -69,25 +74,23 @@ namespace FeedCenter.Data
#endregion
public static bool DatabaseExists => File.Exists(DatabasePath);
public static bool DatabaseExists => File.Exists(DatabaseFile);
public static void CreateDatabase()
{
Tracer.WriteLine("Creating database engine");
// Create the database engine
using (var engine = new SqlCeEngine($"Data Source={DatabasePath}"))
{
Tracer.WriteLine("Creating database");
using var engine = new SqlCeEngine($"Data Source={DatabaseFile}");
Tracer.WriteLine("Creating database");
// Create the database itself
engine.CreateDatabase();
// Create the database itself
engine.CreateDatabase();
Tracer.WriteLine("Running database script");
Tracer.WriteLine("Running database script");
// Run the creation script
ExecuteScript(Resources.CreateDatabase);
}
// Run the creation script
ExecuteScript(Resources.CreateDatabase);
}
private static int GetVersion(SqlCeConnection connection)
@@ -97,14 +100,14 @@ namespace FeedCenter.Data
try
{
// Check the database version table
using (var command = new SqlCeCommand("SELECT Value FROM DatabaseVersion", connection))
versionString = command.ExecuteScalar().ToString();
using var command = new SqlCeCommand("SELECT Value FROM DatabaseVersion", connection);
versionString = command.ExecuteScalar().ToString();
}
catch (SqlCeException)
{
// Check the setting table for the version
using (var command = new SqlCeCommand("SELECT Value FROM Setting WHERE Name = 'DatabaseVersion'", connection))
versionString = command.ExecuteScalar().ToString();
using var command = new SqlCeCommand("SELECT Value FROM Setting WHERE Name = 'DatabaseVersion'", connection);
versionString = command.ExecuteScalar().ToString();
}
if (string.IsNullOrEmpty(versionString))
@@ -120,7 +123,7 @@ namespace FeedCenter.Data
Tracer.WriteLine("Getting database file version");
// Get the database file version
var fileVersion = GetFileVersion(DatabasePath);
var fileVersion = GetFileVersion(DatabaseFile);
Tracer.WriteLine("Database file version: {0}", fileVersion);
@@ -130,54 +133,50 @@ namespace FeedCenter.Data
Tracer.WriteLine("Creating database engine");
// Create the database engine
using (var engine = new SqlCeEngine($"Data Source={DatabasePath}"))
{
Tracer.WriteLine("Upgrading database");
using var engine = new SqlCeEngine($"Data Source={DatabaseFile}");
Tracer.WriteLine("Upgrading database");
// Upgrade the database (if needed)
engine.Upgrade();
}
// Upgrade the database (if needed)
engine.Upgrade();
}
Tracer.WriteLine("Getting database version");
// Create a database connection
using (var connection = new SqlCeConnection($"Data Source={DatabasePath}"))
using var connection = new SqlCeConnection($"Data Source={DatabaseFile}");
// Open the connection
connection.Open();
// Get the database version
var databaseVersion = GetVersion(connection);
// Create a dictionary of database upgrade scripts and their version numbers
var scriptList = new Dictionary<int, string>();
// Loop over the properties of the resource object looking for update scripts
foreach (var property in typeof(Resources).GetProperties().Where(property => property.Name.StartsWith("DatabaseUpdate")))
{
// Open the connection
connection.Open();
// Get the name of the property
var propertyName = property.Name;
// Get the database version
var databaseVersion = GetVersion(connection);
// Extract the version from the name
var version = int.Parse(propertyName.Substring(propertyName.IndexOf("_", StringComparison.Ordinal) + 1));
// Create a dictionary of database upgrade scripts and their version numbers
var scriptList = new Dictionary<int, string>();
// Add to the script list
scriptList[version] = propertyName;
}
// Loop over the properties of the resource object looking for update scripts
foreach (var property in typeof(Resources).GetProperties().Where(property => property.Name.StartsWith("DatabaseUpdate")))
// Loop over the scripts ordered by version
foreach (var pair in scriptList.OrderBy(pair => pair.Key))
{
// If the database version is less than or equal to the script version the script needs to run
if (databaseVersion <= pair.Key)
{
// Get the name of the property
var propertyName = property.Name;
// Get the script text
var scriptText = Resources.ResourceManager.GetString(pair.Value);
// Extract the version from the name
var version = int.Parse(propertyName.Substring(propertyName.IndexOf("_", StringComparison.Ordinal) + 1));
// Add to the script list
scriptList[version] = propertyName;
}
// Loop over the scripts ordered by version
foreach (var pair in scriptList.OrderBy(pair => pair.Key))
{
// If the database version is less than or equal to the script version the script needs to run
if (databaseVersion <= pair.Key)
{
// Get the script text
var scriptText = Resources.ResourceManager.GetString(pair.Value);
// Run the script
ExecuteScript(scriptText);
}
// Run the script
ExecuteScript(scriptText);
}
}
}
@@ -187,36 +186,32 @@ namespace FeedCenter.Data
Tracer.WriteLine("Creating database engine");
// Create the database engine
using (var engine = new SqlCeEngine($"Data Source={DatabasePath}"))
{
Tracer.WriteLine("Shrinking database");
using var engine = new SqlCeEngine($"Data Source={DatabaseFile}");
Tracer.WriteLine("Shrinking database");
// Compact the database
engine.Shrink();
}
// Compact the database
engine.Shrink();
}
private static void ExecuteScript(string scriptText)
{
// Create a database connection
using (var connection = new SqlCeConnection($"Data Source={DatabasePath}"))
using var connection = new SqlCeConnection($"Data Source={DatabaseFile}");
// Open the connection
connection.Open();
// Setup the delimiters
var delimiters = new[] { "\r\nGO\r\n" };
// Split the script at the delimiters
var statements = scriptText.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
// Loop over each statement in the script
foreach (var statement in statements)
{
// Open the connection
connection.Open();
// Setup the delimiters
var delimiters = new[] { "\r\nGO\r\n" };
// Split the script at the delimiters
var statements = scriptText.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
// Loop over each statement in the script
foreach (var statement in statements)
{
// Execute the statement
using (var command = new SqlCeCommand(statement, connection))
command.ExecuteNonQuery();
}
// Execute the statement
using var command = new SqlCeCommand(statement, connection);
command.ExecuteNonQuery();
}
}
}

View File

@@ -0,0 +1,29 @@
using Realms;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
namespace FeedCenter.Data
{
public class RealmObservableCollection<T> : ObservableCollection<T> where T : IRealmObject
{
private readonly Realm _realm;
public RealmObservableCollection(Realm realm) : base(realm.All<T>())
{
_realm = realm;
}
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (e.OldItems != null)
foreach (T item in e.OldItems)
_realm.Remove(item);
if (e.NewItems != null)
foreach (T item in e.NewItems)
_realm.Add(item);
base.OnCollectionChanged(e);
}
}
}