use of org.apache.phoenix.compile.ColumnProjector in project phoenix by apache.
the class PhoenixResultSetMetaData method getColumnDisplaySize.
@Override
public int getColumnDisplaySize(int column) throws SQLException {
ColumnProjector projector = rowProjector.getColumnProjector(column - 1);
PDataType type = projector.getExpression().getDataType();
if (type == null) {
return QueryConstants.NULL_DISPLAY_TEXT.length();
}
if (type.isCoercibleTo(PDate.INSTANCE)) {
return connection.getDatePattern().length();
}
if (projector.getExpression().getMaxLength() != null) {
return projector.getExpression().getMaxLength();
}
return DEFAULT_DISPLAY_WIDTH;
}
use of org.apache.phoenix.compile.ColumnProjector in project phoenix by apache.
the class SqlQueryToColumnInfoFunction method apply.
@Override
public List<ColumnInfo> apply(String sqlQuery) {
Preconditions.checkNotNull(sqlQuery);
Connection connection = null;
List<ColumnInfo> columnInfos = null;
try {
connection = ConnectionUtil.getInputConnection(this.configuration);
final Statement statement = connection.createStatement();
final PhoenixStatement pstmt = statement.unwrap(PhoenixStatement.class);
final QueryPlan queryPlan = pstmt.compileQuery(sqlQuery);
final List<? extends ColumnProjector> projectedColumns = queryPlan.getProjector().getColumnProjectors();
columnInfos = Lists.newArrayListWithCapacity(projectedColumns.size());
columnInfos = Lists.transform(projectedColumns, new Function<ColumnProjector, ColumnInfo>() {
@Override
public ColumnInfo apply(final ColumnProjector columnProjector) {
return new ColumnInfo(columnProjector.getName(), columnProjector.getExpression().getDataType().getSqlType());
}
});
} catch (SQLException e) {
LOG.error(String.format(" Error [%s] parsing SELECT query [%s] ", e.getMessage(), sqlQuery));
throw new RuntimeException(e);
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException sqle) {
LOG.error("Error closing connection!!");
throw new RuntimeException(sqle);
}
}
}
return columnInfos;
}
use of org.apache.phoenix.compile.ColumnProjector in project phoenix by apache.
the class PhoenixResultSet method getBoolean.
@Override
public boolean getBoolean(int columnIndex) throws SQLException {
checkCursorState();
ColumnProjector colProjector = rowProjector.getColumnProjector(columnIndex - 1);
PDataType type = colProjector.getExpression().getDataType();
Object value = colProjector.getValue(currentRow, type, ptr);
wasNull = (value == null);
if (value == null) {
return false;
}
if (type == PBoolean.INSTANCE) {
return Boolean.TRUE.equals(value);
} else if (type == PVarchar.INSTANCE) {
return !STRING_FALSE.equals(value);
} else if (type == PInteger.INSTANCE) {
return !INTEGER_FALSE.equals(value);
} else if (type == PDecimal.INSTANCE) {
return !BIG_DECIMAL_FALSE.equals(value);
} else {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_CALL_METHOD_ON_TYPE).setMessage("Method: getBoolean; Type:" + type).build().buildException();
}
}
Aggregations