/// /// Copyright (c) 2016-2023 Sensus slovensko a.s. /// using System; using System.Drawing; using System.Windows.Forms; using System.Collections.Generic; using log4net; using MySql.Data.MySqlClient; using NHibernate; using Results; using Results.Entities; using TBF.Resources; using System.Text; namespace TBF.UI.Settings { public partial class UpgradeSelectionDlg : Form { private static readonly ILog log = LogManager.GetLogger(typeof(UpgradeSelectionDlg)); int batchFrom; int batchTo; Shared.ModelessActivityForm form; System.Threading.Thread formThread; public UpgradeSelectionDlg() { InitializeComponent(); upgradeConfigDBCheckBox.Checked = true; upgradeResultsDBCheckBox.Checked = true; batchFrom = 1; batchTo = Program.LocalSettings.BatchNr - 1; textBox1.Text = batchFrom.ToString(); textBox2.Text = batchTo.ToString(); } void ActivityStart() { form = new Shared.ModelessActivityForm() { Message = "Upgrading database", FontFamily = "Arial", FontSize = 24, FontStyle = FontStyle.Regular, BackgroundColor = Color.Green, }; formThread = new System.Threading.Thread(() => form.ShowDialog()); formThread.Start(); } void ActivityEnd() { if (form != null && formThread != null) { form.CloseForm(null, new EventArgs()); formThread.Join(); } form = null; formThread = null; } /// /// Common part of database upgrade code /// /// Database connection /// Array of SQL commands to be executed /// true = successful bool ExecuteCommands(MySqlConnection dbConn, string[] commands) { if (commands.Length == 0) return true; 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)); ActivityEnd(); MessageBox.Show(string.Format("Exception {0}", exc.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } finally { dbConn.Close(); } return success; } /// /// Common part of database upgrade code /// /// Database connection /// Array of SQL commands to be executed /// true = successful bool ChangeErrorFlags(string errorFlagsCmpntName) { bool success = false; ISession session = null; try { session = TBF.DB.ConfigDBSessionFactory.OpenSession(); var testParams = session.QueryOver() .Where(x => (x.CmpntName == errorFlagsCmpntName)) .List(); foreach (var tp in testParams) { string temp; if (tp.Parameters.Contains("Info")) { temp = tp.Parameters.Replace("true", "1"); } else if (tp.Parameters.Contains("On")) { temp = tp.Parameters.Replace("true", "2"); } else { temp = tp.Parameters.Replace("true", "0"); } tp.Parameters = temp.Replace("false", "0"); session.SaveOrUpdate(tp); } session.Flush(); session.Close(); success = true; } catch (Exception exc) { ActivityEnd(); MessageBox.Show(string.Format("Upgrade failed:\r\n{0}", exc.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { if (session != null) session.Close(); } return success; } ///--------------------------------------------------------------------- private void upgradeDb_NFC_button_Click(object sender, EventArgs e) { if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } string[] commands = new string[] { "ALTER TABLE `user` ADD `Tag` VARCHAR(255) NULL DEFAULT NULL AFTER `Number`;", }; ActivityStart(); if (!upgradeConfigDBCheckBox.Checked || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), commands)) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_NFC_button.Enabled = false; } } ///--------------------------------------------------------------------- private void upgradeDb_24_29_Button_Click(object sender, EventArgs e) { if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } string[] configDbCommands = new string[] { /// 2.4 -> 2.5 "ALTER TABLE `test` ADD `MassRepeats` INT NOT NULL DEFAULT '5' AFTER `PumpPower`, ADD `MassSpread` FLOAT NOT NULL DEFAULT '0' AFTER `MassRepeats`, ADD `MassMethod` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0' AFTER `MassSpread`", /// 2.5 -> 2.6 --- /// 2.6 -> 2.7 --- /// 2.7 -> 2.8 --- /// 2.8 -> 2.9 "ALTER TABLE `meterspath` DROP `Qfrom`, DROP `Qto`;", }; string[] resultsDbCommands = new string[] { /// 2.4 -> 2.5 --- /// 2.5 -> 2.6 "ALTER TABLE `watermeterdata` DROP `Q3`, CHANGE `Qn` `Q3_Qn` DOUBLE NULL DEFAULT NULL ", "ALTER TABLE `watermeterdata` ADD `DN` DOUBLE NOT NULL DEFAULT '0' AFTER `Producer`, ADD `L` DOUBLE NOT NULL DEFAULT '0' AFTER `DN`, ADD `Mounting` VARCHAR(255) NULL DEFAULT NULL AFTER `L`, ADD `TemperatureClass` VARCHAR(255) NULL DEFAULT NULL AFTER `Q1_Qmin`, ADD `PressureLossClass` VARCHAR(255) NULL DEFAULT NULL AFTER `TemperatureClass`", "ALTER TABLE `watermeterdata` ADD `MaxAdmissiblePressure` VARCHAR(255) NULL DEFAULT NULL AFTER `PressureLossClass`, ADD `FlowProfileSensitivityClass` VARCHAR(255) NULL DEFAULT NULL AFTER `MaxAdmissiblePressure`, ADD `Certificate` VARCHAR(255) NULL DEFAULT NULL AFTER `FlowProfileSensitivityClass`, ADD `PulsesPerLtr` DOUBLE NOT NULL DEFAULT '1' AFTER `Certificate`", "ALTER TABLE `watermeterdata` ADD `Text1` VARCHAR(255) NULL DEFAULT NULL AFTER `PulsesPerLtr`, ADD `Text2` VARCHAR(255) NULL DEFAULT NULL AFTER `Text1`, ADD `Text3` VARCHAR(255) NULL DEFAULT NULL AFTER `Text2`, ADD `Text4` VARCHAR(255) NULL DEFAULT NULL AFTER `Text3`, ADD `Text5` VARCHAR(255) NULL DEFAULT NULL AFTER `Text4`", /// 2.6 -> 2.7 "ALTER TABLE `watermeterdata` ADD `ProducerAux` VARCHAR(255) NULL DEFAULT NULL AFTER `PulsesPerLtr`, ADD `Q3_Qn_Aux` DOUBLE NOT NULL DEFAULT '0' AFTER `ProducerAux`, ADD `MetrologicalClassAux` VARCHAR(255) NULL DEFAULT NULL AFTER `Q3_Qn_Aux`, ADD `ApprovalInfoAux` VARCHAR(255) NULL DEFAULT NULL AFTER `MetrologicalClassAux`", /// 2.7 -> 2.8 "ALTER TABLE `watermeter` ADD `ResultCode` INT NOT NULL DEFAULT '0' AFTER `Passed`", "ALTER TABLE `batch` ADD `Remark` VARCHAR( 2000 ) NULL DEFAULT NULL AFTER `ProtocolTitle`", /// 2.8 -> 2.9 --- }; ActivityStart(); if ((!upgradeConfigDBCheckBox.Checked || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) && (!upgradeResultsDBCheckBox.Checked || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands))) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_24_29_Button.Enabled = false; } } ///--------------------------------------------------------------------- private void upgradeDb_29_212_Button_Click(object sender, EventArgs e) { if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } string[] configDbCommands = new string[] { /// 2.9 -> 2.10 /// 2.10 -> 2.11 "ALTER TABLE `feedingpath` ADD `RegulValvesPct` VARCHAR(255) NULL DEFAULT NULL AFTER `Pump;", /// 2.11 -> 2.12 "ALTER TABLE `benchpath` ADD `PressMtrDelta` VARCHAR(255) NULL DEFAULT NULL AFTER `PressOut;", }; string[] resultsDbCommands = new string[] { /// 2.9 -> 2.10 "ALTER TABLE `testrslt` ADD `AmbTempStart` FLOAT NOT NULL DEFAULT '0' AFTER `AmbientTempAve`", "ALTER TABLE `testrslt` ADD `AmbTempEnd` FLOAT NOT NULL DEFAULT '0' AFTER `AmbTempStart`", "ALTER TABLE `testrslt` ADD `AmbTempMin` FLOAT NOT NULL DEFAULT '0' AFTER `AmbTempEnd`", "ALTER TABLE `testrslt` ADD `AmbTempMax` FLOAT NOT NULL DEFAULT '0' AFTER `AmbTempMin`", "ALTER TABLE `testrslt` ADD `AmbPressStart` FLOAT NOT NULL DEFAULT '0' AFTER `AmbientPressAve`", "ALTER TABLE `testrslt` ADD `AmbPressEnd` FLOAT NOT NULL DEFAULT '0' AFTER `AmbPressStart`", "ALTER TABLE `testrslt` ADD `AmbPressMin` FLOAT NOT NULL DEFAULT '0' AFTER `AmbPressEnd`", "ALTER TABLE `testrslt` ADD `AmbPressMax` FLOAT NOT NULL DEFAULT '0' AFTER `AmbPressMin`", "ALTER TABLE `testrslt` ADD `AmbHumiStart` FLOAT NOT NULL DEFAULT '0' AFTER `AmbientHumiAve`", "ALTER TABLE `testrslt` ADD `AmbHumiEnd` FLOAT NOT NULL DEFAULT '0' AFTER `AmbHumiStart`", "ALTER TABLE `testrslt` ADD `AmbHumiMin` FLOAT NOT NULL DEFAULT '0' AFTER `AmbHumiEnd`", "ALTER TABLE `testrslt` ADD `AmbHumiMax` FLOAT NOT NULL DEFAULT '0' AFTER `AmbHumiMin`", /// 2.10 -> 2.11 /// 2.11 -> 2.12 "ALTER TABLE `testrslt` ADD `PressDeltaMean` FLOAT NOT NULL DEFAULT '0' AFTER `PressDownMax`", "ALTER TABLE `testrslt` ADD `PressDeltaStart` FLOAT NOT NULL DEFAULT '0' AFTER `PressDeltaMean`", "ALTER TABLE `testrslt` ADD `PressDeltaEnd` FLOAT NOT NULL DEFAULT '0' AFTER `PressDeltaStart`", "ALTER TABLE `testrslt` ADD `PressDeltaMin` FLOAT NOT NULL DEFAULT '0' AFTER `PressDeltaEnd`", "ALTER TABLE `testrslt` ADD `PressDeltaMax` FLOAT NOT NULL DEFAULT '0' AFTER `PressDeltaMin`", "ALTER TABLE `testrslt` ADD `FlowStart` FLOAT NOT NULL DEFAULT '0' AFTER `TempDivMax`", "ALTER TABLE `testrslt` ADD `FlowEnd` FLOAT NOT NULL DEFAULT '0' AFTER `FlowStart`", }; ActivityStart(); if ((!upgradeConfigDBCheckBox.Checked || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) && (!upgradeResultsDBCheckBox.Checked || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands))) { if (upgradeConfigDBCheckBox.Checked) { MySqlConnection dbConn = new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString); try { dbConn.Open(); MySqlCommand cmd; cmd = dbConn.CreateCommand(); cmd.CommandText = "UPDATE component SET ClassName = 'TestMethods.Adjustment.Single' WHERE ClassName = 'Adjustment'"; cmd.ExecuteNonQuery(); cmd.Dispose(); cmd = dbConn.CreateCommand(); cmd.CommandText = "UPDATE component SET ClassName = 'TestMethods.FlyingStartMassCollection.Compound' WHERE ClassName = 'CombinedMeters'"; cmd.ExecuteNonQuery(); cmd.Dispose(); cmd = dbConn.CreateCommand(); cmd.CommandText = "UPDATE component SET ClassName = 'TestMethods.CombinedWithDetection.Compound' WHERE ClassName = 'CombinedWithDetection'"; cmd.ExecuteNonQuery(); cmd.Dispose(); cmd = dbConn.CreateCommand(); cmd.CommandText = "UPDATE component SET ClassName = 'TestMethods.FixedStartMassCollection.Single' WHERE ClassName = 'FixedStartMassCollection'"; cmd.ExecuteNonQuery(); cmd.Dispose(); cmd = dbConn.CreateCommand(); cmd.CommandText = "UPDATE component SET ClassName = 'TestMethods.FixedStartMassCollection.Compound' WHERE ClassName = 'FixedStartCombinedMeters'"; cmd.ExecuteNonQuery(); cmd.Dispose(); cmd = dbConn.CreateCommand(); cmd.CommandText = "UPDATE component SET ClassName = 'TestMethods.FlyingStart.Single' WHERE ClassName = 'FlyingStart'"; cmd.ExecuteNonQuery(); cmd.Dispose(); cmd = dbConn.CreateCommand(); cmd.CommandText = "UPDATE component SET ClassName = 'TestMethods.FlyingStartMassCollection.Single' WHERE ClassName = 'Flying-Start-Collection'"; cmd.ExecuteNonQuery(); cmd.Dispose(); cmd = dbConn.CreateCommand(); cmd.CommandText = "UPDATE component SET ClassName = 'TestMethods.LeakTest' WHERE ClassName = 'Leak-Test'"; cmd.ExecuteNonQuery(); cmd.Dispose(); cmd = dbConn.CreateCommand(); cmd.CommandText = "UPDATE component SET ClassName = 'TestMethods.PMaxTest' WHERE ClassName = 'PMax-Test'"; cmd.ExecuteNonQuery(); cmd.Dispose(); cmd = dbConn.CreateCommand(); cmd.CommandText = "UPDATE component SET ClassName = 'Output.Printers.Enhanced.Single' WHERE ClassName = 'BasicResultsPrinter'"; cmd.ExecuteNonQuery(); cmd.Dispose(); ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_29_212_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)); ActivityEnd(); MessageBox.Show(string.Format("Exception {0}", exc.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } finally { dbConn.Close(); } } else { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_29_212_Button.Enabled = false; } } } ///--------------------------------------------------------------------- private void upgradeDb_212_215_Button_Click(object sender, EventArgs e) { if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } 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`;", }; ActivityStart(); if ((!upgradeConfigDBCheckBox.Checked || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) && (!upgradeResultsDBCheckBox.Checked || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands))) { if (upgradeConfigDBCheckBox.Checked) { MySqlConnection dbConn = new MySqlConnection(TBF.DB.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"); ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_212_215_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)); ActivityEnd(); MessageBox.Show(string.Format("Exception {0}", exc.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } finally { dbConn.Close(); } } else { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_212_215_Button.Enabled = false; } } } ///--------------------------------------------------------------------- private void upgradeDb_215_216_Button_Click(object sender, EventArgs e) { if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } 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`;", }; ActivityStart(); if (!upgradeResultsDBCheckBox.Checked || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), commands)) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_215_216_Button.Enabled = false; } } ///--------------------------------------------------------------------- private void upgradeDb_216_217_Button_Click(object sender, EventArgs e) { if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } 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`;", }; ActivityStart(); if (!upgradeResultsDBCheckBox.Checked || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), commands)) { ActivityEnd(); 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) { if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } string[] commands = new string[] { "ALTER TABLE `measurementcorrection` ADD `RangeIx` INT NOT NULL DEFAULT '0' AFTER `Id`;", }; ActivityStart(); if (!upgradeConfigDBCheckBox.Checked || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), commands)) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_217_218_Button.Enabled = false; } } ///--------------------------------------------------------------------- private void upgradeDb_218_222_Button_Click(object sender, EventArgs e) { if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } string[] resultsDbCommands = new string[] { "ALTER TABLE `metertestrslt` ADD `RegReaderType` INT NOT NULL DEFAULT '0' AFTER `CompoundMeterId`;", "ALTER TABLE `testrslt` ADD `MethodClass` VARCHAR(255) NULL DEFAULT NULL AFTER `RepetitionNr`," + " ADD `TimeBtwnMassMsrmnts` INT NOT NULL DEFAULT '0' AFTER `FlowSetTime`," + " ADD `TestTimeCorrection` DOUBLE NOT NULL DEFAULT '0' AFTER `TestTime`," + " ADD `ConstMasterRaw` DOUBLE NOT NULL DEFAULT '1' AFTER `PulsesMaster`," + " ADD `ConstMasterCorr` DOUBLE NOT NULL DEFAULT '1' AFTER `ConstMasterRaw`;", "ALTER TABLE `watermeterdata` ADD `Medium` VARCHAR(255) NULL DEFAULT NULL AFTER `ApprovalInfoAux`;", "ALTER TABLE `batch` ADD `Counter1` INT NOT NULL DEFAULT '0' AFTER `Custom3`," + " ADD `Counter2` INT NOT NULL DEFAULT '0' AFTER `Counter1`," + " ADD `Counter3` INT NOT NULL DEFAULT '0' AFTER `Counter2`," + " ADD `Counter4` INT NOT NULL DEFAULT '0' AFTER `Counter3`," + " ADD `Counter5` INT NOT NULL DEFAULT '0' AFTER `Counter4`," + " ADD `Counter6` INT NOT NULL DEFAULT '0' AFTER `Counter5`," + " ADD `Counter7` INT NOT NULL DEFAULT '0' AFTER `Counter6`," + " ADD `Counter8` INT NOT NULL DEFAULT '0' AFTER `Counter7`," + " ADD `Counter9` INT NOT NULL DEFAULT '0' AFTER `Counter8`," + " ADD `Counter10` INT NOT NULL DEFAULT '0' AFTER `Counter9`;", }; ActivityStart(); if (!upgradeResultsDBCheckBox.Checked || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands)) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_218_222_Button.Enabled = false; } } private void upgradeDb_222_224_Button_Click(object sender, EventArgs e) { if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } string[] configDbCommands = new string[] { /// Table profile "CREATE TABLE IF NOT EXISTS `profile` (" + "`Id` int(11) NOT NULL AUTO_INCREMENT," + " `ItemNr` int(11) NOT NULL DEFAULT '0'," + " `Name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL," + " `Description` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL," + " `CreationUser` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL," + " `CreationTime` datetime DEFAULT NULL," + " `LastChgUser` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL," + " `LastChgTime` datetime DEFAULT NULL," + " `ProfileType` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL," + " `ErrorLimitType` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL," + " PRIMARY KEY (`Id`));", /// Table ptest "CREATE TABLE IF NOT EXISTS `ptest` (" + "`Id` int(11) NOT NULL AUTO_INCREMENT," + " `ItemNr` int(11) NOT NULL DEFAULT '0'," + " `Name` varchar(255) DEFAULT NULL," + " `Part` int(11) NOT NULL DEFAULT '0'," + " `CoefQfrom` double NOT NULL DEFAULT '0'," + " `CoefQto` double NOT NULL DEFAULT '0'," + " `ErrLimLo` double NOT NULL DEFAULT '0'," + " `ErrLimHi` double NOT NULL DEFAULT '0'," + " `TempLimLo` double NOT NULL DEFAULT '0'," + " `TempLimHi` double NOT NULL DEFAULT '0'," + " `PressLimLo` double NOT NULL DEFAULT '0'," + " `PressLimHi` double NOT NULL DEFAULT '0'," + " `Publish` tinyint(4) NOT NULL DEFAULT '1'," + " `Evaluate` boolean NOT NULL DEFAULT TRUE," + " `E1` tinyint(4) NOT NULL DEFAULT '0'," + " `E2` tinyint(4) NOT NULL DEFAULT '0'," + " `E3` tinyint(4) NOT NULL DEFAULT '0'," + " `E4` tinyint(4) NOT NULL DEFAULT '0'," + " `E5` tinyint(4) NOT NULL DEFAULT '0'," + " `E6` tinyint(4) NOT NULL DEFAULT '0'," + " `E7` tinyint(4) NOT NULL DEFAULT '0'," + " `E8` tinyint(4) NOT NULL DEFAULT '0'," + " `E9` tinyint(4) NOT NULL DEFAULT '0'," + " `E10` tinyint(4) NOT NULL DEFAULT '0'," + " `E11` tinyint(4) NOT NULL DEFAULT '0'," + " `E12` tinyint(4) NOT NULL DEFAULT '0'," + " `E13` tinyint(4) NOT NULL DEFAULT '0'," + " `E14` tinyint(4) NOT NULL DEFAULT '0'," + " `E15` tinyint(4) NOT NULL DEFAULT '0'," + " `E16` tinyint(4) NOT NULL DEFAULT '0'," + " `E21` tinyint(4) NOT NULL DEFAULT '0'," + " `Delta_Q_pct` double NOT NULL DEFAULT '0'," + " `Delta_T` double NOT NULL DEFAULT '0'," + " `Delta_Tup_Tdn` double NOT NULL DEFAULT '0'," + " `TestTime_min` double NOT NULL DEFAULT '0'," + " `TestTime_max` double NOT NULL DEFAULT '0'," + " `AmbTemp_min` double NOT NULL DEFAULT '0'," + " `AmbTemp_max` double NOT NULL DEFAULT '0'," + " `Coef_E9` double NOT NULL DEFAULT '0'," + " `Coef_E10` double NOT NULL DEFAULT '0'," + " `Coef_E11` double NOT NULL DEFAULT '0'," + " `Coef_E12` double NOT NULL DEFAULT '0'," + " `Offset_E9` double NOT NULL DEFAULT '0'," + " `Offset_E12` double NOT NULL DEFAULT '0'," + " `Profile_id` int(11) DEFAULT NULL," + " PRIMARY KEY (`Id`));", /// Table test "ALTER TABLE `test` ADD `Qtg` FLOAT NULL DEFAULT NULL AFTER `Evaluate`;", "ALTER TABLE `test` CHANGE `RelTransAfter` `Profile` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;", "UPDATE `test` SET Profile = 'UserDefined';", /// Table uncertainty "CREATE TABLE IF NOT EXISTS `uncertainty` (" + " `Id` int(11) NOT NULL AUTO_INCREMENT," + " `Measurement` float NOT NULL DEFAULT '0'," + " `MainUncertainty` float NOT NULL DEFAULT '0'," + " `Resolution` float NOT NULL DEFAULT '0'," + " `DriftPerYr` float NOT NULL DEFAULT '0'," + " `Conditions` float NOT NULL DEFAULT '0'," + " `UncertaintyOfCorrection` float NOT NULL DEFAULT '0'," + " `Component_id` int(11) DEFAULT NULL," + " PRIMARY KEY (`Id`)," + " KEY `Component_id` (`Component_id`));", /// Table user "ALTER TABLE `user` ADD `Password2` VARCHAR(255) NULL DEFAULT NULL AFTER `Password`, ADD `Password3` VARCHAR(255) NULL DEFAULT NULL AFTER `Password2`, ADD `Password4` VARCHAR(255) NULL DEFAULT NULL AFTER `Password3`;", /// Tables watermeter and wmtype "DROP TABLE `watermeter`", "DROP TABLE `wmtype`", //"ALTER TABLE `procedure` DROP `ProtocolTitle`", }; string[] resultsDbCommands = new string[] { "ALTER TABLE `testdata` ADD `Qtg` DOUBLE NULL DEFAULT NULL AFTER `Name`;", "ALTER TABLE `testrslt` ADD `Uncertnt` FLOAT NOT NULL DEFAULT '0' AFTER `FlowMax`," + " ADD `UncertntScale` FLOAT NOT NULL DEFAULT '0' AFTER `Uncertnt`," + " ADD `UncertntDensity` FLOAT NOT NULL DEFAULT '0' AFTER `UncertntScale`," + " ADD `UncertntTemp` FLOAT NOT NULL DEFAULT '0' AFTER `UncertntDensity`," + " ADD `UncertntPressure` FLOAT NOT NULL DEFAULT '0' AFTER `UncertntTemp`;", "ALTER TABLE `testrslt` CHANGE `ErrorFlags` `ErrorFlags` BIGINT(20) NOT NULL DEFAULT '0', CHANGE `ErrorFlagsMd` `InfoFlags` BIGINT(20) NOT NULL DEFAULT '0';", "ALTER TABLE `watermeter` CHANGE `ErrorFlags` `ErrorFlags` BIGINT(20) NOT NULL DEFAULT '0', ADD `InfoFlags` BIGINT NOT NULL DEFAULT '0' AFTER `ErrorFlags`;", #if IPERL "ALTER TABLE `watermeter` DROP `Q2CorrectionDone`, DROP `Q2Correction`;", #endif }; ActivityStart(); bool success = true; if (upgradeConfigDBCheckBox.Checked) { success = success && ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands); if (!string.IsNullOrEmpty(errorFlagsComponentsTextBox.Text)) { string[] errorFlagsCmpnts = errorFlagsComponentsTextBox.Text.Split(new char[] { '~' }); foreach (var errFCmpnt in errorFlagsCmpnts) { success = success && ChangeErrorFlags(errFCmpnt); } } } if (upgradeResultsDBCheckBox.Checked) { success = success && ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands); } if (success) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded." + Environment.NewLine + "Please, create Density and Buoyancy components.", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_222_224_Button.Enabled = false; } } private void upgradeDb_224_225_Button_Click(object sender, EventArgs e) { MySqlConnection dbConn = null; MySqlCommand cmd; ActivityStart(); bool success = false; try { dbConn = new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString); dbConn.Open(); cmd = dbConn.CreateCommand(); cmd.CommandText = "CREATE TABLE IF NOT EXISTS `usersgroups` ( `Group_id` int(11) NOT NULL, `User_id` int(11) NOT NULL );"; cmd.ExecuteNonQuery(); cmd.Dispose(); cmd = dbConn.CreateCommand(); cmd.CommandText = "ALTER TABLE `usersgroups` ADD KEY `User_id` (`User_id`), ADD KEY `Group_id` (`Group_id`)," + " ADD CONSTRAINT `FKEC3AF23393C4061C` FOREIGN KEY (`Group_id`) REFERENCES `group` (`Id`)," + " ADD CONSTRAINT `FKEC3AF23373767C99` FOREIGN KEY (`User_id`) REFERENCES `user` (`Id`);"; cmd.ExecuteNonQuery(); cmd.Dispose(); int[] groupIdsToKeep = new int[(int)Common.GID.NrOfGroups]; IList groupTable = new List(); cmd = dbConn.CreateCommand(); cmd.CommandText = "SELECT `Id`, `Gid`, `User_id` FROM `group`;"; MySqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { int groupId = dr.GetInt32(0); Common.GID gid = (Common.GID)dr.GetInt32(1); int userId = dr.GetInt32(2); groupTable.Add(new GroupTableRow(groupId, gid, userId)); if ((gid >= 0) && (gid < Common.GID.NrOfGroups) && (groupIdsToKeep[(int)gid] == 0)) { groupIdsToKeep[(int)gid] = groupId; } } dr.Close(); foreach (var groupTableRow in groupTable) { cmd = dbConn.CreateCommand(); cmd.CommandText = string.Format("INSERT INTO `usersgroups` (`Group_id`, `User_id`) VALUES ({0}, {1});", groupIdsToKeep[(int)groupTableRow.Gid], groupTableRow.UserId); cmd.ExecuteNonQuery(); cmd.Dispose(); } //foreach (var groupTableRow in groupTable) //{ // bool deleteThisRow = true; // for (int i = 0; i < groupIdsToKeep.Length; i++) // { // if (groupTableRow.GroupId == groupIdsToKeep[i]) // { // deleteThisRow = false; // break; // } // } // if (deleteThisRow) // { // cmd = dbConn.CreateCommand(); // cmd.CommandText = string.Format("DELETE FROM `group` WHERE `Id` = {0}", groupTableRow.GroupId); // cmd.ExecuteNonQuery(); // cmd.Dispose(); // } //} cmd = dbConn.CreateCommand(); cmd.CommandText = string.Format("ALTER TABLE `group` ADD `AccessFlags` BIGINT NOT NULL DEFAULT '0' AFTER `Name`;"); 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)); ActivityEnd(); MessageBox.Show(string.Format("Exception {0}", exc.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } finally { if (dbConn != null) dbConn.Close(); } if (success) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_224_225_Button.Enabled = false; } } private void upgradeDb_225_226_Button_Click(object sender, EventArgs e) { if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } string[] configDbCommands = new string[] { "ALTER TABLE `procedure` ADD `EventTriggers` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL AFTER `ResultsWriter`;", "CREATE TABLE IF NOT EXISTS `customevent` (" + "`Id` int(11) NOT NULL," + " `Date` datetime DEFAULT NULL," + " `Frequency` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL," + " `AllDay` tinyint(1) DEFAULT NULL," + " `TriggerOnExactDayOnly` tinyint(1) DEFAULT NULL," + " `Source` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL," + " `Title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL," + " `AutoAction` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL," + " `Parameters` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL," + " `Rank` int(11) DEFAULT NULL," + " `Hidden` tinyint(1) DEFAULT NULL," + " `ReadOnly` tinyint(1) DEFAULT NULL," + " `BackColor` int(11) DEFAULT NULL," + " `TextColor` int(11) DEFAULT NULL," + " `TooltipEnabled` tinyint(1) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;", "ALTER TABLE `customevent` ADD PRIMARY KEY (`Id`);", "ALTER TABLE `customevent` MODIFY `Id` int(11) NOT NULL AUTO_INCREMENT;", }; string[] resultsDbCommands = new string[] { #if TURA_IPERL || TURA_IPERL_NEW || TURA_SPECIAL "ALTER TABLE `testrslt` ADD `ConductMean` FLOAT NULL DEFAULT NULL AFTER `FlowMax`," + " ADD `ConductStart` FLOAT NULL DEFAULT NULL AFTER `ConductMean`," + " ADD `ConductEnd` FLOAT NULL DEFAULT NULL AFTER `ConductStart`," + " ADD `ConductMin` FLOAT NULL DEFAULT NULL AFTER `ConductEnd`," + " ADD `ConductMax` FLOAT NULL DEFAULT NULL AFTER `ConductMin`;", #endif }; ActivityStart(); if ((!upgradeConfigDBCheckBox.Checked || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) && (!upgradeResultsDBCheckBox.Checked || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands))) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_225_226_Button.Enabled = false; } } private void pressureRepresentationButton_Click(object sender, EventArgs e) { try { batchFrom = int.Parse(textBox1.Text); batchTo = int.Parse(textBox2.Text); if (batchFrom < 1 || batchFrom > batchTo) throw new Exception(); } catch { MessageBox.Show("Wrong 'From batch #' or 'To batch #'", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (MessageBox.Show("Are you sure ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return; ActivityStart(); ISession session = null; try { session = TBF.DB.ResultsDBSessionFactory.OpenSession(); IList bFrom = session.QueryOver().Where(x => (x.BatchNr == batchFrom)).List(); IList bTo = session.QueryOver().Where(x => (x.BatchNr == batchTo)).List(); if (bFrom.Count != 1) throw new Exception("No start batch"); if (bTo.Count != 1) throw new Exception("No end batch"); IList testResults = session.QueryOver() .WhereRestrictionOn(x => x.StartTime) .IsBetween(bFrom[0].StartTime) .And(bTo[0].EndTime) .List(); foreach (var tr in testResults) { Text = string.Format("Batch # = {0}", tr.Batch.BatchNr); float factor1 = 0.01f; tr.PressUpMean *= factor1; tr.PressUpStart *= factor1; tr.PressUpEnd *= factor1; tr.PressUpMin *= factor1; tr.PressUpMax *= factor1; tr.PressDownMean *= factor1; tr.PressDownStart *= factor1; tr.PressDownEnd *= factor1; tr.PressDownMin *= factor1; tr.PressDownMax *= factor1; tr.AmbPressMean *= 0.001f; session.SaveOrUpdate(tr); } session.Flush(); ActivityEnd(); MessageBox.Show("Upgrade completed", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception exc) { ActivityEnd(); MessageBox.Show(string.Format("Upgrade failed:\r\n{0}", exc.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { if (session != null && session.IsOpen) session.Close(); } } private void customButton_Click(object sender, EventArgs e) { ActivityStart(); ISession session = null; try { session = TBF.DB.ConfigDBSessionFactory.OpenSession(); var proceduresToModify = session.QueryOver() .Where(x => x.CmpntName == "Sensus-Oracle-DB") .List(); foreach (var proc in proceduresToModify) { if (!string.IsNullOrEmpty(proc.Parameters)) { proc.Parameters = proc.Parameters.Replace("Iperl", "iPERL"); proc.Parameters = proc.Parameters.Replace("S620", "Mechanical"); session.SaveOrUpdate(proc); } } session.Flush(); ActivityEnd(); MessageBox.Show("Upgrade completed", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception exc) { ActivityEnd(); MessageBox.Show(string.Format("Upgrade failed:\r\n{0}", exc.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { if (session != null && session.IsOpen) session.Close(); } } private void upgradeDb_226_227_Button_Click(object sender, EventArgs e) { string[] configDbCommands = new string[] { }; string[] resultsDbCommands = new string[] { "ALTER TABLE `watermeter` ADD `RadioAddress` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL AFTER `SerialNrAux`;", }; if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } ActivityStart(); if ((!upgradeConfigDBCheckBox.Checked || configDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) && (!upgradeResultsDBCheckBox.Checked || resultsDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands))) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_226_227_Button.Enabled = false; } } private void upgradeDb_227_228_Button_Click(object sender, EventArgs e) { string[] configDbCommands = new string[] { "ALTER TABLE `test` ADD `TempControl` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL AFTER `TempLimHi`;", "ALTER TABLE `test` DROP `DoControlWaterTemp`;", }; string[] resultsDbCommands = new string[] { }; if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } ActivityStart(); if ((!upgradeConfigDBCheckBox.Checked || configDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) && (!upgradeResultsDBCheckBox.Checked || resultsDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands))) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_227_228_Button.Enabled = false; } } private void upgradeDb_228_229_Button_Click(object sender, EventArgs e) { string[] configDbCommands = new string[] { "ALTER TABLE `measurementcorrection` CHANGE `Measurement` `Measurement` DOUBLE NOT NULL DEFAULT '0';", "ALTER TABLE `measurementcorrection` CHANGE `Correction` `Correction` DOUBLE NOT NULL DEFAULT '0';", }; string[] resultsDbCommands = new string[] { }; if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } ActivityStart(); if ((!upgradeConfigDBCheckBox.Checked || configDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) && (!upgradeResultsDBCheckBox.Checked || resultsDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands))) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_228_229_Button.Enabled = false; } } private void upgradeDb_229_230_Button_Click(object sender, EventArgs e) { string[] configDbCommands = new string[] { "ALTER TABLE `benchpath` ADD `Selector` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL AFTER `Qto`;", "ALTER TABLE `feedingpath` ADD `Selector` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL AFTER `Qto`;", "ALTER TABLE `outputpath` ADD `Selector` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL AFTER `Qto`;", }; string[] resultsDbCommands = new string[] { }; if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } ActivityStart(); if ((!upgradeConfigDBCheckBox.Checked || configDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) && (!upgradeResultsDBCheckBox.Checked || resultsDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands))) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_229_230_Button.Enabled = false; } } private void upgradeDb_230_231_Button_Click(object sender, EventArgs e) { string[] configDbCommands = new string[] { //"ALTER TABLE `ptest` ADD `Publish` TINYINT(4) NOT NULL DEFAULT '0' AFTER `PressLimHi`, ADD `Evaluate` TINYINT(1) NOT NULL DEFAULT '0' AFTER `Publish`;", "ALTER TABLE `ptest` ADD `Method` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL AFTER `Evaluate`;", }; string[] resultsDbCommands = new string[] { }; if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } ActivityStart(); if ((!upgradeConfigDBCheckBox.Checked || configDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) && (!upgradeResultsDBCheckBox.Checked || resultsDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands))) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_230_231_Button.Enabled = false; } } private void upgradeDb_231_232_Button_Click(object sender, EventArgs e) { string[] configDbCommands = new string[] { }; string[] resultsDbCommands = new string[] { "ALTER TABLE `batch` CHANGE `StartTime` `StartTime` DATETIME NOT NULL DEFAULT '0001-01-01 00:00:00';", "ALTER TABLE `batch` CHANGE `EndTime` `EndTime` DATETIME NOT NULL DEFAULT '0001-01-01 00:00:00';", "ALTER TABLE `batch` ADD `SaveTime` DATETIME NOT NULL DEFAULT '0001-01-01 00:00:00' AFTER `EndTime`;" }; if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } ActivityStart(); if ((!upgradeConfigDBCheckBox.Checked || configDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) && (!upgradeResultsDBCheckBox.Checked || resultsDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands))) { ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_231_232_Button.Enabled = false; } } private void upgradeDb_232_233_Button_Click(object sender, EventArgs e) { string[] configDbCommands = new string[] { "ALTER TABLE `test` CHANGE `Qtg` `Qtg` DOUBLE NOT NULL DEFAULT '0';", "ALTER TABLE `test` CHANGE `Qfrom` `Qfrom` DOUBLE NOT NULL DEFAULT '0';", "ALTER TABLE `test` CHANGE `Qto` `Qto` DOUBLE NOT NULL DEFAULT '0';", "ALTER TABLE `test` CHANGE `Volume` `Volume` DOUBLE NOT NULL DEFAULT '0';", "ALTER TABLE `test` CHANGE `TstTime` `TstTime` DOUBLE NOT NULL DEFAULT '0';" }; string[] resultsDbCommands = new string[] { "ALTER TABLE `watermeterdata` ADD `Qnames` TINYINT(4) NOT NULL DEFAULT '1' AFTER `Mounting`;", "ALTER TABLE `watermeterdata` ADD `ErrLimHiQ` DOUBLE NOT NULL DEFAULT '2' AFTER `Q1_Qmin`," + " ADD `ErrLimLoQ` DOUBLE NOT NULL DEFAULT '5' AFTER `ErrLimHiQ`;" }; if (!upgradeConfigDBCheckBox.Checked && !upgradeResultsDBCheckBox.Checked) { MessageBox.Show("No database selected"); return; } ActivityStart(); if ((!upgradeConfigDBCheckBox.Checked || configDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(TBF.DB.CurrentBench.ProceduresDBSettings.ConnectionString), configDbCommands)) && (!upgradeResultsDBCheckBox.Checked || resultsDbCommands.Length == 0 || ExecuteCommands(new MySqlConnection(Results.DB.ConnectionString), resultsDbCommands))) { if (upgradeConfigDBCheckBox.Checked) { ISession session = null; try { session = TBF.DB.ConfigDBSessionFactory.OpenSession(); var tests = session.QueryOver().List(); foreach (var t in tests) { string singlePrecStr = Convert.ToSingle(t.Qtg).ToString(); if (t.Qtg.ToString().Length > 2 * singlePrecStr.Length) t.Qtg = double.Parse(singlePrecStr); singlePrecStr = Convert.ToSingle(t.Qfrom).ToString(); if (t.Qfrom.ToString().Length > 2 * singlePrecStr.Length) t.Qfrom = double.Parse(singlePrecStr); singlePrecStr = Convert.ToSingle(t.Qto).ToString(); if (t.Qto.ToString().Length > 2 * singlePrecStr.Length) t.Qto = double.Parse(singlePrecStr); singlePrecStr = Convert.ToSingle(t.Volume).ToString(); if (t.Volume.ToString().Length > 2 * singlePrecStr.Length) t.Volume = double.Parse(singlePrecStr); singlePrecStr = Convert.ToSingle(t.TestTime).ToString(); if (t.TestTime.ToString().Length > 2 * singlePrecStr.Length) t.TestTime = double.Parse(singlePrecStr); session.SaveOrUpdate(t); } session.Flush(); } catch (Exception exc) { ActivityEnd(); MessageBox.Show(string.Format("Test flow rounding after a conversion to double failed:\r\n{0}", exc.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { if (session != null && session.IsOpen) session.Close(); } } ActivityEnd(); MessageBox.Show("DB succesfully upgraded", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); upgradeDb_232_233_Button.Enabled = false; } } } }