Class: JRubySQL::RDBMS

Inherits:
Object
  • Object
show all
Includes:
Messages
Defined in:
lib/jrubysql/rdbms.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Messages

#m

Constructor Details

#initialize(options) ⇒ RDBMS

Returns a new instance of RDBMS.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/jrubysql/rdbms.rb', line 26

def initialize options
  @conn = 
    if options[:type]
      case options[:type]
      when :mysql
        JDBCHelper::MySQL.connect(
          options[:host], options[:user], options[:password], options[:database])
      when :oracle
        host, svc = options[:host].split('/')
        if svc.nil?
          # FIXME
          raise ArgumentError.new m(:oracle_service_name_required)
        end
        JDBCHelper::Oracle.connect(
          host, options[:user], options[:password], svc)
      when :postgres
        JDBCHelper::Postgres.connect(
          options[:host], options[:user], options[:password], options[:database])
      when :sqlserver
        JDBCHelper::SqlServer.connect(
          options[:host], options[:user], options[:password], options[:database])
      when :cassandra
        JDBCHelper::Cassandra.connect(
          options[:host], options[:database])
      when :sqlite
        JDBCHelper::Connection.new(
          {
            :driver   => 'org.sqlite.JDBC',
            :url      => "jdbc:sqlite:#{options[:host]}",
            :user     => options[:user],
            :password => options[:password]
          }.reject { |k, v| v.nil? }
        )
      end
    elsif options[:driver]
      JDBCHelper::Connection.new(
        {
          :driver   => options[:driver],
          :url      => options[:url],
          :user     => options[:user],
          :password => options[:password]
        }.reject { |k, v| v.nil? }
      )
    else
      raise ArgumentError.new m(:invalid_connection)
    end
end

Class Method Details

.get_type(driver_or_type) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jrubysql/rdbms.rb', line 7

def self.get_type driver_or_type
  case driver_or_type.to_s.downcase
  when /oracle/
    :oracle
  when /mysql/
    :mysql
  when /postgres/
    :postgres
  when /sqlserver/
    :sqlserver
  when /sqlite/
    :sqlite
  when /cassandra/
    :cassandra
  else
    :unknown
  end
end

Instance Method Details

#autocommitObject



74
75
76
# File 'lib/jrubysql/rdbms.rb', line 74

def autocommit
  @conn.java_obj.get_auto_commit
end

#autocommit=(ac) ⇒ Object



78
79
80
# File 'lib/jrubysql/rdbms.rb', line 78

def autocommit= ac
  @conn.java_obj.set_auto_commit ac
end

#closeObject



82
83
84
# File 'lib/jrubysql/rdbms.rb', line 82

def close
  @conn.close
end

#execute(sql) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/jrubysql/rdbms.rb', line 86

def execute sql
  st = Time.now
  result = @conn.execute sql
  elapsed = Time.now - st

  {
    :set? => result.respond_to?(:each),
    :result => result,
    :elapsed => elapsed
  }
end