Skip to content
Browse files

Fixes to get oracle database working with generateChangeLog

  • Loading branch information...
1 parent 050116e commit 258b56ff62f3e1bd4fcbc7a57b01ef99eb5ee8de @nvoxland nvoxland committed Aug 17, 2012
View
19 liquibase-core/src/main/java/liquibase/database/AbstractDatabase.java
@@ -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;
@@ -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);
}
@@ -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) {
@@ -259,6 +250,10 @@ protected String correctObjectName(String objectName) {
public String getDefaultSchemaName() {
+ if (!supportsSchemas()) {
+ return getDefaultCatalogName();
+ }
+
if (defaultSchemaName == null && connection != null) {
defaultSchemaName = doGetDefaultSchemaName();
}
View
7 liquibase-core/src/main/java/liquibase/database/core/DB2Database.java
@@ -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;
@@ -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;
@@ -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);
View
23 liquibase-core/src/main/java/liquibase/database/core/OracleDatabase.java
@@ -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;
@@ -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());
}
View
21 liquibase-core/src/main/java/liquibase/snapshot/jvm/JdbcDatabaseSnapshotGenerator.java
@@ -274,6 +274,8 @@ protected Column readColumn(ResultSet columnMetadataResultSet, Relation table, D
column.setDefaultValue(readDefaultValue(columnMetadataResultSet, column, database));
+ readAdditionalDataType(columnMetadataResultSet, column, database);
+
return column;
}
@@ -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;
@@ -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;
}
View
2 liquibase-core/src/main/java/liquibase/snapshot/jvm/OracleDatabaseSnapshotGenerator.java
@@ -25,7 +25,7 @@ public boolean supports(Database database) {
public int getPriority(Database database) {
return PRIORITY_DATABASE;
}
-//
+ //
//
// /**
// * Oracle specific implementation

0 comments on commit 258b56f

Please sign in to comment.
Something went wrong with that request. Please try again.