001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.commons.dsi.dml;
020:
021: import java.util.*;
022:
023: import org.openharmonise.commons.dsi.*;
024:
025: /**
026: * A DML insert statement .
027: *
028: * @author Michael Bell
029: * @version $Revision: 1.2 $
030: *
031: */
032: public class InsertStatement extends AbstractDMLStatement {
033:
034: /**
035: * The name of the table to receive the inserted data.
036: */
037: protected String m_sTable = null;
038:
039: /**
040: * Map of column references to values to be inserted.
041: */
042: protected HashMap m_ValuePairs = null;
043:
044: /**
045: * Flag which is true if insert columns and values are defined by .
046: * a select statement
047: */
048: private boolean m_bIsValuesSelectDefined = false;
049:
050: /**
051: * Select statement which defines values for insert statement.
052: */
053: private SelectStatement m_select = null;
054:
055: /**
056: * Constructs insert statement
057: *
058: */
059: public InsertStatement() {
060: }
061:
062: /**
063: * Sets name of table to insert data into.
064: *
065: * @param sTable name of table
066: */
067: public void setTable(String sTable) {
068: m_sTable = sTable;
069: }
070:
071: /**
072: * Returns the name of the table to insert data in.
073: *
074: * @return the name of the table to insert data in
075: */
076: public String getTable() {
077: if (m_sTable != null) {
078: return m_sTable;
079: } else if (m_ValuePairs != null) {
080: String sRetr = null;
081: Set set = m_ValuePairs.keySet();
082: Iterator iter = set.iterator();
083:
084: if (iter.hasNext()) {
085: sRetr = ((ColumnRef) iter.next()).getTable();
086: }
087:
088: return sRetr;
089: } else {
090: return null;
091: }
092: }
093:
094: /**
095: * Sets the select statement which determines column value pairs.
096: *
097: * @param select the select statement
098: */
099: public void setColumnValuesBySelect(SelectStatement select) {
100: m_select = select;
101: m_bIsValuesSelectDefined = true;
102: }
103:
104: /**
105: * Returns the select statement which determines the column value pairs.
106: *
107: * @return the select statement which determines the column value pairs
108: */
109: public SelectStatement getColumnValuesSelect() {
110: return m_select;
111: }
112:
113: /**
114: * Returns <code>true</code> if column value pairs set by select.
115: *
116: * @return
117: */
118: public boolean isColumnValuesBySelect() {
119: return m_bIsValuesSelectDefined;
120: }
121:
122: /**
123: * Returns a <code>Map</code> of column reference and value pairs
124: * for inserting.
125: *
126: * @return column reference and value pairs in a <code>Map</code>
127: */
128: public Map getColumnValuePairs() {
129: return m_ValuePairs;
130: }
131:
132: /**
133: * Adds a column reference and associated value to this statement.
134: *
135: * @param colref reference to column
136: * @param i value to be inserted
137: * @throws DataStoreException if column value pair are invalid
138: */
139: public void addColumnValue(ColumnRef colref, int i)
140: throws DataStoreException {
141: addColumnValue(colref, new Integer(i));
142: }
143:
144: /**
145: * Adds a column reference and associated value to this statement.
146: *
147: * @param colref reference to column
148: * @param val value to be inserted
149: * @throws DataStoreException if column value pair are invalid
150: */
151: public void addColumnValue(ColumnRef colref, Object val)
152: throws DataStoreException {
153: if (m_bIsValuesSelectDefined) {
154: throw new DataStoreException("Invalid data type");
155: }
156:
157: if (val != null) {
158: if ((val instanceof String) == false
159: && (val instanceof Integer) == false
160: && (val instanceof Float) == false
161: && (val instanceof Date) == false
162: && (val instanceof SelectStatement) == false) {
163: throw new DataStoreException("Invalid class: "
164: + val.getClass().getName());
165: }
166:
167: if (val instanceof SelectStatement) {
168: SelectStatement select = (SelectStatement) val;
169:
170: List selectcols = select.getSelectColumns();
171:
172: if ((selectcols.size() != 1)
173: || (colref.getDataType() != ((ColumnRef) selectcols
174: .get(0)).getDataType())) {
175: throw new DataStoreException("Invalid type: "
176: + colref.getDataType());
177: }
178: } else if ((colref.getDataType() == ColumnRef.NUMBER)
179: && !(val instanceof Integer || val instanceof Float)) {
180: throw new DataStoreException("Invalid type: "
181: + val.getClass().getName());
182: } else if ((colref.getDataType() == ColumnRef.TEXT || colref
183: .getDataType() == ColumnRef.LONG_TEXT)
184: && (val instanceof String) == false) {
185: throw new DataStoreException("Invalid type: "
186: + val.getClass().getName());
187: } else if ((colref.getDataType() == ColumnRef.DATE)
188: && (val instanceof Date) == false) {
189: throw new DataStoreException("Invalid type: "
190: + val.getClass().getName());
191: }
192: }
193:
194: if (m_ValuePairs == null) {
195: m_ValuePairs = new HashMap(11);
196: }
197:
198: m_ValuePairs.put(colref, val);
199: }
200:
201: /* (non-Javadoc)
202: * @see org.openharmonise.commons.dsi.dml.AbstractDMLStatement#clear()
203: */
204: public void clear() {
205: super.clear();
206: m_sTable = null;
207:
208: if (m_ValuePairs != null) {
209: m_ValuePairs.clear();
210: }
211: }
212: }
|