mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
@@ -37,6 +37,7 @@ import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.proxy.jdbc.JdbcParameter.TYPE;
|
||||
@@ -50,6 +51,7 @@ public class PreparedStatementProxyImpl extends StatementProxyImpl implements Pr
|
||||
protected final String sql;
|
||||
private JdbcParameter[] parameters = new JdbcParameter[16];
|
||||
private int parametersSize = 0;
|
||||
private Map<Integer, JdbcParameter> paramMap = null;
|
||||
|
||||
public PreparedStatementProxyImpl(ConnectionProxy connection, PreparedStatement statement, String sql, long id){
|
||||
super(connection, statement, id);
|
||||
@@ -58,7 +60,14 @@ public class PreparedStatementProxyImpl extends StatementProxyImpl implements Pr
|
||||
}
|
||||
|
||||
public Map<Integer, JdbcParameter> getParameters() {
|
||||
return null;
|
||||
if (paramMap == null) {
|
||||
paramMap = new HashMap<Integer, JdbcParameter>(parametersSize);
|
||||
for (int i = 0; i < parametersSize; ++i) {
|
||||
paramMap.put(i, parameters[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return paramMap;
|
||||
}
|
||||
|
||||
void setParameter(int jdbcIndex, JdbcParameter parameter) {
|
||||
@@ -71,6 +80,10 @@ public class PreparedStatementProxyImpl extends StatementProxyImpl implements Pr
|
||||
parameters = Arrays.copyOf(parameters, parametersSize + 16);
|
||||
}
|
||||
parameters[index] = parameter;
|
||||
|
||||
if (paramMap != null) {
|
||||
paramMap = null;
|
||||
}
|
||||
}
|
||||
|
||||
public int getParametersSize() {
|
||||
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.alibaba.druid.bvt.proxy;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.proxy.jdbc.JdbcParameter;
|
||||
import com.alibaba.druid.proxy.jdbc.PreparedStatementProxy;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class PreparedStatementProxyImplGetParametersTest extends TestCase {
|
||||
|
||||
private String url = "jdbc:wrap-jdbc:filters=default:name=driverTest:jdbc:mock:xxx";
|
||||
private Connection conn;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
conn = DriverManager.getConnection(url);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
JdbcUtils.close(conn);
|
||||
}
|
||||
|
||||
public void test_get_parameters() throws Exception {
|
||||
final PreparedStatementProxy stmt = (PreparedStatementProxy) conn.prepareStatement("select 1");
|
||||
|
||||
{
|
||||
Map<Integer, JdbcParameter> paramMap = stmt.getParameters();
|
||||
Assert.assertNotNull(paramMap);
|
||||
Assert.assertEquals(paramMap.size(), 0);
|
||||
}
|
||||
stmt.setInt(1, 1);
|
||||
{
|
||||
Map<Integer, JdbcParameter> paramMap1 = stmt.getParameters();
|
||||
Assert.assertNotNull(paramMap1);
|
||||
|
||||
Map<Integer, JdbcParameter> paramMap2 = stmt.getParameters();
|
||||
Assert.assertNotNull(paramMap2);
|
||||
|
||||
Assert.assertSame(paramMap1, paramMap2);
|
||||
Assert.assertEquals(paramMap1.size(), 1);
|
||||
}
|
||||
stmt.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user