mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Abstract database init to own class
This commit is contained in:
@@ -69,6 +69,7 @@
|
|||||||
<Compile Include="Helper\Converters.cs" />
|
<Compile Include="Helper\Converters.cs" />
|
||||||
<Compile Include="Core\Generate.cs" />
|
<Compile Include="Core\Generate.cs" />
|
||||||
<Compile Include="Core\Import.cs" />
|
<Compile Include="Core\Import.cs" />
|
||||||
|
<Compile Include="Helper\Database.cs" />
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Helper\Remapping.cs" />
|
<Compile Include="Helper\Remapping.cs" />
|
||||||
|
|||||||
94
DATabase/Helper/Database.cs
Normal file
94
DATabase/Helper/Database.cs
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
using System;
|
||||||
|
using System.Data.SQLite;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace DATabase.Helper
|
||||||
|
{
|
||||||
|
class Database
|
||||||
|
{
|
||||||
|
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 checksums (
|
||||||
|
'file' INTEGER NOT NULL,
|
||||||
|
'size' INTEGER NOT NULL DEFAULT -1,
|
||||||
|
'crc' TEXT NOT NULL,
|
||||||
|
'md5' TEXT NOT NULL,
|
||||||
|
'sha1' TEXT NOT NULL,
|
||||||
|
PRIMARY KEY (file, size, crc, md5, sha1)
|
||||||
|
)";
|
||||||
|
SQLiteCommand slc = new SQLiteCommand(query, dbc);
|
||||||
|
slc.ExecuteNonQuery();
|
||||||
|
|
||||||
|
query = @"
|
||||||
|
CREATE TABLE IF NOT EXISTS files (
|
||||||
|
'id' INTEGER PRIMARY KEY NOT NULL,
|
||||||
|
'setid' INTEGER NOT NULL,
|
||||||
|
'name' TEXT NOT NULL,
|
||||||
|
'type' TEXT NOT NULL DEFAULT 'rom',
|
||||||
|
'lastupdated' TEXT NOT NULL
|
||||||
|
)";
|
||||||
|
slc = new SQLiteCommand(query, dbc);
|
||||||
|
slc.ExecuteNonQuery();
|
||||||
|
|
||||||
|
query = @"
|
||||||
|
CREATE TABLE IF NOT EXISTS games (
|
||||||
|
'id' INTEGER PRIMARY KEY NOT NULL,
|
||||||
|
'system' INTEGER NOT NULL,
|
||||||
|
'name' TEXT NOT NULL,
|
||||||
|
'parent' INTEGER NOT NULL DEFAULT '0',
|
||||||
|
'source' INTEGER NOT NULL DEFAULT '0'
|
||||||
|
)";
|
||||||
|
slc = new SQLiteCommand(query, dbc);
|
||||||
|
slc.ExecuteNonQuery();
|
||||||
|
|
||||||
|
query = @"
|
||||||
|
CREATE TABLE IF NOT EXISTS parent (
|
||||||
|
'id' INTEGER PRIMARY KEY NOT NULL,
|
||||||
|
'name' TEXT NOT NULL
|
||||||
|
)";
|
||||||
|
slc = new SQLiteCommand(query, dbc);
|
||||||
|
slc.ExecuteNonQuery();
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
// Close and return the database connection
|
||||||
|
dbc.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,14 +9,13 @@ namespace DATabase
|
|||||||
{
|
{
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
private static string _connectionString = "Data Source=DATabase.sqlite;Version = 3;";
|
private static string _dbName = "DATabase.sqlite";
|
||||||
|
private static string _connectionString = "Data Source=" + _dbName + ";Version = 3;";
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
// Ensure the database is set up properly
|
// Perform initial setup and verification
|
||||||
EnsureDatabase();
|
Database.EnsureDatabase(_dbName, _connectionString);
|
||||||
|
|
||||||
// Make sure all mappings are created and ready to be used
|
|
||||||
Remapping.CreateRemappings();
|
Remapping.CreateRemappings();
|
||||||
|
|
||||||
// If there's not enough arguments, show the help screen
|
// If there's not enough arguments, show the help screen
|
||||||
@@ -190,91 +189,6 @@ ORDER BY systems.manufacturer, systems.system";
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void EnsureDatabase()
|
|
||||||
{
|
|
||||||
// Make sure the file exists
|
|
||||||
if (!File.Exists("DATabase.sqlite"))
|
|
||||||
{
|
|
||||||
SQLiteConnection.CreateFile("DATabase.sqlite");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 checksums (
|
|
||||||
'file' INTEGER NOT NULL,
|
|
||||||
'size' INTEGER NOT NULL DEFAULT -1,
|
|
||||||
'crc' TEXT NOT NULL,
|
|
||||||
'md5' TEXT NOT NULL,
|
|
||||||
'sha1' TEXT NOT NULL,
|
|
||||||
PRIMARY KEY (file, size, crc, md5, sha1)
|
|
||||||
)";
|
|
||||||
SQLiteCommand slc = new SQLiteCommand(query, dbc);
|
|
||||||
slc.ExecuteNonQuery();
|
|
||||||
|
|
||||||
query = @"
|
|
||||||
CREATE TABLE IF NOT EXISTS files (
|
|
||||||
'id' INTEGER PRIMARY KEY NOT NULL,
|
|
||||||
'setid' INTEGER NOT NULL,
|
|
||||||
'name' TEXT NOT NULL,
|
|
||||||
'type' TEXT NOT NULL DEFAULT 'rom',
|
|
||||||
'lastupdated' TEXT NOT NULL
|
|
||||||
)";
|
|
||||||
slc = new SQLiteCommand(query, dbc);
|
|
||||||
slc.ExecuteNonQuery();
|
|
||||||
|
|
||||||
query = @"
|
|
||||||
CREATE TABLE IF NOT EXISTS games (
|
|
||||||
'id' INTEGER PRIMARY KEY NOT NULL,
|
|
||||||
'system' INTEGER NOT NULL,
|
|
||||||
'name' TEXT NOT NULL,
|
|
||||||
'parent' INTEGER NOT NULL DEFAULT '0',
|
|
||||||
'source' INTEGER NOT NULL DEFAULT '0'
|
|
||||||
)";
|
|
||||||
slc = new SQLiteCommand(query, dbc);
|
|
||||||
slc.ExecuteNonQuery();
|
|
||||||
|
|
||||||
query = @"
|
|
||||||
CREATE TABLE IF NOT EXISTS parent (
|
|
||||||
'id' INTEGER PRIMARY KEY NOT NULL,
|
|
||||||
'name' TEXT NOT NULL
|
|
||||||
)";
|
|
||||||
slc = new SQLiteCommand(query, dbc);
|
|
||||||
slc.ExecuteNonQuery();
|
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine(ex);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
// Close and return the database connection
|
|
||||||
dbc.Close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void Help ()
|
private static void Help ()
|
||||||
{
|
{
|
||||||
Console.Write(@"
|
Console.Write(@"
|
||||||
|
|||||||
Reference in New Issue
Block a user