/* Copyright 2013 David Wolverton
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.googlecode.genericdao.search;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* This class provides two methods for generating query language to fulfill an
* <code>ISearch</code>.
* <ol>
* <li><code>generateQL()</code> - is used for getting the actual search
* results.</li>
* <li><code>generateRowCountQL()</code> - is used for getting just the number
* of results.</li>
* </ol>
* Both methods return a query language sting and a list of values for filling
* named parameters. For example the following query and parameter list might be
* returned:
*
* <pre>
* select _it from com.example.Cat _it
* where _it.age > :p1 and _it.name != :p2
*
* parameter list: [3, 'Mittens']
* </pre>
*
* This is an abstract class. A subclass must be used to implement individual
* query languages. Currently only HQL query language is supported (
* <code>com.googlecode.genericdao.dao.hibernate.HibernateSearchToQLProcessor</code>). The that
* implementation could be used for EQL query language as well with no or minor
* modifications.
*/
public abstract class BaseSearchProcessor {
private static Logger logger = LoggerFactory.getLogger(BaseSearchProcessor.class);
protected static int QLTYPE_HQL = 0;
protected static int QLTYPE_EQL = 1;
protected int qlType;
protected MetadataUtil metadataUtil;
protected String rootAlias = "_it";
protected static final String ROOT_PATH = "";
protected BaseSearchProcessor(int qlType, MetadataUtil metadataUtil) {
if (metadataUtil == null) {
throw new IllegalArgumentException("A SearchProcessor cannot be initialized with a null MetadataUtil.");
}
this.qlType = qlType;
this.metadataUtil = metadataUtil;
}
/**
* The MetadataUtil used by this search processor. This can only be set in
* the constructor.
*/
public MetadataUtil getMetadataUtil() {
return metadataUtil;
}
/**
* This is the string used to represent the root entity of the search within
* the query. The default value is <code>"_it"</code>. It may be necessary
* to use a different alias if there are entities in the data model with the
* name or property "_it".
*/
public void setRootAlias(String alias) {
this.rootAlias = alias;
}
/**
* Generate the QL string for a given search. Fill paramList with the values
* to be used for the query. All parameters within the query string are
* specified as named parameters ":pX", where X is the index of the
* parameter value in paramList.
*/
public String generateQL(Class<?> entityClass, ISearch search, List<Object> paramList) {
if (entityClass == null)
throw new NullPointerException("The entity class for a search cannot be null");
SearchContext ctx = new SearchContext(entityClass, rootAlias, paramList);
List<Field> fields = checkAndCleanFields(search.getFields());
applyFetches(ctx, checkAndCleanFetches(search.getFetches()), fields);
String select = generateSelectClause(ctx, fields, search.isDistinct());
String where = generateWhereClause(ctx, checkAndCleanFilters(search.getFilters()), search.isDisjunction());
String orderBy = generateOrderByClause(ctx, checkAndCleanSorts(search.getSorts()));
String from = generateFromClause(ctx, true);
StringBuilder sb = new StringBuilder();
sb.append(select);
sb.append(from);
sb.append(where);
sb.append(orderBy);
String query = sb.toString();
if (logger.isDebugEnabled())
logger.debug("generateQL:\n " + query);
return query;
}
/**
* Generate the QL string that will query the total number of results from a
* given search (paging is ignored). Fill paramList with the values to be
* used for the query. All parameters within the query string are specified
* as named parameters ":pX", where X is the index of the parameter value in
* paramList.
*
* <b>NOTE:</b> Returns null if column operators are used in the search.
* Such a search will always return 1 row.
*/
public String generateRowCountQL(Class<?> entityClass, ISearch search, List<Object> paramList) {
if (entityClass == null)
throw new NullPointerException("The entity class for a search cannot be null");
SearchContext ctx = new SearchContext(entityClass, rootAlias, paramList);
String where = generateWhereClause(ctx, checkAndCleanFilters(search.getFilters()), search.isDisjunction());
String from = generateFromClause(ctx, false);
boolean useOperator = false, notUseOperator = false;
List<Field> fields = search.getFields();
if (fields != null) {
for (Field field : fields) {
switch (field.getOperator()) {
case Field.OP_AVG:
case Field.OP_COUNT:
case Field.OP_COUNT_DISTINCT:
case Field.OP_MAX:
case Field.OP_MIN:
case Field.OP_SUM:
useOperator = true;
break;
default:
notUseOperator = true;
break;
}
}
}
if (useOperator && notUseOperator) {
throw new Error("A search can not have a mix of fields with operators and fields without operators.");
} else if (useOperator) {
return null; // if we're using column operators, the query will
// always return 1 result.
}
StringBuilder sb = new StringBuilder();
if (!search.isDistinct()) {
sb.append("select count(").append(rootAlias).append(")");
} else if (fields.size() == 0) {
sb.append("select count(distinct ");
sb.append(rootAlias).append(")");
} else if (fields.size() == 1) {
sb.append("select count(distinct ");
String prop = fields.get(0).getProperty();
if (prop == null || "".equals(prop)) {
sb.append(ctx.getRootAlias());
} else {
sb.append(getPathRef(ctx, prop));
}
sb.append(")");
} else {
throw new IllegalArgumentException("Unfortunately, Hibernate Generic DAO does not currently support "
+ "the count operation on a search that has distinct set with multiple fields.");
}
sb.append(from);
sb.append(where);
String query = sb.toString();
if (logger.isDebugEnabled())
logger.debug("generateRowCountQL:\n " + query);
return query;
}
/**
* Internal method for generating the select clause based on the fields of
* the given search.
*/
protected String generateSelectClause(SearchContext ctx, List<Field> fields, boolean distinct) {
StringBuilder sb = null;
boolean useOperator = false, notUseOperator = false;
boolean first = true;
if (fields != null) {
for (Field field : fields) {
if (first) {
sb = new StringBuilder("select ");
if (distinct)
sb.append("distinct ");
first = false;
} else {
sb.append(", ");
}
if (field.getOperator() == Field.OP_CUSTOM) {
// Special case for custom operator.
if (field.getProperty() == null) {
sb.append("null");
} else {
appendCustomExpression(sb, ctx, field.getProperty());
}
} else {
String prop;
if (field.getProperty() == null || "".equals(field.getProperty())) {
prop = ctx.getRootAlias();
} else {
AliasNode aliasNodeForProperty = getAliasForPathIfItExists(ctx, field.getProperty());
if (aliasNodeForProperty != null) {
prop = aliasNodeForProperty.alias;
} else {
prop = getPathRef(ctx, field.getProperty());
}
}
switch (field.getOperator()) {
case Field.OP_AVG:
sb.append("avg(");
useOperator = tru
没有合适的资源?快使用搜索试试~ 我知道了~
ERP CRM SCM 等系统开发框架
共1306个文件
java:539个
js:277个
jsp:161个
0 下载量 35 浏览量
2023-07-13
15:24:50
上传
评论
收藏 4.24MB ZIP 举报
温馨提示
致力开发 开发效率最高,运行效率最高的java web框架 jdk要求 1.8 基于 spring mvc+spring +Hibernate. 该框架适用于ERP CRM SCM 等系统开发。 也可以通过RMI,支持分布式网站系统,电商平台,手机app服务器 Dao数据操作仅仅增删改查,没有特殊需求,不需要写Dao接口和Dao实现类,用下面这种引入方式: @DataAccess(entity = RolePo.class) private CommonDao<RolePo,Integer> roleDao; 数据库sql文件
资源推荐
资源详情
资源评论
收起资源包目录
ERP CRM SCM 等系统开发框架 (1306个子文件)
leave.bpmn 8KB
my_leave.bpmn 7KB
purchase.bpmn 5KB
CHANGES 3KB
bootstrap.css 144KB
bootstrap.min.css 122KB
bootstrap.css 118KB
bootstrap.min.css 98KB
custom.css 80KB
animate.min.css 52KB
jquery.mCustomScrollbar.min.css 42KB
font-awesome.css 37KB
font-awesome.min.css 32KB
bootstrap-theme.css 26KB
style-common.css 25KB
bootstrap-datepicker3.standalone.css 23KB
bootstrap-theme.min.css 23KB
bootstrap-datepicker3.css 22KB
bootstrap-datepicker3.standalone.min.css 21KB
bootstrap-datepicker3.min.css 21KB
bootstrap-datepicker.standalone.css 18KB
bootstrap-datepicker.css 17KB
jquery.datetimepicker.min.css 16KB
bootstrap-datepicker.standalone.min.css 16KB
bootstrap-datepicker.min.css 15KB
bootstrap-theme.css 15KB
bootstrap-theme.min.css 13KB
bootstrap-datetimepicker.min.css 11KB
style-editor.css 10KB
style.css 8KB
ng-grid-2.0.7.min.css 7KB
bootstrap-progressbar-3.3.4.css 6KB
daterangepicker-bs3.css 5KB
bootstrap-progressbar-3.3.4.min.css 5KB
editor.css 3KB
style.css 2KB
eidea.css 2KB
jasmine.css 2KB
nprogress.css 1KB
bootstrap.addtabs.css 728B
fontawesome-webfont.eot 162KB
glyphicons-halflings-regular.eot 20KB
glyphicons-halflings-regular.eot 20KB
glyphicons-halflings-regular.eot 20KB
activiti-admin-webfont.eot 5KB
controller.ftl 6KB
model.ftl 5KB
edit.ftl 4KB
list.ftl 3KB
normalController.ftl 2KB
pagingByDBController.ftl 2KB
serviceClass.ftl 2KB
hibernate_po.ftl 1KB
bo.ftl 815B
serviceInterface.ftl 735B
daoclass.ftl 436B
daoInterface.ftl 270B
class_header.ftl 110B
loading.gif 17KB
nw-handle-dark.gif 839B
se-handle-dark.gif 838B
.gitignore 90B
.gitignore 23B
event-listeners-popup.html 10KB
form-properties-popup.html 9KB
execution-listeners-popup.html 8KB
editor.html 8KB
task-listeners-popup.html 7KB
index.html 5KB
fields-popup.html 4KB
out-parameters-popup.html 3KB
in-parameters-popup.html 3KB
signal-definitions-popup.html 3KB
assignment-popup.html 3KB
message-definitions-popup.html 2KB
subprocess-reference-popup.html 2KB
sequenceflow-order-popup.html 2KB
save-model.html 2KB
index.html 2KB
index.min.html 2KB
condition-expression-popup.html 1KB
stencil-item-template.html 1012B
unsaved-changes.html 977B
test.html 964B
select-shape.html 811B
feedback-popup.html 798B
assignment-display-template.html 796B
text-popup.html 713B
root-stencil-item-template.html 463B
message-property-write-template.html 275B
signal-property-write-template.html 268B
string-property-write-mode-template.html 239B
multiinstance-property-write-template.html 232B
default-value-display-template.html 231B
index.html 210B
execution-listeners-display-template.html 209B
event-listeners-display-template.html 197B
form-properties-display-template.html 195B
out-parameters-display-template.html 192B
task-listeners-display-template.html 192B
共 1306 条
- 1
- 2
- 3
- 4
- 5
- 6
- 14
资源评论
智达教育
- 粉丝: 2w+
- 资源: 279
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功