org.datanucleus.sql4o.query.WhereExpression Maven / Gradle / Ivy
Go to download
sql4o was written by Travis Reeder and provides an SQL
interface to the db4o object datastore.
The newest version!
/**********************************************************************
Copyright (c) 2006 Travis Reeder and others. All rights reserved.
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.
Contributors:
...
**********************************************************************/
package org.datanucleus.sql4o.query;
import java.util.List;
import java.util.ArrayList;
/**
*
*/
public class WhereExpression implements Cloneable
{
public static final String OP_EQUALS = "=";
public static final String OP_GREATER = ">";
public static final String OP_GREATER_OR_EQUAL = ">=";
public static final String OP_LESS = "<";
public static final String OP_LESS_OR_EQUAL = "<=";
public static final String AND = "AND";
public static final String OR = "OR";
public static final String OP_NOT_EQUAL = "<>";
public static final String OP_NOT_EQUAL_2 = "!="; // why not
private List expressions = new ArrayList();
private String type = AND;
private String field;
private String operator;
private String value;
private boolean root;
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}
public WhereExpression(String type)
{
this.type = type;
if (type == null)
{
this.root = true;
}
}
public WhereExpression()
{
}
public String toString()
{
StringBuffer buff = new StringBuffer();
// if(type != null) buff.append(type).append(" ");
if (expressions.size() > 0)
{
if (!root)
buff.append(" (");
for (int i = 0; i < expressions.size(); i++)
{
WhereExpression whereExpression = expressions.get(i);
if (i > 0)
{
buff.append(" ").append(whereExpression.getType());
}
buff.append(whereExpression.toString());
}
if (!root)
buff.append(" )");
}
else
buff.append(" ").append(field).append(" ").append(operator).append(" ").append(value);
return buff.toString();
}
public void add(WhereExpression sub)
{
expressions.add(sub);
}
public void setField(String field)
{
this.field = field;
}
public void setOperator(String operator)
{
this.operator = operator;
}
public void setValue(String value)
{
this.value = value;
}
public List getExpressions()
{
return expressions;
}
public String getType()
{
return type;
}
public String getField()
{
return field;
}
public String getOperator()
{
return operator;
}
public String getValue()
{
return value;
}
public boolean isRoot()
{
return root;
}
}