Java SQL Type fillLogParams(String sql, Map logParams)

Here you can find the source of fillLogParams(String sql, Map logParams)

Description

Returns sql statement used in this prepared statement together with the parameters.

License

Open Source License

Parameter

Parameter Description
sql base sql statement
logParams parameters to print out

Return

returns printable statement

Declaration

public static String fillLogParams(String sql, Map<Object, Object> logParams) 

Method Source Code

//package com.java2s;
/**//from   w w w.ja v  a2  s  .  c  o m
 * Copyright 2009 Wallace Wadge
 *
 * This file is part of BoneCP.
 *
 * BoneCP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * BoneCP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with BoneCP.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Ref;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map;

public class Main {
    /** Returns sql statement used in this prepared statement together with the parameters.
     * @param sql base sql statement
     * @param logParams parameters to print out
     * @return returns printable statement 
     */
    public static String fillLogParams(String sql, Map<Object, Object> logParams) {
        StringBuilder result = new StringBuilder();
        Iterator<Object> it = logParams.values().iterator();
        boolean inQuote = false;
        boolean inQuote2 = false;
        char[] sqlChar = sql.toCharArray();

        for (int i = 0; i < sqlChar.length; i++) {
            if (sqlChar[i] == '\'') {
                inQuote = !inQuote;
            }
            if (sqlChar[i] == '"') {
                inQuote2 = !inQuote2;
            }

            if (sqlChar[i] == '?' && !(inQuote || inQuote2)) {
                if (it.hasNext()) {
                    result.append(prettyPrint(it.next()));
                } else {
                    result.append("?");
                }
            } else {
                result.append(sqlChar[i]);
            }
        }

        return result.toString();
    }

    /** Helper method
     * @param obj item to print
     * @return String for pretty printing.
     */
    protected static String prettyPrint(Object obj) {
        StringBuilder sb = new StringBuilder();
        if (obj == null) {
            sb.append("NULL");
        } else if (obj instanceof Blob) {
            sb.append(formatLogParam((Blob) obj));
        } else if (obj instanceof Clob) {
            sb.append(formatLogParam((Clob) obj));
        } else if (obj instanceof Ref) {
            sb.append(formatLogParam((Ref) obj));
        } else if (obj instanceof Array) {
            sb.append(formatLogParam((Array) obj));
        } else if (obj instanceof String) {
            sb.append("'" + obj.toString() + "'");
        } else {
            sb.append(obj.toString());
        }
        return sb.toString();
    }

    /** Formatter for debugging purposes only. 
     * @param obj to print
     * @return String
     */
    private static String formatLogParam(Blob obj) {
        String result = "";
        try {
            result = "(blob of length " + obj.length() + ")";
        } catch (SQLException e) {
            result = "(blob of unknown length)";
        }
        return result;
    }

    /** Formatter for debugging purposes only. 
     * @param obj to print
     * @return String
     */
    private static String formatLogParam(Clob obj) {
        String result = "";
        try {
            result = "(cblob of length " + obj.length() + ")";
        } catch (SQLException e) {
            result = "(cblob of unknown length)";
        }
        return result;
    }

    /** Formatter for debugging purposes only. 
     * @param obj to print
     * @return String
     */
    private static String formatLogParam(Array obj) {
        String result = "";
        try {
            result = "(array of type" + obj.getBaseTypeName().length() + ")";
        } catch (SQLException e) {
            result = "(array of unknown type)";
        }
        return result;
    }

    /** Formatter for debugging purposes only. 
     * @param obj to print
     * @return String
     */
    private static String formatLogParam(Ref obj) {
        String result = "";
        try {
            result = "(ref of type" + obj.getBaseTypeName().length() + ")";
        } catch (SQLException e) {
            result = "(ref of unknown type)";
        }
        return result;
    }
}

Related

  1. deleteData(Connection conn)
  2. deleteFromAllTables(Connection con)
  3. deserializeSQLTypesToJava(String sqltypename, String value)
  4. dumpTypes(Connection pConnection)
  5. extractConstant(String typeName)
  6. findMethod(Class returnType, Class parameterType)
  7. getAntipodeForFunction(String base, int destType)
  8. getArgTypes()
  9. getAsciiStream(Object value, int columnType)