include/drivers/mysql/mysql.rb in baza-0.0.0 vs include/drivers/mysql/mysql.rb in baza-0.0.1
- old
+ new
@@ -1,9 +1,25 @@
class Baza::Driver::Mysql
attr_reader :knjdb, :conn, :conns, :sep_table, :sep_col, :sep_val
attr_accessor :tables, :cols, :indexes
+ #Helper to enable automatic registering of database using Baza::Db.from_object
+ def self.from_object(args)
+ if args[:object].class.name == "Mysql2::Client"
+ return {
+ :type => :success,
+ :args => {
+ :type => :mysql,
+ :subtype => :mysql2,
+ :conn => args[:object]
+ }
+ }
+ end
+
+ return nil
+ end
+
def initialize(knjdb_ob)
@knjdb = knjdb_ob
@opts = @knjdb.opts
@sep_table = "`"
@sep_col = "`"
@@ -24,11 +40,11 @@
@port = 3306
end
@java_rs_data = {}
@subtype = @knjdb.opts[:subtype]
- @subtype = "mysql" if @subtype.to_s.length <= 0
+ @subtype = :mysql if @subtype.to_s.empty?
self.reconnect
end
#This method handels the closing of statements and results for the Java MySQL-mode.
def java_mysql_resultset_killer(id)
@@ -47,13 +63,13 @@
#Respawns the connection to the MySQL-database.
def reconnect
@mutex.synchronize do
case @subtype
- when "mysql"
+ when :mysql
@conn = Mysql.real_connect(@knjdb.opts[:host], @knjdb.opts[:user], @knjdb.opts[:pass], @knjdb.opts[:db], @port)
- when "mysql2"
+ when :mysql2
require "rubygems"
require "mysql2"
args = {
:host => @knjdb.opts[:host],
@@ -77,33 +93,37 @@
args[:as] = :array if @opts[:result] == "array"
tries = 0
begin
tries += 1
- @conn = Mysql2::Client.new(args)
+ if @knjdb.opts[:conn]
+ @conn = @knjdb.opts[:conn]
+ else
+ @conn = Mysql2::Client.new(args)
+ end
rescue => e
if tries <= 3
if e.message == "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)"
sleep 1
retry
end
end
raise e
end
- when "java"
+ when :java
if !@jdbc_loaded
require "java"
require "/usr/share/java/mysql-connector-java.jar" if File.exists?("/usr/share/java/mysql-connector-java.jar")
import "com.mysql.jdbc.Driver"
@jdbc_loaded = true
end
@conn = java.sql::DriverManager.getConnection("jdbc:mysql://#{@knjdb.opts[:host]}:#{@port}/#{@knjdb.opts[:db]}?user=#{@knjdb.opts[:user]}&password=#{@knjdb.opts[:pass]}&populateInsertRowWithDefaultValues=true&zeroDateTimeBehavior=round&characterEncoding=#{@encoding}&holdResultsOpenOverStatementClose=true")
self.query("SET SQL_MODE = ''")
else
- raise "Unknown subtype: #{@subtype}"
+ raise "Unknown subtype: #{@subtype} (#{@subtype.class.name})"
end
self.query("SET NAMES '#{self.esc(@encoding)}'") if @encoding
end
end
@@ -116,15 +136,15 @@
begin
tries += 1
@mutex.synchronize do
case @subtype
- when "mysql"
- return Baza::Driver::Mysql_result.new(self, @conn.query(str))
- when "mysql2"
- return Baza::Driver::Mysql2_result.new(@conn.query(str, @query_args))
- when "java"
+ when :mysql
+ return Baza::Driver::Mysql::Result.new(self, @conn.query(str))
+ when :mysql2
+ return Baza::Driver::Mysql::ResultMySQL2.new(@conn.query(str, @query_args))
+ when :java
stmt = conn.create_statement
if str.match(/^\s*(delete|update|create|drop|insert\s+into|alter)\s+/i)
begin
stmt.execute(str)
@@ -136,11 +156,11 @@
else
id = nil
begin
res = stmt.execute_query(str)
- ret = KnjDB_java_mysql_result.new(@knjdb, @opts, res)
+ ret = Baza::Driver::Mysql::ResultJava.new(@knjdb, @opts, res)
id = ret.__id__
#If ID is being reused we have to free the result.
self.java_mysql_resultset_killer(id) if @java_rs_data.key?(id)
@@ -178,16 +198,16 @@
#Executes an unbuffered query and returns the result that can be used to access the data.
def query_ubuf(str)
@mutex.synchronize do
case @subtype
- when "mysql"
+ when :mysql
@conn.query_with_result = false
- return Baza::Driver::Mysql_unbuffered_result.new(@conn, @opts, @conn.query(str))
- when "mysql2"
- return Baza::Driver::Mysql2_result.new(@conn.query(str, @query_args.merge(:stream => true)))
- when "java"
+ return Baza::Driver::Mysql::ResultUnbuffered.new(@conn, @opts, @conn.query(str))
+ when :mysql2
+ return Baza::Driver::Mysql::ResultMySQL2.new(@conn.query(str, @query_args.merge(:stream => true)))
+ when :java
if str.match(/^\s*(delete|update|create|drop|insert\s+into)\s+/i)
stmt = @conn.createStatement
begin
stmt.execute(str)
@@ -200,11 +220,11 @@
stmt = @conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY)
stmt.setFetchSize(java.lang.Integer::MIN_VALUE)
begin
res = stmt.executeQuery(str)
- ret = KnjDB_java_mysql_result.new(@knjdb, @opts, res)
+ ret = Baza::Driver::Mysql::ResultJava.new(@knjdb, @opts, res)
#Save reference to result and statement, so we can close them when they are garbage collected.
@java_rs_data[ret.__id__] = {:res => res, :stmt => stmt}
ObjectSpace.define_finalizer(ret, self.method("java_mysql_resultset_killer"))
@@ -222,15 +242,15 @@
end
#Escapes a string to be safe to use in a query.
def escape_alternative(string)
case @subtype
- when "mysql"
+ when :mysql
return @conn.escape_string(string.to_s)
- when "mysql2"
+ when :mysql2
return @conn.escape(string.to_s)
- when "java"
+ when :java
return self.escape(string)
else
raise "Unknown subtype: '#{@subtype}'."
end
end
@@ -259,19 +279,19 @@
alias :esc :escape
#Returns the last inserted ID for the connection.
def lastID
case @subtype
- when "mysql"
+ when :mysql
@mutex.synchronize do
return @conn.insert_id.to_i
end
- when "mysql2"
+ when :mysql2
@mutex.synchronize do
return @conn.last_id.to_i
end
- when "java"
+ when :java
data = self.query("SELECT LAST_INSERT_ID() AS id").fetch
return data[:id].to_i if data.key?(:id)
raise "Could not figure out last inserted ID."
end
end
@@ -382,41 +402,27 @@
end
end
end
#This class controls the results for the normal MySQL-driver.
-class Baza::Driver::Mysql_result
+class Baza::Driver::Mysql::Result
#Constructor. This should not be called manually.
def initialize(driver, result)
@driver = driver
@result = result
@mutex = Mutex.new
if @result
@keys = []
- keys = @result.fetch_fields
- keys.each do |key|
+ @result.fetch_fields.each do |key|
@keys << key.name.to_sym
end
end
end
- #Returns a single result.
- def fetch
- return self.fetch_hash_symbols if @driver.knjdb.opts[:return_keys] == "symbols"
- return self.fetch_hash_strings
- end
-
- #Returns a single result as a hash with strings as keys.
- def fetch_hash_strings
- @mutex.synchronize do
- return @result.fetch_hash
- end
- end
-
#Returns a single result as a hash with symbols as keys.
- def fetch_hash_symbols
+ def fetch
fetched = nil
@mutex.synchronize do
fetched = @result.fetch_row
end
@@ -432,18 +438,18 @@
return ret
end
#Loops over every result yielding it.
def each
- while data = self.fetch_hash_symbols
+ while data = self.fetch
yield(data)
end
end
end
#This class controls the unbuffered result for the normal MySQL-driver.
-class Baza::Driver::Mysql_unbuffered_result
+class Baza::Driver::Mysql::ResultUnbuffered
#Constructor. This should not be called manually.
def initialize(conn, opts, result)
@conn = conn
@result = result
@@ -512,11 +518,11 @@
end
end
end
#This class controls the result for the MySQL2 driver.
-class Baza::Driver::Mysql2_result
+class Baza::Driver::Mysql::ResultMySQL2
#Constructor. This should not be called manually.
def initialize(result)
@result = result
end
@@ -532,18 +538,17 @@
end
#Loops over every single result yielding it.
def each
@result.each do |res|
- #This sometimes happens when streaming results...
- next if !res
+ next if !res #This sometimes happens when streaming results...
yield(res)
end
end
end
#This class controls the result for the Java-MySQL-driver.
-class KnjDB_java_mysql_result
+class Baza::Driver::Mysql::ResultJava
#Constructor. This should not be called manually.
def initialize(knjdb, opts, result)
@knjdb = knjdb
@result = result
\ No newline at end of file