return new ReflectiveSchema(target);
return ReflectiveSchema.create(schema, name, target);
for (Field field : clazz.getFields()) { final String fieldName = field.getName(); final Table<Object> table = fieldRelation(field); if (table == null) { continue; continue; final TableFunction tableFunction = methodMember(method); membersMap.put(methodName, new TableFunctionInSchemaImpl(this, methodName, tableFunction));
/** Returns a table based on a particular field of this schema. If the * field is not of the right type to be a relation, returns null. */ private <T> Table fieldRelation(final Field field) { final Type elementType = getElementType(field.getType()); if (elementType == null) { return null; } Object o; try { o = field.get(target); } catch (IllegalAccessException e) { throw new RuntimeException( "Error while accessing field " + field, e); } @SuppressWarnings("unchecked") final Enumerable<T> enumerable = toEnumerable(o); return new FieldTable<T>(field, elementType, enumerable); }
final Type elementType = getElementType(field.getType()); if (elementType == null) { return null; relDataType, Expressions.field( ReflectiveSchema.this.getTargetExpression(), field)) { public String toString() {
public Schema create( MutableSchema parentSchema, String name, Map<String, Object> operand) { final ReflectiveSchema schema = ReflectiveSchema.create(parentSchema, name, new HrSchema()); // Mine the EMPS table and add it under another name e.g. ELVIS final Table table = schema.getTable("emps", Object.class); String tableName = (String) operand.get("tableName"); schema.addTable( new TableInSchemaImpl( schema, tableName, Schema.TableType.TABLE, table)); final Boolean mutable = (Boolean) operand.get("mutable"); if (mutable == null || mutable) { return schema; } else { // Wrap the schema in DelegatingSchema so that it does not // implement MutableSchema. return new DelegatingSchema(schema); } } }
/** Returns an expression for the object wrapped by this schema (not the * schema itself). */ Expression getTargetExpression(SchemaPlus parentSchema, String name) { return Types.castIfNecessary( target.getClass(), Expressions.call( Schemas.unwrap( getExpression(parentSchema, name), ReflectiveSchema.class), BuiltinMethod.REFLECTIVE_SCHEMA_GET_TARGET.method)); }
public Table<T> apply(final List<Object> arguments) { final List<Expression> list = new ArrayList<Expression>(); for (Object argument : arguments) { list.add(Expressions.constant(argument)); } try { final Object o = method.invoke(schema, arguments.toArray()); return new ReflectiveTable<T>( schema, elementType, relDataType, Expressions.call( schema.getTargetExpression(), method, list)) { public Enumerator<T> enumerator() { @SuppressWarnings("unchecked") final Enumerable<T> enumerable = toEnumerable(o); return enumerable.enumerator(); } }; } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } };
@Override protected Map<String, Table> getTableMap() { final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder(); for (Field field : clazz.getFields()) { final String fieldName = field.getName(); final Table table = fieldRelation(field); if (table == null) { continue; } builder.put(fieldName, table); } return builder.build(); }
public <T> TableFunction<T> methodMember(final Method method) { final ReflectiveSchema schema = this; final Type elementType = getElementType(method.getReturnType()); final RelDataType relDataType = typeFactory.createType(elementType); final Class<?>[] parameterTypes = method.getParameterTypes();
public ReflectiveTable( ReflectiveSchema schema, Type elementType, RelDataType relDataType, Expression expression) { super(schema.getQueryProvider(), elementType, expression); this.schema = schema; this.relDataType = relDataType; }
/** Returns an expression for the object wrapped by this schema (not the * schema itself). */ Expression getTargetExpression() { return Types.castIfNecessary( target.getClass(), Expressions.call( Types.castIfNecessary( ReflectiveSchema.class, getExpression()), BuiltinMethod.GET_TARGET.method)); }
@Override public Expression getExpression(SchemaPlus schema, String tableName, Class clazz) { return Expressions.field( schema.unwrap(ReflectiveSchema.class).getTargetExpression( schema.getParentSchema(), schema.getName()), field); } }
/** * Creates a ReflectiveSchema within another schema. * * @param parentSchema Parent schema * @param name Name of new schema * @param target Object whose fields become the tables of the schema * @return New ReflectiveSchema */ public static ReflectiveSchema create( MutableSchema parentSchema, String name, Object target) { ReflectiveSchema schema = new ReflectiveSchema( parentSchema, name, target, parentSchema.getSubSchemaExpression( name, ReflectiveSchema.class)); parentSchema.addSchema(name, schema); return schema; }
public OptiqConnection createConnection() throws Exception { Class.forName("net.hydromatic.optiq.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:optiq:"); OptiqConnection optiqConnection = connection.unwrap(OptiqConnection.class); MutableSchema rootSchema = optiqConnection.getRootSchema(); ReflectiveSchema.create(rootSchema, name, schema); optiqConnection.setSchema(name); return optiqConnection; } });
public OptiqConnection createConnection() throws Exception { Class.forName("net.hydromatic.optiq.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:optiq:"); OptiqConnection optiqConnection = connection.unwrap(OptiqConnection.class); SchemaPlus rootSchema = optiqConnection.getRootSchema(); rootSchema.add(name, new ReflectiveSchema(schema)); optiqConnection.setSchema(name); return optiqConnection; } });
static OptiqConnection getConnection(String... schema) throws ClassNotFoundException, SQLException { Class.forName("net.hydromatic.optiq.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:optiq:"); OptiqConnection optiqConnection = connection.unwrap(OptiqConnection.class); MutableSchema rootSchema = optiqConnection.getRootSchema(); final List<String> schemaList = Arrays.asList(schema); if (schemaList.contains("hr")) { ReflectiveSchema.create(rootSchema, "hr", new HrSchema()); } if (schemaList.contains("foodmart")) { ReflectiveSchema.create( rootSchema, "foodmart", new FoodmartSchema()); } if (schemaList.contains("lingual")) { ReflectiveSchema.create( rootSchema, "SALES", new LingualSchema()); } if (schemaList.contains("metadata")) { // always present } return optiqConnection; }
connection.unwrap(OptiqConnection.class); SchemaPlus rootSchema = optiqConnection.getRootSchema(); rootSchema.add("hr", new ReflectiveSchema(new Hr())); rootSchema.add("foodmart", new ReflectiveSchema(new Foodmart())); Statement statement = connection.createStatement(); ResultSet resultSet =
connection.unwrap(OptiqConnection.class); MutableSchema rootSchema = optiqConnection.getRootSchema(); ReflectiveSchema.create(rootSchema, "hr", new HrSchema()); ReflectiveSchema.create(rootSchema, "foodmart", new FoodmartSchema()); Statement statement = connection.createStatement(); ResultSet resultSet =
/** Test case for bug where if two tables have different element classes * but those classes have identical fields, Optiq would generate code to use * the wrong element class; a {@link ClassCastException} would ensue. */ @Test public void testDifferentTypesSameFields() throws Exception { Class.forName("net.hydromatic.optiq.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:optiq:"); OptiqConnection optiqConnection = connection.unwrap(OptiqConnection.class); final SchemaPlus rootSchema = optiqConnection.getRootSchema(); rootSchema.add("TEST", new ReflectiveSchema(new MySchema())); Statement statement = optiqConnection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT \"myvalue\" from TEST.\"mytable2\""); assertEquals("myvalue=2\n", OptiqAssert.toString(resultSet)); resultSet.close(); statement.close(); connection.close(); }