/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.ode.utils;

import org.apache.commons.logging.Log;

import java.sql.*;
import java.util.Map;

/**
 * @author Matthieu Riou <mriou at apache dot org>
 */
public class LoggingConnectionWrapper implements Connection {

    private Connection _conn;
    private Log _log;

    public LoggingConnectionWrapper(Connection conn) {
        _conn = conn;
    }

    public LoggingConnectionWrapper(Connection conn, Log log) {
        _conn = conn;
        _log = log;
    }

    public void clearWarnings() throws SQLException {
        _conn.clearWarnings();
    }

    public void close() throws SQLException {
        if (shouldPrint()) print("close");
        _conn.close();
    }

    public void commit() throws SQLException {
        if (shouldPrint()) print("commit");
        _conn.commit();
    }

    public Statement createStatement() throws SQLException {
        return _conn.createStatement();
    }

    public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
        return _conn.createStatement(resultSetType, resultSetConcurrency);
    }

    public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        return _conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
    }

    public boolean getAutoCommit() throws SQLException {
        return _conn.getAutoCommit();
    }

    public String getCatalog() throws SQLException {
        return _conn.getCatalog();
    }

    public int getHoldability() throws SQLException {
        return _conn.getHoldability();
    }

    public DatabaseMetaData getMetaData() throws SQLException {
        return _conn.getMetaData();
    }

    public int getTransactionIsolation() throws SQLException {
        return _conn.getTransactionIsolation();
    }

    public Map<String, Class<?>> getTypeMap() throws SQLException {
        return _conn.getTypeMap();
    }

    public SQLWarning getWarnings() throws SQLException {
        return _conn.getWarnings();
    }

    public boolean isClosed() throws SQLException {
        return _conn.isClosed();
    }

    public boolean isReadOnly() throws SQLException {
        return _conn.isReadOnly();
    }

    public String nativeSQL(String sql) throws SQLException {
        return _conn.nativeSQL(sql);
    }

    public CallableStatement prepareCall(String sql) throws SQLException {
        if (shouldPrint()) print("prepareCall: " + sql);
        return new LoggingStatementWrapper(_conn.prepareCall(sql), _log);
    }

    public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
        if (shouldPrint()) print("prepareCall: " + sql);
        return new LoggingStatementWrapper(_conn.prepareCall(sql, resultSetType, resultSetConcurrency), _log);
    }

    public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        if (shouldPrint()) print("prepareCall: " + sql);
        return new LoggingStatementWrapper(_conn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability), _log);
    }

    public PreparedStatement prepareStatement(String sql) throws SQLException {
        if (shouldPrint()) print("prepareStmt: " + sql);
        if (sql.indexOf("ODE_SCOPE") > 0) {
            for (StackTraceElement traceElement : Thread.currentThread().getStackTrace()) {
                print(traceElement.toString());
            }
        }
        return new LoggingStatementWrapper(_conn.prepareStatement(sql), _log);
    }

    public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
        if (shouldPrint()) print("prepareStmt: " + sql);
        return new LoggingStatementWrapper(_conn.prepareStatement(sql, autoGeneratedKeys), _log);
    }

    public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
        if (shouldPrint()) print("prepareStmt: " + sql);
        return new LoggingStatementWrapper(_conn.prepareStatement(sql, columnIndexes), _log);
    }

    public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
        if (shouldPrint()) print("prepareStmt: " + sql);
        return new LoggingStatementWrapper(_conn.prepareStatement(sql, columnNames), _log);
    }

    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
        if (shouldPrint()) print("prepareStmt: " + sql);
        return new LoggingStatementWrapper(_conn.prepareStatement(sql, resultSetType, resultSetConcurrency), _log);
    }

    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
        if (shouldPrint()) print("prepareStmt: " + sql);
        return new LoggingStatementWrapper(_conn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability), _log);
    }

    public void releaseSavepoint(Savepoint savepoint) throws SQLException {
        _conn.releaseSavepoint(savepoint);
    }

    public void rollback() throws SQLException {
        if (shouldPrint()) print("rollback");
        _conn.rollback();
    }

    public void rollback(Savepoint savepoint) throws SQLException {
        if (shouldPrint()) print("rollback");
        _conn.rollback(savepoint);
    }

    public void setAutoCommit(boolean autoCommit) throws SQLException {
        _conn.setAutoCommit(autoCommit);
    }

    public void setCatalog(String catalog) throws SQLException {
        _conn.setCatalog(catalog);
    }

    public void setHoldability(int holdability) throws SQLException {
        _conn.setHoldability(holdability);
    }

    public void setReadOnly(boolean readOnly) throws SQLException {
        _conn.setReadOnly(readOnly);
    }

    public Savepoint setSavepoint() throws SQLException {
        return _conn.setSavepoint();
    }

    public Savepoint setSavepoint(String name) throws SQLException {
        return _conn.setSavepoint(name);
    }

    public void setTransactionIsolation(int level) throws SQLException {
        if (shouldPrint()) print("Setting isolation level to " + level);
        _conn.setTransactionIsolation(level);
    }

    public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
        _conn.setTypeMap(map);
    }

    private boolean shouldPrint() {
        if (_log != null)
            return _log.isDebugEnabled();
        else return true;
    }

    private void print(String str) {
        if (_log != null)
            _log.debug(str);
        else System.out.println(str);
    }
}
