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

Quick Search    Search Deep

Source code: javatools/db/DbValueList.java


1   /*
2    * DbValueList.java
3    *
4    * Created on 6 agosto 2002, 15.33
5       Javatools (modified version) - Some useful general classes.
6       Copyright (C) 2002-2003  Chris Bitmead (original) Antonio Petrelli (modified)
7   
8       This program is free software; you can redistribute it and/or modify
9       it under the terms of the GNU General Public License as published by
10      the Free Software Foundation; either version 2 of the License, or
11      (at your option) any later version.
12  
13      This program is distributed in the hope that it will be useful,
14      but WITHOUT ANY WARRANTY; without even the implied warranty of
15      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16      GNU General Public License for more details.
17  
18      You should have received a copy of the GNU General Public License
19      along with this program; if not, write to the Free Software
20      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  
22      Contact me at: brenmcguire@users.sourceforge.net
23   */
24  
25  package javatools.db;
26  
27  import java.sql.*;
28  import java.io.*;
29  import java.util.*;
30  
31  /** This class represents a set of values to be used in an expression.
32   * @author Antonio Petrelli
33   * @version 0.1.0
34   */
35  public class DbValueList extends javatools.db.DbExpr {
36  
37      /** Creates new DbValueList
38       * @param db The database to use.
39       * @param pValueList The collection to be used as a value list.
40       */
41      public DbValueList(DbDatabase db, Collection pValueList) {
42          super(db);
43          valueList = pValueList;
44      }
45  
46      /**
47       * Any DbExpr needs to be able to convert into the SQL string equivilent.
48       *
49       * @return The queryString value
50       * @exception DbException If something goes wrong.
51       */
52      public String getQueryString() throws DbException {
53          Iterator valueIt;
54          String tempString;
55          
56          valueIt = valueList.iterator();
57          tempString = null;
58          if (valueIt.hasNext())
59              tempString = "(" + getString(valueIt.next());
60          while (valueIt.hasNext())
61              tempString += ", " + getString(valueIt.next());
62          if (tempString != null)
63              tempString += ")";
64          return tempString;
65      }
66      
67      /**
68       * Any DbExpr needs to be able to substitute any parameters as per JDBC "?"
69       * substitutions.
70       *
71       * @param ps The PreparedStatement
72       * @param i                 The new sqlValues value
73       * @return An index (obscure).
74       * @exception DbException If something goes wrong.
75       * @exception SQLException If something goes wrong.
76       */
77      public int setSqlValues(PreparedStatement ps, int i) throws DbException, SQLException {
78          Iterator valueIt;
79          
80          valueIt = valueList.iterator();
81          while (valueIt.hasNext()) {
82              i = setSqlValue(ps, i, valueIt.next(), null);
83          }
84          return i;
85      }
86      
87      private Collection valueList;
88  }