All Downloads are FREE. Search and download functionalities are using the official Maven repository.

estivate.core.ast.parser.TableParser Maven / Gradle / Ivy

The newest version!
package estivate.core.ast.parser;

import java.lang.annotation.Annotation;

import estivate.annotations.Table;
import estivate.core.ast.EstivateAST;
import estivate.core.ast.ExpressionAST;
import estivate.core.ast.QueryAST;
import estivate.core.ast.lang.TableQueryAST;
import estivate.core.ast.parser.EstivateParser.AnnotationParser;
import estivate.utils.AnnotationsUtils;

/**
 * Parse {@link Table} annotation
 * 
 * @author Benoit Theunissen
 *
 */
public class TableParser implements AnnotationParser {

    public static final TableParser INSTANCE = new TableParser();

    public static final Class TYPE = Table.class;

    public void parseAnnotation(EstivateAST ast, Annotation[] annotations) {
        Table annotation = AnnotationsUtils.find(annotations, TYPE);
        if (annotation != null) {
            ast.addQuery(parse(annotation));
        }
    }

    public void parseAnnotation(ExpressionAST ast, Annotation[] annotations) {
        Table annotation = AnnotationsUtils.find(annotations, TYPE);
        if (annotation != null) {
            ast.setOptional(annotation.optional());
            ast.addQuery(parse(annotation));
        }
    }

    public static QueryAST parse(Table annotation) {
        TableQueryAST query = new TableQueryAST();

        query.setRowSelector(annotation.rowSelect());
        query.setUnique(true);
        query.setIndex(annotation.index());
        query.setFirst(annotation.first());
        query.setLast(annotation.last());
        query.setQueryString(SelectParser.or(annotation.select(), annotation.value()));

        SelectParser.validate(query);

        return query;
    }
}