Search in sources :

Example 16 with OracleDatabase

use of liquibase.database.core.OracleDatabase in project liquibase by liquibase.

the class DataTypeFactory method fromDescription.

//    public Map<String, SortedSet<Class<? extends LiquibaseDataType>>> getRegistry() {
//        return registry;
//    }
//    public LiquibaseDataType fromDescription(String dataTypeDefinition) {
//        return fromDescription(dataTypeDefinition, null);
//    }
public LiquibaseDataType fromDescription(String dataTypeDefinition, Database database) {
    String dataTypeName = dataTypeDefinition;
    if (dataTypeName.matches(".+\\(.*\\).*")) {
        dataTypeName = dataTypeName.replaceFirst("\\s*\\(.*\\)", "");
    }
    if (dataTypeName.matches(".+\\{.*")) {
        dataTypeName = dataTypeName.replaceFirst("\\s*\\{.*", "");
    }
    boolean autoIncrement = false;
    if (dataTypeName.endsWith(" identity")) {
        dataTypeName = dataTypeName.replaceFirst(" identity$", "");
        autoIncrement = true;
    }
    // unquote delimited identifiers
    final String[][] quotePairs = new String[][] { // double quotes
    { "\"", "\"" }, // square brackets (a la mssql)
    { "[", "]" }, // backticks (a la mysql)
    { "`", "`" }, // single quotes
    { "'", "'" } };
    for (String[] quotePair : quotePairs) {
        String openQuote = quotePair[0];
        String closeQuote = quotePair[1];
        if (dataTypeName.startsWith(openQuote)) {
            int indexOfCloseQuote = dataTypeName.indexOf(closeQuote, openQuote.length());
            if (indexOfCloseQuote != -1 && dataTypeName.indexOf(closeQuote, indexOfCloseQuote + closeQuote.length()) == -1) {
                dataTypeName = dataTypeName.substring(openQuote.length(), indexOfCloseQuote) + dataTypeName.substring(indexOfCloseQuote + closeQuote.length(), dataTypeName.length());
                break;
            }
        }
    }
    String additionalInfo = null;
    if (dataTypeName.toLowerCase().startsWith("bit varying") || dataTypeName.toLowerCase().startsWith("character varying")) {
    //not going to do anything. Special case for postgres in our tests, need to better support handling these types of differences
    } else {
        String[] splitTypeName = dataTypeName.split("\\s+", 2);
        dataTypeName = splitTypeName[0];
        if (splitTypeName.length > 1) {
            additionalInfo = splitTypeName[1];
        }
    }
    Collection<Class<? extends LiquibaseDataType>> classes = registry.get(dataTypeName.toLowerCase());
    LiquibaseDataType liquibaseDataType = null;
    if (classes == null) {
        if (dataTypeName.toUpperCase().startsWith("INTERVAL")) {
            liquibaseDataType = new UnknownType(dataTypeDefinition);
        } else {
            liquibaseDataType = new UnknownType(dataTypeName);
        }
    } else {
        Iterator<Class<? extends LiquibaseDataType>> iterator = classes.iterator();
        do {
            try {
                liquibaseDataType = iterator.next().newInstance();
            } catch (Exception e) {
                throw new UnexpectedLiquibaseException(e);
            }
        } while ((database != null) && !liquibaseDataType.supports(database) && iterator.hasNext());
    }
    if ((database != null) && !liquibaseDataType.supports(database)) {
        throw new UnexpectedLiquibaseException("Could not find type for " + liquibaseDataType.toString() + " for databaes " + database.getShortName());
    }
    if (liquibaseDataType == null) {
        liquibaseDataType = new UnknownType(dataTypeName);
    }
    liquibaseDataType.setAdditionalInformation(additionalInfo);
    if (dataTypeDefinition.matches(".+\\s*\\(.*")) {
        String paramStrings = dataTypeDefinition.replaceFirst(".*?\\(", "").replaceFirst("\\).*", "");
        String[] params = paramStrings.split(",");
        for (String param : params) {
            param = StringUtils.trimToNull(param);
            if (param != null) {
                if (liquibaseDataType instanceof CharType && !(database instanceof OracleDatabase)) {
                    //only use byte types on oracle, not sure what else supports it
                    param = param.replaceFirst(" BYTE", "");
                }
                liquibaseDataType.addParameter(param);
            }
        }
    }
    if (dataTypeDefinition.matches(".*\\{.*")) {
        String paramStrings = dataTypeDefinition.replaceFirst(".*?\\{", "").replaceFirst("\\}.*", "");
        String[] params = paramStrings.split(",");
        for (String param : params) {
            param = StringUtils.trimToNull(param);
            if (param != null) {
                String[] paramAndValue = param.split(":", 2);
                try {
                    ObjectUtil.setProperty(liquibaseDataType, paramAndValue[0], paramAndValue[1]);
                } catch (Exception e) {
                    throw new RuntimeException("Unknown property " + paramAndValue[0] + " for " + liquibaseDataType.getClass().getName() + " " + liquibaseDataType.toString());
                }
            }
        }
    }
    if (autoIncrement && liquibaseDataType instanceof IntType) {
        ((IntType) liquibaseDataType).setAutoIncrement(true);
    }
    if (autoIncrement && liquibaseDataType instanceof BigIntType) {
        ((BigIntType) liquibaseDataType).setAutoIncrement(true);
    }
    liquibaseDataType.finishInitialization(dataTypeDefinition);
    return liquibaseDataType;
}
Also used : BigIntType(liquibase.datatype.core.BigIntType) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) IntType(liquibase.datatype.core.IntType) BigIntType(liquibase.datatype.core.BigIntType) UnknownType(liquibase.datatype.core.UnknownType) OracleDatabase(liquibase.database.core.OracleDatabase) CharType(liquibase.datatype.core.CharType) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 17 with OracleDatabase

use of liquibase.database.core.OracleDatabase in project liquibase by liquibase.

the class JdbcExecutorTest method getInstance.

@Test
public void getInstance() {
    final Database oracle1 = new OracleDatabase() {

        @Override
        public boolean equals(Object o) {
            return o == this;
        }
    };
    final Database oracle2 = new OracleDatabase() {

        @Override
        public boolean equals(Object o) {
            return o == this;
        }
    };
    final Database mysql = new MySQLDatabase() {

        @Override
        public boolean equals(Object o) {
            return o == this;
        }
    };
    assertNotNull(ExecutorService.getInstance().getExecutor(oracle1));
    assertNotNull(ExecutorService.getInstance().getExecutor(oracle2));
    assertNotNull(ExecutorService.getInstance().getExecutor(mysql));
    assertTrue(ExecutorService.getInstance().getExecutor(oracle1) == ExecutorService.getInstance().getExecutor(oracle1));
    assertTrue(ExecutorService.getInstance().getExecutor(oracle2) == ExecutorService.getInstance().getExecutor(oracle2));
    assertTrue(ExecutorService.getInstance().getExecutor(mysql) == ExecutorService.getInstance().getExecutor(mysql));
    assertTrue(ExecutorService.getInstance().getExecutor(oracle1) != ExecutorService.getInstance().getExecutor(oracle2));
    assertTrue(ExecutorService.getInstance().getExecutor(oracle1) != ExecutorService.getInstance().getExecutor(mysql));
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) OracleDatabase(liquibase.database.core.OracleDatabase) MySQLDatabase(liquibase.database.core.MySQLDatabase) Database(liquibase.database.Database) MySQLDatabase(liquibase.database.core.MySQLDatabase) Test(org.junit.Test)

Example 18 with OracleDatabase

use of liquibase.database.core.OracleDatabase in project liquibase by liquibase.

the class LockServiceFactoryTest method getLockService.

@Test
public void getLockService() {
    final Database oracle1 = new OracleDatabase() {

        @Override
        public boolean equals(Object o) {
            return o == this;
        }
    };
    final Database oracle2 = new OracleDatabase() {

        @Override
        public boolean equals(Object o) {
            return o == this;
        }
    };
    final Database mysql = new MySQLDatabase() {

        @Override
        public boolean equals(Object o) {
            return o == this;
        }
    };
    DatabaseFactory databaseFactory = DatabaseFactory.getInstance();
    databaseFactory.register(oracle1);
    databaseFactory.register(oracle2);
    databaseFactory.register(mysql);
    LockServiceFactory lockServiceFactory = LockServiceFactory.getInstance();
    assertNotNull(lockServiceFactory.getLockService(oracle1));
    assertNotNull(lockServiceFactory.getLockService(oracle2));
    assertNotNull(lockServiceFactory.getLockService(mysql));
    assertTrue(lockServiceFactory.getLockService(oracle1) == lockServiceFactory.getLockService(oracle1));
    assertTrue(lockServiceFactory.getLockService(oracle2) == lockServiceFactory.getLockService(oracle2));
    assertTrue(lockServiceFactory.getLockService(mysql) == lockServiceFactory.getLockService(mysql));
    assertTrue(lockServiceFactory.getLockService(oracle1) != lockServiceFactory.getLockService(oracle2));
    assertTrue(lockServiceFactory.getLockService(oracle1) != lockServiceFactory.getLockService(mysql));
    assertTrue(lockServiceFactory.getLockService(getMockDatabase()) instanceof MockLockService);
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) MockLockService(liquibase.lockservice.ext.MockLockService) DatabaseFactory(liquibase.database.DatabaseFactory) MockDatabase(liquibase.sdk.database.MockDatabase) OracleDatabase(liquibase.database.core.OracleDatabase) MySQLDatabase(liquibase.database.core.MySQLDatabase) Database(liquibase.database.Database) MySQLDatabase(liquibase.database.core.MySQLDatabase) Test(org.junit.Test)

Example 19 with OracleDatabase

use of liquibase.database.core.OracleDatabase in project liquibase by liquibase.

the class SqlUtil method parseValue.

public static Object parseValue(Database database, Object val, DataType type) {
    if (!(val instanceof String)) {
        return val;
    }
    int typeId = Integer.MIN_VALUE;
    if (type.getDataTypeId() != null) {
        typeId = type.getDataTypeId();
    }
    String typeName = type.getTypeName();
    LiquibaseDataType liquibaseDataType = DataTypeFactory.getInstance().from(type, database);
    String stringVal = (String) val;
    if (stringVal.isEmpty()) {
        if (liquibaseDataType instanceof CharType) {
            return "";
        } else {
            return null;
        }
    }
    if (database instanceof OracleDatabase && !stringVal.startsWith("'") && !stringVal.endsWith("'")) {
        //oracle returns functions without quotes
        Object maybeDate = null;
        if (liquibaseDataType instanceof DateType || typeId == Types.DATE) {
            if (stringVal.endsWith("'HH24:MI:SS')")) {
                maybeDate = DataTypeFactory.getInstance().fromDescription("time", database).sqlToObject(stringVal, database);
            } else {
                maybeDate = DataTypeFactory.getInstance().fromDescription("date", database).sqlToObject(stringVal, database);
            }
        } else if (liquibaseDataType instanceof DateTimeType || typeId == Types.TIMESTAMP) {
            maybeDate = DataTypeFactory.getInstance().fromDescription("datetime", database).sqlToObject(stringVal, database);
        } else if (!stringVal.matches("\\d+\\.?\\d*")) {
            //not just a number
            return new DatabaseFunction(stringVal);
        }
        if (maybeDate != null) {
            if (maybeDate instanceof java.util.Date) {
                return maybeDate;
            } else {
                return new DatabaseFunction(stringVal);
            }
        }
    }
    boolean strippedSingleQuotes = false;
    if (stringVal.startsWith("'") && stringVal.endsWith("'")) {
        stringVal = stringVal.substring(1, stringVal.length() - 1);
        strippedSingleQuotes = true;
    } else if (stringVal.startsWith("((") && stringVal.endsWith("))")) {
        stringVal = stringVal.substring(2, stringVal.length() - 2);
    } else if (stringVal.startsWith("('") && stringVal.endsWith("')")) {
        stringVal = stringVal.substring(2, stringVal.length() - 2);
    } else if (stringVal.startsWith("(") && stringVal.endsWith(")")) {
        return new DatabaseFunction(stringVal.substring(1, stringVal.length() - 1));
    }
    Scanner scanner = new Scanner(stringVal.trim());
    if (typeId == Types.ARRAY) {
        return new DatabaseFunction(stringVal);
    } else if ((liquibaseDataType instanceof BigIntType || typeId == Types.BIGINT)) {
        if (scanner.hasNextBigInteger()) {
            return scanner.nextBigInteger();
        } else {
            return new DatabaseFunction(stringVal);
        }
    } else if (typeId == Types.BINARY) {
        return new DatabaseFunction(stringVal.trim());
    } else if (typeId == Types.BIT) {
        if (stringVal.startsWith("b'") || stringVal.startsWith("B'")) {
            //mysql returns boolean values as b'0' and b'1'
            stringVal = stringVal.replaceFirst("b'", "").replaceFirst("B'", "").replaceFirst("'$", "");
        }
        stringVal = stringVal.trim();
        if (database instanceof MySQLDatabase) {
            return stringVal.equals("1") || stringVal.equalsIgnoreCase("true");
        }
        Object value;
        if (scanner.hasNextBoolean()) {
            value = scanner.nextBoolean();
        } else {
            value = Integer.valueOf(stringVal);
        }
        return value;
    } else if (liquibaseDataType instanceof BlobType || typeId == Types.BLOB) {
        if (strippedSingleQuotes) {
            return stringVal;
        } else {
            return new DatabaseFunction(stringVal);
        }
    } else if ((liquibaseDataType instanceof BooleanType || typeId == Types.BOOLEAN)) {
        if (scanner.hasNextBoolean()) {
            return scanner.nextBoolean();
        } else {
            return new DatabaseFunction(stringVal);
        }
    } else if (liquibaseDataType instanceof CharType || typeId == Types.CHAR) {
        return stringVal;
    } else if (liquibaseDataType instanceof ClobType || typeId == Types.CLOB) {
        return stringVal;
    } else if (typeId == Types.DATALINK) {
        return new DatabaseFunction(stringVal);
    } else if (liquibaseDataType instanceof DateType || typeId == Types.DATE) {
        if (typeName.equalsIgnoreCase("year")) {
            return stringVal.trim();
        }
        return DataTypeFactory.getInstance().fromDescription("date", database).sqlToObject(stringVal, database);
    } else if ((liquibaseDataType instanceof DecimalType || typeId == Types.DECIMAL)) {
        if (scanner.hasNextBigDecimal()) {
            return scanner.nextBigDecimal();
        } else {
            return new DatabaseFunction(stringVal);
        }
    } else if (typeId == Types.DISTINCT) {
        return new DatabaseFunction(stringVal);
    } else if ((liquibaseDataType instanceof DoubleType || typeId == Types.DOUBLE)) {
        if (scanner.hasNextDouble()) {
            return scanner.nextDouble();
        } else {
            return new DatabaseFunction(stringVal);
        }
    } else if ((liquibaseDataType instanceof FloatType || typeId == Types.FLOAT)) {
        if (scanner.hasNextFloat()) {
            return scanner.nextFloat();
        } else {
            return new DatabaseFunction(stringVal);
        }
    } else if ((liquibaseDataType instanceof IntType || typeId == Types.INTEGER)) {
        if (scanner.hasNextInt()) {
            return scanner.nextInt();
        } else {
            return new DatabaseFunction(stringVal);
        }
    } else if (typeId == Types.JAVA_OBJECT) {
        return new DatabaseFunction(stringVal);
    } else if (typeId == Types.LONGNVARCHAR) {
        return stringVal;
    } else if (typeId == Types.LONGVARBINARY) {
        return new DatabaseFunction(stringVal);
    } else if (typeId == Types.LONGVARCHAR) {
        return stringVal;
    } else if (liquibaseDataType instanceof NCharType || typeId == Types.NCHAR || liquibaseDataType.getName().equalsIgnoreCase("NCLOB")) {
        return stringVal;
    } else if (typeId == Types.NCLOB) {
        return stringVal;
    } else if (typeId == Types.NULL) {
        return null;
    } else if ((liquibaseDataType instanceof NumberType || typeId == Types.NUMERIC)) {
        if (scanner.hasNextBigDecimal()) {
            if (database instanceof MSSQLDatabase && stringVal.endsWith(".0") || stringVal.endsWith(".00") || stringVal.endsWith(".000")) {
                //MSSQL can store the value with the decimal digits. return it directly to avoid unexpected differences
                return new DatabaseFunction(stringVal);
            }
            return scanner.nextBigDecimal();
        } else {
            if (stringVal.equals("")) {
                //can have numeric default '' on sql server
                return new DatabaseFunction("''");
            }
            return new DatabaseFunction(stringVal);
        }
    } else if (liquibaseDataType instanceof NVarcharType || typeId == Types.NVARCHAR) {
        return stringVal;
    } else if (typeId == Types.OTHER) {
        if (database instanceof DB2Database && typeName.equalsIgnoreCase("DECFLOAT")) {
            return new BigDecimal(stringVal);
        }
        return new DatabaseFunction(stringVal);
    } else if (typeId == Types.REAL) {
        return new BigDecimal(stringVal.trim());
    } else if (typeId == Types.REF) {
        return new DatabaseFunction(stringVal);
    } else if (typeId == Types.ROWID) {
        return new DatabaseFunction(stringVal);
    } else if ((liquibaseDataType instanceof SmallIntType || typeId == Types.SMALLINT)) {
        if (scanner.hasNextInt()) {
            return scanner.nextInt();
        } else {
            return new DatabaseFunction(stringVal);
        }
    } else if (typeId == Types.SQLXML) {
        return new DatabaseFunction(stringVal);
    } else if (typeId == Types.STRUCT) {
        return new DatabaseFunction(stringVal);
    } else if (liquibaseDataType instanceof TimeType || typeId == Types.TIME) {
        return DataTypeFactory.getInstance().fromDescription("time", database).sqlToObject(stringVal, database);
    } else if (liquibaseDataType instanceof DateTimeType || liquibaseDataType instanceof TimestampType || typeId == Types.TIMESTAMP) {
        return DataTypeFactory.getInstance().fromDescription("datetime", database).sqlToObject(stringVal, database);
    } else if ((liquibaseDataType instanceof TinyIntType || typeId == Types.TINYINT)) {
        if (scanner.hasNextInt()) {
            return scanner.nextInt();
        } else {
            return new DatabaseFunction(stringVal);
        }
    } else if (typeId == Types.VARBINARY) {
        return new DatabaseFunction(stringVal);
    } else if (liquibaseDataType instanceof VarcharType || typeId == Types.VARCHAR) {
        return stringVal;
    } else if (database instanceof MySQLDatabase && typeName.toLowerCase().startsWith("enum")) {
        return stringVal;
    } else {
        if (stringVal.equals("")) {
            return stringVal;
        }
        LogFactory.getLogger().info("Unknown default value: value '" + stringVal + "' type " + typeName + " (" + type + "). Calling it a function so it's not additionally quoted");
        if (strippedSingleQuotes) {
            //put quotes back
            return new DatabaseFunction("'" + stringVal + "'");
        }
        return new DatabaseFunction(stringVal);
    }
}
Also used : DB2Database(liquibase.database.core.DB2Database) Scanner(java.util.Scanner) DatabaseFunction(liquibase.statement.DatabaseFunction) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) LiquibaseDataType(liquibase.datatype.LiquibaseDataType) MySQLDatabase(liquibase.database.core.MySQLDatabase) BigDecimal(java.math.BigDecimal) OracleDatabase(liquibase.database.core.OracleDatabase)

Example 20 with OracleDatabase

use of liquibase.database.core.OracleDatabase in project liquibase by liquibase.

the class ViewSnapshotGenerator method addTo.

@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
    if (!snapshot.getSnapshotControl().shouldInclude(View.class)) {
        return;
    }
    if (foundObject instanceof Schema) {
        Schema schema = (Schema) foundObject;
        Database database = snapshot.getDatabase();
        List<CachedRow> viewsMetadataRs = null;
        try {
            viewsMetadataRs = ((JdbcDatabaseSnapshot) snapshot).getMetaData().getViews(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), null);
            for (CachedRow row : viewsMetadataRs) {
                View view = new View();
                view.setName(row.getString("TABLE_NAME"));
                view.setSchema(schema);
                view.setRemarks(row.getString("REMARKS"));
                view.setDefinition(row.getString("OBJECT_BODY"));
                if (database instanceof OracleDatabase) {
                    view.setAttribute("editioning", "Y".equals(row.getString("EDITIONING_VIEW")));
                }
                schema.addDatabaseObject(view);
            }
        } catch (SQLException e) {
            throw new DatabaseException(e);
        }
    }
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) CachedRow(liquibase.snapshot.CachedRow) SQLException(java.sql.SQLException) Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) OracleDatabase(liquibase.database.core.OracleDatabase) InformixDatabase(liquibase.database.core.InformixDatabase) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) View(liquibase.structure.core.View) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) DatabaseException(liquibase.exception.DatabaseException)

Aggregations

OracleDatabase (liquibase.database.core.OracleDatabase)31 Test (org.junit.Test)11 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)9 DB2Database (liquibase.database.core.DB2Database)8 MySQLDatabase (liquibase.database.core.MySQLDatabase)7 Change (liquibase.change.Change)6 ArrayList (java.util.ArrayList)5 Database (liquibase.database.Database)5 H2Database (liquibase.database.core.H2Database)4 Sql (liquibase.sql.Sql)4 HashSet (java.util.HashSet)3 PostgresDatabase (liquibase.database.core.PostgresDatabase)3 DatabaseException (liquibase.exception.DatabaseException)3 LiquibaseException (liquibase.exception.LiquibaseException)3 IOException (java.io.IOException)2 CatalogAndSchema (liquibase.CatalogAndSchema)2 ColumnConfig (liquibase.change.ColumnConfig)2 AddPrimaryKeyChange (liquibase.change.core.AddPrimaryKeyChange)2 AddUniqueConstraintChange (liquibase.change.core.AddUniqueConstraintChange)2 CreateViewChange (liquibase.change.core.CreateViewChange)2