class FluentQuery::Drivers::Shared::Tokens::SQL::Join

Generic SQL query [LEFT|RIGHT|INNER|OUTER] JOIN token.

Public Instance Methods

render!(mode = :build) click to toggle source

Renders this token.

# File lib/fluent-query/drivers/shared/tokens/sql/join.rb, line 22
def render!(mode = :build)
    stack = [ ]
    unknown = [ ]

    _class = self.unknown_token
    processor = @_query.processor
    result = ""

    @_subtokens.each do |token|
        arguments = token.arguments

        # JOIN token
        if token.name == :join
        
            original_name = token.original_name
            stack << self._transform_token(original_name.to_s)
    
            ##
            
            length = arguments.length
            first = arguments.first
            
            if length > 0
                if first.symbol?
                    stack << processor.process_identifiers(arguments)
                elsif (length > 1) or (first.string?)
                    stack << processor.process_formatted(arguments, mode)
                elsif first.hash?
                    t_name = first.keys.first
                    t_alias = first.values.first
                    stack << (processor.quote_identifier(t_name) + " AS " + processor.quote_identifier(t_alias))
                end
            end

            # Closes opened native token with unknown tokens
            if unknown.length > 0
                native = _class::new(@_driver, @_query, unknown)
                stack << native
                unknown = [ ]
            end

        # Unknown arguments pushes to the general native token
        else
            unknown << token
        end
        
    end

    # Closes opened native token with unknown tokens
    if unknown.length > 0
        native = _class::new(@_driver, @_query, unknown)
        stack << native
        unknown = [ ]
    end

    # Process stack with results
    first = true
    
    stack.each do |item|
        if item.kind_of? _class
            result << item.render!
        elsif item.string?               
            result << item
        end

        result << " "
    end

    return result
end