string replacement, better exception names
This commit is contained in:
61
DbService.cs
61
DbService.cs
@ -71,14 +71,30 @@ namespace com.krohne.genericdatabaseapiservice.Services {
|
||||
|
||||
|
||||
public interface IDbService {
|
||||
Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string databaseTag, string selectStatement, bool justOne, TIN input);
|
||||
Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string databaseTag, string selectStatement, bool justOne, TIN input, bool bindingByStringReplacement);
|
||||
}
|
||||
|
||||
public class DbServiceException : Exception {}
|
||||
public class NoDataFoundException : DbServiceException {}
|
||||
public class TooMuchDataFoundException : DbServiceException {}
|
||||
public class UnsupportedDataTypeException: DbServiceException {}
|
||||
|
||||
public class DbServiceException : Exception {
|
||||
public DbServiceException(string msg): base(msg) { }
|
||||
public DbServiceException(): base() { }
|
||||
}
|
||||
public class NoDataFoundException : DbServiceException {
|
||||
public NoDataFoundException(string msg): base(msg) { }
|
||||
public NoDataFoundException(): base() { }
|
||||
}
|
||||
public class TooMuchDataFoundException : DbServiceException {
|
||||
public TooMuchDataFoundException(string msg): base(msg) { }
|
||||
public TooMuchDataFoundException(): base() { }
|
||||
}
|
||||
public class UnsupportedDataTypeException: DbServiceException {
|
||||
public UnsupportedDataTypeException(string msg): base(msg) { }
|
||||
public UnsupportedDataTypeException(): base() { }
|
||||
}
|
||||
public class StringReplacementNotAllowedException: DbServiceException {
|
||||
public StringReplacementNotAllowedException(string msg): base(msg) { }
|
||||
public StringReplacementNotAllowedException(): base() { }
|
||||
}
|
||||
|
||||
public class DbService : IDbService {
|
||||
private readonly IConfiguration Configuration;
|
||||
private readonly ILogger<DbService> Logger;
|
||||
@ -90,7 +106,7 @@ namespace com.krohne.genericdatabaseapiservice.Services {
|
||||
DbInfoService = dbInfoService;
|
||||
}
|
||||
|
||||
async public Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string databaseTag, string selectStatement, bool justOne, TIN input) {
|
||||
async public Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string databaseTag, string selectStatement, bool justOne, TIN input, bool bindingByStringReplacement) {
|
||||
var itemList = new List<TOUT>();
|
||||
|
||||
var databaseConnInfo = DbInfoService.GetInfoStringByTag(databaseTag);
|
||||
@ -101,8 +117,12 @@ namespace com.krohne.genericdatabaseapiservice.Services {
|
||||
using (var conn = new MySqlConnection(databaseConnInfo)) {
|
||||
await conn.OpenAsync();
|
||||
|
||||
bool commandTextSet = false;
|
||||
using (var cmd = conn.CreateCommand()) {
|
||||
cmd.CommandText = selectStatement;
|
||||
if (! bindingByStringReplacement) {
|
||||
cmd.CommandText = selectStatement;
|
||||
commandTextSet = true;
|
||||
}
|
||||
if (input != null){
|
||||
foreach (var propertyInfo in typeof(TIN).GetProperties()) {
|
||||
Logger.LogInformation("Input Property name: {0} {1} ", propertyInfo.Name, propertyInfo.PropertyType);
|
||||
@ -117,6 +137,9 @@ namespace com.krohne.genericdatabaseapiservice.Services {
|
||||
var p2 = new StringBuilder();
|
||||
var sep = "";
|
||||
foreach (var x in p1) {
|
||||
if (! (x is int || x is long)) {
|
||||
throw new StringReplacementNotAllowedException();
|
||||
}
|
||||
Logger.LogInformation("x: {0}", x);
|
||||
p2.Append(sep);
|
||||
p2.Append(x);
|
||||
@ -124,8 +147,21 @@ namespace com.krohne.genericdatabaseapiservice.Services {
|
||||
}
|
||||
var p3 = p2.ToString();
|
||||
Logger.LogInformation("Input Value: p3:{0} typ:{1} isList:{2}", p3, typ, isList);
|
||||
cmd.Parameters.AddWithValue(dma.Name, p3);
|
||||
if (bindingByStringReplacement) {
|
||||
if (commandTextSet) {
|
||||
throw new StringReplacementNotAllowedException("string replacement only allowed for numeric parameters");
|
||||
}
|
||||
var processedStatement = selectStatement.Replace(String.Format("@{0}", dma.Name), p3);
|
||||
cmd.CommandText = processedStatement;
|
||||
commandTextSet = true;
|
||||
Logger.LogInformation("Statement after replacement is {0}", processedStatement);
|
||||
} else {
|
||||
cmd.Parameters.AddWithValue(dma.Name, p3);
|
||||
}
|
||||
} else {
|
||||
if (bindingByStringReplacement) {
|
||||
throw new StringReplacementNotAllowedException("string replacement only allowed to handle lists of parameters");
|
||||
}
|
||||
Logger.LogInformation("Input Value: {0} {1} {2}", value, typ, isList);
|
||||
cmd.Parameters.AddWithValue(dma.Name, value);
|
||||
}
|
||||
@ -154,6 +190,11 @@ namespace com.krohne.genericdatabaseapiservice.Services {
|
||||
var value = reader.GetInt32(ordinal);
|
||||
propertyInfo.SetValue(item, value);
|
||||
Logger.LogInformation("Output Value:{0}", (System.Int32)value);
|
||||
} else if (propertyInfo.PropertyType == typeof(System.Int64) ||
|
||||
propertyInfo.PropertyType == typeof(System.Nullable<System.Int64>)) {
|
||||
var value = reader.GetInt64(ordinal);
|
||||
propertyInfo.SetValue(item, value);
|
||||
Logger.LogInformation("Output Value:{0}", (System.Int64)value);
|
||||
} else if (propertyInfo.PropertyType == typeof(System.DateTime)) {
|
||||
var value = reader.GetDateTime(ordinal);
|
||||
propertyInfo.SetValue(item, value);
|
||||
@ -164,7 +205,7 @@ namespace com.krohne.genericdatabaseapiservice.Services {
|
||||
propertyInfo.SetValue(item, value);
|
||||
Logger.LogInformation("Output Value:{0}", value);
|
||||
} else {
|
||||
throw new UnsupportedDataTypeException();
|
||||
throw new UnsupportedDataTypeException(String.Format("{0}", propertyInfo.PropertyType));
|
||||
}
|
||||
}
|
||||
itemList.Add(item);
|
||||
|
Reference in New Issue
Block a user