From 97ee13681bb9e50f6b6938d68c7c7b1c04128e06 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 6 Apr 2016 00:26:13 -0700 Subject: [PATCH] Consolodate DB inits --- Deheader/Headerer.cs | 44 +++----------------------------- SabreHelper/DBTools.cs | 57 ++++++++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 60 deletions(-) diff --git a/Deheader/Headerer.cs b/Deheader/Headerer.cs index 973a5779..ec5de486 100644 --- a/Deheader/Headerer.cs +++ b/Deheader/Headerer.cs @@ -6,6 +6,8 @@ using System.Linq; using System.Security.Cryptography; using System.Text.RegularExpressions; +using SabreTools.Helper; + namespace SabreTools { /// @@ -40,7 +42,7 @@ Options: types.Add("snes", 512); // Ensure that the header database is set up - EnsureDatabase(_dbName, _connectionString); + DBTools.EnsureDatabase(_dbName, _connectionString); if (args.Length == 0 || args.Length > 2) { @@ -275,45 +277,5 @@ Options: } } } - - /// - /// Ensure that the databse exists and has the proper schema - /// - /// Name of the databse - /// Connection string for SQLite - public static void EnsureDatabase(string db, string connectionString) - { - // Make sure the file exists - if (!File.Exists(db)) - { - SQLiteConnection.CreateFile(db); - } - - // Connect to the file - SQLiteConnection dbc = new SQLiteConnection(connectionString); - dbc.Open(); - try - { - // Make sure the database has the correct schema - 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(); - } - } } } diff --git a/SabreHelper/DBTools.cs b/SabreHelper/DBTools.cs index d1c63fff..9d8d18a4 100644 --- a/SabreHelper/DBTools.cs +++ b/SabreHelper/DBTools.cs @@ -22,13 +22,19 @@ namespace SabreTools.Helper SQLiteConnection.CreateFile(db); } + //Get "type" from the filename + string type = Path.GetFileNameWithoutExtension(db); + // Connect to the file SQLiteConnection dbc = new SQLiteConnection(connectionString); dbc.Open(); + + // Make sure the database has the correct schema try { - // Make sure the database has the correct schema - string query = @" + if (type == "DATabase") + { + string query = @" CREATE TABLE IF NOT EXISTS checksums ( 'file' INTEGER NOT NULL, 'size' INTEGER NOT NULL DEFAULT -1, @@ -37,10 +43,10 @@ CREATE TABLE IF NOT EXISTS checksums ( 'sha1' TEXT NOT NULL, PRIMARY KEY (file, size, crc, md5, sha1) )"; - SQLiteCommand slc = new SQLiteCommand(query, dbc); - slc.ExecuteNonQuery(); + SQLiteCommand slc = new SQLiteCommand(query, dbc); + slc.ExecuteNonQuery(); - query = @" + query = @" CREATE TABLE IF NOT EXISTS files ( 'id' INTEGER PRIMARY KEY NOT NULL, 'setid' INTEGER NOT NULL, @@ -48,10 +54,10 @@ CREATE TABLE IF NOT EXISTS files ( 'type' TEXT NOT NULL DEFAULT 'rom', 'lastupdated' TEXT NOT NULL )"; - slc = new SQLiteCommand(query, dbc); - slc.ExecuteNonQuery(); + slc = new SQLiteCommand(query, dbc); + slc.ExecuteNonQuery(); - query = @" + query = @" CREATE TABLE IF NOT EXISTS games ( 'id' INTEGER PRIMARY KEY NOT NULL, 'system' INTEGER NOT NULL, @@ -59,34 +65,47 @@ CREATE TABLE IF NOT EXISTS games ( 'parent' INTEGER NOT NULL DEFAULT '0', 'source' INTEGER NOT NULL DEFAULT '0' )"; - slc = new SQLiteCommand(query, dbc); - slc.ExecuteNonQuery(); + slc = new SQLiteCommand(query, dbc); + slc.ExecuteNonQuery(); - query = @" + query = @" CREATE TABLE IF NOT EXISTS parent ( 'id' INTEGER PRIMARY KEY NOT NULL, 'name' TEXT NOT NULL )"; - slc = new SQLiteCommand(query, dbc); - slc.ExecuteNonQuery(); + slc = new SQLiteCommand(query, dbc); + slc.ExecuteNonQuery(); - query = @" + query = @" CREATE TABLE IF NOT EXISTS sources ( 'id' INTEGER PRIMARY KEY NOT NULL, 'name' TEXT NOT NULL UNIQUE, 'url' TEXT NOT NULL )"; - slc = new SQLiteCommand(query, dbc); - slc.ExecuteNonQuery(); + slc = new SQLiteCommand(query, dbc); + slc.ExecuteNonQuery(); - query = @" + query = @" CREATE TABLE IF NOT EXISTS systems ( 'id' INTEGER PRIMARY KEY NOT NULL, 'manufacturer' TEXT NOT NULL, 'system' TEXT NOT NULL )"; - slc = new SQLiteCommand(query, dbc); - slc.ExecuteNonQuery(); + 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) {