filter_columns

in lib/pg_query/filter_columns.rb [6:89]


    def filter_columns 
      load_tables_and_aliases! if @aliases.nil?

      
      statements = @tree.stmts.dup.to_a.map(&:stmt)
      condition_items = []
      filter_columns = []
      loop do
        statement = statements.shift
        if statement
          case statement.node
          when :list
            statements += statement.list.items
          when :raw_stmt
            statements << statement.raw_stmt.stmt
          when :select_stmt
            case statement.select_stmt.op
            when :SETOP_NONE
              if statement.select_stmt.from_clause
                
                statement.select_stmt.from_clause.each do |item|
                  next unless item['RangeSubselect']
                  statements << item['RangeSubselect']['subquery']
                end

                
                condition_items += conditions_from_join_clauses(statement.select_stmt.from_clause)
              end

              
              condition_items << statement.select_stmt.where_clause if statement.select_stmt.where_clause

              
              if statement.select_stmt.with_clause
                statement.select_stmt.with_clause.ctes.each do |item|
                  statements << item.common_table_expr.ctequery if item.node == :common_table_expr
                end
              end
            when :SETOP_UNION
              statements << statement.select_stmt.larg if statement.select_stmt.larg
              statements << statement.select_stmt.rarg if statement.select_stmt.rarg
            end
          when :update_stmt
            condition_items << statement.update_stmt.where_clause if statement.update_stmt.where_clause
          when :delete_stmt
            condition_items << statement.delete_stmt.where_clause if statement.delete_stmt.where_clause
          end
        end

        
        next_item = condition_items.shift
        if next_item
          case next_item.node
          when :a_expr
            condition_items << next_item.a_expr.lexpr if next_item.a_expr.lexpr
            condition_items << next_item.a_expr.rexpr if next_item.a_expr.rexpr
          when :bool_expr
            condition_items += next_item.bool_expr.args
          when :coalesce_expr
            condition_items += next_item.coalesce_expr.args
          when :row_expr
            condition_items += next_item.row_expr.args
          when :column_ref
            column, table = next_item.column_ref.fields.map { |f| f.string.str }.reverse
            filter_columns << [@aliases[table] || table, column]
          when :null_test
            condition_items << next_item.null_test.arg
          when :boolean_test
            condition_items << next_item.boolean_test.arg
          when :func_call
            
            condition_items += next_item.func_call.args if next_item.func_call.args
          when :sub_link
            condition_items << next_item.sub_link.testexpr
            statements << next_item.sub_link.subselect
          end
        end

        break if statements.empty? && condition_items.empty?
      end

      filter_columns.uniq
    end