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

org.eclipse.jnosql.diana.query.method.FindByMethodQueryProvider Maven / Gradle / Ivy

/*
 *  Copyright (c) 2018 Otávio Santana and others
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Apache License v2.0 which accompanies this distribution.
 *  The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 *  and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
 *  You may elect to redistribute this code under either of these licenses.
 *  Contributors:
 *  Otavio Santana
 */
package org.eclipse.jnosql.diana.query.method;

import jakarta.nosql.Sort;
import jakarta.nosql.SortType;
import jakarta.nosql.query.SelectQuery;
import org.antlr.v4.runtime.tree.ParseTree;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;

final class FindByMethodQueryProvider extends AbstractMethodQueryProvider implements BiFunction {

    private List sorts = new ArrayList<>();

    @Override
    public SelectQuery apply(String query, String entity) {
        Objects.requireNonNull(query, " query is required");
        Objects.requireNonNull(entity, " entity is required");
        runQuery(MethodQuery.of(query).get());
        return new MethodSelectQuery(entity, sorts, where);
    }

    @Override
    public void exitOrderName(MethodParser.OrderNameContext ctx) {
        sorts.add(this.sort(ctx));
    }

    @Override
    Function getParserTree() {
        return MethodParser::findBy;
    }

    private Sort sort(MethodParser.OrderNameContext context) {
        String text = context.variable().getText();
        SortType type = context.desc() == null ? SortType.ASC : SortType.DESC;
        return Sort.of(getFormatField(text), type);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy