~ubuntu-branches/ubuntu/maverick/libpgjava/maverick

« back to all changes in this revision

Viewing changes to org/postgresql/core/v2/SimpleParameterList.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2006-04-25 00:07:07 UTC
  • mfrom: (1.3.1 upstream) (3.1.1 dapper)
  • Revision ID: james.westby@ubuntu.com-20060425000707-6lr2s0awuz4z48hm
* Drop support for the old jdbc2 driver (can be reverted if wanted)
  (closes: #358345).
* New upstream (thanks to Wolfgang Baer).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
*
 
3
* Copyright (c) 2004-2005, PostgreSQL Global Development Group
 
4
* Copyright (c) 2004, Open Cloud Limited.
 
5
*
 
6
* IDENTIFICATION
 
7
*   $PostgreSQL: pgjdbc/org/postgresql/core/v2/SimpleParameterList.java,v 1.7 2005/07/04 18:50:28 davec Exp $
 
8
*
 
9
*-------------------------------------------------------------------------
 
10
*/
 
11
package org.postgresql.core.v2;
 
12
 
 
13
import org.postgresql.core.*;
 
14
 
 
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;
 
24
 
 
25
/**
 
26
 * Parameter list for query parameters in the V2 protocol.
 
27
 *
 
28
 * @author Oliver Jowett (oliver@opencloud.com)
 
29
 */
 
30
class SimpleParameterList implements ParameterList {
 
31
    SimpleParameterList(int paramCount) {
 
32
        this.paramValues = new Object[paramCount];
 
33
    }
 
34
    public void registerOutParameter(int index, int sqlType ){};
 
35
    public void registerOutParameter(int index, int sqlType, int precision ){};
 
36
    
 
37
    public int getInParameterCount() {
 
38
        return paramValues.length;
 
39
    }
 
40
    public int getParameterCount()
 
41
    {
 
42
        return paramValues.length;
 
43
    }
 
44
    public int[] getTypeOIDs() {
 
45
        return null;
 
46
    }
 
47
 
 
48
    public void setIntParameter(int index, int value) throws SQLException {
 
49
        setLiteralParameter(index, "" + value, Oid.INT4);
 
50
    }
 
51
 
 
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 );
 
55
 
 
56
        paramValues[index - 1] = value;
 
57
    }
 
58
 
 
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.
 
61
        sbuf.append('\'');
 
62
        for (int i = 0; i < value.length(); ++i)
 
63
        {
 
64
            char ch = value.charAt(i);
 
65
            if (ch == '\0')
 
66
                throw new PSQLException(GT.tr("Zero bytes may not occur in string parameters."), PSQLState.INVALID_PARAMETER_VALUE);
 
67
            if (ch == '\\' || ch == '\'')
 
68
                sbuf.append('\\');
 
69
            sbuf.append(ch);
 
70
        }
 
71
        sbuf.append('\'');
 
72
 
 
73
        setLiteralParameter(index, sbuf.toString(), oid);
 
74
    }
 
75
 
 
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 );
 
79
 
 
80
        paramValues[index - 1] = new StreamWrapper(data, offset, length);
 
81
    }
 
82
 
 
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 );
 
86
 
 
87
        paramValues[index - 1] = new StreamWrapper(stream, length);
 
88
    }
 
89
 
 
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 );
 
93
 
 
94
        paramValues[index - 1] = NULL_OBJECT;
 
95
    }
 
96
 
 
97
    public String toString(int index) {
 
98
        if (index < 1 || index > paramValues.length)
 
99
            throw new IllegalArgumentException("Parameter index " + index + " out of range");
 
100
 
 
101
        if (paramValues[index - 1] == null)
 
102
            return "?";
 
103
        else if (paramValues[index -1] == NULL_OBJECT)
 
104
            return "NULL";
 
105
        else
 
106
            return paramValues[index -1].toString();
 
107
    }
 
108
 
 
109
    /**
 
110
     * Send a streamable bytea encoded as a text representation with an arbitary encoding.
 
111
     */
 
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
 
115
        // unscathed..
 
116
 
 
117
        InputStream stream = param.getStream();
 
118
        char[] buffer = new char[] { '\\', '\\', 0, 0, 0 };
 
119
 
 
120
        encodingWriter.write('\'');
 
121
        for (int remaining = param.getLength(); remaining > 0; --remaining)
 
122
        {
 
123
            int nextByte = stream.read();
 
124
 
 
125
            buffer[2] = (char)( '0' + ((nextByte >> 6) & 3));
 
126
            buffer[3] = (char)( '0' + ((nextByte >> 3) & 7));
 
127
            buffer[4] = (char)( '0' + (nextByte & 7));
 
128
 
 
129
            encodingWriter.write(buffer, 0, 5);
 
130
        }
 
131
 
 
132
        encodingWriter.write('\'');
 
133
    }
 
134
 
 
135
 
 
136
    void writeV2Value(int index, Writer encodingWriter) throws IOException {
 
137
        if (paramValues[index - 1] instanceof StreamWrapper)
 
138
        {
 
139
            streamBytea((StreamWrapper)paramValues[index - 1], encodingWriter);
 
140
        }
 
141
        else
 
142
        {
 
143
            encodingWriter.write((String)paramValues[index - 1]);
 
144
        }
 
145
    }
 
146
 
 
147
    void checkAllParametersSet() throws SQLException {
 
148
        for (int i = 0; i < paramValues.length; i++)
 
149
        {
 
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);
 
152
        }
 
153
    }
 
154
 
 
155
    public ParameterList copy() {
 
156
        SimpleParameterList newCopy = new SimpleParameterList(paramValues.length);
 
157
        System.arraycopy(paramValues, 0, newCopy.paramValues, 0, paramValues.length);
 
158
        return newCopy;
 
159
    }
 
160
 
 
161
    public void clear() {
 
162
        Arrays.fill(paramValues, null);
 
163
    }
 
164
 
 
165
    private final Object[] paramValues;
 
166
 
 
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.
 
170
     */
 
171
    private final static String NULL_OBJECT = new String("NULL");
 
172
}
 
173