ips1ap101.lib.core.db.util.AgenteSql.java Source code

Java tutorial

Introduction

Here is the source code for ips1ap101.lib.core.db.util.AgenteSql.java

Source

/*
 * Este programa es software libre; usted puede redistribuirlo y/o modificarlo bajo los trminos
 * de la licencia "GNU General Public License" publicada por la Fundacin "Free Software Foundation".
 * Este programa se distribuye con la esperanza de que pueda ser til, pero SIN NINGUNA GARANTIA;
 * vea la licencia "GNU General Public License" para obtener mas informacin.
 */
package ips1ap101.lib.core.db.util;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import ips1ap101.lib.base.BaseBundle;
import ips1ap101.lib.base.util.BitUtils;
import ips1ap101.lib.base.util.StrUtils;
import ips1ap101.lib.base.util.ThrowableUtils;
import ips1ap101.lib.core.app.Bitacora;
import ips1ap101.lib.core.app.TLC;
import ips1ap101.lib.core.enumeration.EnumTipoDatoSQL;
import ips1ap101.lib.core.enumeration.EnumTipoResultadoSQL;
import org.apache.commons.lang.StringUtils;

/**
 * @author Jorge Campins
 */
public class AgenteSql {

    //  private static final String DEFAULT_NUMBER_KW = "DEFAULT";
    //
    private boolean establishedConnection = false;

    private Connection connection = null;

    private CallableStatement callableStatement = null;

    private PreparedStatement preparedStatement = null;

    private void stamp() {
        Bitacora.stamp(this);
    }

    public AgenteSql() {
        stamp();
        connect(TLC.getConnection());
    }

    private void connect(Connection connection) {
        if (connection == null) {
            this.establishedConnection = false;
            this.connection = null;
        } else {
            this.establishedConnection = true;
            this.connection = connection;
        }
    }

    /**
     * Getter para propiedad establishedConnection.
     *
     * @return Valor de la propiedad establishedConnection.
     */
    public boolean connected() {
        return establishedConnection;
    }

    /**
     * Setter para propiedad autoCommit.
     *
     * @param autoCommit Nuevo valor de la propiedad autoCommit.
     */
    public boolean setAutoCommit(boolean autoCommit) {
        Bitacora.trace(getClass(), "setAutoCommit", String.valueOf(autoCommit));
        return DB.setAutoCommit(connection, autoCommit);
    }

    public boolean runScript(String script) throws SQLException {
        Bitacora.trace(getClass(), "runScript");
        String BOS = "/* EXEC SQL */";
        String EOS = "/* END EXEC */";
        String EOL = "\\r\\n"; /* "[\\t\\n\\x0B\\f\\r]" */
        String DLM = "$$";
        String regex1 = BOS.replace("*", "[*]") + EOL;
        String regex2 = EOL + EOS.replace("*", "[*]");
        String regex3 = DLM.replace("$", "[$]");
        boolean ok = true;
        if (StringUtils.isNotBlank(script)) {
            String[] token1 = script.split(regex1);
            if (token1 != null && token1.length > 0) {
                for (int i = 0; ok && i < token1.length; i++) {
                    if (StringUtils.isNotBlank(token1[i])) {
                        int j = token1[i].indexOf(EOS);
                        if (j > 0) {
                            String[] token2 = token1[i].split(regex2);
                            if (token2 != null && token2.length > 0 && StringUtils.isNotBlank(token2[0])) {
                                String sql = token2[0];
                                int k = sql.indexOf(DLM);
                                if (k > 0) {
                                    sql = getQuotedStatement(sql, regex3);
                                }
                                Object obj = execute(sql);
                                ok = obj == null ? false : true;
                                if (ok && obj instanceof Integer) {
                                    Bitacora.trace("***");
                                    Bitacora.trace("*** comando [" + i + "] = " + (Integer) obj);
                                    Bitacora.trace("***");
                                }
                                Bitacora.trace(StrUtils.getString(500, '-'));
                            }
                        }
                    }
                }
            }
        }
        return ok;
    }

    private String getQuotedStatement(String sql, String regex) {
        if (StringUtils.isBlank(sql) || StringUtils.isBlank(regex)) {
            return null;
        }
        String qstmt = sql;
        String[] token = sql.split(regex);
        if (token != null && token.length > 0) {
            qstmt = "";
            for (int i = 0; i < token.length; i++) {
                if (i % 2 == 0) {
                    qstmt += token[i];
                } else {
                    qstmt += "'" + token[i].replace("'", "''") + "'";
                }
            }
        }
        return qstmt;
    }

    private Object execute(String sql) throws SQLException {
        return execute(sql, null);
    }

    private Object execute(String sql, Object[] args) throws SQLException {
        return execute(sql, args, null, null);
    }

    private Object execute(String sql, Object[] args, EnumTipoResultadoSQL resultType, EnumTipoDatoSQL dataType)
            throws SQLException {
        Bitacora.trace(getClass(), "execute", sql, args == null ? 0 : args.length, resultType, dataType);
        try {
            InterpreteSql interpreteSql = TLC.getInterpreteSql();
            if (interpreteSql instanceof InterpreteSqlOracle) {
                DB.close(callableStatement);
                callableStatement = DB.prepareCall(connection, sql, args, resultType, dataType);
                if (callableStatement.execute()) {
                    return callableStatement.getResultSet();
                } else if (EnumTipoResultadoSQL.SIMPLE.equals(resultType)) {
                    int n = args == null ? 0 : args.length;
                    return callableStatement.getObject(n + 1);
                } else {
                    return callableStatement.getUpdateCount();
                }
            } else {
                DB.close(preparedStatement);
                preparedStatement = DB.prepareStatement(connection, sql, args);
                if (preparedStatement.execute()) {
                    return preparedStatement.getResultSet();
                } else {
                    return preparedStatement.getUpdateCount();
                }
            }
        } catch (SQLException ex) {
            Bitacora.logFatal(ThrowableUtils.getString(ex));
            DB.rollback(connection);
            throw ex;
        }
    }

    public boolean isStoredProcedure(String sql) throws SQLException {
        Bitacora.trace(getClass(), "isStoredProcedure", sql);
        Object resultado;
        ResultSet resultSet;
        Object object;
        boolean is = false;
        if (sql != null) {
            String procedureName = BaseBundle.getName(sql);
            Object[] args = new Object[] { procedureName };
            resultado = executeProcedure(DB.CHECK_PROCEDURE, args);
            if (resultado instanceof ResultSet) {
                resultSet = (ResultSet) resultado;
                if (resultSet.next()) {
                    object = resultSet.getObject(1);
                    is = BitUtils.valueOf(object);
                }
            } else if (resultado instanceof Number) {
                is = BitUtils.valueOf(resultado);
            }
            Bitacora.trace(procedureName + " " + is);
        }
        return is;
    }

    public Object executeProcedure(String sql) throws SQLException {
        return executeProcedure(sql, null);
    }

    public Object executeProcedure(String sql, Object[] args) throws SQLException {
        String string;
        string = BaseBundle.getType(sql);
        EnumTipoResultadoSQL resultType;
        if (StringUtils.isBlank(string)) {
            resultType = EnumTipoResultadoSQL.SIMPLE;
        } else {
            try {
                string = string.toUpperCase();
                resultType = EnumTipoResultadoSQL.valueOf(string);
            } catch (Exception ex) {
                resultType = EnumTipoResultadoSQL.SIMPLE;
            }
        }
        string = BaseBundle.getDataType(sql);
        EnumTipoDatoSQL dataType;
        if (StringUtils.isBlank(string)) {
            dataType = EnumTipoDatoSQL.BIGINT;
        } else {
            try {
                string = string.toUpperCase();
                dataType = EnumTipoDatoSQL.valueOf(string);
            } catch (Exception ex) {
                dataType = EnumTipoDatoSQL.BIGINT;
            }
        }
        return executeProcedure(sql, args, resultType, dataType);
    }

    public Object executeProcedure(String sql, Object[] args, EnumTipoResultadoSQL resultType,
            EnumTipoDatoSQL dataType) throws SQLException {
        Bitacora.trace(getClass(), "executeProcedure", sql, "resultType=" + resultType);
        String procedureName = BaseBundle.getName(sql);
        int argumentos = args == null ? 0 : args.length;
        String comando = TLC.getInterpreteSql().getComandoExecute(procedureName, argumentos, resultType);
        return execute(comando, args, resultType, dataType);
    }

    public ResultSet executeQuery(String sql) throws SQLException {
        return executeQuery(sql, 0, null);
    }

    public ResultSet executeQuery(String sql, int limite) throws SQLException {
        return executeQuery(sql, limite, null);
    }

    public ResultSet executeQuery(String sql, Object[] args) throws SQLException {
        return executeQuery(sql, 0, args);
    }

    public ResultSet executeQuery(String sql, int limite, Object[] args) throws SQLException {
        Bitacora.trace(getClass(), "executeQuery", sql, "limite=" + limite,
                "args=" + (args == null ? 0 : args.length));
        try {
            String comando = TLC.getInterpreteSql().getComandoSelect(sql, limite);
            DB.close(preparedStatement);
            preparedStatement = DB.prepareStatement(connection, comando, args);
            return preparedStatement.executeQuery();
        } catch (SQLException ex) {
            Bitacora.logFatal(ThrowableUtils.getString(ex));
            DB.rollback(connection);
            throw ex;
        }
    }

    public boolean commit() {
        Bitacora.trace(getClass(), "commit");
        return DB.commit(connection);
    }

    public boolean rollback() {
        Bitacora.trace(getClass(), "rollback");
        return DB.rollback(connection);
    }

    public void close() {
        Bitacora.trace(getClass(), "close");
        DB.close(callableStatement);
        DB.close(preparedStatement);
    }

}