class SQLConstructor::GenericQuery
Internal class - generic query attributes and methods. Should be parent to all Basic* classes.
Attributes
Public Class Methods
Class constructor. _caller - the caller object
# 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
Public Instance Methods
Returns an object by clause (keys of child class' METHODS attribute) or by SQLObject.name
# 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
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).
# 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
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.
# 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
Convert object to string by calling the .export() method of the @exporter object.
# File lib/sqlconstructor.rb, line 293 def to_s return @string if @string @string = @exporter.export self end
Protected Instance Methods
Creates a new BasicJoin
object for the JOIN statement.
# File lib/sqlconstructor.rb, line 385 def _addJoin ( type, *tables ) @string = nil join = _getBasicClass BasicJoin, type, *tables @attr_joins ||= [ ] @attr_joins.push join return join end
Returns an instance of Basic* child dialect-specific class
# File lib/sqlconstructor.rb, line 396 def _getBasicClass ( class_basic, *args ) class_basic_name = class_basic.name.sub /^(?:\w+::)*/, '' class_child = class_basic_name + '_' + @dialect if SQLConstructor.const_defined? class_child.to_sym SQLConstructor.const_get( class_child.to_sym ).new self, *args else SQLConstructor.const_get( class_basic_name.to_sym ).new self, *args end end
Returns the METHODS hash of child dialect-specific class merged with parent's METHODS hash.
# File lib/sqlconstructor.rb, line 410 def _setMethods if ! @methods methods_self = { } self.class.ancestors.each do |_class| next if ! _class.ancestors.include? SQLConstructor::GenericQuery begin class_methods = _class.const_get :METHODS || { } rescue class_methods = { } end methods_self.merge! class_methods end @methods = methods_self end return @methods end