/// <summary> /// Format insert fields /// </summary> /// <param name="fields">Fields</param> /// <param name="parameters">Parameters</param> /// <returns>first:fields,second:parameter fields,third:parameters</returns> public static Tuple <List <string>, List <string>, CommandParameters> FormatInsertFields(int fieldCount, IEnumerable <EntityField> fields, object parameters, int parameterSequence) { if (fields.IsNullOrEmpty()) { return(null); } List <string> formatFields = new List <string>(fieldCount); List <string> parameterFields = new List <string>(fieldCount); CommandParameters cmdParameters = ParseParameters(parameters); foreach (var field in fields) { //fields formatFields.Add($"{WrapKeyword(field.FieldName)}"); //parameter name parameterSequence++; string parameterName = field.PropertyName + parameterSequence; parameterFields.Add($"{ParameterPrefix}{parameterName}"); //parameter value cmdParameters?.Rename(field.PropertyName, parameterName); } return(new Tuple <List <string>, List <string>, CommandParameters>(formatFields, parameterFields, cmdParameters)); }
/// <summary> /// Format insertion fields /// </summary> /// <param name="entityType">Entity type</param> /// <param name="fieldCount">Field count</param> /// <param name="fields">Fields</param> /// <param name="parameters">Parameters</param> /// <param name="parameterSequence">Parameter sequence</param> /// <returns>First:fields,Second:parameter fields,Third:parameters</returns> public static Tuple <List <string>, List <string>, CommandParameters> FormatInsertionFields(Type entityType, int fieldCount, IEnumerable <EntityField> fields, object parameters, int parameterSequence) { if (fields.IsNullOrEmpty()) { throw new EZNEWException($"Entity type {entityType?.Name} not set fields for insertion."); } List <string> formatFields = new List <string>(fieldCount); List <string> parameterFields = new List <string>(fieldCount); CommandParameters cmdParameters = ConvertParameter(parameters); foreach (var field in fields) { formatFields.Add(WrapKeyword(field.FieldName)); //parameter name parameterSequence++; string parameterName = field.PropertyName + parameterSequence; parameterFields.Add($"{ParameterPrefix}{parameterName}"); //parameter value cmdParameters?.Rename(field.PropertyName, parameterName); } return(new Tuple <List <string>, List <string>, CommandParameters>(formatFields, parameterFields, cmdParameters)); }
/// <summary> /// Get database update command /// </summary> /// <param name="translator">Query translator</param> /// <param name="command">Command</param> /// <returns>Return a database update command</returns> DatabaseExecutionCommand GetDatabaseUpdateCommand(IQueryTranslator translator, DefaultCommand command) { if (command?.Fields.IsNullOrEmpty() ?? true) { throw new EZNEWException($"No fields are set to update"); } #region query translation translator.DataAccessContext.SetCommand(command); var tranResult = translator.Translate(command.Query); string conditionString = string.Empty; if (!string.IsNullOrWhiteSpace(tranResult.ConditionString)) { conditionString += "WHERE " + tranResult.ConditionString; } string preScript = tranResult.PreScript; string joinScript = tranResult.AllowJoin ? tranResult.JoinScript : string.Empty; #endregion #region script CommandParameters parameters = PostgreSqlManager.ConvertParameter(command.Parameters) ?? new CommandParameters(); string objectName = translator.DataAccessContext.GetCommandEntityObjectName(command); var fields = PostgreSqlManager.GetFields(command.EntityType, command.Fields); int parameterSequence = translator.ParameterSequence; List <string> updateSetArray = new List <string>(); foreach (var field in fields) { var parameterValue = parameters.GetParameterValue(field.PropertyName); var parameterName = field.PropertyName; string newValueExpression = string.Empty; if (parameterValue != null) { parameterSequence++; parameterName = PostgreSqlManager.FormatParameterName(parameterName, parameterSequence); parameters.Rename(field.PropertyName, parameterName); if (parameterValue is IModificationValue) { var modifyValue = parameterValue as IModificationValue; parameters.ModifyValue(parameterName, modifyValue.Value); if (parameterValue is CalculationModificationValue) { var calculateModifyValue = parameterValue as CalculationModificationValue; string calChar = PostgreSqlManager.GetSystemCalculationOperator(calculateModifyValue.Operator); newValueExpression = $"{translator.ObjectPetName}.{PostgreSqlManager.WrapKeyword(field.FieldName)}{calChar}{PostgreSqlManager.ParameterPrefix}{parameterName}"; } } } if (string.IsNullOrWhiteSpace(newValueExpression)) { newValueExpression = $"{PostgreSqlManager.ParameterPrefix}{parameterName}"; } updateSetArray.Add($"{PostgreSqlManager.WrapKeyword(field.FieldName)}={newValueExpression}"); } string cmdText; string wrapObjName = PostgreSqlManager.WrapKeyword(objectName); if (string.IsNullOrWhiteSpace(joinScript)) { cmdText = $"{preScript}UPDATE {wrapObjName} AS {translator.ObjectPetName} SET {string.Join(",", updateSetArray)} {conditionString};"; } else { string updateTableShortName = translator.ObjectPetName; string updateJoinTableShortName = "UJTB"; var primaryKeyFormatedResult = FormatWrapJoinPrimaryKeys(command.EntityType, translator.ObjectPetName, updateTableShortName, updateJoinTableShortName); cmdText = $"{preScript}UPDATE {wrapObjName} AS {updateTableShortName} SET {string.Join(",", updateSetArray)} FROM (SELECT {string.Join(",", primaryKeyFormatedResult.Item1)} FROM {wrapObjName} AS {translator.ObjectPetName} {joinScript} {conditionString}) AS {updateJoinTableShortName} WHERE {string.Join(" AND ", primaryKeyFormatedResult.Item2)};"; } translator.ParameterSequence = parameterSequence; #endregion #region parameter var queryParameters = PostgreSqlManager.ConvertParameter(tranResult.Parameters); parameters.Union(queryParameters); #endregion return(new DatabaseExecutionCommand() { CommandText = cmdText, CommandType = PostgreSqlManager.GetCommandType(command), MustAffectedData = command.MustAffectedData, Parameters = parameters, HasPreScript = !string.IsNullOrWhiteSpace(preScript) }); }
/// <summary> /// Get update execute command /// </summary> /// <param name="translator">Translator</param> /// <param name="command">Command</param> /// <returns>Return update execute command</returns> DatabaseExecuteCommand GetUpdateExecuteDbCommand(IQueryTranslator translator, RdbCommand command) { #region query translate var tranResult = translator.Translate(command.Query); string conditionString = string.Empty; if (!string.IsNullOrWhiteSpace(tranResult.ConditionString)) { conditionString += "WHERE " + tranResult.ConditionString; } string preScript = tranResult.PreScript; string joinScript = tranResult.AllowJoin ? tranResult.JoinScript : string.Empty; #endregion #region script CommandParameters parameters = ParseParameters(command.Parameters) ?? new CommandParameters(); string objectName = DataManager.GetEntityObjectName(DatabaseServerType.MySQL, command.EntityType, command.ObjectName); var fields = GetFields(command.EntityType, command.Fields); int parameterSequence = translator.ParameterSequence; List <string> updateSetArray = new List <string>(); foreach (var field in fields) { var parameterValue = parameters.GetParameterValue(field.PropertyName); var parameterName = field.PropertyName; string newValueExpression = string.Empty; if (parameterValue != null) { parameterSequence++; parameterName = FormatParameterName(parameterName, parameterSequence); parameters.Rename(field.PropertyName, parameterName); if (parameterValue is IModifyValue) { var modifyValue = parameterValue as IModifyValue; parameters.ModifyValue(parameterName, modifyValue.Value); if (parameterValue is CalculateModifyValue) { var calculateModifyValue = parameterValue as CalculateModifyValue; string calChar = GetCalculateChar(calculateModifyValue.Operator); newValueExpression = $"{translator.ObjectPetName}.`{field.FieldName}`{calChar}{ParameterPrefix}{parameterName}"; } } } if (string.IsNullOrWhiteSpace(newValueExpression)) { newValueExpression = $"{ParameterPrefix}{parameterName}"; } updateSetArray.Add($"{translator.ObjectPetName}.`{field.FieldName}`={newValueExpression}"); } string cmdText = $"{preScript}UPDATE `{objectName}` AS {translator.ObjectPetName} {joinScript} SET {string.Join(",", updateSetArray)} {conditionString};"; translator.ParameterSequence = parameterSequence; #endregion #region parameter var queryParameters = ParseParameters(tranResult.Parameters); parameters.Union(queryParameters); #endregion return(new DatabaseExecuteCommand() { CommandText = cmdText, CommandType = GetCommandType(command), ForceReturnValue = command.MustReturnValueOnSuccess, Parameters = parameters, HasPreScript = !string.IsNullOrWhiteSpace(preScript) }); }