use of liquibase.database.jvm.JdbcConnection in project liquibase by liquibase.
the class DatabaseTestContext method openDatabaseConnection.
public DatabaseConnection openDatabaseConnection(String url) throws Exception {
String username = getUsername(url);
String password = getPassword(url);
JUnitJDBCDriverClassLoader jdbcDriverLoader = JUnitJDBCDriverClassLoader.getInstance();
final Driver driver;
try {
driver = (Driver) Class.forName(DatabaseFactory.getInstance().findDefaultDriver(url), true, jdbcDriverLoader).newInstance();
} catch (Exception e) {
System.out.println("Could not connect to " + url + ": Will not test against. " + e.getMessage());
//could not connect
return null;
}
Properties info = new Properties();
info.put("user", username);
if (password != null) {
info.put("password", password);
}
//for db2
info.put("retrieveMessagesFromServerOnGetMessage", "true");
Connection connection;
try {
connection = driver.connect(url, info);
} catch (SQLException e) {
System.out.println("Could not connect to " + url + ": Will not test against. " + e.getMessage());
//could not connect
return null;
}
if (connection == null) {
throw new DatabaseException("Connection could not be created to " + url + " with driver " + driver.getClass().getName() + ". Possibly the wrong driver for the given database URL");
}
return new JdbcConnection(connection);
}
use of liquibase.database.jvm.JdbcConnection in project liquibase by liquibase.
the class SpringLiquibase method createDatabase.
/**
* Subclasses may override this method add change some database settings such as
* default schema before returning the database object.
*
* @param c
* @return a Database implementation retrieved from the {@link DatabaseFactory}.
* @throws DatabaseException
*/
protected Database createDatabase(Connection c, ResourceAccessor resourceAccessor) throws DatabaseException {
DatabaseConnection liquibaseConnection;
if (c == null) {
log.warning("Null connection returned by liquibase datasource. Using offline unknown database");
liquibaseConnection = new OfflineConnection("offline:unknown", resourceAccessor);
} else {
liquibaseConnection = new JdbcConnection(c);
}
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(liquibaseConnection);
if (StringUtils.trimToNull(this.defaultSchema) != null) {
database.setDefaultSchemaName(this.defaultSchema);
}
return database;
}
use of liquibase.database.jvm.JdbcConnection in project liquibase by liquibase.
the class CatalogSnapshotGenerator method getDatabaseCatalogNames.
protected String[] getDatabaseCatalogNames(Database database) throws SQLException, DatabaseException {
List<String> returnList = new ArrayList<String>();
ResultSet catalogs = null;
try {
if (((AbstractJdbcDatabase) database).jdbcCallsCatalogsSchemas()) {
catalogs = ((JdbcConnection) database.getConnection()).getMetaData().getSchemas();
} else {
catalogs = ((JdbcConnection) database.getConnection()).getMetaData().getCatalogs();
}
while (catalogs.next()) {
if (((AbstractJdbcDatabase) database).jdbcCallsCatalogsSchemas()) {
returnList.add(catalogs.getString("TABLE_SCHEM"));
} else {
returnList.add(catalogs.getString("TABLE_CAT"));
}
}
} finally {
if (catalogs != null) {
try {
catalogs.close();
} catch (SQLException ignore) {
}
}
}
return returnList.toArray(new String[returnList.size()]);
}
use of liquibase.database.jvm.JdbcConnection in project liquibase by liquibase.
the class ColumnSnapshotGenerator method readColumn.
protected Column readColumn(CachedRow columnMetadataResultSet, Relation table, Database database) throws SQLException, DatabaseException {
String rawTableName = (String) columnMetadataResultSet.get("TABLE_NAME");
String rawColumnName = (String) columnMetadataResultSet.get("COLUMN_NAME");
String rawSchemaName = StringUtils.trimToNull((String) columnMetadataResultSet.get("TABLE_SCHEM"));
String rawCatalogName = StringUtils.trimToNull((String) columnMetadataResultSet.get("TABLE_CAT"));
String remarks = StringUtils.trimToNull((String) columnMetadataResultSet.get("REMARKS"));
if (remarks != null) {
//come back escaped sometimes
remarks = remarks.replace("''", "'");
}
Integer position = columnMetadataResultSet.getInt("ORDINAL_POSITION");
Column column = new Column();
column.setName(StringUtils.trimToNull(rawColumnName));
column.setRelation(table);
column.setRemarks(remarks);
column.setOrder(position);
if (database instanceof OracleDatabase) {
String nullable = columnMetadataResultSet.getString("NULLABLE");
if (nullable.equals("Y")) {
column.setNullable(true);
} else {
column.setNullable(false);
}
} else {
int nullable = columnMetadataResultSet.getInt("NULLABLE");
if (nullable == DatabaseMetaData.columnNoNulls) {
column.setNullable(false);
} else if (nullable == DatabaseMetaData.columnNullable) {
column.setNullable(true);
} else if (nullable == DatabaseMetaData.columnNullableUnknown) {
LogFactory.getLogger().info("Unknown nullable state for column " + column.toString() + ". Assuming nullable");
column.setNullable(true);
}
}
if (database.supportsAutoIncrement()) {
if (table instanceof Table) {
if (database instanceof OracleDatabase) {
String data_default = StringUtils.trimToEmpty((String) columnMetadataResultSet.get("DATA_DEFAULT")).toLowerCase();
if (data_default.contains("iseq$$") && data_default.endsWith("nextval")) {
column.setAutoIncrementInformation(new Column.AutoIncrementInformation());
}
} else {
if (columnMetadataResultSet.containsColumn("IS_AUTOINCREMENT")) {
String isAutoincrement = (String) columnMetadataResultSet.get("IS_AUTOINCREMENT");
isAutoincrement = StringUtils.trimToNull(isAutoincrement);
if (isAutoincrement == null) {
column.setAutoIncrementInformation(null);
} else if (isAutoincrement.equals("YES")) {
column.setAutoIncrementInformation(new Column.AutoIncrementInformation());
} else if (isAutoincrement.equals("NO")) {
column.setAutoIncrementInformation(null);
} else if (isAutoincrement.equals("")) {
LogFactory.getLogger().info("Unknown auto increment state for column " + column.toString() + ". Assuming not auto increment");
column.setAutoIncrementInformation(null);
} else {
throw new UnexpectedLiquibaseException("Unknown is_autoincrement value: '" + isAutoincrement + "'");
}
} else {
//probably older version of java, need to select from the column to find out if it is auto-increment
String selectStatement;
if (database.getDatabaseProductName().startsWith("DB2 UDB for AS/400")) {
selectStatement = "select " + database.escapeColumnName(rawCatalogName, rawSchemaName, rawTableName, rawColumnName) + " from " + rawSchemaName + "." + rawTableName + " where 0=1";
LogFactory.getLogger().debug("rawCatalogName : <" + rawCatalogName + ">");
LogFactory.getLogger().debug("rawSchemaName : <" + rawSchemaName + ">");
LogFactory.getLogger().debug("rawTableName : <" + rawTableName + ">");
LogFactory.getLogger().debug("raw selectStatement : <" + selectStatement + ">");
} else {
selectStatement = "select " + database.escapeColumnName(rawCatalogName, rawSchemaName, rawTableName, rawColumnName) + " from " + database.escapeTableName(rawCatalogName, rawSchemaName, rawTableName) + " where 0=1";
}
LogFactory.getLogger().debug("Checking " + rawTableName + "." + rawCatalogName + " for auto-increment with SQL: '" + selectStatement + "'");
Connection underlyingConnection = ((JdbcConnection) database.getConnection()).getUnderlyingConnection();
Statement statement = null;
ResultSet columnSelectRS = null;
try {
statement = underlyingConnection.createStatement();
columnSelectRS = statement.executeQuery(selectStatement);
if (columnSelectRS.getMetaData().isAutoIncrement(1)) {
column.setAutoIncrementInformation(new Column.AutoIncrementInformation());
} else {
column.setAutoIncrementInformation(null);
}
} finally {
try {
if (statement != null) {
statement.close();
}
} catch (SQLException ignore) {
}
if (columnSelectRS != null) {
columnSelectRS.close();
}
}
}
}
}
}
DataType type = readDataType(columnMetadataResultSet, column, database);
column.setType(type);
Object defaultValue = readDefaultValue(columnMetadataResultSet, column, database);
if (defaultValue != null && defaultValue instanceof DatabaseFunction && ((DatabaseFunction) defaultValue).getValue().matches("\\w+")) {
defaultValue = new DatabaseFunction(((DatabaseFunction) defaultValue).getValue().toUpperCase());
}
column.setDefaultValue(defaultValue);
column.setDefaultValueConstraintName(columnMetadataResultSet.getString("COLUMN_DEF_NAME"));
return column;
}
use of liquibase.database.jvm.JdbcConnection in project liquibase by liquibase.
the class SchemaSnapshotGenerator method getDatabaseSchemaNames.
protected String[] getDatabaseSchemaNames(Database database) throws SQLException, DatabaseException {
List<String> returnList = new ArrayList<String>();
ResultSet schemas = null;
try {
schemas = ((JdbcConnection) database.getConnection()).getMetaData().getSchemas();
while (schemas.next()) {
returnList.add(JdbcUtils.getValueForColumn(schemas, "TABLE_SCHEM", database));
}
} finally {
if (schemas != null) {
schemas.close();
}
}
return returnList.toArray(new String[returnList.size()]);
}
Aggregations