/** Returns the root schema. */ public static Schema root(Schema schema) { for (Schema s = schema; ; ) { Schema previous = s; s = s.getParentSchema(); if (s == null) { return previous; } } }
/** Returns the path of a schema, appending the name of a table if not null. */ public static List<String> path(Schema schema, String name) { final List<String> list = new ArrayList<String>(); if (name != null) { list.add(name); } for (Schema s = schema; s != null; s = s.getParentSchema()) { if (s.getParentSchema() != null || !s.getName().equals("")) { // Omit the root schema's name from the path if it's the empty string, // which it usually is. list.add(s.getName()); } } return ImmutableList.copyOf(Lists.reverse(list)); }
/** Returns the expression to access a table within a schema. */ public static Expression tableExpression( Schema schema, Type elementType, String tableName, Class clazz) { final MethodCallExpression expression; if (Table.class.isAssignableFrom(clazz)) { expression = Expressions.call( schema.getExpression(), BuiltinMethod.SCHEMA_GET_TABLE.method, Expressions.constant(tableName)); } else { expression = Expressions.call( BuiltinMethod.SCHEMAS_QUERYABLE.method, DataContext.ROOT, schema.getExpression(), Expressions.constant(elementType), Expressions.constant(tableName)); } return Types.castIfNecessary(clazz, expression); }
/** Returns the expression for a sub-schema. */ public static Expression subSchemaExpression(Schema schema, String name, Class type) { // (Type) schemaExpression.getSubSchema("name") Expression call = Expressions.call( schema.getExpression(), BuiltinMethod.SCHEMA_GET_SUB_SCHEMA.method, Expressions.constant(name)); // CHECKSTYLE: IGNORE 2 //noinspection unchecked if (false && type != null && !type.isAssignableFrom(Schema.class)) { return unwrap(call, type); } return call; }