use of liquibase.database.OfflineConnection in project liquibase by liquibase.
the class DatabaseSnapshot method load.
@Override
public void load(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throws ParsedNodeException {
try {
Map<String, DatabaseObject> referencedObjects = new HashMap<>();
Map<String, DatabaseObject> objects = new HashMap<>();
Map<String, DatabaseObject> allObjects = new HashMap<>();
ParsedNode databaseNode = parsedNode.getChild(null, "database");
DatabaseConnection connection = getDatabase().getConnection();
if ((databaseNode != null) && (connection instanceof OfflineConnection)) {
((OfflineConnection) connection).setDatabaseMajorVersion(databaseNode.getChildValue(null, "majorVersion", Integer.class));
((OfflineConnection) connection).setDatabaseMinorVersion(databaseNode.getChildValue(null, "minorVersion", Integer.class));
((OfflineConnection) connection).setProductVersion(databaseNode.getChildValue(null, "productVersion", String.class));
((OfflineConnection) connection).setConnectionUserName(databaseNode.getChildValue(null, "user", String.class));
}
loadObjects(referencedObjects, allObjects, parsedNode.getChild(null, "referencedObjects"), resourceAccessor);
loadObjects(objects, allObjects, parsedNode.getChild(null, "objects"), resourceAccessor);
for (DatabaseObject object : allObjects.values()) {
for (String attr : new ArrayList<>(object.getAttributes())) {
Object value = object.getAttribute(attr, Object.class);
if ((value instanceof String) && allObjects.containsKey(value)) {
if (ObjectUtil.hasProperty(object, attr)) {
ObjectUtil.setProperty(object, attr, allObjects.get(value));
} else {
object.setAttribute(attr, allObjects.get(value));
}
} else if ((value instanceof Collection) && !((Collection) value).isEmpty() && allObjects.containsKey(((Collection) value).iterator().next())) {
List<DatabaseObject> newList = new ArrayList<DatabaseObject>();
for (String element : (Collection<String>) value) {
newList.add(allObjects.get(element));
}
if (ObjectUtil.hasProperty(object, attr)) {
ObjectUtil.setProperty(object, attr, newList);
} else {
object.setAttribute(attr, newList);
}
} else {
if ((value != null) && ObjectUtil.hasProperty(object, attr)) {
if ((value instanceof byte[]) && ObjectUtil.getPropertyType(object, attr).equals(String.class)) {
value = new String((byte[]) value, GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue());
}
object.setAttribute(attr, null);
ObjectUtil.setProperty(object, attr, value);
}
}
}
}
for (DatabaseObject object : objects.values()) {
this.allFound.add(object);
}
for (DatabaseObject object : referencedObjects.values()) {
this.referencedObjects.add(object);
}
} catch (Exception e) {
throw new ParsedNodeException(e);
}
}
use of liquibase.database.OfflineConnection in project liquibase by liquibase.
the class Ingres9Database method getViewDefinition.
@Override
public String getViewDefinition(CatalogAndSchema schema, String viewName) throws DatabaseException {
final String sql = "select text_segment from iiviews where table_name = '" + viewName + "'";
Statement stmt = null;
String definition = "";
try {
if (getConnection() instanceof OfflineConnection) {
throw new DatabaseException("Cannot execute commands against an offline database");
}
stmt = ((JdbcConnection) getConnection()).getUnderlyingConnection().createStatement();
try (ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
definition += rs.getString("text_segment");
}
}
} catch (Exception ex) {
JdbcUtil.closeStatement(stmt);
stmt = null;
return null;
} finally {
JdbcUtil.closeStatement(stmt);
}
if (definition == null) {
return null;
}
return CREATE_VIEW_AS_PATTERN.matcher(definition).replaceFirst("");
}
use of liquibase.database.OfflineConnection in project liquibase by liquibase.
the class MSSQLDatabase method isCaseSensitive.
@Override
public boolean isCaseSensitive() {
if (caseSensitive == null) {
try {
if (getConnection() instanceof JdbcConnection) {
String catalog = getConnection().getCatalog();
String sql = "SELECT CONVERT([sysname], DATABASEPROPERTYEX(N'" + escapeStringForDatabase(catalog) + "', 'Collation'))";
String collation = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", this).queryForObject(new RawSqlStatement(sql), String.class);
caseSensitive = (collation != null) && !collation.contains("_CI_");
} else if (getConnection() instanceof OfflineConnection) {
caseSensitive = ((OfflineConnection) getConnection()).isCaseSensitive();
}
} catch (DatabaseException e) {
Scope.getCurrentScope().getLog(getClass()).warning("Cannot determine case sensitivity from MSSQL", e);
}
}
return (caseSensitive != null) && caseSensitive;
}
use of liquibase.database.OfflineConnection in project liquibase by liquibase.
the class MSSQLDatabase method sendsStringParametersAsUnicode.
/**
* Determines if the SQL Server instance assigns Unicode data types (e.g. nvarchar) to strings.
*
* @return true if the SQL Server instance uses Unicode types by default, false if not.
*/
public boolean sendsStringParametersAsUnicode() {
if (sendsStringParametersAsUnicode == null) {
try {
if (getConnection() instanceof JdbcConnection) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT CONVERT([sysname], SQL_VARIANT_PROPERTY(?, 'BaseType'))";
ps = ((JdbcConnection) getConnection()).prepareStatement(sql);
ps.setString(1, "Liquibase");
rs = ps.executeQuery();
String baseType = null;
if (rs.next()) {
baseType = rs.getString(1);
}
// baseTypes starting with "n" can be something like nvarchar (or nchar)
sendsStringParametersAsUnicode = (baseType == null) || baseType.startsWith("n");
} finally {
JdbcUtil.close(rs, ps);
}
} else if (getConnection() instanceof OfflineConnection) {
sendsStringParametersAsUnicode = ((OfflineConnection) getConnection()).getSendsStringParametersAsUnicode();
}
} catch (SQLException | DatabaseException e) {
Scope.getCurrentScope().getLog(getClass()).warning("Cannot determine whether String parameters are sent as Unicode for MSSQL", e);
}
}
return (sendsStringParametersAsUnicode == null) ? true : sendsStringParametersAsUnicode;
}
use of liquibase.database.OfflineConnection in project liquibase by liquibase.
the class OracleDatabase method validate.
@Override
public ValidationErrors validate() {
ValidationErrors errors = super.validate();
DatabaseConnection connection = getConnection();
if ((connection == null) || (connection instanceof OfflineConnection)) {
// noinspection HardCodedStringLiteral
Scope.getCurrentScope().getLog(getClass()).info("Cannot validate offline database");
return errors;
}
if (!canAccessDbaRecycleBin()) {
errors.addWarning(getDbaRecycleBinWarning());
}
return errors;
}
Aggregations