Search in sources :

Example 16 with JdbcStatement

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

the class QueryAttributesTest method plainStatementWithQueryAttributesInMultiHost.

/**
 * Tests whether proxied plain statement objects created in multi-host connections handle query attributes correctly.
 *
 * @throws Exception
 */
@Test
void plainStatementWithQueryAttributesInMultiHost() throws Exception {
    // Failover connection.
    Connection testConn = getFailoverConnection();
    Statement testStmt = testConn.createStatement();
    assertTrue(JdbcStatement.class.isInstance(testStmt));
    JdbcStatement testJdbcStmt = (JdbcStatement) testStmt;
    testJdbcStmt.setAttribute("qa", "MySQL Connector/J");
    this.rs = testStmt.executeQuery("SELECT mysql_query_attribute_string('qa')");
    assertTrue(this.rs.next());
    assertEquals("MySQL Connector/J", this.rs.getString(1));
    assertFalse(this.rs.next());
    testConn.close();
    // Loadbalanced connection.
    testConn = getLoadBalancedConnection();
    testStmt = testConn.createStatement();
    assertTrue(JdbcStatement.class.isInstance(testStmt));
    testJdbcStmt = (JdbcStatement) testStmt;
    testJdbcStmt.setAttribute("qa", "MySQL Connector/J");
    this.rs = testStmt.executeQuery("SELECT mysql_query_attribute_string('qa')");
    assertTrue(this.rs.next());
    assertEquals("MySQL Connector/J", this.rs.getString(1));
    assertFalse(this.rs.next());
    testConn.close();
    // Replication connection.
    testConn = getSourceReplicaReplicationConnection();
    testStmt = testConn.createStatement();
    assertTrue(JdbcStatement.class.isInstance(testStmt));
    testJdbcStmt = (JdbcStatement) testStmt;
    testJdbcStmt.setAttribute("qa", "MySQL Connector/J");
    this.rs = testStmt.executeQuery("SELECT mysql_query_attribute_string('qa')");
    assertTrue(this.rs.next());
    assertEquals("MySQL Connector/J", this.rs.getString(1));
    assertFalse(this.rs.next());
    testConn.close();
}
Also used : JdbcStatement(com.mysql.cj.jdbc.JdbcStatement) JdbcStatement(com.mysql.cj.jdbc.JdbcStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) Test(org.junit.jupiter.api.Test)

Example 17 with JdbcStatement

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

the class QueryAttributesTest method multiQueriesWithAttributesInPlainStatement.

/**
 * Tests if query attributes hold in plain statements with multi-queries.
 *
 * @throws Exception
 */
@Test
public void multiQueriesWithAttributesInPlainStatement() throws Exception {
    Properties props = new Properties();
    props.setProperty(PropertyKey.allowMultiQueries.getKeyName(), "true");
    Connection testConn = getConnectionWithProps(props);
    Statement testStmt = testConn.createStatement();
    assertTrue(JdbcStatement.class.isInstance(testStmt));
    JdbcStatement testJdbcStmt = (JdbcStatement) testStmt;
    testJdbcStmt.setAttribute("qa01", "MySQL Connector/J");
    testJdbcStmt.setAttribute("qa02", "8.0.26");
    this.rs = testStmt.executeQuery("SELECT mysql_query_attribute_string('qa01') AS qa01; SELECT mysql_query_attribute_string('qa02') AS qa02;");
    assertTrue(this.rs.next());
    assertEquals("MySQL Connector/J", this.rs.getString("qa01"));
    assertFalse(this.rs.next());
    assertTrue(testStmt.getMoreResults());
    this.rs = testStmt.getResultSet();
    assertTrue(this.rs.next());
    assertEquals("8.0.26", this.rs.getString("qa02"));
    assertFalse(this.rs.next());
    testConn.close();
}
Also used : JdbcStatement(com.mysql.cj.jdbc.JdbcStatement) JdbcStatement(com.mysql.cj.jdbc.JdbcStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 18 with JdbcStatement

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

the class UtilsTest method testIsJdbcInterface.

/**
 * Tests Util.isJdbcInterface()
 */
@Test
public void testIsJdbcInterface() {
    // Classes directly or indirectly implementing JDBC interfaces.
    assertTrue(Util.isJdbcInterface(ClientPreparedStatement.class));
    assertTrue(Util.isJdbcInterface(StatementImpl.class));
    assertTrue(Util.isJdbcInterface(JdbcStatement.class));
    assertTrue(Util.isJdbcInterface(ResultSetImpl.class));
    JdbcStatement s = (JdbcStatement) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class<?>[] { JdbcStatement.class }, new InvocationHandler() {

        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            return null;
        }
    });
    assertTrue(Util.isJdbcInterface(s.getClass()));
    // Classes not implementing JDBC interfaces.
    assertFalse(Util.isJdbcInterface(Util.class));
    assertFalse(Util.isJdbcInterface(UtilsTest.class));
}
Also used : ClientPreparedStatement(com.mysql.cj.jdbc.ClientPreparedStatement) ResultSetImpl(com.mysql.cj.jdbc.result.ResultSetImpl) JdbcStatement(com.mysql.cj.jdbc.JdbcStatement) StatementImpl(com.mysql.cj.jdbc.StatementImpl) Util(com.mysql.cj.util.Util) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) Test(org.junit.jupiter.api.Test)

Aggregations

JdbcStatement (com.mysql.cj.jdbc.JdbcStatement)18 Test (org.junit.jupiter.api.Test)18 PreparedStatement (java.sql.PreparedStatement)17 Connection (java.sql.Connection)13 Properties (java.util.Properties)12 Statement (java.sql.Statement)6 BigDecimal (java.math.BigDecimal)5 Time (java.sql.Time)5 Timestamp (java.sql.Timestamp)5 LocalDate (java.time.LocalDate)5 LocalDateTime (java.time.LocalDateTime)5 LocalTime (java.time.LocalTime)5 Calendar (java.util.Calendar)5 BigInteger (java.math.BigInteger)4 SimpleDateFormat (java.text.SimpleDateFormat)3 OffsetDateTime (java.time.OffsetDateTime)3 OffsetTime (java.time.OffsetTime)3 ZonedDateTime (java.time.ZonedDateTime)3 Date (java.util.Date)3 ClientPreparedStatement (com.mysql.cj.jdbc.ClientPreparedStatement)2