Permalink
Browse files

Add support for connecting to MSSQL via JTDS in the JDBC adapter

While here, fix a minor bug for converting Java blobs to
Sequel::SQL::Blob, which seems to only occur on JTDS.  Also,
update the Connecting to a Database guide to give examples
of jtds and sqlserver connection strings, and move some
documentation from the JDBC module to the guide.
  • Loading branch information...
jeremyevans committed Apr 30, 2010
1 parent 43d91cf commit fb91f217ed8e0a5b95f6cfe9f30ecf6f39a0867d
Showing with 24 additions and 23 deletions.
  1. +2 −0 CHANGELOG
  2. +15 −4 doc/opening_databases.rdoc
  3. +7 −19 lib/sequel/adapters/jdbc.rb
View
@@ -1,5 +1,7 @@
=== HEAD
+* Add support for connecting to MSSQL via JTDS in the JDBC adapter (jeremyevans)
+
* Support returning the number of rows updated/deleted on MSSQL when using the ADO adapter with an explicit :provider (jeremyevans)
* Support transactions in the ADO adapter if not using the default :provider (jeremyevans)
View
@@ -200,22 +200,33 @@ Requires: java
Houses Sequel's JDBC support when running on JRuby.
Support for individual database types is done using sub adapters.
There are currently subadapters for PostgreSQL, MySQL, SQLite, H2,
-Oracle, MSSQL, and AS400. All except Oracle, MSSQL, and AS400 can load the
-JDBC gem, for those you need to have the .jar in your CLASSPATH
-or load the Java class manually.
+Oracle, MSSQL, JTDS, and AS400. All except Oracle, MSSQL, and AS400 can load the
+jdbc-* gem, for those you need to have the .jar in your CLASSPATH
+or load the Java class manually before calling Sequel.connect.
You just use the JDBC connection string directly, which can be specified
via the string given to Sequel.connect or via the :uri, :url, or :database options.
Sequel does no preprocessing of the string, it passes it directly to JDBC.
So if you have problems getting a connection string to work, look up the JDBC
documentation.
-Example connections strings:
+Note that when using a JDBC adapter, the best way to use Sequel
+is via Sequel.connect, NOT Sequel.jdbc. Use the JDBC connection
+string when connecting, which will be in a different format than
+the native connection string. The connection string should start
+with 'jdbc:'. For PostgreSQL, use 'jdbc:postgresql:', and for
+SQLite you do not need 2 preceding slashes for the database name
+(use no preceding slashes for a relative path, and one preceding
+slash for an absolute path).
+
+Example connection strings:
jdbc:sqlite::memory:
jdbc:postgresql://localhost/database?user=username
jdbc:mysql://localhost/test?user=root&password=root
jdbc:h2:mem:
+ jdbc:sqlserver://localhost;database=sequel_test;integratedSecurity=true
+ jdbc:jtds:sqlserver://localhost/sequel_test;user=sequel_test;password=sequel_test
You can also use JNDI connection strings:
@@ -3,24 +3,6 @@
module Sequel
# Houses Sequel's JDBC support when running on JRuby.
- # Support for individual database types is done using sub adapters.
- # PostgreSQL, MySQL, SQLite, Oracle, and MSSQL all have relatively good support,
- # close the the level supported by the native adapter.
- # PostgreSQL, MySQL, SQLite can load necessary support using
- # the jdbc-* gem, if it is installed, though they will work if you
- # have the correct .jar in your CLASSPATH. Oracle and MSSQL should
- # load the necessary support if you have the .jar in your CLASSPATH.
- # For all other databases, the Java class should be loaded manually
- # before calling Sequel.connect.
- #
- # Note that when using a JDBC adapter, the best way to use Sequel
- # is via Sequel.connect, NOT Sequel.jdbc. Use the JDBC connection
- # string when connecting, which will be in a different format than
- # the native connection string. The connection string should start
- # with 'jdbc:'. For PostgreSQL, use 'jdbc:postgresql:', and for
- # SQLite you do not need 2 preceding slashes for the database name
- # (use no preceding slashes for a relative path, and one preceding
- # slash for an absolute path).
module JDBC
# Make it accesing the java.lang hierarchy more ruby friendly.
module JavaLang
@@ -75,6 +57,12 @@ module JavaxNaming
db.extend(Sequel::JDBC::MSSQL::DatabaseMethods)
com.microsoft.sqlserver.jdbc.SQLServerDriver
end,
+ :jtds=>proc do |db|
+ Sequel.ts_require 'adapters/jdbc/mssql'
+ db.extend(Sequel::JDBC::MSSQL::DatabaseMethods)
+ JDBC.load_gem('jtds')
+ Java::net.sourceforge.jtds.jdbc.Driver
+ end,
:h2=>proc do |db|
Sequel.ts_require 'adapters/jdbc/h2'
db.extend(Sequel::JDBC::H2::DatabaseMethods)
@@ -572,7 +560,7 @@ def convert_type(v)
when Java::byte[]
Sequel::SQL::Blob.new(String.from_java_bytes(v))
when Java::JavaSQL::Blob
- convert_type(v.getBytes(0, v.length))
+ convert_type(v.getBytes(1, v.length))
else
v
end

0 comments on commit fb91f21

Please sign in to comment.