~sgdg/stado/stado25

« back to all changes in this revision

Viewing changes to src/org/postgresql/driver/core/v3/SimpleParameterList.java

  • Committer: Jim Mlodgenski
  • Date: 2011-08-30 22:39:37 UTC
  • mfrom: (1.1.3 stado)
  • Revision ID: jim@cirrusql.com-20110830223937-25q231a31x0e08b4
Merge from Spatial branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 * Copyright (C) 2008 EnterpriseDB Corporation.
 
3
 * Copyright (C) 2011 Stado Global Development Group.
 
4
 *
 
5
 * This file is part of Stado.
 
6
 *
 
7
 * Stado is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation, either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * Stado is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with Stado.  If not, see <http://www.gnu.org/licenses/>.
 
19
 *
 
20
 * You can find Stado at http://www.stado.us
 
21
 *
 
22
 ****************************************************************************/
 
23
package org.postgresql.driver.core.v3;
 
24
 
 
25
import java.io.InputStream;
 
26
import java.io.IOException;
 
27
import java.sql.SQLException;
 
28
import java.util.Arrays;
 
29
 
 
30
import org.postgresql.driver.core.*;
 
31
import org.postgresql.driver.util.GT;
 
32
import org.postgresql.driver.util.PSQLException;
 
33
import org.postgresql.driver.util.PSQLState;
 
34
import org.postgresql.driver.util.StreamWrapper;
 
35
 
 
36
/**
 
37
 * Parameter list for a single-statement V3 query.
 
38
 *
 
39
 * @author Oliver Jowett (oliver@opencloud.com)
 
40
 */
 
41
class SimpleParameterList implements V3ParameterList {
 
42
    
 
43
    private final static int IN = 1;
 
44
    private final static int OUT = 2;
 
45
    private final static int INOUT = IN|OUT;
 
46
 
 
47
    SimpleParameterList(int paramCount, ProtocolConnectionImpl protoConnection) {
 
48
        this.paramValues = new Object[paramCount];
 
49
        this.paramTypes = new int[paramCount];
 
50
        this.encoded = new byte[paramCount][];
 
51
        this.direction = new int[paramCount];
 
52
        this.protoConnection = protoConnection;
 
53
    }        
 
54
    
 
55
    public void registerOutParameter( int index, int sqlType ) throws SQLException
 
56
    {
 
57
        if (index < 1 || index > paramValues.length)
 
58
            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 );
 
59
 
 
60
        direction[index-1] |= OUT;
 
61
    }
 
62
 
 
63
    private void bind(int index, Object value, int oid) throws SQLException {
 
64
        if (index < 1 || index > paramValues.length)
 
65
            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 );
 
66
 
 
67
        --index;
 
68
 
 
69
        encoded[index] = null;
 
70
        paramValues[index] = value ;
 
71
        direction[index] |= IN;
 
72
        
 
73
        // If we are setting something to an UNSPECIFIED NULL, don't overwrite
 
74
        // our existing type for it.  We don't need the correct type info to
 
75
        // send this value, and we don't want to overwrite and require a
 
76
        // reparse.
 
77
        if (oid == Oid.UNSPECIFIED && paramTypes[index] != Oid.UNSPECIFIED && value == NULL_OBJECT)
 
78
            return;
 
79
 
 
80
        paramTypes[index] = oid;
 
81
    }
 
82
 
 
83
    public int getParameterCount()
 
84
    {
 
85
        return paramValues.length;
 
86
    }
 
87
    public int getOutParameterCount()
 
88
    {
 
89
        int count=0;
 
90
        for( int i=paramTypes.length; --i >= 0;)
 
91
        {
 
92
            if ((direction[i] & OUT) == OUT )
 
93
            {
 
94
                count++;
 
95
            }
 
96
        }
 
97
        // Every function has at least one output.
 
98
        if (count == 0)
 
99
            count = 1;
 
100
        return count;
 
101
        
 
102
    }
 
103
    public int getInParameterCount() 
 
104
    {
 
105
        int count=0;
 
106
        for( int i=0; i< paramTypes.length;i++)
 
107
        {
 
108
            if (direction[i] != OUT )
 
109
            {
 
110
                count++;
 
111
            }
 
112
        }
 
113
        return count;
 
114
    }
 
115
 
 
116
    public void setIntParameter(int index, int value) throws SQLException {
 
117
        byte[] data = new byte[4];
 
118
        data[3] = (byte)value;
 
119
        data[2] = (byte)(value >> 8);
 
120
        data[1] = (byte)(value >> 16);
 
121
        data[0] = (byte)(value >> 24);
 
122
        bind(index, data, Oid.INT4);
 
123
    }
 
124
 
 
125
    public void setLiteralParameter(int index, String value, int oid) throws SQLException {
 
126
        bind(index, value, oid);
 
127
    }
 
128
 
 
129
    public void setStringParameter(int index, String value, int oid) throws SQLException {
 
130
        bind(index, value, oid);
 
131
    }
 
132
 
 
133
    public void setBytea(int index, byte[] data, int offset, int length) throws SQLException {
 
134
        bind(index, new StreamWrapper(data, offset, length), Oid.BYTEA);
 
135
    }
 
136
 
 
137
    public void setBytea(int index, InputStream stream, int length) throws SQLException {
 
138
        bind(index, new StreamWrapper(stream, length), Oid.BYTEA);
 
139
    }
 
140
 
 
141
    public void setNull(int index, int oid) throws SQLException {
 
142
        bind(index, NULL_OBJECT, oid);
 
143
    }
 
144
 
 
145
    public String toString(int index) {
 
146
        --index;
 
147
        if (paramValues[index] == null)
 
148
            return "?";
 
149
        else if (paramValues[index] == NULL_OBJECT)
 
150
            return "NULL";
 
151
        else {
 
152
            String param = paramValues[index].toString();
 
153
            boolean hasBackslash = param.indexOf('\\') != -1;
 
154
 
 
155
            // add room for quotes + potential escaping.
 
156
            StringBuffer p = new StringBuffer(3 + param.length() * 11 / 10);
 
157
 
 
158
            boolean standardConformingStrings = false;
 
159
            boolean supportsEStringSyntax = false;
 
160
            if (protoConnection != null) {
 
161
                standardConformingStrings = protoConnection.getStandardConformingStrings();
 
162
                supportsEStringSyntax = protoConnection.getServerVersion().compareTo("8.1") >= 0;
 
163
            }
 
164
 
 
165
            if (hasBackslash && !standardConformingStrings && supportsEStringSyntax)
 
166
                p.append('E');
 
167
 
 
168
            p.append('\'');
 
169
            try {
 
170
                p = Utils.appendEscapedLiteral(p, param, standardConformingStrings);
 
171
            } catch (SQLException sqle) {
 
172
                // This should only happen if we have an embedded null
 
173
                // and there's not much we can do if we do hit one.
 
174
                //
 
175
                // The goal of toString isn't to be sent to the server,
 
176
                // so we aren't 100% accurate (see StreamWrapper), put
 
177
                // the unescaped version of the data.
 
178
                //
 
179
                p.append(param);
 
180
            }
 
181
            p.append('\'');
 
182
            return p.toString();
 
183
        }
 
184
    }
 
185
 
 
186
    public void checkAllParametersSet() throws SQLException {
 
187
        for (int i = 0; i < paramTypes.length; ++i)
 
188
        {
 
189
            if (direction[i] != OUT && paramValues[i] == null)
 
190
                throw new PSQLException(GT.tr("No value specified for parameter {0}.", new Integer(i + 1)), PSQLState.INVALID_PARAMETER_VALUE);
 
191
        }
 
192
    }
 
193
 
 
194
    public void convertFunctionOutParameters()
 
195
    {
 
196
        for (int i=0; i<paramTypes.length; ++i)
 
197
        {
 
198
            if (direction[i] == OUT)
 
199
            {
 
200
                paramTypes[i] = Oid.VOID;
 
201
                paramValues[i] = "null";
 
202
            }
 
203
        }
 
204
    }
 
205
 
 
206
    //
 
207
    // bytea helper
 
208
    //
 
209
 
 
210
    private static void streamBytea(PGStream pgStream, StreamWrapper wrapper) throws IOException {
 
211
        byte[] rawData = wrapper.getBytes();
 
212
        if (rawData != null)
 
213
        {
 
214
            pgStream.Send(rawData, wrapper.getOffset(), wrapper.getLength());
 
215
            return ;
 
216
        }
 
217
 
 
218
        pgStream.SendStream(wrapper.getStream(), wrapper.getLength());
 
219
    }
 
220
 
 
221
    public int[] getTypeOIDs() {
 
222
        return paramTypes;
 
223
    }
 
224
 
 
225
    //
 
226
    // Package-private V3 accessors
 
227
    //
 
228
 
 
229
    int getTypeOID(int index) {
 
230
        return paramTypes[index-1];
 
231
    }
 
232
 
 
233
    boolean hasUnresolvedTypes() {
 
234
        for (int i=0; i< paramTypes.length; i++) {
 
235
            if (paramTypes[i] == Oid.UNSPECIFIED)
 
236
                return true;
 
237
        }
 
238
        return false;
 
239
    }
 
240
 
 
241
    void setResolvedType(int index, int oid) {
 
242
        // only allow overwriting an unknown value
 
243
        if (paramTypes[index-1] == Oid.UNSPECIFIED) {
 
244
            paramTypes[index-1] = oid;
 
245
        } else if (paramTypes[index-1] != oid) {
 
246
            throw new IllegalArgumentException("Can't change resolved type for param: " + index + " from " + paramTypes[index-1] + " to " + oid);
 
247
        }
 
248
    }
 
249
 
 
250
    boolean isNull(int index) {
 
251
        return (paramValues[index-1] == NULL_OBJECT);
 
252
    }
 
253
 
 
254
    boolean isBinary(int index) {
 
255
        // Currently, only StreamWrapper uses the binary parameter form.
 
256
        return (paramValues[index-1] instanceof StreamWrapper);
 
257
    }
 
258
 
 
259
    int getV3Length(int index) {
 
260
        --index;
 
261
 
 
262
        // Null?
 
263
        if (paramValues[index] == NULL_OBJECT)
 
264
            throw new IllegalArgumentException("can't getV3Length() on a null parameter");
 
265
 
 
266
        // Directly encoded?
 
267
        if (paramValues[index] instanceof byte[])
 
268
            return ((byte[])paramValues[index]).length;
 
269
 
 
270
        // Binary-format bytea?
 
271
        if (paramValues[index] instanceof StreamWrapper)
 
272
            return ((StreamWrapper)paramValues[index]).getLength();
 
273
 
 
274
        // Already encoded?
 
275
        if (encoded[index] == null)
 
276
        {
 
277
            // Encode value and compute actual length using UTF-8.
 
278
            encoded[index] = Utils.encodeUTF8(paramValues[index].toString());
 
279
        }
 
280
 
 
281
        return encoded[index].length;
 
282
    }
 
283
 
 
284
    void writeV3Value(int index, PGStream pgStream) throws IOException {
 
285
        --index;
 
286
 
 
287
        // Null?
 
288
        if (paramValues[index] == NULL_OBJECT)
 
289
            throw new IllegalArgumentException("can't writeV3Value() on a null parameter");
 
290
 
 
291
        // Directly encoded?
 
292
        if (paramValues[index] instanceof byte[])
 
293
        {
 
294
            pgStream.Send((byte[])paramValues[index]);
 
295
            return ;
 
296
        }
 
297
 
 
298
        // Binary-format bytea?
 
299
        if (paramValues[index] instanceof StreamWrapper)
 
300
        {
 
301
            streamBytea(pgStream, (StreamWrapper)paramValues[index]);
 
302
            return ;
 
303
        }
 
304
 
 
305
        // Encoded string.
 
306
        if (encoded[index] == null)
 
307
            encoded[index] = Utils.encodeUTF8((String)paramValues[index]);
 
308
        pgStream.Send(encoded[index]);
 
309
    }
 
310
 
 
311
    
 
312
    
 
313
    public ParameterList copy() {
 
314
        SimpleParameterList newCopy = new SimpleParameterList(paramValues.length, protoConnection);
 
315
        System.arraycopy(paramValues, 0, newCopy.paramValues, 0, paramValues.length);
 
316
        System.arraycopy(paramTypes, 0, newCopy.paramTypes, 0, paramTypes.length);
 
317
        System.arraycopy(direction, 0, newCopy.direction, 0, direction.length);
 
318
        return newCopy;
 
319
    }
 
320
 
 
321
    public void clear() {
 
322
        Arrays.fill(paramValues, null);
 
323
        Arrays.fill(paramTypes, 0);
 
324
        Arrays.fill(encoded, null);
 
325
        Arrays.fill(direction, 0);
 
326
    }
 
327
    public SimpleParameterList[] getSubparams() {
 
328
        return null;
 
329
    }
 
330
 
 
331
    private final Object[] paramValues;
 
332
    private final int[] paramTypes;
 
333
    private final int[] direction;
 
334
    private final byte[][] encoded;
 
335
    private final ProtocolConnectionImpl protoConnection;
 
336
    
 
337
    /**
 
338
     * Marker object representing NULL; this distinguishes
 
339
     * "parameter never set" from "parameter set to null".
 
340
     */
 
341
    private final static Object NULL_OBJECT = new Object();
 
342
}
 
343