Class: FluentQuery::Drivers::Shared::Tokens::SQL::OrderBy

Inherits:
FluentQuery::Drivers::Shared::Tokens::SQLToken show all
Defined in:
lib/fluent-query/drivers/shared/tokens/sql/orderby.rb

Overview

Generic SQL query ORDER BY token.

Constant Summary

Constants inherited from FluentQuery::Drivers::Shared::Tokens::SQLToken

FluentQuery::Drivers::Shared::Tokens::SQLToken::TRANSFORMER

Instance Method Summary collapse

Methods inherited from FluentQuery::Drivers::Shared::Tokens::SQLToken

#initialize

Constructor Details

This class inherits a constructor from FluentQuery::Drivers::Shared::Tokens::SQLToken

Instance Method Details

#render!(mode = :build) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fluent-query/drivers/shared/tokens/sql/orderby.rb', line 22

def render!(mode = :build)
    
    stack = [ ]
    custom = [ ]
    unknown = [ ]
    fields = [ ]
    distinct = false
    
    result = "ORDER BY "
    processor = @_query.processor
    _class = self.unknown_token


    # Process subtokens
    
    @_subtokens.each do |token|
        arguments = token.arguments
        name = token.name

        # Known select process
        if name == :orderBy
            length = arguments.length
            close_custom = false
            
            if length > 0
                if (length > 1) or (arguments.first.string?)
                    value = processor.process_formatted(arguments, mode).dup
                    custom.push(value)

                    # Closes fields if they are set
                    if not fields.empty?
                        stack << (processor.process_identifiers(fields) << ", ")
                        fields = [ ]
                    end
                    
                elsif arguments.first.array?
                    fields += arguments.first
                    close_custom = true
                    
                elsif arguments.first.symbol?
                    fields += arguments
                    close_custom = true

                end
                
                # Closes custom formulations if they are set
                #  and it's required.
                if close_custom and not custom.empty?
                    stack << (self._close_custom(custom) << ", ")
                    custom = [ ]
                end
            end

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

        # Unknowns arguments pushes to the general native token
        else
            unknown << token

            if not fields.empty?
                stack << processor.process_identifiers(fields)
                fields = [ ]
            elsif not custom.empty?
                stack << self._close_custom(custom)
                custom = [ ]
            end
        end
    end

    # Closes

        # Opened native token with unknown tokens
        if unknown.length > 0
            native = _class::new(@_driver, @_query, unknown)
            value = native.render!
            unknown = [ ]

            if (not fields.empty?) or (not custom.empty?)
                value << ", "
            end
            
            stack << value
        end

        # Fields list
        if not fields.empty?
            value = processor.process_identifiers(fields)

            if not custom.empty?
                value << ", "
            end

            stack << value
        end

        # Custom list
        if not custom.empty?
            stack << self._close_custom(custom)
        end

    # Process stack with results
    result << stack.join(" ")

    return result

end