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);
}
}
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);
}
}
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();
}
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);
}
}
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());
}
Aggregations