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;
}
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));
}
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);
}
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);
}
}
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);
}
}
}
Aggregations