1
/*-------------------------------------------------------------------------
3
* Copyright (c) 2004-2005, PostgreSQL Global Development Group
4
* Copyright (c) 2004, Open Cloud Limited.
7
* $PostgreSQL: pgjdbc/org/postgresql/core/v2/SimpleParameterList.java,v 1.7 2005/07/04 18:50:28 davec Exp $
9
*-------------------------------------------------------------------------
11
package org.postgresql.core.v2;
13
import org.postgresql.core.*;
15
import java.io.InputStream;
16
import java.io.Writer;
17
import java.io.IOException;
18
import java.sql.SQLException;
19
import java.util.Arrays;
20
import org.postgresql.util.PSQLException;
21
import org.postgresql.util.PSQLState;
22
import org.postgresql.util.StreamWrapper;
23
import org.postgresql.util.GT;
26
* Parameter list for query parameters in the V2 protocol.
28
* @author Oliver Jowett (oliver@opencloud.com)
30
class SimpleParameterList implements ParameterList {
31
SimpleParameterList(int paramCount) {
32
this.paramValues = new Object[paramCount];
34
public void registerOutParameter(int index, int sqlType ){};
35
public void registerOutParameter(int index, int sqlType, int precision ){};
37
public int getInParameterCount() {
38
return paramValues.length;
40
public int getParameterCount()
42
return paramValues.length;
44
public int[] getTypeOIDs() {
48
public void setIntParameter(int index, int value) throws SQLException {
49
setLiteralParameter(index, "" + value, Oid.INT4);
52
public void setLiteralParameter(int index, String value, int oid) throws SQLException {
53
if (index < 1 || index > paramValues.length)
54
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 );
56
paramValues[index - 1] = value;
59
public void setStringParameter(int index, String value, int oid) throws SQLException {
60
StringBuffer sbuf = new StringBuffer(2 + value.length() * 11 / 10); // Add 10% for escaping.
62
for (int i = 0; i < value.length(); ++i)
64
char ch = value.charAt(i);
66
throw new PSQLException(GT.tr("Zero bytes may not occur in string parameters."), PSQLState.INVALID_PARAMETER_VALUE);
67
if (ch == '\\' || ch == '\'')
73
setLiteralParameter(index, sbuf.toString(), oid);
76
public void setBytea(int index, byte[] data, int offset, int length) throws SQLException {
77
if (index < 1 || index > paramValues.length)
78
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 );
80
paramValues[index - 1] = new StreamWrapper(data, offset, length);
83
public void setBytea(int index, final InputStream stream, final int length) throws SQLException {
84
if (index < 1 || index > paramValues.length)
85
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 );
87
paramValues[index - 1] = new StreamWrapper(stream, length);
90
public void setNull(int index, int oid) throws SQLException {
91
if (index < 1 || index > paramValues.length)
92
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 );
94
paramValues[index - 1] = NULL_OBJECT;
97
public String toString(int index) {
98
if (index < 1 || index > paramValues.length)
99
throw new IllegalArgumentException("Parameter index " + index + " out of range");
101
if (paramValues[index - 1] == null)
103
else if (paramValues[index -1] == NULL_OBJECT)
106
return paramValues[index -1].toString();
110
* Send a streamable bytea encoded as a text representation with an arbitary encoding.
112
private static void streamBytea(StreamWrapper param, Writer encodingWriter) throws IOException {
113
// NB: we escape everything in this path, as I don't like assuming
114
// that byte values 32..127 will make it through the encoding
117
InputStream stream = param.getStream();
118
char[] buffer = new char[] { '\\', '\\', 0, 0, 0 };
120
encodingWriter.write('\'');
121
for (int remaining = param.getLength(); remaining > 0; --remaining)
123
int nextByte = stream.read();
125
buffer[2] = (char)( '0' + ((nextByte >> 6) & 3));
126
buffer[3] = (char)( '0' + ((nextByte >> 3) & 7));
127
buffer[4] = (char)( '0' + (nextByte & 7));
129
encodingWriter.write(buffer, 0, 5);
132
encodingWriter.write('\'');
136
void writeV2Value(int index, Writer encodingWriter) throws IOException {
137
if (paramValues[index - 1] instanceof StreamWrapper)
139
streamBytea((StreamWrapper)paramValues[index - 1], encodingWriter);
143
encodingWriter.write((String)paramValues[index - 1]);
147
void checkAllParametersSet() throws SQLException {
148
for (int i = 0; i < paramValues.length; i++)
150
if (paramValues[i] == null)
151
throw new PSQLException(GT.tr("No value specified for parameter {0}.", new Integer(i + 1)), PSQLState.INVALID_PARAMETER_VALUE);
155
public ParameterList copy() {
156
SimpleParameterList newCopy = new SimpleParameterList(paramValues.length);
157
System.arraycopy(paramValues, 0, newCopy.paramValues, 0, paramValues.length);
161
public void clear() {
162
Arrays.fill(paramValues, null);
165
private final Object[] paramValues;
167
/* Object representing NULL; conveniently, String streams exactly as we want it to. *
168
* nb: we explicitly say "new String" to avoid interning giving us an object that
169
* might be the same (by identity) as a String elsewhere.
171
private final static String NULL_OBJECT = new String("NULL");