1 package org.apache.torque.oid; 2 3 21 22 import java.math.BigDecimal ; 23 import java.sql.Connection ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 import org.apache.torque.adapter.DB; 29 import org.apache.torque.util.SQLBuilder; 30 31 import com.workingdogs.village.QueryDataSet; 32 import com.workingdogs.village.Record; 33 import com.workingdogs.village.Value; 34 35 42 public class SequenceIdGenerator implements IdGenerator 43 { 44 45 private static Log log = LogFactory.getLog(SequenceIdGenerator.class); 46 47 48 private DB dbAdapter; 49 50 51 private String name = null; 52 53 59 public SequenceIdGenerator(final DB dbAdapter, final String name) 60 { 61 this.dbAdapter = dbAdapter; 62 this.name = name; 63 } 64 65 73 public int getIdAsInt(Connection connection, Object keyInfo) 74 throws Exception 75 { 76 return getIdAsVillageValue(connection, keyInfo).asInt(); 77 } 78 79 87 public long getIdAsLong(Connection connection, Object keyInfo) 88 throws Exception 89 { 90 return getIdAsVillageValue(connection, keyInfo).asLong(); 91 } 92 93 101 public BigDecimal getIdAsBigDecimal(Connection connection, Object keyInfo) 102 throws Exception 103 { 104 return getIdAsVillageValue(connection, keyInfo).asBigDecimal(); 105 } 106 107 115 public String getIdAsString(Connection connection, Object keyInfo) 116 throws Exception 117 { 118 return getIdAsVillageValue(connection, keyInfo).asString(); 119 } 120 121 126 public boolean isPriorToInsert() 127 { 128 return true; 129 } 130 131 136 public boolean isPostInsert() 137 { 138 return false; 139 } 140 141 147 public boolean isConnectionRequired() 148 { 149 return true; 150 } 151 152 160 private Value getIdAsVillageValue(Connection connection, Object keyInfo) 161 throws Exception 162 { 163 String sequenceName = SQLBuilder.getFullTableName(String.valueOf(keyInfo), name); 164 String idSql = dbAdapter.getIDMethodSQL(sequenceName); 165 if (log.isDebugEnabled()) 166 { 167 log.debug(idSql); 168 } 169 170 QueryDataSet qds = new QueryDataSet(connection, idSql); 172 Record rec; 173 try 174 { 175 qds.fetchRecords(1); 176 rec = qds.getRecord(0); } 178 finally 179 { 180 qds.close(); 181 } 182 return rec.getValue(1); } 184 } 185 | Popular Tags |