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