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

122 lines
3.1 KiB
C#
Raw Normal View History

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;
namespace SabreTools.Helper
2016-03-24 16:17:33 -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>
/// <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;
// 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 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,
PRIMARY KEY (id)
2016-09-01 23:17:09 -07:00
)";
SqliteCommand slc = new SqliteCommand(query, dbc);
slc.ExecuteNonQuery();
query = @"
CREATE TABLE IF NOT EXISTS dats (
2016-10-10 13:14:35 -07:00
'id' INTEGER NOT NULL,
'hash' TEXT NOT NULL,
PRIMARY KEY (id, 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-03-24 16:17:33 -07:00
}
}