2015-04-15 18:32:56 +00:00
|
|
|
///
|
2020-03-02 09:42:27 +00:00
|
|
|
/// Copyright (c) 2013-2020 Sensus Slovensko a.s.
|
2015-04-15 18:32:56 +00:00
|
|
|
///
|
2014-07-28 07:54:35 +00:00
|
|
|
using System;
|
2019-08-27 09:00:30 +00:00
|
|
|
using System.Drawing;
|
2020-03-02 09:42:27 +00:00
|
|
|
using System.IO;
|
2019-08-27 09:00:30 +00:00
|
|
|
using System.Windows.Forms;
|
2019-07-25 15:01:51 +00:00
|
|
|
using log4net;
|
2021-09-23 09:46:40 +00:00
|
|
|
using Common;
|
2015-12-31 23:00:03 +00:00
|
|
|
using TBF.Resources;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2019-09-16 08:51:25 +00:00
|
|
|
namespace TBF.UI.Settings
|
2014-07-28 07:54:35 +00:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Dialog to process test bench name and database settings (BenchSettings)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class DatabaseSettingsDlg : Form
|
|
|
|
|
{
|
2019-07-25 15:01:51 +00:00
|
|
|
static readonly ILog log = LogManager.GetLogger(typeof(DatabaseSettingsDlg));
|
|
|
|
|
|
2020-03-02 09:42:27 +00:00
|
|
|
const string CredFileName = "tempfile";
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
// Reference to bench DB settins (not a local copy)
|
2022-01-23 18:56:15 +00:00
|
|
|
DatabaseSettings bench;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2019-08-27 09:00:30 +00:00
|
|
|
string currentConfigDBConnStr; /// Initialized in constructor by the parent
|
|
|
|
|
string currentResultsDBConnStr; /// Initialized in constructor by the parent
|
2020-03-05 12:59:43 +00:00
|
|
|
string currentEventsDBConnStr; /// Initialized in constructor by the parent
|
2019-08-27 09:00:30 +00:00
|
|
|
public bool CurrentDBChanged; /// Set when a current database is modified (erased or imported)
|
|
|
|
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bench">Test bench database settings</param>
|
2022-01-23 18:56:15 +00:00
|
|
|
public DatabaseSettingsDlg(DatabaseSettings bench, string currentConfigDBConnStr, string currentResultsDBConnStr, string currentEventsDBConnStr)
|
2014-07-28 07:54:35 +00:00
|
|
|
{
|
|
|
|
|
this.bench = bench;
|
2019-08-27 09:00:30 +00:00
|
|
|
this.currentConfigDBConnStr = currentConfigDBConnStr;
|
|
|
|
|
this.currentResultsDBConnStr = currentResultsDBConnStr;
|
2020-03-05 12:59:43 +00:00
|
|
|
this.currentEventsDBConnStr = currentEventsDBConnStr;
|
2019-08-27 09:00:30 +00:00
|
|
|
CurrentDBChanged = false;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Localize()
|
|
|
|
|
{
|
|
|
|
|
Text = Strings.Test_Bench_Databases_Specification;
|
|
|
|
|
label1.Text = Strings.Test_bench_name;
|
|
|
|
|
realBenchCheckBox.Text = Strings.Test_bench_is_controlled_by_this_computer;
|
|
|
|
|
dbTypeLabel.Text = Strings.Database_type;
|
|
|
|
|
connectionStringLabel.Text = Strings.Connection_string;
|
2016-12-19 13:39:28 +00:00
|
|
|
|
|
|
|
|
configLabel.Text = Strings.Configuration;
|
|
|
|
|
resultsLabel.Text = Strings.Results;
|
|
|
|
|
usersLabel.Text = Strings.Users;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2015-12-31 21:48:22 +00:00
|
|
|
testConnConfigDbBtn.Text = Strings.Test_connection;
|
|
|
|
|
testConnResultsDbBtn.Text = Strings.Test_connection;
|
2020-03-05 12:59:43 +00:00
|
|
|
testConnEventsDbBtn.Text = Strings.Test_connection;
|
2016-12-19 13:39:28 +00:00
|
|
|
testConnUsersDbBtn.Text = Strings.Test_connection;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2015-12-31 21:48:22 +00:00
|
|
|
createConfigDbBtn.Text = Strings.Create_DB;
|
|
|
|
|
createResultsDbBtn.Text = Strings.Create_DB;
|
2020-03-05 12:59:43 +00:00
|
|
|
createEventsDbBtn.Text = Strings.Create_DB;
|
|
|
|
|
|
|
|
|
|
importConfigDbBtn.Text = Strings.Import_DB;
|
|
|
|
|
importResultsDbBtn.Text = Strings.Import_DB;
|
|
|
|
|
importEventsDbBtn.Text = Strings.Import_DB;
|
2016-12-19 13:39:28 +00:00
|
|
|
|
|
|
|
|
okButton.Text = Strings.OkBtnText;
|
|
|
|
|
cancelButton.Text = Strings.CancelBtnText;
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
private void BenchSettingsDlg_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Localize();
|
|
|
|
|
|
|
|
|
|
benchNameTextBox.Text = bench.BenchName;
|
|
|
|
|
realBenchCheckBox.Checked = bench.IsRealBench;
|
|
|
|
|
|
|
|
|
|
/// Initialize 'Database type' combo-boxes
|
2022-01-23 18:56:15 +00:00
|
|
|
for (int i = 0; i < (int)DBType.Count; i++)
|
2014-07-28 07:54:35 +00:00
|
|
|
{
|
2022-01-23 18:56:15 +00:00
|
|
|
DBType dbType = (DBType)i;
|
2015-12-31 21:48:22 +00:00
|
|
|
configDbTypeComboBox.Items.Add(dbType);
|
|
|
|
|
resultsDbTypeComboBox.Items.Add(dbType);
|
2020-03-05 12:59:43 +00:00
|
|
|
eventsDbTypeComboBox.Items.Add(dbType);
|
2016-12-21 14:17:20 +00:00
|
|
|
usersDbTypeComboBox.Items.Add(dbType);
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
2015-12-31 21:48:22 +00:00
|
|
|
configDbTypeComboBox.SelectedIndex = (int)bench.ProceduresDBSettings.DbType;
|
|
|
|
|
resultsDbTypeComboBox.SelectedIndex = (int)bench.WaterMetersDBSettings.DbType;
|
2020-03-05 12:59:43 +00:00
|
|
|
eventsDbTypeComboBox.SelectedIndex = (int)bench.EventsDBSettings.DbType;
|
2016-12-21 14:17:20 +00:00
|
|
|
usersDbTypeComboBox.SelectedIndex = (int)bench.UsersDBSettings.DbType;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
/// Initialize connection strings
|
2015-12-31 21:48:22 +00:00
|
|
|
configDbConnectionStringTextBox.Text = bench.ProceduresDBSettings.ConnectionString;
|
|
|
|
|
resultsDbConnectionStringTextBox.Text = bench.WaterMetersDBSettings.ConnectionString;
|
2020-03-05 12:59:43 +00:00
|
|
|
eventsDbConnectionStringTextBox.Text = bench.EventsDBSettings.ConnectionString;
|
2016-12-21 14:17:20 +00:00
|
|
|
usersDbConnectionStringTextBox.Text = bench.UsersDBSettings.ConnectionString;
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UpdateBenchFromUIControls()
|
|
|
|
|
{
|
2015-03-09 14:26:44 +00:00
|
|
|
/// TODO: The following code doesnot work as expected - error message shown!
|
|
|
|
|
//for (int i = 0; i < Program.LocalSettings.BenchesCount; i++)
|
|
|
|
|
//{
|
|
|
|
|
// if (bench != Program.LocalSettings.TestBenches[i] &&
|
|
|
|
|
// benchNameTextBox.Text == Program.LocalSettings.TestBenches[i].BenchName)
|
|
|
|
|
// {
|
|
|
|
|
// MessageBox.Show(Strings.Bench_name_conflict_Use_another_name_pls, Strings.Error, MessageBoxButtons.OK);
|
|
|
|
|
// return false;
|
|
|
|
|
// }
|
|
|
|
|
//}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
bench.BenchName = benchNameTextBox.Text;
|
|
|
|
|
bench.IsRealBench = realBenchCheckBox.Checked;
|
|
|
|
|
|
2015-12-31 21:48:22 +00:00
|
|
|
bench.ProceduresDBSettings.ConnectionString = configDbConnectionStringTextBox.Text;
|
|
|
|
|
bench.WaterMetersDBSettings.ConnectionString = resultsDbConnectionStringTextBox.Text;
|
2020-03-05 12:59:43 +00:00
|
|
|
bench.EventsDBSettings.ConnectionString = eventsDbConnectionStringTextBox.Text;
|
2016-12-21 14:17:20 +00:00
|
|
|
bench.UsersDBSettings.ConnectionString = usersDbConnectionStringTextBox.Text;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2022-01-23 18:56:15 +00:00
|
|
|
bench.ProceduresDBSettings.DbType = (DBType)configDbTypeComboBox.SelectedIndex;
|
|
|
|
|
bench.WaterMetersDBSettings.DbType = (DBType)resultsDbTypeComboBox.SelectedIndex;
|
|
|
|
|
bench.EventsDBSettings.DbType = (DBType)eventsDbTypeComboBox.SelectedIndex;
|
|
|
|
|
bench.UsersDBSettings.DbType = (DBType)usersDbTypeComboBox.SelectedIndex;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update bench DB setting, return 'OK'
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">Not used</param>
|
|
|
|
|
/// <param name="e">Not used</param>
|
|
|
|
|
private void okButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (UpdateBenchFromUIControls())
|
|
|
|
|
{
|
|
|
|
|
DialogResult = DialogResult.OK;
|
|
|
|
|
Close();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DialogResult = DialogResult.None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Throw away changes, return 'Cancel'
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">Not used</param>
|
|
|
|
|
/// <param name="e">Not used</param>
|
|
|
|
|
private void cancelButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
DialogResult = DialogResult.Cancel;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 15:01:51 +00:00
|
|
|
|
2020-03-05 12:59:43 +00:00
|
|
|
private void testConnConfigDbBtn_Click(object sender, EventArgs e) { }
|
|
|
|
|
private void testConnResultsDbBtn_Click(object sender, EventArgs e) { }
|
|
|
|
|
private void testConnEventsDbBtn_Click(object sender, EventArgs e) { }
|
|
|
|
|
private void testConnUsersDbBtn_Click(object sender, EventArgs e) { }
|
2019-07-25 15:01:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
private void createConfigDbBtn_Click(object sender, EventArgs e)
|
2014-07-28 07:54:35 +00:00
|
|
|
{
|
2019-07-25 15:01:51 +00:00
|
|
|
/// Update database connection strings
|
2014-07-28 07:54:35 +00:00
|
|
|
if (!UpdateBenchFromUIControls()) return;
|
|
|
|
|
|
|
|
|
|
if (DialogResult.OK == MessageBox.Show(Strings.DB_will_be_overwritten + Environment.NewLine +
|
|
|
|
|
Strings.Do_you_want_to_proceed, Strings.Warning,
|
|
|
|
|
MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation))
|
|
|
|
|
{
|
2019-07-25 15:01:51 +00:00
|
|
|
try
|
|
|
|
|
{
|
2019-08-27 09:00:30 +00:00
|
|
|
string connectionString = bench.ProceduresDBSettings.ConnectionString;
|
|
|
|
|
string databaseName = Config.Utils.GetDBName(connectionString);
|
|
|
|
|
string userName = Config.Utils.GetDBUser(connectionString);
|
|
|
|
|
string password = Config.Utils.GetDBPassword(connectionString); /// TODO: Use password
|
|
|
|
|
|
2020-03-05 12:59:43 +00:00
|
|
|
if (connectionString == currentConfigDBConnStr || connectionString == currentResultsDBConnStr || connectionString == currentEventsDBConnStr)
|
2019-08-27 09:00:30 +00:00
|
|
|
{
|
|
|
|
|
CurrentDBChanged = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 15:01:51 +00:00
|
|
|
Cursor.Current = Cursors.WaitCursor;
|
2019-08-27 09:00:30 +00:00
|
|
|
|
|
|
|
|
log.ErrorFormat("Going to create an empty configuration database '{0}'", databaseName);
|
2020-01-30 09:13:40 +00:00
|
|
|
ExecuteMySqlCmd(string.Format("DROP DATABASE `{0}`;", databaseName), userName, password);
|
|
|
|
|
ExecuteMySqlCmd(string.Format("CREATE DATABASE `{0}` CHARACTER SET utf8 COLLATE utf8_unicode_ci;", databaseName), userName, password);
|
2019-08-27 09:00:30 +00:00
|
|
|
Config.FluentCommon.CreateEmptyConfigDB(bench.ProceduresDBSettings.DbType, connectionString);
|
|
|
|
|
log.ErrorFormat("An empty configuration database '{0}' was created", databaseName);
|
|
|
|
|
|
2019-07-25 15:01:51 +00:00
|
|
|
MessageBox.Show(Strings.DB_was_successfully_created, Strings.Notification);
|
|
|
|
|
Cursor.Current = Cursors.Default;
|
|
|
|
|
DialogResult = DialogResult.None;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
2020-03-02 09:42:27 +00:00
|
|
|
if (File.Exists(CredFileName)) File.Delete(CredFileName);
|
2019-07-25 15:01:51 +00:00
|
|
|
Cursor.Current = Cursors.Default;
|
2020-03-02 09:42:27 +00:00
|
|
|
string msg = string.Format("{0}{1}{2}", Strings.Error_while_creating_DB_Reason, Environment.NewLine, exception.Message);
|
|
|
|
|
if (exception.InnerException != null)
|
|
|
|
|
{
|
|
|
|
|
msg += Environment.NewLine + exception.InnerException.Message;
|
|
|
|
|
}
|
|
|
|
|
MessageBox.Show(msg, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2019-07-25 15:01:51 +00:00
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 15:01:51 +00:00
|
|
|
private void createResultsDbBtn_Click(object sender, EventArgs e)
|
2016-12-19 13:39:28 +00:00
|
|
|
{
|
2019-07-25 15:01:51 +00:00
|
|
|
/// Update database connection strings
|
2016-12-19 13:39:28 +00:00
|
|
|
if (!UpdateBenchFromUIControls()) return;
|
|
|
|
|
|
|
|
|
|
if (DialogResult.OK == MessageBox.Show(Strings.DB_will_be_overwritten + Environment.NewLine +
|
|
|
|
|
Strings.Do_you_want_to_proceed, Strings.Warning,
|
|
|
|
|
MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation))
|
2019-07-25 15:01:51 +00:00
|
|
|
{
|
2016-12-19 13:39:28 +00:00
|
|
|
try
|
|
|
|
|
{
|
2019-08-27 09:00:30 +00:00
|
|
|
string connectionString = bench.WaterMetersDBSettings.ConnectionString;
|
|
|
|
|
string databaseName = Config.Utils.GetDBName(connectionString);
|
|
|
|
|
string userName = Config.Utils.GetDBUser(connectionString);
|
|
|
|
|
string password = Config.Utils.GetDBPassword(connectionString); /// TODO: Use password
|
|
|
|
|
|
2020-03-05 12:59:43 +00:00
|
|
|
if (connectionString == currentConfigDBConnStr || connectionString == currentResultsDBConnStr || connectionString == currentEventsDBConnStr)
|
2019-08-27 09:00:30 +00:00
|
|
|
{
|
|
|
|
|
CurrentDBChanged = true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-19 13:39:28 +00:00
|
|
|
Cursor.Current = Cursors.WaitCursor;
|
2019-08-27 09:00:30 +00:00
|
|
|
|
|
|
|
|
log.ErrorFormat("Going to create an empty results database '{0}'", databaseName);
|
2020-01-30 09:13:40 +00:00
|
|
|
ExecuteMySqlCmd(string.Format("DROP DATABASE `{0}`;", databaseName), userName, password);
|
|
|
|
|
ExecuteMySqlCmd(string.Format("CREATE DATABASE `{0}` CHARACTER SET utf8 COLLATE utf8_unicode_ci;", databaseName), userName, password);
|
2020-03-05 12:59:43 +00:00
|
|
|
global::Results.DB.DbType = bench.WaterMetersDBSettings.DbType;
|
|
|
|
|
global::Results.DB.ConnectionString = connectionString;
|
|
|
|
|
global::Results.DB.CreateEmptyDB();
|
2019-08-27 09:00:30 +00:00
|
|
|
log.ErrorFormat("An empty results database '{0}' was created", databaseName);
|
|
|
|
|
|
2016-12-19 13:39:28 +00:00
|
|
|
MessageBox.Show(Strings.DB_was_successfully_created, Strings.Notification);
|
|
|
|
|
Cursor.Current = Cursors.Default;
|
|
|
|
|
DialogResult = DialogResult.None;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
2020-03-02 09:42:27 +00:00
|
|
|
if (File.Exists(CredFileName)) File.Delete(CredFileName);
|
2016-12-19 13:39:28 +00:00
|
|
|
Cursor.Current = Cursors.Default;
|
2020-03-02 09:42:27 +00:00
|
|
|
string msg = string.Format("{0}{1}{2}", Strings.Error_while_creating_DB_Reason, Environment.NewLine, exception.Message);
|
2016-12-19 13:39:28 +00:00
|
|
|
if (exception.InnerException != null)
|
|
|
|
|
{
|
|
|
|
|
msg += Environment.NewLine + exception.InnerException.Message;
|
|
|
|
|
}
|
|
|
|
|
MessageBox.Show(msg, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
2019-07-25 15:01:51 +00:00
|
|
|
}
|
2016-12-19 13:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-05 12:59:43 +00:00
|
|
|
private void createEventsDbBtn_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
/// Update database connection strings
|
|
|
|
|
if (!UpdateBenchFromUIControls()) return;
|
|
|
|
|
|
|
|
|
|
if (DialogResult.OK == MessageBox.Show(Strings.DB_will_be_overwritten + Environment.NewLine +
|
|
|
|
|
Strings.Do_you_want_to_proceed, Strings.Warning,
|
|
|
|
|
MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string connectionString = bench.EventsDBSettings.ConnectionString;
|
|
|
|
|
string databaseName = Config.Utils.GetDBName(connectionString);
|
|
|
|
|
string userName = Config.Utils.GetDBUser(connectionString);
|
|
|
|
|
string password = Config.Utils.GetDBPassword(connectionString); /// TODO: Use password
|
|
|
|
|
|
|
|
|
|
if (connectionString == currentConfigDBConnStr || connectionString == currentResultsDBConnStr || connectionString == currentEventsDBConnStr)
|
|
|
|
|
{
|
|
|
|
|
CurrentDBChanged = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Cursor.Current = Cursors.WaitCursor;
|
|
|
|
|
|
|
|
|
|
log.ErrorFormat("Going to create an empty events database '{0}'", databaseName);
|
|
|
|
|
ExecuteMySqlCmd(string.Format("DROP DATABASE `{0}`;", databaseName), userName, password);
|
|
|
|
|
ExecuteMySqlCmd(string.Format("CREATE DATABASE `{0}` CHARACTER SET utf8 COLLATE utf8_unicode_ci;", databaseName), userName, password);
|
2021-09-23 09:46:40 +00:00
|
|
|
global::Events.DB.DbType = (DBType)bench.EventsDBSettings.DbType;
|
2020-03-05 12:59:43 +00:00
|
|
|
global::Events.DB.ConnectionString = connectionString;
|
|
|
|
|
global::Events.DB.CreateEmptyDB();
|
|
|
|
|
log.ErrorFormat("An empty events database '{0}' was created", databaseName);
|
|
|
|
|
|
|
|
|
|
MessageBox.Show(Strings.DB_was_successfully_created, Strings.Notification);
|
|
|
|
|
Cursor.Current = Cursors.Default;
|
|
|
|
|
DialogResult = DialogResult.None;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(CredFileName)) File.Delete(CredFileName);
|
|
|
|
|
Cursor.Current = Cursors.Default;
|
|
|
|
|
string msg = string.Format("{0}{1}{2}", Strings.Error_while_creating_DB_Reason, Environment.NewLine, exception.Message);
|
|
|
|
|
if (exception.InnerException != null)
|
|
|
|
|
{
|
|
|
|
|
msg += Environment.NewLine + exception.InnerException.Message;
|
|
|
|
|
}
|
|
|
|
|
MessageBox.Show(msg, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 15:01:51 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Configuration database import (UI)
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void importConfigDbBtn_Click(object sender, EventArgs e)
|
2014-07-28 07:54:35 +00:00
|
|
|
{
|
2019-08-27 09:00:30 +00:00
|
|
|
/// Update database connection strings from UI
|
2019-07-25 15:01:51 +00:00
|
|
|
if (!UpdateBenchFromUIControls()) return;
|
|
|
|
|
|
2019-08-27 09:00:30 +00:00
|
|
|
string connectionString = bench.ProceduresDBSettings.ConnectionString;
|
2019-07-25 15:01:51 +00:00
|
|
|
|
2019-08-27 09:00:30 +00:00
|
|
|
string server = Config.Utils.GetDBServer(connectionString);
|
|
|
|
|
if ((server != "localhost") && (server != "127.0.0.1"))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(Strings.Remote_database_cannot_be_imported, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
return;
|
2019-07-25 15:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-27 09:00:30 +00:00
|
|
|
OpenFileDialog dlg = new OpenFileDialog();
|
|
|
|
|
dlg.InitialDirectory = string.Format(@"C:\TBF\DbBackups\");
|
|
|
|
|
if (dlg.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
if (MessageBox.Show(string.Format(Strings.Do_you_want_to_restore_configuration_DB_from_file_0_qm, dlg.FileName),
|
|
|
|
|
string.Empty,
|
|
|
|
|
MessageBoxButtons.YesNo,
|
|
|
|
|
MessageBoxIcon.Question) == DialogResult.Yes)
|
|
|
|
|
{
|
|
|
|
|
DBImportMessageStart("Importing database with configuration");
|
|
|
|
|
ImportDatabase(connectionString, dlg.FileName);
|
|
|
|
|
DBImportMessageEnd();
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
2019-07-25 15:01:51 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Results database import (UI)
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void importResultsDbBtn_Click(object sender, EventArgs e)
|
2014-07-28 07:54:35 +00:00
|
|
|
{
|
2019-08-27 09:00:30 +00:00
|
|
|
/// Update database connection strings from UI
|
2019-07-25 15:01:51 +00:00
|
|
|
if (!UpdateBenchFromUIControls()) return;
|
|
|
|
|
|
2019-08-27 09:00:30 +00:00
|
|
|
string connectionString = bench.WaterMetersDBSettings.ConnectionString;
|
|
|
|
|
|
|
|
|
|
string server = Config.Utils.GetDBServer(connectionString);
|
|
|
|
|
if ((server != "localhost") && (server != "127.0.0.1"))
|
2019-07-25 15:01:51 +00:00
|
|
|
{
|
2019-08-27 09:00:30 +00:00
|
|
|
MessageBox.Show(Strings.Remote_database_cannot_be_imported, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-07-25 15:01:51 +00:00
|
|
|
|
2019-08-27 09:00:30 +00:00
|
|
|
OpenFileDialog dlg = new OpenFileDialog();
|
|
|
|
|
dlg.InitialDirectory = string.Format(@"C:\TBF\DbBackups\");
|
|
|
|
|
if (dlg.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
if (MessageBox.Show(string.Format(Strings.Do_you_want_to_restore_results_DB_from_file_0_qm, dlg.FileName),
|
|
|
|
|
string.Empty,
|
|
|
|
|
MessageBoxButtons.YesNo,
|
|
|
|
|
MessageBoxIcon.Question) == DialogResult.Yes)
|
|
|
|
|
{
|
2020-03-05 12:59:43 +00:00
|
|
|
DBImportMessageStart("Importing DB with Results");
|
|
|
|
|
ImportDatabase(connectionString, dlg.FileName);
|
|
|
|
|
DBImportMessageEnd();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void importEventsDbBtn_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
/// Update database connection strings from UI
|
|
|
|
|
if (!UpdateBenchFromUIControls()) return;
|
|
|
|
|
|
|
|
|
|
string connectionString = bench.EventsDBSettings.ConnectionString;
|
|
|
|
|
|
|
|
|
|
string server = Config.Utils.GetDBServer(connectionString);
|
|
|
|
|
if ((server != "localhost") && (server != "127.0.0.1"))
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(Strings.Remote_database_cannot_be_imported, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OpenFileDialog dlg = new OpenFileDialog();
|
|
|
|
|
dlg.InitialDirectory = string.Format(@"C:\TBF\DbBackups\");
|
|
|
|
|
if (dlg.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
if (MessageBox.Show(string.Format(Strings.Do_you_want_to_restore_events_DB_from_file_0_qm, dlg.FileName),
|
|
|
|
|
string.Empty,
|
|
|
|
|
MessageBoxButtons.YesNo,
|
|
|
|
|
MessageBoxIcon.Question) == DialogResult.Yes)
|
|
|
|
|
{
|
|
|
|
|
DBImportMessageStart("Importing DB with Events");
|
2019-08-27 09:00:30 +00:00
|
|
|
ImportDatabase(connectionString, dlg.FileName);
|
|
|
|
|
DBImportMessageEnd();
|
|
|
|
|
}
|
2019-07-25 15:01:51 +00:00
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
2016-12-19 13:39:28 +00:00
|
|
|
|
2019-09-16 08:51:25 +00:00
|
|
|
Shared.ModelessActivityForm form;
|
2019-07-25 15:01:51 +00:00
|
|
|
System.Threading.Thread formThread;
|
|
|
|
|
///
|
|
|
|
|
void DBImportMessageStart(string message)
|
2016-12-19 13:39:28 +00:00
|
|
|
{
|
2019-09-16 08:51:25 +00:00
|
|
|
form = new Shared.ModelessActivityForm()
|
2019-07-25 15:01:51 +00:00
|
|
|
{
|
|
|
|
|
Message = message,
|
|
|
|
|
FontFamily = "Arial",
|
|
|
|
|
FontSize = 24,
|
|
|
|
|
FontStyle = FontStyle.Regular,
|
|
|
|
|
BackgroundColor = Color.RoyalBlue,
|
|
|
|
|
};
|
|
|
|
|
formThread = new System.Threading.Thread(() => form.ShowDialog());
|
|
|
|
|
formThread.Start();
|
|
|
|
|
}
|
|
|
|
|
///
|
|
|
|
|
void DBImportMessageEnd()
|
|
|
|
|
{
|
|
|
|
|
if (form != null && formThread != null)
|
|
|
|
|
{
|
|
|
|
|
form.CloseForm(null, new EventArgs());
|
|
|
|
|
formThread.Join();
|
|
|
|
|
}
|
|
|
|
|
form = null;
|
|
|
|
|
formThread = null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-19 13:39:28 +00:00
|
|
|
|
2019-07-25 15:01:51 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Database import (functionality)
|
|
|
|
|
/// Invokes:
|
|
|
|
|
/// mysql -u[userName] --default-character-set=utf8 --database [databaseName]
|
|
|
|
|
/// SOURCE {SQL file name]
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="connectionString">DB connection string</param>
|
|
|
|
|
/// <param name="outputFileName">Output file name</param>
|
|
|
|
|
void ImportDatabase(string connectionString, string inputFileName)
|
|
|
|
|
{
|
|
|
|
|
string databaseName = Config.Utils.GetDBName(connectionString);
|
|
|
|
|
string userName = Config.Utils.GetDBUser(connectionString);
|
|
|
|
|
string password = Config.Utils.GetDBPassword(connectionString); /// TODO: Use password
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-03-05 12:59:43 +00:00
|
|
|
if (connectionString == currentConfigDBConnStr || connectionString == currentResultsDBConnStr || connectionString == currentEventsDBConnStr)
|
2019-08-27 09:00:30 +00:00
|
|
|
{
|
|
|
|
|
CurrentDBChanged = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.ErrorFormat("Going to import database '{0}' from file {1}", databaseName, inputFileName);
|
2020-01-30 09:13:40 +00:00
|
|
|
ExecuteMySqlCmd(string.Format("DROP DATABASE `{0}`;", databaseName), userName, password);
|
|
|
|
|
ExecuteMySqlCmd(string.Format("CREATE DATABASE `{0}` CHARACTER SET utf8 COLLATE utf8_unicode_ci;", databaseName), userName, password);
|
|
|
|
|
ExecuteMySqlCmd(string.Format("SOURCE {0}", inputFileName), userName, password, databaseName);
|
2019-08-27 09:00:30 +00:00
|
|
|
log.ErrorFormat("Database {0} was imported from file {1}", databaseName, inputFileName);
|
2019-07-25 15:01:51 +00:00
|
|
|
}
|
|
|
|
|
catch (Exception exc)
|
|
|
|
|
{
|
2020-03-02 09:42:27 +00:00
|
|
|
if (File.Exists(CredFileName)) File.Delete(CredFileName);
|
2019-08-27 09:00:30 +00:00
|
|
|
log.FatalFormat("Failed to import database '{0}' from file {1}: {2}", databaseName, inputFileName, exc.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Execute a MySQL command line command without specifying a database
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="command">MySQL command</param>
|
|
|
|
|
/// <param name="userName">User name</param>
|
2020-01-30 09:13:40 +00:00
|
|
|
void ExecuteMySqlCmd(string command, string userName, string password)
|
2019-08-27 09:00:30 +00:00
|
|
|
{
|
2020-01-30 09:13:40 +00:00
|
|
|
ExecuteMySqlCmd(command, userName, password, null);
|
2019-08-27 09:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Execute a MySQL command line command, specify database to be used
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="command">MySQL command</param>
|
|
|
|
|
/// <param name="userName">User name</param>
|
|
|
|
|
/// <param name="databaseToBeUsed">Database name</param>
|
2020-01-30 09:13:40 +00:00
|
|
|
void ExecuteMySqlCmd(string command, string userName, string password, string databaseToBeUsed)
|
2019-08-27 09:00:30 +00:00
|
|
|
{
|
2020-03-02 09:42:27 +00:00
|
|
|
using (TextWriter writer = new StreamWriter(CredFileName))
|
2020-01-30 09:13:40 +00:00
|
|
|
{
|
2020-05-06 10:41:42 +00:00
|
|
|
writer.WriteLine("[client]");
|
|
|
|
|
writer.WriteLine(string.Format("password=\"{0}\"", password));
|
2020-01-30 09:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-16 08:51:25 +00:00
|
|
|
System.Diagnostics.Process proc = new System.Diagnostics.Process();
|
2019-08-27 09:00:30 +00:00
|
|
|
|
|
|
|
|
proc.StartInfo.FileName = @"C:\xampp\mysql\bin\mysql.exe";
|
|
|
|
|
if (string.IsNullOrEmpty(databaseToBeUsed))
|
|
|
|
|
{
|
2020-05-06 10:41:42 +00:00
|
|
|
proc.StartInfo.Arguments = string.Format("--defaults-file={0} --user={1} --host=localhost --protocol=tcp --port=3306 --default-character-set=utf8", CredFileName, userName);
|
2019-08-27 09:00:30 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-05-06 10:41:42 +00:00
|
|
|
proc.StartInfo.Arguments = string.Format("--defaults-file={0} --user={1} --host=localhost --protocol=tcp --port=3306 --default-character-set=utf8 --database={2}", CredFileName, userName, databaseToBeUsed);
|
2019-07-25 15:01:51 +00:00
|
|
|
}
|
2019-08-27 09:00:30 +00:00
|
|
|
proc.StartInfo.UseShellExecute = false; /// Must be false to redirect standard input or standard output
|
|
|
|
|
proc.StartInfo.RedirectStandardOutput = false;
|
|
|
|
|
proc.StartInfo.RedirectStandardInput = true;
|
|
|
|
|
proc.StartInfo.CreateNoWindow = false;
|
|
|
|
|
//proc.StartInfo.Verb = "runas"; /// To run mysql.exe as administrator
|
|
|
|
|
|
|
|
|
|
proc.Start();
|
|
|
|
|
proc.StandardInput.WriteLine(command);
|
2019-09-16 08:51:25 +00:00
|
|
|
System.Diagnostics.Debug.WriteLine(command);
|
2019-08-27 09:00:30 +00:00
|
|
|
log.Warn(command);
|
|
|
|
|
proc.StandardInput.WriteLine("exit");
|
|
|
|
|
proc.WaitForExit();
|
2020-01-30 09:13:40 +00:00
|
|
|
|
2020-03-02 09:42:27 +00:00
|
|
|
File.Delete(CredFileName);
|
2016-12-19 13:39:28 +00:00
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
|
|
|
|
}
|