mirror of
https://github.com/sebastian-heinz/mhf-server.git
synced 2025-04-03 13:28:30 +08:00
88 lines
2.6 KiB
C#
88 lines
2.6 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using Arrowgene.Services.Logging;
|
|
|
|
namespace Mhf.Server.Database.Sql
|
|
{
|
|
public class ScriptRunner
|
|
{
|
|
private const string DEFAULT_DELIMITER = ";";
|
|
|
|
private readonly ILogger _logger;
|
|
private IDatabase _database;
|
|
private string delimiter = DEFAULT_DELIMITER;
|
|
private bool fullLineDelimiter = false;
|
|
|
|
/**
|
|
* Default constructor
|
|
*/
|
|
public ScriptRunner(IDatabase database)
|
|
{
|
|
_database = database;
|
|
_logger = LogProvider.Logger(this);
|
|
}
|
|
|
|
public void Run(string path)
|
|
{
|
|
int index = 0;
|
|
try
|
|
{
|
|
string[] file = File.ReadAllLines(path);
|
|
StringBuilder command = null;
|
|
for (; index < file.Length; index++)
|
|
{
|
|
string line = file[index];
|
|
if (command == null)
|
|
{
|
|
command = new StringBuilder();
|
|
}
|
|
|
|
string trimmedLine = line.Trim();
|
|
|
|
if (trimmedLine.Length < 1)
|
|
{
|
|
// Do nothing
|
|
}
|
|
else if (trimmedLine.StartsWith("//") || trimmedLine.StartsWith("--"))
|
|
{
|
|
// Print comment
|
|
}
|
|
else if (!fullLineDelimiter && trimmedLine.EndsWith(delimiter)
|
|
|| fullLineDelimiter && trimmedLine == delimiter)
|
|
{
|
|
command.Append(
|
|
line.Substring(0, line.LastIndexOf(delimiter, StringComparison.InvariantCulture)));
|
|
command.Append(" ");
|
|
_database.Execute(command.ToString());
|
|
command = null;
|
|
}
|
|
else
|
|
{
|
|
command.Append(line);
|
|
command.Append("\n");
|
|
}
|
|
}
|
|
|
|
if (command != null)
|
|
{
|
|
string cmd = command.ToString();
|
|
if (string.IsNullOrWhiteSpace(cmd))
|
|
{
|
|
//do nothing;
|
|
}
|
|
else
|
|
{
|
|
_database.Execute(cmd);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
_logger.Error($"Sql error at Line: {index}");
|
|
_logger.Exception(exception);
|
|
}
|
|
}
|
|
}
|
|
}
|