Files
SabreTools/SabreTools.Helper/Tools/DBTools.cs

74 lines
1.6 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-06-13 20:59:38 -07:00
public class DBTools
2016-03-24 16:17:33 -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>
/// <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-09-02 12:51:08 -07:00
'id' INTEGER NOT NULL
'key' TEXT NOT NULL
'value' TEXT NOT NULL
PRIMARY KEY (id, key, value)
2016-09-01 23:17:09 -07:00
)";
SqliteCommand 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
}
}