package xyz.xkind.core.sql.lambda;import java.util.Objects;import java.util.function.Predicate;/**
* Represents a predicate (boolean-valued function) of one argument.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #test(Object)}.
*
* @param <T> the type of the input to the predicate
*
* @since 1.8
*/@FunctionalInterfacepublicinterfaceSqlPredicate<T>extendsPredicate<T>, SerializedExpression<SqlWrapper>{/**
* Returns a composed predicate that represents a short-circuiting logical
* AND of this predicate and another. When evaluating the composed
* predicate, if this predicate is {@code false}, then the {@code other}
* predicate is not evaluated.
*
* <p>Any exceptions thrown during evaluation of either predicate are relayed
* to the caller; if evaluation of this predicate throws an exception, the
* {@code other} predicate will not be evaluated.
*
* @param other a predicate that will be logically-ANDed with this
* predicate
* @return a composed predicate that represents the short-circuiting logical
* AND of this predicate and the {@code other} predicate
* @throws NullPointerException if other is null
*/default SqlPredicate<T>and(SqlPredicate<?super T> other){
Objects.requireNonNull(other);return(t)->test(t)&& other.test(t);}/**
* Returns a predicate that represents the logical negation of this
* predicate.
*
* @return a predicate that represents the logical negation of this
* predicate
*/default SqlPredicate<T>negate(){return(t)->!test(t);}/**
* Returns a composed predicate that represents a short-circuiting logical
* OR of this predicate and another. When evaluating the composed
* predicate, if this predicate is {@code true}, then the {@code other}
* predicate is not evaluated.
*
* <p>Any exceptions thrown during evaluation of either predicate are relayed
* to the caller; if evaluation of this predicate throws an exception, the
* {@code other} predicate will not be evaluated.
*
* @param other a predicate that will be logically-ORed with this
* predicate
* @return a composed predicate that represents the short-circuiting logical
* OR of this predicate and the {@code other} predicate
* @throws NullPointerException if other is null
*/default SqlPredicate<T>or(SqlPredicate<?super T> other){
Objects.requireNonNull(other);return(t)->test(t)|| other.test(t);}/**
* Returns a predicate that tests if two arguments are equal according
* to {@link Objects#equals(Object, Object)}.
*
* @param <T> the type of arguments to the predicate
* @param targetRef the object reference with which to compare for equality,
* which may be {@code null}
* @return a predicate that tests if two arguments are equal according
* to {@link Objects#equals(Object, Object)}
*/static<T> SqlPredicate<T>isEqual(Object targetRef){return(null == targetRef)? Objects::isNull
: object -> targetRef.equals(object);}}
SqlFunction
package xyz.xkind.core.sql.lambda;import java.util.Objects;import java.util.function.Function;/**
* Represents a function that accepts one argument and produces a result.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #apply(Object)}.
*
* @param <T> the type of the input to the function
* @param <R> the type of the result of the function
*
* @since 1.8
*/@FunctionalInterfacepublicinterfaceSqlFunction<T, R>extendsFunction<T, R>, SerializedExpression<SqlWrapper>{/**
* Returns a composed function that first applies the {@code before}
* function to its input, and then applies this function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of input to the {@code before} function, and to the
* composed function
* @param before the function to apply before this function is applied
* @return a composed function that first applies the {@code before}
* function and then applies this function
* @throws NullPointerException if before is null
*
* @see #andThen(Function)
*/default<V> SqlFunction<V, R>compose(SqlFunction<?super V,?extendsT> before){
Objects.requireNonNull(before);return(V v)->apply(before.apply(v));}/**
* Returns a composed function that first applies this function to
* its input, and then applies the {@code after} function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of output of the {@code after} function, and of the
* composed function
* @param after the function to apply after this function is applied
* @return a composed function that first applies this function and then
* applies the {@code after} function
* @throws NullPointerException if after is null
*
* @see #compose(Function)
*/default<V> SqlFunction<T, V>andThen(SqlFunction<?super R,?extendsV> after){
Objects.requireNonNull(after);return(T t)-> after.apply(apply(t));}/**
* Returns a function that always returns its input argument.
*
* @param <T> the type of the input and output objects to the function
* @return a function that always returns its input argument
*/static<T> SqlFunction<T, T>identity(){return t -> t;}}
调用
import java.time.LocalDate;import java.util.Date;import lombok.Data;import xyz.xkind.core.sql.lambda.SqlPredicate;import xyz.xkind.core.sql.lambda.SqlVisitor;publicclassTestLambdaToSql{@DataclassPerson{private String name;privateint age;private LocalDate create;private Date login;}publicstaticvoidmain(String[] args){final LocalDate ld = LocalDate.of(1970,1,1);final Date date =newDate();
SqlPredicate<Person> sp_0 = p -> p.getName().equals("root");
SqlPredicate<Person> sp_1 = p -> p.getName().startsWith("ro")|| p.getName().endsWith("ot");
SqlPredicate<Person> sp_2 = p -> p.getName().contains("root");
SqlVisitor visitor =newSqlVisitor("`","`");
System.out.println(sp_0.sql(visitor)+ System.lineSeparator());
visitor.clear();
System.out.println(sp_1.sql(visitor)+ System.lineSeparator());
visitor.clear();
System.out.println(sp_2.sql(visitor)+ System.lineSeparator());
SqlPredicate<Person> sp_3 = p -> p.getAge()>18&& p.getLogin().before(date)&& p.getCreate().isAfter(ld);
System.out.println(sp_3.sql()+ System.lineSeparator());}}