Skip to content

Commit

Permalink
Customize DB2 connections
Browse files Browse the repository at this point in the history
- Get rid of isDB2 check in RubyJdbcConnection in favor of extending
  it for DB2, like many of the other database types.
- Register the connection class in the DB2 adapter.
- Load the connection class in the AdapterJavaService.

Conflicts:

	src/java/arjdbc/jdbc/RubyJdbcConnection.java
  • Loading branch information
nicksieger committed Jun 9, 2011
1 parent 8a81a6d commit 3c081a3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/arjdbc/db2/adapter.rb
Expand Up @@ -5,6 +5,10 @@ def self.column_selector
lambda { |cfg, column| column.extend(::ArJdbc::DB2::Column) } ]
end

def self.jdbc_connection_class
::ActiveRecord::ConnectionAdapters::DB2JdbcConnection
end

module Column
def type_cast(value)
return nil if value.nil? || value =~ /^\s*null\s*$/i
Expand Down
62 changes: 62 additions & 0 deletions src/java/arjdbc/db2/DB2RubyJdbcConnection.java
@@ -0,0 +1,62 @@
/*
**** BEGIN LICENSE BLOCK *****
* Copyright (c) 2006-2010 Nick Sieger <nick@nicksieger.com>
* Copyright (c) 2006-2007 Ola Bini <ola.bini@gmail.com>
* Copyright (c) 2008-2009 Thomas E Enebo <enebo@acm.org>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
***** END LICENSE BLOCK *****/
package arjdbc.db2;

import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.builtin.IRubyObject;

import arjdbc.jdbc.RubyJdbcConnection;

/**
*
* @author mikestone
*/
public class DB2RubyJdbcConnection extends RubyJdbcConnection {
protected DB2RubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
super(runtime, metaClass);
}

@Override
protected boolean databaseSupportsSchemas() {
return true;
}

public static RubyClass createDB2JdbcConnectionClass(Ruby runtime, RubyClass jdbcConnection) {
RubyClass clazz = RubyJdbcConnection.getConnectionAdapters(runtime).defineClassUnder("DB2JdbcConnection",
jdbcConnection, DB2_JDBCCONNECTION_ALLOCATOR);
clazz.defineAnnotatedMethods(DB2RubyJdbcConnection.class);

return clazz;
}

private static ObjectAllocator DB2_JDBCCONNECTION_ALLOCATOR = new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
return new DB2RubyJdbcConnection(runtime, klass);
}
};
}
2 changes: 2 additions & 0 deletions src/java/arjdbc/jdbc/AdapterJavaService.java
Expand Up @@ -28,6 +28,7 @@

import java.io.IOException;

import arjdbc.db2.DB2RubyJdbcConnection;
import arjdbc.derby.DerbyModule;
import arjdbc.h2.H2RubyJdbcConnection;
import arjdbc.informix.InformixRubyJdbcConnection;
Expand Down Expand Up @@ -57,6 +58,7 @@ public boolean basicLoad(final Ruby runtime) throws IOException {
Sqlite3RubyJdbcConnection.createSqlite3JdbcConnectionClass(runtime, jdbcConnection);
H2RubyJdbcConnection.createH2JdbcConnectionClass(runtime, jdbcConnection);
MySQLRubyJdbcConnection.createMySQLJdbcConnectionClass(runtime, jdbcConnection);
DB2RubyJdbcConnection.createDB2JdbcConnectionClass(runtime, jdbcConnection);
RubyModule arJdbc = runtime.getOrCreateModule("ArJdbc");
rubyApi = JavaEmbedUtils.newObjectAdapter();
MySQLModule.load(arJdbc);
Expand Down
5 changes: 2 additions & 3 deletions src/java/arjdbc/jdbc/RubyJdbcConnection.java
@@ -1,6 +1,6 @@
/*
**** BEGIN LICENSE BLOCK *****
* Copyright (c) 2006-2010 Nick Sieger <nick@nicksieger.com>
* Copyright (c) 2006-2011 Nick Sieger <nick@nicksieger.com>
* Copyright (c) 2006-2007 Ola Bini <ola.bini@gmail.com>
* Copyright (c) 2008-2009 Thomas E Enebo <enebo@acm.org>
*
Expand Down Expand Up @@ -132,7 +132,6 @@ public Object call(Connection c) throws SQLException {

DatabaseMetaData metadata = c.getMetaData();
String clzName = metadata.getClass().getName().toLowerCase();
boolean isDB2 = clzName.indexOf("db2") != -1 || clzName.indexOf("as400") != -1;
boolean isPostgres = clzName.indexOf("postgresql") != -1;

String catalog = c.getCatalog();
Expand All @@ -154,7 +153,7 @@ else if ( name_parts.length == 3 ) {
if (schemaName != null) schemaName = caseConvertIdentifierForJdbc(metadata, schemaName);
table_name = caseConvertIdentifierForJdbc(metadata, table_name);

if (schemaName != null && !isDB2 && !databaseSupportsSchemas()) { catalog = schemaName; }
if (schemaName != null && !databaseSupportsSchemas()) { catalog = schemaName; }

String[] tableTypes = new String[]{"TABLE","VIEW","SYNONYM"};
RubyArray matchingTables = (RubyArray) tableLookupBlock(context.getRuntime(),
Expand Down

0 comments on commit 3c081a3

Please sign in to comment.