Class: FluentQuery::Connection

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

Overview

Represents query target connection.

Instance Method Summary collapse

Constructor Details

#initialize(driver_class, settings = nil, open = true) ⇒ Connection

Returns a new instance of Connection.



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
# File 'lib/fluent-query/connection.rb', line 55

def initialize(driver_class, settings = nil, open = true)

    # Analyses

    driver = driver_class::new(self)

    if not driver.kind_of? FluentQuery::Driver
        raise FluentQuery::Exception::new("Driver must be subclass of the 'FluentQuery::Driver' class.")
    end

    # Assigns

    @_driver_class = driver_class
    @_settings = settings
    @_open = false
    @_debug = false

    # Opens if required

    if open
        @_driver = driver
        self.open!
    end
   
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fluent-query/connection.rb', line 89

def method_missing(sym, *args, &block)
    
    # Checks if connection is in open state
    if not @_open
        raise FluentQuery::Exception::new("Connection is closed.")
    end

    ##
    
    driver = self.driver
    
    if driver.relevant_method? sym
        return __query_call(sym, *args, &block)
    else
        raise FluentQuery::Exception::new("Method '" << sym.to_s << "' isn't implemented by associated FluentQuery::Driver or FluentQuery::Connection object.")
    end
end

Instance Method Details

#close!Object



188
189
190
191
192
193
194
195
# File 'lib/fluent-query/connection.rb', line 188

def close!
    if @_driver
        self.driver.close_connection!
        @_driver = nil
    end
    
    @_open = false
end

#do(query) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/fluent-query/connection.rb', line 232

def do(query)

    # Checks if connection is in open state
    if not @_open
        raise FluentQuery::Exception::new("Connection is closed.")
    end

    # If query is fluent query object, executes it
    if query.kind_of? FluentQuery::Query
        result = query.do!
    else
        if @_debug == :dump_all
            puts query
        end
            
        result = self.driver.do(query)
    end

    return result
    
end

#driverObject



146
147
148
149
150
151
152
153
154
# File 'lib/fluent-query/connection.rb', line 146

def driver

    # Checks if connection is in open state
    if @_driver.nil?
        @_driver = @_driver_class::new(self)
    end
    
    return @_driver
end

#execute(query) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/fluent-query/connection.rb', line 202

def execute(query)

    # Checks if connection is in open state
    if not @_open
        raise FluentQuery::Exception::new("Connection is closed.")
    end

    # If query is fluent query object, executes it
    if query.kind_of? FluentQuery::Query
        result = query.execute!
    else
        if @_debug == :dump_all
            puts query
        end
        
        result = self.driver.execute(query)
    end

    # Wraps driver result to result class
    result = FluentQuery::Result::new(result)

    return result
    
end

#open!(settings = nil) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/fluent-query/connection.rb', line 161

def open!(settings = nil)

    if @_open
        raise FluentQuery::Exception::new("Connection is already open.")
    end

    ##

    if (settings == nil) and @_settings
        settings = @_settings
    elsif settings == nil
        raise FluentQuery::Exception::new("Connection settings hasn't been set or given to the #open method.")
    end
    
    self.driver.open_connection(settings)

    ##

    @_open = true
    
end

#query(*args, &block) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/fluent-query/connection.rb', line 128

def query(*args, &block)
    query = self._new_query_object
    
    # Calls given block in query context.
    if block
        result = query.instance_eval(&block)
    else
        result = query
    end
    
    return query
end

#set_debug(mode) ⇒ Object



116
117
118
# File 'lib/fluent-query/connection.rb', line 116

def set_debug(mode)
    @_debug = mode
end

#transaction(&block) ⇒ Object



259
260
261
262
263
264
265
266
267
268
# File 'lib/fluent-query/connection.rb', line 259

def transaction(&block)
    self.begin
    begin
        block.call
    rescue ::Exception => e
        self.rollback
        raise e
    end
    self.commit
end