use of com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable in project elide by yahoo.
the class SQLQueryEngine method constructTable.
@Override
protected Table constructTable(Namespace namespace, Type<?> entityClass, EntityDictionary metaDataDictionary) {
String dbConnectionName = null;
Annotation annotation = EntityDictionary.getFirstAnnotation(entityClass, Arrays.asList(FromTable.class, FromSubquery.class));
if (annotation instanceof FromTable) {
dbConnectionName = ((FromTable) annotation).dbConnectionName();
} else if (annotation instanceof FromSubquery) {
dbConnectionName = ((FromSubquery) annotation).dbConnectionName();
}
ConnectionDetails connectionDetails = connectionDetailsLookup.apply(dbConnectionName);
return new SQLTable(namespace, entityClass, metaDataDictionary, connectionDetails);
}
use of com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable in project elide by yahoo.
the class TableTypeTest method testTableNameWithoutSchema.
@Test
void testTableNameWithoutSchema() throws Exception {
Table testTable = Table.builder().table("table1").name("Table").build();
TableType testType = new TableType(testTable);
FromTable fromTable = (FromTable) testType.getAnnotation(FromTable.class);
assertEquals("table1", fromTable.name());
}
use of com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable in project elide by yahoo.
the class SQLTable method resolveTableOrSubselect.
/**
* Maps an entity class to a physical table or subselect query, if neither {@link javax.persistence.Table}
* nor {@link Subselect} annotation is present on this class, try {@link FromTable} and {@link FromSubquery}.
* @param cls The entity class.
* @return The physical SQL table or subselect query.
*/
public static String resolveTableOrSubselect(EntityDictionary dictionary, Type<?> cls) {
if (hasSql(cls)) {
if (cls.isAnnotationPresent(FromSubquery.class)) {
return dictionary.getAnnotation(cls, FromSubquery.class).sql();
}
return dictionary.getAnnotation(cls, Subselect.class).value();
}
javax.persistence.Table table = dictionary.getAnnotation(cls, javax.persistence.Table.class);
if (table != null) {
return resolveTableAnnotation(table);
}
FromTable fromTable = dictionary.getAnnotation(cls, FromTable.class);
return fromTable != null ? fromTable.name() : cls.getSimpleName();
}
use of com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable in project elide by yahoo.
the class TableType method buildAnnotations.
private static Map<Class<? extends Annotation>, Annotation> buildAnnotations(Table table) {
Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<>();
annotations.put(Include.class, getIncludeAnnotation(table));
if (table.getSql() != null && !table.getSql().isEmpty()) {
annotations.put(FromSubquery.class, new FromSubquery() {
@Override
public Class<? extends Annotation> annotationType() {
return FromSubquery.class;
}
@Override
public String sql() {
return table.getSql();
}
@Override
public String dbConnectionName() {
return table.getDbConnectionName();
}
});
} else {
annotations.put(FromTable.class, new FromTable() {
@Override
public Class<? extends Annotation> annotationType() {
return FromTable.class;
}
@Override
public String name() {
String tableName = table.getTable();
if (table.getSchema() != null && !table.getSchema().isEmpty()) {
return table.getSchema() + "." + tableName;
}
return tableName;
}
@Override
public String dbConnectionName() {
return table.getDbConnectionName();
}
});
}
annotations.put(TableMeta.class, new TableMeta() {
@Override
public Class<? extends Annotation> annotationType() {
return TableMeta.class;
}
@Override
public String friendlyName() {
return table.getFriendlyName();
}
@Override
public String description() {
return table.getDescription();
}
@Override
public String category() {
return table.getCategory();
}
@Override
public String[] hints() {
return table.getHints().toArray(new String[0]);
}
@Override
public String[] tags() {
return table.getTags().toArray(new String[0]);
}
@Override
public String filterTemplate() {
return table.getFilterTemplate();
}
@Override
public boolean isFact() {
return table.getIsFact();
}
@Override
public boolean isHidden() {
return table.getHidden() != null && table.getHidden();
}
@Override
public CardinalitySize size() {
if (table.getCardinality() == null || table.getCardinality().isEmpty()) {
return CardinalitySize.UNKNOWN;
}
return CardinalitySize.valueOf(table.getCardinality().toUpperCase(Locale.ENGLISH));
}
@Override
public ArgumentDefinition[] arguments() {
return getArgumentDefinitions(table.getArguments());
}
});
String readPermission = table.getReadAccess();
if (StringUtils.isNotEmpty(readPermission)) {
annotations.put(ReadPermission.class, new ReadPermission() {
@Override
public Class<? extends Annotation> annotationType() {
return ReadPermission.class;
}
@Override
public String expression() {
return readPermission;
}
});
}
return annotations;
}
use of com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable in project elide by yahoo.
the class TableTypeTest method testTableAnnotations.
@Test
void testTableAnnotations() throws Exception {
Set<String> tags = new HashSet<>(Arrays.asList("tag1", "tag2"));
Set<String> hints = new HashSet<>(Arrays.asList("hint1", "hint2"));
Table testTable = Table.builder().cardinality("medium").description("A test table").friendlyName("foo").table("table1").name("Table").schema("db1").category("category1").readAccess("Admin").dbConnectionName("dbConn").isFact(true).filterTemplate("a==b").tags(tags).hints(hints).build();
TableType testType = new TableType(testTable);
Include include = (Include) testType.getAnnotation(Include.class);
assertEquals("Table", include.name());
FromTable fromTable = (FromTable) testType.getAnnotation(FromTable.class);
assertEquals("db1.table1", fromTable.name());
assertEquals("dbConn", fromTable.dbConnectionName());
TableMeta tableMeta = (TableMeta) testType.getAnnotation(TableMeta.class);
assertEquals("foo", tableMeta.friendlyName());
assertEquals(CardinalitySize.MEDIUM, tableMeta.size());
assertEquals("A test table", tableMeta.description());
assertEquals("category1", tableMeta.category());
assertTrue(tableMeta.isFact());
assertEquals(tags, new HashSet<>(Arrays.asList(tableMeta.tags())));
assertEquals(hints, new HashSet<>(Arrays.asList(tableMeta.hints())));
assertEquals("a==b", tableMeta.filterTemplate());
ReadPermission readPermission = (ReadPermission) testType.getAnnotation(ReadPermission.class);
assertEquals("Admin", readPermission.expression());
}
Aggregations