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

Quick Search    Search Deep

Source code: org/jdbf/engine/sql/DeleteStatement.java


1   /*
2    * 07/05/2002 - 23:46:45
3    *
4    * DeleteStatement.java - JDBF Object Relational mapping system
5    * Copyright (C) 2002 Giovanni Martone
6    * giovannimartone@hotmail.com
7    * http://jdbf.sourceforge.net
8    *
9    * This program is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public License
11   * as published by the Free Software Foundation; either version 2
12   * of the License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public License
20   * along with this program; if not, write to the Free Software
21   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22   */
23  
24  package org.jdbf.engine.sql;
25  
26  import java.sql.*;
27  import java.util.ArrayList;
28  import org.jdbf.engine.basic.ObjectMapped;
29  import org.jdbf.engine.criteria.*;
30  import org.jdbf.engine.mapping.*;
31  import org.jdbf.engine.reflection.*;
32  import org.jdbf.engine.repository.*;
33  import org.jdbf.engine.sql.connection.*;
34  
35  /**
36   * <code>DeleteStatement</code> is that  class that represents the delete sql
37   * statement.
38   * DeleteStatement handles the creation of sql statement with the informations
39   * specified in a RepositoryView object and provides to execute the delete statement. 
40   */
41   public class DeleteStatement extends SQLStatement{
42  
43     /** DELETE statement */
44    protected final static String DELETE = SqlInterface.DELETE;
45  
46    
47    /**
48     * Create the object and build the delete statement.
49     *
50     * @param repository
51     */
52    public DeleteStatement(RepositoryView repository){
53      statement = buildStatement(repository,null);
54      criteria = null;
55    }
56  
57    /**
58     * Create the object and build the delete statement.
59     *
60     * @param repository, criteria is Criteria object, it may be null
61     */
62    public DeleteStatement(RepositoryView repository,Criteria criteria){
63      /*this.criteria = criteria;
64      if(criteria != null)
65        statement = buildStatementForCriteria(repository);
66      else
67        statement = buildStatement(repository,null);
68      */
69  
70      super(repository,null,criteria);
71    }
72  
73    
74    /**
75     * Creates the empty object
76     */
77    public DeleteStatement(){
78      statement = "";
79      criteria = null;
80    }
81  
82    
83    /**
84     * Builds the delete statement with the informations passed by repository.
85     * These informations are:
86     * <li> tableName;
87     * <li> columnTableName
88     *
89     * @param repository, propertiesNames name of properties it may be null
90     * @return String delete statement
91     */
92    public String buildStatement(RepositoryView repository,String[] propertiesNames){
93      StringBuffer buff = new StringBuffer(DELETE);
94      BeanDescriptor beanDescriptor = repository.getBeanDescriptor();
95      String tableName = beanDescriptor.getTableName();
96      buff.append(tableName).append(" ").append(WHERE);
97      ArrayList itemDescriptors = beanDescriptor.getItemDescriptors();
98      int size = itemDescriptors.size();
99      
100     //build list of column name
101     for(int i = 0; i < size; i++){
102       ItemDescriptor item = (ItemDescriptor) itemDescriptors.get(i);
103       boolean isPrimaryKey = item.isPrimaryKey();
104       if(isPrimaryKey){      
105         String columnTableName = item.getColumnTableName();
106         buff.append(columnTableName).append("= ?");
107         break;
108       }
109       
110       else
111         continue;
112     }
113     statement = buff.toString();
114     return statement;
115   }
116 
117 
118   /**
119    * Builds the delete statement with criteria.
120    *
121    * @param repository
122    * @return String delete statement
123    */
124   public String buildStatementForCriteria(RepositoryView repository,String[] propertiesNames){
125     StringBuffer buff = new StringBuffer(DELETE);    
126     BeanDescriptor beanDescriptor = repository.getBeanDescriptor();
127     String tableName = beanDescriptor.getTableName();
128     buff.append(tableName).append(" ").append(WHERE)
129       .append(criteria.getCriteria());
130     
131     statement = buff.toString();
132     return statement;
133   }
134 
135 
136 
137   /**
138    * Executes the delete statement and delete in database the ObjectMapped object specified in obj.
139    *
140    * @param obj object to delete, view contains the informations about property of object mapped,
141    *        con Connection object.
142      *
143    * @return int number of rows affected
144    * @throws SQLException,MappingException if datatype is invalid or table not exist.
145    */
146    public int delete(ObjectMapped obj,RepositoryView view,Connection con)throws SQLException,MappingException{
147    
148      //Build insert statement
149      PreparedStatement preStat = con.prepareStatement(statement);
150    
151      //Get the info on the repository
152      BeanDescriptor beanDescriptor = view.getBeanDescriptor();
153      ArrayList itemDescriptors = beanDescriptor.getItemDescriptors();
154      ItemDescriptor item = null;
155      for(int i = 0; i < itemDescriptors.size(); i++){
156        item = (ItemDescriptor)itemDescriptors.get(i);
157        boolean isPrimaryKey = item.isPrimaryKey();
158       
159       if(isPrimaryKey){
160         String propertyName = item.getPropertyName();
161         String dataType = item.getDataType();
162         Object propertyValue = view.getPropertyValue(obj,propertyName);
163         int types = org.jdbf.engine.sql.Types.getSQLType(dataType);                
164         preStat.setObject(1,propertyValue,types);
165         break;
166       }
167               else
168       continue;
169          }
170        int rows = preStat.executeUpdate();
171        preStat.close();
172        System.out.println("rows affected : " + rows);
173        return rows;
174    }
175 
176 
177    /**
178     * Executes the delete statement and delete in database with the specific criteria.
179     *
180     * @param con Connection object.
181       *
182     * @return int number of rows affected
183     * @throws SQLException.
184     */
185    public int delete(Connection con)throws SQLException{
186    
187      //Build insert statement
188      PreparedStatement preStat = con.prepareStatement(statement);        
189       int rows = preStat.executeUpdate();
190       preStat.close();
191       System.out.println("rows affected : " + rows);
192       return rows;
193    }
194 }