Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sql fix #314

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open

Sql fix #314

wants to merge 25 commits into from

Conversation

clackner-gpa
Copy link
Member

No description provided.

if (TargetParentDevices)
newDevices = connection.RetrieveData("SELECT * FROM Device WHERE (IsConcentrator != 0 OR ParentID IS NULL) " +
"AND ID NOT IN (SELECT DeviceID FROM AlarmDevice)").Select();
else newDevices = connection.RetrieveData("SELECT * FROM Device WHERE IsConcentrator = 0 " +
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else newDevices = connection.RetrieveData("SELECT * FROM Device WHERE IsConcentrator = 0 " +
else
newDevices = connection.RetrieveData("SELECT * FROM Device WHERE IsConcentrator = 0 " +

@@ -939,11 +939,11 @@ private static void OptimizeLocalHistorianSettings(AdoDataConnection database, s
statusMessage("Optimizing settings for local historians...");

// Load the defined local system historians
IEnumerable<DataRow> historians = database.Connection.RetrieveData(database.AdapterType, $"SELECT AdapterName FROM RuntimeHistorian WHERE NodeID = {nodeIDQueryString} AND TypeName = 'HistorianAdapters.LocalOutputAdapter'").AsEnumerable();
IEnumerable<DataRow> readers = database.Connection.RetrieveData(database.AdapterType, $"SELECT * FROM CustomInputAdapter WHERE NodeID = {nodeIDQueryString} AND TypeName = 'HistorianAdapters.LocalInputAdapter'").AsEnumerable();
IEnumerable<DataRow> historians = database.Connection.RetrieveData(database.AdapterType, "SELECT AdapterName FROM RuntimeHistorian WHERE NodeID = {0} AND TypeName = 'HistorianAdapters.LocalOutputAdapter'", nodeIDQueryString).AsEnumerable();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nodeIDQueryString is almost definitely a Query so this won't work. We'd need to update/check wherever OptimizeLocalHistorianSettings is used

@@ -911,7 +911,7 @@ private Action<DataSet> GetSynchronizeMetadataAction()
List<int> sourceIndicies;

if (definedSourceIndicies.TryGetValue(id, out sourceIndicies))
command.ExecuteNonQuery(deletePhasorSql + $" AND SourceIndex NOT IN ({string.Join(",", sourceIndicies)})", MetadataSynchronizationTimeout, id);
command.ExecuteNonQuery(deletePhasorSql + " AND SourceIndex NOT IN ({0})", string.Join(",", sourceIndicies), MetadataSynchronizationTimeout, id);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this won't work either because it turns a list of [1,2,3] into a string "1,2,3" where 1 is not in ("1,2,3")

@@ -56,29 +56,32 @@ public static void ValidateDatabaseDefinitions()
/// <param name="database">Database connection to use for checking the data operation</param>
/// <returns>True or false indicating whether the operation exists</returns>
private static bool DataOperationExists(AdoDataConnection database) =>
Convert.ToInt32(database.ExecuteScalar($"SELECT COUNT(*) FROM DataOperation WHERE TypeName='{typeof(PowerCalculationConfigurationValidation).FullName}' AND MethodName='ValidatePowerCalculationConfigurations'")) > 0;
Convert.ToInt32(database.ExecuteScalar("SELECT COUNT(*) FROM DataOperation WHERE TypeName='{0}' AND MethodName='ValidatePowerCalculationConfigurations'", typeof(PowerCalculationConfigurationValidation).FullName)) > 0;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Convert.ToInt32(database.ExecuteScalar("SELECT COUNT(*) FROM DataOperation WHERE TypeName='{0}' AND MethodName='ValidatePowerCalculationConfigurations'", typeof(PowerCalculationConfigurationValidation).FullName)) > 0;
Convert.ToInt32(database.ExecuteScalar("SELECT COUNT(*) FROM DataOperation WHERE TypeName = {0} AND MethodName='ValidatePowerCalculationConfigurations'", typeof(PowerCalculationConfigurationValidation).FullName)) > 0;

Comment on lines +66 to +67
database.ExecuteNonQuery("INSERT INTO DataOperation(Description, AssemblyName, TypeName, MethodName, Enabled) " +
"VALUES ('Power Calculation Validations', 'PowerCalculations.dll', '{0}', 'ValidatePowerCalculationConfigurations', 1)", typeof(PowerCalculationConfigurationValidation).FullName);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
database.ExecuteNonQuery("INSERT INTO DataOperation(Description, AssemblyName, TypeName, MethodName, Enabled) " +
"VALUES ('Power Calculation Validations', 'PowerCalculations.dll', '{0}', 'ValidatePowerCalculationConfigurations', 1)", typeof(PowerCalculationConfigurationValidation).FullName);
database.ExecuteNonQuery("INSERT INTO DataOperation(Description, AssemblyName, TypeName, MethodName, Enabled) " +
"VALUES ('Power Calculation Validations', 'PowerCalculations.dll', {0}, 'ValidatePowerCalculationConfigurations', 1)", typeof(PowerCalculationConfigurationValidation).FullName);

@@ -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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter substitution does not work on Tables

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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter substitution does not work on Tables

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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter substitution does not work on Tables

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}))",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter substitution does not work on Tables

@@ -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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter substitution does not work on Tables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants