Class: FluentQuery::Drivers::DBI Abstract

Inherits:
SQL
  • Object
show all
Defined in:
lib/fluent-query/drivers/dbi.rb,
lib/fluent-query/drivers/dbi/prepared.rb

Overview

This class is abstract.

Generic DBI database driver.

Defined Under Namespace

Classes: Prepared

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ DBI

Returns a new instance of DBI.



25
26
27
28
29
30
31
# File 'lib/fluent-query/drivers/dbi.rb', line 25

def initialize(connection)
    if self.instance_of? DBI
        not_implemented
    end

    super(connection)
end

Instance Method Details

#authentificationObject



119
120
121
# File 'lib/fluent-query/drivers/dbi.rb', line 119

def authentification
    @_nconnection_settings.take_values(:username, :password)
end

#check_conditionally(query, sym, *args, &block) ⇒ Object



218
219
220
221
222
223
224
# File 'lib/fluent-query/drivers/dbi.rb', line 218

def check_conditionally(query, sym, *args, &block)
    if sym.start_with? "prepare_" 
        self.prepare_conditionally(query, sym, *args, &block)
    else
        super(query, sym, *args, &block)
    end
end

#close_connection!Object



164
165
166
167
168
# File 'lib/fluent-query/drivers/dbi.rb', line 164

def close_connection!
    if not @_nconnection.nil?
        @_nconnection.disconnect
    end
end

#connection_stringObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fluent-query/drivers/dbi.rb', line 68

def connection_string   
             
    if @_nconnection_settings.nil?
        raise FluentQuery::Drivers::Exception::new('Connection settings hasn\'t been assigned yet.')
    end
    
    # Gets settings
    
    server = @_nconnection_settings[:server]
    port = @_nconnection_settings[:port]
    socket = @_nconnection_settings[:socket]
    database = @_nconnection_settings[:database]
    
    # Builds connection string and other parameters
    
    if server.nil?
        server = "localhost"
    end
    
    connection_string = "DBI:%s:database=%s;host=%s" % [self.driver_name, database, server]
    
    if not port.nil?
        connection_string << ";port=" << port.to_s
    end
    if not socket.nil?
        connection_string << ";socket=" << socket
    end

    # Returns 
    return connection_string
    
end

#correct_token(name, args) ⇒ Object



261
262
263
264
265
266
267
# File 'lib/fluent-query/drivers/dbi.rb', line 261

def correct_token(name, args)
    if name.start_with? "prepare_" 
        name = name[8..-1].to_sym
    end
    
    return [name, args]
end

#do(query) ⇒ Object



192
193
194
195
196
197
# File 'lib/fluent-query/drivers/dbi.rb', line 192

def do(query)
    connection = self.native_connection
    count = connection.do(query)

    return count
end

#driver_nameObject



109
110
111
# File 'lib/fluent-query/drivers/dbi.rb', line 109

def driver_name
    not_implemented
end

#execute(query) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/fluent-query/drivers/dbi.rb', line 175

def execute(query)
    connection = self.native_connection
    
    if query.array? and query.first.kind_of? FluentQuery::Drivers::DBI::Prepared
        data = query.first.execute(*query.second)
    else
        data = connection.execute(query.to_s)
    end
    
    return FluentQuery::Drivers::Shared::Results::DBI::new(data)
end

#native_connectionObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/fluent-query/drivers/dbi.rb', line 140

def native_connection

    if not @_nconnection_settings
        raise FluentQuery::Drivers::Exception::new("Connection is closed.")
    end

    if @_nconnection.nil?
        require "dbi"
        
        # Gets authentification
        username, password = self.authentification

        # Connects
        @_nconnection = ::DBI::connect(self.connection_string, username, password)
    end

    return @_nconnection                    
end

#open_connection(settings) ⇒ Object



131
132
133
# File 'lib/fluent-query/drivers/dbi.rb', line 131

def open_connection(settings)
    @_nconnection_settings = settings
end

#prepare(query) ⇒ Object



204
205
206
# File 'lib/fluent-query/drivers/dbi.rb', line 204

def prepare(query)
    DBI::Prepared::new(self, query)
end

#prepare_conditionally(query, sym, *args, &block) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/fluent-query/drivers/dbi.rb', line 236

def prepare_conditionally(query, sym, *args, &block)
    case query.type
        when :insert
            if (args.first.symbol?) and (args.second.hash?)
                result = query.prepare!
            end
        when :truncate
            if args.first.symbol?
                result = query.prepare!
            end
        else
            result = nil
    end
    
    return result
end

#quote_placeholderObject



56
57
58
# File 'lib/fluent-query/drivers/dbi.rb', line 56

def quote_placeholder
    "?"
end

#relevant_method?(name) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
# File 'lib/fluent-query/drivers/dbi.rb', line 41

def relevant_method?(name)
    if name.start_with? "prepare_"
        _name = name[8..-1].to_sym
    else
        _name = name
    end
    
    super(_name)
end