111 lines
5.6 KiB
C#
111 lines
5.6 KiB
C#
#pragma warning disable 1591
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.Serialization;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Configuration;
|
|
using MySqlConnector;
|
|
|
|
// 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 {
|
|
public interface IDbService {
|
|
Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string selectStatement, bool justOne, TIN input);
|
|
}
|
|
|
|
public class DbServiceException : Exception {}
|
|
public class NoDataFoundException : DbServiceException {}
|
|
public class TooMuchDataFoundException : DbServiceException {}
|
|
public class UnsupportedDataTypeException: DbServiceException {}
|
|
|
|
public class DbService : IDbService {
|
|
private readonly IConfiguration Configuration;
|
|
private string databaseConnInfo;
|
|
|
|
public DbService(IConfiguration configuration) {
|
|
Configuration = configuration;
|
|
databaseConnInfo = String.Format(
|
|
"Server={0};User ID={1};Password={2};Database={3}",
|
|
Configuration["Database:Host"],
|
|
Configuration["Database:User"],
|
|
Configuration["Database:Password"],
|
|
Configuration["Database:Name"]);
|
|
}
|
|
|
|
async public Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string selectStatement, bool justOne, TIN input) {
|
|
var itemList = new List<TOUT>();
|
|
|
|
Console.WriteLine("ConnInfo: {0}", databaseConnInfo);
|
|
Console.WriteLine("Statement: {0}", selectStatement);
|
|
|
|
using (var conn = new MySqlConnection(databaseConnInfo)) {
|
|
await conn.OpenAsync();
|
|
|
|
using (var cmd = conn.CreateCommand()) {
|
|
cmd.CommandText = selectStatement;
|
|
if (input != null){
|
|
foreach (var propertyInfo in typeof(TIN).GetProperties()) {
|
|
Console.WriteLine("Input Property name: {0} {1} ", propertyInfo.Name, propertyInfo.PropertyType);
|
|
var attributes = propertyInfo.GetCustomAttributes(typeof(DataMemberAttribute), true);
|
|
var dma = (DataMemberAttribute)attributes[0];
|
|
Console.WriteLine("Input DataMember name: {0} {1} ", dma.Name, dma.TypeId);
|
|
var value = propertyInfo.GetValue(input);
|
|
Console.WriteLine("Input Value: {0}", value);
|
|
cmd.Parameters.AddWithValue(dma.Name, propertyInfo.GetValue(input));
|
|
}
|
|
} else {
|
|
Console.WriteLine("no input data");
|
|
}
|
|
using (var reader = await cmd.ExecuteReaderAsync()) {
|
|
while (await reader.ReadAsync()) {
|
|
var item = Activator.CreateInstance<TOUT>();
|
|
foreach (var propertyInfo in typeof(TOUT).GetProperties()) {
|
|
Console.WriteLine("Output Property name: {0} {1} ", propertyInfo.Name, propertyInfo.PropertyType);
|
|
var attributes = propertyInfo.GetCustomAttributes(typeof(DataMemberAttribute), true);
|
|
var dma = (DataMemberAttribute)attributes[0];
|
|
int ordinal = reader.GetOrdinal(dma.Name);
|
|
Console.WriteLine("Output DataMember name: {0} {1} {2} ", dma.Name, dma.TypeId, ordinal);
|
|
if (await reader.IsDBNullAsync(ordinal)) {
|
|
propertyInfo.SetValue(item, null);
|
|
Console.WriteLine("Output Value: null");
|
|
} else if (propertyInfo.PropertyType == typeof(System.String)) {
|
|
var value = reader.GetString(ordinal);
|
|
propertyInfo.SetValue(item, value);
|
|
Console.WriteLine("Output Value:{0}", value);
|
|
} else if (propertyInfo.PropertyType == typeof(System.Int32) ||
|
|
propertyInfo.PropertyType == typeof(System.Nullable<System.Int32>)) {
|
|
var value = reader.GetInt32(ordinal);
|
|
propertyInfo.SetValue(item, value);
|
|
Console.WriteLine("Output Value:{0}", (System.Int32)value);
|
|
} else if (propertyInfo.PropertyType == typeof(System.DateTime)) {
|
|
var value = reader.GetDateTime(ordinal);
|
|
propertyInfo.SetValue(item, value);
|
|
Console.WriteLine("Output Value:{0}", value);
|
|
} else {
|
|
throw new UnsupportedDataTypeException();
|
|
}
|
|
}
|
|
itemList.Add(item);
|
|
Console.WriteLine("Item is {0}", item);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (itemList.Count == 0) {
|
|
throw new NoDataFoundException();
|
|
}
|
|
if (justOne && itemList.Count > 1) {
|
|
throw new TooMuchDataFoundException();
|
|
}
|
|
|
|
return itemList;
|
|
}
|
|
}
|
|
}
|
|
|
|
#pragma warning restore 1591
|