View Javadoc

1   package com.explosion.expfmodules.rdbmsconn.dbom.sql;
2   
3   
4   /* =============================================================================
5    *       
6    *     Copyright 2004 Stephen Cowx
7    *
8    *     Licensed under the Apache License, Version 2.0 (the "License");
9    *     you may not use this file except in compliance with the License.
10   *     You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   *     Unless required by applicable law or agreed to in writing, software
15   *     distributed under the License is distributed on an "AS IS" BASIS,
16   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   *     See the License for the specific language governing permissions and
18   *     limitations under the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.sql.DatabaseMetaData;
24  import java.sql.PreparedStatement;
25  import java.sql.SQLException;
26  import java.text.ParseException;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.log4j.LogManager;
32  import org.apache.log4j.Logger;
33  
34  import com.explosion.expfmodules.rdbmsconn.dbom.DBEntity;
35  import com.explosion.expfmodules.rdbmsconn.dbom.DBEntityColumn;
36  import com.explosion.expfmodules.rdbmsconn.dbom.utils.MetadataUtils;
37  import com.explosion.expfmodules.rdbmsconn.dbom.utils.SQLEngine;
38  import com.explosion.expfmodules.rdbmsconn.dbom.utils.UnconvertableDataTypeException;
39  import com.explosion.expfmodules.rdbmsconn.dbom.utils.UnrecognisedTypeException;
40  import com.explosion.expfmodules.rdbmsconn.dbom.utils.UnsupportedTypeException;
41  import com.explosion.utilities.exception.ExceptionManagerFactory;
42  
43  
44  /***
45   * This class represents an SQL delete statement It will create a prepared statment 
46   * for the delete when preparStatement() is called and bind the provided variables to it.
47   * It will only create the statment once.  Thereafter it will use a  cached version every time
48   * prepareStatement is called
49   * @author Stephen
50   * Created on Apr 22, 2004
51   */
52  public class DeleteStatement
53  {
54      private DBEntity entity;
55      private StringBuffer statementString;
56     
57      private static final String BIND_STRING = "?";
58      private List columns;
59      private static Logger log = LogManager.getLogger(DeleteStatement.class);
60      private MetadataUtils utils = new MetadataUtils();
61      
62      public DeleteStatement(DBEntity entity)
63      {
64          this.entity = entity;
65      }
66      
67      /***
68       * Creates and returns the SQL required for a prepared statement 
69       * by creating a delete statement string which reads 
70       * "delete from full.tablename where full.columnname = ? and full.col....."
71       * 
72       * It first to looks to see if there are any primary key columns.  If there are,
73       * it will only use those in the where clause.  If there are none, it will use
74       * all of the columns in the table.  
75       * by createing the SQL command string
76       * @throws SQLException
77       */
78      public String getPreparedStatementString(DatabaseMetaData meta) throws SQLException
79      {
80          try
81          {
82              statementString = new StringBuffer();
83              statementString.append("delete from " + utils.getFullEntityName(entity, meta));
84              statementString.append(" where ");
85              columns = entity.getDerivedIdentityColumns();
86              
87              for (Iterator i = columns.iterator(); i.hasNext();)
88              {
89              	DBEntityColumn column = (DBEntityColumn) i.next();
90              	statementString.append(utils.getFullColumnName(column.getColumnName(),entity.getEntityName(), meta) + " = ?" );
91              	if (i.hasNext() )
92              	    statementString.append(" and ");  
93              }
94              log.info("Delete statement: " + statementString);
95              
96              return statementString.toString();
97              
98          } catch (Exception e)
99          {
100             ExceptionManagerFactory.getExceptionManager().manageException(e,"Exception caught while creating insert statement.");
101         }   
102         return null;
103     }
104     
105     /***
106      * This method binds the primary key column values provided to the PreparedStatement provided
107      * @throws UnconvertableDataTypeException
108      * @throws SQLException
109      * @throws ParseException
110      * @throws UnsupportedTypeException
111      * @throws UnrecognisedTypeException
112      */
113     public void bindVariables(PreparedStatement statement, Map bindVariables) throws UnconvertableDataTypeException, UnrecognisedTypeException, UnsupportedTypeException, SQLException, ParseException
114     {
115         log.debug("Binding variables to statement");
116         SQLEngine engine = new SQLEngine();
117         int index = 1;
118         
119         for (Iterator i = columns.iterator(); i.hasNext();)
120         {
121         	DBEntityColumn column = (DBEntityColumn) i.next();
122         	Object data = bindVariables.get(column.getColumnName());
123         	log.debug("Binding '"+data+"' to '"+column.getColumnName()+"'");
124 			engine.bind(data, statement, column, index);
125         }
126     }
127     
128 }