All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kawanfw.sql.json.no_obfuscation.CallableStatementHolder Maven / Gradle / Ivy

/*
 * This file is part of AceQL. 
 * AceQL: Remote JDBC access over HTTP.                                     
 * Copyright (C) 2015,  KawanSoft SAS
 * (http://www.kawansoft.com). All rights reserved.                                
 *                                                                               
 * AceQL 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 2.1 of the License, or (at your option) any later version.            
 *                                                                               
 * AceQL 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             
 * Lesser General Public License for more details.                               
 *                                                                               
 * You should have received a copy of the GNU Lesser General Public              
 * License along with this library; if not, write to the Free Software           
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301  USA
 *
 * Any modifications to this file must keep this entire header
 * intact.
 */
package org.kawanfw.sql.json.no_obfuscation;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Serializable;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;
import org.kawanfw.commons.util.DefaultParms;
import org.kawanfw.commons.util.HtmlConverter;
import org.kawanfw.commons.util.Tag;
import org.kawanfw.sql.jdbc.util.TransportAsciiStream;
import org.kawanfw.sql.json.StatementHolder;
import org.kawanfw.sql.jsontypes.JavaTypes;
import org.kawanfw.sql.jsontypes.StaParms;
import org.kawanfw.sql.transport.UrlTransporter;
import org.kawanfw.sql.transport.no_obfsucation.ArrayHttp;
import org.kawanfw.sql.transport.no_obfsucation.RowIdHttp;
import org.kawanfw.sql.transport.no_obfsucation.RowIdTransporter;

public class CallableStatementHolder implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 8907260501485811166L;

    /** The sqlOrder order */
    private String sql = null;

    /**
     * Properties are stored in a int array. Name is short for fast JSON
     * Transport
     */
    private int[] stP = new int[StaParms.NUM_PARMS];

    /** The parameter types */
    private Map parmsT = new TreeMap();

    /** The parameter values */
    private Map parmsV = new TreeMap();

    /** The parameter names */
    private Map parmsN = new TreeMap();

    /** List of indexes of out parameter */
    private List outP = new ArrayList();

    /** Says if the statement parameters are encrypted */
    private boolean paramatersEncrypted = false;

    /** Says if we want to html Encode the Clob when using setCharacterStream */
    private boolean htmlEncodingOn = DefaultParms.DEFAULT_HTML_ENCODING_ON;

    /**
     * No args constructor for JSon
     */
    public CallableStatementHolder() {

    }

    /**
     * Constructor to use for autogenerated keys
     * 
     * @param sqlOrder
     *            the sql order
     * @param resultSetType
     *            the result set type
     * @param resultSetConcurrency
     *            the result set concurrency
     * @param resultSetHoldability
     *            the result set holdability
     */
    public CallableStatementHolder(String sqlOrder, int resultSetType,
	    int resultSetConcurrency, int resultSetHoldability)
	    throws SQLException {
	if (sqlOrder == null) {
	    throw new IllegalArgumentException("sqlOrder is null!");
	}

	sqlOrder = sqlOrder.trim();

	sqlOrder = HtmlConverter.toHtml(sqlOrder);

	this.sql = sqlOrder;

	stP[StaParms.RESULT_SET_TYPE] = resultSetType;
	stP[StaParms.RESULT_SET_CONCURRENCY] = resultSetConcurrency;
	stP[StaParms.RESULT_SET_HOLDABILITY] = resultSetHoldability;
	stP[StaParms.IS_ESCAPE_PROCESSING] = -1;
    }

    // /**
    // * Constructor to use to transmit only parameters (for
    // * PreparedStatement.addBatch())
    // *
    // * @param parmsT
    // * The parameter types
    // * @param parmsV
    // * The parameter values (that are raw and will be internally
    // * converted to html)
    // * @param isHtmlEncondingOn
    // * the html encoding for Clobs
    // */
    // public CallableStatementHolder(Map parmsT,
    // Map parmsV, boolean isHtmlEncondingOn) {
    // MapCopier mapCopier = new MapCopier();
    // this.parmsT = mapCopier.copy(parmsT);
    // System.out.println("here");
    // MapCopier mapCopier2 = new MapCopier();
    // this.parmsV = mapCopier2.copy(parmsV);
    // // Convert the Strings to Html before storing the map
    // Set set = this.parmsV.keySet();
    // for (Integer integer : set) {
    // String s = this.parmsV.get(integer);
    // s = HtmlConverter.toHtml(s);
    // this.parmsV.put(integer, s);
    // }
    //
    // this.htmlEncodingOn = isHtmlEncondingOn;
    //
    // }

    /**
     * Clear the parameters type and values
     */
    public void clearParameters() {
	parmsT = new TreeMap();
	parmsV = new TreeMap();
	outP = new ArrayList();
    }

    /**
     * Sets if html encoding is on for Clobs.
     * 
     * @param htmlEncodingOn
     *            true if html encoding is on, else false
     */
    public void setHtmlEncodingOn(boolean htmlEncodingOn) {
	this.htmlEncodingOn = htmlEncodingOn;
    }

    /**
     * Says if html encoding is on for Clobs.
     * 
     * @return true if html encoding is on
     */
    public boolean isHtmlEncodingOn() {
	return this.htmlEncodingOn;
    }

    /**
     * Return the value of parameter at given index
     * 
     * @param parameterIndex
     *            Index of parameter to retreive
     * @return Value of parameter
     */
    public Object getParameter(int parameterIndex) {
	return parmsV.get(parameterIndex);
    }

    /**
     * Return the value of parameter of given name
     * 
     * @param parmaterName
     *            Name of the parameter
     * @return Value of parameter
     */
    public Object getParameter(String parameterName) {
	Set keySet = parmsN.keySet();
	int parameterIndex = 0;
	for (int index : keySet) {
	    if (parmsN.get(index).equals(parameterName)) {
		parameterIndex = index;
	    }
	}
	return parmsV.get(parameterIndex);
    }

    /**
     * Indicates if parameter at given index is an OUT parameter
     * 
     * @param parameterIndex
     *            Index of parameter
     * @return true if parameter is an OUT parameter
     */
    public boolean isOutParameter(int parameterIndex) {
	if (outP.contains(parameterIndex)) {
	    return true;
	}
	return false;
    }

    /**
     * @param parameterIndex
     *            the first parameter is 1, the second is 2, ...
     * @param type
     *            the out parameter data type
     */
    public void setOutParameter(Integer parameterIndex, int type)
	    throws SQLException {
	if (sql == null) {
	    throw new SQLException(Tag.PRODUCT_PRODUCT_FAIL
		    + "sql can not be null!");
	}

	int parameterNumbers = StringUtils.countMatches(sql, "?");

	if (parameterIndex <= 0 || parameterIndex > parameterNumbers) {
	    throw new SQLException("Parameter index is out of bounds: "
		    + parameterIndex + ". Number of Parameters: "
		    + parameterNumbers);
	}

	parmsT.put(parameterIndex, type);
	outP.add(parameterIndex);
    }

    /**
     * Wrapper for CallableStament.setNull
     * 
     * @param parameterIndex
     *            the first parameter is 1, the second is 2, ...
     * @param sqlType
     *            the SQL type code defined in java.sql.Types
     */
    public void setNullParameter(Integer parameterIndex, int sqlType) {
	parmsT.put(parameterIndex, JavaTypes.NULL);
	parmsV.put(parameterIndex, "" + sqlType);
    }

    /**
     * @param parameterIndex
     *            the first parameter is 1, the second is 2, ...
     * @param x
     *            the parameter value
     */
    /*
     * public void setParameter(Integer parameterIndex, Object x) throws
     * SQLException { if (sql == null) { throw new
     * SQLException(Tag.PRODUCT_PRODUCT_FAIL + "sql can not be null!"); }
     * 
     * int parameterNumbers = StringUtils.countMatches(sql, "?");
     * 
     * if (parameterIndex <= 0 || parameterIndex > parameterNumbers) {
     * 
     * throw new SQLException("Parameter index is out of bounds: " +
     * parameterIndex + ". Number of Parameters: " + parameterNumbers); }
     * 
     * int type = -1; if (x instanceof TransportAsciiStream) { type =
     * JavaTypes.ASCII_STREAM; } else if (x instanceof BigDecimal) { type =
     * JavaTypes.BIG_DECIMAL; } else if (x instanceof Boolean) { type =
     * JavaTypes.BOOLEAN; } else if (x instanceof Double) { type =
     * JavaTypes.DOUBLE; } else if (x instanceof java.sql.Date) { type =
     * JavaTypes.DATE; } else if (x instanceof InputStream) { type =
     * JavaTypes.INPUT_STREAM; } else if (x instanceof Integer) { type =
     * JavaTypes.INT; } else if (x instanceof Float) { type = JavaTypes.FLOAT; }
     * else if (x instanceof Long) { type = JavaTypes.LONG; } else if (x
     * instanceof Reader) { type = JavaTypes.READER; } else if (x instanceof
     * Short) { type = JavaTypes.SHORT; } else if (x instanceof String) { type =
     * JavaTypes.STRING; } else if (x instanceof Time) { type = JavaTypes.TIME;
     * } else if (x instanceof Timestamp) { type = JavaTypes.TIMESTAMP; } // For
     * null values & unknown types: use Object for transportation // This must
     * be done last (all classes are Objects!) else if (x == null || x
     * instanceof Object) { type = JavaTypes.OBJECT; } else { throw new
     * IllegalArgumentException( "Unknown/Unauthorized type for: " + x +
     * " Class: " + x.getClass()); } // 16/09/10 15:50 NDP -
     * PreparedStatementHttp: Fix bug in // parametersToString():
     * value.toString() could be null! // Todo to avoid value.toString() because
     * value maybe null String valueStr = null; if (x != null) { valueStr =
     * x.toString(); valueStr = HtmlConverter.toHtml(valueStr); }
     * System.out.println("Assigning Value : " + valueStr);
     * parmsT.put(parameterIndex, type); parmsV.put(parameterIndex, valueStr); }
     */

    /**
     * @param parameterIndex
     *            the first parameter is 1, the second is 2, ...
     * @param x
     *            the parameter value
     */
    public void setParameter(Integer parameterIndex, Object x)
	    throws SQLException {
	setParameter(parameterIndex, x, null);
    }

    /**
     * @param parameterIndex
     *            the first parameter is 1, the second is 2, ...
     * @param x
     *            the parameter value
     * @param info
     *            more information if necessary for switch(example: for
     *            setNString())
     */
    public void setParameter(Integer parameterIndex, Object x, String info)
	    throws SQLException {

	if (sql == null) {
	    throw new SQLException(Tag.PRODUCT_PRODUCT_FAIL
		    + "sql can not be null!");
	}

	int parameterNumbers = StringUtils.countMatches(sql, "?");

	if (parameterIndex <= 0 || parameterIndex > parameterNumbers) {
	    throw new SQLException("Parameter index is out of bounds: "
		    + parameterIndex + ". Number of Parameters: "
		    + parameterNumbers);
	}

	int type = -1;

	if (x instanceof TransportAsciiStream) {
	    type = JavaTypes.ASCII_STREAM;
	} else if (x instanceof BigDecimal) {
	    type = JavaTypes.BIG_DECIMAL;
	} else if (x instanceof Boolean) {
	    type = JavaTypes.BOOLEAN;
	} else if (x instanceof Double) {
	    type = JavaTypes.DOUBLE;
	} else if (x instanceof java.sql.Date) {
	    type = JavaTypes.DATE;
	} else if (x instanceof InputStream) {
	    type = JavaTypes.INPUT_STREAM;
	} else if (x instanceof Integer) {
	    type = JavaTypes.INT;
	} else if (x instanceof Float) {
	    type = JavaTypes.FLOAT;
	} else if (x instanceof Long) {
	    type = JavaTypes.LONG;
	} else if (x instanceof Reader) {
	    type = JavaTypes.READER;
	} else if (x instanceof Short) {
	    type = JavaTypes.SHORT;
	} else if (x instanceof String) {
	    if (info != null && info.equals(StatementHolder.SET_N_STRING)) {
		type = JavaTypes.NSTRING;
	    } else {
		type = JavaTypes.STRING;
	    }
	} else if (x instanceof Time) {
	    type = JavaTypes.TIME;
	} else if (x instanceof Timestamp) {
	    type = JavaTypes.TIMESTAMP;
	} else if (x instanceof URL) {
	    type = JavaTypes.URL;
	} else if (x instanceof Array) {
	    type = JavaTypes.ARRAY;
	} else if (x instanceof RowId) {
	    type = JavaTypes.ROWID;
	}
	// For null values & unknown types: use Object for transportation
	// This must be done last (all classes are Objects!)
	else if (x == null || x instanceof Object) {
	    type = JavaTypes.OBJECT;
	} else {
	    throw new IllegalArgumentException(
		    "Unknown/Unauthorized type for: " + x + " Class: "
			    + x.getClass());
	}

	// 16/09/10 15:50 NDP - PreparedStatementHttp: Fix bug in
	// parametersToString(): value.toString() could be null!
	// Todo to avoid value.toString() because value maybe null
	String valueStr = null;

	if (x instanceof URL) {
	    UrlTransporter urlTransporter = new UrlTransporter();
	    try {
		URL url = (URL) x;
		valueStr = urlTransporter.toBase64(url);
	    } catch (IOException e) {
		throw new SQLException(e);
	    }
	} else if (x instanceof Array) {
	    // We don't send it on server, we us the server one. We just use the
	    // Id for server location

	    if (!(x instanceof ArrayHttp)) {
		throw new SQLException(
			Tag.PRODUCT
				+ "Invalid Array, not created by a Connection.createArrayOf() call.");
	    }

	    valueStr = ((ArrayHttp) x).getArrayId();
	} else if (x instanceof RowId) {

	    if (!(x instanceof RowIdHttp)) {
		throw new SQLException(
			Tag.PRODUCT
				+ "Invalid RowId, not created by a ResultSet.getRowId() call.");
	    }

	    RowIdTransporter rowIdTransporter = new RowIdTransporter();
	    try {
		RowId rowId = (RowId) x;
		valueStr = rowIdTransporter.toBase64(rowId);
	    } catch (IOException e) {
		throw new SQLException(e);
	    }
	} else {
	    if (x != null) {
		valueStr = x.toString();
		valueStr = HtmlConverter.toHtml(valueStr);
	    }
	}

	// System.out.println();
	// System.out.println("parameterIndex: " + parameterIndex);
	// System.out.println("type          : " + type);
	// System.out.println("valueStr      : " + valueStr);

	parmsT.put(parameterIndex, type);
	parmsV.put(parameterIndex, valueStr);

    }

    public void setOutParameterValue(Integer parameterIndex, Object x)
	    throws SQLException {
	if (sql == null) {
	    throw new SQLException(Tag.PRODUCT_PRODUCT_FAIL
		    + "sql can not be null!");
	}

	int parameterNumbers = StringUtils.countMatches(sql, "?");

	if (parameterIndex <= 0 || parameterIndex > parameterNumbers) {

	    throw new SQLException("Parameter index is out of bounds: "
		    + parameterIndex + ". Number of Parameters: "
		    + parameterNumbers);
	}
	if (!outP.contains(parameterIndex)) {
	    throw new SQLException("Parameter at index " + parameterIndex
		    + " is not out parameter");
	}
	String valueStr = null;
	if (x != null) {
	    valueStr = x.toString();
	    valueStr = HtmlConverter.toHtml(valueStr);
	}
	parmsV.put(parameterIndex, valueStr);
    }

    /**
     * Returns the parameter values as string values (with string converted from
     * Html)
     * 
     * @return the parameter values as string values (with string converted from
     *         Html)
     */
    public Map getParameterStringValues() {

	// Convert the String from Html before returning the map
	Set set = this.parmsV.keySet();
	for (Integer integer : set) {
	    String s = this.parmsV.get(integer);
	    s = HtmlConverter.fromHtml(s);
	    this.parmsV.put(integer, s);
	}
	return this.parmsV;
    }

    /**
     * Returns the parameters types
     * 
     * @return the parameters types
     */
    public Map getParameterTypes() {
	return this.parmsT;
    }

    /**
     * @return true if the statement is an executeUpdate
     */
    public boolean isExecuteUpdate() {
	return stP[StaParms.IS_EXECUTE_UPDATE] == 1 ? true : false;
    }

    /**
     * @param isExecuteUpdate
     *            if true, the statement is an executeUpdate
     */
    public void setExecuteUpdate(boolean isExecuteUpdate) {
	stP[StaParms.IS_EXECUTE_UPDATE] = isExecuteUpdate ? 1 : 0;
    }

    /**
     * @return the maxRows
     */
    public int getMaxRows() {
	return stP[StaParms.MAX_ROWS];
    }

    /**
     * @param maxRows
     *            the maxRows to set
     */
    public void setMaxRows(int maxRows) {
	stP[StaParms.MAX_ROWS] = maxRows;
    }

    /**
     * @return the fetchSize
     */
    public int getFetchSize() {
	return stP[StaParms.FETCH_SIZE];
    }

    /**
     * @param fetchSize
     *            the fetchSize to set
     */
    public void setFetchSize(int fetchSize) {
	stP[StaParms.FETCH_SIZE] = fetchSize;
    }

    /**
     * @return the queryTimeout
     */
    public int getQueryTimeout() {
	return stP[StaParms.QUERY_TIMEOUT];
    }

    /**
     * @param queryTimeout
     *            the queryTimeout to set
     */
    public void setQueryTimeout(int queryTimeout) {
	stP[StaParms.QUERY_TIMEOUT] = queryTimeout;
    }

    /**
     * @return the escapeProcessing value
     */
    public boolean isEscapeProcessing() {
	return stP[StaParms.IS_ESCAPE_PROCESSING] == 1 ? true : false;
    }

    /**
     * Says if escape processing is set (-1 if not set)
     * 
     * @return true if escape processing is set
     */
    public boolean isEscapeProcessingSet() {
	if (stP[StaParms.IS_ESCAPE_PROCESSING] == -1) {
	    return false;
	} else {
	    return true;
	}
    }

    /**
     * @param escapeProcessingInt
     *            the escapeProcessing to set
     */
    public void setEscapeProcessing(int escapeProcessingInt) {
	stP[StaParms.IS_ESCAPE_PROCESSING] = escapeProcessingInt;

	// System.out.println("stP[StaParms.IS_ESCAPE_PROCESSING]: " +
	// stP[StaParms.IS_ESCAPE_PROCESSING]);
    }

    /**
     * Sets if parameters are encrypted
     * 
     * @param paramatersEncrypted
     *            true if parameters are encrypted else false
     */
    public void setParamatersEncrypted(boolean paramatersEncrypted) {
	this.paramatersEncrypted = paramatersEncrypted;
    }

    /**
     * Returns true if the parameters are encrypted
     * 
     * @return true if the parameters are encrypted
     */
    public boolean isParamatersEncrypted() {
	return this.paramatersEncrypted;
    }

    /**
     * Returns the SQL order (converted from Html)
     * 
     * @return the sqlOrder (converted from Html)
     */
    public String getSqlOrder() {
	return HtmlConverter.fromHtml(this.sql);
    }

    /**
     * Reset the SQL Order (and convert it to Html)
     * 
     * @param sql
     *            the sql to set (and convert to Html)
     */
    public void setSqlOrder(String sql) {
	this.sql = HtmlConverter.toHtml(sql);
    }

    /**
     * @return the resultSetType
     */
    public int getResultSetType() {
	return stP[StaParms.RESULT_SET_TYPE];
    }

    /**
     * @return the resultSetConcurrency
     */
    public int getResultSetConcurrency() {
	return stP[StaParms.RESULT_SET_CONCURRENCY];
    }

    /**
     * @return the resultSetHoldability
     */
    public int getResultSetHoldability() {
	return stP[StaParms.RESULT_SET_HOLDABILITY];
    }

    //
    // Package protected getters for CallableStatementHolderTransport
    //

    /**
     * @return the sql
     */
    String getSql() {
	return this.sql;
    }

    /**
     * @return the stP
     */
    int[] getStP() {
	return this.stP;
    }

    /**
     * @return the parmsT
     */
    Map getParmsT() {
	return this.parmsT;
    }

    /**
     * @return the parmsV
     */
    Map getParmsV() {
	return this.parmsV;
    }

    /**
     * @param sql
     *            the sql to set
     */
    void setSql(String sql) {
	this.sql = sql;
    }

    /**
     * @param stP
     *            the stP to set
     */
    void setStP(int[] stP) {
	this.stP = stP;
    }

    /**
     * @param parmsT
     *            the parmsT to set
     */
    void setParmsT(Map parmsT) {
	this.parmsT = parmsT;
    }

    /**
     * @param parmsV
     *            the parmsV to set
     */
    void setParmsV(Map parmsV) {
	this.parmsV = parmsV;
    }

    /**
     * 
     * @return the outP
     */
    public List getOutP() {
	return this.outP;
    }

    /**
     * @param outP
     *            the outP to set
     */
    void setOutP(List outP) {
	this.outP = outP;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy