Search in sources :

Example 16 with StatementBuilder

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);
}
Also used : Table(org.h2.table.Table) Index(org.h2.index.Index) ValueString(org.h2.value.ValueString) Cursor(org.h2.index.Cursor) StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) SearchRow(org.h2.result.SearchRow)

Example 17 with StatementBuilder

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);
    }
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbException(org.h2.message.DbException)

Example 18 with StatementBuilder

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);
    }
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) DbException(org.h2.message.DbException)

Example 19 with StatementBuilder

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);
    }
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) DbException(org.h2.message.DbException)

Example 20 with StatementBuilder

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();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Aggregations

StatementBuilder (org.h2.util.StatementBuilder)82 Value (org.h2.value.Value)16 Column (org.h2.table.Column)15 IndexColumn (org.h2.table.IndexColumn)11 PreparedStatement (java.sql.PreparedStatement)9 Expression (org.h2.expression.Expression)9 ValueString (org.h2.value.ValueString)7 Index (org.h2.index.Index)6 DbException (org.h2.message.DbException)6 ResultSet (java.sql.ResultSet)5 Row (org.h2.result.Row)4 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 Constraint (org.h2.constraint.Constraint)3 Database (org.h2.engine.Database)3 ExpressionColumn (org.h2.expression.ExpressionColumn)3 HashMap (java.util.HashMap)2 SelectOrderBy (org.h2.command.dml.SelectOrderBy)2 ResultInterface (org.h2.result.ResultInterface)2 SearchRow (org.h2.result.SearchRow)2