注册
登录
搜索
标签
帮助
无线搜索论坛
无线搜索技术
开源项目 bba96中的DAO实现-5-DAO辅助类
上一主题
下一主题
发新话题
发布投票
发布商品
发布悬赏
发布活动
发布辩论
发布视频
打印
开源项目 bba96中的DAO实现-5-DAO辅助类
jeffer
jeffer
总司令


个人空间
发短消息
加为好友
当前离线
1#大 中小 发表于 2008-2-27 11:29 只看该作者
开源项目 bba96中的DAO实现-5-DAO辅助类
1ueryObject
package com.bba96.core.query;
/*** @author <a href="mailto:scorpio_leon@hotmail.com">Leon Li</a>
**/public interface QueryObject<T> {
/*** Return the instance of entity's class type.
** @return class type
*/@SuppressWarnings("unchecked")
public Class<T> getEntityClass();
/*** Set the instance of entity's class type.
** @param class type
*/@SuppressWarnings("unchecked")
public void setEntityClass(Class<T> entityClass);
/*** Return the instance of QueryProjections.
** @return the instance of QueryProjections
*/public QueryProjections getQueryProjections();
/*** Set the instance of QueryProjections.
** @param the instance of QueryProjections
*/public void setQueryProjections(QueryProjections queryProjection);
/*** Return the instance of QueryParam.
** @return the instance of QueryParam
*/public QueryParam getQueryParam();
/*** Set the instance of QueryParam.
** @param the instance of QueryParam
*/public void setQueryParam(QueryParam queryParam);
}2ueryObjectBase
package com.bba96.core.query;
/*** @author <a href="mailto:scorpio_leon@hotmail.com">Leon Li</a>
**/public class QueryObjectBase<T> implements QueryObject<T> {
protected static final String CACHE_KEY = "QueryObject_cache";
/*** the instance of entity's class type
*/@SuppressWarnings("unchecked")
private Class<T> entityClass;
/*** the instance of QueryProjections
*/private QueryProjections queryProjections;
/*** the instance of QueryParam
*/private QueryParam queryParam;
/*** Return the instance of entity's class type.
** @return class type
*/@SuppressWarnings("unchecked")
public Class<T> getEntityClass() {
return entityClass;
}/*** Set the instance of entity's class type.
** @param class type
*/@SuppressWarnings("unchecked")
public void setEntityClass(Class<T> entityClass) {
this.entityClass = entityClass;
}/*** Return the instance of QueryProjections.
** @return the instance of QueryProjections
*/public QueryProjections getQueryProjections() {
return queryProjections;
}/*** Set the instance of QueryProjections.
** @param the instance of QueryProjections
*/public void setQueryProjections(QueryProjections queryProjections) {
this.queryProjections = queryProjections;
}/*** Return the instance of QueryParam.
** @return the instance of QueryParam
*/public QueryParam getQueryParam() {
return queryParam;
}/*** Set the instance of QueryParam.
** @param the instance of QueryParam
*/public void setQueryParam(QueryParam queryParam) {
this.queryParam = queryParam;
}}3ueryParam
package com.bba96.core.query;
import java.util.LinkedList;
import java.util.List;
/*** Advanced Query Parameters, can be nested unlimited. Example:
** <pre>
* QueryParam queryParam1 = new QueryParam("name", "like", "admin%");
* QueryParam queryParam2 = new QueryParam("id", ">", new Integer(100));
* QueryParam queryParam3 = new QueryParam("age", "<", new Integer(30));
* QueryParam queryParam4 = new QueryParam("creationDate > '2004-10-24'");
* QueryParam queryParam = new QueryParam();
* queryParam.and(queryParam1);
* queryParam.and(queryParam2);
* queryParam.not(queryParam3);
* queryParam.not(queryParam4);
* </pre>
** the above code will generate the sql: where (name like admin% and id > 100) and not age < 30 and not creationDate > '2004-10-24')
** Note: A QueryParam can not be nested when his model is BASIC.
** @author <a href="mailto:scorpio_leon@hotmail.com">Leon Li</a>
**/public final class QueryParam {
public final static String AND = "and";
public final static String OR = "or";
public final static String NOT = "not";
public final static String OPERATOR_EQ = "=";
public final static String OPERATOR_NE = "!=";
public final static String OPERATOR_NE_ANSINULL_OFF = "!=%";
public final static String OPERATOR_GE = ">=";
public final static String OPERATOR_GT = ">";
public final static String OPERATOR_NGE = "!>=";
public final static String OPERATOR_NGT = "!>";
public final static String OPERATOR_LE = "<=";
public final static String OPERATOR_LT = "<";
public final static String OPERATOR_NLE = "!<=";
public final static String OPERATOR_NLT = "!<";
public final static String OPERATOR_LIKE = "like";
public final static String OPERATOR_NLIKE = "!like";
public final static String OPERATOR_INCLUDE = "include";
public final static String OPERATOR_NINCLUDE = "!include";
public final static String OPERATOR_ILIKE = "ilike";
public final static String OPERATOR_NILIKE = "!ilike";
public final static String OPERATOR_IINCLUDE = "iinclude";
public final static String OPERATOR_NIINCLUDE = "!iinclude";
public final static String OPERATOR_IS = "is";
public final static String OPERATOR_NIS = "!is";
public final static String OPERATOR_IN = "in";
public final static String OPERATOR_NIN = "!in";
public final static String FETCH = "fetch";
public final static int BASIC = 1;
public final static int ADVANCED = 2;
private String name;
private Object value;
private int type;
private String operator = OPERATOR_EQ;
private String sql;
private int queryMode;
private List<QueryParam> andParams;
private List<QueryParam> orParams;
private List<QueryParam> notParams;
public QueryParam(String sql) {
this.queryMode = BASIC;
this.sql = sql;
}public QueryParam(String name, String operator, Object value, int type) {
this.queryMode = BASIC;
setName(name);
setOperator(operator);
setValue(value);
this.type = type;
checkOperatorForNullValue();
}public QueryParam(String name, String operator, Object value) {
this.queryMode = BASIC;
setName(name);
setOperator(operator);
setValue(value);
this.type = -1;
checkOperatorForNullValue();
}public QueryParam(String name, Object value, int type) {
this.queryMode = BASIC;
setName(name);
setValue(value);
this.type = type;
checkOperatorForNullValue();
}public QueryParam(String name, Object value) {
this.queryMode = BASIC;
setName(name);
setValue(value);
this.type = -1;
checkOperatorForNullValue();
}public QueryParam() {
}public void and(QueryParam queryParam) {
if (this.queryMode == BASIC) {
throw new RuntimeException ("Current QueryParam was set as BASIC mode, can not be added a QueryParam!");
}this.queryMode = ADVANCED;
if (andParams == null) {
andParams = new LinkedList<QueryParam>();
}andParams.add(queryParam);
}public void or(QueryParam queryParam) {
if (this.queryMode == BASIC) {
throw new RuntimeException ("Current QueryParam was set as BASIC mode, can not be added a QueryParam!");
}this.queryMode = ADVANCED;
if (orParams == null) {
orParams = new LinkedList<QueryParam>();
}orParams.add(queryParam);
}public void not(QueryParam queryParam) {
if (this.queryMode == BASIC) {
throw new RuntimeException ("Current QueryParam was set as BASIC mode, can not be added a QueryParam!");
}this.queryMode = ADVANCED;
if (notParams == null) {
notParams = new LinkedList<QueryParam>();
}notParams.add(queryParam);
}public String getName() {
return name;
}public String getOperator() {
return operator;
}public int getType() {
return type;
}public Object getValue() {
return value;
}public String getSql() {
return sql;
}private void setName(String name) {
if (name == null) {
throw new RuntimeException ("arameter name can not be NULL!");
}this.name = name.trim();
}private void setOperator(String operator) {
if (operator == null) {
throw new RuntimeException ("Operator can not be NULL!");
}this.operator = operator.trim();
validateOperator();
transferOperator();
}private void setValue(Object value) {
this.value = value;
transferOperator();
}private void transferOperator() {
if (value == null) {
if (OPERATOR_EQ.equals(this.operator)) {
this.operator = OPERATOR_IS;
} else if (OPERATOR_NE.equals(this.operator)) {
this.operator = OPERATOR_NIS;
}} else {
if (OPERATOR_IS.equals(this.operator)) {
this.operator = OPERATOR_EQ;
} else if (OPERATOR_NIS.equals(this.operator)) {
this.operator = OPERATOR_NE;
}}}private void checkOperatorForNullValue() {
if (this.value == null) {
if (!(OPERATOR_IS.equals(this.operator) || OPERATOR_NIS.equals(this.operator))) {
throw new RuntimeException ("The operator can only be set 'is' or '=' or 'is not' or '!=' when value is NULL!");
}} else if (OPERATOR_IS.equals(this.operator) || OPERATOR_NIS.equals(this.operator)) {
throw new RuntimeException ("The operator 'is' or 'is not' is available only when value is NULL!");
}}private void validateOperator() {
if (this.operator.startsWith("!")) {
if (this.operator.endsWith("%"))
return;
if (OPERATOR_NE.equals(this.operator))
return;
if (OPERATOR_NGE.equals(this.operator))
return;
if (OPERATOR_NGT.equals(this.operator))
return;
if (OPERATOR_NLE.equals(this.operator))
return;
if (OPERATOR_NLT.equals(this.operator))
return;
if (OPERATOR_NLIKE.equals(this.operator))
return;
if (OPERATOR_NINCLUDE.equals(this.operator))
return;
if (OPERATOR_NILIKE.equals(this.operator))
return;
if (OPERATOR_NIINCLUDE.equals(this.operator))
return;
if (OPERATOR_NIS.equals(this.operator))
return;
if (OPERATOR_NIN.equals(this.operator))
return;
} else {
if (OPERATOR_EQ.equals(this.operator))
return;
if (OPERATOR_GE.equals(this.operator))
return;
if (OPERATOR_GT.equals(this.operator))
return;
if (OPERATOR_LE.equals(this.operator))
return;
if (OPERATOR_LT.equals(this.operator))
return;
if (OPERATOR_LIKE.equals(this.operator))
return;
if (OPERATOR_INCLUDE.equals(this.operator))
return;
if (OPERATOR_ILIKE.equals(this.operator))
return;
if (OPERATOR_IINCLUDE.equals(this.operator))
return;
if (OPERATOR_IS.equals(this.operator))
return;
if (OPERATOR_IN.equals(this.operator))
return;
if (FETCH.equals(this.operator))
return;
}throw new RuntimeException ("The operator " + this.operator + " could be incorrect!");
}public int getMode() {
return queryMode;
}public List<QueryParam> getAndParams() {
return andParams;
}public List<QueryParam> getNotParams() {
return notParams;
}public List<QueryParam> getOrParams() {
return orParams;
}@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append('(');
if (queryMode == BASIC) {
if (sql != null) {
sb.append(sql);
} else {
sb.append(name).append(' ').append(operator).append(' ').append(value);
}} else {
if (andParams != null && andParams.size() > 0) {
boolean firstFlag = true;
for (QueryParam q : andParams) {
if (firstFlag) {
firstFlag = false;
} else {
sb.append(" and ");
}sb.append(q.toString());
}}if (orParams != null && orParams.size() > 0) {
if (sb.length() > 1) {
sb.append(" or ");
}boolean firstFlag = true;
for (QueryParam q : orParams) {
if (firstFlag) {
firstFlag = false;
} else {
sb.append(" or ");
}sb.append(q.toString());
}}if (notParams != null && notParams.size() > 0) {
if (sb.length() > 0) {
sb.append(" and ");
}boolean firstFlag = true;
for (QueryParam q : notParams) {
if (firstFlag) {
firstFlag = false;
} else {
sb.append(" and ");
}sb.append("not ");
sb.append(q.toString());
}}}sb.append(')');
return sb.toString();
}}4
ueryProjections
package com.bba96.core.query;
/*** @author <a href="mailto:scorpio_leon@hotmail.com">Leon Li</a>
**/public class QueryProjections implements Cloneable {
public static QueryProjections countProjection;
static {
if (countProjection == null) {
countProjection = new QueryProjections();
countProjection.setRowCount(true);
}}private boolean rowCount;
private String[] max;
private String[] min;
private String[] avg;
private String[] sum;
private String[] count;
private boolean distinctFlag;
private String[] countDistinct;
private String[] groupProperty; //TODO having
private String[] distinct;
private String[] property;
private String[] orderProperty;
private boolean[] descFlag;
public String[] getAvg() {
return avg;
}public String[] getCount() {
return count;
}public String[] getCountDistinct() {
return countDistinct;
}public boolean[] getDescFlag() {
return descFlag;
}public String[] getGroupProperty() {
return groupProperty;
}public String[] getMax() {
return max;
}public String[] getMin() {
return min;
}public String[] getOrderProperty() {
return orderProperty;
}public boolean isRowCount() {
return rowCount;
}public String[] getSum() {
return sum;
}public void setAvg(String[] avg) {
this.avg = avg;
}public void setAvg(String avg) {
this.avg = toArray(avg);
}public void setCount(String[] count) {
this.count = count;
}public void setCount(String count) {
this.count = toArray(count);
}public void setCountDistinct(String[] countDistinct) {
this.countDistinct = countDistinct;
}public void setCountDistinct(String countDistinct) {
this.countDistinct = toArray(countDistinct);
}public void setDescFlag(boolean[] descFlag) {
this.descFlag = descFlag;
}public void setDescFlag(boolean descFlag) {
this.descFlag = toArray(descFlag);
}public void setGroupProperty(String[] groupProperty) {
this.groupProperty = groupProperty;
}public void setGroupProperty(String groupProperty) {
this.groupProperty = toArray(groupProperty);
}public void setMax(String[] max) {
this.max = max;
}public void setMax(String max) {
this.max = toArray(max);
}public void setMin(String[] min) {
this.min = min;
}public void setMin(String min) {
this.min = toArray(min);
}public void setOrderProperty(String[] orderProperty) {
this.orderProperty = orderProperty;
}public void setOrderProperty(String orderProperty) {
this.orderProperty = toArray(orderProperty);
}public void setRowCount(boolean rowCount) {
this.rowCount = rowCount;
}public void setSum(String[] sum) {
this.sum = sum;
}public void setSum(String sum) {
this.sum = toArray(sum);
}public boolean isDistinctFlag() {
return distinctFlag;
}public void setDistinctFlag(boolean distinctFlag) {
this.distinctFlag = distinctFlag;
}public String[] getDistinct() {
return distinct;
}public void setDistinct(String[] distinct) {
this.distinct = distinct;
}public void setDistinct(String distinct) {
this.distinct = toArray(distinct);
}public String[] getProperty() {
return property;
}public void setProperty(String[] property) {
this.property = property;
}public void setProperty(String property) {
this.property = toArray(property);
}private String[] toArray(String str) {
String[] array = new String[1];
array[0] = str;
return array;
}private boolean[] toArray(boolean bool) {
boolean[] array = new boolean[1];
array[0] = bool;
return array;
}public QueryProjections clone() {
QueryProjections cloneObject = null;
try {
cloneObject = (QueryProjections)super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}return cloneObject;
}}欢迎大家一起交流!我的QQ:185278122;邮箱:185278122@qq.com;无线搜索论坛网址:www.wxss.org;QQ群:26996585。
UID6 帖子3894 精华184
积分35672 财富65638 贡献12629 威望385 阅读权限20 性别男 来自北京 在线时间595 小时 注册时间2007-2-1 最后登录2008-11-3
查看个人网站
查看详细资料
TOP 上一主题
下一主题
控制面板首页
编辑个人资料
积分记录
公众用户组
升级个人空间
当前时区 GMT+8, 现在时间是 2009-1-9 19:00 粤ICP备07021541号
清除 Cookies
- 联系我们
- 无线搜索论坛
- Archiver
- WAP
- TOP
Powered by Discuz!
6.0.0 (C) 2001-2007 Comsenz Inc.
内页
内页
内页
直接浏览
提示:以上根据您的指令使用Timewe浏览服务访问的www.wxss.org网站,其内容、服务或立场跟Timewe无关
...
欢迎举报存在违法、不良信息的网站,净化网络环境
Wap推荐:
极品游戏大作