1 64 65 package com.jcorporate.expresso.core.dbobj; 66 67 import com.jcorporate.expresso.core.dataobjects.DataException; 68 import com.jcorporate.expresso.core.dataobjects.jdbc.JDBCObjectMetaData; 69 import com.jcorporate.expresso.core.db.DBConnection; 70 import com.jcorporate.expresso.core.db.DBException; 71 import com.jcorporate.expresso.core.misc.ConfigManager; 72 import com.jcorporate.expresso.core.misc.ConfigurationException; 73 import com.jcorporate.expresso.core.misc.StringUtil; 74 75 import java.sql.DatabaseMetaData ; 76 import java.sql.ResultSet ; 77 import java.sql.SQLException ; 78 79 80 87 public class AutoDBObject 88 extends SecuredDBObject { 89 private static final String THIS_CLASS = "com.jcorporate.expreso.core.dbobj.AutoDBObject."; 90 91 97 public AutoDBObject() 98 throws com.jcorporate.expresso.core.db.DBException { 99 super(); 100 } 101 102 109 public AutoDBObject(com.jcorporate.expresso.core.db.DBConnection newConnection) 110 throws com.jcorporate.expresso.core.db.DBException { 111 super(newConnection); 112 } 113 114 115 121 public AutoDBObject(int uid) 122 throws DBException { 123 super(uid); 124 } 125 126 131 public DBObject getThisDBObj() 132 throws com.jcorporate.expresso.core.db.DBException { 133 AutoDBObject returnObj = new AutoDBObject(); 134 returnObj.setTargetTable(this.getDefinitionName()); 135 136 return returnObj; 137 } 138 139 140 147 public synchronized void setTargetTable(String theTable) 148 throws DBException { 149 clear(); 150 DBObjectDef myDef = new DBObjectDef(); 151 myDef.setName(getClass().getName()); 152 sMetadataMap.put(getClass().getName(), myDef); 153 super.setTargetTable(theTable); 154 setupFields(); 155 } 156 157 169 public void setDefinitionName(String definitionName) throws DataException { 170 try { 171 this.setTargetTable(definitionName); 172 } catch (DBException ex) { 173 throw new DataException("Unable to set definition name for AutoDBObject" 174 , ex); 175 } 176 } 177 178 183 public String getDefinitionName() { 184 try { 185 return ((JDBCObjectMetaData) this.getMetaData()).getTargetTable(); 186 } catch (DataException ex) { 187 return null; 188 } 189 } 190 191 192 196 protected void setupFields() 197 throws DBException { 198 199 if (StringUtil.notNull(this.getDefinitionName()).equals("")) { 200 return; 201 } 202 203 DBConnection myConnection = null; 204 205 try { 206 if (localConnection != null) { 207 myConnection = localConnection; 208 } else { 209 String myName = (THIS_CLASS + "setupFields()"); 210 myConnection = this.getConnectionPool().getConnection(myName); 211 } 212 213 DatabaseMetaData dm = myConnection.getDBMetaData(); 214 String tableName = getDefinitionName().toUpperCase(); 215 setDescription("Automatic Object for " + tableName); 216 try { 217 if (ConfigManager.getJdbcRequired(this.getDataContext()).getDriver().equals("org.hsqldb.jdbcDriver")) { 220 throw new DBException(this.getClass() + " does not support hsqldb."); 221 } 222 223 if (ConfigManager.getJdbcRequired(this.getDataContext()).getDriver() 226 .equals("org.postgresql.Driver")) { 227 228 tableName = tableName.toLowerCase(); 229 } 230 } catch (ConfigurationException ce) { 231 ce.printStackTrace(); 232 throw new DBException(ce); 233 } 234 setDescription("Automatic Object for " + tableName); 235 236 ResultSet rs = dm.getColumns(null, "%", tableName, "%"); 237 String fieldName = null; 238 String fieldType; 239 int fieldSize; 240 int nullField = 0; 241 boolean nullable; 242 String fieldDescrip = null; 243 244 while (rs.next()) { 245 fieldName = rs.getString(4); 246 fieldSize = rs.getInt(7); 247 fieldDescrip = rs.getString(12); 248 249 if (fieldDescrip == null) { 250 fieldDescrip = fieldName; 251 } 252 253 fieldType = rs.getString(5); 254 nullField = rs.getInt(11); 255 256 if (nullField == DatabaseMetaData.columnNoNulls) { 257 nullable = false; 258 } else { 259 nullable = true; 260 } 261 if (nullable) { 262 addField(fieldName, fieldType, fieldSize, true, 263 fieldDescrip); 264 } else { 265 addField(fieldName, fieldType, fieldSize, false, 266 fieldDescrip); 267 } 268 } 269 270 271 ResultSet rspk = dm.getPrimaryKeys(null, "%", tableName); 272 273 while (rspk.next()) { 274 addKey(rspk.getString(4)); 275 } 276 } catch (SQLException se) { 277 throw new DBException(se); 278 } finally { 279 if (localConnection == null) { 280 myConnection.release(); 281 } 282 } 283 } 284 285 286 } 287 288 289 | Popular Tags |