Permalink
Browse files

Support JRuby 1.1.3, allow use of jdbc-postgres gem, fix some JDBC is…

…sues

This commit has a small restructuring of the JDBC adapter to allow it
to work with JRuby 1.1.3 (hopefully it still works on JRuby 1.1.2).
It makes sure the Database/Dataset is always correctly modified, by
extending it in Database#initialize instead of Database#connect. This
fixes issues such as creating a dataset before connecting to the
database not picking up the PostgreSQL specific settings.

This commit allows you to use the jdbc-postgres gem, but it still
works if you just have the PostgreSQL-JDBC .jar in your CLASSPATH.

This commit also fixes JDBC::Database#inspect, and handles the
literalization of Java::JavaSql::Timestamp values.
  • Loading branch information...
1 parent e4897e3 commit 7b64f017429a11470bc379655beea9d9474a94ab @jeremyevans committed Jul 23, 2008
Showing with 32 additions and 20 deletions.
  1. +28 −19 lib/sequel_core/adapters/jdbc.rb
  2. +3 −0 lib/sequel_core/adapters/jdbc/postgresql.rb
  3. +1 −1 spec/adapters/postgres_spec.rb
@@ -4,15 +4,21 @@ module Sequel
module JDBC
module JavaLang; include_package 'java.lang'; end
module JavaSQL; include_package 'java.sql'; end
- CLASS_NAMES = {:postgresql=>'org.postgresql.Driver',
- :mysql=>'com.mysql.jdbc.Driver',
- :sqlite=>'org.sqlite.JDBC',
- :oracle=>'oracle.jdbc.driver.OracleDriver',
- :sqlserver=>'com.microsoft.sqlserver.jdbc.SQLServerDriver'}
DATABASE_SETUP = {:postgresql=>proc do |db|
require 'sequel_core/adapters/jdbc/postgresql'
db.extend(Sequel::JDBC::Postgres::DatabaseMethods)
- end
+ begin
+ require 'jdbc/postgres'
+ rescue LoadError
+ # jdbc-postgres gem not used, hopefully the user has the
+ # PostgreSQL-JDBC .jar in their CLASSPATH
+ end
+ org.postgresql.Driver
+ end,
+ :mysql=>proc{com.mysql.jdbc.Driver},
+ :sqlite=>proc{org.sqlite.JDBC},
+ :oracle=>proc{oracle.jdbc.driver.OracleDriver},
+ :sqlserver=>proc{com.microsoft.sqlserver.jdbc.SQLServerDriver}
}
def self.load_driver(driver)
@@ -25,19 +31,16 @@ class Database < Sequel::Database
# The type of database we are connecting to
attr_reader :database_type
- def connect
- raise(Error, "No connection string specified") unless conn_string = @opts[:uri] || @opts[:url] || @opts[:database]
- conn_string = "jdbc:#{conn_string}" unless conn_string =~ /^\Ajdbc:/
- if match = /\Ajdbc:([^:]+)/.match(conn_string)
- @database_type = match[1].to_sym
- DATABASE_SETUP[@database_type].call(self)
- if jdbc_class_name = CLASS_NAMES[@database_type]
- Sequel::JDBC.load_driver(jdbc_class_name)
- end
+ def initialize(opts)
+ super(opts)
+ raise(Error, "No connection string specified") unless uri
+ if match = /\Ajdbc:([^:]+)/.match(uri) and prok = DATABASE_SETUP[match[1].to_sym]
+ prok.call(self)
end
- conn = JavaSQL::DriverManager.getConnection(conn_string)
- setup_connection(conn)
- conn
+ end
+
+ def connect
+ setup_connection(JavaSQL::DriverManager.getConnection(uri))
end
def dataset(opts = nil)
@@ -91,6 +94,12 @@ def execute_dui(sql)
end
def setup_connection(conn)
+ conn
+ end
+
+ def uri
+ ur = @opts[:uri] || @opts[:url] || @opts[:database]
+ ur =~ /^\Ajdbc:/ ? ur : "jdbc:#{ur}"
end
private
@@ -105,7 +114,7 @@ def literal(v)
case v
when Time
literal(v.iso8601)
- when Date, DateTime
+ when Date, DateTime, Java::JavaSql::Timestamp
literal(v.to_s)
else
super
@@ -43,6 +43,7 @@ def dataset(opts=nil)
def setup_connection(conn)
conn.extend(Sequel::JDBC::Postgres::AdapterMethods)
+ conn
end
end
@@ -53,6 +54,8 @@ def literal(v)
case v
when SQL::Blob
"'#{v.gsub(/[\000-\037\047\134\177-\377]/){|b| "\\#{ b[0].to_s(8).rjust(3, '0') }"}}'"
+ when Java::JavaSql::Timestamp
+ "TIMESTAMP #{literal(v.to_s)}"
else
super
end
@@ -284,7 +284,7 @@ def POSTGRES_DB.ret_commit
specify "should store milliseconds in time fields" do
t = Time.now
@d << {:value=>1, :time=>t}
- @d[:value =>'1'][:time].should == t
+ @d.literal(@d[:value =>'1'][:time]).should == @d.literal(t)
@d[:value=>'1'][:time].usec.should == t.usec
end
end

0 comments on commit 7b64f01

Please sign in to comment.