01: /*
02: * Jython Database Specification API 2.0
03: *
04: * $Id: UpdateCountDataHandler.java 2414 2005-02-23 04:26:23Z bzimmer $
05: *
06: * Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
07: *
08: */
09: package com.ziclix.python.sql.handler;
10:
11: import com.ziclix.python.sql.DataHandler;
12: import com.ziclix.python.sql.FilterDataHandler;
13: import com.ziclix.python.sql.zxJDBC;
14: import org.python.core.Py;
15:
16: import java.sql.SQLException;
17: import java.sql.Statement;
18:
19: /**
20: * A data handler that keeps track of the update count for each execution of a
21: * Statement.
22: * <p/>
23: * <p><b>Note:</b> MySql does not return the correct count for a
24: * <a href="http://www.mysql.com/doc/D/E/DELETE.html">delete</a> statement that has
25: * no <code>where</code> clause. Therefore, to assure the correct update count is returned,
26: * either include a <code>where</code> clause, or understand that the value will always be
27: * <code>0</code>.</p>
28: *
29: * @author brian zimmer
30: * @author last revised by $Author: bzimmer $
31: * @version $Revision: 2414 $
32: * @see java.sql.Statement#getUpdateCount()
33: */
34: public class UpdateCountDataHandler extends FilterDataHandler {
35:
36: private static boolean once = false;
37:
38: /**
39: * The update count for the last executed statement.
40: */
41: public int updateCount;
42:
43: /**
44: * Handle capturing the update count. The initial value of the updateCount is
45: * <code>-1</code>.
46: */
47: public UpdateCountDataHandler(DataHandler datahandler) {
48:
49: super (datahandler);
50:
51: if (!once) {
52: Py.writeError("UpdateCountDataHandler", zxJDBC
53: .getString("updateCountDeprecation"));
54: once = true;
55: }
56:
57: this .updateCount = -1;
58: }
59:
60: /**
61: * Sets the update count to <code>-1</code> prior to the statement being executed.
62: */
63: public void preExecute(Statement stmt) throws SQLException {
64:
65: super .preExecute(stmt);
66:
67: this .updateCount = -1;
68: }
69:
70: /**
71: * Gets the update count from the statement after successfully executing.
72: */
73: public void postExecute(Statement stmt) throws SQLException {
74:
75: super.postExecute(stmt);
76:
77: this.updateCount = stmt.getUpdateCount();
78: }
79: }
|