Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javatools/db/DbAndExpr.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 AND 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 AND TRUE will be optimised to just A. The reason you may find a
28   * TRUE expression in your code is the use of DbDatabase.trueExpr().
29   * Of course we could just leave the dummy true expressions in the final SQL
30   * and presumably the dbms can optimise it away fine, but it looks a bit ugly
31   * and nasty to have these dummy expressions in the result.
32   * @see DbTrueExpr
33   */
34  
35  public class DbAndExpr extends DbCriterion {
36  
37      /** Builds a new "and" expression in the form "o1 AND o2".
38       * @param db The database used to build this expression.
39       * @param o1 The first term.
40       * @param o2 The second term.
41       */    
42    public DbAndExpr(DbDatabase db, Object o1, Object o2) {
43      super(db, o1, "AND", o2);
44    }
45          /** Returns the query string related to this expression.
46           * @throws DbException If something goes wrong.
47           * @return The needed string.
48           */        
49    public String getQueryString() throws DbException {
50      // An optimisation hack
51      if (c1 instanceof DbFalseExpr || c2 instanceof DbFalseExpr) {
52        return "";
53      } else if (c1 instanceof DbTrueExpr) {
54        return getString(c2);
55      } else if (c2 instanceof DbTrueExpr) {
56        return getString(c1);
57      } else {
58        return super.getQueryString();
59      }
60    }
61          /** Obscure... Talk to Chris.
62           * @param ps The prepared statement.
63           * @param i The index.
64           * @throws SQLException If something goes wrong.
65           * @throws DbException If something goes wrong.
66           * @return The return value
67           */        
68    public int setSqlValues(PreparedStatement ps, int i) throws java.sql.SQLException, javatools.db.DbException {
69      if (c1 instanceof DbFalseExpr || c2 instanceof DbFalseExpr) {
70        return i;
71      } else if (c1 instanceof DbTrueExpr) {
72        return setSqlValue(ps, i, c2, null);
73      } else if (c2 instanceof DbTrueExpr) {
74        return setSqlValue(ps, i, c1, null);
75      } else {
76        return super.setSqlValues( ps,  i);
77      }
78    }
79  }