use of org.h2.mvstore.MVMap.Builder in project h2database by h2database.
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
* @throws SQLException on failure
*/
public Value[] readRow(Value[] row) throws SQLException {
StringBuilder builder = new StringBuilder("SELECT ");
appendColumnList(builder, false);
builder.append(" FROM ");
appendTableName(builder);
appendKeyCondition(builder);
PreparedStatement prep = conn.prepareStatement(builder.toString());
setKey(prep, 1, row);
JdbcResultSet rs = (JdbcResultSet) prep.executeQuery();
if (!rs.next()) {
throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
}
Value[] newRow = new Value[columnCount];
for (int i = 0; i < columnCount; i++) {
newRow[i] = ValueToObjectConverter.readValue(conn.getSession(), rs, i + 1);
}
return newRow;
}
use of org.h2.mvstore.MVMap.Builder in project h2database by h2database.
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 {
StringBuilder builder = new StringBuilder("INSERT INTO ");
appendTableName(builder);
builder.append('(');
appendColumnList(builder, false);
builder.append(")VALUES(");
for (int i = 0; i < columnCount; i++) {
if (i > 0) {
builder.append(',');
}
Value v = row[i];
if (v == null) {
builder.append("DEFAULT");
} else {
builder.append('?');
}
}
builder.append(')');
PreparedStatement prep = conn.prepareStatement(builder.toString());
for (int i = 0, j = 0; i < columnCount; i++) {
Value v = row[i];
if (v != null) {
JdbcUtils.set(prep, j++ + 1, v, conn);
}
}
int count = prep.executeUpdate();
if (count != 1) {
throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
}
}
use of org.h2.mvstore.MVMap.Builder in project h2database by h2database.
the class DefaultRow method toString.
@Override
public String toString() {
StringBuilder builder = new StringBuilder("( /* key:").append(key).append(" */ ");
for (int i = 0, length = data.length; i < length; i++) {
if (i > 0) {
builder.append(", ");
}
Value v = data[i];
builder.append(v == null ? "null" : v.getTraceSQL());
}
return builder.append(')').toString();
}
use of org.h2.mvstore.MVMap.Builder in project h2database by h2database.
the class Aggregate method getListaggTruncate.
private StringBuilder getListaggTruncate(Value[] array, String separator, String filter, boolean withoutCount) {
int count = array.length;
String[] strings = new String[count];
String s = getListaggItem(array[0]);
strings[0] = s;
StringBuilder builder = new StringBuilder(s);
loop: for (int i = 1; i < count; i++) {
builder.append(separator).append(strings[i] = s = getListaggItem(array[i]));
int length = builder.length();
if (length > Constants.MAX_STRING_LENGTH) {
for (; i > 0; i--) {
length -= strings[i].length();
builder.setLength(length);
builder.append(filter);
if (!withoutCount) {
builder.append('(').append(count - i).append(')');
}
if (builder.length() <= Constants.MAX_STRING_LENGTH) {
break loop;
}
length -= separator.length();
}
builder.setLength(0);
builder.append(filter).append('(').append(count).append(')');
break;
}
}
return builder;
}
use of org.h2.mvstore.MVMap.Builder in project h2database by h2database.
the class ConcatenationOperation method getValue.
private Value getValue(SessionLocal session, int l) {
Value[] values = new Value[l];
for (int i = 0; i < l; i++) {
Value v = args[i].getValue(session).convertTo(type, session);
if (v == ValueNull.INSTANCE) {
return ValueNull.INSTANCE;
}
values[i] = v;
}
int valueType = type.getValueType();
if (valueType == Value.VARCHAR) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < l; i++) {
builder.append(values[i].getString());
}
return ValueVarchar.get(builder.toString(), session);
} else if (valueType == Value.VARBINARY) {
int totalLength = 0;
for (int i = 0; i < l; i++) {
totalLength += values[i].getBytesNoCopy().length;
}
byte[] v = new byte[totalLength];
int offset = 0;
for (int i = 0; i < l; i++) {
byte[] a = values[i].getBytesNoCopy();
int length = a.length;
System.arraycopy(a, 0, v, offset, length);
offset += length;
}
return ValueVarbinary.getNoCopy(v);
} else {
int totalLength = 0;
for (int i = 0; i < l; i++) {
totalLength += ((ValueArray) values[i]).getList().length;
}
Value[] v = new Value[totalLength];
int offset = 0;
for (int i = 0; i < l; i++) {
Value[] a = ((ValueArray) values[i]).getList();
int length = a.length;
System.arraycopy(a, 0, v, offset, length);
offset += length;
}
return ValueArray.get((TypeInfo) type.getExtTypeInfo(), v, session);
}
}
Aggregations