1 19 20 21 package org.apache.cayenne.dba.oracle; 22 23 import java.sql.Connection ; 24 import java.sql.ResultSet ; 25 import java.sql.SQLException ; 26 import java.sql.Statement ; 27 import java.util.ArrayList ; 28 import java.util.Collections ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import org.apache.cayenne.CayenneRuntimeException; 33 import org.apache.cayenne.access.DataNode; 34 import org.apache.cayenne.access.QueryLogger; 35 import org.apache.cayenne.dba.JdbcPkGenerator; 36 import org.apache.cayenne.map.DbEntity; 37 import org.apache.cayenne.map.DbKeyGenerator; 38 39 56 public class OraclePkGenerator extends JdbcPkGenerator { 57 58 private static final String _SEQUENCE_PREFIX = "pk_"; 59 60 public void createAutoPk(DataNode node, List dbEntities) throws Exception { 61 List sequences = getExistingSequences(node); 62 63 Iterator it = dbEntities.iterator(); 65 while (it.hasNext()) { 66 DbEntity ent = (DbEntity) it.next(); 67 if (!sequences.contains(sequenceName(ent))) { 68 runUpdate(node, createSequenceString(ent)); 69 } 70 } 71 } 72 73 public List createAutoPkStatements(List dbEntities) { 74 List list = new ArrayList (); 75 Iterator it = dbEntities.iterator(); 76 while (it.hasNext()) { 77 DbEntity ent = (DbEntity) it.next(); 78 list.add(createSequenceString(ent)); 79 } 80 81 return list; 82 } 83 84 public void dropAutoPk(DataNode node, List dbEntities) throws Exception { 85 List sequences = getExistingSequences(node); 86 87 Iterator it = dbEntities.iterator(); 89 while (it.hasNext()) { 90 DbEntity ent = (DbEntity) it.next(); 91 if (sequences.contains(stripSchemaName(sequenceName(ent)))) { 92 runUpdate(node, dropSequenceString(ent)); 93 } 94 } 95 } 96 97 public List dropAutoPkStatements(List dbEntities) { 98 List list = new ArrayList (); 99 Iterator it = dbEntities.iterator(); 100 while (it.hasNext()) { 101 DbEntity ent = (DbEntity) it.next(); 102 list.add(dropSequenceString(ent)); 103 } 104 105 return list; 106 } 107 108 protected String createSequenceString(DbEntity ent) { 109 StringBuffer buf = new StringBuffer (); 110 buf 111 .append("CREATE SEQUENCE ") 112 .append(sequenceName(ent)) 113 .append(" START WITH 200") 114 .append(" INCREMENT BY ") 115 .append(pkCacheSize(ent)); 116 return buf.toString(); 117 } 118 119 123 protected String dropSequenceString(DbEntity ent) { 124 StringBuffer buf = new StringBuffer (); 125 buf.append("DROP SEQUENCE ").append(sequenceName(ent)); 126 return buf.toString(); 127 } 128 129 137 protected int pkFromDatabase(DataNode node, DbEntity ent) throws Exception { 138 139 DbKeyGenerator pkGenerator = ent.getPrimaryKeyGenerator(); 140 String pkGeneratingSequenceName; 141 if (pkGenerator != null 142 && DbKeyGenerator.ORACLE_TYPE.equals(pkGenerator.getGeneratorType()) 143 && pkGenerator.getGeneratorName() != null) 144 pkGeneratingSequenceName = pkGenerator.getGeneratorName(); 145 else 146 pkGeneratingSequenceName = sequenceName(ent); 147 148 Connection con = node.getDataSource().getConnection(); 149 try { 150 Statement st = con.createStatement(); 151 try { 152 String sql = "SELECT " + pkGeneratingSequenceName + ".nextval FROM DUAL"; 153 QueryLogger.logQuery(sql, Collections.EMPTY_LIST); 154 ResultSet rs = st.executeQuery(sql); 155 try { 156 if (!rs.next()) { 158 throw new CayenneRuntimeException( 159 "Error generating pk for DbEntity " + ent.getName()); 160 } 161 return rs.getInt(1); 162 } 163 finally { 164 rs.close(); 165 } 166 } 167 finally { 168 st.close(); 169 } 170 } 171 finally { 172 con.close(); 173 } 174 } 175 176 protected int pkCacheSize(DbEntity entity) { 177 DbKeyGenerator keyGenerator = entity.getPrimaryKeyGenerator(); 179 if (keyGenerator != null 180 && DbKeyGenerator.ORACLE_TYPE.equals(keyGenerator.getGeneratorType()) 181 && keyGenerator.getGeneratorName() != null) { 182 183 Integer size = keyGenerator.getKeyCacheSize(); 184 return (size != null && size.intValue() >= 1) ? size.intValue() : super 185 .getPkCacheSize(); 186 } 187 else { 188 return super.getPkCacheSize(); 189 } 190 } 191 192 193 protected String sequenceName(DbEntity entity) { 194 195 DbKeyGenerator keyGenerator = entity.getPrimaryKeyGenerator(); 197 if (keyGenerator != null 198 && DbKeyGenerator.ORACLE_TYPE.equals(keyGenerator.getGeneratorType()) 199 && keyGenerator.getGeneratorName() != null) { 200 201 return keyGenerator.getGeneratorName().toLowerCase(); 202 } 203 else { 204 String entName = entity.getName(); 205 String seqName = _SEQUENCE_PREFIX + entName.toLowerCase(); 206 207 if (entity.getSchema() != null && entity.getSchema().length() > 0) { 208 seqName = entity.getSchema() + "." + seqName; 209 } 210 return seqName; 211 } 212 } 213 214 protected String stripSchemaName(String sequenceName) { 215 int ind = sequenceName.indexOf('.'); 216 return (ind >= 0) ? sequenceName.substring(ind + 1) : sequenceName; 217 } 218 219 222 protected List getExistingSequences(DataNode node) throws SQLException { 223 224 Connection con = node.getDataSource().getConnection(); 226 227 try { 228 Statement sel = con.createStatement(); 229 try { 230 String sql = "SELECT LOWER(SEQUENCE_NAME) FROM ALL_SEQUENCES"; 231 QueryLogger.logQuery(sql, Collections.EMPTY_LIST); 232 ResultSet rs = sel.executeQuery(sql); 233 try { 234 List sequenceList = new ArrayList (); 235 while (rs.next()) { 236 sequenceList.add(rs.getString(1)); 237 } 238 return sequenceList; 239 } 240 finally { 241 rs.close(); 242 } 243 } 244 finally { 245 sel.close(); 246 } 247 } 248 finally { 249 con.close(); 250 } 251 } 252 } 253 | Popular Tags |