Skip to content

Commit

Permalink
Finish up work on initial schema creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ddaeschler committed Sep 28, 2015
1 parent 95be174 commit ffed395
Show file tree
Hide file tree
Showing 5 changed files with 2,046 additions and 21 deletions.
100 changes: 79 additions & 21 deletions HalcyonSetuptools/hc-database/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,47 @@ class Program
static private string dbHost;
static private string dbUser;
static private string dbPass;
static private string dbSchema;

private const string CORE_DEFAULT_SCHEMA_NAME = "inworldz";
private const string RDB_DEFAULT_SCHEMA_NAME = "inworldz_rdb";

private const string CORE_SCHEMA_BASE_FILE = "inworldz-core-base.sql";
private const string RDB_SCHEMA_BASE_FILE = "inworldz-rdb-base.sql";


static private OptionSet options = new OptionSet()
{
{ "init", "Initializes a new halcyon database", v => op = DatabaseOperation.Init },
{ "upgrade", "Upgrades halcyon database", v => op = DatabaseOperation.Upgrade },
{ "t|type=", "Specifies the halcyon database type (core, rdb)", v => dbType = v },
{ "h|host=", "Specifies the database hostname", v => dbHost = v },
{ "u|user=", "Specifies the database username", v => dbUser = v },
{ "p|password=", "Specifies the database password", v => dbPass = v },
{ "?|help", "Prints this help message", v => help = v != null },
{ "init", "Initializes a new halcyon database", v => op = DatabaseOperation.Init },
{ "upgrade", "Upgrades halcyon database", v => op = DatabaseOperation.Upgrade },
{ "t|type=", "Specifies the halcyon database type (core, rdb)", v => dbType = v.ToLower() },
{ "h|host=", "Specifies the database hostname", v => dbHost = v },
{ "u|user=", "Specifies the database username", v => dbUser = v },
{ "p|password=", "Specifies the database password", v => dbPass = v },
{ "s|schema=", "Specifies the name of the database schema", v => dbSchema = v },
{ "?|help", "Prints this help message", v => help = v != null },
};

static void Main(string[] args)
static int Main(string[] args)
{
List<string> extra = options.Parse(args);

if (help || op == DatabaseOperation.None)
{
PrintUsage();
return;
return 1;
}

//allow blank passwords
if (dbPass == null) dbPass = String.Empty;

switch (op)
{
case DatabaseOperation.Init:
DoInit();
break;
return DoInit();
}

return 1;
}

private static void PrintUsage()
Expand All @@ -51,28 +64,35 @@ private static void PrintUsage()
options.WriteOptionDescriptions(Console.Out);
}

private static void DoInit()
private static int DoInit()
{
if (String.IsNullOrEmpty(dbType))
{
PrintUsage();
return;
return 1;
}

switch (dbType.ToLower())
switch (dbType)
{
case "core":
DoInitCore();
break;
return DoInitCore();
}

Console.Error.WriteLine("Unknown database type: " + dbType);
return 1;
}

private static void DoInitCore()
private static int DoInitCore()
{
if (String.IsNullOrEmpty(dbSchema))
{
dbSchema = CORE_DEFAULT_SCHEMA_NAME;
}

if (! VerifyDatabaseParameters())
{
PrintUsage();
return;
return 1;
}

MySqlConnection conn = new MySqlConnection(
Expand All @@ -86,10 +106,48 @@ private static void DoInitCore()
catch (MySqlException e)
{
Console.Error.WriteLine("Unable to connect to the database: {0}", e.Message);
return;
return 1;
}

//create the schema if it is missing
try
{
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = String.Format("CREATE DATABASE IF NOT EXISTS {0};", dbSchema);
cmd.ExecuteNonQuery();
}
catch (MySqlException e)
{
Console.Error.WriteLine("Unable to create database schema: {0}", e.Message);
return 1;
}

//for the core database, we'll actually open up and run both the
//core file and the RDB file. that way, a user can run both from
//the same database
try
{
SqlFileRunner coreRunner = new SqlFileRunner(conn, dbSchema, CORE_SCHEMA_BASE_FILE);
coreRunner.Run();
}
catch (Exception e)
{
Console.Error.WriteLine("Unable to load CORE schema: {0}", e.Message);
return 1;
}

try
{
SqlFileRunner rdbRunner = new SqlFileRunner(conn, dbSchema, RDB_DEFAULT_SCHEMA_NAME);
rdbRunner.Run();
}
catch (Exception e)
{
Console.Error.WriteLine("Unable to load RDB schema: {0}", e.Message);
return 1;
}


return 0;
}

/// <summary>
Expand All @@ -99,7 +157,7 @@ private static void DoInitCore()
private static bool VerifyDatabaseParameters()
{
if (String.IsNullOrEmpty(dbHost) || String.IsNullOrEmpty(dbUser)
|| String.IsNullOrEmpty(dbPass))
|| String.IsNullOrEmpty(dbSchema))
{
return false;
}
Expand Down
70 changes: 70 additions & 0 deletions HalcyonSetuptools/hc-database/SqlFileRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.IO;

namespace hc_database
{
/// <summary>
/// Class that runs an SQL structure dump one statement at a time
/// </summary>
public class SqlFileRunner
{
private string[] _commands;
private MySqlConnection _conn;

/// <summary>
/// Constructs a new runner that will execute the given file
/// </summary>
/// <param name="conn">The database connection to execute the commands against</param>
/// <param name="filePath">The path of the SQL dump that contains the commands to run</param>
public SqlFileRunner(MySqlConnection conn, string schema, string filePath)
{
_conn = conn;
_conn.ChangeDatabase(schema);

//try to open the file
string[] lines = File.ReadAllLines(filePath);
List<string> filteredLines = new List<string>(lines.Length);

//filter commented lines
foreach (var line in lines)
{
if (! line.StartsWith("--"))
{
filteredLines.Add(line);
}
}

//join and then split by semicolon which should be one statement
string joined = String.Join("", filteredLines);
_commands = joined.Split(new char[] { ';' });
}

/// <summary>
/// Runs the loaded file
/// </summary>
public void Run()
{
foreach (var command in _commands)
{
var cmd = _conn.CreateCommand();
cmd.CommandText = command;

try
{
cmd.ExecuteNonQuery();
}
catch (MySqlException e)
{
throw new Exception(String.Format("Error {0}\nWhile executing query {1}",
e.Message, command), e);
}

}
}
}
}
1 change: 1 addition & 0 deletions HalcyonSetuptools/hc-database/hc-database.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<Compile Include="DatabaseOperation.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SqlFileRunner.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
Loading

0 comments on commit ffed395

Please sign in to comment.