/** * Given "INTEGER", returns BasicSqlType(INTEGER). Given "VARCHAR(10)", returns * BasicSqlType(VARCHAR, 10). Given "NUMERIC(10, 2)", returns BasicSqlType(NUMERIC, 10, 2). */ private RelDataType parseTypeString(RelDataTypeFactory typeFactory, String typeString) { int precision = -1; int scale = -1; int open = typeString.indexOf("("); if (open >= 0) { int close = typeString.indexOf(")", open); if (close >= 0) { String rest = typeString.substring(open + 1, close); typeString = typeString.substring(0, open); int comma = rest.indexOf(","); if (comma >= 0) { precision = Integer.parseInt(rest.substring(0, comma)); scale = Integer.parseInt(rest.substring(comma)); } else { precision = Integer.parseInt(rest); } } } try { final SqlTypeName typeName = SqlTypeName.valueOf(typeString); return typeName.allowsPrecScale(true, true) ? typeFactory.createSqlType(typeName, precision, scale) : typeName.allowsPrecScale(true, false) ? typeFactory.createSqlType(typeName, precision) : typeFactory.createSqlType(typeName); } catch (IllegalArgumentException e) { return typeFactory.createSqlType(SqlTypeName.ANY); } }
// implement RelDataType public SqlIdentifier getSqlIdentifier() { SqlTypeName typeName = getSqlTypeName(); if (typeName == null) { return null; } return new SqlIdentifier(typeName.name(), SqlParserPos.ZERO); }
private RelDataType sqlType( RelDataTypeFactory typeFactory, int dataType, int precision, int scale, String typeString) { SqlTypeName sqlTypeName = SqlTypeName.getNameForJdbcType(dataType); switch (sqlTypeName) { case ARRAY: RelDataType component = null; if (typeString != null && typeString.endsWith(" ARRAY")) { // E.g. hsqldb gives "INTEGER ARRAY", so we deduce the component type // "INTEGER". final String remaining = typeString.substring(0, typeString.length() - " ARRAY".length()); component = parseTypeString(typeFactory, remaining); } if (component == null) { component = typeFactory.createSqlType(SqlTypeName.ANY); } return typeFactory.createArrayType(component, -1); } if (precision >= 0 && scale >= 0 && sqlTypeName.allowsPrecScale(true, true)) { return typeFactory.createSqlType(sqlTypeName, precision, scale); } else if (precision >= 0 && sqlTypeName.allowsPrecNoScale()) { return typeFactory.createSqlType(sqlTypeName, precision); } else { assert sqlTypeName.allowsNoPrecNoScale(); return typeFactory.createSqlType(sqlTypeName); } }
public int getFieldJdbcType(int fieldOrdinal) { RelDataType type = getFieldNamedType(fieldOrdinal); SqlTypeName typeName = type.getSqlTypeName(); if (typeName == null) { return Types.OTHER; } return typeName.getJdbcOrdinal(); }
public int getFieldScale(int fieldOrdinal) { RelDataType type = getFieldType(fieldOrdinal); SqlTypeName typeName = type.getSqlTypeName(); if (typeName == null) { return 0; } if (typeName.allowsPrecScale(true, true)) { return type.getScale(); } else { return 0; } }
public String getFieldTypeName(int fieldOrdinal) { RelDataType type = getFieldNamedType(fieldOrdinal); SqlTypeName typeName = type.getSqlTypeName(); if (typeName == null) { return type.toString(); } switch (typeName) { case STRUCTURED: case DISTINCT: return type.getSqlIdentifier().toString(); case INTERVAL_DAY_TIME: case INTERVAL_YEAR_MONTH: return type.toString(); } return typeName.name(); }
<T> PrepareResult<T> prepare_( Context context, String sql, Queryable<T> queryable, Type elementType) { final JavaTypeFactory typeFactory = context.getTypeFactory(); OptiqCatalogReader catalogReader = new OptiqCatalogReader(context.getRootSchema(), typeFactory); final OptiqPreparingStmt preparingStmt = new OptiqPreparingStmt(catalogReader, typeFactory, context.getRootSchema()); preparingStmt.setResultCallingConvention(CallingConvention.ENUMERABLE); final RelDataType x; final PreparedResult preparedResult; if (sql != null) { assert queryable == null; SqlParser parser = new SqlParser(sql); SqlNode sqlNode; try { sqlNode = parser.parseQuery(); } catch (SqlParseException e) { throw new RuntimeException("parse failed", e); } final Schema rootSchema = context.getRootSchema(); SqlValidator validator = new SqlValidatorImpl( new ChainedSqlOperatorTable( Arrays.<SqlOperatorTable>asList( SqlStdOperatorTable.instance(), new MySqlOperatorTable(rootSchema, typeFactory))), catalogReader, typeFactory, SqlConformance.Default) {}; preparedResult = preparingStmt.prepareSql(sqlNode, Object.class, validator, true); x = validator.getValidatedNodeType(sqlNode); } else { assert queryable != null; x = context.getTypeFactory().createType(elementType); preparedResult = preparingStmt.prepareQueryable(queryable, x); } // TODO: parameters final List<Parameter> parameters = Collections.emptyList(); // TODO: column meta data final List<ColumnMetaData> columns = new ArrayList<ColumnMetaData>(); RelDataType jdbcType = makeStruct(typeFactory, x); for (RelDataTypeField field : jdbcType.getFields()) { RelDataType type = field.getType(); SqlTypeName sqlTypeName = type.getSqlTypeName(); columns.add( new ColumnMetaData( columns.size(), false, true, false, false, type.isNullable() ? 1 : 0, true, 0, field.getName(), field.getName(), null, sqlTypeName.allowsPrec() && false ? type.getPrecision() : -1, sqlTypeName.allowsScale() ? type.getScale() : -1, null, null, sqlTypeName.getJdbcOrdinal(), sqlTypeName.getName(), true, false, false, null)); } return new PrepareResult<T>(sql, parameters, columns, (Enumerable<T>) preparedResult.execute()); }