1 package org.apache.torque.oid; 2 3 21 22 import java.math.BigDecimal ; 23 import java.sql.Connection ; 24 import org.apache.torque.adapter.DB; 25 import org.apache.torque.util.SQLBuilder; 26 import com.workingdogs.village.QueryDataSet; 27 import com.workingdogs.village.Record; 28 import com.workingdogs.village.Value; 29 30 38 public class AutoIncrementIdGenerator implements IdGenerator 39 { 40 41 private DB dbAdapter; 42 43 44 private String name = null; 45 46 52 public AutoIncrementIdGenerator(final DB dbAdapter, final String name) 53 { 54 this.dbAdapter = dbAdapter; 55 this.name = name; 56 } 57 58 66 public int getIdAsInt(Connection connection, Object keyInfo) 67 throws Exception 68 { 69 return getIdAsVillageValue(connection, keyInfo).asInt(); 70 } 71 72 80 public long getIdAsLong(Connection connection, Object keyInfo) 81 throws Exception 82 { 83 return getIdAsVillageValue(connection, keyInfo).asLong(); 84 } 85 86 95 public BigDecimal getIdAsBigDecimal(Connection connection, Object keyInfo) 96 throws Exception 97 { 98 return getIdAsVillageValue(connection, keyInfo).asBigDecimal(); 99 } 100 101 102 111 public String getIdAsString(Connection connection, Object keyInfo) 112 throws Exception 113 { 114 return getIdAsVillageValue(connection, keyInfo).asString(); 115 } 116 117 122 public boolean isPriorToInsert() 123 { 124 return false; 125 } 126 127 132 public boolean isPostInsert() 133 { 134 return true; 135 } 136 137 143 public final boolean isConnectionRequired() 144 { 145 return true; 146 } 147 148 157 private Value getIdAsVillageValue(Connection connection, Object keyInfo) 158 throws Exception 159 { 160 String tableName = SQLBuilder.getFullTableName(String.valueOf(keyInfo), name); 161 String idSQL = dbAdapter.getIDMethodSQL(tableName); 162 Value id = null; 163 QueryDataSet qds = null; 164 try 165 { 166 qds = new QueryDataSet(connection, idSQL); 167 qds.fetchRecords(1); 168 Record rec = qds.getRecord(0); 169 id = rec.getValue(1); 170 } 171 finally 172 { 173 if (qds != null) 174 { 175 qds.close(); 176 } 177 } 178 return id; 179 } 180 } 181 | Popular Tags |