Skip to content

Commit

Permalink
[CONJ-254] boolean retrieve correction / improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Feb 19, 2016
1 parent 012b8c3 commit 16ad037
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 42 deletions.
Expand Up @@ -1246,12 +1246,39 @@ public InputStream getBinaryInputStream() {
* Get boolean value from raw data.
* @return boolean
*/
public boolean getBoolean() {
public boolean getBoolean() throws SQLException {
if (rawBytes == null) {
return false;
}
final String rawVal = new String(rawBytes, StandardCharsets.UTF_8);
return rawVal.equalsIgnoreCase("true") || rawVal.equals("1") || (rawBytes[0] & 0xff) == 1;
if (!this.isBinaryEncoded) {
if (rawBytes.length == 1 && rawBytes[0] == 0) {
return false;
}
final String rawVal = new String(rawBytes, StandardCharsets.UTF_8);
return !("false".equals(rawVal) || "0".equals(rawVal));
} else {
switch (dataType) {
case BIT:
return rawBytes[0] != 0;
case TINYINT:
return getTinyInt() != 0;
case SMALLINT:
case YEAR:
return getSmallInt() != 0;
case INTEGER:
case MEDIUMINT:
return getMediumInt() != 0;
case BIGINT:
return getLong() != 0;
case FLOAT:
return getFloat() != 0;
case DOUBLE:
return getDouble() != 0;
default:
final String rawVal = new String(rawBytes, StandardCharsets.UTF_8);
return !("false".equals(rawVal) || "0".equals(rawVal));
}
}
}

/**
Expand Down Expand Up @@ -1317,12 +1344,16 @@ public Object getObject(int dataTypeMappingFlags, Calendar cal) throws SQLExcept
switch (dataType) {
case BIT:
if (columnInfo.getLength() == 1) {
return (getBytes()[0] != 0);
return rawBytes[0] != 0;
}
return getBytes();
return rawBytes;
case TINYINT:
if ((dataTypeMappingFlags & TINYINT1_IS_BIT) != 0 && columnInfo.getLength() == 1) {
return (getBytes()[0] != '0');
if (options.tinyInt1isBit && columnInfo.getLength() == 1) {
if (!this.isBinaryEncoded) {
return rawBytes[0] != '0';
} else {
return rawBytes[0] != 0;
}
}
return getInt();
case INTEGER:
Expand Down
Expand Up @@ -47,7 +47,7 @@ public interface ValueObject {

Timestamp getTimestamp(Calendar cal) throws ParseException;

boolean getBoolean();
boolean getBoolean() throws SQLException;

boolean isNull();

Expand Down
Expand Up @@ -151,7 +151,7 @@ public void sendStream(InputStream is, long readLength) throws IOException {
* @param reader reader to send
* @throws IOException if any error occur during data send to server
*/
public void sendStream(java.io.Reader reader) throws IOException {
public void sendStream(Reader reader) throws IOException {
char[] buffer = new char[8192];
int len;
while ((len = reader.read(buffer)) > 0) {
Expand All @@ -166,7 +166,7 @@ public void sendStream(java.io.Reader reader) throws IOException {
* @param readLength max size to send
* @throws IOException if any error occur during data send to server
*/
public void sendStream(java.io.Reader reader, long readLength) throws IOException {
public void sendStream(Reader reader, long readLength) throws IOException {
char[] buffer = new char[8192];
long remainingReadLength = readLength;
int read;
Expand Down
51 changes: 45 additions & 6 deletions src/test/java/org/mariadb/jdbc/BooleanTest.java
Expand Up @@ -4,9 +4,7 @@
import org.junit.BeforeClass;
import org.junit.Test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand All @@ -23,7 +21,8 @@ public class BooleanTest extends BaseTest {
public static void initClass() throws SQLException {
createTable("booleantest", "id int not null primary key auto_increment, test boolean");
createTable("booleanvalue", "test boolean");

createTable("booleanAllField", "t1 BIT, t2 TINYINT(1), t3 SMALLINT(1), t4 MEDIUMINT(1), t5 INT(1), t6 BIGINT(1), t7 DECIMAL(1), t8 FLOAT, "
+ "t9 DOUBLE, t10 CHAR(1), t11 VARCHAR(1), t12 BINARY(1), t13 BLOB(1), t14 TEXT(1)");
}

@Test
Expand All @@ -42,7 +41,6 @@ public void testBoolean() throws SQLException {
} else {
fail("must have a result !");
}

}

@Test
Expand All @@ -60,7 +58,7 @@ public void testBooleanString() throws SQLException {
assertFalse(rs.getBoolean(1));
assertEquals("0", rs.getString(1));
if (rs.next()) {
assertFalse(rs.getBoolean(1));
assertTrue(rs.getBoolean(1));
assertEquals("4", rs.getString(1));
} else {
fail("must have a result !");
Expand All @@ -72,4 +70,45 @@ public void testBooleanString() throws SQLException {
fail("must have a result !");
}
}


/**
* CONJ-254 error when using scala anorm string interpolation.
*
* @throws SQLException exception
*/
@Test
public void testBooleanAllField() throws Exception {
try (Connection connection = setConnection("&maxPerformance=true")) {
Statement stmt = connection.createStatement();
stmt.execute("INSERT INTO booleanAllField VALUES (null, null, null, null, null, null, null, null, null, null, null, null, null, null)");
stmt.execute("INSERT INTO booleanAllField VALUES (0, 0, 0, 0, 0, 0, 0, 0, 0, '0', '0', '0', '0', '0')");
stmt.execute("INSERT INTO booleanAllField VALUES (1, 1, 1, 1, 1, 1, 1, 1, 1, '1', '1', '1', '1', '1')");
stmt.execute("INSERT INTO booleanAllField VALUES (1, 2, 2, 2, 2, 2, 2, 2, 2, '2', '2', '2', '2', '2')");

ResultSet rs = stmt.executeQuery("SELECT * FROM booleanAllField");
checkBooleanValue(rs, false, null);
checkBooleanValue(rs, false, false);
checkBooleanValue(rs, true, true);
checkBooleanValue(rs, true, true);

PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM booleanAllField WHERE 1 = ?");
preparedStatement.setInt(1, 1);
rs = preparedStatement.executeQuery();
checkBooleanValue(rs, false, null);
checkBooleanValue(rs, false, false);
checkBooleanValue(rs, true, true);
checkBooleanValue(rs, true, true);
}
}

private void checkBooleanValue(ResultSet rs, boolean expectedValue, Boolean expectedNull) throws SQLException {
rs.next();
for (int i = 1; i <= 14; i++ ) {
assertEquals(expectedValue, rs.getBoolean(i));
if (i == 1 || i == 2) {
assertEquals(expectedNull, rs.getObject(i));
}
}
}
}
50 changes: 27 additions & 23 deletions src/test/java/org/mariadb/jdbc/CollationTest.java
Expand Up @@ -71,33 +71,37 @@ public void test4BytesUtf8() throws Exception {

String sqlForCharset = "select @@character_set_server";
ResultSet rs = sharedConnection.createStatement().executeQuery(sqlForCharset);
assertTrue(rs.next());
String emoji = "\uD83C\uDF1F";
boolean mustThrowError = true;
if ("utf8mb4".equals(rs.getString(1))) {
mustThrowError = false;
}
if (rs.next()) {
String emoji = "\uD83C\uDF1F";
boolean mustThrowError = true;
if ("utf8mb4".equals(rs.getString(1))) {
mustThrowError = false;
}

PreparedStatement ps = sharedConnection.prepareStatement("INSERT INTO unicodeTestChar (id, field1, field2) VALUES (1, ?, ?)");
ps.setString(1, emoji);
Reader reader = new StringReader(emoji);
ps.setCharacterStream(2, reader);
try {
ps.execute();
ps = sharedConnection.prepareStatement("SELECT field1, field2 FROM unicodeTestChar");
rs = ps.executeQuery();
assertTrue(rs.next());
PreparedStatement ps = sharedConnection.prepareStatement("INSERT INTO unicodeTestChar (id, field1, field2) VALUES (1, ?, ?)");
ps.setString(1, emoji);
Reader reader = new StringReader(emoji);
ps.setCharacterStream(2, reader);
try {
ps.execute();
ps = sharedConnection.prepareStatement("SELECT field1, field2 FROM unicodeTestChar");
rs = ps.executeQuery();
assertTrue(rs.next());

// compare to the Java representation of UTF32
assertEquals(4, rs.getBytes(1).length);
assertEquals(emoji, rs.getString(1));
// compare to the Java representation of UTF32
assertEquals(4, rs.getBytes(1).length);
assertEquals(emoji, rs.getString(1));

assertEquals(4, rs.getBytes(2).length);
assertEquals(emoji, rs.getString(2));
} catch (BatchUpdateException b) {
if (!mustThrowError) {
fail("Must not have thrown error");
assertEquals(4, rs.getBytes(2).length);
assertEquals(emoji, rs.getString(2));
} catch (BatchUpdateException b) {
if (!mustThrowError) {
fail("Must not have thrown error");
}
}
} else {
fail();
}

}
}
2 changes: 1 addition & 1 deletion src/test/java/org/mariadb/jdbc/DataTypeSignedTest.java
Expand Up @@ -377,7 +377,7 @@ private void nullNegativeTest(ResultSet rs, boolean decimal) throws SQLException

if (rs.next()) {
try {
assertFalse(rs.getBoolean(1));
assertTrue(rs.getBoolean(1));
assertFalse(rs.wasNull());
assertEquals(-1, rs.getByte(1));
assertEquals(-1, rs.getShort(1));
Expand Down
7 changes: 5 additions & 2 deletions src/test/java/org/mariadb/jdbc/DatatypeTest.java
Expand Up @@ -51,8 +51,11 @@ public void checkSupported() throws Exception {

void checkClass(String column, Class<?> clazz, String mysqlType, int javaSqlType) throws Exception {
int index = resultSet.findColumn(column);

if (resultSet.getObject(column) != null) {
Object obj = resultSet.getObject(column);
if (obj != null) {
if (!clazz.equals(obj.getClass())) {
System.out.println("test");
}
assertEquals("Unexpected class for column " + column, clazz, resultSet.getObject(column).getClass());
}
assertEquals("Unexpected class name for column " + column, clazz.getName(),
Expand Down

0 comments on commit 16ad037

Please sign in to comment.