1 56 package org.objectstyle.cayenne.dba.openbase; 57 58 import java.sql.Connection ; 59 import java.sql.ResultSet ; 60 import java.sql.Statement ; 61 import java.util.ArrayList ; 62 import java.util.Collections ; 63 import java.util.Iterator ; 64 import java.util.List ; 65 66 import org.objectstyle.cayenne.CayenneRuntimeException; 67 import org.objectstyle.cayenne.access.DataNode; 68 import org.objectstyle.cayenne.access.QueryLogger; 69 import org.objectstyle.cayenne.dba.JdbcPkGenerator; 70 import org.objectstyle.cayenne.map.DbAttribute; 71 import org.objectstyle.cayenne.map.DbEntity; 72 import org.objectstyle.cayenne.map.DerivedDbEntity; 73 74 80 public class OpenBasePkGenerator extends JdbcPkGenerator { 81 82 85 public String generatePkForDbEntityString(DbEntity ent) { 86 return newIDString(ent); 87 } 88 89 94 public Object generatePkForDbEntity(DataNode node, DbEntity entity) 95 throws Exception { 96 Object binPK = binaryPK(entity); 98 if (binPK != null) { 99 return binPK; 100 } 101 return new Integer (pkFromDatabase(node, entity)); 102 } 103 104 112 protected int pkFromDatabase(DataNode node, DbEntity entity) throws Exception { 113 String sql = newIDString(entity); 114 QueryLogger.logQuery(QueryLogger.DEFAULT_LOG_LEVEL, sql, Collections.EMPTY_LIST); 115 116 Connection con = node.getDataSource().getConnection(); 117 try { 118 Statement st = con.createStatement(); 119 try { 120 121 ResultSet rs = st.executeQuery(sql); 122 try { 123 if (!rs.next()) { 125 throw new CayenneRuntimeException( 126 "Error generating pk for DbEntity " + entity.getName()); 127 } 128 return rs.getInt(1); 129 } 130 finally { 131 rs.close(); 132 } 133 } 134 finally { 135 st.close(); 136 } 137 } 138 finally { 139 con.close(); 140 } 141 } 142 143 150 protected String newIDString(DbEntity ent) { 151 if ((null == ent.getPrimaryKey()) || (1 != ent.getPrimaryKey().size())) { 152 throw new CayenneRuntimeException( 153 "Error generating pk for DbEntity " 154 + ent.getName() 155 + ": pk must be single attribute"); 156 } 157 DbAttribute primaryKeyAttribute = (DbAttribute) ent.getPrimaryKey().get(0); 158 159 StringBuffer buf = new StringBuffer ("NEWID FOR "); 160 buf.append(ent.getName()).append(' ').append(primaryKeyAttribute.getName()); 161 return buf.toString(); 162 } 163 164 public void createAutoPk(DataNode node, List dbEntities) throws Exception { 165 168 Iterator it = dbEntities.iterator(); 170 while (it.hasNext()) { 171 DbEntity entity = (DbEntity) it.next(); 172 173 if (!canCreatePK(entity)) { 176 continue; 177 } 178 179 runUpdate(node, createPKString(entity)); 180 runUpdate(node, createUniquePKIndexString(entity)); 181 } 182 } 183 184 187 public List createAutoPkStatements(List dbEntities) { 188 List list = new ArrayList (2 * dbEntities.size()); 189 Iterator it = dbEntities.iterator(); 190 while (it.hasNext()) { 191 DbEntity entity = (DbEntity) it.next(); 192 193 if (!canCreatePK(entity)) { 196 continue; 197 } 198 199 list.add(createPKString(entity)); 200 list.add(createUniquePKIndexString(entity)); 201 } 202 203 return list; 204 } 205 206 protected boolean canCreatePK(DbEntity entity) { 207 if (entity instanceof DerivedDbEntity) { 208 return false; 209 } 210 211 List pk = entity.getPrimaryKey(); 212 if (pk == null || pk.size() == 0) { 213 return false; 214 } 215 return true; 216 } 217 218 221 public void dropAutoPk(DataNode node, List dbEntities) throws Exception { 222 } 228 229 232 public List dropAutoPkStatements(List dbEntities) { 233 return Collections.EMPTY_LIST; 234 } 235 236 239 protected String createPKString(DbEntity entity) { 240 List pk = entity.getPrimaryKey(); 241 242 if (pk == null || pk.size() == 0) { 243 throw new CayenneRuntimeException( 244 "Entity '" + entity.getName() + "' has no PK defined."); 245 } 246 247 StringBuffer buffer = new StringBuffer (); 248 buffer.append("CREATE PRIMARY KEY ").append(entity.getName()).append(" ("); 249 250 Iterator it = pk.iterator(); 251 252 DbAttribute firstColumn = (DbAttribute) it.next(); 254 buffer.append(firstColumn.getName()); 255 256 while (it.hasNext()) { 257 DbAttribute column = (DbAttribute) it.next(); 258 buffer.append(", ").append(column.getName()); 259 } 260 261 buffer.append(")"); 262 return buffer.toString(); 263 } 264 265 269 protected String createUniquePKIndexString(DbEntity entity) { 270 List pk = entity.getPrimaryKey(); 271 272 if (pk == null || pk.size() == 0) { 273 throw new CayenneRuntimeException( 274 "Entity '" + entity.getName() + "' has no PK defined."); 275 } 276 277 StringBuffer buffer = new StringBuffer (); 278 279 buffer 282 .append(pk.size() == 1 ? "CREATE UNIQUE INDEX " : "CREATE INDEX ") 283 .append(entity.getName()) 284 .append(" ("); 285 286 Iterator it = pk.iterator(); 287 288 DbAttribute firstColumn = (DbAttribute) it.next(); 290 buffer.append(firstColumn.getName()); 291 292 while (it.hasNext()) { 293 DbAttribute column = (DbAttribute) it.next(); 294 buffer.append(", ").append(column.getName()); 295 } 296 buffer.append(")"); 297 return buffer.toString(); 298 } 299 300 public void reset() { 301 } 303 304 308 public int getPkCacheSize() { 309 return 0; 310 } 311 312 public void setPkCacheSize(int pkCacheSize) { 313 } 315 316 } | Popular Tags |