Class: NativeQuery::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/native-query/query.rb

Overview

Represents ORM query.

Instance Method Summary collapse

Constructor Details

#initialize(connection, table, &block) ⇒ Query

Constructor.



81
82
83
84
85
86
87
88
89
90
# File 'lib/native-query/query.rb', line 81

def initialize(connection, table, &block)
    @connection = connection
    @table = table
    @fields = [ ]
    @where = [ ]
    @having = [ ]
    @joins = [ ]
    @order = [ ]
    @group = [ ]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Calls mapping to joins specification. Call name is name of the target table.

Block works by the same way as query, but for join. But intra-join calls doesn’t work because it returns Query too.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/native-query/query.rb', line 100

def method_missing(sym, *args, &block)
    join = Join::new(@table, sym)

    if args and not args.empty?
        join.fields(*args)
    end
    
    if not block.nil?
        join.instance_eval(&block)
    end
    
    @joins << join
    return self
end

Instance Method Details

#buildObject Also known as: build!

Builds itself to string.



256
257
258
# File 'lib/native-query/query.rb', line 256

def build
    self.get.query.build
end

#fields(*args) ⇒ Object

Selects fields which to load.



119
120
121
122
# File 'lib/native-query/query.rb', line 119

def fields(*args)
    @fields += args
    return self
end

#getObject

Returns result object.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/native-query/query.rb', line 185

def get
    
    # Builds query
    
        # Process joins
        join_fields, wheres, append_joins = self._process_joins
        
        # Checkouts regular fields
        if @joins.empty?
            fields = @fields
            hashes = [ ]
            group = @group
        else
            fields = @fields.map { |i| __fix_field(i) } 
            hashes = fields.reject { |i| not i.hash? }
            fields -= hashes
        end
        
        
        query = @connection.query
        
        if not fields.empty?
            query.select(fields)
        end
        
        if not join_fields.empty?
            query.select(join_fields)
        end
        
        if not hashes.empty?
            hashes.each do |hash|
                query.select(hash)
            end
        end


        query.from(@table)

        # Appends joins
        append_joins.call(query)
        
        # Where conditions
        wheres += self._fix_where
        wheres.each { |i| query.where(*i) }
        
        # Where conditions
        havings = self._fix_having
        havings.each { |i| query.having(*i) }
          
        # Grouping, ordering and having settings
        self._process_grouping(query)
        self._process_ordering(query)
        
        # Limit and offset
        if not @limit.nil?
            query.limit(@limit)
        end
        
        if not @offset.nil?
            query.offset(@offset)
        end

    # Returns
    return Result::new(query)
    
end

#group(*fields) ⇒ Object

Sets group by to load.



176
177
178
179
# File 'lib/native-query/query.rb', line 176

def group(*fields)
    @group = fields
    return self
end

#having(*args) ⇒ Object

Selects having conditions to load.



137
138
139
140
# File 'lib/native-query/query.rb', line 137

def having(*args)
    @having << args
    return self
end

#limit(limit) ⇒ Object

Sets limit to load.



158
159
160
161
# File 'lib/native-query/query.rb', line 158

def limit(limit)
    @limit = limit
    return self
end

#offset(offset) ⇒ Object

Sets offset to load.



167
168
169
170
# File 'lib/native-query/query.rb', line 167

def offset(offset)
    @offset = offset
    return self
end

#order(*args) ⇒ Object

Selects fields for ordering according them. Default order is :asc.

Expects [:<field>, [:asc|:desc]]+ arguments.



149
150
151
152
# File 'lib/native-query/query.rb', line 149

def order(*args)
    @order += args
    return self
end

#where(*args) ⇒ Object

Selects where conditions to load.



128
129
130
131
# File 'lib/native-query/query.rb', line 128

def where(*args)
    @where << args
    return self
end