@Override public Row get() { if (current == null) { current = createRow(); for (int i = 0; i < current.getColumnCount(); i++) { Value v = DataType.readValue(rs, i + 1, cols[i].getType()); current.setValue(i, v); } } return current; }
@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(result, j + 1, t); buff.append(v.getString()); } buff.append(')'); } result.beforeFirst(); return buff.append(')').toString(); } catch (SQLException e) { throw DbException.convert(e); } }
/** * Construct a local result set by reading all data from a regular result * set. * * @param session the session * @param rs the result set * @param maxrows the maximum number of rows to read (0 for no limit) * @return the local result set */ public static LocalResult read(Session session, ResultSet rs, int maxrows) { Expression[] cols = Expression.getExpressionColumns(session, rs); int columnCount = cols.length; LocalResult result = new LocalResult(session, cols, columnCount); try { for (int i = 0; (maxrows == 0 || i < maxrows) && rs.next(); i++) { Value[] list = new Value[columnCount]; for (int j = 0; j < columnCount; j++) { int type = result.getColumnType(j); list[j] = DataType.readValue(rs, j + 1, type); } result.addRow(list); } } catch (SQLException e) { throw DbException.convert(e); } result.done(); return result; }
/** * 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(rs, i + 1, type); } return newRow; }