1 56 package org.objectstyle.cayenne.access.trans; 57 58 import java.util.Iterator ; 59 import java.util.Map ; 60 61 import org.objectstyle.cayenne.CayenneRuntimeException; 62 import org.objectstyle.cayenne.map.DbAttribute; 63 import org.objectstyle.cayenne.map.DbEntity; 64 import org.objectstyle.cayenne.map.DbRelationship; 65 import org.objectstyle.cayenne.query.UpdateQuery; 66 67 73 public class UpdateTranslator extends QueryAssembler { 74 75 public String aliasForTable(DbEntity dbEnt) { 76 throw new RuntimeException ("aliases not supported"); 77 } 78 79 public void dbRelationshipAdded(DbRelationship dbRel) { 80 throw new RuntimeException ("db relationships not supported"); 81 } 82 83 84 public String createSqlString() throws Exception { 85 StringBuffer queryBuf = new StringBuffer (); 86 queryBuf.append("UPDATE "); 87 88 DbEntity dbEnt = getRootEntity().getDbEntity(); 90 queryBuf.append(dbEnt.getFullyQualifiedName()); 91 92 buildSetClause(queryBuf, (UpdateQuery) query); 94 95 String qualifierStr = 97 adapter.getQualifierTranslator(this).doTranslation(); 98 if (qualifierStr != null) 99 queryBuf.append(" WHERE ").append(qualifierStr); 100 101 return queryBuf.toString(); 102 } 103 104 107 private void buildSetClause(StringBuffer queryBuf, UpdateQuery query) { 108 Map updAttrs = query.getUpdAttributes(); 109 Iterator attrIt = updAttrs.keySet().iterator(); 111 112 if (!attrIt.hasNext()) 113 throw new CayenneRuntimeException("Nothing to update."); 114 115 DbEntity dbEnt = getRootEntity().getDbEntity(); 116 queryBuf.append(" SET "); 117 118 boolean appendedSomething = false; 120 121 while (attrIt.hasNext()) { 123 String nextKey = (String ) attrIt.next(); 124 Object attrVal = updAttrs.get(nextKey); 125 126 if (appendedSomething) 127 queryBuf.append(", "); 128 129 queryBuf.append(nextKey).append(" = ?"); 130 super.addToParamList( 131 (DbAttribute) dbEnt.getAttribute(nextKey), 132 attrVal); 133 appendedSomething = true; 134 } 135 } 136 } 137 | Popular Tags |