Class: SQLConstructor::GenericQuery

Inherits:
SQLObject
  • Object
show all
Defined in:
lib/sqlconstructor.rb

Overview

Internal class - generic query attributes and methods. Should be parent to all Basic*

classes.

Instance Attribute Summary collapse

Attributes inherited from SQLObject

#alias, #inline, #name, #separator

Instance Method Summary collapse

Methods inherited from SQLObject

#_name, #_string, #_string=, get

Constructor Details

#initialize(_caller) ⇒ GenericQuery

Class constructor.

_caller     - the caller object


242
243
244
245
246
247
248
249
# File 'lib/sqlconstructor.rb', line 242

def initialize ( _caller )
    @caller   = _caller
    @dialect  = @caller.dialect
    @tidy     = @caller.tidy
    @exporter = _caller.exporter
    @inline   = @caller.inline
    self._setMethods
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Process method calls described in the child’s METHODS attribute.

If no corresponding entries are found in all object's parent classes,
then send missing methods calls to the @caller object.

Raises:

  • (NoMethodError)


303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/sqlconstructor.rb', line 303

def method_missing ( method, *args )
     # If the method is described in the class' METHODS constant, then
     # create an attribute with the proper name, an attr_accessor
     # for it, and set it's value to the one in METHODS.
    if @methods.has_key? method
        _attr = @methods[method].dup
        attr_name = _attr.name
        val_obj = nil

         # get the current value of the attribute {_attr.name}
        self.class.send :attr_accessor, attr_name.to_sym
        cur_attr = self.send attr_name.to_sym
        cur_attr_val = cur_attr.is_a?( QAttr )  ? cur_attr.val  : cur_attr
 
         # Create an instance of the corresponding class if _attr.val is 
         # on of SQLObject container classes:
        if [ SQLValList, SQLAliasedList, SQLCondList ].include? _attr.val
            _attr.val = _attr.val.new *args

         # Create an array of SQLObjects if _attr.val is SQLObject class:
        elsif _attr.val == SQLObject
            _attr.val = SQLObject.get *args #args.map{ |arg|  SQLObject.get arg }

         # Create an instance of the corresponding class if _attr.val is 
         # SQLConstructor or SQLConditional class:
        elsif [ SQLConstructor, SQLConditional ].include? _attr.val
            val_obj = cur_attr_val || _attr.val.new(
                                            :dialect  => @dialect,
                                            :tidy     => @tidy,
                                            :exporter => @exporter,
                                            :caller   => self
                                      )
            _attr.val = val_obj

         # create a BasicSelect dialect-specific child class: 
        elsif _attr.val == BasicSelect
            val_obj = SQLConstructor.new( 
                                            :dialect  => @dialect, 
                                            :tidy     => @tidy,
                                            :exporter => @exporter 
                                        ).select( *args )
            _attr.val = val_obj

         # If the :val parameter is some different class, then we should 
         # create an instance of it or return the existing value:
        elsif _attr.val && _attr.val.ancestors.include?( GenericQuery )
            val_obj = _getBasicClass( _attr.val, _attr.text )
            _attr.val = val_obj
        end

         # If the object already has attribute {_attr.name} defined and it's
         # an array or one of the SQL* class containers,then we should rather 
         # append to it than reassign the value.
         # If :attr_val=list, then create a new SQLAliasedList container.
        if [ Array, SQLValList, SQLAliasedList, SQLCondList ].include?( cur_attr_val.class ) ||
           _attr.val_type == 'list'
            cur_attr_val ||= SQLAliasedList.new
            cur_attr_val.no_commas = true  if _attr.no_commas
            cur_attr_val.push _attr.val
            _attr = cur_attr_val
        end

#                self.class.send :attr_accessor, attr_name.to_sym  if ! cur_attr_val
        self.send "#{attr_name}=", _attr

        @string = nil
        return ( val_obj || self )
    end

     # Otherwise send the call to @caller object
    @child_caller = self
    return @caller.send( method.to_sym, *args )  if @caller
    raise NoMethodError, ERR_UNKNOWN_METHOD + ": " + method.to_s
end

Instance Attribute Details

#attr_index_hintsObject (readonly)

Returns the value of attribute attr_index_hints.



233
234
235
# File 'lib/sqlconstructor.rb', line 233

def attr_index_hints
  @attr_index_hints
end

#callerObject

Returns the value of attribute caller.



232
233
234
# File 'lib/sqlconstructor.rb', line 232

def caller
  @caller
end

#child_callerObject (readonly)

Returns the value of attribute child_caller.



233
234
235
# File 'lib/sqlconstructor.rb', line 233

def child_caller
  @child_caller
end

#dialectObject (readonly)

Returns the value of attribute dialect.



233
234
235
# File 'lib/sqlconstructor.rb', line 233

def dialect
  @dialect
end

#exporterObject (readonly)

Returns the value of attribute exporter.



233
234
235
# File 'lib/sqlconstructor.rb', line 233

def exporter
  @exporter
end

#stringObject

Returns the value of attribute string.



232
233
234
# File 'lib/sqlconstructor.rb', line 232

def string
  @string
end

#tidyObject (readonly)

Returns the value of attribute tidy.



233
234
235
# File 'lib/sqlconstructor.rb', line 233

def tidy
  @tidy
end

#typeObject (readonly)

Returns the value of attribute type.



233
234
235
# File 'lib/sqlconstructor.rb', line 233

def type
  @type
end

Instance Method Details

#_get(clause, *args) ⇒ Object

Returns an object by clause (keys of child class’ METHODS attribute)

or by SQLObject.name


255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/sqlconstructor.rb', line 255

def _get ( clause, *args )
    result = nil
    if @methods.has_key? clause
        name = args  ? args[0]  : nil
        result = self.send @methods[clause].name
        result = result.val  if result.is_a? QAttr
        if name && [ Array, SQLValList, SQLAliasedList, SQLCondList ].include?( result.class )
             # return the first object if multiple objects have the same name
            result = result.find { |obj|  obj.name == name }
        end
    end                         
    return result
end

#_remove(clause, *args) ⇒ Object

NILs attribute by clause name (specified in the child class’ METHODS

attribure), or removes an named item from a list attribute.
This method must be overriden in child classes if any methods were 
defined explicitly (not in METHODS).


275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/sqlconstructor.rb', line 275

def _remove ( clause, *args )
    if @methods.has_key? clause
        _attr = self.send @methods[clause].name
        name = args  ? args[0]  : nil
        if name && [ Array, SQLValList, SQLAliasedList, SQLCondList ].include?( _attr.class )
            _attr.delete_if { |obj|  obj.name == name }
        else
            self.send "#{@methods[clause].name}=", nil
        end
        @string = nil
    end                         
    return self
end

#to_sObject

Convert object to string by calling the .export() method of

the @exporter object.


293
294
295
296
# File 'lib/sqlconstructor.rb', line 293

def to_s
    return @string  if @string
    @string = @exporter.export self
end

#to_strObject

Dirty hack to make .join work on an array of GenericQueries



236
# File 'lib/sqlconstructor.rb', line 236

alias :to_str :to_s