include/drivers/mysql/mysql.rb in baza-0.0.12 vs include/drivers/mysql/mysql.rb in baza-0.0.13

- old
+ new

@@ -1,28 +1,32 @@ class Baza::Driver::Mysql - attr_reader :knjdb, :conn, :conns, :sep_table, :sep_col, :sep_val + attr_reader :baza, :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] + :conn => args[:object], + :query_args => { + :as => :hash, + :symbolize_keys => true + } } } end return nil end - def initialize(knjdb_ob) - @knjdb = knjdb_ob - @opts = @knjdb.opts + def initialize(baza_db_obj) + @baza_db = baza_db_obj + @opts = @baza_db.opts @sep_table = "`" @sep_col = "`" @sep_val = "'" require "monitor" @@ -32,18 +36,18 @@ @encoding = @opts[:encoding] else @encoding = "utf8" end - if @knjdb.opts.key?(:port) - @port = @knjdb.opts[:port].to_i + if @baza_db.opts.key?(:port) + @port = @baza_db.opts[:port].to_i else @port = 3306 end @java_rs_data = {} - @subtype = @knjdb.opts[:subtype] + @subtype = @baza_db.opts[:subtype] @subtype = :mysql if @subtype.to_s.empty? self.reconnect end #This method handels the closing of statements and results for the Java MySQL-mode. @@ -64,48 +68,49 @@ #Respawns the connection to the MySQL-database. def reconnect @mutex.synchronize do case @subtype when :mysql - @conn = Mysql.real_connect(@knjdb.opts[:host], @knjdb.opts[:user], @knjdb.opts[:pass], @knjdb.opts[:db], @port) + @conn = Mysql.real_connect(@baza_db.opts[:host], @baza_db.opts[:user], @baza_db.opts[:pass], @baza_db.opts[:db], @port) when :mysql2 require "rubygems" require "mysql2" args = { - :host => @knjdb.opts[:host], - :username => @knjdb.opts[:user], - :password => @knjdb.opts[:pass], - :database => @knjdb.opts[:db], + :host => @baza_db.opts[:host], + :username => @baza_db.opts[:user], + :password => @baza_db.opts[:pass], + :database => @baza_db.opts[:db], :port => @port, :symbolize_keys => true, :cache_rows => false } #Symbolize keys should also be given here, else table-data wont be symbolized for some reason - knj. @query_args = {:symbolize_keys => true} - @query_args.merge!(@knjdb.opts[:query_args]) if @knjdb.opts[:query_args] + @query_args.merge!(@baza_db.opts[:query_args]) if @baza_db.opts[:query_args] pos_args = [:as, :async, :cast_booleans, :database_timezone, :application_timezone, :cache_rows, :connect_flags, :cast] pos_args.each do |key| - args[key] = @knjdb.opts[key] if @knjdb.opts.key?(key) + args[key] = @baza_db.opts[key] if @baza_db.opts.key?(key) end args[:as] = :array if @opts[:result] == "array" tries = 0 begin tries += 1 - if @knjdb.opts[:conn] - @conn = @knjdb.opts[:conn] + if @baza_db.opts[:conn] + @conn = @baza_db.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 + tries += 1 retry end end raise e @@ -116,11 +121,11 @@ 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") + @conn = java.sql::DriverManager.getConnection("jdbc:mysql://#{@baza_db.opts[:host]}:#{@port}/#{@baza_db.opts[:db]}?user=#{@baza_db.opts[:user]}&password=#{@baza_db.opts[:pass]}&populateInsertRowWithDefaultValues=true&zeroDateTimeBehavior=round&characterEncoding=#{@encoding}&holdResultsOpenOverStatementClose=true") self.query("SET SQL_MODE = ''") else raise "Unknown subtype: #{@subtype} (#{@subtype.class.name})" end @@ -156,11 +161,11 @@ else id = nil begin res = stmt.execute_query(str) - ret = Baza::Driver::Mysql::ResultJava.new(@knjdb, @opts, res) + ret = Baza::Driver::Mysql::ResultJava.new(@baza_db, @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) @@ -220,11 +225,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 = Baza::Driver::Mysql::ResultJava.new(@knjdb, @opts, res) + ret = Baza::Driver::Mysql::ResultJava.new(@baza_db, @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")) @@ -269,11 +274,11 @@ end #Escapes a string to be safe to use as a column in a query. def esc_col(string) string = string.to_s - raise "Invalid column-string: #{string}" if string.index(@sep_col) != nil + raise "Invalid column-string: #{string}" if string.include?(@sep_col) return string end alias :esc_table :esc_col alias :esc :escape @@ -304,11 +309,11 @@ end #Destroyes the connection. def destroy @conn = nil - @knjdb = nil + @baza_db = nil @mutex = nil @subtype = nil @encoding = nil @query_args = nil @port = nil @@ -350,21 +355,21 @@ first_key = false else sql << "," end - sql << @knjdb.sqlval(val) + sql << @baza_db.sqlval(val) end else hash.each do |key, val| if first_key first_key = false else sql << "," end - sql << @knjdb.sqlval(val) + sql << @baza_db.sqlval(val) end end end sql << ")" @@ -391,17 +396,17 @@ end end #Starts a transaction, yields the database and commits at the end. def transaction - @knjdb.q("START TRANSACTION") + @baza_db.q("START TRANSACTION") begin - yield(@knjdb) - @knjdb.q("COMMIT") + yield(@baza_db) + @baza_db.q("COMMIT") rescue - @knjdb.q("ROLLBACK") + @baza_db.q("ROLLBACK") raise end end end @@ -550,10 +555,10 @@ #This class controls the result for the Java-MySQL-driver. class Baza::Driver::Mysql::ResultJava #Constructor. This should not be called manually. def initialize(knjdb, opts, result) - @knjdb = knjdb + @baza_db = knjdb @result = result if !opts.key?(:result) or opts[:result] == "hash" @as_hash = true elsif opts[:result] == "array" \ No newline at end of file