Skip to content

Commit

Permalink
ExecuteScalar
Browse files Browse the repository at this point in the history
  • Loading branch information
collins-self committed Dec 23, 2024
1 parent 220c77e commit 3945e52
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Source/Libraries/GSF.Core/Data/DataInserter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ private void InsertDestinationRecord(Table toTable, Fields fieldCollection, stri
// Added check to preserve ID number for auto-inc fields
if (!usingIdentityInsert && !skipKeyValuePreservation && m_preserveAutoIncValues && (object)autoIncField != null)
{
int toTableRowCount = int.Parse(Common.ToNonNullString(toTable.Connection.ExecuteScalar("SELECT MAX(" + autoIncField.SQLEscapedName + ") FROM " + toTable.SQLEscapedName, Timeout), "0")) + 1;
int toTableRowCount = int.Parse(Common.ToNonNullString(toTable.Connection.ExecuteScalar("SELECT MAX({0}) FROM {1}", autoIncField.SQLEscapedName, toTable.SQLEscapedName, Timeout), "0")) + 1;
int sourceTablePrimaryFieldValue = int.Parse(Common.ToNonNullString(autoIncField.Value, "0"));
int synchronizations = 0;

Expand Down Expand Up @@ -884,7 +884,7 @@ private void InsertOrUpdate(Table toTable, StringBuilder insertSQL, StringBuilde
try
{
// If record already exists due to triggers or other means we must update it instead of inserting it
if (int.Parse(Common.ToNonNullString(toTable.Connection.ExecuteScalar(countSQL.ToString(), Timeout), "0")) > 0)
if (int.Parse(Common.ToNonNullString(toTable.Connection.ExecuteScalar("{0}", countSQL.ToString(), Timeout), "0")) > 0)
{
// Add where criteria to SQL update statement
updateSQL.Append(whereSQL);
Expand Down
13 changes: 8 additions & 5 deletions Source/Libraries/GSF.Core/Data/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2780,30 +2780,33 @@ public void Analyze()
try
{
// Make sure table exists
m_schemaConnection.ExecuteScalar("SELECT COUNT(*) FROM " + table.SQLEscapedName);
m_schemaConnection.ExecuteScalar("SELECT COUNT(*) FROM {0}", table.SQLEscapedName);

List<Field> fieldsToRemove = new List<Field>();
string testFieldSQL;
string sqlEscapedName = table.SQLEscapedName;
string sqlAutoIncEscapedName = table.AutoIncField.SQLEscapedName;

try
{
// If table has an auto-inc field, this will typically be indexed and will allow for a faster field check than a count
if (table.HasAutoIncField)
testFieldSQL = "SELECT {0} FROM " + table.SQLEscapedName + " WHERE " + table.AutoIncField.SQLEscapedName + " < 0";
testFieldSQL = "SELECT {0} FROM " + sqlEscapedName + " WHERE " + sqlAutoIncEscapedName + " < 0";
else
testFieldSQL = "SELECT COUNT({0}) FROM " + table.SQLEscapedName;
testFieldSQL = "SELECT COUNT({0}) FROM " + sqlEscapedName;
}
catch
{
testFieldSQL = "SELECT COUNT({0}) FROM " + table.SQLEscapedName;
testFieldSQL = "SELECT COUNT({0}) FROM " + sqlEscapedName;
}

foreach (Field field in table.Fields)
{
try
{
string fieldEscapedName = field.SQLEscapedName;
// Make sure field exists
m_schemaConnection.ExecuteScalar(string.Format(testFieldSQL, field.SQLEscapedName));
m_schemaConnection.ExecuteScalar(string.Format(testFieldSQL, fieldEscapedName));
}
catch
{
Expand Down
4 changes: 2 additions & 2 deletions Source/Libraries/GSF.Core/ErrorManagement/ErrorLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1544,8 +1544,8 @@ private void m_tableSizeCurtailmentTimer_Elapsed(object sender, System.Timers.El
command.Transaction = transaction;

// Get min and max IDs from error log
int minID = Convert.ToInt32(command.ExecuteScalar(string.Format("SELECT MIN(ID) FROM ErrorLog")));
int maxID = Convert.ToInt32(command.ExecuteScalar(string.Format("SELECT MAX(ID) FROM ErrorLog")));
int minID = Convert.ToInt32(command.ExecuteScalar("SELECT MIN(ID) FROM ErrorLog"));
int maxID = Convert.ToInt32(command.ExecuteScalar("SELECT MAX(ID) FROM ErrorLog"));
int errorLogSize = maxID - minID; // Roughly, assuming no manual deletions

// When exception log is larger than desired size - delete roughly 25% of the records,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,8 @@ private static void GetDeleteMeasurementDetails(AdoDataConnection database, int
string deviceName = outputDeviceRecord.Field<string>("Acronym");
adapterID = outputDeviceRecord.ConvertField<int>("AdapterID");

string analogPointTag = database.Connection.ExecuteScalar(string.Format(measurementDetailFormat, deviceName, labelName)).ToNonNullString();
analogSignalReference = database.Connection.ExecuteScalar(string.Format(outputMeasurementDetailFormat, analogPointTag)).ToNonNullString();
string analogPointTag = database.Connection.ExecuteScalar(measurementDetailFormat, deviceName, labelName).ToNonNullString();
analogSignalReference = database.Connection.ExecuteScalar(outputMeasurementDetailFormat, analogPointTag).ToNonNullString();
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ private static void GetDeleteMeasurementDetails(AdoDataConnection database, int
string deviceName = outputDeviceRecord.Field<string>("Acronym");
adapterID = outputDeviceRecord.ConvertField<int>("AdapterID");

string digitalPointTag = database.Connection.ExecuteScalar(string.Format(measurementDetailFormat, deviceName, labelName)).ToNonNullString();
digitalSignalReference = database.Connection.ExecuteScalar(string.Format(outputMeasurementDetailFormat, digitalPointTag)).ToNonNullString();
string digitalPointTag = database.Connection.ExecuteScalar(measurementDetailFormat, deviceName, labelName).ToNonNullString();
digitalSignalReference = database.Connection.ExecuteScalar(outputMeasurementDetailFormat, digitalPointTag).ToNonNullString();
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,11 +540,11 @@ private static void GetDeleteMeasurementDetails(AdoDataConnection database, int
string deviceName = outputDeviceRecord.Field<string>("Acronym");
adapterID = outputDeviceRecord.ConvertField<int>("AdapterID");

string anglePointTag = database.Connection.ExecuteScalar(string.Format(measurementDetailFormat, deviceName, labelName, "PA")).ToNonNullString();
angleSignalReference = database.Connection.ExecuteScalar(string.Format(outputMeasurementDetailFormat, anglePointTag)).ToNonNullString();
string anglePointTag = database.Connection.ExecuteScalar(measurementDetailFormat, deviceName, labelName, "PA").ToNonNullString();
angleSignalReference = database.Connection.ExecuteScalar(outputMeasurementDetailFormat, anglePointTag).ToNonNullString();

string magnitudePointTag = database.Connection.ExecuteScalar(string.Format(measurementDetailFormat, deviceName, labelName, "PM")).ToNonNullString();
magnitudeSignalReference = database.Connection.ExecuteScalar(string.Format(outputMeasurementDetailFormat, magnitudePointTag)).ToNonNullString();
string magnitudePointTag = database.Connection.ExecuteScalar(measurementDetailFormat, deviceName, labelName, "PM").ToNonNullString();
magnitudeSignalReference = database.Connection.ExecuteScalar(outputMeasurementDetailFormat, magnitudePointTag).ToNonNullString();
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,7 @@ private ulong GetLatestVersion(ulong currentVersion)
{
try
{
string query = $"SELECT CASE WHEN COUNT(ID) = 0 THEN {currentVersion} ELSE MAX(ID) END FROM TrackedChange";
version = Convert.ToUInt64(database.Connection.ExecuteScalar(query));
version = Convert.ToUInt64(database.Connection.ExecuteScalar("SELECT CASE WHEN COUNT(ID) = 0 THEN {0} ELSE MAX(ID) END FROM TrackedChange", currentVersion));
}
catch
{
Expand All @@ -511,8 +510,7 @@ private bool TrackedChangesAreValid(ulong currentVersion)
{
try
{
string query = $"SELECT COUNT(ID) FROM TrackedChange WHERE ID < {currentVersion}";
changesAreValid = Convert.ToInt32(database.Connection.ExecuteScalar(query)) == 0;
changesAreValid = Convert.ToInt32(database.Connection.ExecuteScalar("SELECT COUNT(ID) FROM TrackedChange WHERE ID < {0}", currentVersion)) == 0;
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ private string GetSystemName()

using AdoDataConnection database = new("systemSettings");

return database.Connection.ExecuteScalar($"SELECT Name FROM Node WHERE ID = '{database.Guid(GetNodeID())}'").ToNonNullString().ToUpper();
return database.Connection.ExecuteScalar("SELECT Name FROM Node WHERE ID = '{0}'", database.Guid(GetNodeID())).ToNonNullString().ToUpper();
}

private void RestartReloadStatisticsTimer()
Expand Down
14 changes: 7 additions & 7 deletions Source/Libraries/GSF.TimeSeries/TimeSeriesStartupOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,11 @@ private static void ValidateDataPublishers(AdoDataConnection database, string no
sttpsDataPublisherEnabled = value.ParseBoolean();
}

int internalDataPublisherCount = Convert.ToInt32(database.Connection.ExecuteScalar(string.Format(DataPublisherCountFormat, "INTERNAL", nodeIDQueryString)));
int externalDataPublisherCount = Convert.ToInt32(database.Connection.ExecuteScalar(string.Format(DataPublisherCountFormat, "EXTERNAL", nodeIDQueryString)));
int tlsDataPublisherCount = Convert.ToInt32(database.Connection.ExecuteScalar(string.Format(DataPublisherCountFormat, "TLS", nodeIDQueryString)));
int sttpDataPublisherCount = Convert.ToInt32(database.Connection.ExecuteScalar(string.Format(DataPublisherCountFormat, "STTP", nodeIDQueryString)));
int sttpsDataPublisherCount = Convert.ToInt32(database.Connection.ExecuteScalar(string.Format(DataPublisherCountFormat, "STTPS", nodeIDQueryString)));
int internalDataPublisherCount = Convert.ToInt32(database.Connection.ExecuteScalar(DataPublisherCountFormat, "INTERNAL", nodeIDQueryString));
int externalDataPublisherCount = Convert.ToInt32(database.Connection.ExecuteScalar(DataPublisherCountFormat, "EXTERNAL", nodeIDQueryString));
int tlsDataPublisherCount = Convert.ToInt32(database.Connection.ExecuteScalar(DataPublisherCountFormat, "TLS", nodeIDQueryString));
int sttpDataPublisherCount = Convert.ToInt32(database.Connection.ExecuteScalar(DataPublisherCountFormat, "STTP", nodeIDQueryString));
int sttpsDataPublisherCount = Convert.ToInt32(database.Connection.ExecuteScalar(DataPublisherCountFormat, "STTPS", nodeIDQueryString));

if (internalDataPublisherCount == 0)
database.Connection.ExecuteNonQuery(string.Format(GEPDataPublisherInsertFormat, nodeIDQueryString, "INTERNAL", "None", "cacheMeasurementKeys={FILTER ActiveMeasurements WHERE SignalType = ''STAT''}", internalDataPublisherEnabled ? 1 : 0));
Expand Down Expand Up @@ -684,8 +684,8 @@ private static void ValidateStatistics(AdoDataConnection database, string nodeID
int statConfigEntityCount = Convert.ToInt32(database.Connection.ExecuteScalar(StatConfigEntityCountFormat));
int statSignalTypeCount = Convert.ToInt32(database.Connection.ExecuteScalar(StatSignalTypeCountFormat));

int statHistorianCount = Convert.ToInt32(database.Connection.ExecuteScalar(string.Format(StatHistorianCountFormat, nodeIDQueryString)));
int statEngineCount = Convert.ToInt32(database.Connection.ExecuteScalar(string.Format(StatEngineCountFormat, nodeIDQueryString)));
int statHistorianCount = Convert.ToInt32(database.Connection.ExecuteScalar(StatHistorianCountFormat, nodeIDQueryString));
int statEngineCount = Convert.ToInt32(database.Connection.ExecuteScalar(StatEngineCountFormat, nodeIDQueryString));
int systemStatCount = Convert.ToInt32(database.Connection.ExecuteScalar(SystemStatCountFormat));
int deviceStatCount = Convert.ToInt32(database.Connection.ExecuteScalar(DeviceStatCountFormat));
int subscriberStatCount = Convert.ToInt32(database.Connection.ExecuteScalar(SubscriberStatCountFormat));
Expand Down
2 changes: 1 addition & 1 deletion Source/Libraries/GSF.TimeSeries/Transport/DataPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3069,7 +3069,7 @@ protected virtual DataSet AquireMetadata(ClientConnection connection, Dictionary
DataSet metadata = new();

// Initialize active node ID
Guid nodeID = Guid.Parse(dbConnection.ExecuteScalar($"SELECT NodeID FROM IaonActionAdapter WHERE ID = {ID}").ToString());
Guid nodeID = Guid.Parse(dbConnection.ExecuteScalar("SELECT NodeID FROM IaonActionAdapter WHERE ID = {0}", ID).ToString());

// Determine whether we're sending internal and external meta-data
bool sendExternalMetadata = connection.OperationalModes.HasFlag(OperationalModes.ReceiveExternalMetadata);
Expand Down
2 changes: 1 addition & 1 deletion Source/Tools/CSVDataManager/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ private void ExportSelectionToFile(Table table, Field[] fields)
{
writer.WriteLine(csvHeader);

object result = DBSchema.Connection.ExecuteScalar($"SELECT COUNT(*) FROM {table.SQLEscapedName}");
object result = DBSchema.Connection.ExecuteScalar("SELECT COUNT(*) FROM {0}", table.SQLEscapedName);
int count = Convert.ToInt32(result);
UpdateProgressBar(ExportProgressBar, 0);

Expand Down

0 comments on commit 3945e52

Please sign in to comment.