001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.result;
007:
008: import java.sql.SQLException;
009:
010: import org.h2.engine.Constants;
011: import org.h2.engine.Session;
012: import org.h2.index.BtreeIndex;
013: import org.h2.index.Cursor;
014: import org.h2.index.Index;
015: import org.h2.index.IndexType;
016: import org.h2.message.Message;
017: import org.h2.schema.Schema;
018: import org.h2.table.Column;
019: import org.h2.table.IndexColumn;
020: import org.h2.table.TableData;
021: import org.h2.util.ObjectArray;
022: import org.h2.value.Value;
023: import org.h2.value.ValueArray;
024:
025: /**
026: * This class implements the temp table buffer for the LocalResult class.
027: */
028: public class ResultTempTable implements ResultExternal {
029: private static final String COLUMN_NAME = "DATA";
030: private Session session;
031: private TableData table;
032: private SortOrder sort;
033: private Index index;
034: private Cursor cursor;
035:
036: public ResultTempTable(Session session, SortOrder sort,
037: int columnCount) throws SQLException {
038: this .session = session;
039: this .sort = sort;
040: Schema schema = session.getDatabase().getSchema(
041: Constants.SCHEMA_MAIN);
042: Column column = new Column(COLUMN_NAME, Value.ARRAY);
043: column.setNullable(false);
044: ObjectArray columns = new ObjectArray();
045: columns.add(column);
046: int tableId = session.getDatabase()
047: .allocateObjectId(true, true);
048: String tableName = "TEMP_RESULT_SET_" + tableId;
049: table = schema.createTable(tableName, tableId, columns, false,
050: false);
051: int indexId = session.getDatabase()
052: .allocateObjectId(true, true);
053: IndexColumn indexColumn = new IndexColumn();
054: indexColumn.column = column;
055: indexColumn.columnName = COLUMN_NAME;
056: IndexType indexType;
057: indexType = IndexType.createPrimaryKey(true, false);
058: IndexColumn[] indexCols = new IndexColumn[] { indexColumn };
059: index = new BtreeIndex(session, table, indexId, tableName,
060: indexCols, indexType, Index.EMPTY_HEAD);
061: table.getIndexes().add(index);
062: }
063:
064: public int removeRow(Value[] values) throws SQLException {
065: Row row = convertToRow(values);
066: Cursor cursor = find(row);
067: if (cursor != null) {
068: row = cursor.get();
069: table.removeRow(session, row);
070: }
071: return (int) table.getRowCount(session);
072: }
073:
074: public boolean contains(Value[] values) throws SQLException {
075: return find(convertToRow(values)) != null;
076: }
077:
078: public int addRow(Value[] values) throws SQLException {
079: Row row = convertToRow(values);
080: Cursor cursor = find(row);
081: if (cursor == null) {
082: table.addRow(session, row);
083: }
084: return (int) table.getRowCount(session);
085: }
086:
087: public void addRows(ObjectArray rows) throws SQLException {
088: if (sort != null) {
089: sort.sort(rows);
090: }
091: for (int i = 0; i < rows.size(); i++) {
092: Value[] values = (Value[]) rows.get(i);
093: addRow(values);
094: }
095: }
096:
097: public void close() {
098: try {
099: if (table != null) {
100: index.remove(session);
101: ObjectArray indexes = table.getIndexes();
102: indexes.remove(indexes.indexOf(index));
103: table.removeChildrenAndResources(session);
104: }
105: } catch (SQLException e) {
106: throw Message.convertToInternal(e);
107: } finally {
108: table = null;
109: }
110: }
111:
112: public void done() throws SQLException {
113: }
114:
115: public Value[] next() throws SQLException {
116: if (!cursor.next()) {
117: return null;
118: }
119: Row row = cursor.get();
120: ValueArray data = (ValueArray) row.getValue(0);
121: return data.getList();
122: }
123:
124: public void reset() throws SQLException {
125: cursor = index.find(session, null, null);
126: }
127:
128: private Row convertToRow(Value[] values) {
129: ValueArray data = ValueArray.get(values);
130: return new Row(new Value[] { data }, data.getMemory());
131: }
132:
133: private Cursor find(Row row) throws SQLException {
134: Cursor cursor = index.find(session, row, row);
135: while (cursor.next()) {
136: SearchRow found;
137: found = cursor.getSearchRow();
138: if (found.getValue(0).equals(row.getValue(0))) {
139: return cursor;
140: }
141: }
142: return null;
143: }
144:
145: }
|