Search in sources :

Example 21 with SqlResult

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

the class CollectionTest method testCollectionAddModifyRemoveAsync.

@SuppressWarnings({ "deprecation", "unchecked" })
@Test
public void testCollectionAddModifyRemoveAsync() throws Exception {
    int i = 0;
    int NUMBER_OF_QUERIES = 5000;
    DbDoc doc = null;
    AddResult res = null;
    SqlResult res1 = null;
    CompletableFuture<DocResult> asyncDocs = null;
    DocResult docs = null;
    DbDoc newDoc1 = new DbDocImpl();
    newDoc1.add("_id", new JsonString().setValue(String.valueOf(1000)));
    newDoc1.add("F1", new JsonString().setValue("Field-1-Data-1"));
    newDoc1.add("F2", new JsonNumber().setValue(String.valueOf(1000)));
    newDoc1.add("T", new JsonNumber().setValue(String.valueOf(10)));
    DbDoc newDoc2 = new DbDocImpl();
    newDoc2.add("_id", new JsonString().setValue(String.valueOf(1000)));
    // add(), modify(),find(),  remove() Success And Failure
    List<Object> futures = new ArrayList<>();
    for (i = 0; i < NUMBER_OF_QUERIES; ++i) {
        if (i % 10 == 0) {
            futures.add(this.collection.add(newDoc1).executeAsync());
        } else if (i % 10 == 1) {
            // Error
            futures.add(this.collection.find("NON_EXISTING_FUNCTION1()").fields("$._id as _id, $.F1 as F1, $.F2 as F2, $.F3 as F3").executeAsync());
        } else if (i % 10 == 2) {
            futures.add(this.collection.find("$.F2 = 1000").fields("$._id as _id, $.F1 as F1, $.F2 as F2, $.T as T").executeAsync());
        } else if (i % 10 == 3) {
            // Error
            futures.add(this.collection.add(newDoc2).executeAsync());
        } else if (i % 10 == 4) {
            futures.add(this.collection.modify("$.F2 = 1000").change("$.T", expr("$.T+1")).sort("$.F3 desc").executeAsync());
        } else if (i % 10 == 5) {
            // Error
            futures.add(this.collection.modify("$.F2 = 1000").change("$.T", expr("NON_EXISTING_FUNCTION2()")).sort("$.F3 desc").executeAsync());
        } else if (i % 10 == 6) {
            // Error
            futures.add(this.collection.remove("NON_EXISTING_FUNCTION3()=:PARAM").bind("PARAM", 1000).orderBy("$.F3 desc").executeAsync());
        } else if (i % 10 == 7) {
            futures.add(this.session.sql("SELECT JSON_OBJECT('_id', JSON_EXTRACT(doc,'$._id'),'F1', JSON_EXTRACT(doc,'$.F1'),'F2', JSON_EXTRACT(doc,'$.F2'),'T', JSON_EXTRACT(doc,'$.T')) AS doc FROM `" + this.collectionName + "` WHERE (JSON_EXTRACT(doc,'$.F2') = 1000) ").executeAsync());
        } else if (i % 10 == 8) {
            // Error
            futures.add(this.session.sql("select non_existingfun() /* loop : " + i + "*/").executeAsync());
        } else {
            futures.add(this.collection.remove("$.F2 = :PARAM").bind("PARAM", 1000).orderBy("$.F3 desc").executeAsync());
        }
    }
    for (i = 0; i < NUMBER_OF_QUERIES; ++i) {
        int i1 = i;
        if (i % 10 == 0) {
            res = ((CompletableFuture<AddResult>) futures.get(i)).get();
            assertEquals(1, res.getAffectedItemsCount());
        } else if (i % 10 == 1) {
            assertThrows(ExecutionException.class, ".*FUNCTION " + this.schema.getName() + ".NON_EXISTING_FUNCTION1 does not exist.*", () -> ((CompletableFuture<DocResult>) futures.get(i1)).get());
        } else if (i % 10 == 2) {
            docs = ((CompletableFuture<DocResult>) futures.get(i)).get();
            assertTrue(docs.hasNext());
            doc = docs.next();
            assertEquals((long) (10), (long) (((JsonNumber) doc.get("T")).getInteger()));
            assertFalse(docs.hasNext());
        } else if (i % 10 == 3) {
            assertThrows(ExecutionException.class, ".*Document contains a field value that is not unique but required to be.*", () -> ((CompletableFuture<AddResult>) futures.get(i1)).get());
        } else if (i % 10 == 4) {
            Result res2 = ((CompletableFuture<AddResult>) futures.get(i)).get();
            assertEquals(1, res2.getAffectedItemsCount());
        } else if (i % 10 == 5) {
            assertThrows(ExecutionException.class, ".*FUNCTION " + this.schema.getName() + ".NON_EXISTING_FUNCTION2 does not exist.*", () -> ((CompletableFuture<Result>) futures.get(i1)).get());
        } else if (i % 10 == 6) {
            assertThrows(ExecutionException.class, ".*FUNCTION " + this.schema.getName() + ".NON_EXISTING_FUNCTION3 does not exist.*", () -> ((CompletableFuture<Result>) futures.get(i1)).get());
        } else if (i % 10 == 7) {
            res1 = ((CompletableFuture<SqlResult>) futures.get(i)).get();
            res1.next();
            assertFalse(res1.hasNext());
        } else if (i % 10 == 8) {
            assertThrows(ExecutionException.class, ".*FUNCTION " + this.schema.getName() + ".non_existingfun does not exist.*", () -> ((CompletableFuture<SqlResult>) futures.get(i1)).get());
        } else {
            Result res2 = ((CompletableFuture<AddResult>) futures.get(i)).get();
            assertEquals(1, res2.getAffectedItemsCount());
        }
    }
    if ((NUMBER_OF_QUERIES - 1) % 10 < 9) {
        assertEquals((1), this.collection.count());
        asyncDocs = this.collection.find("$.T = :X ").bind("X", 10).fields(expr("{'cnt':count($.T)}")).executeAsync();
        docs = asyncDocs.get();
        doc = docs.next();
        assertEquals((long) (1), (long) (((JsonNumber) doc.get("cnt")).getInteger()));
    } else {
        assertEquals((0), this.collection.count());
    }
}
Also used : SqlResult(com.mysql.cj.xdevapi.SqlResult) ArrayList(java.util.ArrayList) AddResult(com.mysql.cj.xdevapi.AddResult) RowResult(com.mysql.cj.xdevapi.RowResult) Result(com.mysql.cj.xdevapi.Result) AddResult(com.mysql.cj.xdevapi.AddResult) DocResult(com.mysql.cj.xdevapi.DocResult) SqlResult(com.mysql.cj.xdevapi.SqlResult) DbDoc(com.mysql.cj.xdevapi.DbDoc) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) CompletableFuture(java.util.concurrent.CompletableFuture) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) JsonString(com.mysql.cj.xdevapi.JsonString) ExecutionException(java.util.concurrent.ExecutionException) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 22 with SqlResult

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

the class DevApiBaseTestCase method setupTestSession.

public boolean setupTestSession() {
    if (this.isSetForXTests) {
        this.session = new SessionImpl(this.testHostInfo);
        this.schema = this.session.getDefaultSchema();
        SqlResult rs = this.session.sql("SHOW VARIABLES LIKE 'character_set_database'").execute();
        this.dbCharset = rs.fetchOne().getString(1);
        rs = this.session.sql("SHOW VARIABLES LIKE 'collation_database'").execute();
        this.dbCollation = rs.fetchOne().getString(1);
        // ensure max_connections value is enough to run tests
        int maxConnections = 0;
        int mysqlxMaxConnections = 0;
        rs = this.session.sql("SHOW VARIABLES LIKE '%max_connections'").execute();
        Row r = rs.fetchOne();
        if (r.getString(0).contains("mysqlx")) {
            mysqlxMaxConnections = r.getInt(1);
            maxConnections = rs.fetchOne().getInt(1);
        } else {
            maxConnections = r.getInt(1);
            mysqlxMaxConnections = rs.fetchOne().getInt(1);
        }
        rs = this.session.sql("show status like 'threads_connected'").execute();
        int usedConnections = rs.fetchOne().getInt(1);
        if (maxConnections - usedConnections < 200) {
            maxConnections += 200;
            this.session.sql("SET GLOBAL max_connections=" + maxConnections).execute();
            if (mysqlxMaxConnections < maxConnections) {
                this.session.sql("SET GLOBAL mysqlx_max_connections=" + maxConnections).execute();
            }
        }
        return true;
    }
    return false;
}
Also used : SqlResult(com.mysql.cj.xdevapi.SqlResult) SessionImpl(com.mysql.cj.xdevapi.SessionImpl) Row(com.mysql.cj.xdevapi.Row)

Example 23 with SqlResult

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

the class DevApiBaseTestCase method isMysqlRunningLocally.

protected boolean isMysqlRunningLocally() {
    if (this.mysqlRunningLocally != null) {
        return this.mysqlRunningLocally;
    }
    try {
        String clientHostname = InetAddress.getLocalHost().getHostName();
        SqlResult res = this.session.sql("SHOW VARIABLES LIKE 'hostname'").execute();
        String serverHostname = res.fetchOne().getString(1);
        this.mysqlRunningLocally = clientHostname.equalsIgnoreCase(serverHostname);
    } catch (UnknownHostException e) {
        this.mysqlRunningLocally = false;
    }
    return this.mysqlRunningLocally;
}
Also used : SqlResult(com.mysql.cj.xdevapi.SqlResult) UnknownHostException(java.net.UnknownHostException)

Example 24 with SqlResult

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

the class DevApiBaseTestCase method assertSecureSession.

protected static void assertSecureSession(Session sess, String user) {
    assertSecureSession(sess);
    SqlResult res = sess.sql("SELECT CURRENT_USER()").execute();
    assertEquals(user, res.fetchOne().getString(0).split("@")[0]);
}
Also used : SqlResult(com.mysql.cj.xdevapi.SqlResult)

Example 25 with SqlResult

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

the class MetadataTest method testGetColumnInfoFromnSession.

@Test
public void testGetColumnInfoFromnSession() throws Exception {
    Column myCol = null;
    List<Column> metadata = null;
    try {
        sqlUpdate("drop table if exists xyz");
        sqlUpdate("create table xyz (i int auto_increment primary key,j bigint not null,k tinyint unsigned unique,l char(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_spanish2_ci,m decimal(16,10),key(l))");
        sqlUpdate("insert into xyz (j,k,l,m) values (1000,1,'a',10.12)");
        sqlUpdate("drop database if exists qadatabase");
        sqlUpdate("create database qadatabase");
        sqlUpdate("create table qadatabase.xyz (d date)");
        sqlUpdate("insert into qadatabase.xyz values ('2016-03-07')");
        SqlResult sRes = this.session.sql("select * from  " + this.schema.getName() + ".xyz , qadatabase.xyz mytable").execute();
        metadata = sRes.getColumns();
        assertEquals(6, metadata.size());
        for (int i = 0; i < metadata.size(); i++) {
            myCol = metadata.get(i);
        }
        myCol = metadata.get(0);
        assertEquals("i", myCol.getColumnName());
        assertEquals("i", myCol.getColumnLabel());
        assertEquals(this.schema.getName(), myCol.getSchemaName());
        assertEquals("xyz", myCol.getTableName());
        assertEquals("xyz", myCol.getTableLabel());
        assertEquals(Type.INT, myCol.getType());
        assertEquals(11, myCol.getLength());
        assertEquals(0, myCol.getFractionalDigits());
        assertEquals(true, myCol.isNumberSigned());
        assertEquals(null, myCol.getCollationName());
        assertEquals(null, myCol.getCharacterSetName());
        assertEquals(false, myCol.isPadded());
        assertEquals(false, myCol.isNullable());
        assertEquals(true, myCol.isAutoIncrement());
        assertEquals(true, myCol.isPrimaryKey());
        assertEquals(false, myCol.isUniqueKey());
        assertEquals(false, myCol.isPartKey());
        myCol = metadata.get(1);
        assertEquals("j", myCol.getColumnName());
        assertEquals("j", myCol.getColumnLabel());
        assertEquals(this.schema.getName(), myCol.getSchemaName());
        assertEquals("xyz", myCol.getTableName());
        assertEquals("xyz", myCol.getTableLabel());
        assertEquals(Type.BIGINT, myCol.getType());
        assertEquals(20, myCol.getLength());
        assertEquals(0, myCol.getFractionalDigits());
        assertEquals(true, myCol.isNumberSigned());
        assertEquals(null, myCol.getCollationName());
        assertEquals(null, myCol.getCharacterSetName());
        assertEquals(false, myCol.isPadded());
        assertEquals(false, myCol.isNullable());
        assertEquals(false, myCol.isAutoIncrement());
        assertEquals(false, myCol.isPrimaryKey());
        assertEquals(false, myCol.isUniqueKey());
        assertEquals(false, myCol.isPartKey());
        myCol = metadata.get(2);
        assertEquals("k", myCol.getColumnName());
        assertEquals("k", myCol.getColumnLabel());
        assertEquals(this.schema.getName(), myCol.getSchemaName());
        assertEquals("xyz", myCol.getTableName());
        assertEquals("xyz", myCol.getTableLabel());
        assertEquals(Type.TINYINT, myCol.getType());
        assertEquals(3, myCol.getLength());
        assertEquals(0, myCol.getFractionalDigits());
        assertEquals(false, myCol.isNumberSigned());
        assertEquals(null, myCol.getCollationName());
        assertEquals(null, myCol.getCharacterSetName());
        assertEquals(false, myCol.isPadded());
        assertEquals(true, myCol.isNullable());
        assertEquals(false, myCol.isAutoIncrement());
        assertEquals(false, myCol.isPrimaryKey());
        assertEquals(true, myCol.isUniqueKey());
        assertEquals(false, myCol.isPartKey());
        myCol = metadata.get(3);
        assertEquals("l", myCol.getColumnName());
        assertEquals("l", myCol.getColumnLabel());
        assertEquals(this.schema.getName(), myCol.getSchemaName());
        assertEquals("xyz", myCol.getTableName());
        assertEquals("xyz", myCol.getTableLabel());
        assertEquals(Type.STRING, myCol.getType());
        assertEquals(800, myCol.getLength());
        assertEquals(0, myCol.getFractionalDigits());
        assertEquals(false, myCol.isNumberSigned());
        assertEquals("utf8mb4_spanish2_ci", myCol.getCollationName());
        assertEquals("utf8mb4", myCol.getCharacterSetName());
        assertEquals(true, myCol.isPadded());
        assertEquals(true, myCol.isNullable());
        assertEquals(false, myCol.isAutoIncrement());
        assertEquals(false, myCol.isPrimaryKey());
        assertEquals(false, myCol.isUniqueKey());
        assertEquals(true, myCol.isPartKey());
        myCol = metadata.get(4);
        assertEquals("m", myCol.getColumnName());
        assertEquals("m", myCol.getColumnLabel());
        assertEquals(this.schema.getName(), myCol.getSchemaName());
        assertEquals("xyz", myCol.getTableName());
        assertEquals("xyz", myCol.getTableLabel());
        assertEquals(Type.DECIMAL, myCol.getType());
        assertEquals(18, myCol.getLength());
        assertEquals(10, myCol.getFractionalDigits());
        assertEquals(true, myCol.isNumberSigned());
        assertEquals(null, myCol.getCollationName());
        assertEquals(null, myCol.getCharacterSetName());
        assertEquals(false, myCol.isPadded());
        assertEquals(true, myCol.isNullable());
        assertEquals(false, myCol.isAutoIncrement());
        assertEquals(false, myCol.isPrimaryKey());
        assertEquals(false, myCol.isUniqueKey());
        assertEquals(false, myCol.isPartKey());
        myCol = metadata.get(5);
        assertEquals("d", myCol.getColumnName());
        assertEquals("d", myCol.getColumnLabel());
        assertEquals("qadatabase", myCol.getSchemaName());
        assertEquals("xyz", myCol.getTableName());
        assertEquals("mytable", myCol.getTableLabel());
        assertEquals(Type.DATE, myCol.getType());
        assertEquals(10, myCol.getLength());
        assertEquals(0, myCol.getFractionalDigits());
        assertEquals(false, myCol.isNumberSigned());
        assertEquals(null, myCol.getCollationName());
        assertEquals(null, myCol.getCharacterSetName());
        assertEquals(false, myCol.isPadded());
        assertEquals(true, myCol.isNullable());
        assertEquals(false, myCol.isAutoIncrement());
        assertEquals(false, myCol.isPrimaryKey());
        assertEquals(false, myCol.isUniqueKey());
        assertEquals(false, myCol.isPartKey());
    } finally {
        sqlUpdate("drop table if exists xyz");
        sqlUpdate("drop database if exists qadatabase");
    }
}
Also used : SqlResult(com.mysql.cj.xdevapi.SqlResult) Column(com.mysql.cj.xdevapi.Column) 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