Commit d0b3994d authored by yanzg's avatar yanzg

默认日期格式的支持

parent 2ce6dbaa
package com.yanzuoguang.dao.impl;
import com.yanzuoguang.dao.cond.SqlCond;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 将条件排序,符合索引
*
* @author 颜佐光
*/
class SqlCondUtil {
/**
* 时间等级
*/
public static final int COND_DATE = 0;
/**
* ID等级
*/
public static final int COND_ID = 1;
/**
* 普通相等
*/
public static final int COND_COMMON = 2;
/**
* 普通IN,NOT IN
*/
public static final int COND_IN = 3;
/**
* Like条件
*/
public static final int COND_LIKE = 4;
/**
* 全文索引
*/
public static final int COND_MATCH = 5;
/**
* 将SQL语句条件排序,符合索引
*
* @param fields
*/
public static List<SqlDataField> sortCond(List<SqlDataField> fields) {
// 最大级别
Map<Integer, List<SqlDataField>> mapLevel = new HashMap<>();
for (SqlDataField field : fields) {
int level = getCondLevel(field);
List<SqlDataField> levelCond = mapLevel.get(level);
if (levelCond == null) {
levelCond = new ArrayList<>();
mapLevel.put(level, levelCond);
}
levelCond.add(field);
}
// 添加到排序好的条件
List<SqlDataField> tos = new ArrayList<>();
for (int i = 0; i <= COND_MATCH; i++) {
List<SqlDataField> levelCond = mapLevel.get(i);
if (levelCond == null) {
continue;
}
tos.addAll(levelCond);
}
return tos;
}
private static int getCondLevel(SqlDataField field) {
if (field == null) {
return COND_COMMON;
}
SqlCond<?> cond = field.getCond();
for (String fieldName : cond.getFields()) {
fieldName = fieldName.toLowerCase();
if (fieldName.indexOf("date") > -1 || fieldName.indexOf("time") > -1) {
return COND_DATE;
} else if (fieldName.indexOf("id") > -1) {
return COND_ID;
}
}
int len = field.getCodes().size() / 2;
for (int i = 0; i < len; i++) {
String codeName = field.getCodes().get(i * 2);
String codeValue = field.getCodes().get(i * 2 + 1).toLowerCase();
if (codeValue.indexOf("in") > -1) {
return COND_IN;
} else if (codeValue.indexOf("like") > -1) {
return COND_LIKE;
} else if (codeValue.indexOf("match") > -1) {
return COND_MATCH;
}
}
return COND_COMMON;
}
}
......@@ -6,7 +6,9 @@ import com.yanzuoguang.dao.cond.SqlCondDefault;
import com.yanzuoguang.util.helper.StringHelper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* SQL语句基本信息
......@@ -400,6 +402,15 @@ public class SqlData {
return to;
}
/**
* 条件排序
*/
public SqlData sortCond() {
SqlCondUtil.sortCond(this.sqlDataFields);
return this;
}
/**
* 获取默认条件
*
......
......@@ -45,31 +45,6 @@ public class TableSqlCache {
this.table = new TableStruct();
}
/**
* 添加SQL语句组
*
* @param sqls
*/
public void add(SqlData... sqls) {
for (SqlData sql : sqls) {
this.add(sql);
}
}
/**
* 添加可执行的SQL语句
*
* @param sql SQL语句
*/
public SqlData add(SqlData sql) {
if (sql != null) {
this.nameCache.put(sql.getName(), sql);
String keyType = String.valueOf(sql.getSqlType());
this.typeCache.get(keyType, new ArrayList<>()).add(sql);
}
return sql;
}
/**
* 获取所有的Sql语句执行类型
......@@ -82,6 +57,17 @@ public class TableSqlCache {
return typeCache.get(keyType);
}
/**
* 添加SQL语句组
*
* @param sqls
*/
public void add(SqlData... sqls) {
for (SqlData sql : sqls) {
this.add(sql);
}
}
/**
* 添加SQL
*
......@@ -108,6 +94,22 @@ public class TableSqlCache {
return this.add(to);
}
/**
* 添加可执行的SQL语句
*
* @param sql SQL语句
*/
public SqlData add(SqlData sql) {
if (sql != null) {
// 排序查询条件,用于排序优先级
sql.sortCond();
this.nameCache.put(sql.getName(), sql);
String keyType = String.valueOf(sql.getSqlType());
this.typeCache.get(keyType, new ArrayList<>()).add(sql);
}
return sql;
}
/**
* 生成GroupSQL语句
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment