Skip to content

Commit

Permalink
DatabaseConfigurationLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
collins-self committed Dec 18, 2024
1 parent 6deaf6b commit 781e00e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
16 changes: 5 additions & 11 deletions Source/Libraries/GSF.PhasorProtocols/UI/DataModels/Phasor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,10 @@ public static IList<int> LoadKeys(AdoDataConnection database, int deviceID, stri

IList<int> phasorList = new List<int>();

string sortClause = string.Empty;

DataTable phasorTable;
if (!string.IsNullOrEmpty(sortMember))
sortClause = $"ORDER BY {sortMember} {sortDirection}";

string query = database.ParameterizedQueryString($"SELECT ID From PhasorDetail WHERE DeviceID = {{0}} {sortClause}", "deviceID");
DataTable phasorTable = database.Connection.RetrieveData(database.AdapterType, query, DefaultTimeout, deviceID);
phasorTable = database.Connection.RetrieveData(database.AdapterType, "SELECT ID From PhasorDetail WHERE DeviceID = {0} ORDER BY {1} {2}", DefaultTimeout, deviceID, sortMember, sortDirection);
else phasorTable = database.Connection.RetrieveData(database.AdapterType, "SELECT ID From PhasorDetail WHERE DeviceID = {0}", DefaultTimeout, deviceID);

foreach (DataRow row in phasorTable.Rows)
phasorList.Add(row.ConvertField<int>("ID"));
Expand Down Expand Up @@ -316,17 +313,14 @@ public static ObservableCollection<Phasor> Load(AdoDataConnection database, ILis
{
createdConnection = CreateConnection(ref database);

string query;
string commaSeparatedKeys;

Phasor[] phasorList = null;

if (keys is not null && keys.Count > 0)
{
commaSeparatedKeys = keys.Select(key => $"{key}").Aggregate((str1, str2) => $"{str1},{str2}");
query = $"SELECT ID, DeviceID, Label, Type, Phase, BaseKV, DestinationPhasorID, SourceIndex, CreatedBy, CreatedOn, UpdatedBy, UpdatedOn FROM Phasor WHERE ID IN ({commaSeparatedKeys})";

DataTable phasorTable = database.Connection.RetrieveData(database.AdapterType, query, DefaultTimeout);
DataTable phasorTable = database.Connection.RetrieveData(database.AdapterType, "SELECT ID, DeviceID, Label, Type, Phase, BaseKV, DestinationPhasorID, SourceIndex, CreatedBy, CreatedOn, UpdatedBy, UpdatedOn FROM Phasor WHERE ID IN ({0})", DefaultTimeout, commaSeparatedKeys);
phasorList = new Phasor[phasorTable.Rows.Count];

foreach (DataRow row in phasorTable.Rows)
Expand Down Expand Up @@ -573,7 +567,7 @@ public static Phasor GetPhasor(AdoDataConnection database, string whereClause)
try
{
createdConnection = CreateConnection(ref database);
DataTable phasorTable = database.Connection.RetrieveData(database.AdapterType, $"SELECT * FROM PhasorDetail {whereClause}");
DataTable phasorTable = database.Connection.RetrieveData(database.AdapterType, "SELECT * FROM PhasorDetail {0}", whereClause);

if (phasorTable.Rows.Count == 0)
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ private void ExecuteDataOperations(ulong trackingVersion = ulong.MinValue)
{
string assemblyName = "", typeName = "", methodName = "";

foreach (DataRow row in database.Connection.RetrieveData(database.AdapterType, $"SELECT * FROM DataOperation WHERE (NodeID IS NULL OR NodeID={NodeIDQueryString}) AND Enabled <> 0 ORDER BY LoadOrder").Rows)
foreach (DataRow row in database.Connection.RetrieveData(database.AdapterType, "SELECT * FROM DataOperation WHERE (NodeID IS NULL OR NodeID={0}) AND Enabled <> 0 ORDER BY LoadOrder", NodeIDQueryString).Rows)
{
try
{
Expand Down Expand Up @@ -410,7 +410,7 @@ private DataTable LoadTable(DataRow entityRow)
{
// Load configuration entity data filtered by node ID
Ticks operationStartTime = DateTime.UtcNow.Ticks;
DataTable source = database.Connection.RetrieveData(database.AdapterType, $"SELECT * FROM {entityRow["SourceName"]} WHERE NodeID={NodeIDQueryString}");
DataTable source = database.Connection.RetrieveData(database.AdapterType, "SELECT * FROM {0} WHERE NodeID={1}", entityRow["SourceName"], NodeIDQueryString);
Time operationElapsedTime = (DateTime.UtcNow.Ticks - operationStartTime).ToSeconds();

// Update table name as defined in configuration entity
Expand Down Expand Up @@ -526,7 +526,7 @@ private bool TrackedChangesAreValid(ulong currentVersion)
private DataTable GetTrackedChanges(ulong currentVersion)
{
DataTable table = null;
Execute(database => table = database.Connection.RetrieveData(database.AdapterType, $"SELECT * FROM TrackedChange WHERE ID > {currentVersion}"));
Execute(database => table = database.Connection.RetrieveData(database.AdapterType, "SELECT * FROM TrackedChange WHERE ID > {0}", currentVersion));
return table;
}

Expand All @@ -536,8 +536,8 @@ private DataTable GetChangedRecords(string tableName, string primaryKeyColumn, u

Execute(database =>
{
string query = $"SELECT * FROM {tableName} WHERE {primaryKeyColumn} IN (SELECT PrimaryKeyValue FROM TrackedChange WHERE TableName = '{tableName}' AND ID > {currentVersion}) AND NodeID = {NodeIDQueryString}";
changes = database.Connection.RetrieveData(database.AdapterType, query);
changes = database.Connection.RetrieveData(database.AdapterType, "SELECT * FROM {0} WHERE {1} IN " +
"(SELECT PrimaryKeyValue FROM TrackedChange WHERE TableName = '{0}' AND ID > {2}) AND NodeID = {3}", tableName, primaryKeyColumn, currentVersion, NodeIDQueryString);
});

return changes;
Expand Down

0 comments on commit 781e00e

Please sign in to comment.