/// <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("ICommand.Query is null"); } #region query translate IQueryTranslator translator = SqlServerFactory.GetQueryTranslator(server); var tranResult = translator.Translate(command.Query); string preScript = tranResult.PreScript; string joinScript = tranResult.AllowJoin ? tranResult.JoinScript : string.Empty; #endregion #region script string cmdText; switch (command.Query.QueryType) { case QueryCommandType.Text: cmdText = tranResult.ConditionString; break; case QueryCommandType.QueryObject: default: int size = command.Query.QuerySize; string objectName = DataManager.GetEntityObjectName(DatabaseServerType.SQLServer, command.EntityType, command.ObjectName); string orderString = string.IsNullOrWhiteSpace(tranResult.OrderString) ? string.Empty : $"ORDER BY {tranResult.OrderString}"; var queryFields = SqlServerFactory.GetQueryFields(command.Query, command.EntityType, true); string outputFormatedField = string.Join(",", SqlServerFactory.FormatQueryFields(translator.ObjectPetName, queryFields, true)); if (string.IsNullOrWhiteSpace(tranResult.CombineScript)) { cmdText = $"{preScript}SELECT {(size > 0 ? $"TOP {size}" : string.Empty)} {outputFormatedField} FROM {SqlServerFactory.WrapKeyword(objectName)} AS {translator.ObjectPetName} {joinScript} {(string.IsNullOrWhiteSpace(tranResult.ConditionString) ? string.Empty : $"WHERE {tranResult.ConditionString}")} {orderString}"; }
/// <summary> /// Execute command /// </summary> /// <param name="server">Server</param> /// <param name="executeOption">Execute option</param> /// <param name="commands">Commands</param> /// <returns>Return effect data numbers</returns> public async Task <int> ExecuteAsync(DatabaseServer server, CommandExecuteOptions executeOption, IEnumerable <ICommand> commands) { #region group execute commands IQueryTranslator translator = SqlServerFactory.GetQueryTranslator(server); List <DatabaseExecuteCommand> executeCommands = new List <DatabaseExecuteCommand>(); var batchExecuteConfig = DataManager.GetBatchExecuteConfiguration(server.ServerType) ?? BatchExecuteConfiguration.Default; var groupStatementsCount = batchExecuteConfig.GroupStatementsCount; groupStatementsCount = groupStatementsCount < 0 ? 1 : groupStatementsCount; var groupParameterCount = batchExecuteConfig.GroupParametersCount; groupParameterCount = groupParameterCount < 0 ? 1 : groupParameterCount; StringBuilder commandTextBuilder = new StringBuilder(); CommandParameters parameters = null; int statementsCount = 0; bool forceReturnValue = false; int cmdCount = 0; DatabaseExecuteCommand GetGroupExecuteCommand() { var executeCommand = new DatabaseExecuteCommand() { CommandText = commandTextBuilder.ToString(), CommandType = CommandType.Text, ForceReturnValue = forceReturnValue, Parameters = parameters }; statementsCount = 0; translator.ParameterSequence = 0; commandTextBuilder.Clear(); parameters = null; forceReturnValue = false; return(executeCommand); } foreach (var cmd in commands) { DatabaseExecuteCommand executeCommand = GetExecuteDbCommand(translator, cmd as RdbCommand); if (executeCommand == null) { continue; } //Trace log SqlServerFactory.LogExecuteCommand(executeCommand); cmdCount++; if (executeCommand.PerformAlone) { if (statementsCount > 0) { executeCommands.Add(GetGroupExecuteCommand()); } executeCommands.Add(executeCommand); continue; } commandTextBuilder.AppendLine(executeCommand.CommandText); parameters = parameters == null ? executeCommand.Parameters : parameters.Union(executeCommand.Parameters); forceReturnValue |= executeCommand.ForceReturnValue; statementsCount++; if (translator.ParameterSequence >= groupParameterCount || statementsCount >= groupStatementsCount) { executeCommands.Add(GetGroupExecuteCommand()); } } if (statementsCount > 0) { executeCommands.Add(GetGroupExecuteCommand()); } #endregion return(await ExecuteCommandAsync(server, executeOption, executeCommands, executeOption?.ExecuteByTransaction ?? cmdCount > 1).ConfigureAwait(false)); }