Search in sources :

Example 1 with FromSubquery

use of com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery 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);
}
Also used : SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) FromSubquery(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) Annotation(java.lang.annotation.Annotation)

Example 2 with FromSubquery

use of com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery 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();
}
Also used : Subselect(org.hibernate.annotations.Subselect) FromSubquery(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable)

Example 3 with FromSubquery

use of com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery 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;
}
Also used : CardinalitySize(com.yahoo.elide.datastores.aggregation.annotation.CardinalitySize) HashMap(java.util.HashMap) FromSubquery(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) TableMeta(com.yahoo.elide.datastores.aggregation.annotation.TableMeta) ReadPermission(com.yahoo.elide.annotation.ReadPermission) Annotation(java.lang.annotation.Annotation)

Example 4 with FromSubquery

use of com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery in project elide by yahoo.

the class TableTypeTest method testTableSQL.

@Test
void testTableSQL() throws Exception {
    Table testTable = Table.builder().sql("SELECT * FROM FOO").name("Table").dbConnectionName("dbConn").build();
    TableType testType = new TableType(testTable);
    FromSubquery fromSubquery = (FromSubquery) testType.getAnnotation(FromSubquery.class);
    assertEquals("SELECT * FROM FOO", fromSubquery.sql());
    assertEquals("dbConn", fromSubquery.dbConnectionName());
}
Also used : Table(com.yahoo.elide.modelconfig.model.Table) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) FromSubquery(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery) Test(org.junit.jupiter.api.Test)

Aggregations

FromSubquery (com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery)4 FromTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable)4 Annotation (java.lang.annotation.Annotation)2 ReadPermission (com.yahoo.elide.annotation.ReadPermission)1 CardinalitySize (com.yahoo.elide.datastores.aggregation.annotation.CardinalitySize)1 TableMeta (com.yahoo.elide.datastores.aggregation.annotation.TableMeta)1 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)1 Table (com.yahoo.elide.modelconfig.model.Table)1 HashMap (java.util.HashMap)1 Subselect (org.hibernate.annotations.Subselect)1 Test (org.junit.jupiter.api.Test)1