01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.result;
07:
08: import java.sql.SQLException;
09:
10: import org.h2.store.DataPage;
11: import org.h2.store.DiskFile;
12: import org.h2.store.Record;
13: import org.h2.value.Value;
14:
15: /**
16: * Represents a row in a table.
17: */
18: public class Row extends Record implements SearchRow {
19: public static final int MEMORY_CALCULATE = -1;
20: private final Value[] data;
21: private final int memory;
22:
23: public Row(Value[] data, int memory) {
24: this .data = data;
25: this .memory = memory;
26: }
27:
28: public Row(Row old) {
29: this .data = old.data;
30: this .memory = old.memory;
31: }
32:
33: public Value getValue(int i) {
34: return data[i];
35: }
36:
37: public void write(DataPage buff) throws SQLException {
38: buff.writeInt(data.length);
39: for (int i = 0; i < data.length; i++) {
40: Value v = data[i];
41: buff.writeValue(v);
42: }
43: }
44:
45: public int getByteCount(DataPage dummy) throws SQLException {
46: int len = data.length;
47: int size = dummy.getIntLen();
48: for (int i = 0; i < len; i++) {
49: Value v = data[i];
50: size += dummy.getValueLen(v);
51: }
52: return size;
53: }
54:
55: public void setValue(int i, Value v) {
56: data[i] = v;
57: }
58:
59: public boolean isEmpty() {
60: return data == null;
61: }
62:
63: public int getColumnCount() {
64: return data.length;
65: }
66:
67: public int getMemorySize() {
68: if (memory != MEMORY_CALCULATE) {
69: return blockCount * (DiskFile.BLOCK_SIZE / 16) + memory * 4;
70: }
71: int m = blockCount * (DiskFile.BLOCK_SIZE / 16);
72: for (int i = 0; data != null && i < data.length; i++) {
73: m += data[i].getMemory();
74: }
75: return m;
76: }
77:
78: public String toString() {
79: StringBuffer buff = new StringBuffer(data.length * 5);
80: buff.append("( /* pos:");
81: buff.append(getPos());
82: if (getDeleted()) {
83: buff.append(" deleted");
84: }
85: buff.append(" */ ");
86: for (int i = 0; i < data.length; i++) {
87: if (i > 0) {
88: buff.append(", ");
89: }
90: buff.append(data[i].getSQL());
91: }
92: buff.append(')');
93: return buff.toString();
94: }
95:
96: }
|