All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eobjects.metamodel.jdbc.dialects.DefaultQueryRewriter Maven / Gradle / Ivy

/**
 * 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.FilterItem;
import org.eobjects.metamodel.query.FromItem;
import org.eobjects.metamodel.query.Query;
import org.eobjects.metamodel.query.SelectItem;

/**
 * Generic query rewriter that adds syntax enhancements that are only possible
 * to resolve just before execution time.
 */
public class DefaultQueryRewriter extends AbstractQueryRewriter {

	private static final String SPECIAL_ALIAS_CHARACTERS = "- ,.|*%()!#¤/\\=?;:~";

	public DefaultQueryRewriter(JdbcDataContext dataContext) {
		super(dataContext);
	}

	@Override
	protected Query beforeRewrite(JdbcDataContext strategy, Query query) {
		query = query.clone();

		if (strategy != null) {
			String identifierQuoteString = strategy.getIdentifierQuoteString();
			if (identifierQuoteString != null) {
				List selectItems = query.getSelectClause()
						.getItems();
				for (SelectItem item : selectItems) {
					String alias = item.getAlias();
					if (needsQuoting(alias, identifierQuoteString)) {
						item.setAlias(identifierQuoteString + alias
								+ identifierQuoteString);
					}
				}
				List fromItems = query.getFromClause().getItems();
				for (FromItem item : fromItems) {
					String alias = item.getAlias();
					if (needsQuoting(alias, identifierQuoteString)) {
						item.setAlias(identifierQuoteString + alias
								+ identifierQuoteString);
					}
				}
			}
		}
		return query;
	}

	private boolean needsQuoting(String alias, String identifierQuoteString) {
		boolean result = false;
		if (alias != null && identifierQuoteString != null) {
			if (alias.indexOf(identifierQuoteString) == -1) {
				for (int i = 0; i < SPECIAL_ALIAS_CHARACTERS.length(); i++) {
					char specialCharacter = SPECIAL_ALIAS_CHARACTERS.charAt(i);
					if (alias.indexOf(specialCharacter) != -1) {
						result = true;
						break;
					}
				}
			}
		}
		if (logger.isDebugEnabled()) {
			logger.debug("needsQuoting(" + alias + "," + identifierQuoteString
					+ ") = " + result);
		}
		return result;
	}

	@Override
	protected String rewriteFilterItem(JdbcDataContext strategy, Query query,
			FilterItem item) {
		Object operand = item.getOperand();
		if (operand != null && operand instanceof String) {
			String str = (String) operand;
			// escape single quotes
			if (str.indexOf('\'') != -1) {
				str = escapeFilterItemQuotes(str);
				return super.rewriteFilterItem(strategy, query, new FilterItem(
						item.getSelectItem(), item.getOperator(), str));
			}
		}
		return super.rewriteFilterItem(strategy, query, item);
	}

	public String escapeFilterItemQuotes(String filterItemOperand) {
		return filterItemOperand.replaceAll("\\'", "\\'\\'");
	}

	@Override
	public boolean isFirstRowSupported() {
		return false;
	}

	@Override
	public boolean isMaxRowsSupported() {
		return false;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy