1   /*
2    * Copyright 2003 - 2013 The eFaps Team
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  package org.efaps.db.wrapper;
22  
23  import java.sql.Connection;
24  import java.sql.SQLException;
25  import java.sql.Statement;
26  
27  import org.efaps.db.Context;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * <p>
33   * An easy wrapper for a SQL delete statement. To initialize this class use
34   * {@link org.efaps.db.databases.AbstractDatabase#newInsert(String, String, boolean)}
35   * to get a database specific insert.
36   * </p>
37   *
38   * <p>
39   * <b>Example:</b><br/>
40   *
41   * <pre>
42   *
43   * SQLInsert insert = Context.getDbType().newDelete(&quot;MYTABLE&quot;, &quot;ID&quot;);
44   * </pre>
45   *
46   * </p>
47   *
48   * @see org.efaps.db.databases.AbstractDatabase#newInsert(String, String,
49   *      boolean)
50   * @author The eFaps Team
51   * @version $Id$
52   */
53  public class SQLDelete
54  {
55  
56      /**
57       * Logging instance used in this class.
58       */
59      private static final Logger LOG = LoggerFactory.getLogger(SQLDelete.class);
60  
61      /**
62       * List of deletets that will be executed.
63       */
64      private final DeleteDefintion[] definitions;
65  
66  
67      /**
68       * @param _deleteDefintions defintion of a delete
69       */
70      public SQLDelete(final DeleteDefintion... _deleteDefintions)
71      {
72          this.definitions = _deleteDefintions;
73      }
74  
75      /**
76       * Getter method for the instance variable {@link #definitions}.
77       *
78       * @return value of instance variable {@link #definitions}
79       */
80      protected DeleteDefintion[] getDefinitions()
81      {
82          return this.definitions;
83      }
84  
85      /**
86       * @param _con Connection the delete will be executed in
87       * @throws SQLException on error during deletion
88       */
89      public void execute(final Connection _con)
90          throws SQLException
91      {
92          final Statement stmt = _con.createStatement();
93  
94          for (final DeleteDefintion def : getDefinitions()) {
95              final StringBuilder cmd = new StringBuilder();
96              cmd.append(Context.getDbType().getSQLPart(SQLPart.DELETE)).append(" ")
97                  .append(Context.getDbType().getSQLPart(SQLPart.FROM)).append(" ")
98                  .append(Context.getDbType().getTableQuote())
99                  .append(def.getTablename())
100                 .append(Context.getDbType().getTableQuote()).append(" ")
101                 .append(Context.getDbType().getSQLPart(SQLPart.WHERE)).append(" ")
102                 .append(Context.getDbType().getColumnQuote())
103                 .append(def.getIdColumn())
104                 .append(Context.getDbType().getColumnQuote())
105                 .append(Context.getDbType().getSQLPart(SQLPart.EQUAL))
106                 .append(def.getId());
107             stmt.addBatch(cmd.toString());
108             if (SQLDelete.LOG.isDebugEnabled()) {
109                 SQLDelete.LOG.debug(cmd.toString());
110             }
111         }
112 
113         try  {
114             final int[] rows =  stmt.executeBatch();
115             for (final int row : rows) {
116                 if (Statement.EXECUTE_FAILED == row) {
117                     throw new SQLException("Deletion of the '" + row + "' object was not executed successfully.");
118                 }
119             }
120         } finally  {
121             stmt.close();
122         }
123     }
124 
125 
126     /**
127      * Defintion of a Delete.
128      */
129     public static class DeleteDefintion
130     {
131 
132         /**
133          * Name of the tabel to be deleet from.
134          */
135         private final String tablename;
136 
137         /**
138          * Name of the ID column.
139          */
140         private final String idColumn;
141 
142         /**
143          * Id to be deleted.
144          */
145         private final Long id;
146 
147         /**
148          * @param _tableName array with name of the tables
149          * @param _idColumn array with name of the id column
150          * @param _id array with ids to be deleted
151          */
152         public DeleteDefintion(final String _tableName,
153                                final String _idColumn,
154                                final Long _id)
155         {
156             this.tablename = _tableName;
157             this.idColumn = _idColumn;
158             this.id = _id;
159         }
160 
161         /**
162          * Getter method for the instance variable {@link #tablename}.
163          *
164          * @return value of instance variable {@link #tablename}
165          */
166         public String getTablename()
167         {
168             return this.tablename;
169         }
170 
171         /**
172          * Getter method for the instance variable {@link #idColumn}.
173          *
174          * @return value of instance variable {@link #idColumn}
175          */
176         public String getIdColumn()
177         {
178             return this.idColumn;
179         }
180 
181         /**
182          * Getter method for the instance variable {@link #id}.
183          *
184          * @return value of instance variable {@link #id}
185          */
186         public Long getId()
187         {
188             return this.id;
189         }
190     }
191 
192 }