Esempio n. 1
0
        /// <summary>
        /// Formata texto de um <see cref="ConditionalTerm"/>
        /// </summary>
        /// <param name="conditionalTerm">Termo condicional</param>
        /// <param name="ignoreContainerFormat">Identifica que é para ignorar a formatação de de container para a consulta.</param>
        /// <returns>Retorna o próprio objeto</returns>
        private MySqlQueryParser Format(ConditionalTerm conditionalTerm, bool ignoreContainerFormat = false)
        {
            if (conditionalTerm is Constant)
            {
                Append(((Constant)conditionalTerm).Text);
            }
            else if (conditionalTerm is Column)
            {
                Format((Column)conditionalTerm);
            }
            else if (conditionalTerm is Variable)
            {
                var variable  = (Variable)conditionalTerm;
                var parameter = Query != null?Query.Parameters.Where(f => f.Name == variable.Name).FirstOrDefault() : null;

                QueryInfo queryParameter = null;
                if (parameter != null && (parameter.Value is QueryInfo || parameter.Value is Queryable))
                {
                    Query.Parameters.Remove(parameter);
                    var value = parameter.Value;
                    if (value is Queryable)
                    {
                        value = ((Queryable)value).CreateQueryInfo();
                    }
                    try
                    {
                        _queryParameters.Add(parameter.Name, (QueryInfo)value);
                    }
                    catch (ArgumentException)
                    {
                        throw new ConditionalParserException(ResourceMessageFormatter.Create(() => Properties.Resources.ConditionalParserException_DuplicateParameter, parameter.Name).Format());
                    }
                    Format(new QueryTerm((QueryInfo)value), ignoreContainerFormat);
                }
                else if (_queryParameters.TryGetValue(variable.Name, out queryParameter))
                {
                    Format(new QueryTerm(queryParameter), ignoreContainerFormat);
                }
                else
                {
                    Append(((Variable)conditionalTerm).Name);
                }
            }
            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 = Query != null?Query.Parameters.Where(f => f.Name == variable.Name).FirstOrDefault() : null;

                    QueryInfo queryParameter = null;
                    if (parameter != null && (parameter.Value is QueryInfo || parameter.Value is Queryable))
                    {
                        Query.Parameters.Remove(parameter);
                        var value = parameter.Value;
                        if (value is Queryable)
                        {
                            value = ((Queryable)value).CreateQueryInfo();
                        }
                        try
                        {
                            _queryParameters.Add(variable.Name, (QueryInfo)value);
                        }
                        catch (ArgumentException)
                        {
                            throw new ConditionalParserException(ResourceMessageFormatter.Create(() => Properties.Resources.ConditionalParserException_DuplicateParameter, parameter.Name).Format());
                        }
                        Format(new QueryTerm((QueryInfo)value), ignoreContainerFormat);
                        values = null;
                    }
                    else if (_queryParameters.TryGetValue(variable.Name, out queryParameter))
                    {
                        Format(new QueryTerm(queryParameter), ignoreContainerFormat);
                        values = null;
                    }
                }
                if (values != null)
                {
                    Append('(');
                    var gone = false;
                    foreach (var i in values.Values)
                    {
                        if (gone)
                        {
                            Append(", ");
                        }
                        else
                        {
                            gone = true;
                        }
                        if (i != null)
                        {
                            Format(i, ignoreContainerFormat);
                        }
                        else
                        {
                            Append("NULL");
                        }
                    }
                    Append(')');
                }
            }
            else if (conditionalTerm is ConditionalContainer)
            {
                Format((ConditionalContainer)conditionalTerm);
            }
            else if (conditionalTerm is QueryTerm)
            {
                Format((QueryTerm)conditionalTerm, ignoreContainerFormat);
            }
            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)
                {
                    Append("DATE_ADD(");
                    Format(f.Parameters[2]);
                    Append(", INTERVAL ");
                    Format(f.Parameters[1]);
                    Append(' ');
                    Format(f.Parameters[0]);
                    Append(')');
                }
                else if (StringComparer.InvariantCultureIgnoreCase.Equals(name, "DATEFORMAT") && f.Parameters.Length == 2)
                {
                    Append("DATE_FORMAT(");
                    Format(f.Parameters[0]);
                    Append(",");
                    Format(f.Parameters[1]);
                    Append(')');
                }
                else if (StringComparer.InvariantCultureIgnoreCase.Equals(name, "CAST") && f.Parameters.Length == 2)
                {
                    Append(name).Append('(').Format(f.Parameters[0]).Append(" AS ");
                    if (f.Parameters[1] is Constant)
                    {
                        var text     = ((Constant)f.Parameters[1]).Text;
                        var comparer = StringComparer.InvariantCultureIgnoreCase;
                        if (comparer.Equals(text, "INT"))
                        {
                            Format(new Constant("SIGNED"));
                        }
                        else if (comparer.Equals(text, "VARCHAR"))
                        {
                            Format(new Constant("CHAR"));
                        }
                        else
                        {
                            Format(f.Parameters[1]);
                        }
                    }
                    else
                    {
                        Format(f.Parameters[1]);
                    }
                    Append(')');
                }
                else
                {
                    if (StringComparer.InvariantCultureIgnoreCase.Equals(name, "ISNULL"))
                    {
                        name = "COALESCE";
                    }
                    else if (StringComparer.InvariantCultureIgnoreCase.Equals(name, "CHARINDEX"))
                    {
                        name = "INSTR";
                    }
                    Append(name);
                    Append('(');
                    if (f.Options.HasFlag(FunctionCallOptions.Distinct))
                    {
                        Append("DISTINCT ");
                    }
                    var gone = false;
                    foreach (var paramTerm in f.Parameters)
                    {
                        if (gone)
                        {
                            Append(", ");
                        }
                        else
                        {
                            gone = true;
                        }
                        if (paramTerm != null)
                        {
                            Format(paramTerm, true);
                        }
                        else
                        {
                            Append("NULL");
                        }
                    }
                    Append(')');
                }
            }
            else if (conditionalTerm is Formula)
            {
                Format((Formula)conditionalTerm);
            }
            else if (conditionalTerm is MinusTerm)
            {
                Append("-");
                Format(((MinusTerm)conditionalTerm).Term);
            }
            else if (conditionalTerm is Conditional)
            {
                Format((Conditional)conditionalTerm);
            }
            else if (conditionalTerm is CaseConditional)
            {
                Format((CaseConditional)conditionalTerm);
            }
            else
            {
                throw new NotSupportedException(ResourceMessageFormatter.Create(() => Properties.Resources.NotSupportedException_TypeOfConditionalTermNotSupported, conditionalTerm.GetType().ToString()).Format());
            }
            return(this);
        }
Esempio n. 2
0
        /// <summary>
        /// Formata texto de um <see cref="ConditionalTerm"/>
        /// </summary>
        /// <param name="conditionalTerm">Termo condicional</param>
        /// <param name="ignoreContainerFormat">Identifica que é para ignorar a formatação de de container para a consulta.</param>
        /// <returns>Retorna o próprio objeto</returns>
        private DefaultSqlQueryParser Format(ConditionalTerm conditionalTerm, bool ignoreContainerFormat = false)
        {
            if (conditionalTerm is Constant)
            {
                Append(((Constant)conditionalTerm).Text);
            }
            else if (conditionalTerm is Column)
            {
                Format((Column)conditionalTerm);
            }
            else if (conditionalTerm is Variable)
            {
                var variable  = (Variable)conditionalTerm;
                var parameter = Query != null?Query.Parameters.Where(f => f.Name == variable.Name).FirstOrDefault() : null;

                QueryInfo queryParameter = null;
                if (parameter != null && (parameter.Value is QueryInfo || parameter.Value is Queryable))
                {
                    Query.Parameters.Remove(parameter);
                    var value = parameter.Value;
                    if (value is Queryable)
                    {
                        value = ((Queryable)value).CreateQueryInfo();
                    }
                    try
                    {
                        _queryParameters.Add(parameter.Name, (QueryInfo)value);
                    }
                    catch (ArgumentException)
                    {
                        throw new ConditionalParserException(ResourceMessageFormatter.Create(() => Properties.Resources.ConditionalParserException_DuplicateParameter, parameter.Name).Format());
                    }
                    Format(new QueryTerm((QueryInfo)value), ignoreContainerFormat);
                }
                else if (_queryParameters.TryGetValue(variable.Name, out queryParameter))
                {
                    Format(new QueryTerm(queryParameter), ignoreContainerFormat);
                }
                else
                {
                    Append(((Variable)conditionalTerm).Name);
                }
            }
            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 = Query != null?Query.Parameters.Where(f => f.Name == variable.Name).FirstOrDefault() : null;

                    QueryInfo queryParameter = null;
                    if (parameter != null && (parameter.Value is QueryInfo || parameter.Value is Queryable))
                    {
                        Query.Parameters.Remove(parameter);
                        var value = parameter.Value;
                        if (value is Queryable)
                        {
                            value = ((Queryable)value).CreateQueryInfo();
                        }
                        try
                        {
                            _queryParameters.Add(parameter.Name, (QueryInfo)value);
                        }
                        catch (ArgumentException)
                        {
                            throw new ConditionalParserException(ResourceMessageFormatter.Create(() => Properties.Resources.ConditionalParserException_DuplicateParameter, parameter.Name).Format());
                        }
                        Format(new QueryTerm((QueryInfo)value), ignoreContainerFormat);
                        values = null;
                    }
                    else if (_queryParameters.TryGetValue(variable.Name, out queryParameter))
                    {
                        Format(new QueryTerm(queryParameter), ignoreContainerFormat);
                        values = null;
                    }
                }
                if (values != null)
                {
                    Append('(');
                    var gone = false;
                    foreach (var i in values.Values)
                    {
                        if (gone)
                        {
                            Append(", ");
                        }
                        else
                        {
                            gone = true;
                        }
                        if (i != null)
                        {
                            Format(i, ignoreContainerFormat);
                        }
                        else
                        {
                            Append("NULL");
                        }
                    }
                    Append(')');
                }
            }
            else if (conditionalTerm is ConditionalContainer)
            {
                Format((ConditionalContainer)conditionalTerm);
            }
            else if (conditionalTerm is QueryTerm)
            {
                Format((QueryTerm)conditionalTerm, ignoreContainerFormat);
            }
            else if (conditionalTerm is FunctionCall)
            {
                var f    = (FunctionCall)conditionalTerm;
                var name = f.Call.ToString().Replace("'", String.Empty);
                if (StringComparer.InvariantCultureIgnoreCase.Equals(name, "CAST") && f.Parameters.Length == 2)
                {
                    Append(name).Append('(').Format(f.Parameters[0]).Append(" AS ").Format(f.Parameters[1]).Append(')');
                }
                else if (StringComparer.InvariantCultureIgnoreCase.Equals(name, "DATEFORMAT") && f.Parameters.Length == 2)
                {
                    throw new Exception("DATEFORMAT function not implemented for this database");
                }
                else
                {
                    Format(f);
                }
            }
            else if (conditionalTerm is Formula)
            {
                Format((Formula)conditionalTerm);
            }
            else if (conditionalTerm is MinusTerm)
            {
                Append("-");
                Format(((MinusTerm)conditionalTerm).Term);
            }
            else if (conditionalTerm is Conditional)
            {
                Format((Conditional)conditionalTerm);
            }
            else if (conditionalTerm is CaseConditional)
            {
                Format((CaseConditional)conditionalTerm);
            }
            else
            {
                throw new NotSupportedException(ResourceMessageFormatter.Create(() => Properties.Resources.NotSupportedException_TypeOfConditionalTermNotSupported, conditionalTerm.GetType().ToString()).Format());
            }
            return(this);
        }
Esempio n. 3
0
 /// <summary>
 /// Formata texto de um <see cref="ConditionalTerm"/>.
 /// </summary>
 /// <param name="conditionalTerm">Termo condicional.</param>
 /// <returns>Retorna o próprio objeto.</returns>
 private CacheQueryParser Format(ConditionalTerm conditionalTerm)
 {
     if (conditionalTerm is Column)
     {
         Format((Column)conditionalTerm);
     }
     else if (conditionalTerm is Variable)
     {
         Append(((Variable)conditionalTerm).Name);
     }
     else if (conditionalTerm is Constant)
     {
         var constantText = ((Constant)conditionalTerm).Text;
         if (StringComparer.InvariantCultureIgnoreCase.Equals(constantText, "NULL"))
         {
             Append("NULL");
         }
         else
         {
             var constantValue = GetConstantValue(constantText);
             var pName         = string.Format("?v{0}", _cacheParameters.Count.ToString("00"));
             _cacheParameters[pName] = constantValue;
             Append(pName);
         }
     }
     else if (conditionalTerm is ValuesArray)
     {
         var valuesArray = (ValuesArray)conditionalTerm;
         Append('(');
         bool isFirst = true;
         ;
         foreach (var value in valuesArray.Values)
         {
             if (isFirst)
             {
                 isFirst = false;
             }
             else
             {
                 Append(',');
             }
             if (value is Constant)
             {
                 var pName        = string.Format("?v{0}", _cacheParameters.Count.ToString("00"));
                 var constantText = ((Constant)value).Text;
                 if (StringComparer.InvariantCultureIgnoreCase.Equals(constantText, "NULL"))
                 {
                     _cacheParameters[pName] = null;
                 }
                 else
                 {
                     var constantValue = GetConstantValue(constantText);
                     _cacheParameters[pName] = constantValue;
                 }
                 Append(pName);
             }
             else if (value is Variable)
             {
                 var variable = (Variable)value;
                 Append(variable.Name);
             }
             else
             {
                 throw new NotSupportedException("ValuesArray accept only Constant and parameter");
             }
         }
         Append(')');
     }
     else if (conditionalTerm is ConditionalContainer)
     {
         Format((ConditionalContainer)conditionalTerm);
     }
     else
     {
         throw new NotSupportedException(ResourceMessageFormatter.Create(() => Properties.Resources.NotSupportedException_TypeOfConditionalTermNotSupported, conditionalTerm.GetType().ToString()).Format());
     }
     return(this);
 }
Esempio n. 4
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);
        }
Esempio n. 5
0
        private OracleQueryParser Format(ConditionalTerm a, bool b = false)
        {
            if (a is Constant)
            {
                Append(((Constant)a).Text);
            }
            else if (a is Column)
            {
                Format((Column)a);
            }
            else if (a is Variable)
            {
                var c = (Variable)a;
                var d = Query != null?Query.Parameters.Where(e => e.Name == c.Name).FirstOrDefault() : null;

                QueryInfo f = null;
                if (d != null && (d.Value is QueryInfo || d.Value is Queryable))
                {
                    Query.Parameters.Remove(d);
                    var g = d.Value;
                    if (g is Queryable)
                    {
                        g = ((Queryable)g).CreateQueryInfo();
                    }
                    try {
                        _queryParameters.Add(d.Name, (QueryInfo)g);
                    }
                    catch (ArgumentException) {
                        throw new ConditionalParserException(ResourceMessageFormatter.Create(() => Properties.Resources.ConditionalParserException_DuplicateParameter, d.Name).Format());
                    }
                    Format(new QueryTerm((QueryInfo)g), b);
                }
                else if (_queryParameters.TryGetValue(c.Name, out f))
                {
                    Format(new QueryTerm(f), b);
                }
                else
                {
                    Append(((Variable)a).Name);
                }
            }
            else if (a is ValuesArray)
            {
                var h = (ValuesArray)a;
                if (h.Values != null && h.Values.Length == 1 && h.Values [0] is Variable)
                {
                    var c = (Variable)h.Values [0];
                    var d = Query != null?Query.Parameters.Where(e => e.Name == c.Name).FirstOrDefault() : null;

                    QueryInfo f = null;
                    if (d != null && (d.Value is QueryInfo || d.Value is Queryable))
                    {
                        Query.Parameters.Remove(d);
                        var g = d.Value;
                        if (g is Queryable)
                        {
                            g = ((Queryable)g).CreateQueryInfo();
                        }
                        try {
                            _queryParameters.Add(d.Name, (QueryInfo)g);
                        }
                        catch (ArgumentException) {
                            throw new ConditionalParserException(ResourceMessageFormatter.Create(() => Properties.Resources.ConditionalParserException_DuplicateParameter, d.Name).Format());
                        }
                        Format(new QueryTerm((QueryInfo)g), b);
                        h = null;
                    }
                    else if (_queryParameters.TryGetValue(c.Name, out f))
                    {
                        Format(new QueryTerm(f), b);
                        h = null;
                    }
                }
                if (h != null)
                {
                    Append('(');
                    var j = false;
                    foreach (var i in h.Values)
                    {
                        if (j)
                        {
                            Append(", ");
                        }
                        else
                        {
                            j = true;
                        }
                        if (i != null)
                        {
                            Format(i, b);
                        }
                        else
                        {
                            Append("NULL");
                        }
                    }
                    Append(')');
                }
            }
            else if (a is ConditionalContainer)
            {
                Format((ConditionalContainer)a);
            }
            else if (a is QueryTerm)
            {
                Format((QueryTerm)a, b);
            }
            else if (a is FunctionCall)
            {
                var e = (FunctionCall)a;
                var k = e.Call.ToString().Replace("'", String.Empty);
                if (StringComparer.InvariantCultureIgnoreCase.Equals(k, "CAST") && e.Parameters.Length == 2)
                {
                    Append(k).Append('(').Format(e.Parameters [0]).Append(" AS ").Format(e.Parameters [1]).Append(')');
                }
                else if (StringComparer.InvariantCultureIgnoreCase.Equals(k, "DATEFORMAT") && e.Parameters.Length == 2)
                {
                    throw new Exception("DATEFORMAT function not implemented for this database");
                }
                else
                {
                    if (StringComparer.InvariantCultureIgnoreCase.Equals(k, "ISNULL"))
                    {
                        k = "NVL";
                    }
                    Append(k);
                    Append('(');
                    if (e.Options.HasFlag(FunctionCallOptions.Distinct))
                    {
                        Append("DISTINCT ");
                    }
                    var j = false;
                    foreach (var paramTerm in e.Parameters)
                    {
                        if (j)
                        {
                            Append(", ");
                        }
                        else
                        {
                            j = true;
                        }
                        if (paramTerm != null)
                        {
                            Format(paramTerm, true);
                        }
                        else
                        {
                            Append("NULL");
                        }
                    }
                    Append(')');
                }
            }
            else if (a is Formula)
            {
                Format((Formula)a);
            }
            else if (a is MinusTerm)
            {
                Append("-");
                Format(((MinusTerm)a).Term);
            }
            else if (a is Conditional)
            {
                Format((Conditional)a);
            }
            else if (a is CaseConditional)
            {
                Format((CaseConditional)a);
            }
            else
            {
                throw new NotSupportedException(ResourceMessageFormatter.Create(() => Properties.Resources.NotSupportedException_TypeOfConditionalTermNotSupported, a.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);
        }
Esempio n. 7
0
        /// <summary>
        /// Processa a junção.
        /// </summary>
        /// <param name="query">Consulta que será analizada.</param>
        /// <param name="term">Termo condicional que será processado.</param>
        /// <returns></returns>
        private LinkResult ProcessLinkResult(QueryInfo query, ConditionalTerm term)
        {
            if (term is Conditional)
            {
                var        conditional = (Conditional)term;
                var        leftColumn  = conditional.Left as Column;
                var        rightColumn = conditional.Right as Column;
                EntityInfo leftEntity  = null;
                EntityInfo rightEntity = null;
                if (leftColumn != null)
                {
                    leftEntity = query.Entities.Where(f => f.Alias == leftColumn.Owner).FirstOrDefault() ?? query.Entities.FirstOrDefault();
                }
                if (rightColumn != null)
                {
                    rightEntity = query.Entities.Where(f => f.Alias == rightColumn.Owner).FirstOrDefault() ?? query.Entities.FirstOrDefault();
                }
                if (leftColumn != null && rightColumn != null)
                {
                    ComparisonType comparisonType = ComparisonType.EQUALS;
                    switch (conditional.Operator.Op)
                    {
                    case "==":
                    case "=":
                        comparisonType = ComparisonType.EQUALS;
                        break;

                    case "<>":
                    case "!=":
                        comparisonType = ComparisonType.NOT_EQUALS;
                        break;

                    case ">":
                        comparisonType = ComparisonType.GREATER_THAN;
                        break;

                    case ">=":
                        comparisonType = ComparisonType.GREATER_THAN_EQUALS;
                        break;

                    case "<":
                        comparisonType = ComparisonType.LESS_THAN;
                        break;

                    case "<=":
                        comparisonType = ComparisonType.LESS_THAN_EQUALS;
                        break;

                    default:
                        throw new InvalidOperationException("Unsupported operator " + conditional.Operator.Op);
                    }
                    return(new LinkResult {
                        Entities = new EntityInfo[] {
                            leftEntity,
                            rightEntity
                        },
                        Items = _cacheProvider.Cache.JoinIndex(leftEntity.FullName, leftColumn.Name, rightEntity.FullName, rightColumn.Name, comparisonType).ToArray()
                    });
                }
                else
                {
                    EntityInfo entity      = leftEntity != null ? leftEntity : rightEntity;
                    string     commandText = null;
                    Hashtable  parameters  = null;
                    Parse(new QueryInfo {
                        Entities = new EntityInfo[] {
                            entity
                        },
                        WhereClause = new ConditionalContainer(conditional),
                        Parameters  = query.Parameters
                    }, out commandText, out parameters);
                    var searchResult = _cacheProvider.Cache.Search(commandText, parameters);
                    return(Convert(searchResult, entity));
                }
            }
            else if (term is ConditionalContainer)
            {
                var        container      = (ConditionalContainer)term;
                var        logicOperators = new Queue <LogicalOperator>(container.LogicalOperators);
                LinkResult joinResult     = null;
                foreach (var term2 in container.Conditionals)
                {
                    if (joinResult == null)
                    {
                        joinResult = ProcessLinkResult(query, term2);
                    }
                    else
                    {
                        joinResult = Merge(logicOperators.Dequeue() == LogicalOperator.And ? MergeType.Intersect : MergeType.Union, joinResult, ProcessLinkResult(query, term2));
                    }
                }
                return(joinResult);
            }
            throw new InvalidOperationException();
        }