Search in sources :

Example 6 with SqlResult

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

the class SecureSessionTest method assertTlsVersion.

private void assertTlsVersion(Session sess, String expectedTlsVersion) {
    SqlResult rs = sess.sql("SHOW SESSION STATUS LIKE 'mysqlx_ssl_version'").execute();
    String actual = rs.fetchOne().getString(1);
    assertEquals(expectedTlsVersion, actual);
}
Also used : SqlResult(com.mysql.cj.xdevapi.SqlResult)

Example 7 with SqlResult

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

the class SecureSessionTest method getHighestCommonTlsVersion.

private String getHighestCommonTlsVersion(Session sess) throws Exception {
    // Find out which TLS protocol versions are supported by this JVM.
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, null, null);
    List<String> jvmSupportedProtocols = Arrays.asList(sslContext.createSSLEngine().getSupportedProtocols());
    SqlResult rset = sess.sql("SHOW GLOBAL VARIABLES LIKE 'tls_version'").execute();
    String value = rset.fetchOne().getString(1);
    // this.rs = sslConn.createStatement().executeQuery("SHOW GLOBAL VARIABLES LIKE 'tls_version'");
    // assertTrue(this.rs.next());
    List<String> serverSupportedProtocols = Arrays.asList(value.trim().split("\\s*,\\s*"));
    String highestCommonTlsVersion = "";
    for (String p : new String[] { "TLSv1.3", "TLSv1.2", "TLSv1.1", "TLSv1" }) {
        if (jvmSupportedProtocols.contains(p) && serverSupportedProtocols.contains(p)) {
            highestCommonTlsVersion = p;
            break;
        }
    }
    System.out.println("Server supports TLS protocols: " + serverSupportedProtocols);
    System.out.println("Highest common TLS protocol: " + highestCommonTlsVersion);
    return highestCommonTlsVersion;
}
Also used : SqlResult(com.mysql.cj.xdevapi.SqlResult) SSLContext(javax.net.ssl.SSLContext)

Example 8 with SqlResult

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

the class SecureSessionTest method assertUser.

private void assertUser(String user, Session sess) {
    SqlResult rows = sess.sql("SELECT USER(),CURRENT_USER()").execute();
    Row row = rows.fetchOne();
    assertEquals(user, row.getString(0).split("@")[0]);
    assertEquals(user, row.getString(1).split("@")[0]);
}
Also used : SqlResult(com.mysql.cj.xdevapi.SqlResult) Row(com.mysql.cj.xdevapi.Row)

Example 9 with SqlResult

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

the class SessionTest method testFetchOneFetchAllAsync.

@Test
public void testFetchOneFetchAllAsync() throws Exception {
    Row r = null;
    List<Row> rowList = null;
    try {
        CompletableFuture<SqlResult> asyncSqlRes = this.session.sql("drop table if exists testFetchOneFetchAllAsync").executeAsync();
        SqlResult sqlRes = asyncSqlRes.get();
        asyncSqlRes = this.session.sql("create table testFetchOneFetchAllAsync(a int,b bigint,c double,d blob)").executeAsync();
        sqlRes = asyncSqlRes.get();
        asyncSqlRes = this.session.sql("insert into testFetchOneFetchAllAsync values(?,?,?,?)").bind(1, 11).bind(21, "A").executeAsync();
        sqlRes = asyncSqlRes.get();
        asyncSqlRes = this.session.sql("insert into testFetchOneFetchAllAsync values(?,?,?,?)").bind(2, 12).bind(22, "B").executeAsync();
        sqlRes = asyncSqlRes.get();
        asyncSqlRes = this.session.sql("insert into testFetchOneFetchAllAsync values(?,?,?,?)").bind(3, 13).bind(23, "C").executeAsync();
        sqlRes = asyncSqlRes.get();
        asyncSqlRes = this.session.sql("insert into testFetchOneFetchAllAsync values(?,?,?,?)").bind(4, 14).bind(23, "D").executeAsync();
        sqlRes = asyncSqlRes.get();
        // With FetchOne()
        asyncSqlRes = this.session.sql("select * from testFetchOneFetchAllAsync where a<=? order by a asc").bind(5).executeAsync();
        SqlResult sqlRes1 = asyncSqlRes.get();
        int i = 0;
        while (sqlRes1.hasNext()) {
            r = sqlRes1.fetchOne();
            assertEquals((long) (i + 1), r.getInt(0));
            i++;
        }
        assertThrows(WrongArgumentException.class, "Cannot fetchAll\\(\\) after starting iteration", () -> sqlRes1.fetchAll());
        asyncSqlRes = this.session.sql("select * from testFetchOneFetchAllAsync where a<=? order by a asc").bind(3).executeAsync();
        sqlRes = asyncSqlRes.get();
        rowList = sqlRes.fetchAll();
        assertEquals((long) 3, (long) rowList.size());
        for (i = 0; i < rowList.size(); i++) {
            r = rowList.get(i);
            assertEquals((long) (i + 1), r.getInt(0));
            i++;
        }
    } finally {
        sqlUpdate("drop table if exists testFetchOneFetchAllAsync");
    }
}
Also used : SqlResult(com.mysql.cj.xdevapi.SqlResult) Row(com.mysql.cj.xdevapi.Row) Test(org.junit.jupiter.api.Test)

Example 10 with SqlResult

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

the class SessionTest method sqlArguments.

@Test
public void sqlArguments() {
    SqlStatement stmt = this.session.sql("select ? as a, 40 + ? as b, ? as c");
    SqlResult res = stmt.bind(1).bind(2).bind(3).execute();
    Row r = res.next();
    assertEquals("1", r.getString("a"));
    assertEquals("42", r.getString("b"));
    assertEquals("3", r.getString("c"));
}
Also used : SqlStatement(com.mysql.cj.xdevapi.SqlStatement) SqlResult(com.mysql.cj.xdevapi.SqlResult) Row(com.mysql.cj.xdevapi.Row) Test(org.junit.jupiter.api.Test)

Aggregations

SqlResult (com.mysql.cj.xdevapi.SqlResult)39 Test (org.junit.jupiter.api.Test)27 Row (com.mysql.cj.xdevapi.Row)17 JsonString (com.mysql.cj.xdevapi.JsonString)11 Session (com.mysql.cj.xdevapi.Session)8 ExecutionException (java.util.concurrent.ExecutionException)7 WrongArgumentException (com.mysql.cj.exceptions.WrongArgumentException)6 SqlStatement (com.mysql.cj.xdevapi.SqlStatement)6 CoreSession (com.mysql.cj.CoreSession)5 CJCommunicationsException (com.mysql.cj.exceptions.CJCommunicationsException)4 StatementExecuteOkBuilder (com.mysql.cj.protocol.x.StatementExecuteOkBuilder)4 SqlResultBuilder (com.mysql.cj.xdevapi.SqlResultBuilder)4 ArrayList (java.util.ArrayList)4 CJPacketTooBigException (com.mysql.cj.exceptions.CJPacketTooBigException)3 FeatureNotAvailableException (com.mysql.cj.exceptions.FeatureNotAvailableException)3 XProtocolRowInputStream (com.mysql.cj.protocol.x.XProtocolRowInputStream)3 DbDoc (com.mysql.cj.xdevapi.DbDoc)3 DbDocImpl (com.mysql.cj.xdevapi.DbDocImpl)3 DocResult (com.mysql.cj.xdevapi.DocResult)3 SessionFactory (com.mysql.cj.xdevapi.SessionFactory)3