| 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.ArrayList; |
| 9 |
|
| 10 |
import org.h2.command.query.TableValueConstructor; |
| 11 |
import org.h2.engine.SessionLocal; |
| 12 |
import org.h2.expression.Expression; |
| 13 |
import org.h2.result.ResultInterface; |
| 14 |
import org.h2.result.SimpleResult; |
| 15 |
import org.h2.schema.Schema; |
| 16 |
|
| 17 |
/** |
| 18 |
* A table for table value constructor. |
| 19 |
*/ |
| 20 |
public class TableValueConstructorTable extends VirtualConstructedTable { |
| 21 |
|
| 22 |
private final ArrayList<ArrayList<Expression>> rows; |
| 23 |
|
| 24 |
public TableValueConstructorTable(Schema schema, SessionLocal session, Column[] columns, |
| 25 |
ArrayList<ArrayList<Expression>> rows) { |
| 26 |
super(schema, 0, "VALUES"); |
| 27 |
setColumns(columns); |
| 28 |
this.rows = rows; |
| 29 |
} |
| 30 |
|
| 31 |
@Override |
| 32 |
public boolean canGetRowCount(SessionLocal session) { |
| 33 |
return true; |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
public long getRowCount(SessionLocal session) { |
| 38 |
return rows.size(); |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
public long getRowCountApproximation(SessionLocal session) { |
| 43 |
return rows.size(); |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public ResultInterface getResult(SessionLocal session) { |
| 48 |
SimpleResult simple = new SimpleResult(); |
| 49 |
int columnCount = columns.length; |
| 50 |
for (int i = 0; i < columnCount; i++) { |
| 51 |
Column column = columns[i]; |
| 52 |
simple.addColumn(column.getName(), column.getType()); |
| 53 |
} |
| 54 |
TableValueConstructor.getVisibleResult(session, simple, columns, rows); |
| 55 |
return simple; |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 60 |
builder.append('('); |
| 61 |
TableValueConstructor.getValuesSQL(builder, sqlFlags, rows); |
| 62 |
return builder.append(')'); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public boolean isDeterministic() { |
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
} |