Exemplo n.º 1
0
        /// <summary>
        /// Formata texto de um <see cref="ConditionalTerm"/>
        /// </summary>
        /// <param name="conditionalTerm">Termo condicional</param>
        /// <param name="sqlCommand">Objeto <see cref="StringBuilder"/> no qual será adicionado texto</param>
        /// <returns>Retorna o próprio objeto</returns>
        private DefaultPersistenceSqlParser Format(ConditionalTerm conditionalTerm, StringBuilder sqlCommand)
        {
            if (conditionalTerm is Constant)
            {
                Append(((Constant)conditionalTerm).Text, sqlCommand);
            }
            else if (conditionalTerm is Column)
            {
                Format((Column)conditionalTerm, sqlCommand);
            }
            else if (conditionalTerm is Variable)
            {
                var variable  = (Variable)conditionalTerm;
                var parameter = Action != null?Action.Parameters.Where(f => f.Name == variable.Name).FirstOrDefault() : null;

                Colosoft.Query.QueryParameter queryParameter = null;
                if (parameter == null && Action != null && Action.Conditional != null)
                {
                    queryParameter = Action.Conditional.ParameterContainer.FirstOrDefault(f => f.Name == variable.Name);
                    if (queryParameter != null)
                    {
                        parameter = new PersistenceParameter(queryParameter.Name, queryParameter.Value);
                    }
                }
                if (parameter != null && (parameter.Value is QueryInfo || parameter.Value is Colosoft.Query.Queryable))
                {
                    if (queryParameter == null)
                    {
                        this.Action.Parameters.Remove(parameter);
                    }
                    else if (Action.Conditional.ParameterContainer is Colosoft.Query.IQueryParameterContainerExt)
                    {
                        ((Colosoft.Query.IQueryParameterContainerExt)Action.Conditional.ParameterContainer).Remove(queryParameter);
                    }
                    var value = parameter.Value;
                    if (value is Colosoft.Query.Queryable)
                    {
                        value = ((Colosoft.Query.Queryable)value).CreateQueryInfo();
                    }
                    Format(new QueryTerm((QueryInfo)value), sqlCommand);
                }
                else
                {
                    Append(((Variable)conditionalTerm).Name, sqlCommand);
                }
            }
            else if (conditionalTerm is ValuesArray)
            {
                var values = (ValuesArray)conditionalTerm;
                if (values.Values != null && values.Values.Length == 1 && values.Values[0] is Variable)
                {
                    var variable  = (Variable)values.Values[0];
                    var parameter = Action != null?Action.Parameters.Where(f => f.Name == variable.Name).FirstOrDefault() : null;

                    Colosoft.Query.QueryParameter queryParameter = null;
                    if (parameter == null && Action != null && Action.Conditional != null)
                    {
                        queryParameter = Action.Conditional.ParameterContainer.FirstOrDefault(f => f.Name == variable.Name);
                        if (queryParameter != null)
                        {
                            parameter = new PersistenceParameter(queryParameter.Name, queryParameter.Value);
                        }
                    }
                    if (parameter != null && (parameter.Value is QueryInfo || parameter.Value is Colosoft.Query.Queryable))
                    {
                        if (queryParameter == null)
                        {
                            this.Action.Parameters.Remove(parameter);
                        }
                        else if (Action.Conditional.ParameterContainer is Colosoft.Query.IQueryParameterContainerExt)
                        {
                            ((Colosoft.Query.IQueryParameterContainerExt)Action.Conditional.ParameterContainer).Remove(queryParameter);
                        }
                        var value = parameter.Value;
                        if (value is Colosoft.Query.Queryable)
                        {
                            value = ((Colosoft.Query.Queryable)value).CreateQueryInfo();
                        }
                        Format(new QueryTerm((QueryInfo)value), sqlCommand);
                        values = null;
                    }
                }
                if (values != null)
                {
                    Append(conditionalTerm.ToString(), sqlCommand);
                }
            }
            else if (conditionalTerm is ConditionalContainer)
            {
                Format((ConditionalContainer)conditionalTerm, sqlCommand);
            }
            else if (conditionalTerm is QueryTerm)
            {
                Format((QueryTerm)conditionalTerm, sqlCommand);
            }
            else if (conditionalTerm is FunctionCall)
            {
                var f = (FunctionCall)conditionalTerm;
                Format(f, sqlCommand);
            }
            else if (conditionalTerm is Formula)
            {
                Format((Formula)conditionalTerm, sqlCommand);
            }
            else if (conditionalTerm is MinusTerm)
            {
                sqlCommand.Append("-");
                Format(((MinusTerm)conditionalTerm).Term, sqlCommand);
            }
            else if (conditionalTerm is Conditional)
            {
                Format((Conditional)conditionalTerm, sqlCommand);
            }
            else
            {
                throw new NotSupportedException(ResourceMessageFormatter.Create(() => Properties.Resources.NotSupportedException_TypeOfConditionalTermNotSupported, conditionalTerm.GetType().ToString()).Format());
            }
            return(this);
        }
        /// <summary>
        /// Formata texto de um <see cref="ConditionalTerm"/>
        /// </summary>
        /// <param name="conditionalTerm">Termo condicional</param>
        /// <param name="sqlCommand">Objeto <see cref="StringBuilder"/> no qual será adicionado texto</param>
        /// <returns>Retorna o próprio objeto</returns>
        private MySqlPersistenceSqlParser Format(ConditionalTerm conditionalTerm, StringBuilder sqlCommand)
        {
            if (conditionalTerm is Constant)
            {
                Append(((Constant)conditionalTerm).Text, sqlCommand);
            }
            else if (conditionalTerm is Column)
            {
                Format((Column)conditionalTerm, sqlCommand);
            }
            else if (conditionalTerm is Variable)
            {
                var variable  = (Variable)conditionalTerm;
                var parameter = Action != null?Action.Parameters.Where(f => f.Name == variable.Name).FirstOrDefault() : null;

                if (parameter != null && (parameter.Value is QueryInfo || parameter.Value is Colosoft.Query.Queryable))
                {
                    this.Action.Parameters.Remove(parameter);
                    var value = parameter.Value;
                    if (value is Colosoft.Query.Queryable)
                    {
                        value = ((Colosoft.Query.Queryable)value).CreateQueryInfo();
                    }
                    Format(new QueryTerm((QueryInfo)value), sqlCommand);
                }
                else
                {
                    Append(((Variable)conditionalTerm).Name, sqlCommand);
                }
            }
            else if (conditionalTerm is ValuesArray)
            {
                var values = (ValuesArray)conditionalTerm;
                if (values.Values != null && values.Values.Length == 1 && values.Values[0] is Variable)
                {
                    var variable  = (Variable)values.Values[0];
                    var parameter = Action != null?Action.Parameters.Where(f => f.Name == variable.Name).FirstOrDefault() : null;

                    if (parameter != null && (parameter.Value is QueryInfo || parameter.Value is Colosoft.Query.Queryable))
                    {
                        this.Action.Parameters.Remove(parameter);
                        var value = parameter.Value;
                        if (value is Colosoft.Query.Queryable)
                        {
                            value = ((Colosoft.Query.Queryable)value).CreateQueryInfo();
                        }
                        Format(new QueryTerm((QueryInfo)value), sqlCommand);
                        values = null;
                    }
                }
                if (values != null)
                {
                    Append(conditionalTerm.ToString(), sqlCommand);
                }
            }
            else if (conditionalTerm is ConditionalContainer)
            {
                Format((ConditionalContainer)conditionalTerm, sqlCommand);
            }
            else if (conditionalTerm is QueryTerm)
            {
                Format((QueryTerm)conditionalTerm, sqlCommand);
            }
            else if (conditionalTerm is FunctionCall)
            {
                var f    = (FunctionCall)conditionalTerm;
                var name = f.Call.ToString().Replace("'", String.Empty);
                if (StringComparer.InvariantCultureIgnoreCase.Equals(name, "DATEADD") && f.Parameters.Length == 3)
                {
                    sqlCommand.Append("DATE_ADD(");
                    Format(f.Parameters[2], sqlCommand);
                    sqlCommand.Append(", INTERVAL ");
                    Format(f.Parameters[1], sqlCommand);
                    sqlCommand.Append(' ');
                    Format(f.Parameters[0], sqlCommand);
                    sqlCommand.Append(')');
                }
                else
                {
                    if (StringComparer.InvariantCultureIgnoreCase.Equals(name, "ISNULL"))
                    {
                        name = "COALESCE";
                    }
                    Append(name, sqlCommand);
                    Append('(', sqlCommand);
                    var gone = false;
                    foreach (var paramTerm in f.Parameters)
                    {
                        if (gone)
                        {
                            Append(", ", sqlCommand);
                        }
                        else
                        {
                            gone = true;
                        }
                        if (paramTerm != null)
                        {
                            Format(paramTerm, sqlCommand);
                        }
                        else
                        {
                            Append("NULL", sqlCommand);
                        }
                    }
                    Append(')', sqlCommand);
                }
            }
            else if (conditionalTerm is Formula)
            {
                Format((Formula)conditionalTerm, sqlCommand);
            }
            else if (conditionalTerm is MinusTerm)
            {
                sqlCommand.Append("-");
                Format(((MinusTerm)conditionalTerm).Term, sqlCommand);
            }
            else if (conditionalTerm is Conditional)
            {
                Format((Conditional)conditionalTerm, sqlCommand);
            }
            else
            {
                throw new NotSupportedException(ResourceMessageFormatter.Create(() => Properties.Resources.NotSupportedException_TypeOfConditionalTermNotSupported, conditionalTerm.GetType().ToString()).Format());
            }
            return(this);
        }