Search in sources :

Example 31 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class SelectExecutor method queryGroupSorted.

private void queryGroupSorted(int columnCount, ResultTarget result) {
    int rowNumber = 0;
    prepared.setCurrentRowNumber(0);
    prepared.setCurrentGroup(null);
    Value[] previousKeyValues = null;
    while (topTableFilter.next()) {
        // for rownum expression
        prepared.setCurrentRowNumber(rowNumber + 1);
        if (condition == null || Boolean.TRUE.equals(condition.getBooleanValue(session))) {
            rowNumber++;
            Value[] keyValues = new Value[groupIndex.length];
            // update group
            for (int i = 0; i < groupIndex.length; i++) {
                int idx = groupIndex[i];
                Expression expr = expressions.get(idx);
                keyValues[i] = expr.getValue(session);
            }
            if (previousKeyValues == null) {
                previousKeyValues = keyValues;
                prepared.setCurrentGroup(New.<Expression, Object>hashMap());
            } else if (!Arrays.equals(previousKeyValues, keyValues)) {
                addGroupSortedRow(previousKeyValues, columnCount, result);
                previousKeyValues = keyValues;
                prepared.setCurrentGroup(New.<Expression, Object>hashMap());
            }
            prepared.increaseCurrentGroupRowId();
            for (int i = 0; i < columnCount; i++) {
                if (groupByExpression == null || !groupByExpression[i]) {
                    Expression expr = expressions.get(i);
                    expr.updateAggregate(session);
                }
            }
        }
    }
    if (previousKeyValues != null) {
        addGroupSortedRow(previousKeyValues, columnCount, result);
    }
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) Value(com.wplatform.ddal.value.Value)

Example 32 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class LocalResult method containsDistinct.

/**
 * Check if this result set contains the given row.
 *
 * @param values the row
 * @return true if the row exists
 */
public boolean containsDistinct(Value[] values) {
    if (external != null) {
        return external.contains(values);
    }
    if (distinctRows == null) {
        distinctRows = ValueHashMap.newInstance();
        for (Value[] row : rows) {
            if (row.length > visibleColumnCount) {
                Value[] r2 = new Value[visibleColumnCount];
                System.arraycopy(row, 0, r2, 0, visibleColumnCount);
                row = r2;
            }
            ValueArray array = ValueArray.get(row);
            distinctRows.put(array, row);
        }
    }
    ValueArray array = ValueArray.get(values);
    return distinctRows.get(array) != null;
}
Also used : Value(com.wplatform.ddal.value.Value) ValueArray(com.wplatform.ddal.value.ValueArray)

Example 33 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class UpdatableRow method updateRow.

/**
 * Update a row in the database.
 *
 * @param current   the old row
 * @param updateRow the new row
 * @throws SQLException if the row has been deleted
 */
public void updateRow(Value[] current, Value[] updateRow) throws SQLException {
    StatementBuilder buff = new StatementBuilder("UPDATE ");
    appendTableName(buff);
    buff.append(" SET ");
    appendColumnList(buff, true);
    // TODO updatable result set: we could add all current values to the
    // where clause
    // - like this optimistic ('no') locking is possible
    appendKeyCondition(buff);
    PreparedStatement prep = conn.prepareStatement(buff.toString());
    int j = 1;
    for (int i = 0; i < columnCount; i++) {
        Value v = updateRow[i];
        if (v == null) {
            v = current[i];
        }
        v.set(prep, j++);
    }
    setKey(prep, j, current);
    int count = prep.executeUpdate();
    if (count != 1) {
        // the row has been deleted
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder) Value(com.wplatform.ddal.value.Value) PreparedStatement(java.sql.PreparedStatement)

Example 34 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class UpdatableRow method readRow.

/**
 * Re-reads a row from the database and updates the values in the array.
 *
 * @param row the values that contain the key
 * @return the row
 */
public Value[] readRow(Value[] row) throws SQLException {
    StatementBuilder buff = new StatementBuilder("SELECT ");
    appendColumnList(buff, false);
    buff.append(" FROM ");
    appendTableName(buff);
    appendKeyCondition(buff);
    PreparedStatement prep = conn.prepareStatement(buff.toString());
    setKey(prep, 1, row);
    ResultSet rs = prep.executeQuery();
    if (!rs.next()) {
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
    Value[] newRow = new Value[columnCount];
    for (int i = 0; i < columnCount; i++) {
        int type = result.getColumnType(i);
        newRow[i] = DataType.readValue(conn.getSession(), rs, i + 1, type);
    }
    return newRow;
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder) ResultSet(java.sql.ResultSet) Value(com.wplatform.ddal.value.Value) PreparedStatement(java.sql.PreparedStatement)

Example 35 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class UpdatableRow method insertRow.

/**
 * Insert a new row into the database.
 *
 * @param row the new row
 * @throws SQLException if the row could not be inserted
 */
public void insertRow(Value[] row) throws SQLException {
    StatementBuilder buff = new StatementBuilder("INSERT INTO ");
    appendTableName(buff);
    buff.append('(');
    appendColumnList(buff, false);
    buff.append(")VALUES(");
    buff.resetCount();
    for (int i = 0; i < columnCount; i++) {
        buff.appendExceptFirst(",");
        Value v = row[i];
        if (v == null) {
            buff.append("DEFAULT");
        } else {
            buff.append('?');
        }
    }
    buff.append(')');
    PreparedStatement prep = conn.prepareStatement(buff.toString());
    for (int i = 0, j = 0; i < columnCount; i++) {
        Value v = row[i];
        if (v != null) {
            v.set(prep, j++ + 1);
        }
    }
    int count = prep.executeUpdate();
    if (count != 1) {
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder) Value(com.wplatform.ddal.value.Value) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Value (com.wplatform.ddal.value.Value)84 Expression (com.wplatform.ddal.command.expression.Expression)14 Column (com.wplatform.ddal.dbobject.table.Column)14 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)13 DbException (com.wplatform.ddal.message.DbException)9 SQLException (java.sql.SQLException)8 Row (com.wplatform.ddal.result.Row)7 SearchRow (com.wplatform.ddal.result.SearchRow)7 PreparedStatement (java.sql.PreparedStatement)7 TableMate (com.wplatform.ddal.dbobject.table.TableMate)6 LocalResult (com.wplatform.ddal.result.LocalResult)6 ResultInterface (com.wplatform.ddal.result.ResultInterface)5 Connection (java.sql.Connection)5 Parameter (com.wplatform.ddal.command.expression.Parameter)4 List (java.util.List)4 DataSource (javax.sql.DataSource)4 Query (com.wplatform.ddal.command.dml.Query)3 TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)3 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)3 JdbcWorker (com.wplatform.ddal.excutor.JdbcWorker)3