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.
/**
* eobjects.org MetaModel
* Copyright (C) 2010 eobjects.org
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.eobjects.metamodel.jdbc.dialects;
import java.util.List;
import org.eobjects.metamodel.jdbc.JdbcDataContext;
import org.eobjects.metamodel.query.AbstractQueryClause;
import org.eobjects.metamodel.query.FilterClause;
import org.eobjects.metamodel.query.FilterItem;
import org.eobjects.metamodel.query.FromClause;
import org.eobjects.metamodel.query.FromItem;
import org.eobjects.metamodel.query.GroupByClause;
import org.eobjects.metamodel.query.GroupByItem;
import org.eobjects.metamodel.query.OperatorType;
import org.eobjects.metamodel.query.OrderByClause;
import org.eobjects.metamodel.query.OrderByItem;
import org.eobjects.metamodel.query.Query;
import org.eobjects.metamodel.query.SelectClause;
import org.eobjects.metamodel.query.SelectItem;
import org.eobjects.metamodel.schema.ColumnType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Abstract implementation of query rewriter. This implementation delegates the
* rewriting of the Query into several subtasks according to the query items to
* be rendered. This makes it easy to overload single methods in order to
* correct syntax quirks.
*/
public abstract class AbstractQueryRewriter implements IQueryRewriter {
protected final Logger logger = LoggerFactory.getLogger(getClass());
private final JdbcDataContext _dataContext;
public AbstractQueryRewriter(JdbcDataContext dataContext) {
_dataContext = dataContext;
}
public JdbcDataContext getDataContext() {
return _dataContext;
}
@Override
public ColumnType getColumnType(int jdbcType, String nativeType, Integer columnSize) {
return ColumnType.convertColumnType(jdbcType);
}
public String rewriteQuery(Query query) {
query = beforeRewrite(query);
final StringBuilder sb = new StringBuilder();
sb.append(rewriteSelectClause(query, query.getSelectClause()));
sb.append(rewriteFromClause(query, query.getFromClause()));
sb.append(rewriteWhereClause(query, query.getWhereClause()));
sb.append(rewriteGroupByClause(query, query.getGroupByClause()));
sb.append(rewriteHavingClause(query, query.getHavingClause()));
sb.append(rewriteOrderByClause(query, query.getOrderByClause()));
return sb.toString();
}
public boolean isSchemaIncludedInColumnPaths() {
return false;
}
/**
* Method to modify query before rewriting begins. Overwrite this method if
* you want to change parts of the query that are not just rendering
* related. Cloning the query before modifying is recommended in order to
* not violate referential integrity of clients (the query is mutable).
*
* @param strategy
* @param query
* @return the modified query
*/
protected Query beforeRewrite(Query query) {
return query;
}
@Override
public String rewriteColumnType(ColumnType columnType) {
return columnType.toString();
}
protected String rewriteOrderByClause(Query query, OrderByClause orderByClause) {
StringBuilder sb = new StringBuilder();
if (orderByClause.getItemCount() > 0) {
sb.append(AbstractQueryClause.PREFIX_ORDER_BY);
List items = orderByClause.getItems();
for (int i = 0; i < items.size(); i++) {
OrderByItem item = items.get(i);
if (i != 0) {
sb.append(AbstractQueryClause.DELIM_COMMA);
}
sb.append(rewriteOrderByItem(query, item));
}
}
return sb.toString();
}
@Override
public String rewriteFromItem(FromItem item) {
return rewriteFromItem(item.getQuery(), item);
}
protected String rewriteOrderByItem(Query query, OrderByItem item) {
return item.toSql(isSchemaIncludedInColumnPaths());
}
protected String rewriteHavingClause(Query query, FilterClause havingClause) {
StringBuilder sb = new StringBuilder();
if (havingClause.getItemCount() > 0) {
sb.append(AbstractQueryClause.PREFIX_HAVING);
List items = havingClause.getItems();
for (int i = 0; i < items.size(); i++) {
FilterItem item = items.get(i);
if (i != 0) {
sb.append(AbstractQueryClause.DELIM_AND);
}
sb.append(rewriteFilterItem(item));
}
}
return sb.toString();
}
protected String rewriteGroupByClause(Query query, GroupByClause groupByClause) {
StringBuilder sb = new StringBuilder();
if (groupByClause.getItemCount() > 0) {
sb.append(AbstractQueryClause.PREFIX_GROUP_BY);
List items = groupByClause.getItems();
for (int i = 0; i < items.size(); i++) {
GroupByItem item = items.get(i);
if (i != 0) {
sb.append(AbstractQueryClause.DELIM_COMMA);
}
sb.append(rewriteGroupByItem(query, item));
}
}
return sb.toString();
}
protected String rewriteGroupByItem(Query query, GroupByItem item) {
return item.toSql(isSchemaIncludedInColumnPaths());
}
protected String rewriteWhereClause(Query query, FilterClause whereClause) {
StringBuilder sb = new StringBuilder();
if (whereClause.getItemCount() > 0) {
sb.append(AbstractQueryClause.PREFIX_WHERE);
List items = whereClause.getItems();
for (int i = 0; i < items.size(); i++) {
FilterItem item = items.get(i);
if (i != 0) {
sb.append(AbstractQueryClause.DELIM_AND);
}
sb.append(rewriteFilterItem(item));
}
}
return sb.toString();
}
@Override
public String rewriteFilterItem(FilterItem item) {
if (item.isCompoundFilter()) {
FilterItem[] childItems = item.getChildItems();
StringBuilder sb = new StringBuilder();
sb.append('(');
for (int i = 0; i < childItems.length; i++) {
FilterItem child = childItems[i];
if (i != 0) {
sb.append(' ');
sb.append(item.getLogicalOperator().toString());
sb.append(' ');
}
sb.append(rewriteFilterItem(child));
}
sb.append(')');
return sb.toString();
}
final String primaryFilterSql = item.toSql(isSchemaIncludedInColumnPaths());
final OperatorType operator = item.getOperator();
if (operator == OperatorType.DIFFERENT_FROM) {
final Object operand = item.getOperand();
if (operand != null) {
// special case in SQL where NULL is not treated as a value -
// see Ticket #1058
FilterItem isNullFilter = new FilterItem(item.getSelectItem(), OperatorType.EQUALS_TO, null);
final String secondaryFilterSql = rewriteFilterItem(isNullFilter);
return '(' + primaryFilterSql + " OR " + secondaryFilterSql + ')';
}
}
return primaryFilterSql;
}
protected String rewriteFromClause(Query query, FromClause fromClause) {
StringBuilder sb = new StringBuilder();
if (fromClause.getItemCount() > 0) {
sb.append(AbstractQueryClause.PREFIX_FROM);
List items = fromClause.getItems();
for (int i = 0; i < items.size(); i++) {
FromItem item = items.get(i);
if (i != 0) {
sb.append(AbstractQueryClause.DELIM_COMMA);
}
sb.append(rewriteFromItem(query, item));
}
}
return sb.toString();
}
protected String rewriteFromItem(Query query, FromItem item) {
return item.toSql(isSchemaIncludedInColumnPaths());
}
protected String rewriteSelectClause(Query query, SelectClause selectClause) {
StringBuilder sb = new StringBuilder();
if (selectClause.getItemCount() > 0) {
sb.append(AbstractQueryClause.PREFIX_SELECT);
if (selectClause.isDistinct()) {
sb.append("DISTINCT ");
}
List items = selectClause.getItems();
for (int i = 0; i < items.size(); i++) {
SelectItem item = items.get(i);
if (i != 0) {
sb.append(AbstractQueryClause.DELIM_COMMA);
}
sb.append(rewriteSelectItem(query, item));
}
}
return sb.toString();
}
protected String rewriteSelectItem(Query query, SelectItem item) {
return item.toSql(isSchemaIncludedInColumnPaths());
}
}