예제 #1
0
 private void GetNamedTableReferences(FromClause from, SqlParserResult result)
 {
     foreach (var reference in from.TableReferences)
     {
         CollectNamedTableReferences(reference, result);
     }
 }
예제 #2
0
        private SqlParserResult CreateNewResult(TSqlTokenType type)
        {
            var r = new SqlParserResult {
                Type = type
            };

            return(r);
        }
예제 #3
0
 private void StoreResult(SqlParserResult result)
 {
     if (result == null)
     {
         return;
     }
     if (Result == null)
     {
         Result = new List <SqlParserResult>();
     }
     Result.Add(result);
 }
예제 #4
0
        private void CollectNamedTableReferences(TableReference reference, SqlParserResult result)
        {
            var referenceModel = reference.ToTableReferenceModel();

            if (referenceModel != null)
            {
                ExtractNamedTableReference(referenceModel, result);
                return;
            }

            switch (reference)
            {
            case JoinTableReference joinTableReference:
                CollectNamedTableReferences(joinTableReference.FirstTableReference, result);
                CollectNamedTableReferences(joinTableReference.SecondTableReference, result);
                return;

            case JoinParenthesisTableReference joinParenthesisTableReference:
                CollectNamedTableReferences(joinParenthesisTableReference.Join, result);
                return;

            case QueryDerivedTable queryDerivedTable:
                ParseSelect(queryDerivedTable.QueryExpression, result);
                result.Aliases[queryDerivedTable.Alias.Value.ToLower()] = null;
                return;

            case PivotedTableReference pivotedTableReference:
                CollectNamedTableReferences(pivotedTableReference.TableReference, result);
                return;

            case UnpivotedTableReference unpivotedTableReference:
                CollectNamedTableReferences(unpivotedTableReference.TableReference, result);
                return;

            case VariableTableReference variableTableReference:
                var statements = ParsedScript?.Batches.Single().Statements;
                if (statements != null)
                {
                    foreach (var p in statements.OfType <ProcedureStatementBodyBase>().SelectMany(s => s.Parameters).Where(p => p.VariableName.Value == variableTableReference.Variable.Name))
                    {
                        if (result == null)
                        {
                            result = CreateNewResult(TSqlTokenType.Select);
                        }
                        result.Tables.Add(GetFQParameterType(p));
                    }
                }
                break;
            }
        }
예제 #5
0
        private SqlParserResult ParseSelect(QueryExpression expression, SqlParserResult result = null)
        {
            switch (expression)
            {
            case QuerySpecification querySpecification:
                if (querySpecification.FromClause != null)
                {
                    foreach (var element in querySpecification.SelectElements)
                    {
                        switch (element)
                        {
                        case SelectScalarExpression scalarExpression:
                            result = ParseScalarExpression(scalarExpression.Expression, result);
                            continue;

                        case SelectStarExpression starExpression:
                            if (result == null)
                            {
                                result = CreateNewResult(TSqlTokenType.Select);
                            }
                            result.Fields.Add(new SqlColumnDefinition(new List <Identifier> {
                                new Identifier {
                                    Value = "*"
                                }
                            }));
                            break;
                        }
                    }

                    if (result != null)
                    {
                        GetNamedTableReferences(querySpecification.FromClause, result);
                    }
                }
                return(result);

            case BinaryQueryExpression binaryQueryExpression:
                result = ParseSelect(binaryQueryExpression.FirstQueryExpression, result);
                result = ParseSelect(binaryQueryExpression.SecondQueryExpression, result);
                return(result);

            case QueryParenthesisExpression queryParenthesisExpression:
                result = ParseSelect(queryParenthesisExpression.QueryExpression, result);
                return(result);
            }

            Debugger.Break();
            return(result);
        }
예제 #6
0
        private SqlParserResult ParseIf(BooleanExpression expression, SqlParserResult result = null)
        {
            switch (expression)
            {
            case BooleanBinaryExpression booleanBinaryExpression:
                result = ParseIf(booleanBinaryExpression.FirstExpression, result);
                result = ParseIf(booleanBinaryExpression.SecondExpression, result);
                return(result);

            case BooleanComparisonExpression booleanComparisonExpression:
                result = ParseScalarExpression(booleanComparisonExpression.FirstExpression, result);
                result = ParseScalarExpression(booleanComparisonExpression.SecondExpression, result);
                return(result);

            case BooleanIsNullExpression booleanIsNullExpression:
                result = ParseScalarExpression(booleanIsNullExpression.Expression, result);
                return(result);

            case BooleanParenthesisExpression booleanParenthesisExpression:
                result = ParseIf(booleanParenthesisExpression.Expression, result);
                return(result);

            case ExistsPredicate existsPredicate:
                result = ParseScalarExpression(existsPredicate.Subquery, result);
                return(result);

            case BooleanNotExpression booleanNotExpression:
                result = ParseIf(booleanNotExpression.Expression, result);
                return(result);

            case InPredicate inPredicate:
                result = ParseScalarExpression(inPredicate.Expression, result);
                result = ParseScalarExpression(inPredicate.Subquery, result);
                return(result);

            case LikePredicate likePredicate:
                result = ParseScalarExpression(likePredicate.FirstExpression, result);
                result = ParseScalarExpression(likePredicate.SecondExpression, result);
                return(result);
            }
            return(result);
        }
예제 #7
0
        public override void Visit(UpdateStatement updateStatement)
        {
            SqlParserResult result = null;
            var             us     = updateStatement.UpdateSpecification;

            foreach (var set in us.SetClauses)
            {
                var assignmentSetClause = set as AssignmentSetClause;
                if (assignmentSetClause == null)
                {
                    Debugger.Break();
                    continue;
                }

                if (assignmentSetClause.Column == null)
                {
                    continue;
                }

                if (result == null)
                {
                    result = CreateNewResult(TSqlTokenType.Update);
                }
                result.Fields.Add(new SqlColumnDefinition(assignmentSetClause.Column.MultiPartIdentifier.Identifiers));
            }

            if (result == null)
            {
                return;
            }
            if (us.FromClause != null)
            {
                GetNamedTableReferences(us.FromClause, result);
            }
            else
            {
                ExtractNamedTableReference(updateStatement.UpdateSpecification.Target.ToTableReferenceModel(), result);
            }
            StoreResult(result);
        }
예제 #8
0
        private static void ExtractNamedTableReference(TableReferenceModel tableReference, SqlParserResult result)
        {
            if (tableReference == null)
            {
                return;
            }

            var baseObjectName = tableReference.SchemaObject.AsObjectName();

            result.Tables.Add(baseObjectName);

            var alias = tableReference.Alias;

            if (alias == null)
            {
                return;
            }
            result.Aliases[alias.Value.ToLower()] = baseObjectName;
        }
예제 #9
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);
        }