/// <summary> /// Formata o texto de uma <see cref="QueryInfo" /> /// </summary> /// <param name="queryInfo"></param> /// <param name="sqlCommand"></param> /// <returns></returns> private MySqlPersistenceSqlParser Format(QueryInfo queryInfo, StringBuilder sqlCommand) { var parser = new Colosoft.Query.Database.MySql.MySqlQueryParser(this.Translator, this.TypeSchema); parser.Query = queryInfo; foreach (var parameter in Query.QueryParameter.GetParameters(queryInfo, true)) { if (!this.Action.Parameters.Any(f => f.Name == parameter.Name)) { this.Action.Parameters.Add(new PersistenceParameter(parameter.Name, parameter.Value, parameter.Direction)); } } Append('(', sqlCommand).Append(parser.GetText(), sqlCommand).Append(')', sqlCommand); return(this); }
/// <summary> /// Método responsável por gerar comandos de atualização /// </summary> private string UpdateParser() { var sqlCommand = new StringBuilder(); var typeMetadata = TypeSchema.GetTypeMetadata(Action.EntityFullName); var keyProperties = typeMetadata.GetKeyProperties(); var wherePart = new StringBuilder(); var setPart = new StringBuilder(); EntityInfo info = null; if (Action.Query != null) { info = Action.Query.Entities.FirstOrDefault(); QueryParser = new Colosoft.Query.Database.MySql.MySqlQueryParser(Translator, TypeSchema); QueryParser.Query = Action.Query; Append("UPDATE ", sqlCommand).AppendTranslatedName(Translator.GetName(info), sqlCommand).Append(' ', sqlCommand).AppendQuoteExpression(info.Alias, sqlCommand).Append(' ', sqlCommand).Append(QueryParser.GetText(SqlQueryPart.Joins | SqlQueryPart.Joins), sqlCommand).Append(" SET ", sqlCommand); foreach (var parameter in Query.QueryParameter.GetParameters(Action.Query, true)) { if (!this.Action.Parameters.Any(f => f.Name == parameter.Name)) { this.Action.Parameters.Add(new PersistenceParameter(parameter.Name, parameter.Value, parameter.Direction)); } } } else { info = GetEntityInfo(Action.EntityFullName); Append("UPDATE ", sqlCommand).AppendTranslatedName(Translator.GetName(info), sqlCommand).Append(" SET ", sqlCommand); } bool isFirstWhere = true; bool isFirstSet = true; var identityKeyIndex = new Tuple <int, IPropertyMetadata>(-1, null); var keysIndex = new List <Tuple <int, IPropertyMetadata> >(); for (int i = 0; i < Action.Parameters.Count; i++) { var parameter = Action.Parameters[i]; if (string.IsNullOrEmpty(parameter.Name) || parameter.Name.StartsWith("?")) { continue; } var propertyMetadata = typeMetadata[parameter.Name]; var translatedName = Translator.GetName(info, propertyMetadata.Name); SetParameterType(propertyMetadata, parameter); if (propertyMetadata.ParameterType == PersistenceParameterType.IdentityKey || propertyMetadata.ParameterType == PersistenceParameterType.Key) { if (Action.Conditional != null) { continue; } if (propertyMetadata.ParameterType == PersistenceParameterType.IdentityKey) { identityKeyIndex = new Tuple <int, IPropertyMetadata>(i, propertyMetadata); } else if (propertyMetadata.ParameterType == PersistenceParameterType.Key) { keysIndex.Add(new Tuple <int, IPropertyMetadata>(i, propertyMetadata)); } if (!isFirstWhere) { Append(" AND ", wherePart); } else { isFirstWhere = false; } if (translatedName is TranslatedColumnName) { if (!string.IsNullOrEmpty(info.Alias)) { AppendQuoteExpression(info.Alias, sqlCommand).Append('.', sqlCommand); } AppendTranslatedName(translatedName, wherePart); } else if (propertyMetadata.Name == Query.DataAccessConstants.RowVersionPropertyName) { AppendQuoteExpression(Query.DataAccessConstants.RowVersionColumnName, wherePart); } else { throw new InvalidOperationException("Expected ColumnName for property \"" + propertyMetadata.Name + "\""); } Append("=?", wherePart).Append(propertyMetadata.Name, wherePart); continue; } if (!isFirstSet) { Append(',', setPart); } else { isFirstSet = false; } if (translatedName is TranslatedColumnName) { if (!string.IsNullOrEmpty(info.Alias)) { AppendQuoteExpression(info.Alias, sqlCommand).Append('.', sqlCommand); } AppendTranslatedName(translatedName, setPart); } else if (propertyMetadata.Name == Query.DataAccessConstants.RowVersionPropertyName) { AppendQuoteExpression(Query.DataAccessConstants.RowVersionColumnName, setPart); } else { throw new InvalidOperationException("Expected ColumnName for property \"" + propertyMetadata.Name + "\""); } Append("=", setPart); if (parameter.Value is PropertyReference) { var propertyReference = (PropertyReference)parameter.Value; var propertyTranslateName = Translator.GetName(info, propertyReference.PropertyName); if (translatedName is TranslatedColumnName) { AppendTranslatedName(translatedName, setPart); } else { throw new InvalidOperationException("Expected ColumnName for property \"" + propertyReference.PropertyName + "\""); } } else if (parameter.Value is ExpressionParameter) { var expressionParameter = (ExpressionParameter)parameter.Value; var expressionContainer = Colosoft.Query.ConditionalContainer.Parse(expressionParameter.Expression, expressionParameter.Parameters.Select(f => new QueryParameter(f.Name, f.Value)).ToArray()); Format(expressionContainer, setPart); } else if (parameter.Value is Colosoft.Query.Queryable) { var queryInfo = ((Colosoft.Query.Queryable)parameter.Value).CreateQueryInfo(); Format(queryInfo, setPart); } else if (parameter.Value is QueryInfo) { var queryInfo = (QueryInfo)parameter.Value; Format(queryInfo, setPart); } else { Append("?", setPart).Append(propertyMetadata.Name, setPart); } } if (Action.Conditional != null) { Format(Action.Conditional, wherePart); } else if (Action.Query != null) { Format(Action.Query.WhereClause, wherePart); } Append(' ', setPart); if (Action.Query != null) { Append(setPart, sqlCommand).Append(QueryParser.GetText(SqlQueryPart.Where), sqlCommand).Append(wherePart, sqlCommand); } else { Append(setPart, sqlCommand).Append(" WHERE ", sqlCommand).Append(wherePart, sqlCommand); } if (Action.Conditional == null && Action.Query == null) { if (identityKeyIndex.Item1 >= 0) { keysIndex.Clear(); keysIndex.Add(identityKeyIndex); } if (typeMetadata.IsVersioned && keysIndex.Count > 0) { AppendVerifyRowVersionExpression(sqlCommand); } AppendGetRowVersionAndVolatilePropertiesExpression(info, keysIndex, typeMetadata, sqlCommand); } return(sqlCommand.ToString()); }