Files
SabreTools/SabreTools.Helper/Tools/DatabaseTools.cs

205 lines
5.2 KiB
C#
Raw Normal View History

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
using SabreTools.Helper.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
namespace SabreTools.Helper.Tools
2016-03-24 16:17:33 -07:00
{
/// <summary>
/// All general database operations
/// </summary>
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>
/// <param name="type">Name of the source skipper file</param>
2016-09-22 21:04:41 -07:00
/// <param name="logger">Logger object for console and file output</param>
public static void AddHeaderToDatabase(string header, string SHA1, string source, Logger logger)
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 + "', " +
"'" + source + "')";
2016-09-22 21:04:41 -07:00
slc = new SqliteCommand(query, dbc);
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();
}
/// <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>
/// <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
if (!File.Exists(db))
2016-03-24 16:17:33 -07:00
{
SqliteConnection.CreateFile(db);
2016-03-24 16:17:33 -07:00
}
// Open the database connection
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 = @"
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();
query = @"
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,
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,
PRIMARY KEY (hash)
)";
slc = new SqliteCommand(query, dbc);
slc.ExecuteNonQuery();
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)
)";
SqliteCommand slc = new SqliteCommand(query, dbc);
2016-04-06 00:26:13 -07:00
slc.ExecuteNonQuery();
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
{
dbc.Dispose();
2016-03-29 02:25:04 -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>
/// <param name="logger">Logger object for console and file output</param>
/// <returns>List of strings representing the headers to add</returns>
public static List<string> RetrieveHeadersFromDatabase(string SHA1, Logger logger)
{
// 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())
{
logger.Verbose("Found match with rom type " + sldr.GetString(1));
headers.Add(sldr.GetString(0));
}
}
else
{
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
}
}