1 56 57 package org.objectstyle.cayenne.dba.db2; 58 59 import java.sql.Connection ; 60 import java.sql.ResultSet ; 61 import java.sql.SQLException ; 62 import java.sql.Statement ; 63 import java.util.ArrayList ; 64 import java.util.Collections ; 65 import java.util.Iterator ; 66 import java.util.List ; 67 68 import org.objectstyle.cayenne.CayenneRuntimeException; 69 import org.objectstyle.cayenne.access.DataNode; 70 import org.objectstyle.cayenne.access.QueryLogger; 71 import org.objectstyle.cayenne.dba.JdbcPkGenerator; 72 import org.objectstyle.cayenne.map.DbEntity; 73 74 79 public class DB2PkGenerator extends JdbcPkGenerator { 80 81 public static final String SEQUENCE_PREFIX = "S_"; 82 83 public void createAutoPk(DataNode node, List dbEntities) throws Exception { 84 List sequences = this.getExistingSequences(node); 85 Iterator it = dbEntities.iterator(); 86 87 while (it.hasNext()) { 88 DbEntity ent = (DbEntity) it.next(); 89 if (!sequences.contains(sequenceName(ent))) { 90 this.runUpdate(node, this.createSequenceString(ent)); 91 } 92 } 93 } 94 95 public List createAutoPkStatements(List dbEntities) { 96 List list = new ArrayList (); 97 Iterator it = dbEntities.iterator(); 98 99 while (it.hasNext()) { 100 DbEntity ent = (DbEntity) it.next(); 101 list.add(this.createSequenceString(ent)); 102 } 103 104 return list; 105 } 106 107 public void dropAutoPk(DataNode node, List dbEntities) throws Exception { 108 List sequences = this.getExistingSequences(node); 109 110 Iterator it = dbEntities.iterator(); 111 while (it.hasNext()) { 112 DbEntity ent = (DbEntity) it.next(); 113 if (sequences.contains(this.sequenceName(ent))) { 114 this.runUpdate(node, this.dropSequenceString(ent)); 115 } 116 } 117 } 118 119 public List dropAutoPkStatements(List dbEntities) { 120 List list = new ArrayList (); 121 Iterator it = dbEntities.iterator(); 122 123 while (it.hasNext()) { 124 DbEntity ent = (DbEntity) it.next(); 125 list.add(this.dropSequenceString(ent)); 126 } 127 128 return list; 129 } 130 131 134 protected String sequenceName(DbEntity ent) { 135 String seqName = SEQUENCE_PREFIX + ent.getName(); 136 137 if (ent.getSchema() != null && ent.getSchema().length() > 0) { 138 seqName = ent.getSchema() + "." + seqName; 139 } 140 141 return seqName; 142 } 143 144 145 148 protected String createSequenceString(DbEntity ent) { 149 StringBuffer buf = new StringBuffer (); 150 buf.append("CREATE SEQUENCE ") 151 .append(this.sequenceName(ent)) 152 .append(" START WITH 200") 153 .append(" INCREMENT BY ").append(getPkCacheSize()) 154 .append(" NO MAXVALUE ") 155 .append(" NO CYCLE ") 156 .append(" CACHE ").append(getPkCacheSize()); 157 return buf.toString(); 158 } 159 160 163 protected String dropSequenceString(DbEntity ent) { 164 return "DROP SEQUENCE " + this.sequenceName(ent) + " RESTRICT "; 165 } 166 167 174 protected int pkFromDatabase(DataNode node, DbEntity ent) throws Exception { 175 176 String seq_name = sequenceName (ent); 177 Connection con = node.getDataSource().getConnection(); 178 try { 179 Statement st = con.createStatement(); 180 try { 181 String pkQueryString = "SELECT NEXTVAL FOR " 182 + seq_name 183 + " FROM SYSIBM.SYSDUMMY1"; 184 QueryLogger.logQuery(QueryLogger.DEFAULT_LOG_LEVEL, pkQueryString, Collections.EMPTY_LIST); 185 ResultSet rs = st.executeQuery(pkQueryString); 186 try { 187 if (!rs.next()) { 188 throw new CayenneRuntimeException( 189 "Error in pkFromDatabase() for table " 190 + ent.getName() 191 + " / sequence " 192 + seq_name); 193 } 194 return rs.getInt(1); 195 } finally { 196 rs.close(); 197 } 198 } finally { 199 st.close(); 200 } 201 } finally { 202 con.close(); 203 } 204 } 205 206 207 210 protected List getExistingSequences(DataNode node) throws SQLException { 211 Connection con = node.getDataSource().getConnection(); 212 try { 213 Statement sel = con.createStatement(); 214 try { 215 StringBuffer q = new StringBuffer (); 216 q.append("SELECT SEQNAME FROM SYSCAT.SEQUENCES WHERE SEQNAME") 217 .append(" LIKE '") 218 .append(SEQUENCE_PREFIX) 219 .append("%'"); 220 221 ResultSet rs = sel.executeQuery(q.toString()); 222 try { 223 List sequenceList = new ArrayList (32); 224 while (rs.next()) { 225 sequenceList.add(rs.getString(1)); 226 } 227 return sequenceList; 228 } finally { 229 rs.close(); 230 } 231 } finally { 232 sel.close(); 233 } 234 } finally { 235 con.close(); 236 } 237 } 238 } 239 | Popular Tags |