forked from HalcyonGrid/halcyon-setuptools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish up work on initial schema creation
- Loading branch information
1 parent
95be174
commit ffed395
Showing
5 changed files
with
2,046 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.