using Mono.Data.Sqlite; using System; using System.IO; namespace SabreTools.Helper { /// /// All general database operations /// public class DBTools { /// /// Ensure that the databse exists and has the proper schema /// /// Schema type to use /// Name of the databse /// Connection string for SQLite public static void EnsureDatabase(string type, string db, string connectionString) { // Set the type to lowercase type = type.ToLowerInvariant(); // Make sure the file exists if (!File.Exists(db)) { SqliteConnection.CreateFile(db); } // Connect to the file SqliteConnection dbc = new SqliteConnection(connectionString); dbc.Open(); // Make sure the database has the correct schema try { if (type == "rombasharp") { string query = @" CREATE TABLE IF NOT EXISTS data ( 'id' INTEGER NOT NULL 'key' TEXT NOT NULL 'value' TEXT NOT NULL PRIMARY KEY (id, key, value) )"; SqliteCommand slc = new SqliteCommand(query, dbc); slc.ExecuteNonQuery(); } else if (type == "headerer") { 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); slc.ExecuteNonQuery(); } } catch (Exception ex) { Console.WriteLine(ex); } finally { // Close and return the database connection dbc.Close(); } } /// /// Add a new source to the database if it doesn't already exist /// /// Source name /// Source URL(s) /// Connection string for SQLite /// True if the source existed or could be added, false otherwise public static bool AddSource(string name, string url, string connectionString) { string query = "SELECT id, name, url FROM source WHERE name='" + name + "'"; using (SqliteConnection dbc = new SqliteConnection(connectionString)) { dbc.Open(); using (SqliteCommand slc = new SqliteCommand(query, dbc)) { using (SqliteDataReader sldr = slc.ExecuteReader()) { // If nothing is found, add the source if (!sldr.HasRows) { string squery = "INSERT INTO source (name, url) VALUES ('" + name + "', '" + url + "')"; using (SqliteCommand sslc = new SqliteCommand(squery, dbc)) { return sslc.ExecuteNonQuery() >= 1; } } // Otherwise, update the source URL if it's different else { sldr.Read(); if (url != sldr.GetString(2)) { string squery = "UPDATE source SET url='" + url + "' WHERE id=" + sldr.GetInt32(0); using (SqliteCommand sslc = new SqliteCommand(squery, dbc)) { return sslc.ExecuteNonQuery() >= 1; } } } } } } return true; } /// /// Remove an existing source from the database /// /// Source ID to be removed from the database /// Connection string for SQLite /// True if the source was removed, false otherwise public static bool RemoveSource(int id, string connectionString) { string query = "DELETE FROM source WHERE id=" + id; using (SqliteConnection dbc = new SqliteConnection(connectionString)) { dbc.Open(); using (SqliteCommand slc = new SqliteCommand(query, dbc)) { return slc.ExecuteNonQuery() >= 1; } } } /// /// Add a new system to the database if it doesn't already exist /// /// Manufacturer name /// System name /// Connection string for SQLite /// True if the system existed or could be added, false otherwise public static bool AddSystem(string manufacturer, string system, string connectionString) { string query = "SELECT id, manufacturer, name FROM system WHERE manufacturer='" + manufacturer + "' AND system='" + system + "'"; using (SqliteConnection dbc = new SqliteConnection(connectionString)) { dbc.Open(); using (SqliteCommand slc = new SqliteCommand(query, dbc)) { using (SqliteDataReader sldr = slc.ExecuteReader()) { // If nothing is found, add the system if (!sldr.HasRows) { string squery = "INSERT INTO name (manufacturer, system) VALUES ('" + manufacturer + "', '" + system + "')"; using (SqliteCommand sslc = new SqliteCommand(squery, dbc)) { return sslc.ExecuteNonQuery() >= 1; } } } } } return true; } /// /// Remove an existing system from the database /// /// System ID to be removed from the database /// Connection string for SQLite /// True if the system was removed, false otherwise public static bool RemoveSystem(int id, string connectionString) { string query = "DELETE FROM system WHERE id=" + id; using (SqliteConnection dbc = new SqliteConnection(connectionString)) { dbc.Open(); using (SqliteCommand slc = new SqliteCommand(query, dbc)) { return slc.ExecuteNonQuery() >= 1; } } } } }