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

org.postgresql.core.v3.SimpleParameterList Maven / Gradle / Ivy

The newest version!
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2011, PostgreSQL Global Development Group
* Copyright (c) 2004, Open Cloud Limited.
*
* IDENTIFICATION
*   $PostgreSQL: pgjdbc/org/postgresql/core/v3/SimpleParameterList.java,v 1.20 2011/08/02 13:40:12 davecramer Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.core.v3;

import java.io.InputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;

import org.postgresql.core.*;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;
import org.postgresql.util.StreamWrapper;
import org.postgresql.util.GT;

/**
 * Parameter list for a single-statement V3 query.
 *
 * @author Oliver Jowett ([email protected])
 */
class SimpleParameterList implements V3ParameterList {
    
    private final static int IN = 1;
    private final static int OUT = 2;
    private final static int INOUT = IN|OUT;

    SimpleParameterList(int paramCount, ProtocolConnectionImpl protoConnection) {
        this.paramValues = new Object[paramCount];
        this.paramTypes = new int[paramCount];
        this.encoded = new byte[paramCount][];
        this.direction = new int[paramCount];
        this.protoConnection = protoConnection;
    }        
    
    public void registerOutParameter( int index, int sqlType ) throws SQLException
    {
        if (index < 1 || index > paramValues.length)
            throw new PSQLException(GT.tr("The column index is out of range: {0}, number of columns: {1}.", new Object[]{new Integer(index), new Integer(paramValues.length)}), PSQLState.INVALID_PARAMETER_VALUE );

        direction[index-1] |= OUT;
    }

    private void bind(int index, Object value, int oid) throws SQLException {
        if (index < 1 || index > paramValues.length)
            throw new PSQLException(GT.tr("The column index is out of range: {0}, number of columns: {1}.", new Object[]{new Integer(index), new Integer(paramValues.length)}), PSQLState.INVALID_PARAMETER_VALUE );

        --index;

        encoded[index] = null;
        paramValues[index] = value ;
        direction[index] |= IN;
        
        // If we are setting something to an UNSPECIFIED NULL, don't overwrite
        // our existing type for it.  We don't need the correct type info to
        // send this value, and we don't want to overwrite and require a
        // reparse.
        if (oid == Oid.UNSPECIFIED && paramTypes[index] != Oid.UNSPECIFIED && value == NULL_OBJECT)
            return;

        paramTypes[index] = oid;
    }

    public int getParameterCount()
    {
        return paramValues.length;
    }
    public int getOutParameterCount()
    {
        int count=0;
        for( int i=paramTypes.length; --i >= 0;)
        {
            if ((direction[i] & OUT) == OUT )
            {
                count++;
            }
        }
        // Every function has at least one output.
        if (count == 0)
            count = 1;
        return count;
        
    }
    public int getInParameterCount() 
    {
        int count=0;
        for( int i=0; i< paramTypes.length;i++)
        {
            if (direction[i] != OUT )
            {
                count++;
            }
        }
        return count;
    }

    public void setIntParameter(int index, int value) throws SQLException {
        byte[] data = new byte[4];
        data[3] = (byte)value;
        data[2] = (byte)(value >> 8);
        data[1] = (byte)(value >> 16);
        data[0] = (byte)(value >> 24);
        bind(index, data, Oid.INT4);
    }

    public void setLiteralParameter(int index, String value, int oid) throws SQLException {
        bind(index, value, oid);
    }

    public void setStringParameter(int index, String value, int oid) throws SQLException {
        bind(index, value, oid);
    }

    public void setBytea(int index, byte[] data, int offset, int length) throws SQLException {
        bind(index, new StreamWrapper(data, offset, length), Oid.BYTEA);
    }

    public void setBytea(int index, InputStream stream, int length) throws SQLException {
        bind(index, new StreamWrapper(stream, length), Oid.BYTEA);
    }

    public void setNull(int index, int oid) throws SQLException {
        bind(index, NULL_OBJECT, oid);
    }

    public String toString(int index) {
        --index;
        if (paramValues[index] == null)
            return "?";
        else if (paramValues[index] == NULL_OBJECT)
            return "NULL";
        else {
            String param = paramValues[index].toString();
            boolean hasBackslash = param.indexOf('\\') != -1;

            // add room for quotes + potential escaping.
            StringBuffer p = new StringBuffer(3 + param.length() * 11 / 10);

            boolean standardConformingStrings = false;
            boolean supportsEStringSyntax = false;
            if (protoConnection != null) {
                standardConformingStrings = protoConnection.getStandardConformingStrings();
                supportsEStringSyntax = protoConnection.getServerVersion().compareTo("8.1") >= 0;
            }

            if (hasBackslash && !standardConformingStrings && supportsEStringSyntax)
                p.append('E');

            p.append('\'');
            try {
                p = Utils.appendEscapedLiteral(p, param, standardConformingStrings);
            } catch (SQLException sqle) {
                // This should only happen if we have an embedded null
                // and there's not much we can do if we do hit one.
                //
                // The goal of toString isn't to be sent to the server,
                // so we aren't 100% accurate (see StreamWrapper), put
                // the unescaped version of the data.
                //
                p.append(param);
            }
            p.append('\'');
            return p.toString();
        }
    }

    public void checkAllParametersSet() throws SQLException {
        for (int i = 0; i < paramTypes.length; ++i)
        {
            if (direction[i] != OUT && paramValues[i] == null)
                throw new PSQLException(GT.tr("No value specified for parameter {0}.", new Integer(i + 1)), PSQLState.INVALID_PARAMETER_VALUE);
        }
    }

    public void convertFunctionOutParameters()
    {
        for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy