Class: SQLExporter::Exporter_generic

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

Overview

The exporter class of a ‘generic’ sql dialect. This should be the parent for

all dialect-specific exporter classes.

Direct Known Subclasses

Exporter_mysql

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tidy) ⇒ Exporter_generic

Class constructor.



53
54
55
56
# File 'lib/sqlexporter.rb', line 53

def initialize ( tidy )
    @tidy = tidy
    @separator = @tidy ? "\n" : " "
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Construct an expression string for an object’s attribute defined in

in the METHODS constant array.


99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sqlexporter.rb', line 99

def method_missing ( method, *args )
    obj = args[0]
    return ''  if ! obj.is_a? SQLObject
    result = ""
    _attr = obj.send method
    if _attr.is_a? Array
        result += _attr.join @separator
    elsif _attr
        result += _attr.to_s
    end
 
    return result
end

Instance Attribute Details

#separatorObject (readonly)

Returns the value of attribute separator.



48
49
50
# File 'lib/sqlexporter.rb', line 48

def separator
  @separator
end

Instance Method Details

#export(obj) ⇒ Object

exports a string with a query from object.

this method should be called from a child class with defined constant
arrays [select|delete|update|insert]_syntax. methods defined in the array 
are called in the specified order for the object obj.


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sqlexporter.rb', line 64

def export ( obj )
    rules_const_name = obj.class.name.sub( /^.+?::Basic([^_]+).+/, '\1' ).upcase + "_SYNTAX"
    begin
        rules = self.class.const_get( rules_const_name.to_sym )
    rescue NameError
        raise NameError, ERR_INVALID_RULES + " '" + self.send( :dialect ) + "'"
    end
    string = ""
    rules.each do |rule|
        if rule.is_a? String
            string += rule + @separator
        elsif rule.is_a? Symbol
            res = self.send( rule, obj ).to_s
            string += res
            string += @separator  if ! res.empty?
        end
    end
    return string
end

#gen_joins(obj) ⇒ Object

Generic representation of the JOIN clause



87
88
89
90
91
92
93
# File 'lib/sqlexporter.rb', line 87

def gen_joins ( obj )
    arr_joins = [ ]
    if obj.gen_joins 
        arr_joins = obj.gen_joins.map { |join|  join.to_s }
    end
    return arr_joins.join( @separator )
end