1 56 package org.objectstyle.cayenne.access.jdbc; 57 58 import java.sql.Connection ; 59 import java.sql.PreparedStatement ; 60 import java.sql.SQLException ; 61 62 import org.objectstyle.cayenne.CayenneRuntimeException; 63 import org.objectstyle.cayenne.access.OperationObserver; 64 import org.objectstyle.cayenne.access.QueryLogger; 65 import org.objectstyle.cayenne.access.QueryTranslator; 66 import org.objectstyle.cayenne.access.trans.DeleteTranslator; 67 import org.objectstyle.cayenne.access.trans.UpdateTranslator; 68 import org.objectstyle.cayenne.dba.DbAdapter; 69 import org.objectstyle.cayenne.map.EntityResolver; 70 import org.objectstyle.cayenne.query.DeleteQuery; 71 import org.objectstyle.cayenne.query.Query; 72 import org.objectstyle.cayenne.query.UpdateQuery; 73 74 78 public class UpdateAction extends BaseSQLAction { 79 80 protected Query query; 81 82 public UpdateAction(Query query, DbAdapter adapter, EntityResolver entityResolver) { 83 super(adapter, entityResolver); 84 this.query = query; 85 } 86 87 protected QueryTranslator createTranslator(Connection connection) { 88 QueryTranslator translator = checkDeprecatedQueries(query); 89 90 if (translator == null) { 91 if (query instanceof UpdateQuery) { 92 translator = new UpdateTranslator(); 93 } 94 else if (query instanceof DeleteQuery) { 95 translator = new DeleteTranslator(); 96 } 97 else { 98 throw new CayenneRuntimeException("Can't make a translator for query " 99 + query); 100 } 101 } 102 103 translator.setAdapter(getAdapter()); 104 translator.setQuery(query); 105 translator.setEntityResolver(getEntityResolver()); 106 translator.setConnection(connection); 107 108 return translator; 109 } 110 111 114 final QueryTranslator checkDeprecatedQueries(Query query) { 115 if (query instanceof org.objectstyle.cayenne.query.InsertQuery) { 116 return new org.objectstyle.cayenne.access.trans.InsertTranslator(); 117 } 118 119 return null; 120 } 121 122 public void performAction(Connection connection, OperationObserver observer) 123 throws SQLException , Exception { 124 125 QueryTranslator translator = createTranslator(connection); 126 127 PreparedStatement statement = translator.createStatement(query.getLoggingLevel()); 128 129 try { 130 int count = statement.executeUpdate(); 132 QueryLogger.logUpdateCount(query.getLoggingLevel(), count); 133 134 observer.nextCount(query, count); 136 } 137 finally { 138 statement.close(); 139 } 140 } 141 } | Popular Tags |