1 22 23 28 29 package org.xquark.mapper.dbms; 30 31 import java.sql.SQLException ; 32 33 import org.xquark.mapper.RepositoryException; 34 35 42 public class Sequence 43 { 44 private static final String RCSRevision = "$Revision: 1.1 $"; 45 private static final String RCSName = "$Name: $"; 46 47 private TableInfo seq; 48 private short preAllocation; 49 private long lastDBValue; 50 private long currentValue; 51 private long maxValue; 52 private boolean initialized = false; 53 54 55 public Sequence(TableInfo seq, short preAllocation) 56 { 57 this.seq = seq; 58 this.preAllocation = preAllocation; 59 maxValue = (long) (Math.pow(10, seq.getColumns()[0].getPrecision()) - 1f); 60 } 61 62 63 public long nextValue(AbstractConnection connection) throws RepositoryException 64 { 65 if (currentValue == maxValue) 66 throw new RepositoryException(RepositoryException.NOT_ALLOWED, 67 "The sequence " + seq.getName() + " has reached its maximum value."); 68 if (!initialized || (currentValue == (lastDBValue + preAllocation - 1))) 69 currentValue = lastDBValue = connection.nextSequenceValue(seq, preAllocation); 70 else 71 currentValue++; 72 initialized = true; 73 return currentValue; 74 } 75 76 79 public long currentValue() throws RepositoryException 80 { 81 if (initialized) 82 return currentValue; 83 else 84 throw new RepositoryException(RepositoryException.NOT_ALLOWED, 85 "The sequence was not initialized."); 86 } 87 88 90 public void create(AbstractConnection connection) throws RepositoryException 91 { 92 if (initialized) 93 throw new RepositoryException(RepositoryException.NOT_ALLOWED, 94 "The sequence is already initialized."); 95 else 96 { 97 try 98 { 99 connection.createSequence(seq, preAllocation); 100 } 101 catch (SQLException e) 102 { 103 throw new RepositoryException(RepositoryException.DB_ERROR, 104 "JDBC error while creating the "+ seq.getName() + " sequence."); 105 } 106 initialized = true; 107 } 108 } 109 110 public TableInfo getTableInfo() 111 { 112 return seq; 113 } 114 } 115 | Popular Tags |