All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
tech.guyi.ipojo.module.h2.where.WhereBuilder Maven / Gradle / Ivy
package tech.guyi.ipojo.module.h2.where;
import lombok.Getter;
import org.apache.commons.dbutils.ResultSetHandler;
import tech.guyi.ipojo.module.h2.entity.Entity;
import tech.guyi.ipojo.module.h2.entry.DbEntity;
import tech.guyi.ipojo.module.h2.entry.page.Page;
import tech.guyi.ipojo.module.h2.entry.page.PageRequest;
import tech.guyi.ipojo.module.h2.executor.JdbcExecutor;
import tech.guyi.ipojo.module.h2.executor.handler.SingleIntegerResultHandler;
import tech.guyi.ipojo.module.h2.type.BeanHandler;
import tech.guyi.ipojo.module.h2.type.BeanListHandler;
import tech.guyi.ipojo.module.h2.where.condition.builder.AndWhereConditionBuilder;
import tech.guyi.ipojo.module.h2.where.condition.builder.OrWhereConditionBuilder;
import tech.guyi.ipojo.module.h2.where.condition.builder.OrderConditionBuilder;
import tech.guyi.ipojo.module.h2.where.condition.builder.WhereConditionBuilder;
import tech.guyi.ipojo.module.h2.where.condition.converter.WhereConditionTypeConverter;
import tech.guyi.ipojo.module.h2.where.condition.type.WhereConditionType;
import java.sql.SQLException;
import java.util.*;
public class WhereBuilder {
private static SingleIntegerResultHandler singleIntegerResultHandler = new SingleIntegerResultHandler();
@Getter
private DbEntity entity;
private Map types;
private List converters;
private JdbcExecutor executor;
private BeanHandler beanHandler;
private BeanListHandler beanListHandler;
public WhereBuilder(DbEntity entity,
Map types,
List converters,
JdbcExecutor executor,
BeanHandler beanHandler,
BeanListHandler beanListHandler) {
this.entity = entity;
this.types = types;
this.converters = converters;
this.executor = executor;
this.beanHandler = beanHandler;
this.beanListHandler = beanListHandler;
}
private List conditionBuilders = new LinkedList<>();
private List getArgs(){
List args = new ArrayList<>();
for (WhereConditionBuilder condition : this.conditionBuilders) {
args.add(condition.getItem().getValue());
}
return args;
}
public WhereConditionBuilder and(){
WhereConditionBuilder builder = new AndWhereConditionBuilder<>(this,types);
this.conditionBuilders.add(builder);
return builder;
}
public WhereConditionBuilder and(String name,Object value){
WhereConditionBuilder builder = new AndWhereConditionBuilder<>(this,types,name,value);
this.conditionBuilders.add(builder);
return builder;
}
public WhereConditionBuilder or(){
WhereConditionBuilder builder = new OrWhereConditionBuilder<>(this,types);
this.conditionBuilders.add(builder);
return builder;
}
public WhereConditionBuilder or(String name,Object value){
WhereConditionBuilder builder = new OrWhereConditionBuilder<>(this,types,name,value);
this.conditionBuilders.add(builder);
return builder;
}
public String getSql(){
StringBuilder conditions = new StringBuilder();
Collections.sort(this.conditionBuilders, new Comparator() {
@Override
public int compare(WhereConditionBuilder o1, WhereConditionBuilder o2) {
return Integer.compare(o1.orderNum(),o2.orderNum());
}
});
for (WhereConditionBuilder condition : this.conditionBuilders) {
conditions.append(" ").append(condition.getSql(this.converters)).append(" ");
}
return String.format(
" from %s where 1 = 1 %s ",
this.entity.getTableName(),
conditions.toString()
);
}
public int delete() {
String sql = String.format("delete %s",this.getSql());
try {
return this.executor.update(sql,this.getArgs());
} catch (SQLException e) {
throw new SqlRuntimeException(e);
}
}
public int count() {
String sql = String.format("select count(1) %s",this.getSql());
try {
return this.executor.query(sql,singleIntegerResultHandler,this.getArgs());
} catch (SQLException e) {
throw new SqlRuntimeException(e);
}
}
public List query(ResultSetHandler> handler) {
String sql = String.format("select * %s",this.getSql());
try {
return this.executor.query(sql,handler,this.getArgs());
} catch (SQLException e) {
throw new SqlRuntimeException(e);
}
}
public List query() {
return this.query(this.beanListHandler);
}
public R single(ResultSetHandler handler) {
String sql = String.format("select * %s limit 0,1",this.getSql());
try {
return this.executor.query(sql,handler,this.getArgs());
} catch (SQLException e) {
throw new SqlRuntimeException(e);
}
}
public E single() {
return this.single(this.beanHandler);
}
public WhereConditionBuilder orderBy(String name,String order){
WhereConditionBuilder builder = new OrderConditionBuilder<>(this,types,name,order);
this.conditionBuilders.add(builder);
return builder;
}
public Page page(PageRequest request){
return this.page(request,this.beanListHandler);
}
public Page page(PageRequest request,ResultSetHandler> handler){
String sql = String.format("select * %s limit %s,%s",this.getSql(),request.getStart(),request.getEnd());
try {
List content = this.executor.query(sql,handler,this.getArgs());
long total = this.count();
return new Page<>(request.getPage(),request.getPageSize(),total,content);
} catch (SQLException e) {
throw new SqlRuntimeException(e);
}
}
}