private bool Process ( DataBaseConnectionInfo connectionInfo , string storeProcedureAliasOrName , string httpMethod , JToken parameters , out JToken result , Func < IDataReader , Type // fieldType , string // fieldName , int // row index , int // column index , JProperty // JObject Field 对象 > onReadRowColumnProcessFunc = null , int commandTimeoutInSeconds = 90 ) { var r = false; result = null; var allowExecuteWhiteList = connectionInfo.AllowExecuteWhiteList; var storeProcedureName = string.Empty; if (allowExecuteWhiteList != null) { if (allowExecuteWhiteList.Count > 0) { r = CheckList ( allowExecuteWhiteList , storeProcedureAliasOrName , httpMethod , out StoreProcedureInfo storeProcedureInfo ); if (r) { storeProcedureName = storeProcedureInfo.Name; } } } else { r = true; } if (r) { r = Process ( connectionInfo.ConnectionString , connectionInfo.DataBaseType.ToString() , storeProcedureName , parameters , out result , onReadRowColumnProcessFunc , commandTimeoutInSeconds ); } return(r); }
GetDataBasesConnectionsInfoProcess ( string dbConnectionsJsonFile = "dbConnections.json" ) { var configurationBuilder = new ConfigurationBuilder() .AddJsonFile(dbConnectionsJsonFile); var configuration = configurationBuilder.Build(); var result = configuration .GetSection("Connections") .AsEnumerable() .Where ( (x) => { return (!x .Value .IsNullOrEmptyOrWhiteSpace()); } ) .GroupBy ( (x) => { var key = x.Key; var i = key.FindIndex(":", 2); var rr = key.Substring(0, i); return(rr); } ) .ToDictionary ( (x) => { var r = configuration[$"{x.Key}ConnectionID"]; return(r); } , (x) => { var allowExecuteWhiteList = configuration .GetSection($"{x.Key}WhiteList") .AsEnumerable() .Where ( (xx) => { var v = xx.Value; var rr = !v.IsNullOrEmptyOrWhiteSpace(); return(rr); } ) .GroupBy ( (xx) => { var key = xx.Key; var i = key.FindIndex(":", 4); var rr = key.Substring(0, i); return(rr); } ) .ToDictionary ( (xx) => { var key = configuration[$"{xx.Key}StoreProcedureAlias"]; var storeProcedureName = configuration[$"{xx.Key}StoreProcedureName"]; if (key.IsNullOrEmptyOrWhiteSpace()) { key = storeProcedureName; } return(key); } , (xx) => { var storeProcedureName = configuration[$"{xx.Key}StoreProcedureName"]; var s = configuration[$"{xx.Key}AllowedHttpMethods"]; var allowedHttpMethods = Enum .Parse <HttpMethodsFlags> ( s , true ); var rr = new StoreProcedureInfo() { Alias = xx.Key , Name = storeProcedureName , AllowedHttpMethods = allowedHttpMethods }; return (rr); } , StringComparer .OrdinalIgnoreCase ); //var connectionTimeoutInSeconds = 120; //int.TryParse // ( // configuration[$"{x.Key}ConnectionTimeoutInSeconds"] // , out connectionTimeoutInSeconds // ); var r = new DataBaseConnectionInfo() { ConnectionID = configuration[$"{x.Key}ConnectionID"] , ConnectionString = configuration[$"{x.Key}ConnectionString"] //, ConnectionTimeoutInSeconds = connectionTimeoutInSeconds , DataBaseType = Enum.Parse <DataBasesType>(configuration[$"{x.Key}DataBaseType"], true) , AllowExecuteWhiteList = allowExecuteWhiteList }; return(r); } , StringComparer .OrdinalIgnoreCase ); return(result); }
Process ( string connectionID //= "mssql" , string storeProcedureName , JToken parameters = null , Func < IDataReader , Type // fieldType , string // fieldName , int // row index , int // column index , JProperty // JObject Field 对象 > onReadRowColumnProcessFunc = null , string httpMethod = "Get" , int commandTimeoutInSeconds = 101 ) { var beginTime = DateTime.Now; JToken result = null; var r = false; int statusCode = 200; DataBaseConnectionInfo connectionInfo = null; r = _indexedConnections .TryGetValue ( connectionID , out connectionInfo ); if (r) { r = Process ( connectionInfo , storeProcedureName , httpMethod , parameters , out result , onReadRowColumnProcessFunc , commandTimeoutInSeconds ); } if (!r) { statusCode = 403; result = null; return(statusCode, result); } result["BeginTime"] = beginTime; var endTime = DateTime.Now; result["EndTime"] = endTime; result["DurationInMilliseconds"] = DateTimeHelper .MillisecondsDiff ( beginTime , endTime ); return(statusCode, result); }