Search in sources :

Example 16 with SqlResult

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

the class CollectionTest method validateArrayIndex.

private void validateArrayIndex(String keydName, String collName, int noFields) throws Exception {
    int indexFound = 0;
    boolean arrayExpr = false;
    Session sess = null;
    try {
        sess = new SessionFactory().getSession(this.baseUrl);
        SqlResult res = sess.sql("show index from `" + collName + "`").execute();
        assertTrue(res.hasNext());
        for (Row row : res.fetchAll()) {
            if (keydName.equals(row.getString("Key_name"))) {
                indexFound++;
                assertEquals(collName, row.getString("Table"));
                String expr = row.getString("Expression");
                System.out.println(expr);
                if (expr != null) {
                    arrayExpr = true;
                }
            }
        }
    } finally {
        if (sess != null) {
            sess.close();
            sess = null;
        }
    }
    if ((indexFound != noFields) || (!arrayExpr)) {
        throw new Exception("Index not matching");
    }
}
Also used : SessionFactory(com.mysql.cj.xdevapi.SessionFactory) SqlResult(com.mysql.cj.xdevapi.SqlResult) Row(com.mysql.cj.xdevapi.Row) JsonString(com.mysql.cj.xdevapi.JsonString) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) ExecutionException(java.util.concurrent.ExecutionException) Session(com.mysql.cj.xdevapi.Session)

Example 17 with SqlResult

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

the class MetadataTest method testGetSchemaName.

@Test
public void testGetSchemaName() throws Exception {
    RowResult rows = null;
    Table table = null;
    try {
        sqlUpdate("drop table if exists qatable");
        sqlUpdate("create table qatable (_id varchar(32), a varchar(20), b date, c int)");
        sqlUpdate("insert into qatable values ('X', 'Abcd', '2016-03-07',10)");
        sqlUpdate("drop database if exists qadatabase");
        sqlUpdate("create database qadatabase");
        sqlUpdate("create table qadatabase.qatable (_id varchar(32), a varchar(20), b date, c int)");
        sqlUpdate("insert into qadatabase.qatable values ('X', 'Abcd', '2016-03-07',10)");
        table = this.schema.getTable("qatable");
        rows = table.select("_id, a, b, c").execute();
        List<Column> metadata = rows.getColumns();
        assertEquals(4, metadata.size());
        Column idCol = metadata.get(0);
        assertEquals(this.schema.getName(), idCol.getSchemaName());
        Column aCol = metadata.get(1);
        assertEquals(this.schema.getName(), aCol.getSchemaName());
        Column bCol = metadata.get(2);
        assertEquals(this.schema.getName(), bCol.getSchemaName());
        Column cCol = metadata.get(3);
        assertEquals(this.schema.getName(), cCol.getSchemaName());
        SqlResult sRes = this.session.sql("select * from  qadatabase.qatable").execute();
        metadata = sRes.getColumns();
        assertEquals(4, metadata.size());
        idCol = metadata.get(0);
        assertEquals("qadatabase", idCol.getSchemaName());
        aCol = metadata.get(1);
        assertEquals("qadatabase", aCol.getSchemaName());
        bCol = metadata.get(2);
        assertEquals("qadatabase", bCol.getSchemaName());
        cCol = metadata.get(3);
        assertEquals("qadatabase", cCol.getSchemaName());
    } finally {
        sqlUpdate("drop database if exists qadatabase");
    }
}
Also used : RowResult(com.mysql.cj.xdevapi.RowResult) Table(com.mysql.cj.xdevapi.Table) SqlResult(com.mysql.cj.xdevapi.SqlResult) Column(com.mysql.cj.xdevapi.Column) Test(org.junit.jupiter.api.Test)

Example 18 with SqlResult

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

the class SessionTest method testBug23721537.

@Test
public void testBug23721537() throws Exception {
    try {
        sqlUpdate("drop table if exists testBug23721537");
        sqlUpdate("create table testBug23721537 (id int, name varchar(20) not null)");
        sqlUpdate("insert into testBug23721537 values (0, 'a')");
        this.session.sql("drop procedure if exists newproc").execute();
        this.session.sql("create procedure newproc (in p1 int,in p2 char(20)) begin select 1; update testBug23721537 set name='b' where id=0; select 2; select 3; end;").execute();
        /* sync execution */
        SqlResult res1 = this.session.sql("call newproc(?,?)").bind(10).bind("X").execute();
        assertTrue(res1 instanceof SqlMultiResult);
        assertTrue(res1.hasData());
        assertTrue(res1.hasNext());
        Row r = res1.next();
        assertEquals(1, r.getInt(0));
        assertFalse(res1.hasNext());
        // res1 should finish streaming here
        SqlResult res2 = this.session.sql("select 10").execute();
        assertTrue(res1.nextResult());
        assertTrue(res1.hasData());
        assertTrue(res1.hasNext());
        r = res1.next();
        assertEquals(2, r.getInt(0));
        assertFalse(res1.hasNext());
        assertTrue(res1.nextResult());
        assertTrue(res1.hasData());
        assertTrue(res1.hasNext());
        r = res1.next();
        assertEquals(3, r.getInt(0));
        assertFalse(res1.hasNext());
        assertFalse(res1.nextResult());
        // 
        assertTrue(res2.hasData());
        assertTrue(res2.hasNext());
        r = res2.next();
        assertEquals(10, r.getInt(0));
        assertFalse(res2.hasNext());
        assertFalse(res2.nextResult());
        /* async execution */
        res1 = this.session.sql("call newproc(?,?)").bind(10).bind("X").executeAsync().get();
        assertTrue(res1.hasData());
        r = res1.next();
        assertEquals(1, r.getInt(0));
        assertFalse(res1.hasNext());
        // res1 should finish streaming here
        res2 = this.session.sql("select 10").executeAsync().get();
        assertTrue(res1.nextResult());
        assertTrue(res1.hasData());
        assertTrue(res1.hasNext());
        r = res1.next();
        assertEquals(2, r.getInt(0));
        assertFalse(res1.hasNext());
        assertTrue(res1.nextResult());
        assertTrue(res1.hasData());
        assertTrue(res1.hasNext());
        r = res1.next();
        assertEquals(3, r.getInt(0));
        assertFalse(res1.hasNext());
        assertFalse(res1.nextResult());
        // 
        assertTrue(res2.hasData());
        assertTrue(res2.hasNext());
        r = res2.next();
        assertEquals(10, r.getInt(0));
        assertFalse(res2.hasNext());
        assertFalse(res2.nextResult());
    } finally {
        sqlUpdate("drop table if exists testBug23721537");
        this.session.sql("drop procedure if exists newproc").execute();
    }
}
Also used : SqlResult(com.mysql.cj.xdevapi.SqlResult) SqlMultiResult(com.mysql.cj.xdevapi.SqlMultiResult) Row(com.mysql.cj.xdevapi.Row) Test(org.junit.jupiter.api.Test)

Example 19 with SqlResult

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

the class TableInsertTest method testGetAutoIncrementValueAsync.

@Test
public void testGetAutoIncrementValueAsync() throws Exception {
    try {
        SqlResult res = this.session.sql("drop table if exists mytab").executeAsync().get();
        res = this.session.sql("create table mytab (x bigint auto_increment primary key,y int)").executeAsync().get();
        res = this.session.sql("drop table if exists mytabtmp").executeAsync().get();
        res = this.session.sql("create table mytabtmp (x bigint,y int)").executeAsync().get();
        res = this.session.sql("insert into mytabtmp values(NULL,8)").executeAsync().get();
        res = this.session.sql("insert into mytabtmp values(1111,9)").executeAsync().get();
        res = this.session.sql("ALTER TABLE mytab AUTO_INCREMENT = 111").executeAsync().get();
        res = this.session.sql("insert into mytab values(NULL,1)").executeAsync().get();
        assertEquals((long) 111, (long) res.getAutoIncrementValue());
        res = this.session.sql("insert into mytab values(-100,2)").executeAsync().get();
        assertEquals((long) -100, (long) res.getAutoIncrementValue());
        res = this.session.sql("insert into mytab (y)values(3)").executeAsync().get();
        assertEquals((long) 112, (long) res.getAutoIncrementValue());
        res = this.session.sql("insert into mytab values(NULL,4),(NULL,5),(887,6),(NULL,7)").executeAsync().get();
        assertEquals((long) 113, (long) res.getAutoIncrementValue());
        res = this.session.sql("insert into mytab select * from mytabtmp").executeAsync().get();
        assertEquals((long) 889, (long) res.getAutoIncrementValue());
        res = this.session.sql("insert into mytab (y) select (y+1) from mytabtmp").executeAsync().get();
        assertEquals((long) 1112, (long) res.getAutoIncrementValue());
        // Ignore duplicate
        res = this.session.sql("insert IGNORE  mytab select * from mytabtmp").executeAsync().get();
        assertEquals((long) 1115, (long) res.getAutoIncrementValue());
        // ON DUPLICATE KEY
        res = this.session.sql("insert into mytab values(-100,2) ON DUPLICATE KEY UPDATE Y=Y*-1").executeAsync().get();
        assertEquals((long) -100, (long) res.getAutoIncrementValue());
        // ON DUPLICATE KEY
        res = this.session.sql("insert into mytab values(-100,2) ON DUPLICATE KEY UPDATE X=X*-2").executeAsync().get();
        assertEquals((long) 200, (long) res.getAutoIncrementValue());
        // Replace
        res = this.session.sql("Replace into mytab (y)values(100000)").executeAsync().get();
        assertEquals((long) 1116, (long) res.getAutoIncrementValue());
    } finally {
        sqlUpdate("drop table if exists mytabtmp");
        sqlUpdate("drop table if exists mytab");
    }
}
Also used : SqlResult(com.mysql.cj.xdevapi.SqlResult) Test(org.junit.jupiter.api.Test)

Example 20 with SqlResult

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

the class CollectionTest method validateIndex.

private void validateIndex(String keyName, String collName, int sequence, boolean unique, boolean required, boolean array, String dataType, boolean unsigned, Integer length) throws Exception {
    boolean indexFound = false;
    SqlResult res = this.session.sql("show index from `" + collName + "`").execute();
    assertTrue(res.hasNext());
    for (Row row : res.fetchAll()) {
        if (keyName.equals(row.getString("Key_name"))) {
            if (sequence != row.getInt("Seq_in_index")) {
                continue;
            }
            indexFound = true;
            assertEquals(collName.toUpperCase(), row.getString("Table").toUpperCase());
            assertEquals(unique ? "0" : "1", row.getString("Non_unique"));
            if (!array && row.getString("Column_name") != null) {
                String[] columnNameTokens = row.getString("Column_name").toString().split("_");
                assertEquals(dataType, unsigned ? columnNameTokens[1] + "_" + columnNameTokens[2] : columnNameTokens[1]);
            } else if (array && row.getString("Expression") != null) {
                String expr = row.getString("Expression");
                int typePos = expr.indexOf(" as ");
                assertTrue(typePos >= 0, "Not an array index?");
                expr = expr.substring(typePos + 4, expr.length() - 1);
                assertTrue(expr.endsWith("array"));
                expr = expr.substring(0, expr.indexOf(" array"));
                assertEquals(dataType, expr);
            } else {
                fail("Unexpected type of index");
            }
            // assertEquals("", row.getString("Collation")); // TODO enable when applicable
            assertEquals(length == null ? 0 : length.intValue(), row.getInt("Sub_part"));
            assertEquals(required ? "" : "YES", row.getString("Null"));
            break;
        }
    }
    if (!indexFound) {
        throw new Exception("Index not found.");
    }
}
Also used : SqlResult(com.mysql.cj.xdevapi.SqlResult) Row(com.mysql.cj.xdevapi.Row) JsonString(com.mysql.cj.xdevapi.JsonString) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) ExecutionException(java.util.concurrent.ExecutionException)

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