扩展PreparedStatement,以便输出执行的sql语句,即sql日志

 

 

下面把参考文章附上:

 

主题:装饰器模式(decorator)---p6spy

 

今天为了在打印sql日志时,能够打印出完整的sql日志(不带‘?’),测试了下 p6spy ,可以满足需求,查看了下里面的大致的源码,主要采用了装饰器模式进行扩展的。在此做下笔记。

 

1,p6spy的配置在网络上google下。

2,Class.forName("com.p6spy.engine.spy.P6SpyDriver");进行触发该类下静态模块的执行,

---》这个主要实现了该类下的initMethod  P6SpyDriver extends P6SpyDriverCore

 

 

Java代码 复制代码
  1. public abstract class P6SpyDriverCore implements Driver {   
  2.     //由实现的接口,以及该声明,就可知其为decorator   
  3.     protected Driver passthru = null;   
  4.   
  5.   public synchronized static void initMethod(String spydriver) {   
  6.    //主要实现为两步,1,对DrvierManager的扩展,2,connect的获取   
  7.   
  8.    //这里只粘出重点的代码   
  9.   
  10.     if (hasModules) {   
  11.         spy = new P6SpyDriver();   
  12.         //注册进DriverManager,你如果在写连接时不用Class.forName()的话,也可以用这种设置   
  13.         DriverManager.registerDriver(spy);   
  14.     }   
  15.   
  16.    //在配置文件里,读取真的driver   
  17.     Driver realDriver = (Driver)P6Util.forName(className).newInstance();   
  18.     if (P6SpyOptions.getDeregisterDrivers()) {   
  19.         // just in case you had to deregister   
  20.         DriverManager.registerDriver(realDriver);   
  21.     }   
  22.              
  23.               // now wrap your realDriver in the spy   
  24.          if (hasModules) {   
  25.             //设置到以上的参数中   
  26.             spy.setPassthru(realDriver);   
  27.              realDrivers.add(realDriver);   
  28.          }   
  29.   }   
  30.   
  31.   
  32.    public Connection connect(String p0, java.util.Properties p1) throws SQLException {         
  33.            
  34.         Connection conn = passthru.connect(realUrl,p1);   
  35.            
  36.         if (conn != null) {   
  37.             //这里是对connection的包装,也是一个decorator //P6LogConnection implements Connection    
  38.            //供DriverManager.getConnection()获取,   
  39.             conn = wrapConnection(conn);   
  40.         }   
  41.         return conn;   
  42.     }   
  43.   
  44.   
  45. }  
public abstract class P6SpyDriverCore implements Driver {
    //由实现的接口,以及该声明,就可知其为decorator
    protected Driver passthru = null;

  public synchronized static void initMethod(String spydriver) {
   //主要实现为两步,1,对DrvierManager的扩展,2,connect的获取

   //这里只粘出重点的代码

    if (hasModules) {
        spy = new P6SpyDriver();
        //注册进DriverManager,你如果在写连接时不用Class.forName()的话,也可以用这种设置
        DriverManager.registerDriver(spy);
    }

   //在配置文件里,读取真的driver
    Driver realDriver = (Driver)P6Util.forName(className).newInstance();
	if (P6SpyOptions.getDeregisterDrivers()) {
	    // just in case you had to deregister
	    DriverManager.registerDriver(realDriver);
	}
		  
              // now wrap your realDriver in the spy
         if (hasModules) {
            //设置到以上的参数中
            spy.setPassthru(realDriver);
             realDrivers.add(realDriver);
         }
  }


   public Connection connect(String p0, java.util.Properties p1) throws SQLException {      
        
        Connection conn = passthru.connect(realUrl,p1);
        
        if (conn != null) {
            //这里是对connection的包装,也是一个decorator //P6LogConnection implements Connection 
           //供DriverManager.getConnection()获取,
            conn = wrapConnection(conn);
        }
        return conn;
    }


}

 3,当DriverManager.getConnection(...)时,就会去调用以上的connect而这个connect也是经过包装的。。

 

 

Java代码 复制代码
  1. //现在到了P6Connection.prepareStatement(sql);   
  2. public class P6Connection extends P6Base implements java.sql.Connection {   
  3.  protected Connection passthru;   
  4.   
  5. //P6PreparedStatement 返回的也是对PreparedStatement的装饰扩展   
  6.  public PreparedStatement prepareStatement(String p0) throws SQLException {   
  7.         return (getP6Factory().getPreparedStatement(passthru.prepareStatement(p0), this, p0));   
  8.     }   
  9.   
  10. }  
//现在到了P6Connection.prepareStatement(sql);
public class P6Connection extends P6Base implements java.sql.Connection {
 protected Connection passthru;

//P6PreparedStatement 返回的也是对PreparedStatement的装饰扩展
 public PreparedStatement prepareStatement(String p0) throws SQLException {
        return (getP6Factory().getPreparedStatement(passthru.prepareStatement(p0), this, p0));
    }

}

 

4,这一步到了处理PreparedStatement,以上的扩展为的就是为了驱动时执行我们所装饰的P6PreparedStatement

 

Java代码 复制代码
  1. public class P6PreparedStatement extends P6Statement implements PreparedStatement {   
  2.        
  3.        
  4.     public final static int P6_MAX_FIELDS = 32;   
  5.     public static int P6_GROW_MAX = 32;   
  6.     protected PreparedStatement prepStmtPassthru;   
  7.     protected String preparedQuery;   
  8.     protected Object values[];//扩展就是为了取我们所设置的值   
  9.     protected boolean isString[];   
  10.        
  11.     public P6PreparedStatement(P6Factory factory, PreparedStatement statement, P6Connection conn, String query) {   
  12.         super(factory, statement, conn);   
  13.         prepStmtPassthru = statement;   
  14.         this.preparedQuery = query;   
  15.         initValues();   
  16.     }   
  17.        
  18.     protected void initValues() {   
  19.         values = new Object[P6_MAX_FIELDS+1];   
  20.         isString = new boolean[P6_MAX_FIELDS+1];   
  21.     }   
  22.        
  23.     public void addBatch() throws SQLException {   
  24.         prepStmtPassthru.addBatch();   
  25.     }   
  26.        
  27.     public void clearParameters() throws SQLException {   
  28.         prepStmtPassthru.clearParameters();   
  29.     }   
  30.        
  31.     public boolean execute() throws SQLException {   
  32.         return prepStmtPassthru.execute();   
  33.     }   
  34.        
  35.     public ResultSet executeQuery() throws SQLException {   
  36.         ResultSet resultSet = prepStmtPassthru.executeQuery();   
  37.         return (getP6Factory().getResultSet(resultSet, this, preparedQuery, getQueryFromPreparedStatement()));   
  38.     }   
  39.        
  40.     public int executeUpdate() throws SQLException {   
  41.         return prepStmtPassthru.executeUpdate();   
  42.     }   
  43.        
  44.     public ResultSetMetaData getMetaData() throws SQLException {   
  45.         return prepStmtPassthru.getMetaData();   
  46.     }   
  47.        
  48.     public void setArray(int p0, Array p1) throws SQLException {   
  49.         setObjectAsString(p0, p1);   
  50.         // we need to make sure we get the real object in this case   
  51.         if (p1 instanceof P6Array) {   
  52.             prepStmtPassthru.setArray(p0,((P6Array)p1).passthru);   
  53.         } else{   
  54.             prepStmtPassthru.setArray(p0,p1);   
  55.         }   
  56.     }   
  57.     //以下设置都为装饰器的扩展,使我们每一次设置到?的值都会加到数组中去。   
  58.     public void setAsciiStream(int p0, InputStream p1, int p2) throws SQLException {   
  59.         setObjectAsString(p0, p1);   
  60.         prepStmtPassthru.setAsciiStream(p0,p1,p2);   
  61.     }   
  62.        
  63.     public void setBigDecimal(int p0, BigDecimal p1) throws SQLException {   
  64.         setObjectAsString(p0, p1);   
  65.         prepStmtPassthru.setBigDecimal(p0,p1);   
  66.     }   
  67.   
  68.     public void setBinaryStream(int p0, InputStream p1, int p2) throws SQLException {   
  69.         setObjectAsString(p0, p1);   
  70.         prepStmtPassthru.setBinaryStream(p0,p1,p2);   
  71.     }   
  72.        
  73.     public void setBlob(int p0, Blob p1) throws SQLException {   
  74.         setObjectAsString(p0, p1);   
  75.         prepStmtPassthru.setBlob(p0,p1);   
  76.     }   
  77.        
  78.     public void setBoolean(int p0, boolean p1) throws SQLException {   
  79.         setObjectAsString(p0, new Boolean(p1));   
  80.         prepStmtPassthru.setBoolean(p0,p1);   
  81.     }   
  82.        
  83.     public void setByte(int p0, byte p1) throws SQLException {   
  84.         setObjectAsString(p0, new Byte(p1));   
  85.         prepStmtPassthru.setByte(p0,p1);   
  86.     }   
  87.        
  88.     public void setBytes(int p0, byte[] p1) throws SQLException {   
  89.         setObjectAsString(p0, p1);   
  90.         prepStmtPassthru.setBytes(p0,p1);   
  91.     }   
  92.        
  93.     public void setCharacterStream(int p0, Reader p1, int p2) throws SQLException {   
  94.         setObjectAsString(p0, p1);   
  95.         prepStmtPassthru.setCharacterStream(p0,p1,p2);   
  96.     }   
  97.        
  98.     public void setClob(int p0, Clob p1) throws SQLException {   
  99.         setObjectAsString(p0, p1);   
  100.         prepStmtPassthru.setClob(p0,p1);   
  101.     }   
  102.        
  103.     public void setDate(int p0, Date p1) throws SQLException {   
  104.         setObjectAsString(p0, p1);   
  105.         prepStmtPassthru.setDate(p0,p1);   
  106.     }    
  107.        
  108.     public void setDate(int p0, Date p1, java.util.Calendar p2) throws SQLException {   
  109.         setObjectAsString(p0, p1);   
  110.         prepStmtPassthru.setDate(p0,p1,p2);   
  111.     }   
  112.        
  113.     public void setDouble(int p0, double p1) throws SQLException {   
  114.         setObjectAsInt(p0, new Double(p1));   
  115.         prepStmtPassthru.setDouble(p0,p1);   
  116.     }   
  117.        
  118.     public void setFloat(int p0, float p1) throws SQLException {   
  119.         setObjectAsInt(p0, new Float(p1));   
  120.         prepStmtPassthru.setFloat(p0,p1);   
  121.     }   
  122.        
  123.     public void setInt(int p0, int p1) throws SQLException {   
  124.         setObjectAsInt(p0, new Integer(p1));   
  125.         prepStmtPassthru.setInt(p0,p1);   
  126.     }   
  127.        
  128.     public void setLong(int p0, long p1) throws SQLException {   
  129.         setObjectAsInt(p0, new Long(p1));   
  130.         prepStmtPassthru.setLong(p0,p1);   
  131.     }   
  132.        
  133.     public void setNull(int p0, int p1, String p2) throws SQLException {   
  134.         setObjectAsString(p0, null);   
  135.         prepStmtPassthru.setNull(p0,p1,p2);   
  136.     }   
  137.        
  138.     public void setNull(int p0, int p1) throws SQLException {   
  139.         setObjectAsString(p0, null);   
  140.         prepStmtPassthru.setNull(p0,p1);   
  141.     }   
  142.        
  143.     public void setObject(int p0, Object p1, int p2, int p3) throws SQLException {   
  144.         setObjectAsString(p0, p1);   
  145.         prepStmtPassthru.setObject(p0,p1,p2,p3);   
  146.     }   
  147.        
  148.     public void setObject(int p0, Object p1, int p2) throws SQLException {   
  149.         setObjectAsString(p0, p1);   
  150.         prepStmtPassthru.setObject(p0,p1,p2);   
  151.     }   
  152.        
  153.     public void setObject(int p0, Object p1) throws SQLException {   
  154.         setObjectAsString(p0, p1);   
  155.         prepStmtPassthru.setObject(p0,p1);   
  156.     }   
  157.        
  158.     public void setRef(int p0, Ref p1) throws SQLException {   
  159.         setObjectAsString(p0, p1);   
  160.         prepStmtPassthru.setRef(p0,p1);   
  161.     }   
  162.        
  163.     public void setShort(int p0, short p1) throws SQLException {   
  164.         setObjectAsString(p0, new Short(p1));   
  165.         prepStmtPassthru.setShort(p0,p1);   
  166.     }   
  167.        
  168.     public void setString(int p0, String p1) throws SQLException {   
  169.         setObjectAsString(p0, p1);   
  170.         prepStmtPassthru.setString(p0,p1);   
  171.     }   
  172.        
  173.     public void setTime(int p0, Time p1, java.util.Calendar p2) throws SQLException {   
  174.         setObjectAsString(p0, p1);   
  175.         prepStmtPassthru.setTime(p0,p1,p2);   
  176.     }   
  177.        
  178.     public void setTime(int p0, Time p1) throws SQLException {   
  179.         setObjectAsString(p0, p1);   
  180.         prepStmtPassthru.setTime(p0,p1);   
  181.     }   
  182.        
  183.     public void setTimestamp(int p0, Timestamp p1, java.util.Calendar p2) throws SQLException {   
  184.         setObjectAsString(p0, p1);   
  185.         prepStmtPassthru.setTimestamp(p0,p1,p2);   
  186.     }   
  187.        
  188.     public void setTimestamp(int p0, Timestamp p1) throws SQLException {   
  189.         setObjectAsString(p0, p1);   
  190.         prepStmtPassthru.setTimestamp(p0,p1);   
  191.     }   
  192.        
  193.     public void setUnicodeStream(int p0, InputStream p1, int p2) throws SQLException {   
  194.         setObjectAsString(p0, p1);   
  195.         prepStmtPassthru.setUnicodeStream(p0,p1,p2);   
  196.     }   
  197.        
  198.     /* we override this because the p6statement version will not be   
  199.      * able to return the accurate prepared statement or query information  
  200.      */  
  201.     // bug 161: getResultSet() should return null if this is an update   
  202.     // count or there are not more result sets   
  203.     public java.sql.ResultSet getResultSet() throws java.sql.SQLException {   
  204.     ResultSet rs = passthru.getResultSet();   
  205.         return (rs == null) ? null : getP6Factory().getResultSet(rs, this, preparedQuery, getQueryFromPreparedStatement());   
  206.     }   
  207.        
  208.     /*  
  209.      * P6Spy specific functionality  
  210.      */  
  211.    //这一步是对数组与sql进行处理,输出完整的sql语句   
  212.     public final String getQueryFromPreparedStatement() {   
  213.         int len = preparedQuery.length();   
  214.         StringBuffer t = new StringBuffer(len * 2);   
  215.            
  216.         if (values != null) {   
  217.             int i = 1, limit = 0, base = 0;   
  218.                
  219.             while ((limit = preparedQuery.indexOf('?',limit)) != -1) {   
  220.                 if (isString[i]) {   
  221.                     t.append(preparedQuery.substring(base,limit));   
  222.                     t.append("'");   
  223.                     t.append(values[i]);   
  224.                     t.append("'");   
  225.                 } else {   
  226.                     t.append(preparedQuery.substring(base,limit));   
  227.                     t.append(values[i]);   
  228.                 }   
  229.                 i++;   
  230.                 limit++;   
  231.                 base = limit;   
  232.             }   
  233.             if (base < len) {   
  234.                 t.append(preparedQuery.substring(base));   
  235.             }   
  236.         }   
  237.            
  238.         return t.toString();   
  239.     }   
  240.        
  241.     protected void growValues(int newMax) {   
  242.         int size = values.length;   
  243.         Object [] values_tmp = new Object[newMax + P6_GROW_MAX];   
  244.         boolean [] isString_tmp = new boolean[newMax + P6_GROW_MAX];   
  245.         System.arraycopy(values, 0, values_tmp,  0, size);   
  246.         values = values_tmp;   
  247.         System.arraycopy(isString, 0, isString_tmp, 0, size);   
  248.         isString = isString_tmp;   
  249.     }   
  250.        
  251.        
  252.     protected  void setObjectAsString(int i, Object o) {   
  253.         if (values != null) {   
  254.             if (i >= 0) {   
  255.            if ( i >= values.length) {   
  256.            growValues(i);   
  257.         }   
  258.                 values[i] = (o == null) ? "" : o.toString();   
  259.                 isString[i]  = true;   
  260.             }   
  261.         }   
  262.     }   
  263.        
  264.     protected  void setObjectAsInt(int i, Object o) {   
  265.         if (values != null) {   
  266.             if (i >=0) {       
  267.                 if (i >= values.length) {   
  268.                     growValues(i);   
  269.                 }   
  270.                 values[i] = (o == null) ? "" : o.toString();   
  271.                 isString[i]  = false;   
  272.             }    
  273.         }   
  274.     }   
  275.   
  276.     // Since JDK 1.4   
  277.     public void setURL(int p0, java.net.URL p1) throws java.sql.SQLException {   
  278.         setObjectAsString(p0, p1);   
  279.         prepStmtPassthru.setURL(p0, p1);   
  280.     }   
  281.        
  282.     // Since JDK 1.4   
  283.     public java.sql.ParameterMetaData getParameterMetaData() throws java.sql.SQLException {   
  284.         return(prepStmtPassthru.getParameterMetaData());   
  285.     }   
  286.   
  287.     /**  
  288.      * Returns the underlying JDBC object (in this case, a  
  289.      * java.sql.PreparedStatement).  
  290.      * <p>  
  291.      * The returned object is a java.sql.Statement due  
  292.      * to inheritance reasons, so you'll need to cast   
  293.      * appropriately.  
  294.      *  
  295.      * @return the wrapped JDBC object   
  296.      */  
  297.     public Statement getJDBC() {   
  298.     Statement wrapped = (prepStmtPassthru instanceof P6Statement) ?   
  299.         ((P6Statement) prepStmtPassthru).getJDBC() :   
  300.         prepStmtPassthru;   
  301.   
  302.     return wrapped;   
  303.     }   
  304.   
  305.    public int getValuesLength() {   
  306.      return values.length;   
  307.     }   
  308. }  
public class P6PreparedStatement extends P6Statement implements PreparedStatement {
    
    
    public final static int P6_MAX_FIELDS = 32;
    public static int P6_GROW_MAX = 32;
    protected PreparedStatement prepStmtPassthru;
    protected String preparedQuery;
    protected Object values[];//扩展就是为了取我们所设置的值
    protected boolean isString[];
    
    public P6PreparedStatement(P6Factory factory, PreparedStatement statement, P6Connection conn, String query) {
        super(factory, statement, conn);
        prepStmtPassthru = statement;
        this.preparedQuery = query;
        initValues();
    }
    
    protected void initValues() {
        values = new Object[P6_MAX_FIELDS+1];
        isString = new boolean[P6_MAX_FIELDS+1];
    }
    
    public void addBatch() throws SQLException {
        prepStmtPassthru.addBatch();
    }
    
    public void clearParameters() throws SQLException {
        prepStmtPassthru.clearParameters();
    }
    
    public boolean execute() throws SQLException {
        return prepStmtPassthru.execute();
    }
    
    public ResultSet executeQuery() throws SQLException {
        ResultSet resultSet = prepStmtPassthru.executeQuery();
        return (getP6Factory().getResultSet(resultSet, this, preparedQuery, getQueryFromPreparedStatement()));
    }
    
    public int executeUpdate() throws SQLException {
        return prepStmtPassthru.executeUpdate();
    }
    
    public ResultSetMetaData getMetaData() throws SQLException {
        return prepStmtPassthru.getMetaData();
    }
    
    public void setArray(int p0, Array p1) throws SQLException {
        setObjectAsString(p0, p1);
        // we need to make sure we get the real object in this case
        if (p1 instanceof P6Array) {
            prepStmtPassthru.setArray(p0,((P6Array)p1).passthru);
        } else{
            prepStmtPassthru.setArray(p0,p1);
        }
    }
    //以下设置都为装饰器的扩展,使我们每一次设置到?的值都会加到数组中去。
    public void setAsciiStream(int p0, InputStream p1, int p2) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setAsciiStream(p0,p1,p2);
    }
    
    public void setBigDecimal(int p0, BigDecimal p1) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setBigDecimal(p0,p1);
    }

    public void setBinaryStream(int p0, InputStream p1, int p2) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setBinaryStream(p0,p1,p2);
    }
    
    public void setBlob(int p0, Blob p1) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setBlob(p0,p1);
    }
    
    public void setBoolean(int p0, boolean p1) throws SQLException {
        setObjectAsString(p0, new Boolean(p1));
        prepStmtPassthru.setBoolean(p0,p1);
    }
    
    public void setByte(int p0, byte p1) throws SQLException {
        setObjectAsString(p0, new Byte(p1));
        prepStmtPassthru.setByte(p0,p1);
    }
    
    public void setBytes(int p0, byte[] p1) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setBytes(p0,p1);
    }
    
    public void setCharacterStream(int p0, Reader p1, int p2) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setCharacterStream(p0,p1,p2);
    }
    
    public void setClob(int p0, Clob p1) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setClob(p0,p1);
    }
    
    public void setDate(int p0, Date p1) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setDate(p0,p1);
    } 
    
    public void setDate(int p0, Date p1, java.util.Calendar p2) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setDate(p0,p1,p2);
    }
    
    public void setDouble(int p0, double p1) throws SQLException {
        setObjectAsInt(p0, new Double(p1));
        prepStmtPassthru.setDouble(p0,p1);
    }
    
    public void setFloat(int p0, float p1) throws SQLException {
        setObjectAsInt(p0, new Float(p1));
        prepStmtPassthru.setFloat(p0,p1);
    }
    
    public void setInt(int p0, int p1) throws SQLException {
        setObjectAsInt(p0, new Integer(p1));
        prepStmtPassthru.setInt(p0,p1);
    }
    
    public void setLong(int p0, long p1) throws SQLException {
        setObjectAsInt(p0, new Long(p1));
        prepStmtPassthru.setLong(p0,p1);
    }
    
    public void setNull(int p0, int p1, String p2) throws SQLException {
        setObjectAsString(p0, null);
        prepStmtPassthru.setNull(p0,p1,p2);
    }
    
    public void setNull(int p0, int p1) throws SQLException {
        setObjectAsString(p0, null);
        prepStmtPassthru.setNull(p0,p1);
    }
    
    public void setObject(int p0, Object p1, int p2, int p3) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setObject(p0,p1,p2,p3);
    }
    
    public void setObject(int p0, Object p1, int p2) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setObject(p0,p1,p2);
    }
    
    public void setObject(int p0, Object p1) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setObject(p0,p1);
    }
    
    public void setRef(int p0, Ref p1) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setRef(p0,p1);
    }
    
    public void setShort(int p0, short p1) throws SQLException {
        setObjectAsString(p0, new Short(p1));
        prepStmtPassthru.setShort(p0,p1);
    }
    
    public void setString(int p0, String p1) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setString(p0,p1);
    }
    
    public void setTime(int p0, Time p1, java.util.Calendar p2) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setTime(p0,p1,p2);
    }
    
    public void setTime(int p0, Time p1) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setTime(p0,p1);
    }
    
    public void setTimestamp(int p0, Timestamp p1, java.util.Calendar p2) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setTimestamp(p0,p1,p2);
    }
    
    public void setTimestamp(int p0, Timestamp p1) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setTimestamp(p0,p1);
    }
    
    public void setUnicodeStream(int p0, InputStream p1, int p2) throws SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setUnicodeStream(p0,p1,p2);
    }
    
    /* we override this because the p6statement version will not be 
     * able to return the accurate prepared statement or query information
     */
    // bug 161: getResultSet() should return null if this is an update
    // count or there are not more result sets
    public java.sql.ResultSet getResultSet() throws java.sql.SQLException {
	ResultSet rs = passthru.getResultSet();
        return (rs == null) ? null : getP6Factory().getResultSet(rs, this, preparedQuery, getQueryFromPreparedStatement());
    }
    
    /*
     * P6Spy specific functionality
     */
   //这一步是对数组与sql进行处理,输出完整的sql语句
    public final String getQueryFromPreparedStatement() {
        int len = preparedQuery.length();
        StringBuffer t = new StringBuffer(len * 2);
        
        if (values != null) {
            int i = 1, limit = 0, base = 0;
            
            while ((limit = preparedQuery.indexOf('?',limit)) != -1) {
                if (isString[i]) {
                    t.append(preparedQuery.substring(base,limit));
                    t.append("'");
                    t.append(values[i]);
                    t.append("'");
                } else {
                    t.append(preparedQuery.substring(base,limit));
                    t.append(values[i]);
                }
                i++;
                limit++;
                base = limit;
            }
            if (base < len) {
                t.append(preparedQuery.substring(base));
            }
        }
        
        return t.toString();
    }
    
    protected void growValues(int newMax) {
        int size = values.length;
        Object [] values_tmp = new Object[newMax + P6_GROW_MAX];
        boolean [] isString_tmp = new boolean[newMax + P6_GROW_MAX];
        System.arraycopy(values, 0, values_tmp,  0, size);
        values = values_tmp;
        System.arraycopy(isString, 0, isString_tmp, 0, size);
        isString = isString_tmp;
    }
    
    
    protected  void setObjectAsString(int i, Object o) {
        if (values != null) {
            if (i >= 0) {
	       if ( i >= values.length) {
		   growValues(i);
		}
                values[i] = (o == null) ? "" : o.toString();
                isString[i]  = true;
            }
        }
    }
    
    protected  void setObjectAsInt(int i, Object o) {
        if (values != null) {
            if (i >=0) {    
                if (i >= values.length) {
                    growValues(i);
                }
                values[i] = (o == null) ? "" : o.toString();
                isString[i]  = false;
            } 
        }
    }

    // Since JDK 1.4
    public void setURL(int p0, java.net.URL p1) throws java.sql.SQLException {
        setObjectAsString(p0, p1);
        prepStmtPassthru.setURL(p0, p1);
    }
    
    // Since JDK 1.4
    public java.sql.ParameterMetaData getParameterMetaData() throws java.sql.SQLException {
        return(prepStmtPassthru.getParameterMetaData());
    }

    /**
     * Returns the underlying JDBC object (in this case, a
     * java.sql.PreparedStatement).
     * <p>
     * The returned object is a java.sql.Statement due
     * to inheritance reasons, so you'll need to cast 
     * appropriately.
     *
     * @return the wrapped JDBC object 
     */
    public Statement getJDBC() {
	Statement wrapped = (prepStmtPassthru instanceof P6Statement) ?
	    ((P6Statement) prepStmtPassthru).getJDBC() :
	    prepStmtPassthru;

	return wrapped;
    }

   public int getValuesLength() {
     return values.length;
    }
}

 

  以上用了三个装饰器,对数据库驱动进去了扩展,使之每一次设置到?的值都会被一个数组接收,,,,,,

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值