read database info from file
This commit is contained in:
parent
1c3c0d101a
commit
046186fd7d
1
.gitignore
vendored
1
.gitignore
vendored
@ -7,4 +7,5 @@ tmp/
|
||||
*~
|
||||
.*~
|
||||
ENV.database
|
||||
databaseInfo.json
|
||||
|
||||
|
63
DbService.cs
63
DbService.cs
@ -1,19 +1,65 @@
|
||||
#pragma warning disable 1591
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MySqlConnector;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
// 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 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 interface IDbInfoService {
|
||||
DbInfoObject GetInfoByTag(string tag);
|
||||
string GetInfoStringByTag(string tag);
|
||||
}
|
||||
|
||||
public class DbInfoService : IDbInfoService {
|
||||
private readonly IConfiguration Configuration;
|
||||
private Dictionary<string, DbInfoObject> DbInfos;
|
||||
|
||||
public DbInfoService(IConfiguration configuration) {
|
||||
Configuration = configuration;
|
||||
Console.WriteLine("Database Infofile: {0}", Configuration["Database:InfoFile"]);
|
||||
DbInfos = JsonConvert.DeserializeObject<Dictionary<string, DbInfoObject>>(File.ReadAllText(Configuration["Database:InfoFile"]));
|
||||
}
|
||||
|
||||
public DbInfoObject GetInfoByTag(string tag) {
|
||||
return DbInfos[tag];
|
||||
}
|
||||
|
||||
public string GetInfoStringByTag(string tag) {
|
||||
return String.Format(
|
||||
"Server={0};User ID={1};Password={2};Database={3}",
|
||||
DbInfos[tag].Host,
|
||||
DbInfos[tag].User,
|
||||
DbInfos[tag].Password,
|
||||
DbInfos[tag].Name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface IDbService {
|
||||
Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string selectStatement, bool justOne, TIN input);
|
||||
Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string databaseTag, string selectStatement, bool justOne, TIN input);
|
||||
}
|
||||
|
||||
public class DbServiceException : Exception {}
|
||||
@ -23,21 +69,18 @@ namespace com.krohne.genericdatabaseapiservice.Services {
|
||||
|
||||
public class DbService : IDbService {
|
||||
private readonly IConfiguration Configuration;
|
||||
private string databaseConnInfo;
|
||||
private readonly IDbInfoService DbInfoService;
|
||||
|
||||
public DbService(IConfiguration configuration) {
|
||||
public DbService(IConfiguration configuration, IDbInfoService dbInfoService) {
|
||||
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"]);
|
||||
DbInfoService = dbInfoService;
|
||||
}
|
||||
|
||||
async public Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string selectStatement, bool justOne, TIN input) {
|
||||
async public Task<List<TOUT>> ReadBySelect<TIN, TOUT>(string databaseTag, string selectStatement, bool justOne, TIN input) {
|
||||
var itemList = new List<TOUT>();
|
||||
|
||||
var databaseConnInfo = DbInfoService.GetInfoStringByTag(databaseTag);
|
||||
|
||||
Console.WriteLine("ConnInfo: {0}", databaseConnInfo);
|
||||
Console.WriteLine("Statement: {0}", selectStatement);
|
||||
|
||||
|
8
databaseInfo.json-example
Normal file
8
databaseInfo.json-example
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"pdb_el_reader1": {
|
||||
"Host": "172.16.10.18",
|
||||
"User": "apiservicetestdb",
|
||||
"Password": "xxx",
|
||||
"Name": "apiservicetestdb"
|
||||
}
|
||||
}
|
@ -75,6 +75,7 @@ statementChecker = {
|
||||
'delete': re.compile('^delete', re.IGNORECASE),
|
||||
'post': re.compile('^insert', re.IGNORECASE)
|
||||
}
|
||||
databaseTagFinder = re.compile('DATABASETAGBEGIN (.*) DATABASETAGEND')
|
||||
|
||||
operations = []
|
||||
for path in apiDefinition['paths'].values():
|
||||
@ -100,6 +101,9 @@ for path in apiDefinition['paths'].values():
|
||||
statement = statementFinderResult.group(1)
|
||||
if not statementChecker[method].match(statement):
|
||||
raise Exception(f"Invalid statement {statement} for method {method}")
|
||||
databaseTagFinderResult = databaseTagFinder.search(description)
|
||||
if databaseTagFinderResult:
|
||||
databaseTag = databaseTagFinderResult.group(1)
|
||||
|
||||
bodyInputType = {}
|
||||
if 'requestBody' in operation:
|
||||
@ -124,6 +128,7 @@ for path in apiDefinition['paths'].values():
|
||||
'allSelector': operation['operationId'].endswith('all'),
|
||||
'byIdSelector': operation['operationId'].endswith('byid'),
|
||||
'statement': statement,
|
||||
'databaseTag': databaseTag,
|
||||
'bodyInputType': bodyInputType,
|
||||
'paramInputTypes': paramInputTypes
|
||||
})
|
||||
|
@ -90,7 +90,7 @@ if [ "$STAGE2" = "1" ]; then
|
||||
echo "patch DbService registering into generated startup code"
|
||||
sed -i output/src/$PACKAGE_NAME/Startup.cs \
|
||||
-e 's#\(using '$PACKAGE_NAME'.OpenApi;\)#\1\n\n// added by post-processor\nusing '$PACKAGE_NAME'.Services;\n#' \
|
||||
-e 's#^\([[:space:]]*\)\(// Add framework services.\)#\1// added by post-processor\n\1services.AddTransient<IDbService, DbService>();\n\n\1\2#' \
|
||||
-e 's#^\([[:space:]]*\)\(// Add framework services.\)#\1// added by post-processor\n\1services.AddSingleton<IDbInfoService, DbInfoService>();\n\1services.AddTransient<IDbService, DbService>();\n\n\1\2#' \
|
||||
-e 's#\(c.RoutePrefix = "\)openapi\(";\)#\1'$ROUTE_PREFIX'/api/doc\2#' \
|
||||
-e 's#\(c.SwaggerEndpoint("\)/openapi/2.0.0/openapi.json\(", "Generic Database API Service");\)#\1/'$ROUTE_PREFIX'/api/openapi/JSON\2#' \
|
||||
-e 's#\(c.RouteTemplate = "\)openapi/{documentName}/openapi.json\(";\)#\1'$ROUTE_PREFIX'/api/openapi/JSON\2#' \
|
||||
|
@ -26,6 +26,9 @@ paths:
|
||||
operationId: Regular.productionOrder
|
||||
summary: Returns productionOrder entries
|
||||
description:
|
||||
DATABASETAGBEGIN
|
||||
pdb_el_reader1
|
||||
DATABASETAGEND
|
||||
STATEMENTBEGIN
|
||||
SELECT produktionsauftrag AS productionOrderNumber,
|
||||
C_Nummer AS cgNumber,
|
||||
|
@ -86,7 +86,8 @@ ${operation['func']}InputType, #slurp
|
||||
#else
|
||||
Object, #slurp
|
||||
#end if
|
||||
$operation['resultType']['csName']>("#slurp
|
||||
$operation['resultType']['csName']>(#slurp
|
||||
"$operation['databaseTag']", "#slurp
|
||||
#if not $operation['statement']
|
||||
SELECT #slurp
|
||||
#set $sep = ""
|
||||
|
Loading…
x
Reference in New Issue
Block a user