File: FastpathArg.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 (86 lines) | stat: -rw-r--r-- 2,366 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
/*-------------------------------------------------------------------------
*
* Copyright (c) 2003-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
*   $PostgreSQL: pgjdbc/org/postgresql/fastpath/FastpathArg.java,v 1.13 2008/01/08 06:56:28 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.fastpath;

import java.sql.SQLException;
import org.postgresql.core.ParameterList;

// Not a very clean mapping to the new QueryExecutor/ParameterList
// stuff, but it seems hard to support both v2 and v3 cleanly with
// the same model while retaining API compatibility. So I've just
// done it the ugly way..

/**
 *     Each fastpath call requires an array of arguments, the number and type
 *     dependent on the function being called.
 */
public class FastpathArg
{
    /**
     * Encoded byte value of argument.
     */
    private final byte[] bytes;
    private final int bytesStart;
    private final int bytesLength;

    /**
     * Constructs an argument that consists of an integer value
     * @param value int value to set
     */
    public FastpathArg(int value)
    {
        bytes = new byte[4];
        bytes[3] = (byte) (value);
        bytes[2] = (byte) (value >> 8);
        bytes[1] = (byte) (value >> 16);
        bytes[0] = (byte) (value >> 24);
        bytesStart = 0;
        bytesLength = 4;
    }

    /**
     * Constructs an argument that consists of an array of bytes
     * @param bytes array to store
     */
    public FastpathArg(byte bytes[])
    {
        this(bytes, 0, bytes.length);
    }

    /**
     * Constructs an argument that consists of part of a byte array
     * @param buf source array
     * @param off offset within array
     * @param len length of data to include
     */
    public FastpathArg(byte buf[], int off, int len)
    {
        this.bytes = buf;
        this.bytesStart = off;
        this.bytesLength = len;
    }

    /**
     * Constructs an argument that consists of a String.
     * @param s String to store
     */
    public FastpathArg(String s)
    {
        this(s.getBytes());
    }

    void populateParameter(ParameterList params, int index) throws SQLException {
        if (bytes == null)
            params.setNull(index, 0);
        else
            params.setBytea(index, bytes, bytesStart, bytesLength);
    }
}