2016-06-13 23:54:13 -07:00
|
|
|
|
using Mono.Data.Sqlite;
|
|
|
|
|
|
using System;
|
2016-03-24 16:17:33 -07:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
2016-03-29 13:48:10 -07:00
|
|
|
|
namespace SabreTools.Helper
|
2016-03-24 16:17:33 -07:00
|
|
|
|
{
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// All general database operations
|
|
|
|
|
|
/// </summary>
|
2016-09-22 21:04:41 -07:00
|
|
|
|
public 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>
|
2016-09-22 21:04:41 -07:00
|
|
|
|
/// <param name="logger">Logger object for console and file output</param>
|
2016-09-22 21:32:06 -07:00
|
|
|
|
public static void AddHeaderToDatabase(string header, string SHA1, string source, Logger logger)
|
2016-09-22 21:04:41 -07:00
|
|
|
|
{
|
|
|
|
|
|
bool exists = false;
|
|
|
|
|
|
|
|
|
|
|
|
// 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);
|
2016-09-23 15:09:00 -07:00
|
|
|
|
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 = @"
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS data (
|
2016-10-10 13:14:35 -07:00
|
|
|
|
'id' INTEGER NOT NULL,
|
|
|
|
|
|
'size' INTEGER NOT NULL,
|
|
|
|
|
|
'crc' TEXT NOT NULL,
|
|
|
|
|
|
'md5' TEXT NOT NULL,
|
|
|
|
|
|
'sha1' TEXT NOT NULL,
|
|
|
|
|
|
'indepot' INTEGER NOT NULL,
|
2016-10-10 10:51:19 -07:00
|
|
|
|
PRIMARY KEY (id)
|
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 = @"
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS dats (
|
2016-10-10 13:14:35 -07:00
|
|
|
|
'id' INTEGER NOT NULL,
|
|
|
|
|
|
'hash' TEXT NOT NULL,
|
2016-10-10 10:51:19 -07:00
|
|
|
|
PRIMARY KEY (id, hash)
|
|
|
|
|
|
)";
|
|
|
|
|
|
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-03-24 16:17:33 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|