Sha256: b1a9902cb7546942847432cbccd4a97f9757c3e8d1e0b729fca1c2a6cfff5f75

Contents?: true

Size: 1.72 KB

Versions: 4

Compression:

Stored size: 1.72 KB

Contents

if !Object.const_defined?('Sequel')
  require File.join(File.dirname(__FILE__), '../sequel')
end

require 'dbi'

module Sequel
  module DBI

    class Database < Sequel::Database
      set_adapter_scheme :dbi
    
      def connect
        dbname = @opts[:database] =~ /^DBI:/ ? \
          @opts[:database] : @opts[:database] = 'DBI:' + @opts[:database]
        ::DBI.connect(dbname, @opts[:user], @opts[:password])
      end
    
      def dataset(opts = nil)
        DBI::Dataset.new(self, opts)
      end
    
      def execute(sql)
        @logger.info(sql) if @logger
        @pool.hold do |conn|
          conn.execute(sql)
        end
      end
      
      def do(sql)
        @logger.info(sql) if @logger
        @pool.hold do |conn|
          conn.do(sql)
        end
      end
    end
    
    class Dataset < Sequel::Dataset
      def literal(v)
        case v
        when Time: literal(v.iso8601)
        else
          super
        end
      end

      def fetch_rows(sql, &block)
        @db.synchronize do
          s = @db.execute sql
          begin
            @columns = stmt.column_names.map {|c| c.to_sym}
            s.fetch {|r| yield hash_row(s, r)}
          ensure
            s.finish rescue nil
          end
        end
        self
      end
      
      def hash_row(stmt, row)
        @columns.inject({}) do |m, c|
          m[c] = row.shift
          m
        end
      end
    
      def insert(*values)
        @db.do insert_sql(*values)
      end
    
      def update(values, opts = nil)
        @db.do update_sql(values, opts)
        self
      end
    
      def delete(opts = nil)
        @db.do delete_sql(opts)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sequel-0.1.9.2 lib/sequel/dbi.rb
sequel-0.1.9.1 lib/sequel/dbi.rb
sequel-0.1.8 lib/sequel/dbi.rb
sequel-0.1.9 lib/sequel/dbi.rb