Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit 7b64f017429a11470bc379655beea9d9474a94ab 1 parent e4897e3
Jeremy Evans authored July 23, 2008
47  lib/sequel_core/adapters/jdbc.rb
@@ -4,15 +4,21 @@ module Sequel
4 4
   module JDBC
5 5
     module JavaLang; include_package 'java.lang'; end
6 6
     module JavaSQL; include_package 'java.sql'; end
7  
-    CLASS_NAMES = {:postgresql=>'org.postgresql.Driver',
8  
-      :mysql=>'com.mysql.jdbc.Driver',
9  
-      :sqlite=>'org.sqlite.JDBC',
10  
-      :oracle=>'oracle.jdbc.driver.OracleDriver',
11  
-      :sqlserver=>'com.microsoft.sqlserver.jdbc.SQLServerDriver'}
12 7
     DATABASE_SETUP = {:postgresql=>proc do |db|
13 8
         require 'sequel_core/adapters/jdbc/postgresql'
14 9
         db.extend(Sequel::JDBC::Postgres::DatabaseMethods)
15  
-      end
  10
+        begin
  11
+          require 'jdbc/postgres'
  12
+        rescue LoadError
  13
+          # jdbc-postgres gem not used, hopefully the user has the
  14
+          # PostgreSQL-JDBC .jar in their CLASSPATH
  15
+        end
  16
+        org.postgresql.Driver
  17
+      end,
  18
+      :mysql=>proc{com.mysql.jdbc.Driver},
  19
+      :sqlite=>proc{org.sqlite.JDBC},
  20
+      :oracle=>proc{oracle.jdbc.driver.OracleDriver},
  21
+      :sqlserver=>proc{com.microsoft.sqlserver.jdbc.SQLServerDriver}
16 22
     }
17 23
     
18 24
     def self.load_driver(driver)
@@ -25,19 +31,16 @@ class Database < Sequel::Database
25 31
       # The type of database we are connecting to
26 32
       attr_reader :database_type
27 33
       
28  
-      def connect
29  
-        raise(Error, "No connection string specified") unless conn_string = @opts[:uri] || @opts[:url] || @opts[:database]
30  
-        conn_string = "jdbc:#{conn_string}" unless conn_string =~ /^\Ajdbc:/
31  
-        if match = /\Ajdbc:([^:]+)/.match(conn_string)
32  
-          @database_type = match[1].to_sym
33  
-          DATABASE_SETUP[@database_type].call(self)
34  
-          if jdbc_class_name = CLASS_NAMES[@database_type]
35  
-            Sequel::JDBC.load_driver(jdbc_class_name)
36  
-          end
  34
+      def initialize(opts)
  35
+        super(opts)
  36
+        raise(Error, "No connection string specified") unless uri
  37
+        if match = /\Ajdbc:([^:]+)/.match(uri) and prok = DATABASE_SETUP[match[1].to_sym]
  38
+          prok.call(self)
37 39
         end
38  
-        conn = JavaSQL::DriverManager.getConnection(conn_string)
39  
-        setup_connection(conn)
40  
-        conn
  40
+      end
  41
+      
  42
+      def connect
  43
+        setup_connection(JavaSQL::DriverManager.getConnection(uri))
41 44
       end
42 45
       
43 46
       def dataset(opts = nil)
@@ -91,6 +94,12 @@ def execute_dui(sql)
91 94
       end
92 95
       
93 96
       def setup_connection(conn)
  97
+        conn
  98
+      end
  99
+      
  100
+      def uri
  101
+        ur = @opts[:uri] || @opts[:url] || @opts[:database]
  102
+        ur =~ /^\Ajdbc:/ ? ur : "jdbc:#{ur}"
94 103
       end
95 104
       
96 105
       private
@@ -105,7 +114,7 @@ def literal(v)
105 114
         case v
106 115
         when Time
107 116
           literal(v.iso8601)
108  
-        when Date, DateTime
  117
+        when Date, DateTime, Java::JavaSql::Timestamp
109 118
           literal(v.to_s)
110 119
         else
111 120
           super
3  lib/sequel_core/adapters/jdbc/postgresql.rb
@@ -43,6 +43,7 @@ def dataset(opts=nil)
43 43
         
44 44
         def setup_connection(conn)
45 45
           conn.extend(Sequel::JDBC::Postgres::AdapterMethods)
  46
+          conn
46 47
         end
47 48
       end
48 49
       
@@ -53,6 +54,8 @@ def literal(v)
53 54
           case v
54 55
           when SQL::Blob
55 56
             "'#{v.gsub(/[\000-\037\047\134\177-\377]/){|b| "\\#{ b[0].to_s(8).rjust(3, '0') }"}}'"
  57
+          when Java::JavaSql::Timestamp
  58
+            "TIMESTAMP #{literal(v.to_s)}"
56 59
           else
57 60
             super
58 61
           end
2  spec/adapters/postgres_spec.rb
@@ -284,7 +284,7 @@ def POSTGRES_DB.ret_commit
284 284
   specify "should store milliseconds in time fields" do
285 285
     t = Time.now
286 286
     @d << {:value=>1, :time=>t}
287  
-    @d[:value =>'1'][:time].should == t
  287
+    @d.literal(@d[:value =>'1'][:time]).should == @d.literal(t)
288 288
     @d[:value=>'1'][:time].usec.should == t.usec
289 289
   end
290 290
 end

0 notes on commit 7b64f01

Please sign in to comment.
Something went wrong with that request. Please try again.