001package com.hfg.sql; 002 003import com.hfg.sql.jdbc.JDBCException; 004import com.hfg.sql.table.DatabaseCol; 005import com.hfg.sql.table.DatabaseRow; 006import com.hfg.sql.table.DatabaseTable; 007import com.hfg.sql.table.field.DatabaseField; 008import com.hfg.util.StringBuilderPlus; 009import com.hfg.util.StringUtil; 010 011import java.sql.Connection; 012import java.sql.SQLException; 013 014//------------------------------------------------------------------------------ 015/** 016 Container for building a SQL delete statement. 017 <div> 018 @author J. Alex Taylor, hairyfatguy.com 019 </div> 020 */ 021//------------------------------------------------------------------------------ 022// com.hfg XML/HTML Coding Library 023// 024// This library is free software; you can redistribute it and/or 025// modify it under the terms of the GNU Lesser General Public 026// License as published by the Free Software Foundation; either 027// version 2.1 of the License, or (at your option) any later version. 028// 029// This library is distributed in the hope that it will be useful, 030// but WITHOUT ANY WARRANTY; without even the implied warranty of 031// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 032// Lesser General Public License for more details. 033// 034// You should have received a copy of the GNU Lesser General Public 035// License along with this library; if not, write to the Free Software 036// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 037// 038// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 039// jataylor@hairyfatguy.com 040//------------------------------------------------------------------------------ 041 042public class SQLDelete extends SQLCmd 043{ 044 private DatabaseTable mTable; 045 046 //########################################################################### 047 // CONSTRUCTORS 048 //########################################################################### 049 050 //--------------------------------------------------------------------------- 051 public SQLDelete() 052 { 053 } 054 055 //--------------------------------------------------------------------------- 056 public SQLDelete(DatabaseRow inRow) 057 { 058 setTable(inRow.getDatabaseTable()); 059 060 DatabaseCol idCol = inRow.getIdCol(); 061 if (null == idCol) 062 { 063 throw new JDBCException(inRow.getDatabaseTable() + " doesn't have an id column to use for deletion!"); 064 } 065 066 addClause(new WhereClause(idCol + " = " + inRow.getField(idCol).getSQLValue())); 067 } 068 069 //########################################################################### 070 // PUBLIC METHODS 071 //########################################################################### 072 073 //--------------------------------------------------------------------------- 074 public SQLDelete setTable(DatabaseTable inValue) 075 { 076 mTable = inValue; 077 078 return this; 079 } 080 081 //--------------------------------------------------------------------------- 082 public int execute(Connection inConn) 083 throws SQLException 084 { 085 return SQLUtil.executeUpdate(inConn, toSQL()); 086 } 087 088 //--------------------------------------------------------------------------- 089 public String toSQL() 090 { 091 if (null == mTable) 092 { 093 throw new JDBCException("No table has been specified for deletion!"); 094 } 095 096 String whereClause = generateWhereClause(); 097 if (! StringUtil.isSet(whereClause)) 098 { 099 throw new JDBCException("No where clause has been specified!"); 100 } 101 102 StringBuilderPlus sql = new StringBuilderPlus("DELETE FROM "); 103 sql.appendln(mTable.getQualifiedName() 104 + (mTable.getAlias() != null ? " " + mTable.getAlias() : "")); 105 106 sql.appendln(whereClause); 107 108 return sql.toString(); 109 } 110 111 //--------------------------------------------------------------------------- 112 @Override 113 public String toString() 114 { 115 return toSQL(); 116 } 117 118 //--------------------------------------------------------------------------- 119 public SQLDelete addClause(SQLClause inValue) 120 { 121 super.addClause(inValue); 122 123 return this; 124 } 125 126 //--------------------------------------------------------------------------- 127 public SQLDelete addWhereClause(String inValue) 128 { 129 super.addClause(new WhereClause(inValue)); 130 131 return this; 132 } 133 134 //--------------------------------------------------------------------------- 135 public SQLDelete addWhereClause(DatabaseField inValue) 136 { 137 super.addClause(new WhereClause(inValue)); 138 139 return this; 140 } 141 142 //--------------------------------------------------------------------------- 143 public SQLDelete addWhereClauseGroup(WhereClauseGroup inValue) 144 { 145 return (SQLDelete) super.addWhereClauseGroup(inValue); 146 } 147 148}