1 19 package org.openharmonise.commons.dsi.dml; 20 21 22 23 import java.util.*; 24 25 import org.openharmonise.commons.dsi.*; 26 27 34 public class InsertStatement extends AbstractDMLStatement { 35 36 39 protected String m_sTable = null; 40 41 44 protected HashMap m_ValuePairs = null; 45 46 50 private boolean m_bIsValuesSelectDefined = false; 51 52 55 private SelectStatement m_select = null; 56 57 61 public InsertStatement() { 62 } 63 64 69 public void setTable(String sTable) { 70 m_sTable = sTable; 71 } 72 73 78 public String getTable() { 79 if (m_sTable != null) { 80 return m_sTable; 81 } else if (m_ValuePairs != null) { 82 String 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 101 public void setColumnValuesBySelect(SelectStatement select) { 102 m_select = select; 103 m_bIsValuesSelectDefined = true; 104 } 105 106 111 public SelectStatement getColumnValuesSelect() { 112 return m_select; 113 } 114 115 120 public boolean isColumnValuesBySelect() { 121 return m_bIsValuesSelectDefined; 122 } 123 124 130 public Map getColumnValuePairs() { 131 return m_ValuePairs; 132 } 133 134 141 public void addColumnValue(ColumnRef colref, int i) 142 throws DataStoreException { 143 addColumnValue(colref, new Integer (i)); 144 } 145 146 153 public void addColumnValue(ColumnRef colref, Object 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 ) == false && 161 (val instanceof Integer ) == false && 162 (val instanceof Float ) == 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 || val instanceof Float )) { 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 ) == 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 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 |