Search in sources :

Example 1 with MockRowId

use of com.alibaba.druid.mock.MockRowId in project druid by alibaba.

the class LogFilterTest method executeSQL.

@SuppressWarnings("deprecation")
private void executeSQL(DataSourceProxyImpl dataSource) throws SQLException {
    String sql = "SELECT 1";
    Connection conn = dataSource.connect(new Properties());
    conn.commit();
    conn.rollback();
    Statement stmt = conn.createStatement();
    stmt.execute(sql);
    stmt.addBatch(sql);
    stmt.executeBatch();
    stmt.executeQuery(sql);
    stmt.executeUpdate(sql);
    stmt.cancel();
    PreparedStatement pstmt = conn.prepareStatement(sql);
    pstmt.setArray(1, null);
    pstmt.setAsciiStream(2, null);
    pstmt.setBigDecimal(3, null);
    pstmt.setBinaryStream(4, null);
    pstmt.setBlob(5, conn.createBlob());
    pstmt.setByte(6, (byte) 1);
    pstmt.setBytes(7, new byte[1]);
    pstmt.setCharacterStream(8, null);
    pstmt.setClob(9, conn.createClob());
    pstmt.setDate(10, null);
    pstmt.setFloat(11, 1F);
    pstmt.setInt(12, 1);
    pstmt.setLong(13, 1L);
    pstmt.setNCharacterStream(14, null);
    pstmt.setNClob(15, conn.createNClob());
    pstmt.setNString(16, null);
    pstmt.setNull(17, Types.VARCHAR);
    pstmt.setObject(18, null);
    pstmt.setRef(19, new MockRef());
    pstmt.setRowId(20, new MockRowId());
    pstmt.setShort(21, (short) 1);
    pstmt.setSQLXML(22, conn.createSQLXML());
    pstmt.setString(23, "");
    pstmt.setTime(24, null);
    pstmt.setTimestamp(25, null);
    pstmt.setUnicodeStream(26, null, 0);
    pstmt.setURL(27, null);
    pstmt.execute();
    pstmt.addBatch();
    pstmt.executeBatch();
    pstmt.executeQuery();
    pstmt.executeUpdate();
    conn.prepareCall(sql);
    ResultSet rs = stmt.executeQuery(sql);
    rs.next();
    rs.close();
    {
        Exception error = null;
        try {
            stmt.execute(MockStatement.ERROR_SQL);
        } catch (SQLException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    stmt.close();
    pstmt.close();
    conn.close();
}
Also used : MockRowId(com.alibaba.druid.mock.MockRowId) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) MockStatement(com.alibaba.druid.mock.MockStatement) Statement(java.sql.Statement) MockRef(com.alibaba.druid.mock.MockRef) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) MockResultSet(com.alibaba.druid.mock.MockResultSet) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) SQLException(java.sql.SQLException)

Example 2 with MockRowId

use of com.alibaba.druid.mock.MockRowId in project druid by alibaba.

the class StatFilterBuildSlowParameterTest method test_buildSlowSql.

public void test_buildSlowSql() throws Exception {
    long currentMillis = System.currentTimeMillis();
    String sql = "select ?, ?, ?, ?, ?";
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date(System.currentTimeMillis());
    String dateText = dateFormat.format(date);
    {
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setBoolean(1, true);
        stmt.setInt(2, 123);
        stmt.setLong(3, 10001);
        stmt.setTimestamp(4, new java.sql.Timestamp(currentMillis));
        stmt.setDate(5, new java.sql.Date(currentMillis));
        ResultSet rs = stmt.executeQuery();
        rs.close();
        stmt.close();
        conn.close();
        // //////
        JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
        Assert.assertNotNull(sqlStat);
        String slowParameters = sqlStat.getLastSlowParameters();
        Assert.assertNotNull(slowParameters);
        List<Object> parameters = (List<Object>) JSONUtils.parse(slowParameters);
        Assert.assertEquals(5, parameters.size());
        Assert.assertEquals(true, parameters.get(0));
        Assert.assertEquals(123, parameters.get(1));
        Assert.assertEquals(10001, parameters.get(2));
        Assert.assertEquals(dateText, parameters.get(3));
        Assert.assertEquals(dateText, parameters.get(4));
    }
    currentMillis = System.currentTimeMillis();
    date = new Date(System.currentTimeMillis());
    dateText = dateFormat.format(date);
    {
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setBoolean(1, false);
        stmt.setInt(2, 234);
        stmt.setLong(3, 10002);
        stmt.setTimestamp(4, new java.sql.Timestamp(currentMillis));
        stmt.setDate(5, new java.sql.Date(currentMillis));
        ResultSet rs = stmt.executeQuery();
        rs.close();
        stmt.close();
        conn.close();
        // //////
        JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
        Assert.assertNotNull(sqlStat);
        String slowParameters = sqlStat.getLastSlowParameters();
        Assert.assertNotNull(slowParameters);
        List<Object> parameters = (List<Object>) JSONUtils.parse(slowParameters);
        Assert.assertEquals(5, parameters.size());
        Assert.assertEquals(false, parameters.get(0));
        Assert.assertEquals(234, parameters.get(1));
        Assert.assertEquals(10002, parameters.get(2));
        Assert.assertEquals(dateText, parameters.get(3));
        Assert.assertEquals(dateText, parameters.get(4));
    }
    {
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < 10; ++i) {
            buf.append("abcdefghijklmnABCDEFGHIJKLMN1234567890!@#$%^&*(");
        }
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setNull(1, Types.VARCHAR);
        stmt.setString(2, buf.toString());
        stmt.setClob(3, new MockClob());
        stmt.setNClob(4, new MockNClob());
        stmt.setBlob(5, new MockBlob());
        ResultSet rs = stmt.executeQuery();
        rs.close();
        stmt.close();
        conn.close();
        // //////
        JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
        Assert.assertNotNull(sqlStat);
        String slowParameters = sqlStat.getLastSlowParameters();
        Assert.assertNotNull(slowParameters);
        List<Object> parameters = (List<Object>) JSONUtils.parse(slowParameters);
        Assert.assertEquals(5, parameters.size());
        Assert.assertEquals(null, parameters.get(0));
        Assert.assertEquals(buf.substring(0, 97) + "...", parameters.get(1));
        Assert.assertEquals("<Clob>", parameters.get(2));
        Assert.assertEquals("<NClob>", parameters.get(3));
        Assert.assertEquals("<Blob>", parameters.get(4));
    }
    {
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < 10; ++i) {
            buf.append("中国abcdefghijklmnABCDEFGHIJKLMN1234567890!@#$%^&*(");
        }
        Connection conn = dataSource.getConnection();
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setBinaryStream(1, new ByteInputStream(new byte[0]));
        stmt.setString(2, buf.toString());
        stmt.setTime(3, new Time(currentMillis));
        stmt.setBigDecimal(4, new BigDecimal("56789.123"));
        stmt.setRowId(5, new MockRowId());
        ResultSet rs = stmt.executeQuery();
        rs.close();
        stmt.close();
        conn.close();
        // //////
        JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
        Assert.assertNotNull(sqlStat);
        String slowParameters = sqlStat.getLastSlowParameters();
        Assert.assertNotNull(slowParameters);
        List<Object> parameters = (List<Object>) JSONUtils.parse(slowParameters);
        Assert.assertEquals(5, parameters.size());
        Assert.assertEquals("<InputStream>", parameters.get(0));
        Assert.assertEquals(buf.substring(0, 97) + "...", parameters.get(1));
        Assert.assertEquals(dateText, parameters.get(2));
        Assert.assertEquals(56789.123, parameters.get(3));
        Assert.assertEquals("<com.alibaba.druid.mock.MockRowId>", parameters.get(4));
    }
}
Also used : MockNClob(com.alibaba.druid.mock.MockNClob) JdbcSqlStat(com.alibaba.druid.stat.JdbcSqlStat) MockRowId(com.alibaba.druid.mock.MockRowId) ByteInputStream(org.nutz.lang.util.ByteInputStream) MockBlob(com.alibaba.druid.mock.MockBlob) MockClob(com.alibaba.druid.mock.MockClob) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Time(java.sql.Time) Date(java.util.Date) BigDecimal(java.math.BigDecimal) ResultSet(java.sql.ResultSet) List(java.util.List) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

MockRowId (com.alibaba.druid.mock.MockRowId)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 MockBlob (com.alibaba.druid.mock.MockBlob)1 MockClob (com.alibaba.druid.mock.MockClob)1 MockNClob (com.alibaba.druid.mock.MockNClob)1 MockRef (com.alibaba.druid.mock.MockRef)1 MockResultSet (com.alibaba.druid.mock.MockResultSet)1 MockStatement (com.alibaba.druid.mock.MockStatement)1 JdbcSqlStat (com.alibaba.druid.stat.JdbcSqlStat)1 BigDecimal (java.math.BigDecimal)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Time (java.sql.Time)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 List (java.util.List)1 Properties (java.util.Properties)1 ByteInputStream (org.nutz.lang.util.ByteInputStream)1