Source code: javatools/db/DbDeleter.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.util.*;
23 import java.sql.*;
24 import javatools.util.FileLog;
25
26 /**
27 * A class used to delete records from SQL tables. The constructor is not
28 * public. To obtain a DbDeleter call DbTable.deleter(); Example: To delete all
29 * the people who are (younger than 18 or older than 80) and whose name is
30 * "Fred"... <PRE>
31 * DbDatabase db = ...;
32 * DbTable people = db.getTable("PEOPLE");
33 * DbDeleter deleter = people.deleter();
34 * deleter.setWhere(people.getColumn("AGE").lessThan(new Integer(18)).or(
35 * people.getColumn("AGE").greaterThan(new Integer(80))).and(
36 * people.getColumn("NAME").equal("FRED"));
37 * int numberOfPeopleDeleted = deleter.execute();
38 * </PRE> This is equivilent to... <PRE>
39 * DELETE FROM PEOPLE WHERE (AGE < 18 OR AGE > 80 ) AND NAME='Fred'
40 * </PRE> Note the use of equal(), NOT equals().
41 *
42 * @author Chris Bitmead (original), Antonio Petrelli (modified)
43 * @created December 13, 2001
44 * @version 0.2.0
45 * @commentedby Antonio Petrelli
46 */
47
48 public class DbDeleter {
49 /** The table for this operation.
50 */
51 DbAbstractTable table;
52 /** The where clause to select rows for deletion.
53 */
54 DbExpr where;
55
56 /** Builds a new DbDeleter object.
57 * @param table The table from which rows should be deleted.
58 */
59 DbDeleter(DbAbstractTable table) {
60 this.table = table;
61 }
62
63 /**
64 * Set the where condition for this delete operation.
65 *
66 * @param where The new where value
67 */
68 public void setWhere(DbExpr where) {
69 this.where = where;
70 }
71
72 /**
73 * Execute this delete command on a specific connection.
74 *
75 * @param dbcon The connection to the database.
76 * @return The number of record affected.
77 * @exception DbException If something goes wrong.
78 */
79 public int execute(DbConnection dbcon) throws DbException {
80 try {
81 PreparedStatement stmt = dbcon.con.prepareStatement(getQueryString());
82 if (where != null) {
83 int i = where.setSqlValues(stmt, 1);
84 }
85 return stmt.executeUpdate();
86 } catch (SQLException e) {
87 throw new DbException(e);
88 }
89 }
90
91 /**
92 * Execute this command on the default connection.
93 *
94 * @return The number of record affected.
95 * @exception DbException Description of Exception
96 */
97 public int execute() throws DbException {
98 return execute(table.db.getThreadConnection());
99 }
100
101 /** Returns the query string related to this deleter.
102 * @throws DbException If something goes wrong.
103 * @return The requested query string.
104 */
105 String getQueryString() throws DbException {
106 String rtn = "DELETE FROM " + table.getFullTableName();
107 if (where != null) {
108 rtn += " WHERE ";
109 rtn += where.getQueryString();
110 }
111 FileLog.singleton().debug("DbSelector", rtn);
112 return rtn;
113 }
114
115 }