1 64 65 package com.jcorporate.expresso.core.dbobj; 66 67 76 77 import com.jcorporate.expresso.core.dataobjects.jdbc.JDBCObjectMetaData; 78 import com.jcorporate.expresso.core.db.DBConnection; 79 import com.jcorporate.expresso.core.db.DBException; 80 81 import java.util.Iterator ; 82 import java.util.Vector ; 83 84 85 114 public abstract class DBSequence 115 extends SecuredDBObject { 116 117 private static final String THIS_CLASS = DBSequence.class.getName(); 118 119 private Integer increment = null; 120 private Integer minvalue = null; 121 private Integer maxvalue = null; 122 private Integer start = null; 123 124 130 public DBSequence() 131 throws com.jcorporate.expresso.core.db.DBException { 132 super(); 133 } 134 135 140 public DBSequence(DBConnection theConnection) 141 throws DBException { 142 super(theConnection); 143 } 144 145 146 public DBSequence(int uid) 147 throws DBException { 148 super(uid); 149 } 150 151 154 public void add() 155 throws DBException { 156 throw new DBException("You cannot add to a sequence"); 157 } 158 159 160 164 public synchronized void clear() 165 throws DBException { 166 throw new DBException("Cannot call clear() on a DBSequence"); 167 } 168 169 170 173 public synchronized void createTable() 174 throws DBException { 175 String myName = (THIS_CLASS + "createTable()"); 176 String createStatement = ("CREATE SEQUENCE "); 177 DBConnection myConnection = null; 178 179 try { 180 myConnection = this.getConnectionPool().getConnection(myName); 181 createStatement = createStatement + getJDBCMetaData().getTargetTable(); 182 183 184 185 if (myConnection.getDBDriver().equals("postgresql.Driver") || 186 myConnection.getDBDriver().equals("org.postgresql.Driver")) { 187 if (increment != null) { 188 createStatement = createStatement + " increment = " + 189 increment.intValue(); 190 } 191 if (minvalue != null) { 192 createStatement = createStatement + " minvalue = " + 193 minvalue.intValue(); 194 } 195 if (maxvalue != null) { 196 createStatement = createStatement + " maxvalue = " + 197 maxvalue.intValue(); 198 } 199 if (start != null) { 200 createStatement = createStatement + " start = " + 201 start.intValue(); 202 } 203 } else { 204 throw new DBException("Only PostgreSQL currently supported"); 205 } 206 207 myConnection.executeUpdate(createStatement); 208 } catch (DBException de) { 209 throw de; 210 } finally { 211 myConnection.release(); 212 } 213 } 214 215 216 220 public void delete() 221 throws com.jcorporate.expresso.core.db.DBException { 222 throw new DBException("Can't Delete from a sequence"); 223 } 224 225 226 231 public boolean find() 232 throws com.jcorporate.expresso.core.db.DBException { 233 throw new DBException("Can't execute find on a DBSequence"); 234 } 235 236 237 240 public long getNext() 241 throws DBException { 242 this.retrieve(); 243 244 long returnValue; 245 246 try { 247 returnValue = new Long (getField("nextval")).longValue(); 248 } catch (NumberFormatException ne) { 249 throw new DBException("DBSequence.getNext: Could not convert \'" + 250 getField("nextval") + "\' to a number:" + 251 ne.getMessage()); 252 } catch (Exception e) { 253 throw new DBException("DBSequence.getNext: " + e.toString()); 254 } 255 256 return returnValue; 257 } 258 259 260 266 public abstract DBObject getThisDBObj() 267 throws DBException; 268 269 270 274 public void retrieve() 275 throws com.jcorporate.expresso.core.db.DBException { 276 String myName = (THIS_CLASS + "retrieve()"); 277 DBConnection myConnection = null; 278 JDBCObjectMetaData metadata = this.getJDBCMetaData(); 279 try { 280 myConnection = this.getConnectionPool().getConnection(myName); 281 282 String myStatement = "SELECT "; 283 284 285 286 if (myConnection.getDBDriver().equals("postgresql.Driver") || 287 myConnection.getDBDriver().equals("org.postgresql.Driver")) { 288 myStatement = myStatement + "nextval('" + metadata.getTargetTable() + 289 "') as nextval"; 290 } else { 291 throw new DBException("DBSequence only supported in PostgreSQL"); 292 } 293 if (myConnection == null) { 294 throw new DBException(myName + ":No connection - object not " + 295 " correctly initialized (" + metadata.getName() + 296 ") No such record"); 297 } 298 try { 299 myConnection.execute(myStatement); 300 301 String oneFieldName = (""); 302 String oneFieldValue = (""); 303 304 if (myConnection.next()) { 305 int i = 1; 306 307 for (Iterator e = metadata.getFieldListArray().iterator(); 308 e.hasNext();) { 309 oneFieldName = (String ) e.next(); 310 311 try { 312 313 int myint = myConnection.getInt(i); 315 oneFieldValue = "" + myint; 316 } catch (DBException de1) { 317 throw new DBException(myName + ": (" + metadata.getName() + 318 ") Error retrieving field '" + 319 oneFieldName + "' index " + i + 320 ":" + de1.getMessage(), 321 de1.getDBMessage()); 322 } 323 324 i++; 325 setField(oneFieldName, oneFieldValue); 326 } 327 } else { 328 throw new DBException(myName + ": (" + metadata.getName() + 329 ") No such record"); 330 } 331 } catch (DBException de) { 332 throw de; 333 } finally { 334 myConnection.release(); 335 } 336 } catch (Exception e) { 337 throw new DBException("DBSequence retrieve error: " + 338 e.toString()); 339 } 340 } 341 342 343 347 public synchronized void search() 348 throws com.jcorporate.expresso.core.db.DBException { 349 throw new DBException("Can't search on a DBSequence"); 350 } 351 352 353 358 public synchronized Vector searchAndRetrieve() 359 throws com.jcorporate.expresso.core.db.DBException { 360 throw new DBException("Can't search on a DBSequence"); 361 } 362 363 364 370 public synchronized Vector searchAndRetrieve(String sortKeyString) 371 throws com.jcorporate.expresso.core.db.DBException { 372 throw new DBException("can't search on a DBSequence"); 373 } 374 375 376 381 public void setIncrement(Integer I) { 382 increment = I; 383 } 384 385 390 public void setMaxValue(Integer I) { 391 maxvalue = I; 392 } 393 394 399 public void setMinValue(Integer I) { 400 minvalue = I; 401 } 402 403 408 public void setStart(Integer I) { 409 start = I; 410 } 411 412 426 protected void setupFields() 427 throws com.jcorporate.expresso.core.db.DBException { 428 addField("nextval", "int", 0, false, "Next Number Value"); 429 } 430 431 432 438 public void update() 439 throws com.jcorporate.expresso.core.db.DBException { 440 throw new DBException("Can't call update on a DBSequence"); 441 } 442 443 444 447 public void verify() 448 throws com.jcorporate.expresso.core.db.DBException { 449 } 450 451 452 } 453 454 | Popular Tags |