Skip to content

Commit

Permalink
ErrorLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
collins-self committed Dec 26, 2024
1 parent 586c742 commit 68fe330
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ private static bool DataOperationExists(AdoDataConnection database) =>
/// </summary>
/// <param name="database">Database connection to use for creating the data operation</param>
private static void CreateDataOperation(AdoDataConnection database) =>
database.ExecuteNonQuery($"INSERT INTO DataOperation(Description, AssemblyName, TypeName, MethodName, Enabled) VALUES ('Power Calculation Validations', 'PowerCalculations.dll', '{typeof(PowerCalculationConfigurationValidation).FullName}', 'ValidatePowerCalculationConfigurations', 1)");
database.ExecuteNonQuery("INSERT INTO DataOperation(Description, AssemblyName, TypeName, MethodName, Enabled) " +
"VALUES ('Power Calculation Validations', 'PowerCalculations.dll', '{0}', 'ValidatePowerCalculationConfigurations', 1)", typeof(PowerCalculationConfigurationValidation).FullName);

/// <summary>
/// Returns true if a data operation exists to run this class. Returns false otherwise.
Expand All @@ -78,7 +79,9 @@ private static bool AdapterInstanceExists(AdoDataConnection database) =>
/// </summary>
/// <param name="database">Database connection to use for creating the data operation</param>
private static void CreateAdapterInstance(AdoDataConnection database) =>
database.ExecuteNonQuery($"INSERT INTO CustomActionAdapter(NodeID, AdapterName, AssemblyName, TypeName, ConnectionString, Enabled) VALUES ('{ConfigurationFile.Current.Settings["systemSettings"]["NodeID"].ValueAs<Guid>()}', 'PHASOR!POWERCALC', 'PowerCalculations.dll', '{typeof(PowerMultiCalculatorAdapter).FullName}', 'FramesPerSecond=30; LagTime=5.0; LeadTime=3.0', 1)");
database.ExecuteNonQuery("INSERT INTO CustomActionAdapter(NodeID, AdapterName, AssemblyName, TypeName, ConnectionString, Enabled) " +
"VALUES ('{0}', 'PHASOR!POWERCALC', 'PowerCalculations.dll', '{1}', 'FramesPerSecond=30; LagTime=5.0; LeadTime=3.0', 1)",
ConfigurationFile.Current.Settings["systemSettings"]["NodeID"].ValueAs<Guid>(), typeof(PowerMultiCalculatorAdapter).FullName);

/// <summary>
/// Data operation to validate power calculation configuration. This method checks that input measurements and non-null output measurements exist, are enabled, and have the correct signal type.
Expand Down
29 changes: 14 additions & 15 deletions Source/Libraries/GSF.Core/Data/DataInserter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,12 @@ private bool ClearTable(Table table)
useTruncateTable = m_forceTruncateTable || (table.Parent.Parent.DataSourceType == DatabaseType.SQLServer && !table.ReferencedByForeignKeys);
}

if (useTruncateTable)
deleteSql = "TRUNCATE TABLE " + table.SQLEscapedName;
else
deleteSql = "DELETE FROM " + table.SQLEscapedName;

try
{
table.Connection.ExecuteNonQuery(deleteSql, Timeout);

if (useTruncateTable)
table.Connection.ExecuteNonQuery("TRUNCATE TABLE {0}", table.SQLEscapedName, Timeout);
else
table.Connection.ExecuteNonQuery("DELETE FROM {0}", table.SQLEscapedName, Timeout);
if ((object)TableCleared != null)
TableCleared(this, new EventArgs<string>(table.Name)); //-V3083

Expand All @@ -437,7 +434,8 @@ private bool ClearTable(Table table)
return ClearTable(table);
}

OnSQLFailure(deleteSql, ex);
if (useTruncateTable) OnSQLFailure("TRUNCATE TABLE " + table.SQLEscapedName, ex);
else OnSQLFailure("DELETE FROM " + table.SQLEscapedName, ex);
}

return false;
Expand All @@ -453,22 +451,23 @@ private void ResetAutoIncValues(Table table)
{
case DatabaseType.SQLServer:
resetAutoIncValueSQL = "DBCC CHECKIDENT('" + table.SQLEscapedName + "', RESEED)";
table.Connection.ExecuteNonQuery(resetAutoIncValueSQL, Timeout);
table.Connection.ExecuteNonQuery("DBCC CHECKIDENT('{0}', RESEED)", table.SQLEscapedName, Timeout);
break;
case DatabaseType.MySQL:
resetAutoIncValueSQL = "ALTER TABLE " + table.SQLEscapedName + " AUTO_INCREMENT = 1";
table.Connection.ExecuteNonQuery(resetAutoIncValueSQL, Timeout);
table.Connection.ExecuteNonQuery("ALTER TABLE {0} AUTO_INCREMENT = 1", table.SQLEscapedName, Timeout);
break;
case DatabaseType.SQLite:
resetAutoIncValueSQL = "DELETE FROM sqlite_sequence WHERE name = '" + table.Name + "'";
table.Connection.ExecuteNonQuery(resetAutoIncValueSQL, Timeout);
table.Connection.ExecuteNonQuery("DELETE FROM sqlite_sequence WHERE name = '{0}'", table.Name, Timeout);
break;
case DatabaseType.PostgreSQL:
// The escaping of names here is very deliberate; for certain table names,
// it is necessary to escape the table name in the pg_get_serial_sequence() call,
// but the call will fail if you attempt to escape the autoIncField name
resetAutoIncValueSQL = $"SELECT setval(pg_get_serial_sequence('{table.SQLEscapedName}', '{table.AutoIncField.Name.ToLower()}'), (SELECT MAX({table.AutoIncField.SQLEscapedName}) FROM {table.SQLEscapedName}))";
table.Connection.ExecuteNonQuery(resetAutoIncValueSQL, Timeout);
table.Connection.ExecuteNonQuery("SELECT setval(pg_get_serial_sequence('{0}', '{1}'), (SELECT MAX({2}) FROM {3}))",
table.SQLEscapedName, table.AutoIncField.Name.ToLower(), table.AutoIncField.SQLEscapedName, table.SQLEscapedName, Timeout);
break;
}
}
Expand Down Expand Up @@ -616,7 +615,7 @@ private void ExecuteInserts(Table fromTable, Table toTable)
case DatabaseType.SQLServer:
try
{
toTable.Connection.ExecuteNonQuery("SET IDENTITY_INSERT " + toTable.SQLEscapedName + " ON", Timeout);
toTable.Connection.ExecuteNonQuery("SET IDENTITY_INSERT {0} ON", toTable.SQLEscapedName, Timeout);
usingIdentityInsert = true;
}
catch
Expand Down Expand Up @@ -677,7 +676,7 @@ private void ExecuteInserts(Table fromTable, Table toTable)
try
{
// Turn off identity inserts
toTable.Connection.ExecuteNonQuery(setIndentityInsertSQL, Timeout);
toTable.Connection.ExecuteNonQuery("SET IDENTITY_INSERT {0} OFF", toTable.SQLEscapedName, Timeout);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -833,7 +832,7 @@ private void InsertDestinationRecord(Table toTable, Fields fieldCollection, stri
int currentIdentityValue = int.Parse(Common.ToNonNullString(toTable.Connection.ExecuteScalar(toTable.IdentitySQL, Timeout), "0"));

// Delete record which was just inserted
toTable.Connection.ExecuteNonQuery("DELETE FROM " + toTable.SQLEscapedName + " WHERE " + autoIncField.SQLEscapedName + " = " + currentIdentityValue, Timeout);
toTable.Connection.ExecuteNonQuery("DELETE FROM {0} WHERE {1} = {2}", toTable.SQLEscapedName, autoIncField.SQLEscapedName, currentIdentityValue, Timeout);

// For very long spans of auto-inc identity gaps we at least provide some level of feedback
if (synchronizations++ % 50 == 0)
Expand Down
2 changes: 1 addition & 1 deletion Source/Libraries/GSF.Core/ErrorManagement/ErrorLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1553,7 +1553,7 @@ private void m_tableSizeCurtailmentTimer_Elapsed(object sender, System.Timers.El
if (errorLogSize >= m_databaseLogSize)
{
executingTransaction = true;
command.ExecuteNonQuery("DELETE FROM ErrorLog WHERE ID <= " + (int)(errorLogSize / 4 + minID), 60);
command.ExecuteNonQuery("DELETE FROM ErrorLog WHERE ID <= {0}", (int)(errorLogSize / 4 + minID), 60);
transaction.Commit();
}
}
Expand Down

0 comments on commit 68fe330

Please sign in to comment.