| 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.command.dml; |
| 7 |
|
| 8 |
import org.h2.command.CommandInterface; |
| 9 |
import org.h2.command.Prepared; |
| 10 |
import org.h2.engine.SessionLocal; |
| 11 |
import org.h2.expression.Alias; |
| 12 |
import org.h2.expression.Expression; |
| 13 |
import org.h2.expression.ExpressionColumn; |
| 14 |
import org.h2.expression.ExpressionVisitor; |
| 15 |
import org.h2.expression.function.table.TableFunction; |
| 16 |
import org.h2.result.LocalResult; |
| 17 |
import org.h2.result.ResultInterface; |
| 18 |
import org.h2.table.Column; |
| 19 |
import org.h2.value.Value; |
| 20 |
|
| 21 |
/** |
| 22 |
* This class represents the statement |
| 23 |
* CALL. |
| 24 |
*/ |
| 25 |
public class Call extends Prepared { |
| 26 |
|
| 27 |
private Expression expression; |
| 28 |
|
| 29 |
private TableFunction tableFunction; |
| 30 |
|
| 31 |
private Expression[] expressions; |
| 32 |
|
| 33 |
public Call(SessionLocal session) { |
| 34 |
super(session); |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public ResultInterface queryMeta() { |
| 39 |
int columnCount = expressions.length; |
| 40 |
LocalResult result = new LocalResult(session, expressions, columnCount, columnCount); |
| 41 |
result.done(); |
| 42 |
return result; |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public long update() { |
| 47 |
if (tableFunction != null) { |
| 48 |
// this will throw an exception |
| 49 |
// methods returning a result set may not be called like this. |
| 50 |
return super.update(); |
| 51 |
} |
| 52 |
Value v = expression.getValue(session); |
| 53 |
int type = v.getValueType(); |
| 54 |
switch (type) { |
| 55 |
case Value.UNKNOWN: |
| 56 |
case Value.NULL: |
| 57 |
return 0; |
| 58 |
default: |
| 59 |
return v.getInt(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public ResultInterface query(long maxrows) { |
| 65 |
setCurrentRowNumber(1); |
| 66 |
if (tableFunction != null) { |
| 67 |
return tableFunction.getValue(session); |
| 68 |
} |
| 69 |
LocalResult result = new LocalResult(session, expressions, 1, 1); |
| 70 |
result.addRow(expression.getValue(session)); |
| 71 |
result.done(); |
| 72 |
return result; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void prepare() { |
| 77 |
if (tableFunction != null) { |
| 78 |
prepareAlways = true; |
| 79 |
tableFunction.optimize(session); |
| 80 |
ResultInterface result = tableFunction.getValueTemplate(session); |
| 81 |
int columnCount = result.getVisibleColumnCount(); |
| 82 |
expressions = new Expression[columnCount]; |
| 83 |
for (int i = 0; i < columnCount; i++) { |
| 84 |
String name = result.getColumnName(i); |
| 85 |
String alias = result.getAlias(i); |
| 86 |
Expression e = new ExpressionColumn(getDatabase(), new Column(name, result.getColumnType(i))); |
| 87 |
if (!alias.equals(name)) { |
| 88 |
e = new Alias(e, alias, false); |
| 89 |
} |
| 90 |
expressions[i] = e; |
| 91 |
} |
| 92 |
} else { |
| 93 |
expressions = new Expression[] { expression = expression.optimize(session) }; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public void setExpression(Expression expression) { |
| 98 |
this.expression = expression; |
| 99 |
} |
| 100 |
|
| 101 |
public void setTableFunction(TableFunction tableFunction) { |
| 102 |
this.tableFunction = tableFunction; |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public boolean isQuery() { |
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public boolean isTransactional() { |
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public boolean isReadOnly() { |
| 117 |
return tableFunction == null && expression.isEverything(ExpressionVisitor.READONLY_VISITOR); |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public int getType() { |
| 123 |
return CommandInterface.CALL; |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public boolean isCacheable() { |
| 128 |
return tableFunction == null; |
| 129 |
} |
| 130 |
|
| 131 |
} |