| 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 org.h2.engine.SessionLocal; |
| 9 |
import org.h2.expression.function.table.TableFunction; |
| 10 |
import org.h2.result.ResultInterface; |
| 11 |
import org.h2.schema.Schema; |
| 12 |
|
| 13 |
/** |
| 14 |
* A table backed by a system or user-defined function that returns a result |
| 15 |
* set. |
| 16 |
*/ |
| 17 |
public class FunctionTable extends VirtualConstructedTable { |
| 18 |
|
| 19 |
private final TableFunction function; |
| 20 |
|
| 21 |
public FunctionTable(Schema schema, SessionLocal session, TableFunction function) { |
| 22 |
super(schema, 0, function.getName()); |
| 23 |
this.function = function; |
| 24 |
function.optimize(session); |
| 25 |
ResultInterface result = function.getValueTemplate(session); |
| 26 |
int columnCount = result.getVisibleColumnCount(); |
| 27 |
Column[] cols = new Column[columnCount]; |
| 28 |
for (int i = 0; i < columnCount; i++) { |
| 29 |
cols[i] = new Column(result.getColumnName(i), result.getColumnType(i)); |
| 30 |
} |
| 31 |
setColumns(cols); |
| 32 |
} |
| 33 |
|
| 34 |
@Override |
| 35 |
public boolean canGetRowCount(SessionLocal session) { |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
public long getRowCount(SessionLocal session) { |
| 41 |
return Long.MAX_VALUE; |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
public long getRowCountApproximation(SessionLocal session) { |
| 46 |
return Long.MAX_VALUE; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public ResultInterface getResult(SessionLocal session) { |
| 51 |
return function.getValue(session); |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public String getSQL(int sqlFlags) { |
| 56 |
return function.getSQL(sqlFlags); |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 61 |
return builder.append(function.getSQL(sqlFlags)); |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public boolean isDeterministic() { |
| 66 |
return function.isDeterministic(); |
| 67 |
} |
| 68 |
|
| 69 |
} |