com.github.chengyuxing.sql.dsl.Query Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rabbit-sql Show documentation
Show all versions of rabbit-sql Show documentation
Light wrapper of JDBC, support ddl, dml, query, plsql/procedure/function, transaction and manage sql
file.
package com.github.chengyuxing.sql.dsl;
import com.github.chengyuxing.common.DataRow;
import com.github.chengyuxing.common.tuple.Pair;
import com.github.chengyuxing.sql.PagedResource;
import com.github.chengyuxing.sql.dsl.clause.GroupBy;
import com.github.chengyuxing.sql.dsl.clause.OrderBy;
import com.github.chengyuxing.sql.dsl.clause.Where;
import com.github.chengyuxing.sql.dsl.types.FieldReference;
import com.github.chengyuxing.sql.plugins.PageHelperProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import org.jetbrains.annotations.Unmodifiable;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Stream;
/**
* DSL query operator.
*
* @param entity type
* @param self type
*/
public interface Query> {
/**
* Where clause.
* Support flatten style and nested style to build the complex condition struct, and invoke
* {@link Where#peek(BiConsumer)} to check the build result.
* By default, all condition concat with {@code and}, excepted {@link Where#and(Function, boolean...)}
* and {@link Where#or(Function, boolean...)} .
* SQL:
*
* where id > 5 and id < 10 or id in (17, 18, 19)
*
* Flatten:
*
* .where(g -> g.gt(Guest::getId, 5))
* .where(g -> g.lt(Guest::getId, 10))
* .where(g -> g.or(o -> o.in(Guest::getId, Arrays.asList(17, 18, 19))))
*
* Nested:
*
* .where(g -> g.gt(Guest::getId, 5)
* .lt(Guest::getId, 10)
* .or(o -> o.in(Guest::getId, Arrays.asList(17, 18, 19))))
*
*
* @param where where builder
* @return self
*/
SELF where(@NotNull Function, Where> where);
/**
* Group by clause.
* Support complex having clause struct likes {@link #where(Function)}, e.g.
* SQL:
*
* select age,
* count(*) as count_all,
* max(age) as max_age,
* avg(age) as avg_age
* from ...
* group by age
* having count(*) > 1
*
* Group by builder:
*
* .groupBy(g -> g.count()
* .max(Guest::getAge)
* .avg(Guest::getAge)
* .by(Guest::getAge)
* .having(h -> h.count(StandardOperator.GT, 1))
* )
*
*
* @param group group by builder
* @return self
*/
SELF groupBy(@NotNull Function, GroupBy> group);
/**
* Order by clause.
*
* @param order order by builder
* @return self
*/
SELF orderBy(@NotNull Function, OrderBy> order);
/**
* Select columns into the result, if empty select will include all column.
*
* @param columns the column that without annotation {@link javax.persistence.Transient @Transient}
* @return self
*/
SELF select(@NotNull List> columns);
/**
* Deselect columns into the result.
*
* @param columns the column that without annotation {@link javax.persistence.Transient @Transient}
* @return self
*/
SELF deselect(@NotNull List> columns);
/**
* Check the built result currently.
*
* @param consumer (sql, (name parameter sql, params)) -> _
* @return self
*/
SELF peek(@NotNull BiConsumer>> consumer);
/**
* Collect result to DataRow stream (use {@code try-with-resource} to close at the end).
*
* @return stream
* @see com.github.chengyuxing.sql.support.JdbcSupport#executeQueryStream(String, Map) java8 stream query
*/
Stream toRowStream();
/**
* Collect result to DataRow stream (use {@code try-with-resource} to close at the end).
*
* @return stream
* @see com.github.chengyuxing.sql.support.JdbcSupport#executeQueryStream(String, Map) java8 stream query
*/
Stream toStream();
/**
* Collect result to list.
*
* @return list
*/
List toList();
/**
* Collect result to list.
*
* @param mapper item mapper
* @param result type
* @return list
*/
List toList(@NotNull Function mapper);
/**
* Collect.
*
* @param mapper item mapper
* @param collector result collector
* @param result type
* @param mapped value type
* @return any
*/
R collect(@NotNull Function mapper, @NotNull Collector collector);
/**
* Collect.
*
* @param collector result collector
* @param result type
* @return any
*/
R collect(@NotNull Collector collector);
/**
* Find first item.
*
* @return optional first item
*/
@NotNull Optional findFirst();
/**
* Get first item.
*
* @return first item
*/
@Nullable T getFirst();
/**
* Collect result to paged resource.
*
* @param page page number
* @param size page size
* @param pageHelperProvider page helper provider for current.
* @return paged resource
*/
@NotNull PagedResource toPagedResource(@Range(from = 1, to = Integer.MAX_VALUE) int page,
@Range(from = 1, to = Integer.MAX_VALUE) int size,
@Nullable PageHelperProvider pageHelperProvider);
/**
* Collect result to paged resource.
*
* @param page page number
* @param size page size
* @return paged resource
*/
@NotNull PagedResource toPagedResource(@Range(from = 1, to = Integer.MAX_VALUE) int page,
@Range(from = 1, to = Integer.MAX_VALUE) int size);
/**
* Collect result to paged resource.
*
* @param page page number
* @param size page size
* @param pageHelperProvider page helper provider for current.
* @return paged resource
*/
@NotNull PagedResource toPagedRowResource(@Range(from = 1, to = Integer.MAX_VALUE) int page,
@Range(from = 1, to = Integer.MAX_VALUE) int size,
@Nullable PageHelperProvider pageHelperProvider);
/**
* Collect result to paged resource.
*
* @param page page number
* @param size page size
* @return paged resource
*/
@NotNull PagedResource toPagedRowResource(@Range(from = 1, to = Integer.MAX_VALUE) int page,
@Range(from = 1, to = Integer.MAX_VALUE) int size);
/**
* Check the result exists or not by condition.
* Notice: Query will not include the {@link #groupBy(Function) Group by clause}.
*
* @return true exists otherwise false
*/
boolean exists();
/**
* Get query result count.
*
* @return result count
*/
@Range(from = 0, to = Long.MAX_VALUE)
long count();
/**
* Collect.
*
* @param mapper item mapper
* @param collector result collector
* @param result type
* @param mapped value type
* @return any
*/
R collectRow(@NotNull Function mapper, @NotNull Collector collector);
/**
* Collect.
*
* @param collector result collector
* @param result type
* @return any
*/
R collectRow(@NotNull Collector collector);
/**
* Find first row.
*
* @return optional row
*/
@NotNull Optional findFirstRow();
/**
* Get first row.
*
* @return first row
*/
@NotNull DataRow getFirstRow();
/**
* Collect to row list.
*
* @return list
*/
List toRows();
/**
* Collect to map list.
*
* @return list
*/
List