1 19 20 package org.apache.cayenne.access.trans; 21 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import org.apache.cayenne.CayenneRuntimeException; 26 import org.apache.cayenne.map.DbAttribute; 27 import org.apache.cayenne.map.DbEntity; 28 import org.apache.cayenne.map.DbRelationship; 29 import org.apache.cayenne.query.UpdateQuery; 30 31 37 public class UpdateTranslator extends QueryAssembler { 38 39 public String aliasForTable(DbEntity dbEnt) { 40 throw new RuntimeException ("aliases not supported"); 41 } 42 43 public void dbRelationshipAdded(DbRelationship dbRel) { 44 throw new RuntimeException ("db relationships not supported"); 45 } 46 47 48 public String createSqlString() throws Exception { 49 StringBuffer queryBuf = new StringBuffer (); 50 queryBuf.append("UPDATE "); 51 52 DbEntity dbEnt = getRootEntity().getDbEntity(); 54 queryBuf.append(dbEnt.getFullyQualifiedName()); 55 56 buildSetClause(queryBuf, (UpdateQuery) query); 58 59 String qualifierStr = 61 adapter.getQualifierTranslator(this).doTranslation(); 62 if (qualifierStr != null) 63 queryBuf.append(" WHERE ").append(qualifierStr); 64 65 return queryBuf.toString(); 66 } 67 68 71 private void buildSetClause(StringBuffer queryBuf, UpdateQuery query) { 72 Map updAttrs = query.getUpdAttributes(); 73 Iterator attrIt = updAttrs.entrySet().iterator(); 75 76 if (!attrIt.hasNext()) 77 throw new CayenneRuntimeException("Nothing to update."); 78 79 DbEntity dbEnt = getRootEntity().getDbEntity(); 80 queryBuf.append(" SET "); 81 82 boolean appendedSomething = false; 84 85 while (attrIt.hasNext()) { 87 Map.Entry entry = (Map.Entry ) attrIt.next(); 88 String nextKey = (String ) entry.getKey(); 89 Object attrVal = entry.getValue(); 90 91 if (appendedSomething) 92 queryBuf.append(", "); 93 94 queryBuf.append(nextKey).append(" = ?"); 95 super.addToParamList( 96 (DbAttribute) dbEnt.getAttribute(nextKey), 97 attrVal); 98 appendedSomething = true; 99 } 100 } 101 } 102 | Popular Tags |