org.databene.dbsanity.parser.StringLengthParser Maven / Gradle / Ivy
/*
* (c) Copyright 2011 by Volker Bergmann. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, is permitted under the terms of the
* GNU General Public License (GPL).
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
* REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
* HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.databene.dbsanity.parser;
import java.util.Set;
import org.databene.commons.CollectionUtil;
import org.databene.commons.StringUtil;
import org.databene.commons.SyntaxError;
import org.databene.commons.xml.XMLUtil;
import org.databene.dbsanity.model.query.DefectQueryCheck;
import org.w3c.dom.Element;
/**
* Parses the <stringLength> part of a <check> and creates a SQL defect query.
* Created: 26.05.2011 12:44:57
* @since 0.8
* @author Volker Bergmann
*/
public class StringLengthParser extends DbSanityXMLParser {
private static final Set REQUIRED_ATTRIBUTES = CollectionUtil.toSet("expression");
private static final Set OPTIONAL_ATTRIBUTES = CollectionUtil.toSet("minLength", "maxLength");
public StringLengthParser() {
super("stringLength", REQUIRED_ATTRIBUTES, OPTIONAL_ATTRIBUTES, DefectQueryCheck.class);
}
@Override
public Object parse(Element element, Object[] parentPath, DbSanityParseContext context) {
// check parent
DefectQueryCheck parent = (DefectQueryCheck) this.parent(parentPath);
String table = parent.getTable();
if (StringUtil.isEmpty(table))
throw new SyntaxError("Missing 'table' spec in outer ", XMLUtil.format(element));
// parse XML element
String expression = getRequiredAttribute("expression", element);
String minLengthSpec = getOptionalAttribute("minLength", element);
String maxLengthSpec = getOptionalAttribute("maxLength", element);
if (StringUtil.isEmpty(minLengthSpec) && StringUtil.isEmpty(maxLengthSpec))
throw new SyntaxError("'minLength' or 'maxLength' must be specified", XMLUtil.format(element));
Integer minLength = (minLengthSpec != null ? Integer.parseInt(minLengthSpec) : null);
Integer maxLength = (maxLengthSpec != null ? Integer.parseInt(maxLengthSpec) : null);
// create query
StringBuilder builder = new StringBuilder("select * from ").append(table).append(" where not ");
builder.append("length(").append(expression).append(") ");
if (minLength != null && maxLength != null)
builder.append("between ").append(minLength).append(" and ").append(maxLength);
else if (minLength != null)
builder.append(" >= ").append(minLength);
else
builder.append(" <= ").append(maxLength);
String defectQuery = builder.toString();
parent.setQuery(defectQuery);
return defectQuery;
}
}