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

co.streamx.fluent.SQL.spi.SQLConfiguration Maven / Gradle / Ivy

package co.streamx.fluent.SQL.spi;

import static co.streamx.fluent.SQL.Operators.EXISTS;
import static co.streamx.fluent.SQL.Operators.IN;

import java.util.Collection;
import java.util.ServiceLoader;

import co.streamx.fluent.JPA.spi.JPAConfiguration;
import co.streamx.fluent.JPA.spi.SQLConfigurator;
import co.streamx.fluent.SQL.Library;
import co.streamx.fluent.SQL.Operators;
import co.streamx.fluent.SQL.ScalarFunctions;
import co.streamx.fluent.functions.Function1;
import co.streamx.fluent.functions.Function2;

public class SQLConfiguration implements JPAConfiguration {
    public SQLConfiguration() {
        ServiceLoader loader = ServiceLoader.load(SQLConfigurator.class);
        SQLConfigurator SQLConfig = loader.iterator().next();

        SQLConfig.registerMethodSubstitution(String::concat, ScalarFunctions::CONCAT);
        SQLConfig.registerMethodSubstitution(String::toLowerCase, ScalarFunctions::LOWER);
        SQLConfig.registerMethodSubstitution(String::toUpperCase, ScalarFunctions::UPPER);

        SQLConfig.registerMethodSubstitution(String::replace, ScalarFunctions::REPLACE);
        SQLConfig.registerMethodSubstitution(String::trim, ScalarFunctions::TRIM);
        SQLConfig.registerMethodSubstitution(String::matches, Operators::LIKE);

        SQLConfig.registerMethodSubstitution(
                (Function2, Comparable, Boolean>) Collection::contains, (collection,
                                                                                                item) -> IN(item,
                                                                                                        collection));

        SQLConfig.registerMethodSubstitution((Function1>, Integer>) c -> c.size(),
                collection -> Library.COUNT());
        SQLConfig.registerMethodSubstitution((Function1>, Boolean>) Collection::isEmpty,
                c -> !EXISTS(c));

        SQLConfig.registerMethodSubstitution((Function1) Math::abs, ScalarFunctions::ABS);
        SQLConfig.registerMethodSubstitution((Function1) Math::acos, ScalarFunctions::ACOS);
        SQLConfig.registerMethodSubstitution((Function1) Math::asin, ScalarFunctions::ASIN);
        SQLConfig.registerMethodSubstitution((Function1) Math::atan, ScalarFunctions::ATAN);
        SQLConfig.registerMethodSubstitution((Function1) Math::ceil, ScalarFunctions::CEILING);
        SQLConfig.registerMethodSubstitution((Function1) Math::cos, ScalarFunctions::COS);
        SQLConfig.registerMethodSubstitution((Function1) Math::toDegrees, ScalarFunctions::DEGREES);
        SQLConfig.registerMethodSubstitution((Function1) Math::exp, ScalarFunctions::EXP);
        SQLConfig.registerMethodSubstitution((Function1) Math::floor, ScalarFunctions::FLOOR);

        SQLConfig.registerMethodSubstitution((Function2) Math::pow, ScalarFunctions::POWER);
        SQLConfig.registerMethodSubstitution((Function1) Math::toRadians, ScalarFunctions::RADIANS);
        // SQLConfig.registerMethodSubstitution((Function0) Math::random, ScalarFunctions::);
        // ROUND approach differs between java to SQL
        // SQLConfig.registerMethodSubstitution((Function1) Math::round, ScalarFunctions::ROUND);
        SQLConfig.registerMethodSubstitution((Function1) Math::signum, ScalarFunctions::SIGN);
        SQLConfig.registerMethodSubstitution((Function1) Math::sin, ScalarFunctions::SIN);
        SQLConfig.registerMethodSubstitution((Function1) Math::sqrt, ScalarFunctions::SQRT);
        SQLConfig.registerMethodSubstitution((Function1) Math::tan, ScalarFunctions::TAN);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy