mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge pull request #345 from wenshao/master
bug fixed for PreparedStatementImpl
This commit is contained in:
@@ -16,8 +16,8 @@
|
||||
<spring.version>3.1.1.RELEASE</spring.version>
|
||||
<junit.version>4.11</junit.version>
|
||||
|
||||
<gpg.skip>false</gpg.skip>
|
||||
<javadoc.skip>false</javadoc.skip>
|
||||
<gpg.skip>trye</gpg.skip>
|
||||
<javadoc.skip>true</javadoc.skip>
|
||||
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<jdk.version>1.6</jdk.version>
|
||||
|
||||
@@ -90,7 +90,7 @@ public class PreparedStatementProxyImpl extends StatementProxyImpl implements Pr
|
||||
lastExecuteType = StatementExecuteType.Execute;
|
||||
lastExecuteStartNano = -1L;
|
||||
lastExecuteTimeNano = -1L;
|
||||
|
||||
|
||||
firstResultSet = createChain().preparedStatement_execute(this);
|
||||
return firstResultSet;
|
||||
}
|
||||
@@ -98,26 +98,26 @@ public class PreparedStatementProxyImpl extends StatementProxyImpl implements Pr
|
||||
@Override
|
||||
public ResultSet executeQuery() throws SQLException {
|
||||
firstResultSet = true;
|
||||
|
||||
|
||||
updateCount = null;
|
||||
lastExecuteSql = sql;
|
||||
lastExecuteType = StatementExecuteType.ExecuteQuery;
|
||||
lastExecuteStartNano = -1L;
|
||||
lastExecuteTimeNano = -1L;
|
||||
|
||||
|
||||
return createChain().preparedStatement_executeQuery(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int executeUpdate() throws SQLException {
|
||||
firstResultSet = false;
|
||||
|
||||
|
||||
updateCount = null;
|
||||
lastExecuteSql = sql;
|
||||
lastExecuteType = StatementExecuteType.ExecuteUpdate;
|
||||
lastExecuteStartNano = -1L;
|
||||
lastExecuteTimeNano = -1L;
|
||||
|
||||
|
||||
updateCount = createChain().preparedStatement_executeUpdate(this);
|
||||
return updateCount;
|
||||
}
|
||||
@@ -369,11 +369,111 @@ public class PreparedStatementProxyImpl extends StatementProxyImpl implements Pr
|
||||
|
||||
@Override
|
||||
public void setObject(int parameterIndex, Object x) throws SQLException {
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.OTHER, null));
|
||||
setObjectParameter(parameterIndex, x);
|
||||
|
||||
createChain().preparedStatement_setObject(this, parameterIndex, x);
|
||||
}
|
||||
|
||||
private void setObjectParameter(int parameterIndex, Object x) {
|
||||
if (x == null) {
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.OTHER, null));
|
||||
return;
|
||||
}
|
||||
|
||||
Class<?> clazz = x.getClass();
|
||||
if (clazz == Byte.class) {
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.TINYINT, x));
|
||||
return;
|
||||
}
|
||||
|
||||
if (clazz == Short.class) {
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.SMALLINT, x));
|
||||
return;
|
||||
}
|
||||
|
||||
if (clazz == Integer.class) {
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.INTEGER, x));
|
||||
return;
|
||||
}
|
||||
|
||||
if (clazz == Long.class) {
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.BIGINT, x));
|
||||
return;
|
||||
}
|
||||
|
||||
if (clazz == String.class) {
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.VARCHAR, x));
|
||||
return;
|
||||
}
|
||||
|
||||
if (clazz == BigDecimal.class) {
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.DECIMAL, x));
|
||||
return;
|
||||
}
|
||||
|
||||
if (clazz == Float.class) {
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.FLOAT, x));
|
||||
return;
|
||||
}
|
||||
|
||||
if (clazz == Double.class) {
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.DOUBLE, x));
|
||||
return;
|
||||
}
|
||||
|
||||
if (clazz == java.sql.Date.class || clazz == java.util.Date.class) {
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.DATE, x));
|
||||
return;
|
||||
}
|
||||
|
||||
if (clazz == java.sql.Timestamp.class) {
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.TIMESTAMP, x));
|
||||
return;
|
||||
}
|
||||
|
||||
if (clazz == java.sql.Time.class) {
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.TIME, x));
|
||||
return;
|
||||
}
|
||||
|
||||
if (clazz == Boolean.class) {
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.BOOLEAN, x));
|
||||
return;
|
||||
}
|
||||
|
||||
if (clazz == byte[].class) {
|
||||
setParameter(parameterIndex, new JdbcParameter(TYPE.BYTES, x));
|
||||
return;
|
||||
}
|
||||
|
||||
if (x instanceof InputStream) {
|
||||
setParameter(parameterIndex, new JdbcParameter(JdbcParameter.TYPE.BinaryInputStream, x));
|
||||
return;
|
||||
}
|
||||
|
||||
if (x instanceof Reader) {
|
||||
setParameter(parameterIndex, new JdbcParameter(JdbcParameter.TYPE.CharacterInputStream, x));
|
||||
return;
|
||||
}
|
||||
|
||||
if (x instanceof Clob) {
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.CLOB, x));
|
||||
return;
|
||||
}
|
||||
|
||||
if (x instanceof NClob) {
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.NCLOB, x));
|
||||
return;
|
||||
}
|
||||
|
||||
if (x instanceof Blob) {
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.BLOB, x));
|
||||
return;
|
||||
}
|
||||
|
||||
setParameter(parameterIndex, new JdbcParameter(Types.OTHER, null));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
|
||||
setParameter(parameterIndex, new JdbcParameter(targetSqlType, x));
|
||||
|
||||
@@ -224,6 +224,7 @@ public class SQLExprParser extends SQLParser {
|
||||
case SCHEMA:
|
||||
case COLUMN:
|
||||
case IF:
|
||||
case END:
|
||||
sqlExpr = new SQLIdentifierExpr(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
break;
|
||||
|
||||
@@ -39,7 +39,6 @@ public class LogFactory {
|
||||
throw new IllegalStateException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
tryImplementation("java.lang.Object", "com.alibaba.druid.support.logging.NoLoggingImpl");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@@ -48,7 +48,7 @@ public class DruidDataSourceTest_notEmptyWait2 extends TestCase {
|
||||
startLatch.countDown();
|
||||
try {
|
||||
Connection conn = dataSource.getConnection();
|
||||
Thread.sleep(1);
|
||||
Thread.sleep(2);
|
||||
conn.close();
|
||||
} catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.Types;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl;
|
||||
|
||||
public class PreparedStatementProxyImplTest extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setInitialSize(1);
|
||||
dataSource.setFilters("log4j");
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_setObject() throws Exception {
|
||||
String sql = "insert t values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
stmt.setObject(1, (byte) 1);
|
||||
stmt.setObject(2, (short) 1);
|
||||
stmt.setObject(3, (int) 1);
|
||||
stmt.setObject(4, (long) 1);
|
||||
stmt.setObject(5, (float) 1);
|
||||
|
||||
stmt.setObject(6, (double) 1);
|
||||
stmt.setObject(7, new BigDecimal(1));
|
||||
stmt.setObject(8, true);
|
||||
stmt.setObject(9, "xxx");
|
||||
stmt.setObject(10, new java.sql.Date(System.currentTimeMillis()));
|
||||
|
||||
stmt.setObject(11, new java.util.Date(System.currentTimeMillis()));
|
||||
stmt.setObject(12, new java.sql.Timestamp(System.currentTimeMillis()));
|
||||
stmt.setObject(13, new java.sql.Time(System.currentTimeMillis()));
|
||||
|
||||
stmt.execute();
|
||||
|
||||
PreparedStatementProxyImpl stmtProxy = stmt.unwrap(PreparedStatementProxyImpl.class);
|
||||
Assert.assertNotNull(stmtProxy);
|
||||
|
||||
Assert.assertEquals(Types.TINYINT, stmtProxy.getParameters().get(1).getSqlType());
|
||||
Assert.assertEquals(Types.SMALLINT, stmtProxy.getParameters().get(2).getSqlType());
|
||||
Assert.assertEquals(Types.INTEGER, stmtProxy.getParameters().get(3).getSqlType());
|
||||
Assert.assertEquals(Types.BIGINT, stmtProxy.getParameters().get(4).getSqlType());
|
||||
Assert.assertEquals(Types.FLOAT, stmtProxy.getParameters().get(5).getSqlType());
|
||||
|
||||
Assert.assertEquals(Types.DOUBLE, stmtProxy.getParameters().get(6).getSqlType());
|
||||
Assert.assertEquals(Types.DECIMAL, stmtProxy.getParameters().get(7).getSqlType());
|
||||
Assert.assertEquals(Types.BOOLEAN, stmtProxy.getParameters().get(8).getSqlType());
|
||||
Assert.assertEquals(Types.VARCHAR, stmtProxy.getParameters().get(9).getSqlType());
|
||||
Assert.assertEquals(Types.DATE, stmtProxy.getParameters().get(10).getSqlType());
|
||||
|
||||
Assert.assertEquals(Types.DATE, stmtProxy.getParameters().get(11).getSqlType());
|
||||
Assert.assertEquals(Types.TIMESTAMP, stmtProxy.getParameters().get(12).getSqlType());
|
||||
Assert.assertEquals(Types.TIME, stmtProxy.getParameters().get(13).getSqlType());
|
||||
|
||||
stmt.close();
|
||||
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
|
||||
public class MySqlInsertTest_7 extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "insert into Apply (applicant_id, applytime, applytype, approver_id, end, process, start) values (?, ?, ?, ?, ?, ?, ?)";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
|
||||
|
||||
Assert.assertEquals(7, insertStmt.getColumns().size());
|
||||
Assert.assertEquals(1, insertStmt.getValuesList().size());
|
||||
Assert.assertEquals(7, insertStmt.getValuesList().get(0).getValues().size());
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
Assert.assertEquals("INSERT INTO Apply (applicant_id, applytime, applytype, approver_id, end" + //
|
||||
"\n\t, process, start)" + //
|
||||
"\nVALUES (?, ?, ?, ?, ?" + //
|
||||
"\n\t, ?, ?)", //
|
||||
SQLUtils.toMySqlString(insertStmt));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user