File: QueryExecutorImpl.java

package info (click to toggle)
libpgjava 8.4-701-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,532 kB
  • ctags: 4,162
  • sloc: java: 33,948; xml: 3,158; makefile: 14; sh: 10
file content (613 lines) | stat: -rw-r--r-- 23,729 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*-------------------------------------------------------------------------
*
* Copyright (c) 2003-2008, PostgreSQL Global Development Group
* Copyright (c) 2004, Open Cloud Limited.
*
* IDENTIFICATION
*   $PostgreSQL: pgjdbc/org/postgresql/core/v2/QueryExecutorImpl.java,v 1.22 2009/07/01 05:00:40 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.core.v2;

import java.util.Vector;
import java.io.IOException;
import java.io.Writer;
import java.sql.*;

import org.postgresql.core.*;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;
import org.postgresql.util.GT;
import org.postgresql.copy.CopyOperation;

/**
 * QueryExecutor implementation for the V2 protocol.
 */
public class QueryExecutorImpl implements QueryExecutor {
    public QueryExecutorImpl(ProtocolConnectionImpl protoConnection, PGStream pgStream, Logger logger) {
        this.protoConnection = protoConnection;
        this.pgStream = pgStream;
        this.logger = logger;
    }

    //
    // Query parsing
    //

    public Query createSimpleQuery(String sql) {
        return new V2Query(sql, false, protoConnection);
    }

    public Query createParameterizedQuery(String sql) {
        return new V2Query(sql, true, protoConnection);
    }

    //
    // Fastpath
    //

    public ParameterList createFastpathParameters(int count) {
        return new FastpathParameterList(count);
    }

    public synchronized byte[]
    fastpathCall(int fnid, ParameterList parameters, boolean suppressBegin) throws SQLException {
        if (protoConnection.getTransactionState() == ProtocolConnection.TRANSACTION_IDLE && !suppressBegin)
        {

            if (logger.logDebug())
                logger.debug("Issuing BEGIN before fastpath call.");

            ResultHandler handler = new ResultHandler() {
                                        private boolean sawBegin = false;
                                        private SQLException sqle = null;

                                        public void handleResultRows(Query fromQuery, Field[] fields, Vector tuples, ResultCursor cursor) {
                                        }

                                        public void handleCommandStatus(String status, int updateCount, long insertOID) {
                                            if (!sawBegin)
                                            {
                                                if (!status.equals("BEGIN"))
                                                    handleError(new PSQLException(GT.tr("Expected command status BEGIN, got {0}.", status),
                                                                                  PSQLState.PROTOCOL_VIOLATION));
                                                sawBegin = true;
                                            }
                                            else
                                            {
                                                handleError(new PSQLException(GT.tr("Unexpected command status: {0}.", status),
                                                                              PSQLState.PROTOCOL_VIOLATION));
                                            }
                                        }

                                        public void handleWarning(SQLWarning warning) {
                                            // we don't want to ignore warnings and it would be tricky
                                            // to chain them back to the connection, so since we don't
                                            // expect to get them in the first place, we just consider
                                            // them errors.
                                            handleError(warning);
                                        }

                                        public void handleError(SQLException error) {
                                            if (sqle == null)
                                            {
                                                sqle = error;
                                            }
                                            else
                                            {
                                                sqle.setNextException(error);
                                            }
                                        }

                                        public void handleCompletion() throws SQLException{
                                            if (sqle != null)
                                                throw sqle;
                                        }
                                    };

            try
            {
                // Create and issue a dummy query to use the existing prefix infrastructure
                V2Query query = (V2Query)createSimpleQuery("");
                SimpleParameterList params = (SimpleParameterList)query.createParameterList();
                sendQuery(query, params, "BEGIN");
                processResults(query, handler, 0, 0);
            }
            catch (IOException ioe)
            {
                throw new PSQLException(GT.tr("An I/O error occured while sending to the backend."), PSQLState.CONNECTION_FAILURE, ioe);
            }
        }

        try
        {
            sendFastpathCall(fnid, (FastpathParameterList)parameters);
            return receiveFastpathResult();
        }
        catch (IOException ioe)
        {
            throw new PSQLException(GT.tr("An I/O error occured while sending to the backend."), PSQLState.CONNECTION_FAILURE, ioe);
        }
    }

    private void sendFastpathCall(int fnid, FastpathParameterList params) throws IOException {
        // Send call.
        int count = params.getParameterCount();

        if (logger.logDebug())
            logger.debug(" FE=> FastpathCall(fnid=" + fnid + ",paramCount=" + count + ")");

        pgStream.SendChar('F');
        pgStream.SendChar(0);
        pgStream.SendInteger4(fnid);
        pgStream.SendInteger4(count);

        for (int i = 1; i <= count; ++i)
            params.writeV2FastpathValue(i, pgStream);

        pgStream.flush();
    }

    public synchronized void processNotifies() throws SQLException {
        // Asynchronous notifies only arrive when we are not in a transaction
        if (protoConnection.getTransactionState() != ProtocolConnection.TRANSACTION_IDLE)
            return;
            
        try {
            while (pgStream.hasMessagePending()) {
                int c = pgStream.ReceiveChar();
                switch (c) {
                case 'A':  // Asynchronous Notify
                    receiveAsyncNotify();
                    break;
                case 'E':  // Error Message
                    throw receiveErrorMessage();
                    // break;
                case 'N':  // Error Notification
                    protoConnection.addWarning(receiveNotification());
                    break;
                default:
                    throw new PSQLException(GT.tr("Unknown Response Type {0}.", new Character((char) c)), PSQLState.CONNECTION_FAILURE);
                }
            }
        } catch (IOException ioe) {
            throw new PSQLException(GT.tr("An I/O error occured while sending to the backend."), PSQLState.CONNECTION_FAILURE, ioe);
        }
    }

    private byte[] receiveFastpathResult() throws IOException, SQLException {
        SQLException error = null;
        boolean endQuery = false;
        byte[] result = null;

        while (!endQuery)
        {
            int c = pgStream.ReceiveChar();

            switch (c)
            {
            case 'A':  // Asynchronous Notify
                receiveAsyncNotify();
                break;

            case 'E':  // Error Message
                SQLException newError = receiveErrorMessage();
                if (error == null)
                    error = newError;
                else
                    error.setNextException(newError);
                // keep processing
                break;

            case 'N':  // Error Notification
                protoConnection.addWarning(receiveNotification());
                break;

            case 'V':    // Fastpath result
                c = pgStream.ReceiveChar();
                if (c == 'G')
                {
                    if (logger.logDebug())
                        logger.debug(" <=BE FastpathResult");

                    // Result.
                    int len = pgStream.ReceiveInteger4();
                    result = pgStream.Receive(len);
                    c = pgStream.ReceiveChar();
                }
                else
                {
                    if (logger.logDebug())
                        logger.debug(" <=BE FastpathVoidResult");
                }

                if (c != '0')
                    throw new PSQLException(GT.tr("Unknown Response Type {0}.", new Character((char) c)), PSQLState.CONNECTION_FAILURE);

                break;

            case 'Z':
                if (logger.logDebug())
                    logger.debug(" <=BE ReadyForQuery");
                endQuery = true;
                break;

            default:
                throw new PSQLException(GT.tr("Unknown Response Type {0}.", new Character((char) c)), PSQLState.CONNECTION_FAILURE);
            }

        }

        // did we get an error during this query?
        if (error != null)
            throw error;

        return result;
    }

    //
    // Query execution
    //

    public synchronized void execute(Query query,
                                     ParameterList parameters,
                                     ResultHandler handler,
                                     int maxRows, int fetchSize, int flags)
    throws SQLException
    {
        execute((V2Query)query, (SimpleParameterList)parameters, handler, maxRows, flags);
    }

    // Nothing special yet, just run the queries one at a time.
    public synchronized void execute(Query[] queries,
                                     ParameterList[] parameters,
                                     ResultHandler handler,
                                     int maxRows, int fetchSize, int flags)
    throws SQLException
    {
        final ResultHandler delegateHandler = handler;
        handler = new ResultHandler() {
                      public void handleResultRows(Query fromQuery, Field[] fields, Vector tuples, ResultCursor cursor) {
                          delegateHandler.handleResultRows(fromQuery, fields, tuples, cursor);
                      }

                      public void handleCommandStatus(String status, int updateCount, long insertOID) {
                          delegateHandler.handleCommandStatus(status, updateCount, insertOID);
                      }

                      public void handleWarning(SQLWarning warning) {
                          delegateHandler.handleWarning(warning);
                      }

                      public void handleError(SQLException error) {
                          delegateHandler.handleError(error);
                      }

                      public void handleCompletion() throws SQLException {
                      }
                  };

        for (int i = 0; i < queries.length; ++i)
            execute((V2Query)queries[i], (SimpleParameterList)parameters[i], handler, maxRows, flags);

        delegateHandler.handleCompletion();
    }

    public void fetch(ResultCursor cursor, ResultHandler handler, int rows) throws SQLException {
        throw org.postgresql.Driver.notImplemented(this.getClass(), "fetch(ResultCursor,ResultHandler,int)");
    }

    private void execute(V2Query query,
                         SimpleParameterList parameters,
                         ResultHandler handler,
                         int maxRows, int flags) throws SQLException
    {

        // The V2 protocol has no support for retrieving metadata
        // without executing the whole query.
        if ((flags & QueryExecutor.QUERY_DESCRIBE_ONLY) != 0)
            return;

        if (parameters == null)
            parameters = (SimpleParameterList)query.createParameterList();

        parameters.checkAllParametersSet();

        String queryPrefix = null;
        if (protoConnection.getTransactionState() == ProtocolConnection.TRANSACTION_IDLE &&
                (flags & QueryExecutor.QUERY_SUPPRESS_BEGIN) == 0)
        {

            queryPrefix = "BEGIN;";

            // Insert a handler that intercepts the BEGIN.
            final ResultHandler delegateHandler = handler;
            handler = new ResultHandler() {
                          private boolean sawBegin = false;

                          public void handleResultRows(Query fromQuery, Field[] fields, Vector tuples, ResultCursor cursor) {
                              if (sawBegin)
                                  delegateHandler.handleResultRows(fromQuery, fields, tuples, cursor);
                          }

                          public void handleCommandStatus(String status, int updateCount, long insertOID) {
                              if (!sawBegin)
                              {
                                  if (!status.equals("BEGIN"))
                                      handleError(new PSQLException(GT.tr("Expected command status BEGIN, got {0}.", status),
                                                                    PSQLState.PROTOCOL_VIOLATION));
                                  sawBegin = true;
                              }
                              else
                              {
                                  delegateHandler.handleCommandStatus(status, updateCount, insertOID);
                              }
                          }

                          public void handleWarning(SQLWarning warning) {
                              delegateHandler.handleWarning(warning);
                          }

                          public void handleError(SQLException error) {
                              delegateHandler.handleError(error);
                          }

                          public void handleCompletion() throws SQLException{
                              delegateHandler.handleCompletion();
                          }
                      };
        }

        try
        {
            sendQuery(query, parameters, queryPrefix);
            processResults(query, handler, maxRows, flags);
        }
        catch (IOException e)
        {
            protoConnection.close();
            handler.handleError(new PSQLException(GT.tr("An I/O error occured while sending to the backend."), PSQLState.CONNECTION_FAILURE, e));
        }

        handler.handleCompletion();
    }

    /*
     * Send a query to the backend.
     */
    protected void sendQuery(V2Query query, SimpleParameterList params, String queryPrefix) throws IOException {
        if (logger.logDebug())
            logger.debug(" FE=> Query(\"" + (queryPrefix == null ? "" : queryPrefix) + query.toString(params) + "\")");

        pgStream.SendChar('Q');

        Writer encodingWriter = pgStream.getEncodingWriter();

        if (queryPrefix != null)
            encodingWriter.write(queryPrefix);

        String[] fragments = query.getFragments();
        for (int i = 0 ; i < fragments.length; ++i)
        {
            encodingWriter.write(fragments[i]);
            if (i < params.getParameterCount())
                params.writeV2Value(i + 1, encodingWriter);
        }

        encodingWriter.write(0);
        pgStream.flush();
    }

    protected void processResults(Query originalQuery, ResultHandler handler, int maxRows, int flags) throws IOException {
        boolean bothRowsAndStatus = (flags & QueryExecutor.QUERY_BOTH_ROWS_AND_STATUS) != 0;
        Field[] fields = null;
        Vector tuples = null;

        boolean endQuery = false;
        while (!endQuery)
        {
            int c = pgStream.ReceiveChar();

            switch (c)
            {
            case 'A':  // Asynchronous Notify
                receiveAsyncNotify();
                break;

            case 'B':  // Binary Data Transfer
                {
                    if (fields == null)
                        throw new IOException("Data transfer before field metadata");

                    if (logger.logDebug())
                        logger.debug(" <=BE BinaryRow");

                    Object tuple = null;
                    try {
                        tuple = pgStream.ReceiveTupleV2(fields.length, true);
                    } catch(OutOfMemoryError oome) {
                        if (maxRows == 0 || tuples.size() < maxRows) {
                            handler.handleError(new PSQLException(GT.tr("Ran out of memory retrieving query results."), PSQLState.OUT_OF_MEMORY, oome));
                        }
                    }

                    for (int i = 0; i < fields.length; i++)
                        fields[i].setFormat(Field.BINARY_FORMAT); //Set the field to binary format
                    if (maxRows == 0 || tuples.size() < maxRows)
                        tuples.addElement(tuple);
                }
                break;

            case 'C':  // Command Status
                String status = pgStream.ReceiveString();

                if (logger.logDebug())
                    logger.debug(" <=BE CommandStatus(" + status + ")");

                if (fields != null)
                {
                    handler.handleResultRows(originalQuery, fields, tuples, null);
                    fields = null;

                    if (bothRowsAndStatus)
                        interpretCommandStatus(status, handler);
                }
                else
                {
                    interpretCommandStatus(status, handler);
                }

                break;

            case 'D':  // Text Data Transfer
                {
                    if (fields == null)
                        throw new IOException("Data transfer before field metadata");

                    if (logger.logDebug())
                        logger.debug(" <=BE DataRow");

                    Object tuple = null;
                    try {
                        tuple = pgStream.ReceiveTupleV2(fields.length, false);
                    } catch(OutOfMemoryError oome) {
                        if (maxRows == 0 || tuples.size() < maxRows)
                            handler.handleError(new PSQLException(GT.tr("Ran out of memory retrieving query results."), PSQLState.OUT_OF_MEMORY, oome));
                    }
                    if (maxRows == 0 || tuples.size() < maxRows)
                        tuples.addElement(tuple);
                }

                break;

            case 'E':  // Error Message
                handler.handleError(receiveErrorMessage());
                // keep processing
                break;

            case 'I':  // Empty Query
                if (logger.logDebug())
                    logger.debug(" <=BE EmptyQuery");
                c = pgStream.ReceiveChar();
                if (c != 0)
                    throw new IOException("Expected \\0 after EmptyQuery, got: " + c);
                break;

            case 'N':  // Error Notification
                handler.handleWarning(receiveNotification());
                break;

            case 'P':  // Portal Name
                String portalName = pgStream.ReceiveString();
                if (logger.logDebug())
                    logger.debug(" <=BE PortalName(" + portalName + ")");
                break;

            case 'T':  // MetaData Field Description
                fields = receiveFields();
                tuples = new Vector();
                break;

            case 'Z':
                if (logger.logDebug())
                    logger.debug(" <=BE ReadyForQuery");
                endQuery = true;
                break;

            default:
                throw new IOException("Unexpected packet type: " + c);
            }

        }
    }

    /*
     * Receive the field descriptions from the back end.
     */
    private Field[] receiveFields() throws IOException
    {
        int size = pgStream.ReceiveInteger2();
        Field[] fields = new Field[size];

        if (logger.logDebug())
            logger.debug(" <=BE RowDescription(" + fields.length + ")");

        for (int i = 0; i < fields.length; i++)
        {
            String columnLabel = pgStream.ReceiveString();
            int typeOid = pgStream.ReceiveInteger4();
            int typeLength = pgStream.ReceiveInteger2();
            int typeModifier = pgStream.ReceiveInteger4();
            fields[i] = new Field(columnLabel, columnLabel, typeOid, typeLength, typeModifier, 0, 0);
        }

        return fields;
    }

    private void receiveAsyncNotify() throws IOException {
        int pid = pgStream.ReceiveInteger4();
        String msg = pgStream.ReceiveString();

        if (logger.logDebug())
            logger.debug(" <=BE AsyncNotify(pid=" + pid + ",msg=" + msg + ")");

        protoConnection.addNotification(new org.postgresql.core.Notification(msg, pid));
    }

    private SQLException receiveErrorMessage() throws IOException {
        String errorMsg = pgStream.ReceiveString().trim();
        if (logger.logDebug())
            logger.debug(" <=BE ErrorResponse(" + errorMsg + ")");
        return new PSQLException(errorMsg, PSQLState.UNKNOWN_STATE);
    }

    private SQLWarning receiveNotification() throws IOException {
        String warnMsg = pgStream.ReceiveString();

        // Strip out the severity field so we have consistency with
        // the V3 protocol.  SQLWarning.getMessage should return just
        // the actual message.
        //
        int severityMark = warnMsg.indexOf(":");
        warnMsg = warnMsg.substring(severityMark+1).trim();
        if (logger.logDebug())
            logger.debug(" <=BE NoticeResponse(" + warnMsg + ")");
        return new SQLWarning(warnMsg);
    }

    private void interpretCommandStatus(String status, ResultHandler handler) throws IOException {
        int update_count = 0;
        long insert_oid = 0;

        if (status.equals("BEGIN"))
            protoConnection.setTransactionState(ProtocolConnection.TRANSACTION_OPEN);
        else if (status.equals("COMMIT") || status.equals("ROLLBACK"))
            protoConnection.setTransactionState(ProtocolConnection.TRANSACTION_IDLE);
        else if (status.startsWith("INSERT") || status.startsWith("UPDATE") || status.startsWith("DELETE") || status.startsWith("MOVE"))
        {
            try
            {
                update_count = Integer.parseInt(status.substring(1 + status.lastIndexOf(' ')));
                if (status.startsWith("INSERT"))
                    insert_oid = Long.parseLong(status.substring(1 + status.indexOf(' '),
                                                status.lastIndexOf(' ')));
            }
            catch (NumberFormatException nfe)
            {
                handler.handleError(new PSQLException(GT.tr("Unable to interpret the update count in command completion tag: {0}.", status), PSQLState.CONNECTION_FAILURE));
                return ;
            }
        }

        handler.handleCommandStatus(status, update_count, insert_oid);
    }

    private final ProtocolConnectionImpl protoConnection;
    private final PGStream pgStream;
    private final Logger logger;

    public CopyOperation startCopy(String sql) throws SQLException {
        throw new PSQLException(GT.tr("Copy not implemented for protocol version 2"), PSQLState.NOT_IMPLEMENTED);
    }
}