Exemplo n.º 1
0
        public override void Visit(SelectStatement node)
        {
            var commonTableExpressions = node.WithCtesAndXmlNamespaces?.CommonTableExpressions;
            var cteAliases             = new List <string>();

            if (commonTableExpressions != null)
            {
                foreach (var commonTableExpression in commonTableExpressions)
                {
                    var exprName = commonTableExpression.ExpressionName.Value;
                    cteAliases.Add(exprName);
                    var cteQuerySpecification = commonTableExpression.QueryExpression as QuerySpecification;
                    ProcessFromClause(cteQuerySpecification?.FromClause, SqlAction.Select);
                    ProcessWhereClause(cteQuerySpecification?.WhereClause, SqlAction.Select);
                }
            }

            var fromIds = ProcessSelect(node.QueryExpression);

            // Remove aliases
            if (fromIds != null)
            {
                foreach (var fromId in fromIds)
                {
                    if (cteAliases.Contains(fromId.Name))
                    {
                        SelectTables.RemoveWhere(t => t == fromId.Name);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void ProcessFunctionCall(FunctionCall functionCall)
        {
            var func = functionCall.FunctionName.Value;

            if (!SqlUtils.IsBuiltInFunction(func))
            {
                SelectTables.Add(func);
            }
        }
Exemplo n.º 3
0
        private IList <TableIdentifier> ProcessTableReferences(IList <TableReference> tRefs, SqlAction action, HashSet <string> tablesParam = null)
        {
            var identifiers = new List <TableIdentifier>();

            if (tRefs != null)
            {
                foreach (var tref in tRefs)
                {
                    var namedRef = tref as NamedTableReference;
                    if (namedRef != null)
                    {
                        var tables     = tablesParam ?? GetTables(action);
                        var identifier = new TableIdentifier(string.Empty, string.Empty);
                        if (!string.IsNullOrWhiteSpace(namedRef?.Alias?.Value))
                        {
                            identifier.Alias = namedRef?.Alias?.Value;
                        }
                        var table = GetVal(namedRef);
                        identifier.Name = table;
                        identifiers.Add(identifier);
                        if (table != null)
                        {
                            tables.Add(table);
                        }
                    }

                    var joinRef = tref as QualifiedJoin;
                    if (joinRef != null)
                    {
                        var joinIds = ProcessJoin(joinRef, action);
                        identifiers.AddRange(joinIds);
                    }

                    var uqJoinRef = tref as UnqualifiedJoin;
                    if (uqJoinRef != null)
                    {
                        var joinIds = ProcessJoin(uqJoinRef, action);
                        identifiers.AddRange(joinIds);
                    }

                    var pivotedRef = tref as PivotedTableReference;
                    if (pivotedRef != null)
                    {
                        ProcessTableReference(pivotedRef.TableReference, SqlAction.Select);
                    }

                    if (tref is QueryDerivedTable)
                    {
                        var queryExpr =
                            (tref as QueryDerivedTable).QueryExpression;
                        if (queryExpr != null)
                        {
                            ProcessSelect(queryExpr);
                        }
                    }
                    else
                    if (tref is SchemaObjectFunctionTableReference)
                    {
                        var schemaObject = tref as SchemaObjectFunctionTableReference;
                        var function     = GetVal(schemaObject.SchemaObject);
                        var id           = new TableIdentifier(schemaObject.Alias?.Value, function);
                        identifiers.Add(id);
                        if (!string.IsNullOrWhiteSpace(function))
                        {
                            SelectTables.Add(function);
                        }
                    }
                }
            }
            return(identifiers);
        }