Source code: javatools/db/DbOrExpr.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
24 /**
25 * An expression of the form A OR B. The reason we have this class as well as
26 * DbCriterion, is that this class will optimise away unnecessary segments.
27 * i.e. A OR FALSE will be optimised to just A. The reason you may find a FALSE
28 * expression in your code is the use of DbDatabase.falseExpr(). This is a very
29 * convenient thing for dynamically generated queries.
30 * Of course we could just leave the dummy false expressions in the
31 * final SQL and presumably the dbms can optimise it away fine, but it looks a
32 * bit ugly and nasty to have these dummy expressions in the result.
33 * @see DbFalseExpr.
34 * @author Bitmec
35 * @created 26 September 2001
36 * @version 0.7
37 * @commentedby Antonio Petrelli
38 */
39 public class DbOrExpr extends DbCriterion {
40
41 /** Creates a new OR expression.
42 * @param db The database that will be used.
43 * @param o1 The first term.
44 * @param o2 The second term.
45 */
46 public DbOrExpr(DbDatabase db, Object o1, Object o2) {
47 super(db, o1, "OR", o2);
48 }
49
50 /** Puts data into a statement.
51 * @param ps The statement.
52 * @param i An index (obscure).
53 * @throws SQLException If something goes wrong.
54 * @throws DbException If something goes wrong.
55 * @return An index (obscure).
56 */
57 public int setSqlValues(PreparedStatement ps, int i) throws java.sql.SQLException, javatools.db.DbException {
58 if (c1 instanceof DbTrueExpr || c2 instanceof DbTrueExpr) {
59 return i;
60 } else if (c1 instanceof DbFalseExpr) {
61 return setSqlValue(ps, i, c2, null);
62 } else if (c2 instanceof DbFalseExpr) {
63 return setSqlValue(ps, i, c1, null);
64 } else {
65 return super.setSqlValues(ps, i);
66 }
67 }
68
69 /** Returns the query string related to this expression.
70 * @throws DbException If something goes wrong.
71 * @return The requested query string.
72 */
73 public String getQueryString() throws DbException {
74 // An optimisation hack
75 if (c1 instanceof DbTrueExpr || c2 instanceof DbTrueExpr) {
76 return "";
77 } else if (c1 instanceof DbFalseExpr) {
78 return getString(c2);
79 } else if (c2 instanceof DbFalseExpr) {
80 return getString(c1);
81 } else {
82 return super.getQueryString();
83 }
84 }
85 }