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.MetadataException;
37  import com.explosion.expfmodules.rdbmsconn.dbom.utils.MetadataUtils;
38  import com.explosion.expfmodules.rdbmsconn.dbom.utils.SQLEngine;
39  import com.explosion.expfmodules.rdbmsconn.dbom.utils.UnconvertableDataTypeException;
40  import com.explosion.expfmodules.rdbmsconn.dbom.utils.UnrecognisedTypeException;
41  import com.explosion.expfmodules.rdbmsconn.dbom.utils.UnsupportedTypeException;
42  
43  
44  /***
45   * This class represents an SQL insert statement
46   * @author Stephen
47   * Created on Apr 22, 2004
48   */
49  public class InsertStatement
50  {
51      private DBEntity entity;
52      private static final String BIND_STRING = "?";
53      private List columns;
54      private static Logger log = LogManager.getLogger(InsertStatement.class);
55      private MetadataUtils utils = new MetadataUtils();
56      
57      /***
58       * @param entity The entity into which the data is being inserted
59       * @param columns A list of columns which will be inserted into the database row 
60       * @param entity
61       * @param bindings
62       */
63      public InsertStatement(DBEntity entity, List columns)
64      {
65          this.entity = entity;
66          this.columns = columns;
67      }
68      
69      /***
70       * This method returns the tring that will be used for a prepare statement
71       * neccessary variables bound to it.
72       * @return
73       * @throws Exception
74       */
75      public String getPreparedStatementString(DatabaseMetaData meta) throws SQLException, InvalidInsertException, MetadataException
76      {
77          /* First, we need to build an insert statement as a string */
78          String statement = "insert into " + utils.getFullEntityName(entity,meta);
79          statement += " (";
80          String values = "";
81  
82          for (Iterator i = columns.iterator(); i.hasNext();)
83          {
84              DBEntityColumn column = entity.getColumn((String) i.next());
85  
86              if (includeInInsert(column))
87              {
88                statement += (column.getColumnName() + (i.hasNext() ? ", " : ""));
89                values += ("?" + (i.hasNext() ? ", " : ""));
90              }
91          }
92  
93          statement += ") values (" + values + ")";
94  
95          return statement;    
96      }
97      
98      /***
99       * This method will bind the variables provided to the statement
100      * provided
101      * @param bindings A map containing the columnNames (Strings) and the Appropriate values for those columns to be inserted into the database
102      *                 e.g. columnName=Value.  Value must be an object of the correct type otherwise an error will be thrown when the 
103      *  			   statement gets executed.
104      * @param statement Prepared Statement.  Note: this statement needs to have been prepared from the 
105      * 					string produced from getPreparedStatementString()  method from this instanceof the 
106      *                  class 
107      * @throws UnconvertableDataTypeException
108      * @throws SQLException
109      * @throws ParseException
110      * @throws UnsupportedTypeException
111      * @throws UnrecognisedTypeException
112      */
113     public void bindVariables(Map bindVariables, PreparedStatement statement) throws UnconvertableDataTypeException, UnrecognisedTypeException, UnsupportedTypeException, SQLException, ParseException
114     {
115         SQLEngine engine = new SQLEngine();
116         for (int i=0;i<columns.size(); i++)
117         {
118             String columnName = (String) columns.get(i);
119             DBEntityColumn column = entity.getColumn(columnName);
120             if (includeInInsert(column))
121             {
122                 Object object = bindVariables.get(columnName);
123                 log.debug("Binding " + object + " to column " + columnName);
124                 engine.bind(object,statement, column, i+1);
125             }
126         }   
127     }
128     
129     /***
130      * This method will tell you if the column will be included in an insert statement or not
131      * It also outputs a debug level message if not
132      * @param column
133      */
134     public static boolean includeInInsert(DBEntityColumn column)
135     {
136         boolean include = true;
137         
138         if (column == null)
139             return false;
140         
141         /* Exclusions and errors */
142         if (column.isAutoIncrement())
143         {
144                log.debug("Not adding column " + column.getColumnName()+ " into insert statement because it is an autoIncrement column.");
145                include = false;
146         }
147         
148         return include;
149     }    
150         
151 }