#pragma warning disable 1591 using System; using System.Collections.Generic; using System.Runtime.Serialization; using System.Threading.Tasks; using Microsoft.Extensions.Configuration; using MySqlConnector; using de.hottis.genericdatabaseapiservice.Models; namespace de.hottis.genericdatabaseapiservice.Services { public interface IDbService { Task> ReadBySelect(string selectStatement, bool justOne, TIN input); } public class DbServiceException : Exception {} public class NotDataFoundException : DbServiceException {} public class TooMuchDataFoundException : 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> ReadBySelect(string selectStatement, bool justOne, TIN input) { var itemList = new List(); 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(); 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 (reader.IsDBNull(ordinal)) { propertyInfo.SetValue(item, null); Console.WriteLine("Output Value: null"); } else if (propertyInfo.PropertyType == typeof(System.String)) { propertyInfo.SetValue(item, reader.GetString(ordinal)); Console.WriteLine("Output Value:{0}", reader.GetString(ordinal)); } else if (propertyInfo.PropertyType == typeof(System.Int32)) { propertyInfo.SetValue(item, reader.GetInt32(ordinal)); Console.WriteLine("Output Value:{0}", reader.GetInt32(ordinal)); } } itemList.Add(item); } } } } if (itemList.Count == 0) { throw new NotDataFoundException(); } if (justOne && itemList.Count > 1) { throw new TooMuchDataFoundException(); } return itemList; } } } #pragma warning restore 1591