168 lines
7.6 KiB
C#
Raw Normal View History

2021-11-25 14:49:08 +01:00
#pragma warning disable 1591
using System;
2021-12-06 17:46:58 +01:00
using System.IO;
using System.Collections.Generic;
2021-11-24 12:28:45 +01:00
using System.Runtime.Serialization;
using System.Threading.Tasks;
2021-11-25 17:48:50 +01:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
2021-11-23 18:38:08 +01:00
using MySqlConnector;
2021-12-06 17:46:58 +01:00
using Newtonsoft.Json;
2021-12-06 13:39:21 +01:00
// make sure to adjust the prefix with the PACKAGE_NAME from ENV
using com.krohne.genericdatabaseapiservice.Models;
// make sure to adjust the prefix with the PACKAGE_NAME from ENV
namespace com.krohne.genericdatabaseapiservice.Services {
2021-12-06 17:46:58 +01:00
public class DbInfoObject {
public DbInfoObject(string host, string user, string password, string name) {
Host = host;
User = user;
Password = password;
Name = name;
}
public string Host { get; set; }
public string User { get; set; }
public string Password { get; set; }
public string Name { get; set; }
}
public class DbInfoServiceException : Exception {}
public class UnknownDatabaseTagException: DbInfoServiceException {}
2021-12-06 17:46:58 +01:00
public interface IDbInfoService {
DbInfoObject GetInfoByTag(string tag);
string GetInfoStringByTag(string tag);
}
public class DbInfoService : IDbInfoService {
private readonly IConfiguration Configuration;
private readonly ILogger<DbInfoService> Logger;
2021-12-06 17:46:58 +01:00
private Dictionary<string, DbInfoObject> DbInfos;
public DbInfoService(IConfiguration configuration, ILogger<DbInfoService> logger) {
2021-12-06 17:46:58 +01:00
Configuration = configuration;
Logger = Logger;
2021-12-06 17:46:58 +01:00
Console.WriteLine("Database Infofile: {0}", Configuration["Database:InfoFile"]);
DbInfos = JsonConvert.DeserializeObject<Dictionary<string, DbInfoObject>>(File.ReadAllText(Configuration["Database:InfoFile"]));
}
public DbInfoObject GetInfoByTag(string tag) {
try {
return DbInfos[tag];
} catch (KeyNotFoundException) {
throw new UnknownDatabaseTagException();
}
2021-12-06 17:46:58 +01:00
}
public string GetInfoStringByTag(string tag) {
return String.Format(
"Server={0};User ID={1};Password={2};Database={3}",
GetInfoByTag(tag).Host,
GetInfoByTag(tag).User,
GetInfoByTag(tag).Password,
GetInfoByTag(tag).Name);
2021-12-06 17:46:58 +01:00
}
}
public interface IDbService {
2021-12-06 17:46:58 +01:00
Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string databaseTag, string selectStatement, bool justOne, TIN input);
}
public class DbServiceException : Exception {}
2021-12-02 15:57:58 +01:00
public class NoDataFoundException : DbServiceException {}
2021-11-30 19:09:22 +01:00
public class TooMuchDataFoundException : DbServiceException {}
2021-12-02 15:57:58 +01:00
public class UnsupportedDataTypeException: DbServiceException {}
2021-11-25 14:49:08 +01:00
public class DbService : IDbService {
2021-11-25 17:48:50 +01:00
private readonly IConfiguration Configuration;
private readonly ILogger<DbService> Logger;
2021-12-06 17:46:58 +01:00
private readonly IDbInfoService DbInfoService;
2021-11-25 17:48:50 +01:00
public DbService(IConfiguration configuration, ILogger<DbService> logger, IDbInfoService dbInfoService) {
2021-11-25 17:48:50 +01:00
Configuration = configuration;
Logger = logger;
2021-12-06 17:46:58 +01:00
DbInfoService = dbInfoService;
2021-11-25 17:48:50 +01:00
}
2021-12-06 17:46:58 +01:00
async public Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string databaseTag, string selectStatement, bool justOne, TIN input) {
2021-11-25 14:49:08 +01:00
var itemList = new List<TOUT>();
2021-11-23 18:38:08 +01:00
2021-12-06 17:46:58 +01:00
var databaseConnInfo = DbInfoService.GetInfoStringByTag(databaseTag);
Logger.LogDebug("ConnInfo: {0}", databaseConnInfo);
Logger.LogDebug("Statement: {0}", selectStatement);
2021-11-30 18:11:03 +01:00
2021-11-25 17:48:50 +01:00
using (var conn = new MySqlConnection(databaseConnInfo)) {
2021-11-23 18:38:08 +01:00
await conn.OpenAsync();
using (var cmd = conn.CreateCommand()) {
2021-11-25 14:49:08 +01:00
cmd.CommandText = selectStatement;
if (input != null){
foreach (var propertyInfo in typeof(TIN).GetProperties()) {
Logger.LogDebug("Input Property name: {0} {1} ", propertyInfo.Name, propertyInfo.PropertyType);
2021-11-25 14:49:08 +01:00
var attributes = propertyInfo.GetCustomAttributes(typeof(DataMemberAttribute), true);
var dma = (DataMemberAttribute)attributes[0];
Logger.LogDebug("Input DataMember name: {0} {1} ", dma.Name, dma.TypeId);
2021-11-25 14:49:08 +01:00
var value = propertyInfo.GetValue(input);
Logger.LogDebug("Input Value: {0}", value);
2021-11-25 14:49:08 +01:00
cmd.Parameters.AddWithValue(dma.Name, propertyInfo.GetValue(input));
}
} else {
Logger.LogDebug("no input data");
2021-11-25 14:49:08 +01:00
}
2021-11-23 18:38:08 +01:00
using (var reader = await cmd.ExecuteReaderAsync()) {
while (await reader.ReadAsync()) {
2021-11-25 14:49:08 +01:00
var item = Activator.CreateInstance<TOUT>();
foreach (var propertyInfo in typeof(TOUT).GetProperties()) {
Logger.LogDebug("Output Property name: {0} {1} ", propertyInfo.Name, propertyInfo.PropertyType);
2021-11-24 12:28:45 +01:00
var attributes = propertyInfo.GetCustomAttributes(typeof(DataMemberAttribute), true);
var dma = (DataMemberAttribute)attributes[0];
2021-11-30 18:44:06 +01:00
int ordinal = reader.GetOrdinal(dma.Name);
Logger.LogDebug("Output DataMember name: {0} {1} {2} ", dma.Name, dma.TypeId, ordinal);
2021-12-02 17:28:29 +01:00
if (await reader.IsDBNullAsync(ordinal)) {
2021-11-30 19:09:22 +01:00
propertyInfo.SetValue(item, null);
Logger.LogDebug("Output Value: null");
2021-11-30 19:09:22 +01:00
} else if (propertyInfo.PropertyType == typeof(System.String)) {
2021-12-02 15:57:58 +01:00
var value = reader.GetString(ordinal);
propertyInfo.SetValue(item, value);
Logger.LogDebug("Output Value:{0}", value);
2021-12-02 17:28:29 +01:00
} else if (propertyInfo.PropertyType == typeof(System.Int32) ||
propertyInfo.PropertyType == typeof(System.Nullable<System.Int32>)) {
2021-12-02 15:57:58 +01:00
var value = reader.GetInt32(ordinal);
propertyInfo.SetValue(item, value);
Logger.LogDebug("Output Value:{0}", (System.Int32)value);
2021-12-02 17:28:29 +01:00
} else if (propertyInfo.PropertyType == typeof(System.DateTime)) {
var value = reader.GetDateTime(ordinal);
propertyInfo.SetValue(item, value);
Logger.LogDebug("Output Value:{0}", value);
2021-12-02 15:57:58 +01:00
} else {
throw new UnsupportedDataTypeException();
}
2021-11-23 18:38:08 +01:00
}
itemList.Add(item);
Logger.LogDebug("Item is {0}", item);
2021-11-23 18:38:08 +01:00
}
}
}
}
if (itemList.Count == 0) {
Logger.LogWarning("no data found");
2021-12-02 15:57:58 +01:00
throw new NoDataFoundException();
}
2021-11-30 19:09:22 +01:00
if (justOne && itemList.Count > 1) {
Logger.LogWarning("too much data found");
2021-11-30 19:09:22 +01:00
throw new TooMuchDataFoundException();
}
return itemList;
}
}
2021-11-23 18:38:08 +01:00
}
2021-11-25 14:49:08 +01:00
#pragma warning restore 1591