Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-333] ResultSet.getString() on a time column return NULL when va…
…lue=00:00:00 when option useServerPrepStmts is set to true
  • Loading branch information
rusher committed Aug 22, 2016
1 parent c7e8a79 commit 9fe880c
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -6,7 +6,7 @@
<artifactId>mariadb-java-client</artifactId>
<packaging>jar</packaging>
<name>mariadb-java-client</name>
<version>1.5.1-RC</version>
<version>1.5.2-SNAPSHOT</version>
<description>JDBC driver for MariaDB and MySQL</description>
<url>https://mariadb.com/kb/en/mariadb/about-mariadb-connector-j/</url>

Expand Down
Expand Up @@ -872,7 +872,7 @@ private String getString(byte[] rawBytes, ColumnInformation columnInfo, Calendar
case FLOAT:
return String.valueOf(getFloat(rawBytes, columnInfo));
case TIME:
return getTimeString(rawBytes);
return getTimeString(rawBytes, columnInfo);
case DATE:
if (isBinaryEncoded) {
try {
Expand Down Expand Up @@ -2917,16 +2917,25 @@ public void setReturnTableAlias(boolean returnTableAlias) {
this.returnTableAlias = returnTableAlias;
}

private String getTimeString(byte[] rawBytes) {
if (rawBytes == null || rawBytes.length == 0) {
return null;
private String getTimeString(byte[] rawBytes, ColumnInformation columnInfo) {
if (rawBytes == null) return null;
if (rawBytes.length == 0) {
// binary send 00:00:00 as 0.
if (columnInfo.getDecimals() == 0) {
return "00:00:00";
} else {
String value = "00:00:00.";
int decimal = columnInfo.getDecimals();
while (decimal-- > 0) value += "0";
return value;
}
}
String rawValue = new String(rawBytes, StandardCharsets.UTF_8);
if ("0000-00-00".equals(rawValue)) {
return null;
}
if (!this.isBinaryEncoded) {
if (!options.useLegacyDatetimeCode && rawValue.indexOf(".") > 0) {
if (options.maximizeMysqlCompatibility && options.useLegacyDatetimeCode && rawValue.indexOf(".") > 0) {
return rawValue.substring(0, rawValue.indexOf("."));
}
return rawValue;
Expand Down
Expand Up @@ -51,10 +51,10 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
package org.mariadb.jdbc.internal.util.constant;

public final class Version {
public static final String version = "1.5.1-RC";
public static final String version = "1.5.2-SNAPSHOT";
public static final int majorVersion = 1;
public static final int minorVersion = 5;
public static final int patchVersion = 1;
public static final String qualifier = "RC";
public static final int patchVersion = 2;
public static final String qualifier = "SNAPSHOT";

}
104 changes: 104 additions & 0 deletions src/test/java/org/mariadb/jdbc/DatatypeCompatibilityTest.java
@@ -1,5 +1,6 @@
package org.mariadb.jdbc;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

Expand All @@ -11,6 +12,8 @@
import static org.junit.Assert.*;

public class DatatypeCompatibilityTest extends BaseTest {
private static final String sql = "SELECT id, time_test FROM time_test;";

/**
* Initialization.
* @throws SQLException exception
Expand Down Expand Up @@ -56,6 +59,8 @@ public static void initClass() throws SQLException {
);
createTable("ytab", "y year");
createTable("maxcharlength", "maxcharlength char(1)", "character set utf8");
createTable("time_test", "ID int unsigned NOT NULL, time_test time(6), PRIMARY KEY (ID)", "engine=InnoDB");
sharedConnection.createStatement().execute("insert into time_test(id, time_test) values(1, '00:00:00'), (2, '00:00:00.123'), (3, null);");
}

@Test
Expand Down Expand Up @@ -155,4 +160,103 @@ public void timeAsTimestamp() throws Exception {
assertEquals(testTime, time);
}

/**
* Check Time getTime() answer using Statement.
*
* @param connection connection
* @throws SQLException if any error occur
*/
public void testStatementGetTime(Connection connection) throws SQLException {
try (Statement statement = connection.createStatement()) {
try (ResultSet resultSet = statement.executeQuery(sql)) {
Assert.assertTrue(resultSet.next());
Assert.assertEquals("00:00:00", "" + resultSet.getTime(2));
Assert.assertTrue(resultSet.next());
Assert.assertEquals("00:00:00", "" + resultSet.getTime(2));
Assert.assertTrue(resultSet.next());
Assert.assertNull(resultSet.getTime(2));
Assert.assertFalse(resultSet.next());
}
}
}

/**
* Check Time getString() answer using Statement.
*
* @param connection connection
* @throws SQLException if any error occur
*/
public void testStatementGetString(Connection connection) throws SQLException {
try (Statement statement = connection.createStatement()) {
try (ResultSet resultSet = statement.executeQuery(sql)) {
Assert.assertTrue(resultSet.next());
Assert.assertEquals("00:00:00.000000", resultSet.getString(2));
Assert.assertTrue(resultSet.next());
Assert.assertEquals("00:00:00.123000", resultSet.getString(2));
Assert.assertTrue(resultSet.next());
Assert.assertNull(resultSet.getString(2));
Assert.assertFalse(resultSet.next());
}
}
}

/**
* Check Time getTime() answer using prepareStatement.
*
* @param connection connection
* @throws SQLException if any error occur
*/
public void testPreparedStatementGetTime(Connection connection) throws SQLException {
try (Statement statement = connection.createStatement()) {
try (ResultSet resultSet = statement.executeQuery(sql)) {
Assert.assertTrue(resultSet.next());
Assert.assertEquals("00:00:00", "" + resultSet.getTime(2));
Assert.assertTrue(resultSet.next());
Assert.assertEquals("00:00:00", "" + resultSet.getTime(2));
Assert.assertTrue(resultSet.next());
Assert.assertNull(resultSet.getTime(2));
Assert.assertFalse(resultSet.next());
}
}
}

/**
* Check Time getString() answer using prepareStatement.
*
* @param connection connection
* @throws SQLException if any error occur
*/
public void testPreparedStatementGetString(Connection connection) throws SQLException {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
try (ResultSet resultSet = preparedStatement.executeQuery()) {
Assert.assertTrue(resultSet.next());
Assert.assertEquals("00:00:00.000000", resultSet.getString(2));
Assert.assertTrue(resultSet.next());
Assert.assertEquals("00:00:00.123000", resultSet.getString(2));
Assert.assertTrue(resultSet.next());
Assert.assertNull(resultSet.getString(2));
Assert.assertFalse(resultSet.next());
}
}
}

@Test
public void testTimePrepareStatement() throws SQLException {
try (Connection connection = setConnection("&useServerPrepStmts=true")) {
testStatementGetTime(connection);
testPreparedStatementGetTime(connection);
testStatementGetString(connection);
testPreparedStatementGetString(connection);
}
}

@Test
public void testTimeNotPrepareStatement() throws SQLException {
try (Connection connection = setConnection("&useServerPrepStmts=false")) {
testStatementGetTime(connection);
testPreparedStatementGetTime(connection);
testStatementGetString(connection);
testPreparedStatementGetString(connection);
}
}
}

0 comments on commit 9fe880c

Please sign in to comment.