Search in sources :

Example 6 with ClientPreparedStatement

use of com.mysql.cj.jdbc.ClientPreparedStatement in project aws-mysql-jdbc by awslabs.

the class StatementsTest method testParameterBindings.

@Test
public void testParameterBindings() throws Exception {
    Properties props = new Properties();
    props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name());
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    props.setProperty(PropertyKey.characterEncoding.getKeyName(), "UTF-8");
    props.setProperty(PropertyKey.treatUtilDateAsTimestamp.getKeyName(), "false");
    props.setProperty(PropertyKey.autoDeserialize.getKeyName(), "true");
    for (boolean useSPS : new boolean[] { false, true }) {
        props.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), Boolean.toString(useSPS));
        // Need to check character set stuff, so need a new connection
        Connection utfConn = getConnectionWithProps(props);
        java.util.Date now = new java.util.Date();
        Object[] valuesToTest = new Object[] { new Byte(Byte.MIN_VALUE), new Short(Short.MIN_VALUE), new Integer(Integer.MIN_VALUE), // to test isNull
        new Long(Long.MIN_VALUE), // to test isNull
        new Double(Double.MIN_VALUE), // to test isNull
        "\u4E2D\u6587", // to test isNull
        new BigDecimal(Math.PI), // to test isNull
        null, // to test serialization
        now };
        StringBuilder statementText = new StringBuilder("SELECT ?");
        for (int i = 1; i < valuesToTest.length; i++) {
            statementText.append(",?");
        }
        this.pstmt = utfConn.prepareStatement(statementText.toString());
        for (int i = 0; i < valuesToTest.length; i++) {
            this.pstmt.setObject(i + 1, valuesToTest[i]);
        }
        ParameterBindings bindings = ((ClientPreparedStatement) this.pstmt).getParameterBindings();
        for (int i = 0; i < valuesToTest.length; i++) {
            Object boundObject = bindings.getObject(i + 1);
            if (boundObject == null || valuesToTest[i] == null) {
                continue;
            }
            Class<?> boundObjectClass = boundObject.getClass();
            Class<?> testObjectClass = valuesToTest[i].getClass();
            if (boundObject instanceof Number) {
                assertEquals(valuesToTest[i].toString(), boundObject.toString(), "For binding #" + (i + 1) + " of class " + boundObjectClass + " compared to " + testObjectClass);
            } else if (boundObject instanceof Date) {
            } else {
                assertEquals(valuesToTest[i], boundObject, "For binding #" + (i + 1) + " of class " + boundObjectClass + " compared to " + testObjectClass);
            }
        }
    }
}
Also used : ClientPreparedStatement(com.mysql.cj.jdbc.ClientPreparedStatement) ParameterBindings(com.mysql.cj.jdbc.ParameterBindings) Connection(java.sql.Connection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) Properties(java.util.Properties) LocalDate(java.time.LocalDate) Date(java.sql.Date) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 7 with ClientPreparedStatement

use of com.mysql.cj.jdbc.ClientPreparedStatement in project aws-mysql-jdbc by awslabs.

the class StatementsTest method testSetNCharacterStream.

/**
 * Tests for PreparedStatement.setNCharacterSteam()
 *
 * @throws Exception
 */
@Test
public void testSetNCharacterStream() throws Exception {
    // suppose sql_mode don't include "NO_BACKSLASH_ESCAPES"
    createTable("testSetNCharacterStream", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10), " + "c3 NATIONAL CHARACTER(10)) ENGINE=InnoDB");
    Properties props1 = new Properties();
    props1.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name());
    props1.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    // use client-side prepared statement
    props1.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), "false");
    // ensure charset isn't utf8 here
    props1.setProperty(PropertyKey.characterEncoding.getKeyName(), "latin1");
    Connection conn1 = getConnectionWithProps(props1);
    ClientPreparedStatement pstmt1 = (ClientPreparedStatement) conn1.prepareStatement("INSERT INTO testSetNCharacterStream (c1, c2, c3) VALUES (?, ?, ?)");
    pstmt1.setNCharacterStream(1, null, 0);
    pstmt1.setNCharacterStream(2, new StringReader("aaa"), 3);
    pstmt1.setNCharacterStream(3, new StringReader("\'aaa\'"), 5);
    pstmt1.execute();
    ResultSet rs1 = this.stmt.executeQuery("SELECT c1, c2, c3 FROM testSetNCharacterStream");
    rs1.next();
    assertEquals(null, rs1.getString(1));
    assertEquals("aaa", rs1.getString(2));
    assertEquals("\'aaa\'", rs1.getString(3));
    rs1.close();
    pstmt1.close();
    conn1.close();
    createTable("testSetNCharacterStream", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10), " + "c3 NATIONAL CHARACTER(10)) ENGINE=InnoDB");
    Properties props2 = new Properties();
    props2.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name());
    props2.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    // use client-side prepared statement
    props2.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), "false");
    // ensure charset is utf8 here
    props2.setProperty(PropertyKey.characterEncoding.getKeyName(), "UTF-8");
    Connection conn2 = getConnectionWithProps(props2);
    ClientPreparedStatement pstmt2 = (ClientPreparedStatement) conn2.prepareStatement("INSERT INTO testSetNCharacterStream (c1, c2, c3) VALUES (?, ?, ?)");
    pstmt2.setNCharacterStream(1, null, 0);
    pstmt2.setNCharacterStream(2, new StringReader("aaa"), 3);
    pstmt2.setNCharacterStream(3, new StringReader("\'aaa\'"), 5);
    pstmt2.execute();
    ResultSet rs2 = this.stmt.executeQuery("SELECT c1, c2, c3 FROM testSetNCharacterStream");
    rs2.next();
    assertEquals(null, rs2.getString(1));
    assertEquals("aaa", rs2.getString(2));
    assertEquals("\'aaa\'", rs2.getString(3));
    rs2.close();
    pstmt2.close();
    conn2.close();
}
Also used : ClientPreparedStatement(com.mysql.cj.jdbc.ClientPreparedStatement) Connection(java.sql.Connection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) StringReader(java.io.StringReader) ResultSet(java.sql.ResultSet) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Aggregations

ClientPreparedStatement (com.mysql.cj.jdbc.ClientPreparedStatement)7 Properties (java.util.Properties)7 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)6 Test (org.junit.jupiter.api.Test)6 Connection (java.sql.Connection)5 MysqlConnection (com.mysql.cj.MysqlConnection)3 ServerPreparedStatement (com.mysql.cj.jdbc.ServerPreparedStatement)3 File (java.io.File)2 ResultSet (java.sql.ResultSet)2 QueryBindings (com.mysql.cj.QueryBindings)1 CallableStatement (com.mysql.cj.jdbc.CallableStatement)1 JdbcStatement (com.mysql.cj.jdbc.JdbcStatement)1 MysqlSavepoint (com.mysql.cj.jdbc.MysqlSavepoint)1 ParameterBindings (com.mysql.cj.jdbc.ParameterBindings)1 StatementImpl (com.mysql.cj.jdbc.StatementImpl)1 ReplicationConnection (com.mysql.cj.jdbc.ha.ReplicationConnection)1 ResultSetInternalMethods (com.mysql.cj.jdbc.result.ResultSetInternalMethods)1 UpdatableResultSet (com.mysql.cj.jdbc.result.UpdatableResultSet)1 ColumnDefinition (com.mysql.cj.protocol.ColumnDefinition)1 Message (com.mysql.cj.protocol.Message)1