SPR-8270 Adding improved support for detecting current schema name for Oracle
This commit is contained in:
parent
1317b821b9
commit
92d6e5a14c
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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;
|
private boolean generatedKeysColumnNameArraySupported = true;
|
||||||
|
|
||||||
/** database products we know not supporting the use of a String[] for generated keys */
|
/** 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");
|
Arrays.asList("Apache Derby", "HSQL Database Engine");
|
||||||
|
|
||||||
/** Collection of TableParameterMetaData objects */
|
/** Collection of TableParameterMetaData objects */
|
||||||
|
|
@ -267,11 +267,17 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
|
||||||
|
|
||||||
public String metaDataSchemaNameToUse(String schemaName) {
|
public String metaDataSchemaNameToUse(String schemaName) {
|
||||||
if (schemaName == null) {
|
if (schemaName == null) {
|
||||||
return schemaNameToUse(userName);
|
return schemaNameToUse(getDefaultSchema());
|
||||||
}
|
}
|
||||||
return schemaNameToUse(schemaName);
|
return schemaNameToUse(schemaName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provide access to default schema for subclasses.
|
||||||
|
*/
|
||||||
|
protected String getDefaultSchema() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provide access to version info for subclasses.
|
* Provide access to version info for subclasses.
|
||||||
|
|
@ -327,16 +333,19 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
|
||||||
else {
|
else {
|
||||||
TableMetaData tmd;
|
TableMetaData tmd;
|
||||||
if (schemaName == null) {
|
if (schemaName == null) {
|
||||||
tmd = tableMeta.get(userName.toUpperCase());
|
tmd = tableMeta.get(getDefaultSchema());
|
||||||
|
if (tmd == null) {
|
||||||
|
tmd = tableMeta.get(userName.toUpperCase());
|
||||||
|
}
|
||||||
if (tmd == null) {
|
if (tmd == null) {
|
||||||
tmd = tableMeta.get("PUBLIC");
|
tmd = tableMeta.get("PUBLIC");
|
||||||
if (tmd == null) {
|
}
|
||||||
tmd = tableMeta.get("DBO");
|
if (tmd == null) {
|
||||||
}
|
tmd = tableMeta.get("DBO");
|
||||||
if (tmd == null) {
|
}
|
||||||
throw new DataAccessResourceFailureException("Unable to locate table meta data for '" +
|
if (tmd == null) {
|
||||||
tableName + "' in the default schema");
|
throw new DataAccessResourceFailureException("Unable to locate table meta data for '" +
|
||||||
}
|
tableName + "' in the default schema");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -461,6 +470,7 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public String getType() {
|
public String getType() {
|
||||||
return this.type;
|
return this.type;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -17,9 +17,11 @@
|
||||||
package org.springframework.jdbc.core.metadata;
|
package org.springframework.jdbc.core.metadata;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.sql.CallableStatement;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DatabaseMetaData;
|
import java.sql.DatabaseMetaData;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Types;
|
||||||
|
|
||||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||||
import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor;
|
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}.
|
* 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 Thomas Risberg
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
|
@ -37,6 +43,8 @@ public class OracleTableMetaDataProvider extends GenericTableMetaDataProvider {
|
||||||
|
|
||||||
private final boolean includeSynonyms;
|
private final boolean includeSynonyms;
|
||||||
|
|
||||||
|
private String defaultSchema;
|
||||||
|
|
||||||
|
|
||||||
public OracleTableMetaDataProvider(DatabaseMetaData databaseMetaData) throws SQLException {
|
public OracleTableMetaDataProvider(DatabaseMetaData databaseMetaData) throws SQLException {
|
||||||
this(databaseMetaData, false);
|
this(databaseMetaData, false);
|
||||||
|
|
@ -45,8 +53,16 @@ public class OracleTableMetaDataProvider extends GenericTableMetaDataProvider {
|
||||||
public OracleTableMetaDataProvider(DatabaseMetaData databaseMetaData, boolean includeSynonyms) throws SQLException {
|
public OracleTableMetaDataProvider(DatabaseMetaData databaseMetaData, boolean includeSynonyms) throws SQLException {
|
||||||
super(databaseMetaData);
|
super(databaseMetaData);
|
||||||
this.includeSynonyms = includeSynonyms;
|
this.includeSynonyms = includeSynonyms;
|
||||||
|
lookupDefaultSchema(databaseMetaData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getDefaultSchema() {
|
||||||
|
if (defaultSchema != null) {
|
||||||
|
return defaultSchema;
|
||||||
|
}
|
||||||
|
return super.getDefaultSchema();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initializeWithTableColumnMetaData(DatabaseMetaData databaseMetaData,
|
public void initializeWithTableColumnMetaData(DatabaseMetaData databaseMetaData,
|
||||||
|
|
@ -65,7 +81,7 @@ public class OracleTableMetaDataProvider extends GenericTableMetaDataProvider {
|
||||||
}
|
}
|
||||||
boolean isOracleCon;
|
boolean isOracleCon;
|
||||||
try {
|
try {
|
||||||
Class oracleConClass = getClass().getClassLoader().loadClass("oracle.jdbc.OracleConnection");
|
Class<?> oracleConClass = getClass().getClassLoader().loadClass("oracle.jdbc.OracleConnection");
|
||||||
isOracleCon = oracleConClass.isInstance(con);
|
isOracleCon = oracleConClass.isInstance(con);
|
||||||
}
|
}
|
||||||
catch (ClassNotFoundException ex) {
|
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) {}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue