| 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.table; |
| 7 |
|
| 8 |
import java.util.HashMap; |
| 9 |
|
| 10 |
import org.h2.result.Row; |
| 11 |
import org.h2.value.Value; |
| 12 |
import org.h2.value.ValueBigint; |
| 13 |
|
| 14 |
/** |
| 15 |
* Column resolver for generated columns. |
| 16 |
*/ |
| 17 |
class GeneratedColumnResolver implements ColumnResolver { |
| 18 |
|
| 19 |
private final Table table; |
| 20 |
|
| 21 |
private Column[] columns; |
| 22 |
|
| 23 |
private HashMap<String, Column> columnMap; |
| 24 |
|
| 25 |
private Row current; |
| 26 |
|
| 27 |
/** |
| 28 |
* Column resolver for generated columns. |
| 29 |
* |
| 30 |
* @param table |
| 31 |
* the table |
| 32 |
*/ |
| 33 |
GeneratedColumnResolver(Table table) { |
| 34 |
this.table = table; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Set the current row. |
| 39 |
* |
| 40 |
* @param current |
| 41 |
* the current row |
| 42 |
*/ |
| 43 |
void set(Row current) { |
| 44 |
this.current = current; |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public Column[] getColumns() { |
| 49 |
Column[] columns = this.columns; |
| 50 |
if (columns == null) { |
| 51 |
this.columns = columns = createColumns(); |
| 52 |
} |
| 53 |
return columns; |
| 54 |
} |
| 55 |
|
| 56 |
private Column[] createColumns() { |
| 57 |
Column[] allColumns = table.getColumns(); |
| 58 |
int totalCount = allColumns.length, baseCount = totalCount; |
| 59 |
for (int i = 0; i < totalCount; i++) { |
| 60 |
if (allColumns[i].isGenerated()) { |
| 61 |
baseCount--; |
| 62 |
} |
| 63 |
} |
| 64 |
Column[] baseColumns = new Column[baseCount]; |
| 65 |
for (int i = 0, j = 0; i < totalCount; i++) { |
| 66 |
Column c = allColumns[i]; |
| 67 |
if (!c.isGenerated()) { |
| 68 |
baseColumns[j++] = c; |
| 69 |
} |
| 70 |
} |
| 71 |
return baseColumns; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public Column findColumn(String name) { |
| 76 |
HashMap<String, Column> columnMap = this.columnMap; |
| 77 |
if (columnMap == null) { |
| 78 |
columnMap = table.getDatabase().newStringMap(); |
| 79 |
for (Column c : getColumns()) { |
| 80 |
columnMap.put(c.getName(), c); |
| 81 |
} |
| 82 |
this.columnMap = columnMap; |
| 83 |
} |
| 84 |
return columnMap.get(name); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public Value getValue(Column column) { |
| 89 |
int columnId = column.getColumnId(); |
| 90 |
if (columnId == -1) { |
| 91 |
return ValueBigint.get(current.getKey()); |
| 92 |
} |
| 93 |
return current.getValue(columnId); |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public Column getRowIdColumn() { |
| 98 |
return table.getRowIdColumn(); |
| 99 |
} |
| 100 |
|
| 101 |
} |