1 19 package org.apache.cayenne.dba.db2; 20 21 import java.sql.Connection ; 22 import java.sql.ResultSet ; 23 import java.sql.SQLException ; 24 import java.sql.Statement ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import org.apache.cayenne.CayenneRuntimeException; 32 import org.apache.cayenne.access.DataNode; 33 import org.apache.cayenne.access.QueryLogger; 34 import org.apache.cayenne.dba.JdbcPkGenerator; 35 import org.apache.cayenne.map.DbEntity; 36 37 42 public class DB2PkGenerator extends JdbcPkGenerator { 43 44 private static final String _SEQUENCE_PREFIX = "S_"; 45 46 49 public static final String SEQUENCE_PREFIX = "S_"; 50 51 protected int pkFromDatabase(DataNode node, DbEntity ent) throws Exception { 52 53 String pkGeneratingSequenceName = sequenceName(ent); 54 55 Connection con = node.getDataSource().getConnection(); 56 try { 57 Statement st = con.createStatement(); 58 try { 59 String sql = "SELECT NEXTVAL FOR " 60 + pkGeneratingSequenceName 61 + " FROM SYSIBM.SYSDUMMY1"; 62 QueryLogger.logQuery(sql, Collections.EMPTY_LIST); 63 ResultSet rs = st.executeQuery(sql); 64 try { 65 if (!rs.next()) { 67 throw new CayenneRuntimeException( 68 "Error generating pk for DbEntity " + ent.getName()); 69 } 70 return rs.getInt(1); 71 } 72 finally { 73 rs.close(); 74 } 75 } 76 finally { 77 st.close(); 78 } 79 } 80 finally { 81 con.close(); 82 } 83 } 84 85 public void createAutoPk(DataNode node, List dbEntities) throws Exception { 86 Collection sequences = getExistingSequences(node); 87 Iterator it = dbEntities.iterator(); 88 89 while (it.hasNext()) { 90 DbEntity entity = (DbEntity) it.next(); 91 if (!sequences.contains(sequenceName(entity))) { 92 this.runUpdate(node, createSequenceString(entity)); 93 } 94 } 95 } 96 97 100 public List createAutoPkStatements(List dbEntities) { 101 List list = new ArrayList (dbEntities.size()); 102 Iterator it = dbEntities.iterator(); 103 104 while (it.hasNext()) { 105 DbEntity ent = (DbEntity) it.next(); 106 list.add(createSequenceString(ent)); 107 } 108 109 return list; 110 } 111 112 115 public void dropAutoPk(DataNode node, List dbEntities) throws Exception { 116 Collection sequences = getExistingSequences(node); 117 118 Iterator it = dbEntities.iterator(); 119 while (it.hasNext()) { 120 DbEntity ent = (DbEntity) it.next(); 121 if (sequences.contains(sequenceName(ent))) { 122 runUpdate(node, dropSequenceString(ent)); 123 } 124 } 125 } 126 127 130 public List dropAutoPkStatements(List dbEntities) { 131 132 List list = new ArrayList (dbEntities.size()); 133 Iterator it = dbEntities.iterator(); 134 135 while (it.hasNext()) { 136 DbEntity entity = (DbEntity) it.next(); 137 list.add(dropSequenceString(entity)); 138 } 139 140 return list; 141 } 142 143 146 protected List getExistingSequences(DataNode node) throws SQLException { 147 148 Connection con = node.getDataSource().getConnection(); 150 151 try { 152 Statement sel = con.createStatement(); 153 try { 154 StringBuffer buffer = new StringBuffer (); 155 buffer.append("SELECT SEQNAME FROM SYSCAT.SEQUENCES ").append( 156 "WHERE SEQNAME LIKE '").append(_SEQUENCE_PREFIX).append("%'"); 157 158 String sql = buffer.toString(); 159 QueryLogger.logQuery(sql, Collections.EMPTY_LIST); 160 ResultSet rs = sel.executeQuery(sql); 161 try { 162 List sequenceList = new ArrayList (); 163 while (rs.next()) { 164 sequenceList.add(rs.getString(1)); 165 } 166 return sequenceList; 167 } 168 finally { 169 rs.close(); 170 } 171 } 172 finally { 173 sel.close(); 174 } 175 } 176 finally { 177 con.close(); 178 } 179 } 180 181 184 protected String sequenceName(DbEntity entity) { 185 186 String entName = entity.getName(); 187 String seqName = _SEQUENCE_PREFIX + entName; 188 189 if (entity.getSchema() != null && entity.getSchema().length() > 0) { 190 seqName = entity.getSchema() + "." + seqName; 191 } 192 return seqName; 193 } 194 195 198 protected String dropSequenceString(DbEntity entity) { 199 return "DROP SEQUENCE " + sequenceName(entity) + " RESTRICT "; 200 } 201 202 205 protected String createSequenceString(DbEntity entity) { 206 StringBuffer buf = new StringBuffer (); 207 buf 208 .append("CREATE SEQUENCE ") 209 .append(sequenceName(entity)) 210 .append(" START WITH 200") 211 .append(" INCREMENT BY ") 212 .append(getPkCacheSize()) 213 .append(" NO MAXVALUE ") 214 .append(" NO CYCLE ") 215 .append(" CACHE ") 216 .append(getPkCacheSize()); 217 return buf.toString(); 218 } 219 } 220 | Popular Tags |