using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using Amazon.Runtime; using Amazon.DynamoDBv2.Model; using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.DocumentModel; using ServerCore; using ServerBase; namespace ServerBase; //============================================================================================== // QueryBatch 클래스의 주요 절차들을 처리해 주는 클래스 이다. //============================================================================================== public static class QueryHelper { //============================================================================================== // 1. DB 쿼리를 요청한다. // 2. 배치쿼리를 요청한다. //============================================================================================== public static async Task sendQueryAndBusinessLog(QueryBatchBase batch) { var result = new Result(); var err_msg = string.Empty; if (batch == null) { err_msg = $"Failed to DBQuery !!!, QueryBatchBase is null"; result.setFail(ServerErrorCode.FunctionInvalidParam, err_msg); return result; } Stopwatch? stopwatch = null; var event_tid = string.Empty; var server_logic = ServerLogicApp.getServerLogicApp(); ArgumentNullException.ThrowIfNull(server_logic); var server_config = server_logic.getServerConfig(); ArgumentNullException.ThrowIfNull(server_config); if (true == server_config.PerformanceCheckEnable) { event_tid = string.IsNullOrEmpty(batch.getTransId()) ? System.Guid.NewGuid().ToString("N") : batch.getTransId(); stopwatch = Stopwatch.StartNew(); } result = await sendQuery(batch); if(result.isSuccess()) { batch.sendBusinessLog(); } if (null != stopwatch) { var elapsed_msec = stopwatch.ElapsedMilliseconds; stopwatch.Stop(); if (elapsed_msec > Constant.STOPWATCH_LOG_LIMIT_MSEC) { Log.getLogger().debug($"QueryBatch - Total Stopwatch Stop : ETID:{event_tid}, ElapsedMSec:{elapsed_msec}, LogAction:{batch.getLogAction().getLogActionType()}"); } } return result; } //============================================================================================== // 1. DB 쿼리를 요청한다. // 2. 배치쿼리를 요청한다. //============================================================================================== private static async Task sendQuery(QueryBatchBase batch) { var result = new Result(); var err_msg = string.Empty; if (batch == null) { err_msg = $"Failed to sendQuery !!!, QueryBatchBase is null"; result.setFail(ServerErrorCode.FunctionInvalidParam, err_msg); return result; } if (false == batch.hasQuery()) { err_msg = $"Not has DBQuery !!!"; result.setFail(ServerErrorCode.DynamoDbQueryNoRequested, err_msg); return result; } result = await batch.prepareQueryWithStopwatch(); if (result.isFail()) { return result; } result = await batch.doQueryWithStopwatch(); if (result.isFail()) { return result; } return result; } } //============================================================================================== // 쿼리 관련 확장 함수들을 정의하는 클래스 이다. //============================================================================================== public static class QueryExtension { public static bool isSuccess(this CancellationReason reason) { return true == reason.Code.Equals(DynamoDbQueryExceptionNotifier.Success, StringComparison.OrdinalIgnoreCase); } public static string toExceptionString(this TransactionCanceledException e) { return $"[TransactionCanceledException] - exception:{e}"; } public static string toExceptionString(this AmazonDynamoDBException e) { var err_msg = $"[AmazonDynamoDBException] - occurred:{e.Message}, "; err_msg += (e.InnerException != null) ? $" Inner Exception:{e.InnerException.Message}," : ""; err_msg += $" exception:{e}"; return err_msg; } public static string toExceptionString(this AmazonServiceException e) { return $"[AmazonServiceException] - exception:{e}"; } public static bool isEqualQuery(this IQueryDbRequester dbRequester) where TDbRequestWithDocument : IQueryDbRequester { if(dbRequester.GetType() == typeof(TDbRequestWithDocument)) { return true; } return false; } }