01: /*-------------------------------------------------------------------------
02: *
03: * Copyright (c) 2003-2005, PostgreSQL Global Development Group
04: *
05: * IDENTIFICATION
06: * $PostgreSQL: pgjdbc/org/postgresql/fastpath/FastpathArg.java,v 1.12 2005/01/11 08:25:45 jurka Exp $
07: *
08: *-------------------------------------------------------------------------
09: */
10: package org.postgresql.fastpath;
11:
12: import java.sql.SQLException;
13: import org.postgresql.core.ParameterList;
14:
15: // Not a very clean mapping to the new QueryExecutor/ParameterList
16: // stuff, but it seems hard to support both v2 and v3 cleanly with
17: // the same model while retaining API compatibility. So I've just
18: // done it the ugly way..
19:
20: /**
21: * Each fastpath call requires an array of arguments, the number and type
22: * dependent on the function being called.
23: */
24: public class FastpathArg {
25: /**
26: * Encoded byte value of argument.
27: */
28: private final byte[] bytes;
29: private final int bytesStart;
30: private final int bytesLength;
31:
32: /**
33: * Constructs an argument that consists of an integer value
34: * @param value int value to set
35: */
36: public FastpathArg(int value) {
37: bytes = new byte[4];
38: bytes[3] = (byte) (value);
39: bytes[2] = (byte) (value >> 8);
40: bytes[1] = (byte) (value >> 16);
41: bytes[0] = (byte) (value >> 24);
42: bytesStart = 0;
43: bytesLength = 4;
44: }
45:
46: /**
47: * Constructs an argument that consists of an array of bytes
48: * @param bytes array to store
49: */
50: public FastpathArg(byte bytes[]) {
51: this (bytes, 0, bytes.length);
52: }
53:
54: /**
55: * Constructs an argument that consists of part of a byte array
56: * @param buf source array
57: * @param off offset within array
58: * @param len length of data to include
59: */
60: public FastpathArg(byte buf[], int off, int len) {
61: this .bytes = buf;
62: this .bytesStart = off;
63: this .bytesLength = len;
64: }
65:
66: /**
67: * Constructs an argument that consists of a String.
68: * @param s String to store
69: */
70: public FastpathArg(String s) {
71: this (s.getBytes());
72: }
73:
74: void populateParameter(ParameterList params, int index)
75: throws SQLException {
76: if (bytes == null)
77: params.setNull(index, 0);
78: else
79: params.setBytea(index, bytes, bytesStart, bytesLength);
80: }
81: }
|