class FluentQuery::Drivers::DBI
Generic DBI
database driver. @abstract
Public Class Methods
Constructor.
# File lib/fluent-query/drivers/dbi.rb, line 25 def initialize(connection) if self.instance_of? DBI not_implemented end super(connection) end
Public Instance Methods
Returns authentification settings. @return [Array] with username and password
# File lib/fluent-query/drivers/dbi.rb, line 119 def authentification @_nconnection_settings.take_values(:username, :password) end
Checks query conditionally. It’s called after first token of the query.
@see FluentQuery::Drivers::SQL#execute_conditionally @see prepare_conditionally
@since 0.9.2
# 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
Closes the connection.
# File lib/fluent-query/drivers/dbi.rb, line 164 def close_connection! if not @_nconnection.nil? @_nconnection.disconnect end end
Builds connection string according to settings. @return [String] connection string
# 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
Corrects token before it’s pushed to the token. So allows to modify data assigned to the query from driver level.
@since 0.9.2
# 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
Executes the query and returns count of the changed/inserted rows.
# File lib/fluent-query/drivers/dbi.rb, line 192 def do(query) connection = self.native_connection count = connection.do(query) return count end
Returns DBI
driver name.
@return [String] driver name @abstract
# File lib/fluent-query/drivers/dbi.rb, line 109 def driver_name not_implemented end
Executes the query and returns data result.
# 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
Returns native connection.
# 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
Opens the connection.
It’s lazy, so it will open connection before first request through {@link native_connection
()} method.
# File lib/fluent-query/drivers/dbi.rb, line 131 def open_connection(settings) @_nconnection_settings = settings end
Generates prepared query.
# File lib/fluent-query/drivers/dbi.rb, line 204 def prepare(query) DBI::Prepared::new(self, query) end
Prepares query conditionally.
If query isn’t suitable for preparing, returns it. In otherwise returns nil
, result or number of changed rows.
@since 0.9.2
# 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
Returns preparation placeholder.
# File lib/fluent-query/drivers/dbi.rb, line 56 def quote_placeholder "?" end
Indicates, method is relevant for the driver.
@since 0.9.2 @abstract
# 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