File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\result\Row.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.result;
8
9 import org.h2.engine.Constants;
10 import org.h2.store.Data;
11 import org.h2.util.StatementBuilder;
12 import org.h2.value.Value;
13 import org.h2.value.ValueLong;
14
15 /**
16 * Represents a row in a table.
17 */
18 public class Row implements SearchRow {
19
20 public static final int MEMORY_CALCULATE = -1;
21 public static final Row[] EMPTY_ARRAY = {};
22
23 private long key;
24 private final Value[] data;
25 private int memory;
26 private int version;
27 private boolean deleted;
28 private int sessionId;
29
30 public Row(Value[] data, int memory) {
31 this.data = data;
32 this.memory = memory;
33 }
34
35 /**
36 * Get a copy of the row that is distinct from (not equal to) this row.
37 * This is used for FOR UPDATE to allow pseudo-updating a row.
38 *
39 * @return a new row with the same data
40 */
41 public Row getCopy() {
42 Value[] d2 = new Value[data.length];
43 System.arraycopy(data, 0, d2, 0, data.length);
44 Row r2 = new Row(d2, memory);
45 r2.key = key;
46 r2.version = version + 1;
47 r2.sessionId = sessionId;
48 return r2;
49 }
50
51 public void setKeyAndVersion(SearchRow row) {
52 setKey(row.getKey());
53 setVersion(row.getVersion());
54 }
55
56 public int getVersion() {
57 return version;
58 }
59
60 public void setVersion(int version) {
61 this.version = version;
62 }
63
64 public long getKey() {
65 return key;
66 }
67
68 public void setKey(long key) {
69 this.key = key;
70 }
71
72 public Value getValue(int i) {
73 return i == -1 ? ValueLong.get(key) : data[i];
74 }
75
76 /**
77 * Get the number of bytes required for the data.
78 *
79 * @param dummy the template buffer
80 * @return the number of bytes
81 */
82 public int getByteCount(Data dummy) {
83 int size = 0;
84 for (Value v : data) {
85 size += dummy.getValueLen(v);
86 }
87 return size;
88 }
89
90 public void setValue(int i, Value v) {
91 if (i == -1) {
92 this.key = v.getLong();
93 } else {
94 data[i] = v;
95 }
96 }
97
98 public boolean isEmpty() {
99 return data == null;
100 }
101
102 public int getColumnCount() {
103 return data.length;
104 }
105
106 public int getMemory() {
107 if (memory != MEMORY_CALCULATE) {
108 return memory;
109 }
110 int m = Constants.MEMORY_ROW;
111 if (data != null) {
112 int len = data.length;
113 m += Constants.MEMORY_OBJECT + len * Constants.MEMORY_POINTER;
114 for (int i = 0; i < len; i++) {
115 Value v = data[i];
116 if (v != null) {
117 m += v.getMemory();
118 }
119 }
120 }
121 this.memory = m;
122 return m;
123 }
124
125 public String toString() {
126 StatementBuilder buff = new StatementBuilder("( /* key:");
127 buff.append(getKey());
128 if (version != 0) {
129 buff.append(" v:" + version);
130 }
131 if (isDeleted()) {
132 buff.append(" deleted");
133 }
134 buff.append(" */ ");
135 if (data != null) {
136 for (Value v : data) {
137 buff.appendExceptFirst(", ");
138 buff.append(v == null ? "null" : v.getTraceSQL());
139 }
140 }
141 return buff.append(')').toString();
142 }
143
144 public void setDeleted(boolean deleted) {
145 this.deleted = deleted;
146 }
147
148 public void setSessionId(int sessionId) {
149 this.sessionId = sessionId;
150 }
151
152 public int getSessionId() {
153 return sessionId;
154 }
155
156 /**
157 * This record has been committed. The session id is reset.
158 */
159 public void commit() {
160 this.sessionId = 0;
161 }
162
163 public boolean isDeleted() {
164 return deleted;
165 }
166
167 }
168