1 package com.explosion.expfmodules.rdbmsconn.dbom.sql;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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 }