Debian Codegraph @

src/main/org/h2/expression/ParameterRemote.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.expression;
7
8 import java.io.IOException;
9 import java.sql.ResultSetMetaData;
10
11 import org.h2.api.ErrorCode;
12 import org.h2.message.DbException;
13 import org.h2.value.Transfer;
14 import org.h2.value.TypeInfo;
15 import org.h2.value.Value;
16 import org.h2.value.ValueLob;
17
18 /**
19 * A client side (remote) parameter.
20 */
21 public class ParameterRemote implements ParameterInterface {
22
23 private Value value;
24 private final int index;
25 private TypeInfo type = TypeInfo.TYPE_UNKNOWN;
26 private int nullable = ResultSetMetaData.columnNullableUnknown;
27
28 public ParameterRemote(int index) {
29 this.index = index;
30 }
31
32 @Override
33 public void setValue(Value newValue, boolean closeOld) {
34 if (closeOld && value instanceof ValueLob) {
35 ((ValueLob) value).remove();
36 }
37 value = newValue;
38 }
39
40 @Override
41 public Value getParamValue() {
42 return value;
43 }
44
45 @Override
46 public void checkSet() {
47 if (value == null) {
48 throw DbException.get(ErrorCode.PARAMETER_NOT_SET_1, "#" + (index + 1));
49 }
50 }
51
52 @Override
53 public boolean isValueSet() {
54 return value != null;
55 }
56
57 @Override
58 public TypeInfo getType() {
59 return value == null ? type : value.getType();
60 }
61
62 @Override
63 public int getNullable() {
64 return nullable;
65 }
66
67 /**
68 * Read the parameter meta data from the transfer object.
69 *
70 * @param transfer the transfer object
71 * @throws IOException on failure
72 */
73 public void readMetaData(Transfer transfer) throws IOException {
74 type = transfer.readTypeInfo();
75 nullable = transfer.readInt();
76 }
77
78 /**
79 * Write the parameter meta data to the transfer object.
80 *
81 * @param transfer the transfer object
82 * @param p the parameter
83 * @throws IOException on failure
84 */
85 public static void writeMetaData(Transfer transfer, ParameterInterface p) throws IOException {
86 transfer.writeTypeInfo(p.getType()).writeInt(p.getNullable());
87 }
88
89 }