com.litongjava.table.services.DbSqlService Maven / Gradle / Ivy
package com.litongjava.table.services;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.litongjava.db.TableInput;
import com.litongjava.table.model.DataQueryRequest;
import com.litongjava.table.model.Sql;
import com.litongjava.table.utils.ObjectUtils;
/**
* @author Ping E Lee
* @version 1.0
* @date 2020年8月27日_下午7:11:29
* @desc
*/
public class DbSqlService {
private OperatorService operatorService = new OperatorService();
/**
* 移除kv中不需要的值
*
* @param kv
*/
private void removeKv(TableInput kv) {
// {pageNo=1, pageSize=10,
// tableName=cron4j_task,orderBy=update_time,isAsc=false}
kv.remove("page_no");
kv.remove("page_size");
kv.remove("table_name");
kv.remove("order_by");
kv.remove("is_asc");
kv.remove("columns");
}
public Sql getWhereClause(DataQueryRequest queryRequest, TableInput kv) {
// 移除kv中的值
// pageNo,pageSize,tableName,orderBy,orderBy,isAsc
removeKv(kv);
// 获取查询条件
Sql whereClause = getWhereQueryClause(kv);
StringBuffer where = whereClause.getWhere();
String orderBy = queryRequest.getOrderBy();
Boolean isAsc = queryRequest.getIsAsc();
String groupBy = queryRequest.getGroupBy();
if (orderBy != null) {
where.append(" order by ").append(orderBy);
}
if (isAsc != null && !isAsc) {
where.append(" desc");
}
if (groupBy != null) {
where.append(" group by ").append(groupBy);
}
whereClause.setWhere(where);
return whereClause;
}
/**
* 根据kv中的键和值生成sql语句,并返回参数
*
* @param kv
* @return
*/
public Sql getWhereQueryClause(TableInput kv) {
StringBuffer sql = new StringBuffer();
sql.append("where ");
// 查询条件的值
List