001: /*
002: * Created on 12/12/2006
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2006 Igor Regis da Silva Simões
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: */
023: package br.com.igor.db;
024:
025: import static br.com.igor.db.CondicaoSQL.Condicao.BETWEEN;
026: import static br.com.igor.db.CondicaoSQL.Condicao.IGUAL;
027: import static br.com.igor.db.CondicaoSQL.Condicao.IS_NULL;
028: import static br.com.igor.db.CondicaoSQL.Condicao.MAIOR;
029: import static br.com.igor.db.CondicaoSQL.Condicao.MAIOR_IGUAL;
030: import static br.com.igor.db.CondicaoSQL.Condicao.MENOR;
031: import static br.com.igor.db.CondicaoSQL.Condicao.MENOR_IGUAL;
032:
033: /**
034: * Monta uma condição de clausula where sql com os parametros
035: * passados conforme descrito a seguir:
036: *
037: * Condição <br>
038: * MAIOR: coluna < valor <br>
039: * MAIOR_IGUAL: coluna <= valor <br>
040: * MENOR: coluna > valor <br>
041: * MENOR_IGUAL: coluna >= valor <br>
042: * IGUAL: coluna = valor <br>
043: * IS_NULL: coluna IS NULL <br>
044: * BETWEEN: coluna between valor and valor_2 <br>
045: *
046: * @author Igor Regis da Silva Simoes
047: * @since 12/12/2006
048: */
049: public class CondicaoSQL<E extends Object> {
050: public enum Condicao {
051: IGUAL, MAIOR, MENOR, BETWEEN, MAIOR_IGUAL, MENOR_IGUAL, IS_NULL
052: }
053:
054: private String coluna = null;
055:
056: private E valor = null;
057:
058: private E valor_2 = null;
059:
060: private Condicao condicao;
061:
062: /**
063: * Condição <br>
064: * MAIOR: coluna > valor <br>
065: * MAIOR_IGUAL: coluna >= valor <br>
066: * MENOR: coluna < valor <br>
067: * MENOR_IGUAL: coluna <= valor <br>
068: * IGUAL: coluna = valor <br>
069: * IS_NULL: coluna IS NULL <br>
070: * BETWEEN: coluna between valor and valor_2 <br>
071: *
072: * @param coluna
073: * @param valor
074: * @param condicao
075: */
076: public CondicaoSQL(String coluna, E valor, Condicao condicao) {
077: setColuna(coluna);
078: setValor(valor);
079: setCondicao(condicao);
080: }
081:
082: /**
083: * Condição <br>
084: * MAIOR: coluna > valor <br>
085: * MAIOR_IGUAL: coluna >= valor <br>
086: * MENOR: coluna < valor <br>
087: * MENOR_IGUAL: coluna <= valor <br>
088: * IGUAL: coluna = valor <br>
089: * IS_NULL: coluna IS NULL <br>
090: * BETWEEN: coluna between valor and valor_2 <br>
091: *
092: * @param coluna
093: * @param condicao
094: */
095: @SuppressWarnings("unchecked")
096: public CondicaoSQL(String coluna, Condicao condicao) {
097: setColuna(coluna);
098: setCondicao(condicao);
099: }
100:
101: /**
102: * Condição <br>
103: * MAIOR: coluna > valor <br>
104: * MAIOR_IGUAL: coluna >= valor <br>
105: * MENOR: coluna < valor <br>
106: * MENOR_IGUAL: coluna <= valor <br>
107: * IGUAL: coluna = valor <br>
108: * IS_NULL: coluna IS NULL <br>
109: * BETWEEN: coluna between valor and valor_2 <br>
110: *
111: * @param coluna
112: * @param valor
113: * @param valor_2
114: * @param condicao
115: */
116: public CondicaoSQL(String coluna, E valor, E valor_2,
117: Condicao condicao) {
118: setColuna(coluna);
119: setValor(valor);
120: setValor_2(valor_2);
121: setCondicao(condicao);
122: }
123:
124: public String getColuna() {
125: return "\"" + coluna + "\"";
126: }
127:
128: public void setColuna(String coluna) {
129: this .coluna = coluna;
130: }
131:
132: public E getValor() {
133: return valor;
134: }
135:
136: public void setValor(E valor) {
137: this .valor = valor;
138: }
139:
140: public E getValor_2() {
141: return valor_2;
142: }
143:
144: public void setValor_2(E valor_2) {
145: this .valor_2 = valor_2;
146: }
147:
148: public Condicao getCondicao() {
149: return condicao;
150: }
151:
152: public void setCondicao(Condicao condicao) {
153: this .condicao = condicao;
154: }
155:
156: @Override
157: public String toString() {
158: switch (condicao) {
159: case MAIOR:
160: return " " + coluna + " > " + valor + " ";
161: case MAIOR_IGUAL:
162: return " " + coluna + " >= " + valor + " ";
163: case MENOR:
164: return " " + coluna + " < " + valor + " ";
165: case MENOR_IGUAL:
166: return " " + coluna + " <= " + valor + " ";
167: case IGUAL:
168: return " " + coluna + " = " + valor + " ";
169: case IS_NULL:
170: return " " + coluna + " IS NULL ";
171: case BETWEEN:
172: if (valor_2 == null)
173: throw new RuntimeException(
174: "Invalid value for value_2 property");
175: return " " + coluna + " between " + valor + " and "
176: + valor_2 + " ";
177: default:
178: return "<empty condition>";
179: }
180: }
181:
182: public String getParsedString() {
183: switch (condicao) {
184: case MAIOR:
185: return " " + getColuna() + " > ? ";
186: case MAIOR_IGUAL:
187: return " " + getColuna() + " >= ? ";
188: case MENOR:
189: return " " + getColuna() + " < ? ";
190: case MENOR_IGUAL:
191: return " " + getColuna() + " <= ? ";
192: case IGUAL:
193: return " " + getColuna() + " = ? ";
194: case IS_NULL:
195: return " " + getColuna() + " IS NULL ";
196: case BETWEEN:
197: if (valor_2 == null)
198: throw new RuntimeException(
199: "Invalid value for value_2 property");
200: return " " + getColuna() + " between ? and ? ";
201: default:
202: return "<empty condition>";
203: }
204: }
205: }
|