File: SimpleParameterList.java

package info (click to toggle)
libpgjava 8.1-405-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,484 kB
  • ctags: 4,266
  • sloc: java: 32,071; xml: 3,017; sql: 21; makefile: 18; sh: 10
file content (282 lines) | stat: -rw-r--r-- 8,769 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2005, PostgreSQL Global Development Group
* Copyright (c) 2004, Open Cloud Limited.
*
* IDENTIFICATION
*   $PostgreSQL: pgjdbc/org/postgresql/core/v3/SimpleParameterList.java,v 1.10 2005/07/08 17:38:29 davec 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 (oliver@opencloud.com)
 */
class SimpleParameterList implements V3ParameterList {
    
    private final static int IN=1;
    private final static int OUT=2;
    private final static int INOUT=3;
    // this is here to avoid creating objects for copy
    // copy will simply discard them
    
    
    
    SimpleParameterList(int paramCount) {
        this.paramValues = new Object[paramCount];
        this.paramTypes = new int[paramCount];
        this.encoded = new byte[paramCount][];
        this.direction = new int[paramCount];
    }
        
    
    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 null, don't overwrite our existing type
        // for it.  We don't need the correct type info to send NULL and we
        // don't want to overwrite and require a reparse.
        if (oid == Oid.INVALID && paramTypes[index] != Oid.INVALID)
            return;

        paramTypes[index] = oid;
    }

    public int getParameterCount()
    {
        return paramValues.length;
    }
    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
            return paramValues[index].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);
        }
    }

    //
    // bytea helper
    //

    private static void streamBytea(PGStream pgStream, StreamWrapper wrapper) throws IOException {
        byte[] rawData = wrapper.getBytes();
        if (rawData != null)
        {
            pgStream.Send(rawData, wrapper.getOffset(), wrapper.getLength());
            return ;
        }

        pgStream.SendStream(wrapper.getStream(), wrapper.getLength());
    }

    public int[] getTypeOIDs() {
        return paramTypes;
    }

    //
    // Package-private V3 accessors
    //

    int getTypeOID(int index) {
        if (direction[index-1] == OUT)
        {
            paramTypes[index-1] = Oid.VOID;
            paramValues[index-1] = "null";
        }
        	
        return paramTypes[index-1];
    }

    boolean hasUnresolvedTypes() {
        for (int i=0; i< paramTypes.length; i++) {
            if (paramTypes[i] == Oid.INVALID)
                return true;
        }
        return false;
    }

    void setResolvedType(int index, int oid) {
        // only allow overwriting an unknown value
        if (paramTypes[index-1] == Oid.INVALID) {
            paramTypes[index-1] = oid;
        } else if (paramTypes[index-1] != oid) {
            throw new IllegalArgumentException("Can't change resolved type for param: " + index + " from " + paramTypes[index] + " to " + oid);
        }
    }

    boolean isNull(int index) {
        return (paramValues[index-1] == NULL_OBJECT);
    }

    boolean isBinary(int index) {
        // Currently, only StreamWrapper uses the binary parameter form.
        return (paramValues[index-1] instanceof StreamWrapper);
    }

    int getV3Length(int index) {
        --index;

//      Null?
        if (paramValues[index] == NULL_OBJECT)
            throw new IllegalArgumentException("can't getV3Length() on a null parameter");

        // Directly encoded?
        if (paramValues[index] instanceof byte[])
            return ((byte[])paramValues[index]).length;

        // Binary-format bytea?
        if (paramValues[index] instanceof StreamWrapper)
            return ((StreamWrapper)paramValues[index]).getLength();

        // Already encoded?
        if (encoded[index] == null)
        {
            // Encode value and compute actual length using UTF-8.
            encoded[index] = Utils.encodeUTF8(paramValues[index].toString());
        }

        return encoded[index].length;
    }

    void writeV3Value(int index, PGStream pgStream) throws IOException {
        --index;

        // Null?
        if (paramValues[index] == NULL_OBJECT)
            throw new IllegalArgumentException("can't writeV3Value() on a null parameter");

        // Directly encoded?
        if (paramValues[index] instanceof byte[])
        {
            pgStream.Send((byte[])paramValues[index]);
            return ;
        }

        // Binary-format bytea?
        if (paramValues[index] instanceof StreamWrapper)
        {
            streamBytea(pgStream, (StreamWrapper)paramValues[index]);
            return ;
        }

        // Encoded string.
        if (encoded[index] == null)
            encoded[index] = Utils.encodeUTF8((String)paramValues[index]);
        pgStream.Send(encoded[index]);
    }

    
    
    public ParameterList copy() {
        SimpleParameterList newCopy = new SimpleParameterList(paramValues.length);
        System.arraycopy(paramValues, 0, newCopy.paramValues, 0, paramValues.length);
        System.arraycopy(paramTypes, 0, newCopy.paramTypes, 0, paramTypes.length);
        System.arraycopy(direction, 0, newCopy.direction, 0, direction.length);
        return newCopy;
    }

    public void clear() {
        Arrays.fill(paramValues, null);
        Arrays.fill(paramTypes, 0);
        Arrays.fill(encoded, null);
        Arrays.fill(direction, 0);
    }
    public SimpleParameterList[] getSubparams() {
        return null;
    }

    private final Object[] paramValues;
    private final int[] paramTypes;
    private final int[] direction;
    private final byte[][] encoded;
    
    /**
     * Marker object representing NULL; this distinguishes
     * "parameter never set" from "parameter set to null".
     */
    private final static Object NULL_OBJECT = new Object();

    
}