Example #1
0
 private void PrintSelectScalarExperssionRecurse(int aSelectElementID, ScalarExpression aScalarExpression)
 {
     if (aScalarExpression.GetType() == typeof(ColumnReferenceExpression))
     {
         ColumnReferenceExpression aColumnReferenceExpression = (ColumnReferenceExpression)aScalarExpression;
         AddLogText(string.Format("ColumnType={0}", aColumnReferenceExpression.ColumnType.ToString()));
         MultiPartIdentifier aMultiPartIdentifier = aColumnReferenceExpression.MultiPartIdentifier;
         AddLogText(string.Format("Reference Identifier={0}",
                                  MultiPartIdentifierToString(aSelectElementID, aMultiPartIdentifier)));
     }
     else if (aScalarExpression.GetType() == typeof(ConvertCall))
     {
         ConvertCall      aConvertCall = (ConvertCall)aScalarExpression;
         ScalarExpression aScalarExpressionParameter = aConvertCall.Parameter;
         PrintSelectScalarExperssionRecurse(aSelectElementID, aScalarExpressionParameter);
     }
     else
     {
         AddLogText(String.Format("Not supported Expression:{0}", aScalarExpression.GetType().ToString()));
     }
 }
 internal static gsScalarExpressionParserBase CreateParser(ScalarExpression expression, string columnAlias)
 {
     if (expression is ColumnReferenceExpression)
     {
         return(new gsScalarExpressionParserColumnReference(expression, columnAlias));
     }
     else if (expression is Literal)
     {
         return(new gsScalarExpressionParserLiteral(expression, columnAlias));
     }
     else if (expression is SearchedCaseExpression)
     {
         return(new gsScalarExpressionParserSearchedCase(expression, columnAlias));
     }
     else if (expression is VariableReference)
     {
         return(new gsScalarExpressionParserVariableReference(expression, columnAlias));
     }
     else if (expression is FunctionCall)
     {
         return(new gsScalarExpressionParserFunctionCall(expression, columnAlias));
     }
     else if (expression is ScalarSubquery)
     {
         return(new gsScalarExpressionParserScalarSubquery(expression, columnAlias));
     }
     else if (expression is CoalesceExpression)
     {
         return(new gsScalarExpressionParserCoalesce(expression, columnAlias));
     }
     else if (expression is ParenthesisExpression)
     {
         return(new gsScalarExpressionParserParenthesis(expression, columnAlias));
     }
     else if (expression is BinaryExpression)
     {
         return(new gsScalarExpressionParserBinary(expression, columnAlias));
     }
     else if (expression is UnaryExpression)
     {
         return(new gsScalarExpressionParserUnary(expression, columnAlias));
     }
     else
     {
         throw new NotImplementedException($"ScalarExpression of type {expression.GetType().Name} not supported");
     }
 }
Example #3
0
        private WScalarExpression ParseScalarExpression(ScalarExpression scalarExpr)
        {
            if (scalarExpr == null)
            {
                return null;
            }

            switch (scalarExpr.GetType().Name)
            {
                case "BinaryExpression":
                    {
                        var bexpr = scalarExpr as BinaryExpression;
                        var wexpr = new WBinaryExpression
                        {
                            ExpressionType = bexpr.BinaryExpressionType,
                            FirstExpr = ParseScalarExpression(bexpr.FirstExpression),
                            SecondExpr = ParseScalarExpression(bexpr.SecondExpression),
                            FirstTokenIndex = bexpr.FirstTokenIndex,
                            LastTokenIndex = bexpr.LastTokenIndex,
                        };

                        return wexpr;
                    }
                case "UnaryExpression":
                    {
                        var uexpr = scalarExpr as UnaryExpression;
                        var wuexpr = new WUnaryExpression
                        {
                            Expression = ParseScalarExpression(uexpr.Expression),
                            ExpressionType = uexpr.UnaryExpressionType,
                            FirstTokenIndex = uexpr.FirstTokenIndex,
                            LastTokenIndex = uexpr.LastTokenIndex
                        };

                        return wuexpr;
                    }
                case "ColumnReferenceExpression":
                    {
                        var cre = scalarExpr as ColumnReferenceExpression;
                        var wexpr = new WColumnReferenceExpression
                        {
                            MultiPartIdentifier = ParseMultiPartIdentifier(cre.MultiPartIdentifier),
                            ColumnType = cre.ColumnType,
                            FirstTokenIndex = cre.FirstTokenIndex,
                            LastTokenIndex = cre.LastTokenIndex
                        };

                        return wexpr;
                    }
                case "ScalarSubquery":
                    {
                        var oquery = scalarExpr as ScalarSubquery;
                        var wexpr = new WScalarSubquery
                        {
                            SubQueryExpr = ParseSelectQueryStatement(oquery.QueryExpression),
                            FirstTokenIndex = oquery.FirstTokenIndex,
                            LastTokenIndex = oquery.LastTokenIndex
                        };

                        return wexpr;
                    }
                case "ParenthesisExpression":
                    {
                        var parenExpr = scalarExpr as ParenthesisExpression;
                        var wexpr = new WParenthesisExpression
                        {
                            Expression = ParseScalarExpression(parenExpr.Expression),
                            FirstTokenIndex = parenExpr.FirstTokenIndex,
                            LastTokenIndex = parenExpr.LastTokenIndex,
                        };

                        return wexpr;
                    }
                case "FunctionCall":
                    {
                        var fc = scalarExpr as FunctionCall;
                        var wexpr = new WFunctionCall
                        {
                            CallTarget = ParseCallTarget(fc.CallTarget),
                            FunctionName = fc.FunctionName,
                            UniqueRowFilter = fc.UniqueRowFilter,
                            FirstTokenIndex = fc.FirstTokenIndex,
                            LastTokenIndex = fc.LastTokenIndex,
                        };

                        if (fc.Parameters == null) return wexpr;
                        wexpr.Parameters = new List<WScalarExpression>(fc.Parameters.Count);
                        foreach (var pe in fc.Parameters.Select(ParseScalarExpression).Where(pe => pe != null))
                        {
                            wexpr.Parameters.Add(pe);
                        }

                        return wexpr;
                    }
                case "SearchedCaseExpression":
                    {
                        var caseExpr = scalarExpr as SearchedCaseExpression;
                        var wexpr = new WSearchedCaseExpression
                        {
                            FirstTokenIndex = caseExpr.FirstTokenIndex,
                            LastTokenIndex = caseExpr.LastTokenIndex,
                            WhenClauses = new List<WSearchedWhenClause>(caseExpr.WhenClauses.Count)
                        };

                        foreach (var pwhen in caseExpr.WhenClauses.Select(swhen => new WSearchedWhenClause
                        {
                            WhenExpression = ParseBooleanExpression(swhen.WhenExpression),
                            ThenExpression = ParseScalarExpression(swhen.ThenExpression),
                            FirstTokenIndex = swhen.FirstTokenIndex,
                            LastTokenIndex = swhen.LastTokenIndex,
                        }))
                        {
                            wexpr.WhenClauses.Add(pwhen);
                        }

                        wexpr.ElseExpr = ParseScalarExpression(caseExpr.ElseExpression);

                        return wexpr;
                    }
                case "CastCall":
                    {
                        var castExpr = scalarExpr as CastCall;
                        var wexpr = new WCastCall
                        {
                            DataType = ParseDataType(castExpr.DataType),
                            Parameter = ParseScalarExpression(castExpr.Parameter),
                            FirstTokenIndex = castExpr.FirstTokenIndex,
                            LastTokenIndex = castExpr.LastTokenIndex,
                        };

                        return wexpr;
                    }
                default:
                    {
                        if (!(scalarExpr is ValueExpression)) return null;
                        var wexpr = new WValueExpression
                        {
                            FirstTokenIndex = scalarExpr.FirstTokenIndex,
                            LastTokenIndex = scalarExpr.LastTokenIndex,
                        };

                        var expr = scalarExpr as Literal;
                        if (expr != null)
                        {
                            wexpr.Value = expr.Value;

                            if (expr.LiteralType == LiteralType.String)
                            {
                                wexpr.SingleQuoted = true;
                            }
                        }
                        else
                        {
                            var reference = scalarExpr as VariableReference;
                            wexpr.Value = reference != null ? reference.Name : ((GlobalVariableExpression)scalarExpr).Name;
                        }

                        return wexpr;
                    }
            }
        }
Example #4
0
        private SqlParserResult ParseScalarExpression(ScalarExpression expression, SqlParserResult result = null)
        {
            switch (expression)
            {
            case null:
                return(result);

            case ColumnReferenceExpression columnReferenceExpression:
                if (result == null)
                {
                    result = CreateNewResult(TSqlTokenType.Select);
                }
                if (columnReferenceExpression.MultiPartIdentifier != null)
                {
                    result.Fields.Add(new SqlColumnDefinition(columnReferenceExpression.MultiPartIdentifier.Identifiers));
                }
                return(result);

            case FunctionCall functionCall:
                if (functionCall.CallTarget != null)
                {
                    if (functionCall.CallTarget is MultiPartIdentifierCallTarget multipartIdentifierCallTarget)
                    {
                        var r  = CreateNewResult(TSqlTokenType.Function);
                        var sb = new StringBuilder();
                        foreach (var identifier in multipartIdentifierCallTarget.MultiPartIdentifier.Identifiers)
                        {
                            if (sb.Length > 0)
                            {
                                sb.Append(".");
                            }
                            sb.Append(identifier.Value);
                        }
                        if (sb.Length > 0)
                        {
                            sb.Append(".");
                        }
                        sb.Append(functionCall.FunctionName.Value);
                        r.Target = sb.ToString();
                        StoreResult(r);
                    }
                    else if (functionCall.CallTarget is ExpressionCallTarget)
                    {
                        var expressionCallTarget = (ExpressionCallTarget)functionCall.CallTarget;
                        result = ParseScalarExpression(expressionCallTarget.Expression, result);
                    }
                    else
                    {
                        Debugger.Break();
                    }
                }

                foreach (var parameter in functionCall.Parameters)
                {
                    result = ParseScalarExpression(parameter, result);
                }
                return(result);

            case LeftFunctionCall leftFunctionCall:
                foreach (var parameter in leftFunctionCall.Parameters)
                {
                    result = ParseScalarExpression(parameter, result);
                }
                return(result);

            case RightFunctionCall rightFunctionCall:
                foreach (var parameter in rightFunctionCall.Parameters)
                {
                    result = ParseScalarExpression(parameter, result);
                }
                return(result);

            case ConvertCall convertCall:
                result = ParseScalarExpression(convertCall.Parameter, result);
                return(result);

            case BinaryExpression binaryExpression:
                result = ParseScalarExpression(binaryExpression.FirstExpression, result);
                result = ParseScalarExpression(binaryExpression.SecondExpression, result);
                return(result);

            case CastCall castCall:
                result = ParseScalarExpression(castCall.Parameter, result);
                return(result);

            case ParenthesisExpression parenthesisExpression:
                result = ParseScalarExpression(parenthesisExpression.Expression, result);
                return(result);

            case UnaryExpression unaryExpression:
                result = ParseScalarExpression(unaryExpression.Expression, result);
                return(result);

            case SearchedCaseExpression searchCaseExpression:
                foreach (var when in searchCaseExpression.WhenClauses)
                {
                    result = ParseScalarExpression(when.ThenExpression, result);
                }
                return(result);

            case ScalarSubquery scalarSubQuery:
                result = ParseSelect(scalarSubQuery.QueryExpression, result);
                return(result);

            case NullIfExpression nullIfExpression:
                result = ParseScalarExpression(nullIfExpression.FirstExpression, result);
                result = ParseScalarExpression(nullIfExpression.SecondExpression, result);
                return(result);

            case CoalesceExpression coalesceExpression:
                foreach (var e in coalesceExpression.Expressions)
                {
                    result = ParseScalarExpression(e, result);
                }
                return(result);

            case SimpleCaseExpression simpleCaseExpression:
                result = ParseScalarExpression(simpleCaseExpression.InputExpression, result);
                foreach (var when in simpleCaseExpression.WhenClauses)
                {
                    result = ParseScalarExpression(when.ThenExpression, result);
                }
                return(result);

            case Literal _:
            case GlobalVariableExpression _:
            case IdentityFunctionCall _:
                return(result);
            }

            Debug.WriteLine(expression.GetType() + " ignored");
            return(result);
        }
Example #5
0
 public MergeStatamentParsingException(string name, int ordinal, ScalarExpression columnValue, int rowIndex)
     : base(string.Format("Could not parse column ordinal: {1} in table: {0}, row number: {3} extra info:  base type: {2}",
                          name, ordinal, columnValue.GetType(), rowIndex))
 {
 }
 public MergeStatamentParsingException(string name, int ordinal, ScalarExpression columnValue, int rowIndex)
     : base(string.Format("Could not parse column ordinal: {1} in table: {0}, row number: {3} extra info:  base type: {2}",
         name, ordinal, columnValue.GetType(), rowIndex))
 {
 }