1 19 20 package org.apache.cayenne.dba.openbase; 21 22 import java.sql.Connection ; 23 import java.sql.ResultSet ; 24 import java.sql.Statement ; 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import org.apache.cayenne.CayenneRuntimeException; 31 import org.apache.cayenne.access.DataNode; 32 import org.apache.cayenne.access.QueryLogger; 33 import org.apache.cayenne.dba.JdbcPkGenerator; 34 import org.apache.cayenne.map.DbAttribute; 35 import org.apache.cayenne.map.DbEntity; 36 import org.apache.cayenne.map.DerivedDbEntity; 37 38 43 public class OpenBasePkGenerator extends JdbcPkGenerator { 44 45 50 public Object generatePkForDbEntity(DataNode node, DbEntity entity) throws Exception { 51 Object binPK = binaryPK(entity); 53 if (binPK != null) { 54 return binPK; 55 } 56 return new Integer (pkFromDatabase(node, entity)); 57 } 58 59 69 protected int pkFromDatabase(DataNode node, DbEntity entity) throws Exception { 70 String sql = newIDString(entity); 71 QueryLogger.logQuery(sql, Collections.EMPTY_LIST); 72 73 Connection con = node.getDataSource().getConnection(); 74 try { 75 Statement st = con.createStatement(); 76 try { 77 78 ResultSet rs = st.executeQuery(sql); 79 try { 80 if (!rs.next()) { 82 throw new CayenneRuntimeException( 83 "Error generating pk for DbEntity " + entity.getName()); 84 } 85 return rs.getInt(1); 86 } 87 finally { 88 rs.close(); 89 } 90 } 91 finally { 92 st.close(); 93 } 94 } 95 finally { 96 con.close(); 97 } 98 } 99 100 106 protected String newIDString(DbEntity ent) { 107 if ((null == ent.getPrimaryKey()) || (1 != ent.getPrimaryKey().size())) { 108 throw new CayenneRuntimeException("Error generating pk for DbEntity " 109 + ent.getName() 110 + ": pk must be single attribute"); 111 } 112 DbAttribute primaryKeyAttribute = (DbAttribute) ent.getPrimaryKey().get(0); 113 114 StringBuffer buf = new StringBuffer ("NEWID FOR "); 115 buf.append(ent.getName()).append(' ').append(primaryKeyAttribute.getName()); 116 return buf.toString(); 117 } 118 119 public void createAutoPk(DataNode node, List dbEntities) throws Exception { 120 123 Iterator it = dbEntities.iterator(); 125 while (it.hasNext()) { 126 DbEntity entity = (DbEntity) it.next(); 127 128 if (!canCreatePK(entity)) { 131 continue; 132 } 133 134 runUpdate(node, createPKString(entity)); 135 runUpdate(node, createUniquePKIndexString(entity)); 136 } 137 } 138 139 142 public List createAutoPkStatements(List dbEntities) { 143 List list = new ArrayList (2 * dbEntities.size()); 144 Iterator it = dbEntities.iterator(); 145 while (it.hasNext()) { 146 DbEntity entity = (DbEntity) it.next(); 147 148 if (!canCreatePK(entity)) { 151 continue; 152 } 153 154 list.add(createPKString(entity)); 155 list.add(createUniquePKIndexString(entity)); 156 } 157 158 return list; 159 } 160 161 protected boolean canCreatePK(DbEntity entity) { 162 if (entity instanceof DerivedDbEntity) { 163 return false; 164 } 165 166 List pk = entity.getPrimaryKey(); 167 if (pk == null || pk.size() == 0) { 168 return false; 169 } 170 return true; 171 } 172 173 176 public void dropAutoPk(DataNode node, List dbEntities) throws Exception { 177 } 183 184 187 public List dropAutoPkStatements(List dbEntities) { 188 return Collections.EMPTY_LIST; 189 } 190 191 194 protected String createPKString(DbEntity entity) { 195 List pk = entity.getPrimaryKey(); 196 197 if (pk == null || pk.size() == 0) { 198 throw new CayenneRuntimeException("Entity '" 199 + entity.getName() 200 + "' has no PK defined."); 201 } 202 203 StringBuffer buffer = new StringBuffer (); 204 buffer.append("CREATE PRIMARY KEY ").append(entity.getName()).append(" ("); 205 206 Iterator it = pk.iterator(); 207 208 DbAttribute firstColumn = (DbAttribute) it.next(); 210 buffer.append(firstColumn.getName()); 211 212 while (it.hasNext()) { 213 DbAttribute column = (DbAttribute) it.next(); 214 buffer.append(", ").append(column.getName()); 215 } 216 217 buffer.append(")"); 218 return buffer.toString(); 219 } 220 221 225 protected String createUniquePKIndexString(DbEntity entity) { 226 List pk = entity.getPrimaryKey(); 227 228 if (pk == null || pk.size() == 0) { 229 throw new CayenneRuntimeException("Entity '" 230 + entity.getName() 231 + "' has no PK defined."); 232 } 233 234 StringBuffer buffer = new StringBuffer (); 235 236 buffer.append(pk.size() == 1 ? "CREATE UNIQUE INDEX " : "CREATE INDEX ").append( 239 entity.getName()).append(" ("); 240 241 Iterator it = pk.iterator(); 242 243 DbAttribute firstColumn = (DbAttribute) it.next(); 245 buffer.append(firstColumn.getName()); 246 247 while (it.hasNext()) { 248 DbAttribute column = (DbAttribute) it.next(); 249 buffer.append(", ").append(column.getName()); 250 } 251 buffer.append(")"); 252 return buffer.toString(); 253 } 254 255 public void reset() { 256 } 258 259 263 public int getPkCacheSize() { 264 return 0; 265 } 266 267 public void setPkCacheSize(int pkCacheSize) { 268 } 270 271 } 272 | Popular Tags |