use of org.h2.expression.ExpressionColumn in project h2database by h2database.
the class Explain method query.
@Override
public ResultInterface query(int maxrows) {
Column column = new Column("PLAN", Value.STRING);
Database db = session.getDatabase();
ExpressionColumn expr = new ExpressionColumn(db, column);
Expression[] expressions = { expr };
result = new LocalResult(session, expressions, 1);
if (maxrows >= 0) {
String plan;
if (executeCommand) {
PageStore store = null;
Store mvStore = null;
if (db.isPersistent()) {
store = db.getPageStore();
if (store != null) {
store.statisticsStart();
}
mvStore = db.getMvStore();
if (mvStore != null) {
mvStore.statisticsStart();
}
}
if (command.isQuery()) {
command.query(maxrows);
} else {
command.update();
}
plan = command.getPlanSQL();
Map<String, Integer> statistics = null;
if (store != null) {
statistics = store.statisticsEnd();
} else if (mvStore != null) {
statistics = mvStore.statisticsEnd();
}
if (statistics != null) {
int total = 0;
for (Entry<String, Integer> e : statistics.entrySet()) {
total += e.getValue();
}
if (total > 0) {
statistics = new TreeMap<>(statistics);
StringBuilder buff = new StringBuilder();
if (statistics.size() > 1) {
buff.append("total: ").append(total).append('\n');
}
for (Entry<String, Integer> e : statistics.entrySet()) {
int value = e.getValue();
int percent = (int) (100L * value / total);
buff.append(e.getKey()).append(": ").append(value);
if (statistics.size() > 1) {
buff.append(" (").append(percent).append("%)");
}
buff.append('\n');
}
plan += "\n/*\n" + buff.toString() + "*/";
}
}
} else {
plan = command.getPlanSQL();
}
add(plan);
}
result.done();
return result;
}
use of org.h2.expression.ExpressionColumn in project h2database by h2database.
the class GeneratedKeys method getKeys.
/**
* Returns generated keys.
*
* @param session
* session
* @return local result with generated keys
*/
public LocalResult getKeys(Session session) {
Database db = session == null ? null : session.getDatabase();
if (Boolean.FALSE.equals(generatedKeysRequest)) {
clear(null);
return new LocalResult();
}
ArrayList<ExpressionColumn> expressionColumns;
if (Boolean.TRUE.equals(generatedKeysRequest)) {
expressionColumns = new ArrayList<>(allColumns.size());
for (Column column : allColumns) {
expressionColumns.add(new ExpressionColumn(db, column));
}
} else if (generatedKeysRequest instanceof int[]) {
if (table != null) {
int[] indices = (int[]) generatedKeysRequest;
Column[] columns = table.getColumns();
int cnt = columns.length;
allColumns.clear();
expressionColumns = new ArrayList<>(indices.length);
for (int idx : indices) {
if (idx >= 1 && idx <= cnt) {
Column column = columns[idx - 1];
expressionColumns.add(new ExpressionColumn(db, column));
allColumns.add(column);
}
}
} else {
clear(null);
return new LocalResult();
}
} else if (generatedKeysRequest instanceof String[]) {
if (table != null) {
String[] names = (String[]) generatedKeysRequest;
allColumns.clear();
expressionColumns = new ArrayList<>(names.length);
for (String name : names) {
Column column;
search: if (table.doesColumnExist(name)) {
column = table.getColumn(name);
} else {
name = StringUtils.toUpperEnglish(name);
if (table.doesColumnExist(name)) {
column = table.getColumn(name);
} else {
for (Column c : table.getColumns()) {
if (c.getName().equalsIgnoreCase(name)) {
column = c;
break search;
}
}
continue;
}
}
expressionColumns.add(new ExpressionColumn(db, column));
allColumns.add(column);
}
} else {
clear(null);
return new LocalResult();
}
} else {
clear(null);
return new LocalResult();
}
int columnCount = expressionColumns.size();
if (columnCount == 0) {
clear(null);
return new LocalResult();
}
LocalResult result = new LocalResult(session, expressionColumns.toArray(new Expression[0]), columnCount);
for (Map<Column, Value> map : data) {
Value[] row = new Value[columnCount];
for (Map.Entry<Column, Value> entry : map.entrySet()) {
int idx = allColumns.indexOf(entry.getKey());
if (idx >= 0) {
row[idx] = entry.getValue();
}
}
for (int i = 0; i < columnCount; i++) {
if (row[i] == null) {
row[i] = ValueNull.INSTANCE;
}
}
result.addRow(row);
}
clear(null);
return result;
}
use of org.h2.expression.ExpressionColumn in project h2database by h2database.
the class Expression method getExpressionColumns.
/**
* Extracts expression columns from ValueArray
*
* @param session the current session
* @param value the value to extract columns from
* @return array of expression columns
*/
static Expression[] getExpressionColumns(Session session, ValueArray value) {
Value[] list = value.getList();
ExpressionColumn[] expr = new ExpressionColumn[list.length];
for (int i = 0, len = list.length; i < len; i++) {
Value v = list[i];
Column col = new Column("C" + (i + 1), v.getType(), v.getPrecision(), v.getScale(), v.getDisplaySize());
expr[i] = new ExpressionColumn(session.getDatabase(), col);
}
return expr;
}
use of org.h2.expression.ExpressionColumn in project h2database by h2database.
the class ExpressionList method getExpressionColumns.
@Override
public Expression[] getExpressionColumns(Session session) {
ExpressionColumn[] expr = new ExpressionColumn[list.length];
for (int i = 0; i < list.length; i++) {
Expression e = list[i];
Column col = new Column("C" + (i + 1), e.getType(), e.getPrecision(), e.getScale(), e.getDisplaySize());
expr[i] = new ExpressionColumn(session.getDatabase(), col);
}
return expr;
}
use of org.h2.expression.ExpressionColumn in project h2database by h2database.
the class Function method getSequence.
private Sequence getSequence(Session session, Value v0, Value v1) {
String schemaName, sequenceName;
if (v1 == null) {
Parser p = new Parser(session);
String sql = v0.getString();
Expression expr = p.parseExpression(sql);
if (expr instanceof ExpressionColumn) {
ExpressionColumn seq = (ExpressionColumn) expr;
schemaName = seq.getOriginalTableAliasName();
if (schemaName == null) {
schemaName = session.getCurrentSchemaName();
sequenceName = sql;
} else {
sequenceName = seq.getColumnName();
}
} else {
throw DbException.getSyntaxError(sql, 1);
}
} else {
schemaName = v0.getString();
sequenceName = v1.getString();
}
Schema s = database.findSchema(schemaName);
if (s == null) {
schemaName = StringUtils.toUpperEnglish(schemaName);
s = database.getSchema(schemaName);
}
Sequence seq = s.findSequence(sequenceName);
if (seq == null) {
sequenceName = StringUtils.toUpperEnglish(sequenceName);
seq = s.getSequence(sequenceName);
}
return seq;
}
Aggregations