1 19 20 package org.apache.cayenne.access.jdbc; 21 22 import java.sql.Connection ; 23 import java.sql.PreparedStatement ; 24 import java.sql.SQLException ; 25 26 import org.apache.cayenne.CayenneRuntimeException; 27 import org.apache.cayenne.access.OperationObserver; 28 import org.apache.cayenne.access.QueryLogger; 29 import org.apache.cayenne.access.QueryTranslator; 30 import org.apache.cayenne.access.trans.DeleteTranslator; 31 import org.apache.cayenne.access.trans.UpdateTranslator; 32 import org.apache.cayenne.dba.DbAdapter; 33 import org.apache.cayenne.map.EntityResolver; 34 import org.apache.cayenne.query.DeleteQuery; 35 import org.apache.cayenne.query.Query; 36 import org.apache.cayenne.query.UpdateQuery; 37 38 42 public class UpdateAction extends BaseSQLAction { 43 44 protected Query query; 45 46 public UpdateAction(Query query, DbAdapter adapter, EntityResolver entityResolver) { 47 super(adapter, entityResolver); 48 this.query = query; 49 } 50 51 protected QueryTranslator createTranslator(Connection connection) { 52 QueryTranslator translator; 53 54 if (query instanceof UpdateQuery) { 55 translator = new UpdateTranslator(); 56 } 57 else if (query instanceof DeleteQuery) { 58 translator = new DeleteTranslator(); 59 } 60 else { 61 throw new CayenneRuntimeException("Can't make a translator for query " 62 + query); 63 } 64 65 translator.setAdapter(getAdapter()); 66 translator.setQuery(query); 67 translator.setEntityResolver(getEntityResolver()); 68 translator.setConnection(connection); 69 70 return translator; 71 } 72 73 public void performAction(Connection connection, OperationObserver observer) 74 throws SQLException , Exception { 75 76 QueryTranslator translator = createTranslator(connection); 77 78 PreparedStatement statement = translator.createStatement(); 79 80 try { 81 int count = statement.executeUpdate(); 83 QueryLogger.logUpdateCount(count); 84 85 observer.nextCount(query, count); 87 } 88 finally { 89 statement.close(); 90 } 91 } 92 } 93 | Popular Tags |