use of liquibase.database.AbstractJdbcDatabase in project liquibase by liquibase.
the class TableSnapshotGenerator method snapshotObject.
@Override
protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException {
Database database = snapshot.getDatabase();
String objectName = example.getName();
Schema schema = example.getSchema();
List<CachedRow> rs = null;
try {
JdbcDatabaseSnapshot.CachingDatabaseMetaData metaData = ((JdbcDatabaseSnapshot) snapshot).getMetaDataFromCache();
rs = metaData.getTables(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), objectName);
Table table;
if (!rs.isEmpty()) {
table = readTable(rs.get(0), database);
} else {
return null;
}
return table;
} catch (SQLException e) {
throw new DatabaseException(e);
}
}
use of liquibase.database.AbstractJdbcDatabase in project liquibase by liquibase.
the class TableSnapshotGenerator method addTo.
@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
if (!snapshot.getSnapshotControl().shouldInclude(Table.class)) {
return;
}
if (foundObject instanceof Schema) {
Database database = snapshot.getDatabase();
Schema schema = (Schema) foundObject;
List<CachedRow> tableMetaDataRs = null;
try {
tableMetaDataRs = ((JdbcDatabaseSnapshot) snapshot).getMetaDataFromCache().getTables(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), null);
for (CachedRow row : tableMetaDataRs) {
String tableName = row.getString("TABLE_NAME");
Table tableExample = (Table) new Table().setName(cleanNameFromDatabase(tableName, database)).setSchema(schema);
schema.addDatabaseObject(tableExample);
}
} catch (SQLException e) {
throw new DatabaseException(e);
}
}
}
use of liquibase.database.AbstractJdbcDatabase in project liquibase by liquibase.
the class TableSnapshotGenerator method readTable.
protected Table readTable(CachedRow tableMetadataResultSet, Database database) throws SQLException, DatabaseException {
String rawTableName = tableMetadataResultSet.getString("TABLE_NAME");
String rawSchemaName = StringUtil.trimToNull(tableMetadataResultSet.getString("TABLE_SCHEM"));
String rawCatalogName = StringUtil.trimToNull(tableMetadataResultSet.getString("TABLE_CAT"));
String remarks = StringUtil.trimToNull(tableMetadataResultSet.getString("REMARKS"));
String tablespace = StringUtil.trimToNull(tableMetadataResultSet.getString("TABLESPACE_NAME"));
String defaultTablespaceString = StringUtil.trimToNull(tableMetadataResultSet.getString("DEFAULT_TABLESPACE"));
if (remarks != null) {
// come back escaped sometimes
remarks = remarks.replace("''", "'");
}
Table table = new Table().setName(cleanNameFromDatabase(rawTableName, database));
table.setRemarks(remarks);
table.setTablespace(tablespace);
table.setDefaultTablespace(BooleanUtil.isTrue(Boolean.parseBoolean(defaultTablespaceString)));
CatalogAndSchema schemaFromJdbcInfo = ((AbstractJdbcDatabase) database).getSchemaFromJdbcInfo(rawCatalogName, rawSchemaName);
table.setSchema(new Schema(schemaFromJdbcInfo.getCatalogName(), schemaFromJdbcInfo.getSchemaName()));
if ("Y".equals(tableMetadataResultSet.getString("TEMPORARY"))) {
table.setAttribute("temporary", "GLOBAL");
String duration = tableMetadataResultSet.getString("DURATION");
if ((duration != null) && "SYS$TRANSACTION".equals(duration)) {
table.setAttribute("duration", "ON COMMIT DELETE ROWS");
} else if ((duration != null) && "SYS$SESSION".equals(duration)) {
table.setAttribute("duration", "ON COMMIT PRESERVE ROWS");
}
}
return table;
}
use of liquibase.database.AbstractJdbcDatabase in project liquibase by liquibase.
the class ColumnSnapshotGenerator method addTo.
@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException {
if (!snapshot.getSnapshotControl().shouldInclude(Column.class)) {
return;
}
if (foundObject instanceof Relation) {
Database database = snapshot.getDatabase();
Relation relation = (Relation) foundObject;
List<CachedRow> allColumnsMetadataRs;
try {
JdbcDatabaseSnapshot.CachingDatabaseMetaData databaseMetaData = ((JdbcDatabaseSnapshot) snapshot).getMetaDataFromCache();
Schema schema;
schema = relation.getSchema();
allColumnsMetadataRs = databaseMetaData.getColumns(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), relation.getName(), null);
List<CachedRow> metaDataNotNullConst = databaseMetaData.getNotNullConst(schema.getCatalogName(), schema.getName(), relation.getName());
/*
* Microsoft SQL Server, SAP SQL Anywhere and probably other RDBMS guarantee non-duplicate
* ORDINAL_POSITIONs for the columns of a single table. But they do not guarantee there are no gaps
* in that integers (e.g. if columns have been deleted). So we need to check for that and renumber
* if needed.
*/
TreeMap<Integer, CachedRow> treeSet = new TreeMap<>();
for (CachedRow row : allColumnsMetadataRs) {
treeSet.put(row.getInt("ORDINAL_POSITION"), row);
}
Logger log = Scope.getCurrentScope().getLog(getClass());
// Now we can iterate through the sorted list and repair if needed.
int currentOrdinal = 0;
for (CachedRow row : treeSet.values()) {
currentOrdinal++;
int rsOrdinal = row.getInt("ORDINAL_POSITION");
if (rsOrdinal != currentOrdinal) {
log.fine(String.format("Repairing ORDINAL_POSITION with gaps for table=%s, column name=%s, " + "bad ordinal=%d, new ordinal=%d", relation.getName(), row.getString("COLUMN_NAME"), rsOrdinal, currentOrdinal));
row.set("ORDINAL_POSITION", currentOrdinal);
}
}
// Iterate through all (repaired) rows and add the columns to our result.
for (CachedRow row : allColumnsMetadataRs) {
Column column = readColumn(row, relation, database);
setAutoIncrementDetails(column, database, snapshot);
populateValidateNullableIfNeeded(column, metaDataNotNullConst, database);
column.setAttribute(LIQUIBASE_COMPLETE, !column.isNullable());
relation.getColumns().add(column);
}
} catch (SQLException e) {
throw new DatabaseException(e);
}
}
}
use of liquibase.database.AbstractJdbcDatabase 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()]);
}
Aggregations