diff --git a/Headerer/Database.cs b/Headerer/Database.cs
index 50cdb5b5..9191e274 100644
--- a/Headerer/Database.cs
+++ b/Headerer/Database.cs
@@ -1,3 +1,5 @@
+using System;
+using System.Collections.Generic;
using System.IO;
using Microsoft.Data.Sqlite;
using SabreTools.IO;
@@ -8,22 +10,106 @@ namespace Headerer
{
#region Constants
- public static string HeadererFileName = Path.Combine(PathTool.GetRuntimeDirectory(), "Headerer.sqlite");
- public static string HeadererConnectionString = $"Data Source={HeadererFileName};";
+ ///
+ /// Default location for the database
+ ///
+ private static readonly string DbFileName = Path.Combine(PathTool.GetRuntimeDirectory(), "Headerer.sqlite");
+
+ ///
+ /// Connection string for the database
+ ///
+ private static readonly string DbConnectionString = $"Data Source={DbFileName};";
#endregion
///
- /// Ensure that the database exists and has the proper schema
+ /// Add a header to the database
///
- public static void EnsureDatabase()
+ /// String representing the header bytes
+ /// SHA-1 of the deheadered file
+ /// Name of the source skipper file
+ /// Enable additional log statements for debugging
+ public static void AddHeader(string header, string SHA1, string source, bool debug)
{
- // Make sure the file exists
- if (!File.Exists(HeadererFileName))
- File.Create(HeadererFileName);
+ // Ensure the database exists
+ EnsureDatabase();
// Open the database connection
- SqliteConnection dbc = new(HeadererConnectionString);
+ SqliteConnection dbc = new(DbConnectionString);
+ dbc.Open();
+
+ string query = $"SELECT * FROM data WHERE sha1='{SHA1}' AND header='{header}'";
+ var slc = new SqliteCommand(query, dbc);
+ SqliteDataReader sldr = slc.ExecuteReader();
+ bool exists = sldr.HasRows;
+
+ if (!exists)
+ {
+ query = $"INSERT INTO data (sha1, header, type) VALUES ('{SHA1}', '{header}', '{source}')";
+ slc = new SqliteCommand(query, dbc);
+ if (debug) Console.WriteLine($"Result of inserting header: {slc.ExecuteNonQuery()}");
+ }
+
+ // Dispose of database objects
+ slc.Dispose();
+ sldr.Dispose();
+ dbc.Dispose();
+ }
+
+ ///
+ /// Retrieve headers from the database
+ ///
+ /// SHA-1 of the deheadered file
+ /// Enable additional log statements for debugging
+ /// List of strings representing the headers to add
+ public static List RetrieveHeaders(string SHA1, bool debug)
+ {
+ // Ensure the database exists
+ EnsureDatabase();
+
+ // Open the database connection
+ var dbc = new SqliteConnection(DbConnectionString);
+ dbc.Open();
+
+ // Create the output list of headers
+ List headers = [];
+
+ string query = $"SELECT header, type FROM data WHERE sha1='{SHA1}'";
+ var slc = new SqliteCommand(query, dbc);
+ SqliteDataReader sldr = slc.ExecuteReader();
+
+ if (sldr.HasRows)
+ {
+ while (sldr.Read())
+ {
+ if (debug) Console.WriteLine($"Found match with rom type '{sldr.GetString(1)}'");
+ headers.Add(sldr.GetString(0));
+ }
+ }
+ else
+ {
+ Console.Error.WriteLine("No matching header could be found!");
+ }
+
+ // Dispose of database objects
+ slc.Dispose();
+ sldr.Dispose();
+ dbc.Dispose();
+
+ return headers;
+ }
+
+ ///
+ /// Ensure that the database exists and has the proper schema
+ ///
+ private static void EnsureDatabase()
+ {
+ // Make sure the file exists
+ if (!File.Exists(DbFileName))
+ File.Create(DbFileName);
+
+ // Open the database connection
+ SqliteConnection dbc = new(DbConnectionString);
dbc.Open();
// Make sure the database has the correct schema
diff --git a/Headerer/Extract.cs b/Headerer/Extract.cs
index 78e44e94..07095b5b 100644
--- a/Headerer/Extract.cs
+++ b/Headerer/Extract.cs
@@ -1,6 +1,5 @@
using System;
using System.IO;
-using Microsoft.Data.Sqlite;
using SabreTools.Hashing;
using SabreTools.IO.Extensions;
using SabreTools.Skippers;
@@ -63,44 +62,10 @@ namespace Headerer
if (!nostore)
{
string sha1 = HashTool.GetFileHash(newfile, HashType.SHA1) ?? string.Empty;
- AddHeaderToDatabase(hstr, sha1, rule.SourceFile!, debug);
+ Database.AddHeader(hstr, sha1, rule.SourceFile!, debug);
}
return true;
}
-
- ///
- /// Add a header to the database
- ///
- /// String representing the header bytes
- /// SHA-1 of the deheadered file
- /// Name of the source skipper file
- /// Enable additional log statements for debugging
- private static void AddHeaderToDatabase(string header, string SHA1, string source, bool debug)
- {
- // Ensure the database exists
- Database.EnsureDatabase();
-
- // Open the database connection
- SqliteConnection dbc = new(Database.HeadererConnectionString);
- dbc.Open();
-
- string query = $"SELECT * FROM data WHERE sha1='{SHA1}' AND header='{header}'";
- var slc = new SqliteCommand(query, dbc);
- SqliteDataReader sldr = slc.ExecuteReader();
- bool exists = sldr.HasRows;
-
- if (!exists)
- {
- query = $"INSERT INTO data (sha1, header, type) VALUES ('{SHA1}', '{header}', '{source}')";
- slc = new SqliteCommand(query, dbc);
- if (debug) Console.WriteLine($"Result of inserting header: {slc.ExecuteNonQuery()}");
- }
-
- // Dispose of database objects
- slc.Dispose();
- sldr.Dispose();
- dbc.Dispose();
- }
}
}
diff --git a/Headerer/Restore.cs b/Headerer/Restore.cs
index 809b1110..6951f9ba 100644
--- a/Headerer/Restore.cs
+++ b/Headerer/Restore.cs
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
-using Microsoft.Data.Sqlite;
using SabreTools.Hashing;
using SabreTools.IO.Extensions;
@@ -26,7 +25,7 @@ namespace Headerer
string sha1 = HashTool.GetFileHash(file, HashType.SHA1) ?? string.Empty;
// Retrieve a list of all related headers from the database
- List headers = RetrieveHeadersFromDatabase(sha1, debug);
+ List headers = Database.RetrieveHeaders(sha1, debug);
// If we have nothing retrieved, we return false
if (headers.Count == 0)
@@ -44,49 +43,6 @@ namespace Headerer
return true;
}
- ///
- /// Retrieve headers from the database
- ///
- /// SHA-1 of the deheadered file
- /// Enable additional log statements for debugging
- /// List of strings representing the headers to add
- private static List RetrieveHeadersFromDatabase(string SHA1, bool debug)
- {
- // Ensure the database exists
- Database.EnsureDatabase();
-
- // Open the database connection
- var dbc = new SqliteConnection(Database.HeadererConnectionString);
- dbc.Open();
-
- // Create the output list of headers
- List headers = [];
-
- string query = $"SELECT header, type FROM data WHERE sha1='{SHA1}'";
- var slc = new SqliteCommand(query, dbc);
- SqliteDataReader sldr = slc.ExecuteReader();
-
- if (sldr.HasRows)
- {
- while (sldr.Read())
- {
- if (debug) Console.WriteLine($"Found match with rom type '{sldr.GetString(1)}'");
- headers.Add(sldr.GetString(0));
- }
- }
- else
- {
- Console.Error.WriteLine("No matching header could be found!");
- }
-
- // Dispose of database objects
- slc.Dispose();
- sldr.Dispose();
- dbc.Dispose();
-
- return headers;
- }
-
///
/// Add an aribtrary number of bytes to the inputted file
///