use of org.h2.tools.SimpleResultSet in project h2database by h2database.
the class WebApp method getResultSet.
private String getResultSet(String sql, ResultSet rs, boolean metadata, boolean list, boolean edit, long time, boolean allowEdit) throws SQLException {
int maxrows = getMaxrows();
time = System.currentTimeMillis() - time;
StringBuilder buff = new StringBuilder();
if (edit) {
buff.append("<form id=\"editing\" name=\"editing\" method=\"post\" " + "action=\"editResult.do?jsessionid=${sessionId}\" " + "id=\"mainForm\" target=\"h2result\">" + "<input type=\"hidden\" name=\"op\" value=\"1\" />" + "<input type=\"hidden\" name=\"row\" value=\"\" />" + "<table cellspacing=0 cellpadding=0 id=\"editTable\">");
} else {
buff.append("<table cellspacing=0 cellpadding=0>");
}
if (metadata) {
SimpleResultSet r = new SimpleResultSet();
r.addColumn("#", Types.INTEGER, 0, 0);
r.addColumn("label", Types.VARCHAR, 0, 0);
r.addColumn("catalog", Types.VARCHAR, 0, 0);
r.addColumn("schema", Types.VARCHAR, 0, 0);
r.addColumn("table", Types.VARCHAR, 0, 0);
r.addColumn("column", Types.VARCHAR, 0, 0);
r.addColumn("type", Types.INTEGER, 0, 0);
r.addColumn("typeName", Types.VARCHAR, 0, 0);
r.addColumn("class", Types.VARCHAR, 0, 0);
r.addColumn("precision", Types.INTEGER, 0, 0);
r.addColumn("scale", Types.INTEGER, 0, 0);
r.addColumn("displaySize", Types.INTEGER, 0, 0);
r.addColumn("autoIncrement", Types.BOOLEAN, 0, 0);
r.addColumn("caseSensitive", Types.BOOLEAN, 0, 0);
r.addColumn("currency", Types.BOOLEAN, 0, 0);
r.addColumn("nullable", Types.INTEGER, 0, 0);
r.addColumn("readOnly", Types.BOOLEAN, 0, 0);
r.addColumn("searchable", Types.BOOLEAN, 0, 0);
r.addColumn("signed", Types.BOOLEAN, 0, 0);
r.addColumn("writable", Types.BOOLEAN, 0, 0);
r.addColumn("definitelyWritable", Types.BOOLEAN, 0, 0);
ResultSetMetaData m = rs.getMetaData();
for (int i = 1; i <= m.getColumnCount(); i++) {
r.addRow(i, m.getColumnLabel(i), m.getCatalogName(i), m.getSchemaName(i), m.getTableName(i), m.getColumnName(i), m.getColumnType(i), m.getColumnTypeName(i), m.getColumnClassName(i), m.getPrecision(i), m.getScale(i), m.getColumnDisplaySize(i), m.isAutoIncrement(i), m.isCaseSensitive(i), m.isCurrency(i), m.isNullable(i), m.isReadOnly(i), m.isSearchable(i), m.isSigned(i), m.isWritable(i), m.isDefinitelyWritable(i));
}
rs = r;
}
ResultSetMetaData meta = rs.getMetaData();
int columns = meta.getColumnCount();
int rows = 0;
if (list) {
buff.append("<tr><th>Column</th><th>Data</th></tr><tr>");
while (rs.next()) {
if (maxrows > 0 && rows >= maxrows) {
break;
}
rows++;
buff.append("<tr><td>Row #</td><td>").append(rows).append("</tr>");
for (int i = 0; i < columns; i++) {
buff.append("<tr><td>").append(PageParser.escapeHtml(meta.getColumnLabel(i + 1))).append("</td><td>").append(escapeData(rs, i + 1)).append("</td></tr>");
}
}
} else {
buff.append("<tr>");
if (edit) {
buff.append("<th>${text.resultEdit.action}</th>");
}
for (int i = 0; i < columns; i++) {
buff.append("<th>").append(PageParser.escapeHtml(meta.getColumnLabel(i + 1))).append("</th>");
}
buff.append("</tr>");
while (rs.next()) {
if (maxrows > 0 && rows >= maxrows) {
break;
}
rows++;
buff.append("<tr>");
if (edit) {
buff.append("<td>").append("<img onclick=\"javascript:editRow(").append(rs.getRow()).append(",'${sessionId}', '${text.resultEdit.save}', " + "'${text.resultEdit.cancel}'").append(")\" width=16 height=16 src=\"ico_write.gif\" " + "onmouseover = \"this.className ='icon_hover'\" " + "onmouseout = \"this.className ='icon'\" " + "class=\"icon\" alt=\"${text.resultEdit.edit}\" " + "title=\"${text.resultEdit.edit}\" border=\"1\"/>").append("<img onclick=\"javascript:deleteRow(").append(rs.getRow()).append(",'${sessionId}', '${text.resultEdit.delete}', " + "'${text.resultEdit.cancel}'").append(")\" width=16 height=16 src=\"ico_remove.gif\" " + "onmouseover = \"this.className ='icon_hover'\" " + "onmouseout = \"this.className ='icon'\" " + "class=\"icon\" alt=\"${text.resultEdit.delete}\" " + "title=\"${text.resultEdit.delete}\" border=\"1\" /></a>").append("</td>");
}
for (int i = 0; i < columns; i++) {
buff.append("<td>").append(escapeData(rs, i + 1)).append("</td>");
}
buff.append("</tr>");
}
}
boolean isUpdatable = false;
try {
if (!session.getContents().isDB2()) {
isUpdatable = rs.getConcurrency() == ResultSet.CONCUR_UPDATABLE && rs.getType() != ResultSet.TYPE_FORWARD_ONLY;
}
} catch (NullPointerException e) {
// ignore
// workaround for a JDBC-ODBC bridge problem
}
if (edit) {
ResultSet old = session.result;
if (old != null) {
old.close();
}
session.result = rs;
} else {
rs.close();
}
if (edit) {
buff.append("<tr><td>").append("<img onclick=\"javascript:editRow(-1, " + "'${sessionId}', '${text.resultEdit.save}', '${text.resultEdit.cancel}'").append(")\" width=16 height=16 src=\"ico_add.gif\" " + "onmouseover = \"this.className ='icon_hover'\" " + "onmouseout = \"this.className ='icon'\" " + "class=\"icon\" alt=\"${text.resultEdit.add}\" " + "title=\"${text.resultEdit.add}\" border=\"1\"/>").append("</td>");
for (int i = 0; i < columns; i++) {
buff.append("<td></td>");
}
buff.append("</tr>");
}
buff.append("</table>");
if (edit) {
buff.append("</form>");
}
if (rows == 0) {
buff.append("(${text.result.noRows}");
} else if (rows == 1) {
buff.append("(${text.result.1row}");
} else {
buff.append('(').append(rows).append(" ${text.result.rows}");
}
buff.append(", ");
time = System.currentTimeMillis() - time;
buff.append(time).append(" ms)");
if (!edit && isUpdatable && allowEdit) {
buff.append("<br /><br />" + "<form name=\"editResult\" method=\"post\" " + "action=\"query.do?jsessionid=${sessionId}\" target=\"h2result\">" + "<input type=\"submit\" class=\"button\" " + "value=\"${text.resultEdit.editResult}\" />" + "<input type=\"hidden\" name=\"sql\" value=\"@edit ").append(PageParser.escapeHtmlData(sql)).append("\" /></form>");
}
return buff.toString();
}
use of org.h2.tools.SimpleResultSet in project h2database by h2database.
the class Data method readValue.
/**
* Read a value.
*
* @return the value
*/
public Value readValue() {
int type = data[pos++] & 255;
switch(type) {
case Value.NULL:
return ValueNull.INSTANCE;
case BOOLEAN_TRUE:
return ValueBoolean.TRUE;
case BOOLEAN_FALSE:
return ValueBoolean.FALSE;
case INT_NEG:
return ValueInt.get(-readVarInt());
case Value.ENUM:
case Value.INT:
return ValueInt.get(readVarInt());
case LONG_NEG:
return ValueLong.get(-readVarLong());
case Value.LONG:
return ValueLong.get(readVarLong());
case Value.BYTE:
return ValueByte.get(readByte());
case Value.SHORT:
return ValueShort.get(readShortInt());
case DECIMAL_0_1:
return (ValueDecimal) ValueDecimal.ZERO;
case DECIMAL_0_1 + 1:
return (ValueDecimal) ValueDecimal.ONE;
case DECIMAL_SMALL_0:
return ValueDecimal.get(BigDecimal.valueOf(readVarLong()));
case DECIMAL_SMALL:
{
int scale = readVarInt();
return ValueDecimal.get(BigDecimal.valueOf(readVarLong(), scale));
}
case Value.DECIMAL:
{
int scale = readVarInt();
int len = readVarInt();
byte[] buff = Utils.newBytes(len);
read(buff, 0, len);
BigInteger b = new BigInteger(buff);
return ValueDecimal.get(new BigDecimal(b, scale));
}
case LOCAL_DATE:
{
return ValueDate.fromDateValue(readVarLong());
}
case Value.DATE:
{
long x = readVarLong() * MILLIS_PER_MINUTE;
return ValueDate.fromMillis(DateTimeUtils.getTimeUTCWithoutDst(x));
}
case LOCAL_TIME:
{
long nanos = readVarLong() * 1_000_000 + readVarLong();
return ValueTime.fromNanos(nanos);
}
case Value.TIME:
// need to normalize the year, month and day
return ValueTime.fromMillis(DateTimeUtils.getTimeUTCWithoutDst(readVarLong()));
case LOCAL_TIMESTAMP:
{
long dateValue = readVarLong();
long nanos = readVarLong() * 1_000_000 + readVarLong();
return ValueTimestamp.fromDateValueAndNanos(dateValue, nanos);
}
case Value.TIMESTAMP:
{
return ValueTimestamp.fromMillisNanos(DateTimeUtils.getTimeUTCWithoutDst(readVarLong()), readVarInt());
}
case Value.TIMESTAMP_TZ:
{
long dateValue = readVarLong();
long nanos = readVarLong();
short tz = (short) readVarInt();
return ValueTimestampTimeZone.fromDateValueAndNanos(dateValue, nanos, tz);
}
case Value.BYTES:
{
int len = readVarInt();
byte[] b = Utils.newBytes(len);
read(b, 0, len);
return ValueBytes.getNoCopy(b);
}
case Value.GEOMETRY:
{
int len = readVarInt();
byte[] b = Utils.newBytes(len);
read(b, 0, len);
return ValueGeometry.get(b);
}
case Value.JAVA_OBJECT:
{
int len = readVarInt();
byte[] b = Utils.newBytes(len);
read(b, 0, len);
return ValueJavaObject.getNoCopy(null, b, handler);
}
case Value.UUID:
return ValueUuid.get(readLong(), readLong());
case Value.STRING:
return ValueString.get(readString());
case Value.STRING_IGNORECASE:
return ValueStringIgnoreCase.get(readString());
case Value.STRING_FIXED:
return ValueStringFixed.get(readString());
case FLOAT_0_1:
return ValueFloat.get(0);
case FLOAT_0_1 + 1:
return ValueFloat.get(1);
case DOUBLE_0_1:
return ValueDouble.get(0);
case DOUBLE_0_1 + 1:
return ValueDouble.get(1);
case Value.DOUBLE:
return ValueDouble.get(Double.longBitsToDouble(Long.reverse(readVarLong())));
case Value.FLOAT:
return ValueFloat.get(Float.intBitsToFloat(Integer.reverse(readVarInt())));
case Value.BLOB:
case Value.CLOB:
{
int smallLen = readVarInt();
if (smallLen >= 0) {
byte[] small = Utils.newBytes(smallLen);
read(small, 0, smallLen);
return ValueLobDb.createSmallLob(type, small);
} else if (smallLen == -3) {
int tableId = readVarInt();
long lobId = readVarLong();
long precision = readVarLong();
return ValueLobDb.create(type, handler, tableId, lobId, null, precision);
} else {
int tableId = readVarInt();
int objectId = readVarInt();
long precision = 0;
boolean compression = false;
// including file name)
if (smallLen == -1 || smallLen == -2) {
precision = readVarLong();
compression = readByte() == 1;
}
if (smallLen == -2) {
String filename = readString();
return ValueLob.openUnlinked(type, handler, tableId, objectId, precision, compression, filename);
}
return ValueLob.openLinked(type, handler, tableId, objectId, precision, compression);
}
}
case Value.ARRAY:
{
int len = readVarInt();
Value[] list = new Value[len];
for (int i = 0; i < len; i++) {
list[i] = readValue();
}
return ValueArray.get(list);
}
case Value.RESULT_SET:
{
SimpleResultSet rs = new SimpleResultSet();
rs.setAutoClose(false);
int columns = readVarInt();
for (int i = 0; i < columns; i++) {
rs.addColumn(readString(), readVarInt(), readVarInt(), readVarInt());
}
while (readByte() != 0) {
Object[] o = new Object[columns];
for (int i = 0; i < columns; i++) {
o[i] = readValue().getObject();
}
rs.addRow(o);
}
return ValueResultSet.get(rs);
}
default:
if (type >= INT_0_15 && type < INT_0_15 + 16) {
return ValueInt.get(type - INT_0_15);
} else if (type >= LONG_0_7 && type < LONG_0_7 + 8) {
return ValueLong.get(type - LONG_0_7);
} else if (type >= BYTES_0_31 && type < BYTES_0_31 + 32) {
int len = type - BYTES_0_31;
byte[] b = Utils.newBytes(len);
read(b, 0, len);
return ValueBytes.getNoCopy(b);
} else if (type >= STRING_0_31 && type < STRING_0_31 + 32) {
return ValueString.get(readString(type - STRING_0_31));
}
throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "type: " + type);
}
}
use of org.h2.tools.SimpleResultSet in project h2database by h2database.
the class LinkSchema method linkSchema.
/**
* Link all tables of a schema to the database.
*
* @param conn the connection to the database where the links are to be
* created
* @param targetSchema the schema name where the objects should be created
* @param driver the driver class name of the linked database
* @param url the database URL of the linked database
* @param user the user name
* @param password the password
* @param sourceSchema the schema where the existing tables are
* @return a result set with the created tables
*/
public static ResultSet linkSchema(Connection conn, String targetSchema, String driver, String url, String user, String password, String sourceSchema) {
Connection c2 = null;
Statement stat = null;
ResultSet rs = null;
SimpleResultSet result = new SimpleResultSet();
result.setAutoClose(false);
result.addColumn("TABLE_NAME", Types.VARCHAR, Integer.MAX_VALUE, 0);
try {
c2 = JdbcUtils.getConnection(driver, url, user, password);
stat = conn.createStatement();
stat.execute("CREATE SCHEMA IF NOT EXISTS " + StringUtils.quoteIdentifier(targetSchema));
// Workaround for PostgreSQL to avoid index names
if (url.startsWith("jdbc:postgresql:")) {
rs = c2.getMetaData().getTables(null, sourceSchema, null, new String[] { "TABLE", "LINKED TABLE", "VIEW", "EXTERNAL" });
} else {
rs = c2.getMetaData().getTables(null, sourceSchema, null, null);
}
while (rs.next()) {
String table = rs.getString("TABLE_NAME");
StringBuilder buff = new StringBuilder();
buff.append("DROP TABLE IF EXISTS ").append(StringUtils.quoteIdentifier(targetSchema)).append('.').append(StringUtils.quoteIdentifier(table));
stat.execute(buff.toString());
buff = new StringBuilder();
buff.append("CREATE LINKED TABLE ").append(StringUtils.quoteIdentifier(targetSchema)).append('.').append(StringUtils.quoteIdentifier(table)).append('(').append(StringUtils.quoteStringSQL(driver)).append(", ").append(StringUtils.quoteStringSQL(url)).append(", ").append(StringUtils.quoteStringSQL(user)).append(", ").append(StringUtils.quoteStringSQL(password)).append(", ").append(StringUtils.quoteStringSQL(sourceSchema)).append(", ").append(StringUtils.quoteStringSQL(table)).append(')');
stat.execute(buff.toString());
result.addRow(table);
}
} catch (SQLException e) {
throw DbException.convert(e);
} finally {
JdbcUtils.closeSilently(rs);
JdbcUtils.closeSilently(c2);
JdbcUtils.closeSilently(stat);
}
return result;
}
use of org.h2.tools.SimpleResultSet in project h2database by h2database.
the class TableFunction method getSimpleResultSet.
private static SimpleResultSet getSimpleResultSet(LocalResult rs, int maxrows) {
int columnCount = rs.getVisibleColumnCount();
SimpleResultSet simple = new SimpleResultSet();
simple.setAutoClose(false);
for (int i = 0; i < columnCount; i++) {
String name = rs.getColumnName(i);
DataType dataType = DataType.getDataType(rs.getColumnType(i));
int sqlType = dataType.sqlType;
String sqlTypeName = dataType.name;
int precision = MathUtils.convertLongToInt(rs.getColumnPrecision(i));
int scale = rs.getColumnScale(i);
simple.addColumn(name, sqlType, sqlTypeName, precision, scale);
}
rs.reset();
for (int i = 0; i < maxrows && rs.next(); i++) {
Object[] list = new Object[columnCount];
for (int j = 0; j < columnCount; j++) {
list[j] = rs.currentRow()[j].getObject();
}
simple.addRow(list);
}
return simple;
}
use of org.h2.tools.SimpleResultSet in project h2database by h2database.
the class FullText method createResultSet.
/**
* Create an empty search result and initialize the columns.
*
* @param data true if the result set should contain the primary key data as
* an array.
* @return the empty result set
*/
protected static SimpleResultSet createResultSet(boolean data) {
SimpleResultSet result = new SimpleResultSet();
if (data) {
result.addColumn(FullText.FIELD_SCHEMA, Types.VARCHAR, 0, 0);
result.addColumn(FullText.FIELD_TABLE, Types.VARCHAR, 0, 0);
result.addColumn(FullText.FIELD_COLUMNS, Types.ARRAY, 0, 0);
result.addColumn(FullText.FIELD_KEYS, Types.ARRAY, 0, 0);
} else {
result.addColumn(FullText.FIELD_QUERY, Types.VARCHAR, 0, 0);
}
result.addColumn(FullText.FIELD_SCORE, Types.FLOAT, 0, 0);
return result;
}
Aggregations