SPR-8270 Adding improved support for detecting current schema name for Oracle

This commit is contained in:
Thomas Risberg 2011-10-07 03:01:43 +00:00
parent 1317b821b9
commit 92d6e5a14c
2 changed files with 62 additions and 14 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -66,7 +66,7 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
private boolean generatedKeysColumnNameArraySupported = true;
/** database products we know not supporting the use of a String[] for generated keys */
private List productsNotSupportingGeneratedKeysColumnNameArray =
private List<String> productsNotSupportingGeneratedKeysColumnNameArray =
Arrays.asList("Apache Derby", "HSQL Database Engine");
/** Collection of TableParameterMetaData objects */
@ -267,11 +267,17 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
public String metaDataSchemaNameToUse(String schemaName) {
if (schemaName == null) {
return schemaNameToUse(userName);
return schemaNameToUse(getDefaultSchema());
}
return schemaNameToUse(schemaName);
}
/**
* Provide access to default schema for subclasses.
*/
protected String getDefaultSchema() {
return userName;
}
/**
* Provide access to version info for subclasses.
@ -327,16 +333,19 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
else {
TableMetaData tmd;
if (schemaName == null) {
tmd = tableMeta.get(userName.toUpperCase());
tmd = tableMeta.get(getDefaultSchema());
if (tmd == null) {
tmd = tableMeta.get(userName.toUpperCase());
}
if (tmd == null) {
tmd = tableMeta.get("PUBLIC");
if (tmd == null) {
tmd = tableMeta.get("DBO");
}
if (tmd == null) {
throw new DataAccessResourceFailureException("Unable to locate table meta data for '" +
tableName + "' in the default schema");
}
}
if (tmd == null) {
tmd = tableMeta.get("DBO");
}
if (tmd == null) {
throw new DataAccessResourceFailureException("Unable to locate table meta data for '" +
tableName + "' in the default schema");
}
}
else {
@ -461,6 +470,7 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
this.type = type;
}
@SuppressWarnings("unused")
public String getType() {
return this.type;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,9 +17,11 @@
package org.springframework.jdbc.core.metadata;
import java.lang.reflect.Method;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.sql.Types;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor;
@ -27,7 +29,11 @@ import org.springframework.util.ReflectionUtils;
/**
* Oracle-specific implementation of the {@link org.springframework.jdbc.core.metadata.TableMetaDataProvider}.
* Supports a feature for including synonyms in the metadata lookup.
* Supports a feature for including synonyms in the metadata lookup. Also supports lookup of current schema using
* the sys_context.
*
* <p>Thanks to Mike Youngstrom and Bruce Campbell for submitting the original suggestion for the Oracle
* current schema lookup implementation.
*
* @author Thomas Risberg
* @author Juergen Hoeller
@ -37,6 +43,8 @@ public class OracleTableMetaDataProvider extends GenericTableMetaDataProvider {
private final boolean includeSynonyms;
private String defaultSchema;
public OracleTableMetaDataProvider(DatabaseMetaData databaseMetaData) throws SQLException {
this(databaseMetaData, false);
@ -45,8 +53,16 @@ public class OracleTableMetaDataProvider extends GenericTableMetaDataProvider {
public OracleTableMetaDataProvider(DatabaseMetaData databaseMetaData, boolean includeSynonyms) throws SQLException {
super(databaseMetaData);
this.includeSynonyms = includeSynonyms;
lookupDefaultSchema(databaseMetaData);
}
@Override
protected String getDefaultSchema() {
if (defaultSchema != null) {
return defaultSchema;
}
return super.getDefaultSchema();
}
@Override
public void initializeWithTableColumnMetaData(DatabaseMetaData databaseMetaData,
@ -65,7 +81,7 @@ public class OracleTableMetaDataProvider extends GenericTableMetaDataProvider {
}
boolean isOracleCon;
try {
Class oracleConClass = getClass().getClassLoader().loadClass("oracle.jdbc.OracleConnection");
Class<?> oracleConClass = getClass().getClassLoader().loadClass("oracle.jdbc.OracleConnection");
isOracleCon = oracleConClass.isInstance(con);
}
catch (ClassNotFoundException ex) {
@ -109,4 +125,26 @@ public class OracleTableMetaDataProvider extends GenericTableMetaDataProvider {
}
}
/*
* Oracle implementation for detecting current schema
*
* @param databaseMetaData
*/
private void lookupDefaultSchema(DatabaseMetaData databaseMetaData) {
try {
CallableStatement cstmt = null;
try {
cstmt = databaseMetaData.getConnection().prepareCall("{? = call sys_context('USERENV', 'CURRENT_SCHEMA')}");
cstmt.registerOutParameter(1, Types.VARCHAR);
cstmt.execute();
this.defaultSchema = cstmt.getString(1);
}
finally {
if (cstmt != null) {
cstmt.close();
}
}
} catch (Exception ignore) {}
}
}