提交 e2dea134 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Use TypeInfo in results

上级 f70ff5e2
......@@ -402,11 +402,8 @@ public abstract class Expression {
Database db = session == null ? null : session.getDatabase();
for (int i = 0; i < columnCount; i++) {
String name = result.getColumnName(i);
int type = result.getColumnType(i);
long precision = result.getColumnPrecision(i);
int scale = result.getColumnScale(i);
int displaySize = result.getDisplaySize(i);
Column col = new Column(name, type, precision, scale, displaySize);
TypeInfo type = result.getColumnType(i);
Column col = new Column(name, type);
Expression expr = new ExpressionColumn(db, col);
expressions[i] = expr;
}
......
......@@ -72,7 +72,7 @@ public class ConditionInSelect extends Condition {
return ValueBoolean.TRUE;
}
} else {
int dataType = rows.getColumnType(0);
int dataType = rows.getColumnType(0).getValueType();
if (dataType == Value.NULL) {
return ValueBoolean.FALSE;
}
......
......@@ -15,6 +15,7 @@ import org.h2.api.ErrorCode;
import org.h2.message.DbException;
import org.h2.message.TraceObject;
import org.h2.result.SimpleResult;
import org.h2.value.TypeInfo;
import org.h2.value.Value;
import org.h2.value.ValueArray;
import org.h2.value.ValueLong;
......@@ -257,9 +258,9 @@ public class JdbcArray extends TraceObject implements Array {
private ResultSet getResultSetImpl(long index, int count) {
int id = getNextId(TraceObject.RESULT_SET);
SimpleResult rs = new SimpleResult();
rs.addColumn("INDEX", "INDEX", Value.LONG, 0, 0, ValueLong.DISPLAY_SIZE);
rs.addColumn("INDEX", "INDEX", TypeInfo.TYPE_LONG);
// TODO array result set: there are multiple data types possible
rs.addColumn("VALUE", "VALUE", Value.NULL, 0, 0, 15);
rs.addColumn("VALUE", "VALUE", TypeInfo.TYPE_NULL);
if (value != ValueNull.INSTANCE) {
Value[] values = ((ValueArray) value).getList();
count = checkRange(index, count, values.length);
......
......@@ -11,7 +11,6 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.RowIdLifetime;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Arrays;
import java.util.Map.Entry;
import java.util.Properties;
......@@ -26,6 +25,7 @@ import org.h2.message.Trace;
import org.h2.message.TraceObject;
import org.h2.result.SimpleResult;
import org.h2.util.StringUtils;
import org.h2.value.TypeInfo;
import org.h2.value.ValueInt;
import org.h2.value.ValueString;
......@@ -3187,12 +3187,12 @@ public class JdbcDatabaseMetaData extends TraceObject implements
public ResultSet getClientInfoProperties() throws SQLException {
Properties clientInfo = conn.getClientInfo();
SimpleResult result = new SimpleResult();
result.addColumn("NAME", "NAME", Types.VARCHAR, 0, 0, Integer.MAX_VALUE);
result.addColumn("MAX_LEN", "MAX_LEN", Types.INTEGER, 0, 0, ValueInt.DISPLAY_SIZE);
result.addColumn("DEFAULT_VALUE", "DEFAULT_VALUE", Types.VARCHAR, 0, 0, Integer.MAX_VALUE);
result.addColumn("DESCRIPTION", "DESCRIPTION", Types.VARCHAR, 0, 0, Integer.MAX_VALUE);
result.addColumn("NAME", "NAME", TypeInfo.TYPE_STRING_DEFAULT);
result.addColumn("MAX_LEN", "MAX_LEN", TypeInfo.TYPE_INT);
result.addColumn("DEFAULT_VALUE", "DEFAULT_VALUE", TypeInfo.TYPE_STRING_DEFAULT);
result.addColumn("DESCRIPTION", "DESCRIPTION", TypeInfo.TYPE_STRING_DEFAULT);
// Non-standard column
result.addColumn("VALUE", "VALUE", Types.VARCHAR, 0, 0, Integer.MAX_VALUE);
result.addColumn("VALUE", "VALUE", TypeInfo.TYPE_STRING_DEFAULT);
for (Entry<Object, Object> entry : clientInfo.entrySet()) {
result.addRow(ValueString.get((String) entry.getKey()), ValueInt.get(Integer.MAX_VALUE),
ValueString.EMPTY, ValueString.EMPTY, ValueString.get((String) entry.getValue()));
......
......@@ -102,7 +102,7 @@ public class JdbcResultSetMetaData extends TraceObject implements
try {
debugCodeCall("getColumnType", column);
checkColumnIndex(column);
int type = result.getColumnType(--column);
int type = result.getColumnType(--column).getValueType();
return DataType.convertTypeToSQLType(type);
} catch (Exception e) {
throw logAndConvert(e);
......@@ -121,7 +121,7 @@ public class JdbcResultSetMetaData extends TraceObject implements
try {
debugCodeCall("getColumnTypeName", column);
checkColumnIndex(column);
int type = result.getColumnType(--column);
int type = result.getColumnType(--column).getValueType();
return DataType.getDataType(type).name;
} catch (Exception e) {
throw logAndConvert(e);
......@@ -370,7 +370,7 @@ public class JdbcResultSetMetaData extends TraceObject implements
try {
debugCodeCall("getColumnClassName", column);
checkColumnIndex(column);
int type = result.getColumnType(--column);
int type = result.getColumnType(--column).getValueType();
return DataType.getTypeClassName(type, true);
} catch (Exception e) {
throw logAndConvert(e);
......@@ -389,7 +389,7 @@ public class JdbcResultSetMetaData extends TraceObject implements
try {
debugCodeCall("getPrecision", column);
checkColumnIndex(column);
long prec = result.getColumnPrecision(--column);
long prec = result.getColumnType(--column).getPrecision();
return MathUtils.convertLongToInt(prec);
} catch (Exception e) {
throw logAndConvert(e);
......@@ -408,7 +408,7 @@ public class JdbcResultSetMetaData extends TraceObject implements
try {
debugCodeCall("getScale", column);
checkColumnIndex(column);
return result.getColumnScale(--column);
return result.getColumnType(--column).getScale();
} catch (Exception e) {
throw logAndConvert(e);
}
......@@ -426,7 +426,7 @@ public class JdbcResultSetMetaData extends TraceObject implements
try {
debugCodeCall("getColumnDisplaySize", column);
checkColumnIndex(column);
return result.getDisplaySize(--column);
return result.getColumnType(--column).getDisplaySize();
} catch (Exception e) {
throw logAndConvert(e);
}
......
......@@ -26,6 +26,7 @@ import org.h2.store.DataHandler;
import org.h2.util.JdbcUtils;
import org.h2.util.Utils;
import org.h2.value.CompareMode;
import org.h2.value.TypeInfo;
import org.h2.value.Value;
import org.h2.value.ValueArray;
import org.h2.value.ValueBoolean;
......@@ -417,10 +418,11 @@ public class ValueDataType implements DataType {
for (int i = 0; i < columnCount; i++) {
writeString(buff, result.getAlias(i));
writeString(buff, result.getColumnName(i));
buff.putVarInt(result.getColumnType(i)).
putVarLong(result.getColumnPrecision(i)).
putVarInt(result.getColumnScale(i)).
putVarInt(result.getDisplaySize(i));
TypeInfo columnType = result.getColumnType(i);
buff.putVarInt(columnType.getValueType()).
putVarLong(columnType.getPrecision()).
putVarInt(columnType.getScale()).
putVarInt(columnType.getDisplaySize());
}
while (result.next()) {
buff.put((byte) 1);
......
......@@ -8,6 +8,7 @@ package org.h2.result;
import org.h2.engine.SessionInterface;
import org.h2.expression.Expression;
import org.h2.message.DbException;
import org.h2.value.TypeInfo;
import org.h2.value.Value;
/**
......@@ -139,23 +140,8 @@ public abstract class LazyResult implements ResultInterface {
}
@Override
public int getColumnType(int i) {
return expressions[i].getValueType();
}
@Override
public long getColumnPrecision(int i) {
return expressions[i].getType().getPrecision();
}
@Override
public int getColumnScale(int i) {
return expressions[i].getType().getScale();
}
@Override
public int getDisplaySize(int i) {
return expressions[i].getType().getDisplaySize();
public TypeInfo getColumnType(int i) {
return expressions[i].getType();
}
@Override
......
......@@ -15,6 +15,7 @@ import org.h2.message.DbException;
import org.h2.mvstore.db.MVTempResult;
import org.h2.util.Utils;
import org.h2.util.ValueHashMap;
import org.h2.value.TypeInfo;
import org.h2.value.Value;
import org.h2.value.ValueArray;
......@@ -527,24 +528,14 @@ public class LocalResultImpl implements LocalResult {
return expressions[i].getSchemaName();
}
@Override
public int getDisplaySize(int i) {
return expressions[i].getType().getDisplaySize();
}
@Override
public String getColumnName(int i) {
return expressions[i].getColumnName();
}
@Override
public int getColumnType(int i) {
return expressions[i].getValueType();
}
@Override
public long getColumnPrecision(int i) {
return expressions[i].getType().getPrecision();
public TypeInfo getColumnType(int i) {
return expressions[i].getType();
}
@Override
......@@ -557,11 +548,6 @@ public class LocalResultImpl implements LocalResult {
return expressions[i].isAutoIncrement();
}
@Override
public int getColumnScale(int i) {
return expressions[i].getType().getScale();
}
/**
* Set the offset of the first row to return.
*
......
......@@ -40,8 +40,7 @@ public final class MergedResult {
SimpleResult.Column[] cols = new SimpleResult.Column[count];
for (int i = 0; i < count; i++) {
SimpleResult.Column c = new SimpleResult.Column(result.getAlias(i), result.getColumnName(i),
result.getColumnType(i), result.getColumnPrecision(i), result.getColumnScale(i),
result.getDisplaySize(i));
result.getColumnType(i));
cols[i] = c;
if (!columns.contains(c)) {
columns.add(c);
......
......@@ -8,6 +8,7 @@ package org.h2.result;
import java.io.IOException;
import org.h2.value.Transfer;
import org.h2.value.TypeInfo;
/**
* A result set column of a remote result.
......@@ -35,24 +36,9 @@ public class ResultColumn {
final String columnName;
/**
* The value type of this column.
* The column type.
*/
final int columnType;
/**
* The precision.
*/
final long precision;
/**
* The scale.
*/
final int scale;
/**
* The expected display size.
*/
final int displaySize;
final TypeInfo columnType;
/**
* True if this is an autoincrement column.
......@@ -74,10 +60,11 @@ public class ResultColumn {
schemaName = in.readString();
tableName = in.readString();
columnName = in.readString();
columnType = in.readInt();
precision = in.readLong();
scale = in.readInt();
displaySize = in.readInt();
int valueType = in.readInt();
long precision = in.readLong();
int scale = in.readInt();
int displaySize = in.readInt();
columnType = new TypeInfo(valueType, precision, scale, displaySize, null);
autoIncrement = in.readBoolean();
nullable = in.readInt();
}
......@@ -95,10 +82,11 @@ public class ResultColumn {
out.writeString(result.getSchemaName(i));
out.writeString(result.getTableName(i));
out.writeString(result.getColumnName(i));
out.writeInt(result.getColumnType(i));
out.writeLong(result.getColumnPrecision(i));
out.writeInt(result.getColumnScale(i));
out.writeInt(result.getDisplaySize(i));
TypeInfo type = result.getColumnType(i);
out.writeInt(type.getValueType());
out.writeLong(type.getPrecision());
out.writeInt(type.getScale());
out.writeInt(type.getDisplaySize());
out.writeBoolean(result.isAutoIncrement(i));
out.writeInt(result.getNullable(i));
}
......
......@@ -6,6 +6,7 @@
package org.h2.result;
import org.h2.engine.SessionInterface;
import org.h2.value.TypeInfo;
import org.h2.value.Value;
/**
......@@ -123,31 +124,7 @@ public interface ResultInterface extends AutoCloseable {
* @param i the column number (starting with 0)
* @return the column data type
*/
int getColumnType(int i);
/**
* Get the precision for this column.
*
* @param i the column number (starting with 0)
* @return the precision
*/
long getColumnPrecision(int i);
/**
* Get the scale for this column.
*
* @param i the column number (starting with 0)
* @return the scale
*/
int getColumnScale(int i);
/**
* Get the display size for this column.
*
* @param i the column number (starting with 0)
* @return the display size
*/
int getDisplaySize(int i);
TypeInfo getColumnType(int i);
/**
* Check if this is an auto-increment column.
......
......@@ -14,6 +14,7 @@ import org.h2.engine.SysProperties;
import org.h2.message.DbException;
import org.h2.message.Trace;
import org.h2.value.Transfer;
import org.h2.value.TypeInfo;
import org.h2.value.Value;
/**
......@@ -77,25 +78,10 @@ public class ResultRemote implements ResultInterface {
}
@Override
public int getColumnType(int i) {
public TypeInfo getColumnType(int i) {
return columns[i].columnType;
}
@Override
public long getColumnPrecision(int i) {
return columns[i].precision;
}
@Override
public int getColumnScale(int i) {
return columns[i].scale;
}
@Override
public int getDisplaySize(int i) {
return columns[i].displaySize;
}
@Override
public boolean isAutoIncrement(int i) {
return columns[i].autoIncrement;
......
......@@ -10,6 +10,7 @@ import java.util.ArrayList;
import org.h2.engine.SessionInterface;
import org.h2.util.Utils;
import org.h2.value.TypeInfo;
import org.h2.value.Value;
/**
......@@ -28,16 +29,7 @@ public class SimpleResult implements ResultInterface {
final String columnName;
/** Column type. */
final int columnType;
/** Column precision. */
final long columnPrecision;
/** Column scale. */
final int columnScale;
/** Displayed size of the column. */
final int displaySize;
final TypeInfo columnType;
Column(String alias, String columnName, int columnType, long columnPrecision, int columnScale,
int displaySize) {
......@@ -46,10 +38,16 @@ public class SimpleResult implements ResultInterface {
}
this.alias = alias;
this.columnName = columnName;
this.columnType = TypeInfo.getTypeInfo(columnType, columnPrecision, columnScale, displaySize, null);
}
Column(String alias, String columnName, TypeInfo columnType) {
if (alias == null || columnName == null) {
throw new NullPointerException();
}
this.alias = alias;
this.columnName = columnName;
this.columnType = columnType;
this.columnPrecision = columnPrecision;
this.columnScale = columnScale;
this.displaySize = displaySize;
}
@Override
......@@ -107,7 +105,7 @@ public class SimpleResult implements ResultInterface {
*
* @param alias Column's alias.
* @param columnName Column's name.
* @param columnType Column's type.
* @param columnType Column's value type.
* @param columnPrecision Column's precision.
* @param columnScale Column's scale.
* @param displaySize Column's display data size.
......@@ -117,6 +115,17 @@ public class SimpleResult implements ResultInterface {
addColumn(new Column(alias, columnName, columnType, columnPrecision, columnScale, displaySize));
}
/**
* Add column to the result.
*
* @param alias Column's alias.
* @param columnName Column's name.
* @param columnType Column's type.
*/
public void addColumn(String alias, String columnName, TypeInfo columnType) {
addColumn(new Column(alias, columnName, columnType));
}
/**
* Add column to the result.
*
......@@ -212,25 +221,10 @@ public class SimpleResult implements ResultInterface {
}
@Override
public int getColumnType(int i) {
public TypeInfo getColumnType(int i) {
return columns.get(i).columnType;
}
@Override
public long getColumnPrecision(int i) {
return columns.get(i).columnPrecision;
}
@Override
public int getColumnScale(int i) {
return columns.get(i).columnScale;
}
@Override
public int getDisplaySize(int i) {
return columns.get(i).displaySize;
}
@Override
public boolean isAutoIncrement(int i) {
return false;
......
......@@ -232,7 +232,7 @@ public class UpdatableRow {
}
Value[] newRow = new Value[columnCount];
for (int i = 0; i < columnCount; i++) {
int type = result.getColumnType(i);
int type = result.getColumnType(i).getValueType();
newRow[i] = DataType.readValue(conn.getSession(), rs, i + 1, type);
}
return newRow;
......
......@@ -27,6 +27,7 @@ import org.h2.util.DateTimeUtils;
import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils;
import org.h2.util.Utils;
import org.h2.value.TypeInfo;
import org.h2.value.Value;
import org.h2.value.ValueArray;
import org.h2.value.ValueBoolean;
......@@ -679,10 +680,11 @@ public class Data {
for (int i = 0; i < columnCount; i++) {
writeString(result.getAlias(i));
writeString(result.getColumnName(i));
writeVarInt(result.getColumnType(i));
writeVarLong(result.getColumnPrecision(i));
writeVarInt(result.getColumnScale(i));
writeVarInt(result.getDisplaySize(i));
TypeInfo columnType = result.getColumnType(i);
writeVarInt(columnType.getValueType());
writeVarLong(columnType.getPrecision());
writeVarInt(columnType.getScale());
writeVarInt(columnType.getDisplaySize());
}
while (result.next()) {
writeByte((byte) 1);
......@@ -1166,10 +1168,11 @@ public class Data {
for (int i = 0; i < columnCount; i++) {
len += getStringLen(result.getAlias(i));
len += getStringLen(result.getColumnName(i));
len += getVarIntLen(result.getColumnType(i));
len += getVarLongLen(result.getColumnPrecision(i));
len += getVarIntLen(result.getColumnScale(i));
len += getVarIntLen(result.getDisplaySize(i));
TypeInfo columnType = result.getColumnType(i);
len += getVarIntLen(columnType.getValueType());
len += getVarLongLen(columnType.getPrecision());
len += getVarIntLen(columnType.getScale());
len += getVarIntLen(columnType.getDisplaySize());
}
while (result.next()) {
len++;
......
......@@ -67,11 +67,7 @@ public class FunctionTable extends Table {
int columnCount = result.getVisibleColumnCount();
Column[] cols = new Column[columnCount];
for (int i = 0; i < columnCount; i++) {
cols[i] = new Column(result.getColumnName(i),
result.getColumnType(i),
result.getColumnPrecision(i),
result.getColumnScale(i),
result.getDisplaySize(i));
cols[i] = new Column(result.getColumnName(i), result.getColumnType(i));
}
setColumns(cols);
}
......
......@@ -509,6 +509,7 @@ public class DataType {
dataType.params = "PRECISION,SCALE";
dataType.supportsPrecision = true;
dataType.supportsScale = true;
dataType.maxScale = maxPrecision;
}
dataType.decimal = true;
dataType.autoIncrement = autoInc;
......
......@@ -512,18 +512,19 @@ public class Transfer {
int columnCount = result.getVisibleColumnCount();
writeInt(columnCount);
for (int i = 0; i < columnCount; i++) {
TypeInfo columnType = result.getColumnType(i);
if (version >= Constants.TCP_PROTOCOL_VERSION_18) {
writeString(result.getAlias(i));
writeString(result.getColumnName(i));
writeInt(result.getColumnType(i));
writeLong(result.getColumnPrecision(i));
writeInt(result.getColumnScale(i));
writeInt(result.getDisplaySize(i));
writeInt(columnType.getValueType());
writeLong(columnType.getPrecision());
writeInt(columnType.getScale());
writeInt(columnType.getDisplaySize());
} else {
writeString(result.getColumnName(i));
writeInt(DataType.getDataType(result.getColumnType(i)).sqlType);
writeInt(MathUtils.convertLongToInt(result.getColumnPrecision(i)));
writeInt(result.getColumnScale(i));
writeInt(DataType.getDataType(columnType.getValueType()).sqlType);
writeInt(MathUtils.convertLongToInt(columnType.getPrecision()));
writeInt(columnType.getScale());
}
}
while (result.next()) {
......
......@@ -1335,8 +1335,7 @@ public abstract class Value extends VersionedValue {
private ValueResultSet convertToResultSet() {
SimpleResult result = new SimpleResult();
TypeInfo type = getType();
result.addColumn("X", "X", type.getValueType(), type.getPrecision(), type.getScale(), type.getDisplaySize());
result.addColumn("X", "X", getType());
result.addRow(this);
return ValueResultSet.get(result);
}
......@@ -1637,8 +1636,7 @@ public abstract class Value extends VersionedValue {
*/
public ResultInterface getResult() {
SimpleResult rs = new SimpleResult();
TypeInfo type = getType();
rs.addColumn("X", "X", type.getValueType(), type.getPrecision(), type.getScale(), type.getDisplaySize());
rs.addColumn("X", "X", getType());
rs.addRow(this);
return rs;
}
......
......@@ -64,7 +64,8 @@ public class ValueResultSet extends Value {
for (int i = 0; i < maxrows && rs.next(); i++) {
Value[] list = new Value[columnCount];
for (int j = 0; j < columnCount; j++) {
list[j] = DataType.convertToValue(session, rs.getObject(j + 1), simple.getColumnType(j));
list[j] = DataType.convertToValue(session, rs.getObject(j + 1),
simple.getColumnType(j).getValueType());
}
simple.addRow(list);
}
......@@ -87,8 +88,7 @@ public class ValueResultSet extends Value {
int columnCount = result.getVisibleColumnCount();
SimpleResult simple = new SimpleResult();
for (int i = 0; i < columnCount; i++) {
simple.addColumn(result.getAlias(i), result.getColumnName(i), result.getColumnType(i),
result.getColumnPrecision(i), result.getColumnScale(i), result.getDisplaySize(i));
simple.addColumn(result.getAlias(i), result.getColumnName(i), result.getColumnType(i));
}
result.reset();
for (int i = 0; i < maxrows && result.next(); i++) {
......
......@@ -540,8 +540,8 @@ public class TestFunctions extends TestDb implements AggregateFunction {
stat.execute("create aggregate agg_sum for \""+getClass().getName()+"\"");
rs = stat.executeQuery("select agg_sum(1), sum(1.6) from dual");
rs.next();
assertEquals(1, rs.getMetaData().getScale(2));
assertEquals(32767, rs.getMetaData().getScale(1));
assertEquals(Integer.MAX_VALUE, rs.getMetaData().getScale(2));
assertEquals(Integer.MAX_VALUE, rs.getMetaData().getScale(1));
stat.executeQuery("select * from information_schema.function_aliases");
conn.close();
}
......
......@@ -37,6 +37,7 @@ import org.h2.test.utils.AssertThrows;
import org.h2.tools.SimpleResultSet;
import org.h2.util.Bits;
import org.h2.value.DataType;
import org.h2.value.TypeInfo;
import org.h2.value.Value;
import org.h2.value.ValueArray;
import org.h2.value.ValueBytes;
......@@ -253,16 +254,18 @@ public class TestValue extends TestDb {
assertEquals(2, res.getVisibleColumnCount());
assertEquals("ID", res.getAlias(0));
assertEquals("ID", res.getColumnName(0));
assertEquals(Value.INT, res.getColumnType(0));
assertEquals(0, res.getColumnPrecision(0));
assertEquals(0, res.getColumnScale(0));
assertEquals(fromSimple ? 15 : ValueInt.DISPLAY_SIZE, res.getDisplaySize(0));
TypeInfo type = res.getColumnType(0);
assertEquals(Value.INT, type.getValueType());
assertEquals(ValueInt.PRECISION, type.getPrecision());
assertEquals(0, type.getScale());
assertEquals(ValueInt.DISPLAY_SIZE, type.getDisplaySize());
assertEquals("NAME", res.getAlias(1));
assertEquals("NAME", res.getColumnName(1));
assertEquals(Value.STRING, res.getColumnType(1));
assertEquals(255, res.getColumnPrecision(1));
assertEquals(0, res.getColumnScale(1));
assertEquals(fromSimple ? 15 : 255, res.getDisplaySize(1));
type = res.getColumnType(1);
assertEquals(Value.STRING, type.getValueType());
assertEquals(255, type.getPrecision());
assertEquals(0, type.getScale());
assertEquals(255, type.getDisplaySize());
if (count >= 1) {
assertTrue(res.next());
assertEquals(new Value[] {ValueInt.get(1), ValueString.get("Hello")}, res.currentRow());
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论