001: /*
002: * Copyright 2006 Davide Deidda
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: /*
018: * QueryFilter.java
019: *
020: * Created on 4 ottobre 2003, 10.27
021: */
022:
023: package it.biobytes.ammentos.query;
024:
025: import java.sql.*;
026:
027: import it.biobytes.ammentos.*;
028: import static it.biobytes.ammentos.FieldTypeEnum.*;
029:
030: /**
031: *
032: * @author davide
033: */
034: public class SimpleQueryFilter implements QueryFilter {
035:
036: private int m_previousAppender;
037: private String m_fieldName;
038: private Object m_fieldValue;
039: private FieldType m_type;
040:
041: private int m_operator;
042:
043: public SimpleQueryFilter() {
044: m_previousAppender = -1;
045: }
046:
047: /** Creates a new instance of QueryFilter */
048: public SimpleQueryFilter(String fieldName) {
049: this (-1, fieldName);
050: }
051:
052: public SimpleQueryFilter(int previousAppender, String fieldName) {
053: m_previousAppender = previousAppender;
054: m_fieldName = fieldName;
055: }
056:
057: public String getSql() {
058: StringBuffer res = new StringBuffer();
059: res.append(" ( ");
060: res.append(m_fieldName);
061: res.append(" ").append(getOperatorSymbol());
062: res.append("? )");
063: return res.toString();
064: }
065:
066: public String getPreviousAppender() {
067: String res = " AND ";
068: switch (m_previousAppender) {
069: case APP_AND:
070: res = " AND ";
071: break;
072: case APP_OR:
073: res = " OR ";
074: break;
075: }
076: return res;
077: }
078:
079: public String getOperatorSymbol() {
080: String res = null;
081: switch (m_operator) {
082: case OP_EQUALS:
083: res = "=";
084: break;
085: case OP_NOTEQUALS:
086: res = "!=";
087: break;
088: case OP_BIGGERTHAN:
089: res = ">";
090: break;
091: case OP_BIGGERTHANOREQUALS:
092: res = ">=";
093: break;
094: case OP_SMALLERTHAN:
095: res = "<";
096: break;
097: case OP_SMALLERTHANOREQUALS:
098: res = "<=";
099: break;
100: }
101: return res;
102: }
103:
104: public int getOperator() {
105: return m_operator;
106: }
107:
108: public void setOperator(int operator) {
109: m_operator = operator;
110: }
111:
112: public void setString(String value) {
113: m_type = STRING.getFieldType();
114: m_fieldValue = value;
115: }
116:
117: public void setDate(java.util.Date date) {
118: m_type = DATE.getFieldType();
119: m_fieldValue = date;
120: }
121:
122: public void setLong(long value) {
123: m_type = LONG.getFieldType();
124: m_fieldValue = new Long(value);
125: }
126:
127: public void setInteger(int value) {
128: m_type = INTEGER.getFieldType();
129: m_fieldValue = new Integer(value);
130: }
131:
132: public void setDecimal(double value) {
133: m_type = NUMERIC.getFieldType();
134: m_fieldValue = new Double(value);
135: }
136:
137: public void setObject(Object value, FieldType type) {
138: m_type = type;
139: m_fieldValue = value;
140: }
141:
142: protected int setParamValue(PreparedStatement pstmt, int paramIndex)
143: throws SQLException {
144: Field field = new Field(m_fieldName, m_fieldName, m_type, null);
145: field.setParamValue(m_fieldValue, pstmt, paramIndex);
146: return ++paramIndex;
147: }
148:
149: public int setParamValues(PreparedStatement pstmt, int initialIndex)
150: throws SQLException {
151: return setParamValue(pstmt, initialIndex);
152: }
153:
154: public void setPreviousAppender(int previousAppender) {
155: m_previousAppender = previousAppender;
156: }
157:
158: public Object getValue() {
159: return m_fieldValue;
160: }
161:
162: public String getFieldName() {
163: return m_fieldName;
164: }
165:
166: public FieldType getFieldType() {
167: return m_type;
168: }
169:
170: void setBoolean(boolean value) {
171: m_type = BOOLEAN.getFieldType();
172: m_fieldValue = new Boolean(value);
173: }
174: }
|