Skip to content

Commit

Permalink
Fix for Bug#28924137, WL#12463:IF COLLECTION DOESN'T EXIST, COLL.COUNT()
Browse files Browse the repository at this point in the history
IS GIVING A WRONG ERROR MESSAGE.
  • Loading branch information
soklakov committed Nov 22, 2018
1 parent 8cc52c2 commit b457e0c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

Version 8.0.14

- Fix for Bug#28924137, WL#12463:IF COLLECTION DOESN'T EXIST, COLL.COUNT() IS GIVING A WRONG ERROR MESSAGE.

- WL#12463, DevAPI: Standardize count method.

- Fix for Bug#92508 (28747636), mysql-connector in bootclasspath causing memory leak.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,14 @@ public void dropIndex(String indexName) {
}

public long count() {
return this.mysqlxSession.getDataStoreMetadata().getTableRowCount(this.schema.getName(), this.name);
try {
return this.mysqlxSession.getDataStoreMetadata().getTableRowCount(this.schema.getName(), this.name);
} catch (XProtocolError e) {
if (e.getErrorCode() == 1146) {
throw new XProtocolError("Collection '" + this.name + "' does not exist in schema '" + this.schema.getName() + "'", e);
}
throw e;
}
}

public DbDoc newDoc() {
Expand Down
10 changes: 9 additions & 1 deletion src/main/user-impl/java/com/mysql/cj/xdevapi/TableImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.mysql.cj.MysqlxSession;
import com.mysql.cj.protocol.x.XMessage;
import com.mysql.cj.protocol.x.XMessageBuilder;
import com.mysql.cj.protocol.x.XProtocolError;
import com.mysql.cj.result.Row;
import com.mysql.cj.result.StringValueFactory;
import com.mysql.cj.result.ValueFactory;
Expand Down Expand Up @@ -114,7 +115,14 @@ public DeleteStatement delete() {
}

public long count() {
return this.mysqlxSession.getDataStoreMetadata().getTableRowCount(this.schema.getName(), this.name);
try {
return this.mysqlxSession.getDataStoreMetadata().getTableRowCount(this.schema.getName(), this.name);
} catch (XProtocolError e) {
if (e.getErrorCode() == 1146) {
throw new XProtocolError("Table '" + this.name + "' does not exist in schema '" + this.schema.getName() + "'", e);
}
throw e;
}
}

@Override
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/testsuite/x/devapi/CollectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ public void testCount() {
}
assertEquals(3, this.collection.count());
assertEquals(3, ((JsonNumber) this.collection.find().fields("COUNT(*) as cnt").execute().fetchOne().get("cnt")).getInteger().intValue());

// test "not exists" message
String collName = "testExists";
dropCollection(collName);
Collection coll = this.schema.getCollection(collName);
assertThrows(XProtocolError.class, "Collection '" + collName + "' does not exist in schema '" + this.schema.getName() + "'", new Callable<Void>() {
public Void call() throws Exception {
coll.count();
return null;
}
});
}

@Test
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/testsuite/x/devapi/TableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.concurrent.Callable;

import org.junit.Test;

import com.mysql.cj.protocol.x.XProtocolError;
import com.mysql.cj.xdevapi.DatabaseObject.DbObjectStatus;
import com.mysql.cj.xdevapi.Table;

Expand Down Expand Up @@ -133,5 +136,16 @@ public void testCount() {
} finally {
sqlUpdate("drop table if exists testCount");
}

// test "not exists" message
String tableName = "testExists";
dropCollection(tableName);
Table t = this.schema.getTable(tableName);
assertThrows(XProtocolError.class, "Table '" + tableName + "' does not exist in schema '" + this.schema.getName() + "'", new Callable<Void>() {
public Void call() throws Exception {
t.count();
return null;
}
});
}
}

0 comments on commit b457e0c

Please sign in to comment.