Skip to content

Commit

Permalink
wip #53
Browse files Browse the repository at this point in the history
* added implementation for OracleWatcherPersistenceService
* adeed method in oracleHelper to execute procedures
  • Loading branch information
mrksoftware committed Mar 20, 2020
1 parent 8978e8c commit d0d0e6d
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using Oracle.ManagedDataAccess.Client;
using Elfo.Firmenich.Wardein.Abstractions.Watchers;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Threading.Tasks;

namespace Elfo.Wardein.Core.Helpers
Expand Down Expand Up @@ -43,6 +46,63 @@ public IEnumerable<T> Query<T>(string query, IDictionary<string, object> paramet
}
}

public Task<T> CallProcedureAsync<T>(string packageName, string procedureName, Func<OracleParameter[], T> howToGetResult, params OracleParameter[] parameters)
{
try
{
using (var connection = new OracleConnection(oracleConfiguration.ConnectionString))
{
connection.Open();
using (var transaction = connection.BeginTransaction())
{
connection.SetSessionInfoForTransaction(oracleConfiguration);
using (DbCommand command = new OracleCommand())
{
command.Connection = connection;

ReinitializeSession(command);

command.CommandType = CommandType.StoredProcedure;
command.CommandText = string.Concat(packageName, ".", procedureName);

((OracleCommand)command).BindByName = true;

foreach (var parameter in parameters)
{
command.Parameters.Add(parameter);
}

command.ExecuteNonQuery();

if (parameters.Count() > 0)
{
return Task.FromResult(
howToGetResult(
parameters.Where(x =>
x.Direction == ParameterDirection.Output || x.Direction == ParameterDirection.InputOutput
).ToArray()
)
);
}
else
return Task.FromResult(default(T));
}
}
}
}
catch (Exception exception)
{
throw exception;
}
}

private void ReinitializeSession(IDbCommand command)
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "reinitialize_session";
command.ExecuteNonQuery();
}

public int Execute(string command, IDictionary<string, object> parameters = null)
{

Expand Down
28 changes: 26 additions & 2 deletions Elfo.Wardein.Core/Persistence/OracleWatcherPersistenceService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Elfo.Firmenich.Wardein.Abstractions.Watchers;
using Elfo.Wardein.Core.Helpers;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -10,15 +12,37 @@ namespace Elfo.Firmenich.Wardein.Core.Persistence
public class OracleWatcherPersistenceService : IAmWatcherPersistenceService
{
private readonly OracleConnectionConfiguration oracleConnectionConfiguration;
private readonly OracleHelper oracleHelper;

public OracleWatcherPersistenceService(OracleConnectionConfiguration oracleConnectionConfiguration)
{
this.oracleConnectionConfiguration = oracleConnectionConfiguration;
this.oracleHelper = new OracleHelper(oracleConnectionConfiguration);
}

public Task<WatcherStatusResult> UpsertCurrentStatus(int watcherConfigurationId, int applicationId, string applicationHostname, bool isHealthy, Exception failureException = null)
public async Task<WatcherStatusResult> UpsertCurrentStatus(int watcherConfigurationId, int applicationId, string applicationHostname, bool isHealthy, Exception failureException = null)
{
throw new NotImplementedException();
return await this.oracleHelper.CallProcedureAsync<WatcherStatusResult>(
packageName: "PKG_WRD",
procedureName: "PRC_UPS_WTCH_RES",
howToGetResult: (parameters) =>
{
int.TryParse(parameters.FirstOrDefault(x => x.ParameterName == "po_flr_count")?.Value?.ToString(), out int errorCount);
bool wasHealthy = parameters.FirstOrDefault(x => x.ParameterName == "po_prv_status")?.Value?.ToString()?.ToUpperInvariant() == "Y";
return new WatcherStatusResult()
{
FailureCount = errorCount,
PreviousStatus = wasHealthy
};
},
new OracleParameter("p_wtchr_cnfg_id", OracleDbType.Int32, watcherConfigurationId, System.Data.ParameterDirection.Input),
new OracleParameter("p_appl_id", OracleDbType.Int32, applicationId, System.Data.ParameterDirection.Input),
new OracleParameter("p_hostname", OracleDbType.Varchar2, applicationId, System.Data.ParameterDirection.Input),
new OracleParameter("p_is_healthy", OracleDbType.Varchar2, applicationId, System.Data.ParameterDirection.Input),
new OracleParameter("p_flr_msg", OracleDbType.Varchar2, applicationId, System.Data.ParameterDirection.Input),
new OracleParameter("po_flr_count", OracleDbType.Int32, applicationId, System.Data.ParameterDirection.Output),
new OracleParameter("po_prv_status", OracleDbType.Varchar2, applicationId, System.Data.ParameterDirection.Output)
);
}
}
}

0 comments on commit d0d0e6d

Please sign in to comment.