use of org.h2.util.StatementBuilder in project h2database by h2database.
the class Aggregate method getValue.
@Override
public Value getValue(Session session) {
if (select.isQuickAggregateQuery()) {
switch(type) {
case COUNT:
case COUNT_ALL:
Table table = select.getTopTableFilter().getTable();
return ValueLong.get(table.getRowCount(session));
case MIN:
case MAX:
{
boolean first = type == AggregateType.MIN;
Index index = getMinMaxColumnIndex();
int sortType = index.getIndexColumns()[0].sortType;
if ((sortType & SortOrder.DESCENDING) != 0) {
first = !first;
}
Cursor cursor = index.findFirstOrLast(session, first);
SearchRow row = cursor.getSearchRow();
Value v;
if (row == null) {
v = ValueNull.INSTANCE;
} else {
v = row.getValue(index.getColumns()[0].getColumnId());
}
return v;
}
case MEDIAN:
{
return AggregateDataMedian.getResultFromIndex(session, on, dataType);
}
default:
DbException.throwInternalError("type=" + type);
}
}
HashMap<Expression, Object> group = select.getCurrentGroup();
if (group == null) {
throw DbException.get(ErrorCode.INVALID_USE_OF_AGGREGATE_FUNCTION_1, getSQL());
}
AggregateData data = (AggregateData) group.get(this);
if (data == null) {
data = AggregateData.create(type);
}
if (type == AggregateType.GROUP_CONCAT) {
Value[] array = ((AggregateDataCollecting) data).getArray();
if (array == null) {
return ValueNull.INSTANCE;
}
if (orderByList != null || distinct) {
sortWithOrderBy(array);
}
StatementBuilder buff = new StatementBuilder();
String sep = groupConcatSeparator == null ? "," : groupConcatSeparator.getValue(session).getString();
for (Value val : array) {
String s;
if (val.getType() == Value.ARRAY) {
s = ((ValueArray) val).getList()[0].getString();
} else {
s = val.getString();
}
if (s == null) {
continue;
}
if (sep != null) {
buff.appendExceptFirst(sep);
}
buff.append(s);
}
return ValueString.get(buff.toString());
} else if (type == AggregateType.ARRAY_AGG) {
Value[] array = ((AggregateDataCollecting) data).getArray();
if (array == null) {
return ValueNull.INSTANCE;
}
if (orderByList != null || distinct) {
sortWithOrderBy(array);
}
if (orderByList != null) {
for (int i = 0; i < array.length; i++) {
array[i] = ((ValueArray) array[i]).getList()[0];
}
}
return ValueArray.get(array);
}
return data.getValue(session.getDatabase(), dataType, distinct);
}
use of org.h2.util.StatementBuilder in project h2database by h2database.
the class LinkedIndex method find.
@Override
public Cursor find(Session session, SearchRow first, SearchRow last) {
ArrayList<Value> params = New.arrayList();
StatementBuilder buff = new StatementBuilder("SELECT * FROM ");
buff.append(targetTableName).append(" T");
for (int i = 0; first != null && i < first.getColumnCount(); i++) {
Value v = first.getValue(i);
if (v != null) {
buff.appendOnlyFirst(" WHERE ");
buff.appendExceptFirst(" AND ");
Column col = table.getColumn(i);
buff.append(col.getSQL());
if (v == ValueNull.INSTANCE) {
buff.append(" IS NULL");
} else {
buff.append(">=");
addParameter(buff, col);
params.add(v);
}
}
}
for (int i = 0; last != null && i < last.getColumnCount(); i++) {
Value v = last.getValue(i);
if (v != null) {
buff.appendOnlyFirst(" WHERE ");
buff.appendExceptFirst(" AND ");
Column col = table.getColumn(i);
buff.append(col.getSQL());
if (v == ValueNull.INSTANCE) {
buff.append(" IS NULL");
} else {
buff.append("<=");
addParameter(buff, col);
params.add(v);
}
}
}
String sql = buff.toString();
try {
PreparedStatement prep = link.execute(sql, params, false);
ResultSet rs = prep.getResultSet();
return new LinkedCursor(link, rs, session, sql, prep);
} catch (Exception e) {
throw TableLink.wrapException(sql, e);
}
}
use of org.h2.util.StatementBuilder in project h2database by h2database.
the class LinkedIndex method add.
@Override
public void add(Session session, Row row) {
ArrayList<Value> params = New.arrayList();
StatementBuilder buff = new StatementBuilder("INSERT INTO ");
buff.append(targetTableName).append(" VALUES(");
for (int i = 0; i < row.getColumnCount(); i++) {
Value v = row.getValue(i);
buff.appendExceptFirst(", ");
if (v == null) {
buff.append("DEFAULT");
} else if (isNull(v)) {
buff.append("NULL");
} else {
buff.append('?');
params.add(v);
}
}
buff.append(')');
String sql = buff.toString();
try {
link.execute(sql, params, true);
rowCount++;
} catch (Exception e) {
throw TableLink.wrapException(sql, e);
}
}
use of org.h2.util.StatementBuilder in project h2database by h2database.
the class LinkedIndex method update.
/**
* Update a row using a UPDATE statement. This method is to be called if the
* emit updates option is enabled.
*
* @param oldRow the old data
* @param newRow the new data
*/
public void update(Row oldRow, Row newRow) {
ArrayList<Value> params = New.arrayList();
StatementBuilder buff = new StatementBuilder("UPDATE ");
buff.append(targetTableName).append(" SET ");
for (int i = 0; i < newRow.getColumnCount(); i++) {
buff.appendExceptFirst(", ");
buff.append(table.getColumn(i).getSQL()).append('=');
Value v = newRow.getValue(i);
if (v == null) {
buff.append("DEFAULT");
} else {
buff.append('?');
params.add(v);
}
}
buff.append(" WHERE ");
buff.resetCount();
for (int i = 0; i < oldRow.getColumnCount(); i++) {
Column col = table.getColumn(i);
buff.appendExceptFirst(" AND ");
buff.append(col.getSQL());
Value v = oldRow.getValue(i);
if (isNull(v)) {
buff.append(" IS NULL");
} else {
buff.append('=');
params.add(v);
addParameter(buff, col);
}
}
String sql = buff.toString();
try {
link.execute(sql, params, true);
} catch (Exception e) {
throw TableLink.wrapException(sql, e);
}
}
use of org.h2.util.StatementBuilder in project h2database by h2database.
the class JavaAggregate method getSQL.
@Override
public String getSQL() {
StatementBuilder buff = new StatementBuilder();
buff.append(Parser.quoteIdentifier(userAggregate.getName())).append('(');
for (Expression e : args) {
buff.appendExceptFirst(", ");
buff.append(e.getSQL());
}
buff.append(')');
if (filterCondition != null) {
buff.append(" FILTER (WHERE ").append(filterCondition.getSQL()).append(')');
}
return buff.toString();
}
Aggregations