use of org.apache.derby.iapi.types.DataTypeDescriptor in project derby by apache.
the class ResultColumnList method createListFromResultSetMetaData.
/**
* Generate an RCL to match the contents of a ResultSetMetaData.
* This is useful when dealing with VTIs.
*
* @param rsmd The ResultSetMetaData.
* @param tableName The TableName for the BCNs.
* @param javaClassName The name of the VTI
*
* @exception StandardException Thrown on error
*/
void createListFromResultSetMetaData(ResultSetMetaData rsmd, TableName tableName, String javaClassName) throws StandardException {
try {
// JDBC columns #s are 1-based
// Check to make sure # of columns >= 1
int numColumns = rsmd.getColumnCount();
if (numColumns <= 0) {
throw StandardException.newException(SQLState.LANG_INVALID_V_T_I_COLUMN_COUNT, javaClassName, String.valueOf(numColumns));
}
for (int index = 1; index <= numColumns; index++) {
boolean nullableResult = (rsmd.isNullable(index) != ResultSetMetaData.columnNoNulls);
TypeId cti;
int jdbcColumnType = rsmd.getColumnType(index);
switch(jdbcColumnType) {
case Types.JAVA_OBJECT:
case Types.OTHER:
{
cti = TypeId.getUserDefinedTypeId(rsmd.getColumnTypeName(index));
break;
}
default:
{
cti = TypeId.getBuiltInTypeId(jdbcColumnType);
break;
}
}
// Handle the case where a VTI returns a bad column type
if (cti == null) {
throw StandardException.newException(SQLState.LANG_BAD_J_D_B_C_TYPE_INFO, Integer.toString(index));
}
// Get the maximum byte storage for this column
int maxWidth;
/* Get maximum byte storage from rsmd for variable
* width types, set it to MAXINT for the long types,
* otherwise get it from the TypeId
*/
if (cti.variableLength()) {
maxWidth = rsmd.getColumnDisplaySize(index);
} else if (jdbcColumnType == Types.LONGVARCHAR || jdbcColumnType == Types.LONGVARBINARY) {
maxWidth = Integer.MAX_VALUE;
} else {
maxWidth = 0;
}
int precision = cti.isDecimalTypeId() ? rsmd.getPrecision(index) : 0;
int scale = cti.isDecimalTypeId() ? rsmd.getScale(index) : 0;
DataTypeDescriptor dts = new DataTypeDescriptor(cti, precision, scale, nullableResult, maxWidth);
addColumn(tableName, rsmd.getColumnName(index), dts);
}
} catch (Throwable t) {
if (t instanceof StandardException) {
throw (StandardException) t;
} else {
throw StandardException.unexpectedUserException(t);
}
}
}
use of org.apache.derby.iapi.types.DataTypeDescriptor in project derby by apache.
the class ResultColumnList method buildEmptyIndexRow.
/**
* Build an empty index row for the given conglomerate.
*
* @return an empty row of the correct size and shape.
* @exception StandardException Thrown on error
*/
public ExecRow buildEmptyIndexRow(TableDescriptor td, ConglomerateDescriptor cd, StoreCostController scc, DataDictionary dd) throws StandardException {
ResultColumn rc;
if (SanityManager.DEBUG) {
if (!cd.isIndex()) {
SanityManager.THROWASSERT("ConglomerateDescriptor expected to be for index: " + cd);
}
}
int[] baseCols = cd.getIndexDescriptor().baseColumnPositions();
ExecRow row = getExecutionFactory().getValueRow(baseCols.length + 1);
for (int i = 0; i < baseCols.length; i++) {
ColumnDescriptor coldes = td.getColumnDescriptor(baseCols[i]);
DataTypeDescriptor dataType = coldes.getType();
// rc = getResultColumn(baseCols[i]);
// rc = (ResultColumn) at(baseCols[i] - 1);
// dataType = rc.getTypeServices();
DataValueDescriptor dataValue = dataType.getNull();
row.setColumn(i + 1, dataValue);
}
RowLocation rlTemplate = scc.newRowLocationTemplate();
row.setColumn(baseCols.length + 1, rlTemplate);
return row;
}
use of org.apache.derby.iapi.types.DataTypeDescriptor in project derby by apache.
the class ResultColumnList method streamableType.
private static boolean streamableType(ResultColumn rc) {
DataTypeDescriptor dtd = rc.getType();
TypeId s = TypeId.getBuiltInTypeId(dtd.getTypeName());
if (s != null) {
return s.streamStorable();
} else {
return false;
}
}
use of org.apache.derby.iapi.types.DataTypeDescriptor in project derby by apache.
the class SQLToJavaValueNode method getJSQLType.
/**
* Get the JSQLType that corresponds to this node. Could be a SQLTYPE,
* a Java primitive, or a Java class.
*
* Overrides method in JavaValueNode.
*
* @return the corresponding JSQLType
*/
@Override
JSQLType getJSQLType() throws StandardException {
if (jsqlType == null) {
if (value.requiresTypeFromContext()) {
ParameterNode pn;
if (value instanceof UnaryOperatorNode)
pn = ((UnaryOperatorNode) value).getParameterOperand();
else
pn = (ParameterNode) (value);
jsqlType = pn.getJSQLType();
} else {
DataTypeDescriptor dtd = value.getTypeServices();
if (dtd != null)
jsqlType = new JSQLType(dtd);
}
}
return jsqlType;
}
use of org.apache.derby.iapi.types.DataTypeDescriptor in project derby by apache.
the class UserAggregateDefinition method getAggregator.
/**
* Determines the result datatype and verifies that the input datatype is correct.
*
* @param inputType the input type
* @param aggregatorClass (Output arg) the name of the Derby execution-time class which wraps the aggregate logic
*
* @return the result type of the user-defined aggregator
*/
public final DataTypeDescriptor getAggregator(DataTypeDescriptor inputType, StringBuffer aggregatorClass) throws StandardException {
try {
CompilerContext cc = (CompilerContext) QueryTreeNode.getContext(CompilerContext.CONTEXT_ID);
ClassFactory classFactory = cc.getClassFactory();
TypeCompilerFactory tcf = cc.getTypeCompilerFactory();
Class<?> derbyAggregatorInterface = classFactory.loadApplicationClass("org.apache.derby.agg.Aggregator");
Class<?> userAggregatorClass = classFactory.loadApplicationClass(_alias.getJavaClassName());
Class[][] typeBounds = classFactory.getClassInspector().getTypeBounds(derbyAggregatorInterface, userAggregatorClass);
if ((typeBounds == null) || (typeBounds.length != AGGREGATOR_PARAM_COUNT) || (typeBounds[INPUT_TYPE] == null) || (typeBounds[RETURN_TYPE] == null)) {
throw StandardException.newException(SQLState.LANG_ILLEGAL_UDA_CLASS, _alias.getSchemaName(), _alias.getName(), userAggregatorClass.getName());
}
Class<?>[] genericParameterTypes = classFactory.getClassInspector().getGenericParameterTypes(derbyAggregatorInterface, userAggregatorClass);
if (genericParameterTypes == null) {
genericParameterTypes = new Class<?>[AGGREGATOR_PARAM_COUNT];
}
AggregateAliasInfo aai = (AggregateAliasInfo) _alias.getAliasInfo();
DataTypeDescriptor expectedInputType = DataTypeDescriptor.getType(aai.getForType());
DataTypeDescriptor expectedReturnType = DataTypeDescriptor.getType(aai.getReturnType());
Class<?> expectedInputClass = getJavaClass(classFactory, expectedInputType);
Class<?> expectedReturnClass = getJavaClass(classFactory, expectedReturnType);
// the input operand must be coercible to the expected input type of the aggregate
if (!tcf.getTypeCompiler(expectedInputType.getTypeId()).storable(inputType.getTypeId(), classFactory)) {
return null;
}
//
// Make sure that the declared input type of the UDA actually falls within
// the type bounds of the Aggregator implementation.
//
Class[] inputBounds = typeBounds[INPUT_TYPE];
for (int i = 0; i < inputBounds.length; i++) {
vetCompatibility((Class<?>) inputBounds[i], expectedInputClass, SQLState.LANG_UDA_WRONG_INPUT_TYPE);
}
if (genericParameterTypes[INPUT_TYPE] != null) {
vetCompatibility(genericParameterTypes[INPUT_TYPE], expectedInputClass, SQLState.LANG_UDA_WRONG_INPUT_TYPE);
}
//
// Make sure that the declared return type of the UDA actually falls within
// the type bounds of the Aggregator implementation.
//
Class[] returnBounds = typeBounds[RETURN_TYPE];
for (int i = 0; i < returnBounds.length; i++) {
vetCompatibility(returnBounds[i], expectedReturnClass, SQLState.LANG_UDA_WRONG_RETURN_TYPE);
}
if (genericParameterTypes[RETURN_TYPE] != null) {
vetCompatibility(genericParameterTypes[RETURN_TYPE], expectedReturnClass, SQLState.LANG_UDA_WRONG_RETURN_TYPE);
}
aggregatorClass.append(ClassName.UserDefinedAggregator);
return expectedReturnType;
} catch (ClassNotFoundException cnfe) {
throw aggregatorInstantiation(cnfe);
}
}
Aggregations