Search in sources :

Example 16 with OfflineConnection

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);
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) OfflineConnection(liquibase.database.OfflineConnection) ParsedNodeException(liquibase.parser.core.ParsedNodeException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException) ParsedNodeException(liquibase.parser.core.ParsedNodeException) DatabaseObject(liquibase.structure.DatabaseObject) DatabaseObjectCollection(liquibase.structure.DatabaseObjectCollection) DatabaseConnection(liquibase.database.DatabaseConnection) DatabaseObject(liquibase.structure.DatabaseObject) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 17 with OfflineConnection

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("");
}
Also used : Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection) DatabaseException(liquibase.exception.DatabaseException) DatabaseException(liquibase.exception.DatabaseException)

Example 18 with OfflineConnection

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;
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection) DatabaseException(liquibase.exception.DatabaseException)

Example 19 with OfflineConnection

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;
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) JdbcConnection(liquibase.database.jvm.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) OfflineConnection(liquibase.database.OfflineConnection) DatabaseException(liquibase.exception.DatabaseException)

Example 20 with OfflineConnection

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;
}
Also used : ValidationErrors(liquibase.exception.ValidationErrors) DatabaseConnection(liquibase.database.DatabaseConnection) OfflineConnection(liquibase.database.OfflineConnection)

Aggregations

OfflineConnection (liquibase.database.OfflineConnection)23 DatabaseException (liquibase.exception.DatabaseException)14 DatabaseConnection (liquibase.database.DatabaseConnection)12 JdbcConnection (liquibase.database.jvm.JdbcConnection)12 ResultSet (java.sql.ResultSet)6 SQLException (java.sql.SQLException)6 Statement (java.sql.Statement)6 Database (liquibase.database.Database)6 RawSqlStatement (liquibase.statement.core.RawSqlStatement)5 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)4 CallableStatement (java.sql.CallableStatement)3 ParseException (java.text.ParseException)3 DateParseException (liquibase.exception.DateParseException)3 ExecutorService (liquibase.executor.ExecutorService)3 Method (java.lang.reflect.Method)2 Connection (java.sql.Connection)2 Map (java.util.Map)2 DatabaseFactory (liquibase.database.DatabaseFactory)2 Executor (liquibase.executor.Executor)2 ParsedNode (liquibase.parser.core.ParsedNode)2