1 32 33 package com.knowgate.hipergate.datamodel; 34 35 import java.util.ListIterator ; 36 import java.util.HashMap ; 37 38 import java.sql.Connection ; 39 import java.sql.SQLException ; 40 import java.sql.Statement ; 41 import java.sql.PreparedStatement ; 42 43 import com.knowgate.dataobjs.DBColumn; 44 import com.knowgate.dataobjs.DBTable; 45 import com.knowgate.misc.Gadgets; 46 47 52 public class TableLoader extends DBTable implements ImportLoader { 53 54 private String [] aColNames; 55 private Object [] aValues; 56 HashMap oInsrColPos; 57 HashMap oUpdtColPos; 58 private PreparedStatement oInsr; 59 private PreparedStatement oUpdt; 60 61 63 public TableLoader(String sTableName) { 64 super(sTableName); 65 aColNames = null; 66 } 67 68 70 public String [] columnNames() throws IllegalStateException { 71 if (null==aColNames) 72 throw new IllegalStateException ("TableLoader: must call prepare() before columnNames()"); 73 return aColNames; 74 } 75 76 78 public void prepare(Connection oConn, ColumnList oColList) throws SQLException { 79 readColumns(oConn, oConn.getMetaData()); 80 aColNames = Gadgets.split(getColumnsStr(),','); 81 aValues = new Object [columnCount()]; 82 String sSQL; 83 String sCol; 84 oInsrColPos = new HashMap (1+oColList.size()*2); 85 oUpdtColPos = new HashMap (1+oColList.size()*2); 86 87 sSQL = "INSERT INTO "+getName()+" ("+oColList.toString(",")+") VALUES ("; 90 for (int c=0; c<oColList.size(); c++) { 91 oInsrColPos.put(oColList.getColumnName(c),new Integer (c+1)); 92 sSQL += (c == 0 ? "" : ",") + "?"; 93 } 94 sSQL += ")"; 95 oInsr = oConn.prepareStatement(sSQL); 96 97 100 sSQL = oColList.toString("=?,"); 101 ListIterator oIter = getPrimaryKey().listIterator(); 102 while (oIter.hasNext()) { 103 sCol = (String ) oIter.next(); 104 try { 105 sSQL = Gadgets.replace(sSQL, sCol + "=?,", ""); 106 sSQL = Gadgets.replace(sSQL, sCol + "=?", ""); 107 } catch (Exception neverthrown) {} 108 } 109 String [] aUpdtCols = Gadgets.split(sSQL, ","); 110 for (int c=0; c<aUpdtCols.length; c++) { 111 oUpdtColPos.put(Gadgets.dechomp(aUpdtCols[c],"=?"),new Integer (c+1)); 112 } 113 sSQL = "UPDATE "+getName()+" SET "+sSQL+ " WHERE "; 114 oIter = getPrimaryKey().listIterator(); 115 int iPK=1; 116 while (oIter.hasNext()) { 117 sCol = (String ) oIter.next(); 118 oUpdtColPos.put(sCol,new Integer (aUpdtCols.length+iPK)); 119 if (iPK>1) sSQL += " AND "; 120 sSQL += sCol+"=?"; 121 iPK++; 122 } 123 124 oUpdt = oConn.prepareStatement(sSQL); 125 } 127 129 public Object get(int iColumnIndex) throws ArrayIndexOutOfBoundsException { 130 return aValues[iColumnIndex]; 131 } 132 133 135 public Object get(String sColumnName) throws ArrayIndexOutOfBoundsException { 136 return aValues[getColumnIndex(sColumnName)-1]; 137 } 138 139 141 public void put(int iColumnIndex, Object oValue) throws ArrayIndexOutOfBoundsException { 142 aValues[iColumnIndex]=oValue; 143 } 144 145 147 public void put(String sColumnName, Object oValue) throws ArrayIndexOutOfBoundsException { 148 aValues[getColumnIndex(sColumnName)-1]=oValue; 149 } 150 151 153 public void setAllColumnsToNull() { 154 for (int c=columnCount(); c>=0; c--) aValues[c]=null; 155 } 156 157 159 public void close() throws SQLException { 160 try { if (oUpdt!=null) oUpdt.close(); } catch (Exception ignore) {} 161 try { if (oInsr!=null) oUpdt.close(); } catch (Exception ignore) {} 162 } 163 164 166 private static boolean test(int iInputValue, int iBitMask) { 167 return (iInputValue&iBitMask)!=0; 168 } 170 172 public void store(Connection oConn, String sWorkArea, int iFlags) 173 throws SQLException ,IllegalArgumentException ,NullPointerException { 174 175 int iAffected; 176 if (oUpdt==null || oInsr==null) 177 throw new SQLException ("Invalid command sequece. Must call ContactLoader.prepare() before TableLoader.store()"); 178 179 if (!test(iFlags,MODE_APPEND) && !test(iFlags,MODE_UPDATE)) 180 throw new IllegalArgumentException ("TableLoader.store() Flags bitmask must contain either MODE_APPEND, MODE_UPDATE or both"); 181 182 if (test(iFlags,MODE_UPDATE)) { 183 ListIterator oIter = getColumns().listIterator(); 184 int iPos = 0; 185 while (oIter.hasNext()) { 186 DBColumn oCol = (DBColumn) oIter.next(); 187 Integer iUpdtPos = (Integer ) oUpdtColPos.get(oCol.getName()); 188 if (null!=iUpdtPos) { 189 oUpdt.setObject(iUpdtPos.intValue(),aValues[iPos], oCol.getSqlType()); 190 } 191 iPos++; 192 } iAffected = oUpdt.executeUpdate(); 194 } 196 if (test(iFlags,MODE_APPEND)) { 197 ListIterator oIter = getColumns().listIterator(); 198 int iPos = 0; 199 while (oIter.hasNext()) { 200 DBColumn oCol = (DBColumn) oIter.next(); 201 Integer iInsrPos = (Integer ) oUpdtColPos.get(oCol.getName()); 202 if (null!=iInsrPos) { 203 oUpdt.setObject(iInsrPos.intValue(),aValues[iPos], oCol.getSqlType()); 204 } 205 iPos++; 206 } iAffected = oInsr.executeUpdate(); 208 } } 210 211 } 213 | Popular Tags |