1 30 package com.genimen.djeneric.repository.rdbms; 31 32 import java.sql.ResultSet ; 33 import java.sql.SQLException ; 34 35 import com.genimen.djeneric.repository.DjExtent; 36 import com.genimen.djeneric.repository.DjObject; 37 import com.genimen.djeneric.repository.DjSession; 38 import com.genimen.djeneric.repository.exceptions.DjenericException; 39 import com.genimen.djeneric.util.DjLogger; 40 41 51 52 public class RdbmsExtent extends DjExtent 53 { 54 public RdbmsExtent(String objectType, String name, String alias, String internalCode, String title, 55 String nameSingular, String namePlural) 56 { 57 super(objectType, name, alias, internalCode, title, nameSingular, namePlural); 58 } 59 60 public boolean isEmpty(DjSession session) throws DjenericException 61 { 62 return isEmpty(this, session); 63 } 64 65 protected String getTableName() 66 { 67 return getName(); 68 } 69 70 protected String createWhereClause() 71 { 72 return createWhereClause(""); 73 } 74 75 85 protected String createWhereClause(String addThis) 86 { 87 if (multipleTypesInExtent()) 88 { 89 String result = " where " + RdbmsPersistenceManager.OBJECT_TYPE_COLUMN + " = '" + getAlias() + "' "; 90 if (addThis != null && addThis.trim().length() != 0) result += " and " + addThis; 91 return result; 92 } 93 else 94 { 95 if (addThis == null || addThis.trim().length() == 0) return ""; 96 return " where " + addThis; 97 } 98 99 } 100 101 108 109 public boolean multipleTypesInExtent() 110 { 111 return false; 112 } 113 114 126 private boolean isEmpty(DjExtent extent, DjSession session) throws DjenericException 127 { 128 boolean result = true; 129 RdbmsSession rses = (RdbmsSession) session; 130 131 String s1 = "select 1 from " + RdbmsPersistenceManager.MODEL_TABLE + " where exists("; 132 133 String s2 = "select 1 from " + ((RdbmsExtent) extent).getTableName() + createWhereClause(); 134 135 RdbmsPersistenceManager mgr = (RdbmsPersistenceManager) ((RdbmsSession) session).getPersistenceManager(); 136 String translated = mgr.external2internalStatement(s2); 137 String combined = s1 + translated + ")"; 138 139 SqlStatement stmt = rses.getInternalSqlStatement(combined); 140 141 try 142 { 143 ResultSet rs = stmt.executeQuery(); 144 if (rs.next()) 145 { 146 result = false; 147 } 149 rs.close(); 150 stmt.close(); 151 152 if (!result) return false; 154 155 DjExtent[] specializations = extent.getSpecializations(); 156 for (int i = 0; i < specializations.length; i++) 157 { 158 if (!isEmpty(specializations[i], session)) return false; 160 } 161 return true; 162 } 163 catch (SQLException x) 164 { 165 throw new DjenericException(x); 166 } 167 168 } 169 170 191 protected SqlStatement getUpdateStatement(RdbmsSession session, String [] propertyNames, Object [] propertyValues) 192 throws DjenericException 193 { 194 String keyProperty = getIdProperty().getName(); 195 String s = "update " + getTableName() + " set "; 196 boolean first = true; 197 for (int i = 0; i < getPropertyCount(); i++) 198 { 199 String name = getPropertyName(i); 200 if (!name.equalsIgnoreCase(keyProperty)) 201 { 202 if (!first) s += ","; 203 first = false; 204 s += name + " = :" + name + "\n"; 205 } 206 } 207 208 String wcl = ""; 209 first = true; 210 for (int i = 0; i < propertyNames.length; i++) 211 { 212 if (getProperty(propertyNames[i]).isMappedToLong()) continue; 214 if (!first) wcl += "and "; 215 first = false; 216 if (propertyValues[i] != null) 217 { 218 wcl += propertyNames[i] + " = :org_" + propertyNames[i] + "\n"; 219 } 220 else 221 { 222 wcl += propertyNames[i] + " is null\n"; 223 } 224 } 225 226 s += createWhereClause(wcl); 227 228 SqlStatement result = session.getSqlStatement(s); 229 return result; 230 } 231 232 private String _insertStmt = null; 233 234 243 protected SqlStatement getInsertStatement(RdbmsSession session) throws DjenericException 244 { 245 if (_insertStmt == null) 246 { 247 String s = "insert into " + getTableName() + "("; 248 boolean first = true; 249 250 if (multipleTypesInExtent()) 251 { 252 s += RdbmsPersistenceManager.OBJECT_TYPE_COLUMN; 253 first = false; 254 } 255 256 for (int i = 0; i < getPropertyCount(); i++) 257 { 258 String name = getPropertyName(i); 259 if (!first) s += ","; 260 first = false; 261 s += name; 262 } 263 s += ")\nvalues("; 264 first = true; 265 if (multipleTypesInExtent()) 266 { 267 s += "'" + getAlias() + "'"; 268 first = false; 269 } 270 271 for (int i = 0; i < getPropertyCount(); i++) 272 { 273 String name = getPropertyName(i); 274 if (!first) s += ","; 275 first = false; 276 s += ":" + name; 277 } 278 s += ")"; 279 280 SqlStatement result = session.getSqlStatement(s); 281 _insertStmt = result.getInternalSql(); 282 return result; 283 } 284 else return session.getInternalSqlStatement(_insertStmt); 285 } 286 287 308 protected SqlStatement getDeleteStatement(RdbmsSession session, String [] propertyNames, Object [] propertyValues) 309 throws DjenericException 310 { 311 String s = "delete from " + getTableName(); 312 String wcl = ""; 313 314 boolean first = true; 315 for (int i = 0; i < propertyNames.length; i++) 316 { 317 if (getProperty(propertyNames[i]).isMappedToLong()) continue; 319 if (!first) wcl += "and "; 320 first = false; 321 if (propertyValues[i] != null) 322 { 323 wcl += propertyNames[i] + " = :org_" + propertyNames[i] + "\n"; 324 } 325 else 326 { 327 wcl += propertyNames[i] + " is null\n"; 328 } 329 } 330 s += createWhereClause(wcl); 331 332 SqlStatement result = session.getSqlStatement(s); 333 return result; 334 } 335 336 private String _selectStmt = null; 337 338 347 protected SqlStatement getSelectStatement(RdbmsSession session) throws DjenericException 348 { 349 if (_selectStmt == null) 350 { 351 String keyProperty = getIdProperty().getName(); 352 String s = "select * from " + getTableName() + createWhereClause(keyProperty + " = :" + keyProperty); 353 SqlStatement result = session.getSqlStatement(s); 354 _selectStmt = result.getInternalSql(); 355 return result; 356 } 357 else return session.getInternalSqlStatement(_selectStmt); 358 } 359 360 365 public Object clone() 366 { 367 RdbmsExtent nt = new RdbmsExtent(getObjectType(), getName(), getAlias(), getInternalCode(), getTitle(), 368 getNameSingular(), getNamePlural()); 369 nt.setInternalId(getInternalId()); 370 371 try 372 { 373 copyPropertiesInto(nt); 374 } 375 catch (DjenericException x) 376 { 377 DjLogger.log(x); 378 throw new RuntimeException ("Internal error: RdbmsExtent.clone() failed"); 379 } 380 381 return nt; 382 } 383 384 public Class getNativeTypeClass() 385 { 386 return Long .class; 387 } 388 389 } | Popular Tags |