Source code: javatools/db/DbCriterion.java
1 /*
2 Javatools (modified version) - Some useful general classes.
3 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 Contact me at: brenmcguire@users.sourceforge.net
20 */
21 package javatools.db;
22 import java.sql.*;
23 import java.util.*;
24
25
26 /** It is a class that represents an expression of the type "A op B", where "op" is
27 * a generic operator.
28 * @author Chris Bitmead
29 * @version 0.7
30 * @commentedby Antonio Petrelli
31 */
32 public class DbCriterion extends DbExpr {
33 /** The first term.
34 */
35 Object c1;
36 /** The operator.
37 */
38 String op;
39 /** The second term.
40 */
41 Object c2;
42
43 /** Builds a new DbCriterion object.
44 * @param db The database for this operation.
45 * @param c1 The first term.
46 * @param op The operator.
47 * @param c2 The second term.
48 */
49 public DbCriterion(DbDatabase db, Object c1, String op, Object c2) {
50 super(db);
51 this.c1 = c1;
52 this.op = op;
53 this.c2 = c2;
54 }
55
56 /** Returns the query string related to this expression.
57 * @throws DbException If something goes wrong.
58 * @return The requested string.
59 */
60 public String getQueryString() throws DbException {
61 return "(" + getString(c1) + " " + op + " " + getString(c2) + ")";
62 }
63
64 /** Sets the real values in the passed statement. Anyway it is an obscure method,
65 * call Chris Bitmead.
66 * @param ps The statement to put data into.
67 * @param i An index.
68 * @throws DbException If something goes wrong.
69 * @throws SQLException If something goes wrong.
70 * @return An index.
71 */
72 public int setSqlValues(PreparedStatement ps, int i) throws DbException, SQLException {
73 i = setSqlValue(ps, i, c1, null);
74 i = setSqlValue(ps, i, c2, null);
75 return i;
76 }
77 /** Adds to the passed set, the tables used by this expression.
78 * @param c The set to add tables to.
79 */
80 public void usesTables(Set c) {
81 usesTables(c, c1);
82 usesTables(c, c2);
83 }
84
85 /*public DbExpr getSubExpr(DbColumn[] cols) throws DbException {
86 DbExpr tempc1, tempc2;
87
88 if (c1 instanceof DbExpr)
89 tempc1 = ((DbExpr) c1).getSubExpr(cols);
90 else
91 tempc1 = null;
92 if (c2 instanceof DbExpr)
93 tempc2 = ((DbExpr) c2).getSubExpr(cols);
94 else
95 tempc2 = null;
96 if (tempc1 != null)
97 if (tempc2 != null)
98 return new DbCriterion(db, tempc1, op, tempc2);
99 else
100 return new DbCriterion(db, tempc1, op, c2);
101 else
102 if (tempc2 != null)
103 return new DbCriterion(db, c1, op, tempc1);
104 else
105 return null;
106 } */
107
108 /*public DbExpr substituteColumn(DbColumn oldCol, DbColumn newCol) throws DbException {
109 DbExpr tempc1, tempc2;
110
111 if (c1 instanceof DbExpr)
112 tempc1 = ((DbExpr) c1).substituteColumn(oldCol, newCol);
113 else
114 tempc1 = null;
115 if (c2 instanceof DbExpr)
116 tempc2 = ((DbExpr) c2).substituteColumn(oldCol, newCol);
117 else
118 tempc2 = null;
119 if (tempc1 != null)
120 if (tempc2 != null)
121 return new DbCriterion(db, tempc1, op, tempc2);
122 else
123 return new DbCriterion(db, tempc1, op, c2);
124 else
125 if (tempc2 != null)
126 return new DbCriterion(db, c1, op, tempc1);
127 else
128 return null;
129 } */
130
131 }