Skip to content

Commit

Permalink
JRUBY-1638, JRUBY-2404: schema.table handling, Oracle NUMBER fixes
Browse files Browse the repository at this point in the history
Thanks Darcy Schultz & Jesse Hu


git-svn-id: svn+ssh://rubyforge.org/var/svn/jruby-extras/trunk/activerecord-jdbc@990 8ba958d5-0c1a-0410-94a6-a65dfc1b28a6
  • Loading branch information
nicksieger committed May 13, 2008
1 parent fe5d4b5 commit bdb46e4
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions src/java/jdbc_adapter/JdbcAdapterInternalService.java
@@ -1,18 +1,18 @@
/***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2006-2007 Nick Sieger <nick@nicksieger.com>
* Copyright (c) 2006-2008 Nick Sieger <nick@nicksieger.com>
* Copyright (c) 2006-2007 Ola Bini <ola.bini@gmail.com>
*
*
* 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
Expand Down Expand Up @@ -259,8 +259,8 @@ private static IRubyObject withConnectionAndRetry(IRubyObject recv, SQLBlock blo
throw wrap(recv, toWrap);
}

private static SQLBlock tableLookupBlock(final Ruby runtime,
final String catalog, final String schemapat,
private static SQLBlock tableLookupBlock(final Ruby runtime,
final String catalog, final String schemapat,
final String tablepat, final String[] types) {
return new SQLBlock() {
public IRubyObject call(Connection c) throws SQLException {
Expand All @@ -271,6 +271,16 @@ public IRubyObject call(Connection c) throws SQLException {
boolean isOracle = clzName.indexOf("oracle") != -1 || clzName.indexOf("oci") != -1;

String realschema = schemapat;
String realtablepat = tablepat;

if(metadata.storesUpperCaseIdentifiers()) {
if (realschema != null) realschema = realschema.toUpperCase();
if (realtablepat != null) realtablepat = realtablepat.toUpperCase();
} else if(metadata.storesLowerCaseIdentifiers()) {
if (null != realschema) realschema = realschema.toLowerCase();
if (realtablepat != null) realtablepat = realtablepat.toLowerCase();
}

if (realschema == null && isOracle) {
ResultSet schemas = metadata.getSchemas();
String username = metadata.getUserName();
Expand All @@ -282,7 +292,7 @@ public IRubyObject call(Connection c) throws SQLException {
}
schemas.close();
}
rs = metadata.getTables(catalog, realschema, tablepat, types);
rs = metadata.getTables(catalog, realschema, realtablepat, types);
List arr = new ArrayList();
while (rs.next()) {
String name = rs.getString(3).toLowerCase();
Expand Down Expand Up @@ -351,7 +361,7 @@ private static String[] getTypes(IRubyObject[] args) {
@JRubyMethod(name = "native_database_types")
public static IRubyObject native_database_types(IRubyObject recv) {
return rubyApi.getInstanceVariable(recv, "@tps");
}
}

@JRubyMethod(name = "set_native_database_types")
public static IRubyObject set_native_database_types(IRubyObject recv) throws SQLException, IOException {
Expand Down Expand Up @@ -410,9 +420,10 @@ public IRubyObject call(Connection c) throws SQLException {
String table_name = rubyApi.convertToRubyString(args[0]).getUnicodeValue();
String schemaName = null;

if(table_name.indexOf(".") != -1) {
schemaName = table_name.substring(table_name.indexOf(".")+1);
table_name = table_name.substring(0, table_name.indexOf(".")+1);
int index = table_name.indexOf(".");
if(index != -1) {
schemaName = table_name.substring(0, index);
table_name = table_name.substring(index + 1);
}

DatabaseMetaData metadata = c.getMetaData();
Expand All @@ -425,8 +436,10 @@ public IRubyObject call(Connection c) throws SQLException {
}

if(metadata.storesUpperCaseIdentifiers()) {
if (null != schemaName) schemaName = schemaName.toUpperCase();
table_name = table_name.toUpperCase();
} else if(metadata.storesLowerCaseIdentifiers()) {
if (null != schemaName) schemaName = schemaName.toLowerCase();
table_name = table_name.toLowerCase();
}

Expand All @@ -442,7 +455,7 @@ public IRubyObject call(Connection c) throws SQLException {
schemas.close();
}

RubyArray matchingTables = (RubyArray) tableLookupBlock(recv.getRuntime(),
RubyArray matchingTables = (RubyArray) tableLookupBlock(recv.getRuntime(),
c.getCatalog(), schemaName, table_name, new String[]{"TABLE","VIEW"}).call(c);
if (matchingTables.isEmpty()) {
throw new SQLException("Table " + table_name + " does not exist");
Expand Down Expand Up @@ -486,6 +499,9 @@ private static IRubyObject unmarshal_columns(IRubyObject recv, DatabaseMetaData
if(scal != null) {
scale = Integer.parseInt(scal);
}
else if(isOracle && rs.getInt(5) == java.sql.Types.DECIMAL) { // NUMBER type in Oracle
prec = null;
}
}
String type = rs.getString(6);
if(prec != null && precision > 0) {
Expand Down Expand Up @@ -611,7 +627,7 @@ public static IRubyObject execute_query(final IRubyObject recv, IRubyObject[] ar
} else {
maxrows = 0;
}

return withConnectionAndRetry(recv, new SQLBlock() {
public IRubyObject call(Connection c) throws SQLException {
Statement stmt = null;
Expand Down Expand Up @@ -682,7 +698,7 @@ col_names[i], jdbc_to_ruby(runtime, i+1, col_types[i], col_scale[i], rs)
rs.close();
} catch(Exception e) {}
}

return runtime.newArray(results);
}

Expand Down Expand Up @@ -771,7 +787,7 @@ col_names[i], jdbc_to_ruby(runtime, i+1, col_types[i], col_scale[i], rs)
rs.close();
} catch(Exception e) {}
}

return runtime.newArray(results);
}

Expand Down Expand Up @@ -856,7 +872,7 @@ private static String convertToStringOrNull(IRubyObject obj) {

private static int getTypeValueFor(Ruby runtime, IRubyObject type) throws SQLException {
if(!(type instanceof RubySymbol)) {
type = rubyApi.callMethod(type, "type");
type = rubyApi.callMethod(type, "class");
}
if(type == runtime.newSymbol("string")) {
return Types.VARCHAR;
Expand Down Expand Up @@ -884,7 +900,7 @@ private static int getTypeValueFor(Ruby runtime, IRubyObject type) throws SQLExc
return -1;
}
}

private final static DateFormat FORMAT = new SimpleDateFormat("%y-%M-%d %H:%m:%s");

private static void setValue(PreparedStatement ps, int index, Ruby runtime, ThreadContext context,
Expand Down Expand Up @@ -1012,7 +1028,7 @@ public IRubyObject call(Connection c) throws SQLException {
ps = c.prepareStatement(sql);
if (args[0].isTrue()) { // binary
ByteList outp = rubyApi.convertToRubyString(args[5]).getByteList();
ps.setBinaryStream(1, new ByteArrayInputStream(outp.bytes,
ps.setBinaryStream(1, new ByteArrayInputStream(outp.bytes,
outp.begin, outp.realSize), outp.realSize);
} else { // clob
String ss = rubyApi.convertToRubyString(args[5]).getUnicodeValue();
Expand Down Expand Up @@ -1048,7 +1064,7 @@ private static ResultSet intoResultSet(IRubyObject inp) {
jo = (JavaObject) rubyApi.getInstanceVariable(inp, "@java_object");
}
return (ResultSet) jo.getValue();
}
}

private static boolean isConnectionBroken(IRubyObject recv, Connection c) {
try {
Expand Down

1 comment on commit bdb46e4

@drpentode
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied this fix to my activerecord-jdbc-adapter gem 0.8 and still got the same error as before.

C:/jruby_installs/jruby-1.1.1/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `columns_with_query_cache’: Table OPS$DFLOYD. does not exist (ActiveRecord::ActiveRecordError)

The schema is OPS$DFLOYD, the table name is LOG_MESSAGE_ENTRY.

Am I missing something?

Please sign in to comment.