if !Object.const_defined?('Sequel') require File.join(File.dirname(__FILE__), '../sequel') end require 'mysql' module Sequel module MySQL class Database < Sequel::Database set_adapter_scheme :mysql def connect Mysql.real_connect(@opts[:host], @opts[:user], @opts[:password], @opts[:database], @opts[:port]) end def dataset(opts = nil) MySQL::Dataset.new(self, opts) end def execute(sql) @logger.info(sql) if @logger @pool.hold do |conn| conn.query(sql) end end def execute_insert(sql) @logger.info(sql) if @logger @pool.hold do |conn| conn.query(sql) conn.insert_id end end def execute_affected(sql) @logger.info(sql) if @logger @pool.hold do |conn| conn.query(sql) conn.affected_rows end end def transaction(&block) @pool.hold {|conn| conn.transaction(&block)} end end class Dataset < Sequel::Dataset def each(opts = nil, &block) query_each(select_sql(opts), true, &block) self end def count(opts = nil) query_single_value(count_sql(opts)).to_i end def insert(*values) @db.execute_insert(insert_sql(*values)) end def update(values, opts = nil) @db.execute_affected(update_sql(values, opts)) end def delete(opts = nil) @db.execute_affected(delete_sql(opts)) end def query_each(sql, use_model_class = false) @db.synchronize do result = @db.execute(sql) begin if use_model_class && @model_class result.each_hash {|r| yield @model_class.new(r)} else result.each_hash {|r| yield r} end ensure result.free end end self end def single_record(opts = nil) query_single(select_sql(opts), true) end def single_value(opts = nil) query_single_value(select_sql(opts)) end def query_single(sql, use_model_class = false) @db.synchronize do result = @db.execute(sql) begin if use_model_class && @model_class row = @model_class.new(result.fetch_hash) else row = result.fetch_hash end ensure result.free end row end end def query_single_value(sql) @db.synchronize do result = @db.execute(sql) begin return result.fetch_hash.values[0] ensure result.free end end end end end end