/**
 * Copyright (C) 2011 LShift Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.lshift.hibernate.migrations;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

abstract public class TraceableMigrationElement implements MigrationElement {

  static Logger log = LoggerFactory.getLogger(TraceableMigrationElement.class);

  private List<String> statements = new ArrayList<String>();

  protected void logStatement(String s) {
    statements.add(s);
  }

  protected PreparedStatement prepareAndLog(Connection conn, String sql) throws SQLException {
    // TODO Should demote this to TRACE level
    log.debug("Preparing statement: " + sql);
    logStatement(sql);
    return conn.prepareStatement(sql);
  }

  protected void prepareAndLogAndExecute(Connection conn, String sql) throws SQLException {
    PreparedStatement stmt = prepareAndLog(conn, sql);
    stmt.execute();
    stmt.close();
  }

  @Override
  public List<String> getStatements() {
    return statements;
  }
}

/**
 * Copyright (C) 2011 LShift Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.lshift.hibernate.migrations;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

abstract public class TraceableMigrationElement implements MigrationElement {

  static Logger log = LoggerFactory.getLogger(TraceableMigrationElement.class);

  private List<String> statements = new ArrayList<String>();

  protected void logStatement(String s) {
    statements.add(s);
  }

  protected PreparedStatement prepareAndLog(Connection conn, String sql) throws SQLException {
    log.trace("Preparing statement: " + sql);
    logStatement(sql);
    return conn.prepareStatement(sql);
  }

  protected void prepareAndLogAndExecute(Connection conn, String sql) throws SQLException {

    PreparedStatement stmt = prepareAndLog(conn, sql);
    stmt.execute();
    stmt.close();
  }

  @Override
  public List<String> getStatements() {
    return statements;
  }
}

Add tracing to migration steps