/// <summary> /// Query datas /// </summary> /// <typeparam name="T">Data type</typeparam> /// <param name="server">Database server</param> /// <param name="command">Command</param> /// <returns>Return datas</returns> public async Task <IEnumerable <T> > QueryAsync <T>(DatabaseServer server, ICommand command) { if (command.Query == null) { throw new EZNEWException($"{nameof(ICommand.Query)} is null"); } #region query translation IQueryTranslator translator = PostgreSqlManager.GetQueryTranslator(DataAccessContext.Create(server, command)); var tranResult = translator.Translate(command.Query); string joinScript = tranResult.AllowJoin ? tranResult.JoinScript : string.Empty; #endregion #region script string cmdText; switch (command.Query.ExecutionMode) { case QueryExecutionMode.Text: cmdText = tranResult.ConditionString; break; case QueryExecutionMode.QueryObject: default: int size = command.Query.QuerySize; string objectName = translator.DataAccessContext.GetCommandEntityObjectName(command); string sortString = string.IsNullOrWhiteSpace(tranResult.SortString) ? string.Empty : $"ORDER BY {tranResult.SortString}"; var queryFields = PostgreSqlManager.GetQueryFields(command.Query, command.EntityType, true); string outputFormatedField = string.Join(",", PostgreSqlManager.FormatQueryFields(translator.ObjectPetName, queryFields, true)); if (string.IsNullOrWhiteSpace(tranResult.CombineScript)) { cmdText = $"{tranResult.PreScript}SELECT {outputFormatedField} FROM {PostgreSqlManager.WrapKeyword(objectName)} AS {translator.ObjectPetName} {joinScript} {(string.IsNullOrWhiteSpace(tranResult.ConditionString) ? string.Empty : $"WHERE {tranResult.ConditionString}")} {sortString} {(size > 0 ? $"LIMIT {size} OFFSET 0" : string.Empty)}"; }
/// <summary> /// Execute command /// </summary> /// <param name="server">Database server</param> /// <param name="executionOptions">Execution options</param> /// <param name="commands">Commands</param> /// <returns>Return the affected data numbers</returns> public async Task <int> ExecuteAsync(DatabaseServer server, CommandExecutionOptions executionOptions, IEnumerable <ICommand> commands) { #region group execution commands IQueryTranslator translator = PostgreSqlManager.GetQueryTranslator(DataAccessContext.Create(server)); List <DatabaseExecutionCommand> databaseExecutionCommands = new List <DatabaseExecutionCommand>(); var batchExecutionConfig = DataManager.GetBatchExecutionConfiguration(server.ServerType) ?? BatchExecutionConfiguration.Default; var groupStatementsCount = batchExecutionConfig.GroupStatementsCount; groupStatementsCount = groupStatementsCount < 0 ? 1 : groupStatementsCount; var groupParameterCount = batchExecutionConfig.GroupParametersCount; groupParameterCount = groupParameterCount < 0 ? 1 : groupParameterCount; StringBuilder commandTextBuilder = new StringBuilder(); CommandParameters parameters = null; int statementsCount = 0; bool forceReturnValue = false; int cmdCount = 0; DatabaseExecutionCommand GetGroupExecuteCommand() { var executionCommand = new DatabaseExecutionCommand() { CommandText = commandTextBuilder.ToString(), CommandType = CommandType.Text, MustAffectedData = forceReturnValue, Parameters = parameters }; statementsCount = 0; translator.ParameterSequence = 0; commandTextBuilder.Clear(); parameters = null; forceReturnValue = false; return(executionCommand); } foreach (var cmd in commands) { DatabaseExecutionCommand databaseExecutionCommand = GetDatabaseExecutionCommand(translator, cmd as DefaultCommand); if (databaseExecutionCommand == null) { continue; } //Trace log PostgreSqlManager.LogExecutionCommand(databaseExecutionCommand); cmdCount++; if (databaseExecutionCommand.PerformAlone) { if (statementsCount > 0) { databaseExecutionCommands.Add(GetGroupExecuteCommand()); } databaseExecutionCommands.Add(databaseExecutionCommand); continue; } commandTextBuilder.AppendLine(databaseExecutionCommand.CommandText); parameters = parameters == null ? databaseExecutionCommand.Parameters : parameters.Union(databaseExecutionCommand.Parameters); forceReturnValue |= databaseExecutionCommand.MustAffectedData; statementsCount++; if (translator.ParameterSequence >= groupParameterCount || statementsCount >= groupStatementsCount) { databaseExecutionCommands.Add(GetGroupExecuteCommand()); } } if (statementsCount > 0) { databaseExecutionCommands.Add(GetGroupExecuteCommand()); } #endregion return(await ExecuteDatabaseCommandAsync(server, executionOptions, databaseExecutionCommands, executionOptions?.ExecutionByTransaction ?? cmdCount > 1).ConfigureAwait(false)); }