1   package net.sourceforge.backpedal.p6spy;
2   
3   import com.p6spy.engine.spy.P6Connection;
4   import com.p6spy.engine.spy.P6Factory;
5   import com.p6spy.engine.spy.P6PreparedStatement;
6   import net.sourceforge.backpedal.api.core.BackpedalEngine;
7   import net.sourceforge.backpedal.api.core.BackpedalStatementEssentials;
8   import net.sourceforge.backpedal.api.core.BackpedalStatementStack;
9   import net.sourceforge.backpedal.api.core.StatementParser;
10  import net.sourceforge.backpedal.impl.db.BindVariableComparator;
11  import net.sourceforge.backpedal.impl.db.BindVariableSetImpl;
12  import net.sourceforge.backpedal.impl.db.BooleanBindVariable;
13  import net.sourceforge.backpedal.impl.db.DateBindVariable;
14  import net.sourceforge.backpedal.impl.db.IntegerBindVariable;
15  import net.sourceforge.backpedal.impl.db.NullBindVariable;
16  import net.sourceforge.backpedal.impl.db.StringBindVariable;
17  import net.sourceforge.backpedal.impl.db.TimestampBindVariable;
18  import org.picocontainer.PicoContainer;
19  
20  import java.sql.Connection;
21  import java.sql.Date;
22  import java.sql.PreparedStatement;
23  import java.sql.SQLException;
24  import java.sql.Timestamp;
25  import java.util.Collection;
26  import java.util.TreeSet;
27  
28  public class BackpedalPreparedStatement extends P6PreparedStatement {
29      private final BackpedalEngine backpedalEngine;
30      private final BackpedalStatementStack backpedalStatementQueue;
31      private final StatementParser statementParser;
32      private Collection bindVariables = null;
33  
34      public BackpedalPreparedStatement(final PicoContainer container, P6Factory factory, PreparedStatement real, P6Connection conn, String query) throws SQLException {
35          super(factory, real, conn, query);
36          bindVariables = new TreeSet(new BindVariableComparator());
37          backpedalEngine = (BackpedalEngine) container.getComponent(BackpedalEngine.class);
38          backpedalStatementQueue = (BackpedalStatementStack) container.getComponent(BackpedalStatementStack.class);
39          statementParser = (StatementParser) container.getComponent(StatementParser.class);
40      }
41  
42      public void setString(int p0, String p1) throws SQLException {
43          bindVariables.add(new StringBindVariable(p0, p1,null));
44          super.setString(p0, p1);
45      }
46  
47      public void setInt(int p0, int p1) throws SQLException {
48          bindVariables.add(new IntegerBindVariable(p0, p1,null));
49          super.setInt(p0, p1);
50      }
51  
52      public void setBoolean(int p0, boolean p1) throws SQLException {
53          bindVariables.add(new BooleanBindVariable(p0, p1,null));
54          super.setBoolean(p0, p1);
55      }
56  
57      public void setDate(int p0, Date p1) throws SQLException {
58          bindVariables.add(new DateBindVariable(p0, p1,null));
59          super.setDate(p0, p1);
60      }
61  
62      public void setTimestamp(int p0, Timestamp p1) throws SQLException {
63          bindVariables.add(new TimestampBindVariable(p0, p1,null));
64          super.setTimestamp(p0, p1);
65      }
66  
67      public boolean execute() throws SQLException {
68          recordStatement();
69          return super.execute();
70      }
71  
72      private void recordStatement() throws SQLException {
73          final BindVariableSetImpl bindVariableSet = new BindVariableSetImpl(bindVariables);
74          final Connection realConnection = connection.getJDBC();
75          BackpedalStatementEssentials backpedalStatementEssentials = null;
76          if (preparedQuery.startsWith(BackpedalStatementEssentials.INSERT)) {
77              backpedalStatementEssentials = backpedalEngine.backpedalPreparedStatementInsert(realConnection, statementParser.parseInsertStatement(preparedQuery), bindVariableSet);
78          } else if (preparedQuery.startsWith(BackpedalStatementEssentials.UPDATE)) {
79              backpedalStatementEssentials = backpedalEngine.backpedalPreparedStatementUpdate(realConnection, statementParser.parseUpdateStatement(preparedQuery), bindVariableSet);
80          } else if (preparedQuery.startsWith(BackpedalStatementEssentials.DELETE)) {
81              backpedalStatementEssentials = backpedalEngine.backpedalPreparedStatementDelete(realConnection, statementParser.parseDeleteStatement(preparedQuery), bindVariableSet);
82          }
83          if (backpedalStatementEssentials != null) {
84              backpedalStatementQueue.add(backpedalStatementEssentials);
85          }
86      }
87  
88      public int executeUpdate() throws SQLException {
89          recordStatement();
90          return super.executeUpdate();
91      }
92  
93      public void clearParameters() throws SQLException {
94          bindVariables.clear();
95          super.clearParameters();
96      }
97  
98      protected void initValues() {
99          super.initValues();
100     }
101 
102     public void setNull(int p0, int p1, String p2) throws SQLException {
103         super.setNull(p0, p1, p2);
104     }
105 
106     public void setNull(int p0, int p1) throws SQLException {
107         bindVariables.add(new NullBindVariable(p0, p1,null));
108         super.setNull(p0, p1);
109     }
110 }
This page was automatically generated by Maven