using System; using System.Windows.Forms; using System.Collections.Generic; using log4net; using MySql.Data.MySqlClient; namespace TBF.Forms { public partial class UpgradeSelectionDlg : Form { private static readonly ILog log = LogManager.GetLogger(typeof(UpgradeSelectionDlg)); public UpgradeSelectionDlg() { InitializeComponent(); } /// /// Common part of database upgrade code /// /// Database connection /// Array of SQL commands to be executed /// true = successful bool UpgradeCommon(MySqlConnection dbConn, string[] commands) { bool success = false; try { dbConn.Open(); foreach (var cmdText in commands) { MySqlCommand cmd = dbConn.CreateCommand(); cmd.CommandText = cmdText; cmd.ExecuteNonQuery(); cmd.Dispose(); } success = true; } catch (Exception exc) { log.ErrorFormat(string.Format("Exception when upgrading DB: {0}", exc.Message)); if (exc.InnerException != null) log.ErrorFormat(string.Format("Inner exception: {0}", exc.InnerException.Message)); MessageBox.Show(string.Format("Exception {0}", exc.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } finally { dbConn.Close(); } return success; } ///--------------------------------------------------------------------- private void upgradeDb_212_217_Button_Click(object sender, EventArgs e) { string[] configDbCommands = new string[] { /// 2.12 -> 2.13 "ALTER TABLE `procedure` ADD `Protected` BOOLEAN NOT NULL DEFAULT '0' AFTER `ProtocolTitle`, ADD `MustBeComplete` BOOLEAN NOT NULL DEFAULT '0' AFTER `Protected`, ADD `ManualCtrlDisabled` BOOLEAN NOT NULL DEFAULT '0' AFTER `MustBeComplete`", /// 2.13 -> 2.14 "ALTER TABLE `test` ADD `DoControlWaterTemp` BOOLEAN NOT NULL DEFAULT '0' AFTER `Zeroing`, ADD `TempLimLo` FLOAT NOT NULL DEFAULT '15' AFTER `DoControlWaterTemp`, ADD `TempLimHi` FLOAT NOT NULL DEFAULT '25' AFTER `TempLimLo`", /// 2.14 -> 2.15 "ALTER TABLE `procedure` ADD `AltProcName` VARCHAR(255) NULL DEFAULT NULL , ADD `AltProcPeriod` INT NOT NULL DEFAULT '0';", //"ALTER TABLE `procedure` DROP `ProtocolTitle`;", /// Commented out to maintain compatibility with an old ResultsBrowser }; string[] resultsDbCommands = new string[] { /// 2.12 -> 2.13 "ALTER TABLE `watermeter` ADD `Remark` VARCHAR( 2000 ) NULL DEFAULT NULL AFTER `ResultCode`", "ALTER TABLE `batch` ADD `Dirty` BOOLEAN NOT NULL DEFAULT '0' AFTER `EndTime`", /// 2.13 -> 2.14 #if !IPERL "ALTER TABLE `testrslt` ADD `FlowMean` FLOAT NOT NULL DEFAULT '0' AFTER `TempDivMax`", #endif "ALTER TABLE `testrslt` ADD `TestDone` BOOLEAN NOT NULL DEFAULT '1' AFTER `RepetitionNr`, ADD `Remark` VARCHAR( 2000 ) NULL DEFAULT NULL AFTER `TestDone`", "ALTER TABLE `testdata` ADD `DoControlWaterTemp` BOOLEAN NOT NULL DEFAULT '0' AFTER `Uncertainty`, ADD `TempLimLo` FLOAT NOT NULL DEFAULT '15' AFTER `DoControlWaterTemp`, ADD `TempLimHi` FLOAT NOT NULL DEFAULT '25' AFTER `TempLimLo`", /// 2.14 -> 2.15 "ALTER TABLE `watermeter` ADD `ArchivePath` VARCHAR(255) NULL DEFAULT NULL AFTER `ResultCode`;", /// 2.15 -> 2.17 "ALTER TABLE `testrslt` ADD `ErrorFlagsMd` TINYINT( 4 ) NOT NULL DEFAULT '0' AFTER `ErrorMaster`, ADD `ErrorFlags` INT NOT NULL DEFAULT '0' AFTER `ErrorFlagsMd`, ADD `DiverterStart` FLOAT NOT NULL DEFAULT '0.001' AFTER `ErrorMaster`, ADD `DiverterEnd` FLOAT NOT NULL DEFAULT '0.001' AFTER `DiverterStart`;", "ALTER TABLE `watermeter` ADD `ErrorFlags` INT NOT NULL DEFAULT '0' AFTER `QFall`;", }; if (UpgradeCommon(new MySqlConnection(Config.Data.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands) && UpgradeCommon(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands)) { MySqlConnection dbConn = new MySqlConnection(Config.Data.CurrentBench.ProceduresDBSettings.ConnectionString); try { dbConn.Open(); int maxItemNr = 0; MySqlCommand cmd = dbConn.CreateCommand(); cmd.CommandText = "SELECT ItemNr FROM component"; MySqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { int itemNr = dr.GetInt32(0); if (itemNr > maxItemNr) maxItemNr = itemNr; } dr.Close(); cmd = dbConn.CreateCommand(); cmd.CommandText = "UPDATE component SET ClassName = 'RegisterReader for standing start/stop' WHERE ClassName = 'Fixed-Start-Register-Reader'"; cmd.ExecuteNonQuery(); cmd.Dispose(); cmd = dbConn.CreateCommand(); cmd.CommandText = "UPDATE component SET ClassName = 'TestMethods.iPerlCommunication' WHERE ClassName = 'iPerlCommunication'"; cmd.ExecuteNonQuery(); cmd.Dispose(); cmd = dbConn.CreateCommand(); cmd.CommandText = "UPDATE component SET ClassName = 'WaterMeter' WHERE ClassName = 'iPerl'"; cmd.ExecuteNonQuery(); cmd.Dispose(); for (int i = 1; i <= 40; i++) { Text = string.Format("Upgrade selection: {0}/40", i); /// Read original iPerl water meter parameters int id = 0; string parameters = string.Empty; cmd = dbConn.CreateCommand(); cmd.CommandText = string.Format("SELECT Id, Parameters FROM Component WHERE Name = 'iPerl{0}' AND ClassName = 'WaterMeter'", i); dr = cmd.ExecuteReader(); if (dr.Read()) { id = dr.GetInt32(0); parameters = dr.GetString(1); } dr.Close(); cmd.Dispose(); if (id != 0) { /// Rename iPerl water meter cmd = dbConn.CreateCommand(); cmd.CommandText = string.Format("UPDATE component SET Name = 'WM{0}' WHERE Name = 'iPerl{0}'", i); cmd.ExecuteNonQuery(); cmd.Dispose(); /// Create iPerl Head cmd = dbConn.CreateCommand(); cmd.CommandText = string.Format("INSERT INTO component(ItemNr, Name, ClassName, Parentname, Mode, Logging, Parameters) VALUES ({0}, 'iPerl{1}', 'RegisterReader for iPerl', '', 'Normal', 'Off', @params)", maxItemNr + 1, i); cmd.Parameters.AddWithValue("@params", parameters.Replace("WaterMeterCfg", "IperlHeadCfg")); cmd.ExecuteNonQuery(); cmd.Dispose(); /// Clone procedure parameters IList idList = new List(); IList parList = new List(); IList procIdList = new List(); cmd = dbConn.CreateCommand(); cmd.CommandText = string.Format("SELECT Id, Parameters, Procedure_id FROM componentprocedure WHERE CmpntName = 'iPerl{0}'", i); dr = cmd.ExecuteReader(); while (dr.Read()) { idList.Add(dr.GetInt32(0)); parList.Add(dr.GetString(1)); procIdList.Add(dr.GetInt32(2)); } dr.Close(); cmd.Dispose(); if ((idList.Count == parList.Count) && (parList.Count == procIdList.Count)) { for (int j = 0; j < parList.Count; j++) { cmd = dbConn.CreateCommand(); cmd.CommandText = string.Format("INSERT INTO componentprocedure(CmpntName, Parameters, Procedure_id) VALUES ('WM{0}', @params, {1})", i, procIdList[j]); cmd.Parameters.AddWithValue("@params", parList[j].Replace("Q4", "Qmax") .Replace("Q3", "Qn") .Replace("Q2", "Qt") .Replace("Q1", "Qmin") .Replace("", "true\r\n ")); cmd.ExecuteNonQuery(); cmd.Dispose(); } } } } /// Delete 'Parmeters' of iPerl water meters cmd = dbConn.CreateCommand(); cmd.CommandText = "UPDATE component SET Parameters = '\r\n\r\n' WHERE ClassName = 'WaterMeter'"; cmd.ExecuteNonQuery(); cmd.Dispose(); Text = string.Format("Upgrade selection"); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_212_217_Button.Enabled = false; } catch (Exception exc) { log.ErrorFormat(string.Format("Exception when upgrading DB: {0}", exc.Message)); if (exc.InnerException != null) log.ErrorFormat(string.Format("Inner exception: {0}", exc.InnerException.Message)); MessageBox.Show(string.Format("Exception {0}", exc.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } finally { dbConn.Close(); } } } ///--------------------------------------------------------------------- private void upgradeDb_215_217_Button_Click(object sender, EventArgs e) { string[] commands = new string[] { "ALTER TABLE `testrslt` ADD `ErrorFlagsMd` TINYINT( 4 ) NOT NULL DEFAULT '0' AFTER `ErrorMaster`, ADD `ErrorFlags` INT NOT NULL DEFAULT '0' AFTER `ErrorFlagsMd`, ADD `DiverterStart` FLOAT NOT NULL DEFAULT '0.001' AFTER `ErrorMaster`, ADD `DiverterEnd` FLOAT NOT NULL DEFAULT '0.001' AFTER `DiverterStart`;", "ALTER TABLE `watermeter` ADD `ErrorFlags` INT NOT NULL DEFAULT '0' AFTER `QFall`;", }; if (UpgradeCommon(new MySqlConnection(Results.DB.ConnectionString), commands)) { MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_215_217_Button.Enabled = false; } } ///--------------------------------------------------------------------- private void upgradeDb_216_217_Button_Click(object sender, EventArgs e) { string[] commands = new string[] { "ALTER TABLE `testrslt` ADD `DiverterStart` FLOAT NOT NULL DEFAULT '0.001' AFTER `ErrorMaster`, ADD `DiverterEnd` FLOAT NOT NULL DEFAULT '0.001' AFTER `DiverterStart`;", "ALTER TABLE `watermeter` ADD `ErrorFlags` INT NOT NULL DEFAULT '0' AFTER `QFall`;", }; if (UpgradeCommon(new MySqlConnection(Results.DB.ConnectionString), commands)) { MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_216_217_Button.Enabled = false; } } private void upgradeDb_217_218_Button_Click(object sender, EventArgs e) { string[] commands = new string[] { "ALTER TABLE `measurementcorrection` ADD `RangeIx` INT NOT NULL DEFAULT '0' AFTER `Id`;", }; if (UpgradeCommon(new MySqlConnection(Config.Data.CurrentBench.ProceduresDBSettings.ConnectionString), commands)) { MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_217_218_Button.Enabled = false; } } } }