2020-06-10 22:37:19 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
2016-10-31 14:26:23 -07:00
|
|
|
|
using System.Collections.Generic;
|
2016-10-24 13:51:39 -07:00
|
|
|
|
|
2017-05-04 02:41:11 -07:00
|
|
|
|
using SabreTools.Library.Data;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
using Microsoft.Data.Sqlite;
|
2016-10-26 22:10:47 -07:00
|
|
|
|
|
2017-05-04 02:41:11 -07:00
|
|
|
|
namespace SabreTools.Library.Tools
|
2016-03-24 16:17:33 -07:00
|
|
|
|
{
|
2019-02-08 21:03:28 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// All general database operations
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class DatabaseTools
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Add a header to the database
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="header">String representing the header bytes</param>
|
|
|
|
|
|
/// <param name="SHA1">SHA-1 of the deheadered file</param>
|
|
|
|
|
|
/// <param name="type">Name of the source skipper file</param>
|
|
|
|
|
|
public static void AddHeaderToDatabase(string header, string SHA1, string source)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Ensure the database exists
|
|
|
|
|
|
EnsureDatabase(Constants.HeadererDbSchema, Constants.HeadererFileName, Constants.HeadererConnectionString);
|
|
|
|
|
|
|
|
|
|
|
|
// Open the database connection
|
|
|
|
|
|
SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString);
|
|
|
|
|
|
dbc.Open();
|
|
|
|
|
|
|
2020-06-10 22:37:19 -07:00
|
|
|
|
string query = $"SELECT * FROM data WHERE sha1='{SHA1}' AND header='{header}'";
|
2019-02-08 21:03:28 -08:00
|
|
|
|
SqliteCommand slc = new SqliteCommand(query, dbc);
|
|
|
|
|
|
SqliteDataReader sldr = slc.ExecuteReader();
|
2020-07-15 09:41:59 -07:00
|
|
|
|
bool exists = sldr.HasRows;
|
2019-02-08 21:03:28 -08:00
|
|
|
|
|
|
|
|
|
|
if (!exists)
|
|
|
|
|
|
{
|
2020-06-10 22:37:19 -07:00
|
|
|
|
query = $"INSERT INTO data (sha1, header, type) VALUES ('{SHA1}', '{header}', '{source}')";
|
2019-02-08 21:03:28 -08:00
|
|
|
|
slc = new SqliteCommand(query, dbc);
|
2020-06-10 22:37:19 -07:00
|
|
|
|
Globals.Logger.Verbose($"Result of inserting header: {slc.ExecuteNonQuery()}");
|
2019-02-08 21:03:28 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Dispose of database objects
|
|
|
|
|
|
slc.Dispose();
|
|
|
|
|
|
sldr.Dispose();
|
|
|
|
|
|
dbc.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Ensure that the databse exists and has the proper schema
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="type">Schema type to use</param>
|
|
|
|
|
|
/// <param name="db">Name of the databse</param>
|
|
|
|
|
|
/// <param name="connectionString">Connection string for SQLite</param>
|
|
|
|
|
|
public static void EnsureDatabase(string type, string db, string connectionString)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Set the type to lowercase
|
|
|
|
|
|
type = type.ToLowerInvariant();
|
|
|
|
|
|
|
|
|
|
|
|
// Make sure the file exists
|
|
|
|
|
|
if (!File.Exists(db))
|
2020-07-15 09:41:59 -07:00
|
|
|
|
File.Create(db);
|
2019-02-08 21:03:28 -08:00
|
|
|
|
|
|
|
|
|
|
// Open the database connection
|
|
|
|
|
|
SqliteConnection dbc = new SqliteConnection(connectionString);
|
|
|
|
|
|
dbc.Open();
|
|
|
|
|
|
|
|
|
|
|
|
// Make sure the database has the correct schema
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (type == "rombasharp")
|
|
|
|
|
|
{
|
|
|
|
|
|
string query = @"
|
2016-10-14 14:04:15 -07:00
|
|
|
|
CREATE TABLE IF NOT EXISTS crc (
|
2019-02-08 21:03:28 -08:00
|
|
|
|
'crc' TEXT NOT NULL,
|
|
|
|
|
|
PRIMARY KEY (crc)
|
2016-09-01 23:17:09 -07:00
|
|
|
|
)";
|
2019-02-08 21:03:28 -08:00
|
|
|
|
SqliteCommand slc = new SqliteCommand(query, dbc);
|
|
|
|
|
|
slc.ExecuteNonQuery();
|
2016-10-10 10:51:19 -07:00
|
|
|
|
|
2019-02-08 21:03:28 -08:00
|
|
|
|
query = @"
|
2016-10-14 14:04:15 -07:00
|
|
|
|
CREATE TABLE IF NOT EXISTS md5 (
|
2019-02-08 21:03:28 -08:00
|
|
|
|
'md5' TEXT NOT NULL,
|
|
|
|
|
|
PRIMARY KEY (md5)
|
2016-10-14 14:04:15 -07:00
|
|
|
|
)";
|
2019-02-08 21:03:28 -08:00
|
|
|
|
slc = new SqliteCommand(query, dbc);
|
|
|
|
|
|
slc.ExecuteNonQuery();
|
2016-10-14 14:04:15 -07:00
|
|
|
|
|
2019-02-08 21:03:28 -08:00
|
|
|
|
query = @"
|
2016-10-14 14:04:15 -07:00
|
|
|
|
CREATE TABLE IF NOT EXISTS sha1 (
|
2019-02-08 21:03:28 -08:00
|
|
|
|
'sha1' TEXT NOT NULL,
|
|
|
|
|
|
'depot' TEXT,
|
|
|
|
|
|
PRIMARY KEY (sha1)
|
2016-10-14 14:04:15 -07:00
|
|
|
|
)";
|
2019-02-08 21:03:28 -08:00
|
|
|
|
slc = new SqliteCommand(query, dbc);
|
|
|
|
|
|
slc.ExecuteNonQuery();
|
2016-10-14 14:04:15 -07:00
|
|
|
|
|
2019-02-08 21:03:28 -08:00
|
|
|
|
query = @"
|
2016-10-14 14:04:15 -07:00
|
|
|
|
CREATE TABLE IF NOT EXISTS crcsha1 (
|
2019-02-08 21:03:28 -08:00
|
|
|
|
'crc' TEXT NOT NULL,
|
|
|
|
|
|
'sha1' TEXT NOT NULL,
|
|
|
|
|
|
PRIMARY KEY (crc, sha1)
|
2016-10-14 14:04:15 -07:00
|
|
|
|
)";
|
2019-02-08 21:03:28 -08:00
|
|
|
|
slc = new SqliteCommand(query, dbc);
|
|
|
|
|
|
slc.ExecuteNonQuery();
|
2016-10-14 14:04:15 -07:00
|
|
|
|
|
2019-02-08 21:03:28 -08:00
|
|
|
|
query = @"
|
2016-10-14 14:04:15 -07:00
|
|
|
|
CREATE TABLE IF NOT EXISTS md5sha1 (
|
2019-02-08 21:03:28 -08:00
|
|
|
|
'md5' TEXT NOT NULL,
|
|
|
|
|
|
'sha1' TEXT NOT NULL,
|
|
|
|
|
|
PRIMARY KEY (md5, sha1)
|
2016-10-14 14:04:15 -07:00
|
|
|
|
)";
|
2019-02-08 21:03:28 -08:00
|
|
|
|
slc = new SqliteCommand(query, dbc);
|
|
|
|
|
|
slc.ExecuteNonQuery();
|
2016-10-14 14:04:15 -07:00
|
|
|
|
|
2019-02-08 21:03:28 -08:00
|
|
|
|
query = @"
|
2016-10-14 14:04:15 -07:00
|
|
|
|
CREATE TABLE IF NOT EXISTS dat (
|
2019-02-08 21:03:28 -08:00
|
|
|
|
'hash' TEXT NOT NULL,
|
|
|
|
|
|
PRIMARY KEY (hash)
|
2016-10-10 10:51:19 -07:00
|
|
|
|
)";
|
2019-02-08 21:03:28 -08:00
|
|
|
|
slc = new SqliteCommand(query, dbc);
|
|
|
|
|
|
slc.ExecuteNonQuery();
|
|
|
|
|
|
slc.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == "headerer")
|
|
|
|
|
|
{
|
|
|
|
|
|
string query = @"
|
2016-04-06 00:26:13 -07:00
|
|
|
|
CREATE TABLE IF NOT EXISTS data (
|
2019-02-08 21:03:28 -08:00
|
|
|
|
'sha1' TEXT NOT NULL,
|
|
|
|
|
|
'header' TEXT NOT NULL,
|
|
|
|
|
|
'type' TEXT NOT NULL,
|
|
|
|
|
|
PRIMARY KEY (sha1, header, type)
|
2016-04-06 00:26:13 -07:00
|
|
|
|
)";
|
2019-02-08 21:03:28 -08:00
|
|
|
|
SqliteCommand slc = new SqliteCommand(query, dbc);
|
|
|
|
|
|
slc.ExecuteNonQuery();
|
|
|
|
|
|
slc.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine(ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
dbc.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Retrieve headers from the database
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="SHA1">SHA-1 of the deheadered file</param>
|
|
|
|
|
|
/// <returns>List of strings representing the headers to add</returns>
|
|
|
|
|
|
public static List<string> RetrieveHeadersFromDatabase(string SHA1)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Ensure the database exists
|
|
|
|
|
|
EnsureDatabase(Constants.HeadererDbSchema, Constants.HeadererFileName, Constants.HeadererConnectionString);
|
|
|
|
|
|
|
|
|
|
|
|
// Open the database connection
|
|
|
|
|
|
SqliteConnection dbc = new SqliteConnection(Constants.HeadererConnectionString);
|
|
|
|
|
|
dbc.Open();
|
|
|
|
|
|
|
|
|
|
|
|
// Create the output list of headers
|
|
|
|
|
|
List<string> headers = new List<string>();
|
|
|
|
|
|
|
2020-06-10 22:37:19 -07:00
|
|
|
|
string query = $"SELECT header, type FROM data WHERE sha1='{SHA1}'";
|
2019-02-08 21:03:28 -08:00
|
|
|
|
SqliteCommand slc = new SqliteCommand(query, dbc);
|
|
|
|
|
|
SqliteDataReader sldr = slc.ExecuteReader();
|
|
|
|
|
|
|
|
|
|
|
|
if (sldr.HasRows)
|
|
|
|
|
|
{
|
|
|
|
|
|
while (sldr.Read())
|
|
|
|
|
|
{
|
2020-06-10 22:37:19 -07:00
|
|
|
|
Globals.Logger.Verbose($"Found match with rom type '{sldr.GetString(1)}'");
|
2019-02-08 21:03:28 -08:00
|
|
|
|
headers.Add(sldr.GetString(0));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Warning("No matching header could be found!");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Dispose of database objects
|
|
|
|
|
|
slc.Dispose();
|
|
|
|
|
|
sldr.Dispose();
|
|
|
|
|
|
dbc.Dispose();
|
|
|
|
|
|
|
|
|
|
|
|
return headers;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-03-24 16:17:33 -07:00
|
|
|
|
}
|