use of org.h2.util.StatementBuilder in project h2database by h2database.
the class RowImpl method toString.
@Override
public String toString() {
StatementBuilder buff = new StatementBuilder("( /* key:");
buff.append(getKey());
if (version != 0) {
buff.append(" v:").append(version);
}
if (isDeleted()) {
buff.append(" deleted");
}
buff.append(" */ ");
if (data != null) {
for (Value v : data) {
buff.appendExceptFirst(", ");
buff.append(v == null ? "null" : v.getTraceSQL());
}
}
return buff.append(')').toString();
}
use of org.h2.util.StatementBuilder in project h2database by h2database.
the class SortOrder method getSQL.
/**
* Create the SQL snippet that describes this sort order.
* This is the SQL snippet that usually appears after the ORDER BY clause.
*
* @param list the expression list
* @param visible the number of columns in the select list
* @return the SQL snippet
*/
public String getSQL(Expression[] list, int visible) {
StatementBuilder buff = new StatementBuilder();
int i = 0;
for (int idx : queryColumnIndexes) {
buff.appendExceptFirst(", ");
if (idx < visible) {
buff.append(idx + 1);
} else {
buff.append('=').append(StringUtils.unEnclose(list[idx].getSQL()));
}
int type = sortTypes[i++];
if ((type & DESCENDING) != 0) {
buff.append(" DESC");
}
if ((type & NULLS_FIRST) != 0) {
buff.append(" NULLS FIRST");
} else if ((type & NULLS_LAST) != 0) {
buff.append(" NULLS LAST");
}
}
return buff.toString();
}
use of org.h2.util.StatementBuilder in project h2database by h2database.
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);
}
}
use of org.h2.util.StatementBuilder in project h2database by h2database.
the class Command method select.
private Result select(DbInterface db) throws SQLException {
StatementBuilder buff = new StatementBuilder("SELECT ");
for (String s : selectList) {
buff.appendExceptFirst(", ");
buff.append(s);
}
buff.append(" FROM ").append(table.getName()).append(" M").append(' ').append(join);
if (condition != null) {
buff.append(" WHERE ").append(condition);
}
if (order.trim().length() > 0) {
buff.append(" ORDER BY ").append(order);
}
return db.select(buff.toString());
}
use of org.h2.util.StatementBuilder in project h2database by h2database.
the class TableDefinition method delete.
void delete(Db db, Object obj) {
if (primaryKeyColumnNames == null || primaryKeyColumnNames.size() == 0) {
throw new IllegalStateException("No primary key columns defined " + "for table " + obj.getClass() + " - no update possible");
}
SQLStatement stat = new SQLStatement(db);
StatementBuilder buff = new StatementBuilder("DELETE FROM ");
buff.append(db.getDialect().getTableName(schemaName, tableName));
buff.resetCount();
Object alias = ClassUtils.newObject(obj.getClass());
Query<Object> query = Query.from(db, alias);
boolean firstCondition = true;
for (FieldDefinition field : fields) {
if (field.isPrimaryKey) {
Object aliasValue = field.getValue(alias);
Object value = field.getValue(obj);
if (!firstCondition) {
query.addConditionToken(ConditionAndOr.AND);
}
firstCondition = false;
query.addConditionToken(new Condition<>(aliasValue, value, CompareType.EQUAL));
}
}
stat.setSQL(buff.toString());
query.appendWhere(stat);
StatementLogger.delete(stat.getSQL());
stat.executeUpdate();
}
Aggregations