Search in sources :

Example 1 with JdbcConnection

use of liquibase.database.jvm.JdbcConnection in project dropwizard by dropwizard.

the class AbstractLiquibaseCommand method createDatabase.

private Database createDatabase(ManagedDataSource dataSource, Namespace namespace) throws SQLException, LiquibaseException {
    final DatabaseConnection conn = new JdbcConnection(dataSource.getConnection());
    final Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(conn);
    final String catalogName = namespace.getString("catalog");
    final String schemaName = namespace.getString("schema");
    if (database.supportsCatalogs() && catalogName != null) {
        database.setDefaultCatalogName(catalogName);
        database.setOutputDefaultCatalog(true);
    }
    if (database.supportsSchemas() && schemaName != null) {
        database.setDefaultSchemaName(schemaName);
        database.setOutputDefaultSchema(true);
    }
    return database;
}
Also used : Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection)

Example 2 with JdbcConnection

use of liquibase.database.jvm.JdbcConnection in project spring-boot by spring-projects.

the class LiquibaseEndpoint method invoke.

@Override
public List<LiquibaseReport> invoke() {
    List<LiquibaseReport> reports = new ArrayList<>();
    DatabaseFactory factory = DatabaseFactory.getInstance();
    StandardChangeLogHistoryService service = new StandardChangeLogHistoryService();
    for (Map.Entry<String, SpringLiquibase> entry : this.liquibases.entrySet()) {
        try {
            DataSource dataSource = entry.getValue().getDataSource();
            JdbcConnection connection = new JdbcConnection(dataSource.getConnection());
            try {
                Database database = factory.findCorrectDatabaseImplementation(connection);
                reports.add(new LiquibaseReport(entry.getKey(), service.queryDatabaseChangeLogTable(database)));
            } finally {
                connection.close();
            }
        } catch (Exception ex) {
            throw new IllegalStateException("Unable to get Liquibase changelog", ex);
        }
    }
    return reports;
}
Also used : LiquibaseReport(org.springframework.boot.actuate.endpoint.LiquibaseEndpoint.LiquibaseReport) ArrayList(java.util.ArrayList) JdbcConnection(liquibase.database.jvm.JdbcConnection) StandardChangeLogHistoryService(liquibase.changelog.StandardChangeLogHistoryService) DataSource(javax.sql.DataSource) DatabaseFactory(liquibase.database.DatabaseFactory) SpringLiquibase(liquibase.integration.spring.SpringLiquibase) Database(liquibase.database.Database) Map(java.util.Map)

Example 3 with JdbcConnection

use of liquibase.database.jvm.JdbcConnection in project liquibase by liquibase.

the class H2Database method setConnection.

@Override
public void setConnection(DatabaseConnection conn) {
    Connection sqlConn = null;
    if (!(conn instanceof OfflineConnection)) {
        try {
            if (conn instanceof JdbcConnection) {
                Method wrappedConn = conn.getClass().getMethod("getWrappedConnection");
                wrappedConn.setAccessible(true);
                sqlConn = (Connection) wrappedConn.invoke(conn);
            }
        } catch (Exception e) {
            throw new UnexpectedLiquibaseException(e);
        }
        if (sqlConn != null) {
            Statement statement = null;
            ResultSet resultSet = null;
            try {
                statement = sqlConn.createStatement();
                resultSet = statement.executeQuery("SELECT SCHEMA()");
                String schemaName = null;
                if (resultSet.next()) {
                    schemaName = resultSet.getString(1);
                }
                if (schemaName != null) {
                    this.connectionSchemaName = schemaName;
                }
            } catch (SQLException e) {
                LogFactory.getLogger().info("Could not read current schema name: " + e.getMessage());
            } finally {
                JdbcUtils.close(resultSet, statement);
            }
        }
    }
    super.setConnection(conn);
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) DatabaseConnection(liquibase.database.DatabaseConnection) OfflineConnection(liquibase.database.OfflineConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) ResultSet(java.sql.ResultSet) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection) Method(java.lang.reflect.Method) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) SQLException(java.sql.SQLException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ParseException(java.text.ParseException) DatabaseException(liquibase.exception.DatabaseException) DateParseException(liquibase.exception.DateParseException)

Example 4 with JdbcConnection

use of liquibase.database.jvm.JdbcConnection in project liquibase by liquibase.

the class LiquibaseServletListener method executeUpdate.

/**
     * Executes the Liquibase update.
     */
private void executeUpdate(ServletContext servletContext, InitialContext ic) throws NamingException, SQLException, LiquibaseException {
    setDataSource((String) servletValueContainer.getValue(LIQUIBASE_DATASOURCE));
    if (getDataSource() == null) {
        throw new RuntimeException("Cannot run Liquibase, " + LIQUIBASE_DATASOURCE + " is not set");
    }
    setChangeLogFile((String) servletValueContainer.getValue(LIQUIBASE_CHANGELOG));
    if (getChangeLogFile() == null) {
        throw new RuntimeException("Cannot run Liquibase, " + LIQUIBASE_CHANGELOG + " is not set");
    }
    setContexts((String) servletValueContainer.getValue(LIQUIBASE_CONTEXTS));
    setLabels((String) servletValueContainer.getValue(LIQUIBASE_LABELS));
    this.defaultSchema = StringUtils.trimToNull((String) servletValueContainer.getValue(LIQUIBASE_SCHEMA_DEFAULT));
    Connection connection = null;
    Database database = null;
    try {
        DataSource dataSource = (DataSource) ic.lookup(this.dataSourceName);
        connection = dataSource.getConnection();
        Thread currentThread = Thread.currentThread();
        ClassLoader contextClassLoader = currentThread.getContextClassLoader();
        ResourceAccessor threadClFO = new ClassLoaderResourceAccessor(contextClassLoader);
        ResourceAccessor clFO = new ClassLoaderResourceAccessor();
        ResourceAccessor fsFO = new FileSystemResourceAccessor();
        database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
        database.setDefaultSchemaName(getDefaultSchema());
        Liquibase liquibase = new Liquibase(getChangeLogFile(), new CompositeResourceAccessor(clFO, fsFO, threadClFO), database);
        @SuppressWarnings("unchecked") Enumeration<String> initParameters = servletContext.getInitParameterNames();
        while (initParameters.hasMoreElements()) {
            String name = initParameters.nextElement().trim();
            if (name.startsWith(LIQUIBASE_PARAMETER + ".")) {
                liquibase.setChangeLogParameter(name.substring(LIQUIBASE_PARAMETER.length() + 1), servletValueContainer.getValue(name));
            }
        }
        liquibase.update(new Contexts(getContexts()), new LabelExpression(getLabels()));
    } finally {
        if (database != null) {
            database.close();
        } else if (connection != null) {
            connection.close();
        }
    }
}
Also used : CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) ResourceAccessor(liquibase.resource.ResourceAccessor) Connection(java.sql.Connection) JdbcConnection(liquibase.database.jvm.JdbcConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) Contexts(liquibase.Contexts) DataSource(javax.sql.DataSource) Liquibase(liquibase.Liquibase) CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) Database(liquibase.database.Database) LabelExpression(liquibase.LabelExpression) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor)

Example 5 with JdbcConnection

use of liquibase.database.jvm.JdbcConnection in project liquibase by liquibase.

the class SpringLiquibase method getDatabaseProductName.

public String getDatabaseProductName() throws DatabaseException {
    Connection connection = null;
    Database database = null;
    String name = "unknown";
    try {
        connection = getDataSource().getConnection();
        database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
        name = database.getDatabaseProductName();
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        if (database != null) {
            database.close();
        } else if (connection != null) {
            try {
                if (!connection.getAutoCommit()) {
                    connection.rollback();
                }
                connection.close();
            } catch (Exception e) {
                log.warning("problem closing connection", e);
            }
        }
    }
    return name;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) URLConnection(java.net.URLConnection) DatabaseConnection(liquibase.database.DatabaseConnection) OfflineConnection(liquibase.database.OfflineConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) Database(liquibase.database.Database) JdbcConnection(liquibase.database.jvm.JdbcConnection) DatabaseException(liquibase.exception.DatabaseException) SQLException(java.sql.SQLException) DatabaseException(liquibase.exception.DatabaseException) LiquibaseException(liquibase.exception.LiquibaseException)

Aggregations

JdbcConnection (liquibase.database.jvm.JdbcConnection)26 DatabaseException (liquibase.exception.DatabaseException)15 SQLException (java.sql.SQLException)13 OfflineConnection (liquibase.database.OfflineConnection)11 DatabaseConnection (liquibase.database.DatabaseConnection)10 Connection (java.sql.Connection)8 Database (liquibase.database.Database)8 ResultSet (java.sql.ResultSet)7 Statement (java.sql.Statement)6 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)6 RawSqlStatement (liquibase.statement.core.RawSqlStatement)5 Driver (java.sql.Driver)3 ArrayList (java.util.ArrayList)3 Liquibase (liquibase.Liquibase)3 DatabaseFactory (liquibase.database.DatabaseFactory)3 Method (java.lang.reflect.Method)2 CallableStatement (java.sql.CallableStatement)2 PreparedStatement (java.sql.PreparedStatement)2 DataSource (javax.sql.DataSource)2 LiquibaseException (liquibase.exception.LiquibaseException)2