Skip to content

Commit

Permalink
Fixes to get oracle database working with generateChangeLog
Browse files Browse the repository at this point in the history
  • Loading branch information
nvoxland committed Aug 17, 2012
1 parent 050116e commit 258b56f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 21 deletions.
Expand Up @@ -31,7 +31,6 @@
import java.io.Writer;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -182,7 +181,7 @@ public String getDefaultCatalogName() {
if (defaultCatalogName == null) {
if (connection != null) {
try {
defaultCatalogName = connection.getCatalog();
defaultCatalogName = doGetDefaultCatalogName();
} catch (DatabaseException e) {
LogFactory.getLogger().info("Error getting default catalog", e);
}
Expand All @@ -191,16 +190,8 @@ public String getDefaultCatalogName() {
return defaultCatalogName;
}

/**
* Returns the default schema from the current database connection. By default, this will be the user name
* for the current connection. Implementations should override this method for databases which allow for the
* schema to be something other than the current user.
*
* @return The current schema name
* @throws DatabaseException if an error occured
*/
protected String getDefaultDatabaseSchemaName() throws DatabaseException {
return getConnection().getConnectionUserName();
protected String doGetDefaultCatalogName() throws DatabaseException {
return connection.getCatalog();
}

public Schema correctSchema(Schema schema) {
Expand Down Expand Up @@ -259,6 +250,10 @@ protected String correctObjectName(String objectName) {

public String getDefaultSchemaName() {

if (!supportsSchemas()) {
return getDefaultCatalogName();
}

if (defaultSchemaName == null && connection != null) {
defaultSchemaName = doGetDefaultSchemaName();
}
Expand Down
Expand Up @@ -7,6 +7,7 @@
import liquibase.exception.DatabaseException;
import liquibase.exception.DateParseException;
import liquibase.util.JdbcUtils;
import sun.util.logging.resources.logging;

import java.sql.ResultSet;
import java.sql.SQLException;
Expand Down Expand Up @@ -46,7 +47,7 @@ public String getTypeName() {
}

@Override
protected String getDefaultDatabaseSchemaName() throws DatabaseException {//NOPMD
public String getDefaultSchemaName() {
if( defaultSchemaName == null ) {
Statement stmt = null;
ResultSet rs = null;
Expand All @@ -58,11 +59,11 @@ protected String getDefaultDatabaseSchemaName() throws DatabaseException {//NOPM
if( result != null ) {
this.defaultSchemaName = result;
} else {
this.defaultSchemaName = super.getDefaultDatabaseSchemaName();
this.defaultSchemaName = super.getDefaultSchemaName();
}
}
} catch (Exception e) {
throw new DatabaseException("Could not determine current schema", e);
throw new RuntimeException("Could not determine current schema", e);
} finally {
JdbcUtils.closeResultSet(rs);
JdbcUtils.closeStatement(stmt);
Expand Down
Expand Up @@ -2,13 +2,15 @@

import liquibase.database.AbstractDatabase;
import liquibase.database.DatabaseConnection;
import liquibase.database.jvm.JdbcConnection;
import liquibase.database.structure.Schema;
import liquibase.exception.DatabaseException;
import liquibase.logging.LogFactory;
import liquibase.statement.DatabaseFunction;

import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -105,6 +107,27 @@ public boolean supportsSequences() {
return true;
}

/**
* Oracle supports catalogs in liquibase terms
* @return
*/
@Override
public boolean supportsSchemas() {
return false;
}

@Override
protected String doGetDefaultCatalogName() throws DatabaseException {
try {
ResultSet resultSet = ((JdbcConnection) getConnection()).prepareCall("select sys_context( 'userenv', 'current_schema' ) from dual").executeQuery();
resultSet.next();
return resultSet.getString(1);
} catch (Exception e) {
LogFactory.getLogger().info("Error getting default schema", e);
}
return null;
}

public boolean isCorrectDatabaseImplementation(DatabaseConnection conn) throws DatabaseException {
return PRODUCT_NAME.equalsIgnoreCase(conn.getDatabaseProductName());
}
Expand Down
Expand Up @@ -274,6 +274,8 @@ protected Column readColumn(ResultSet columnMetadataResultSet, Relation table, D

column.setDefaultValue(readDefaultValue(columnMetadataResultSet, column, database));

readAdditionalDataType(columnMetadataResultSet, column, database);

return column;
}

Expand All @@ -291,6 +293,19 @@ protected DataType readDataType(ResultSet columnMetadataResultSet, Column column
decimalDigits = null;
}

DataType type = new DataType(columnTypeName);
type.setDataTypeId(dataType);
type.setColumnSize(columnSize);
type.setDecimalDigits(decimalDigits);
type.setColumnSizeUnit(DataType.ColumnSizeUnit.BYTE);

return type;
}

/**
* Some databases, such as Oracle, have problems reading column_def after other metadata has been read, so this is mainly to have a point for more type data to be read in
*/
protected DataType readAdditionalDataType(ResultSet columnMetadataResultSet, Column column, Database database) throws SQLException {
Integer radix = columnMetadataResultSet.getInt("NUM_PREC_RADIX");
if (columnMetadataResultSet.wasNull()) {
radix = null;
Expand All @@ -300,13 +315,9 @@ protected DataType readDataType(ResultSet columnMetadataResultSet, Column column
characterOctetLength = null;
}

DataType type = new DataType(columnTypeName);
type.setDataTypeId(dataType);
type.setColumnSize(columnSize);
type.setDecimalDigits(decimalDigits);
DataType type = column.getType();
type.setRadix(radix);
type.setCharacterOctetLength(characterOctetLength);
type.setColumnSizeUnit(DataType.ColumnSizeUnit.BYTE);

return type;
}
Expand Down
Expand Up @@ -25,7 +25,7 @@ public boolean supports(Database database) {
public int getPriority(Database database) {
return PRIORITY_DATABASE;
}
//
//
//
// /**
// * Oracle specific implementation
Expand Down

0 comments on commit 258b56f

Please sign in to comment.