Search in sources :

Example 26 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.

the class UpdatableRow method deleteRow.

/**
     * Delete the given row in the database.
     *
     * @param current the row
     * @throws SQLException if this row has already been deleted
     */
public void deleteRow(Value[] current) throws SQLException {
    StatementBuilder buff = new StatementBuilder("DELETE FROM ");
    appendTableName(buff);
    appendKeyCondition(buff);
    PreparedStatement prep = conn.prepareStatement(buff.toString());
    setKey(prep, 1, current);
    int count = prep.executeUpdate();
    if (count != 1) {
        // the row has already been deleted
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder) PreparedStatement(java.sql.PreparedStatement)

Example 27 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder 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)

Example 28 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.

the class ValueArray method getSQL.

@Override
public String getSQL() {
    StatementBuilder buff = new StatementBuilder("(");
    for (Value v : values) {
        buff.appendExceptFirst(", ");
        buff.append(v.getSQL());
    }
    if (values.length == 1) {
        buff.append(',');
    }
    return buff.append(')').toString();
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder)

Example 29 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.

the class ValueResultSet method getString.

@Override
public String getString() {
    try {
        StatementBuilder buff = new StatementBuilder("(");
        result.beforeFirst();
        ResultSetMetaData meta = result.getMetaData();
        int columnCount = meta.getColumnCount();
        for (int i = 0; result.next(); i++) {
            if (i > 0) {
                buff.append(", ");
            }
            buff.append('(');
            buff.resetCount();
            for (int j = 0; j < columnCount; j++) {
                buff.appendExceptFirst(", ");
                int t = DataType.getValueTypeFromResultSet(meta, j + 1);
                Value v = DataType.readValue(null, result, j + 1, t);
                buff.append(v.getString());
            }
            buff.append(')');
        }
        result.beforeFirst();
        return buff.append(')').toString();
    } catch (SQLException e) {
        throw DbException.convert(e);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) StatementBuilder(com.wplatform.ddal.util.StatementBuilder)

Example 30 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.

the class BatchUpdateWorker method error.

/**
     * @param e
     */
protected void error(Throwable e) {
    StatementBuilder buff = new StatementBuilder();
    buff.append(shardName).append(" executing batchUpdate error:").append(sql);
    for (List<Value> params : array) {
        if (params != null) {
            if (params != null && params.size() > 0) {
                buff.appendExceptFirst(", ");
                buff.append("\n{");
                int i = 1;
                for (Value v : params) {
                    buff.appendExceptFirst(", ");
                    buff.append(i++).append(": ").append(v.getSQL());
                }
                buff.append('}');
            }
        }
    }
    buff.append(';');
    trace.error(e, buff.toString());
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder) Value(com.wplatform.ddal.value.Value)

Aggregations

StatementBuilder (com.wplatform.ddal.util.StatementBuilder)49 Value (com.wplatform.ddal.value.Value)13 Expression (com.wplatform.ddal.command.expression.Expression)9 Column (com.wplatform.ddal.dbobject.table.Column)8 PreparedStatement (java.sql.PreparedStatement)7 SQLException (java.sql.SQLException)4 IndexColumn (com.wplatform.ddal.dbobject.table.IndexColumn)3 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)3 Connection (java.sql.Connection)3 DataSource (javax.sql.DataSource)3 Index (com.wplatform.ddal.dbobject.index.Index)2 JdbcWorker (com.wplatform.ddal.excutor.JdbcWorker)2 DbException (com.wplatform.ddal.message.DbException)2 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)1 AlterTableAlterColumn (com.wplatform.ddal.command.ddl.AlterTableAlterColumn)1