KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > commons > dsi > dml > InsertStatement


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.commons.dsi.dml;
20
21
22
23 import java.util.*;
24
25 import org.openharmonise.commons.dsi.*;
26
27 /**
28  * A DML insert statement .
29  *
30  * @author Michael Bell
31  * @version $Revision: 1.2 $
32  *
33  */

34 public class InsertStatement extends AbstractDMLStatement {
35     
36     /**
37      * The name of the table to receive the inserted data.
38      */

39     protected String JavaDoc m_sTable = null;
40     
41     /**
42      * Map of column references to values to be inserted.
43      */

44     protected HashMap m_ValuePairs = null;
45     
46     /**
47      * Flag which is true if insert columns and values are defined by .
48      * a select statement
49      */

50     private boolean m_bIsValuesSelectDefined = false;
51     
52     /**
53      * Select statement which defines values for insert statement.
54      */

55     private SelectStatement m_select = null;
56
57     /**
58      * Constructs insert statement
59      *
60      */

61     public InsertStatement() {
62     }
63
64     /**
65      * Sets name of table to insert data into.
66      *
67      * @param sTable name of table
68      */

69     public void setTable(String JavaDoc sTable) {
70         m_sTable = sTable;
71     }
72
73     /**
74      * Returns the name of the table to insert data in.
75      *
76      * @return the name of the table to insert data in
77      */

78     public String JavaDoc getTable() {
79         if (m_sTable != null) {
80             return m_sTable;
81         } else if (m_ValuePairs != null) {
82             String JavaDoc sRetr = null;
83             Set set = m_ValuePairs.keySet();
84             Iterator iter = set.iterator();
85
86             if (iter.hasNext()) {
87                 sRetr = ((ColumnRef) iter.next()).getTable();
88             }
89
90             return sRetr;
91         } else {
92             return null;
93         }
94     }
95
96     /**
97      * Sets the select statement which determines column value pairs.
98      *
99      * @param select the select statement
100      */

101     public void setColumnValuesBySelect(SelectStatement select) {
102         m_select = select;
103         m_bIsValuesSelectDefined = true;
104     }
105
106    /**
107     * Returns the select statement which determines the column value pairs.
108     *
109     * @return the select statement which determines the column value pairs
110     */

111     public SelectStatement getColumnValuesSelect() {
112         return m_select;
113     }
114
115     /**
116      * Returns <code>true</code> if column value pairs set by select.
117      *
118      * @return
119      */

120     public boolean isColumnValuesBySelect() {
121         return m_bIsValuesSelectDefined;
122     }
123
124     /**
125      * Returns a <code>Map</code> of column reference and value pairs
126      * for inserting.
127      *
128      * @return column reference and value pairs in a <code>Map</code>
129      */

130     public Map getColumnValuePairs() {
131         return m_ValuePairs;
132     }
133
134     /**
135      * Adds a column reference and associated value to this statement.
136      *
137      * @param colref reference to column
138      * @param i value to be inserted
139      * @throws DataStoreException if column value pair are invalid
140      */

141     public void addColumnValue(ColumnRef colref, int i)
142                         throws DataStoreException {
143         addColumnValue(colref, new Integer JavaDoc(i));
144     }
145
146     /**
147      * Adds a column reference and associated value to this statement.
148      *
149      * @param colref reference to column
150      * @param val value to be inserted
151      * @throws DataStoreException if column value pair are invalid
152      */

153     public void addColumnValue(ColumnRef colref, Object JavaDoc val)
154                         throws DataStoreException {
155         if (m_bIsValuesSelectDefined) {
156             throw new DataStoreException("Invalid data type");
157         }
158
159         if (val != null) {
160             if ((val instanceof String JavaDoc) == false &&
161             (val instanceof Integer JavaDoc) == false &&
162             (val instanceof Float JavaDoc) == false &&
163             (val instanceof Date) == false &&
164             (val instanceof SelectStatement) == false) {
165                 throw new DataStoreException("Invalid class: " +
166                                              val.getClass().getName());
167             }
168
169             if (val instanceof SelectStatement) {
170                 SelectStatement select = (SelectStatement) val;
171
172                 List selectcols = select.getSelectColumns();
173
174                 if ((selectcols.size() != 1) ||
175                         (colref.getDataType() != ((ColumnRef) selectcols.get(0)).getDataType())) {
176                     throw new DataStoreException("Invalid type: " +
177                                                  colref.getDataType());
178                 }
179             } else if ((colref.getDataType() == ColumnRef.NUMBER) &&
180                            !(val instanceof Integer JavaDoc || val instanceof Float JavaDoc)) {
181                 throw new DataStoreException("Invalid type: " +
182                                              val.getClass().getName());
183             } else if ((colref.getDataType() == ColumnRef.TEXT ||
184                                colref.getDataType() == ColumnRef.LONG_TEXT) &&
185                            (val instanceof String JavaDoc) == false) {
186                 throw new DataStoreException("Invalid type: " +
187                                              val.getClass().getName());
188             } else if ((colref.getDataType() == ColumnRef.DATE) &&
189                            (val instanceof Date) == false) {
190                 throw new DataStoreException("Invalid type: " +
191                                              val.getClass().getName());
192             }
193         }
194
195         if (m_ValuePairs == null) {
196             m_ValuePairs = new HashMap(11);
197         }
198
199         m_ValuePairs.put(colref, val);
200     }
201
202     /* (non-Javadoc)
203      * @see org.openharmonise.commons.dsi.dml.AbstractDMLStatement#clear()
204      */

205     public void clear() {
206         super.clear();
207         m_sTable = null;
208
209         if (m_ValuePairs != null) {
210             m_ValuePairs.clear();
211         }
212     }
213 }
Popular Tags