Skip to content

Commit

Permalink
[CONJ-253] text encoding on blob field
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Feb 20, 2016
1 parent 16ad037 commit 123f02a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
Expand Up @@ -189,10 +189,6 @@ public String getString(Calendar cal) throws SQLException {
break;
case DECIMAL:
return getBigDecimal().toString();
case BLOB:
case LONGBLOB:
case MEDIUMBLOB:
case TINYBLOB:
case GEOMETRY:
return new String(getBytes());
case NULL:
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/org/mariadb/jdbc/CollationTest.java
Expand Up @@ -5,6 +5,7 @@

import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.sql.*;

import static org.junit.Assert.assertEquals;
Expand All @@ -21,6 +22,8 @@ public static void initClass() throws SQLException {
createTable("emojiTest", "id int unsigned, field longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
createTable("unicodeTestChar", "id int unsigned, field1 varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, field2 longtext "
+ "CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
createTable("textUtf8", "column1 text", "DEFAULT CHARSET=utf8");
createTable("blobUtf8", "column1 blob", "DEFAULT CHARSET=utf8");
}

/**
Expand Down Expand Up @@ -102,6 +105,42 @@ public void test4BytesUtf8() throws Exception {
} else {
fail();
}
}

@Test
public void testText() throws SQLException {
String str = "\u4f60\u597d(hello in Chinese)";
try (PreparedStatement ps = sharedConnection.prepareStatement("insert into textUtf8 values (?)")) {
ps.setString(1, str);
ps.executeUpdate();
}
try (PreparedStatement ps = sharedConnection.prepareStatement("select * from textUtf8");
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
String tmp = rs.getString(1);
assertEquals(tmp, str);
}
}
}

@Test
public void testBinary() throws SQLException {
String str = "\u4f60\u597d(hello in Chinese)";
byte[] strBytes = str.getBytes(Charset.forName("UTF-8"));
try (PreparedStatement ps = sharedConnection.prepareStatement("insert into blobUtf8 values (?)")) {
ps.setBytes(1, strBytes);
ps.executeUpdate();
}
try (PreparedStatement ps = sharedConnection.prepareStatement("select * from blobUtf8");
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
byte[] tmp = rs.getBytes(1);
for (int i = 0; i < tmp.length; i++) {
assertEquals(strBytes[i], tmp[i]);
}

}
}
}

}
5 changes: 3 additions & 2 deletions src/test/java/org/mariadb/jdbc/DatatypeTest.java
Expand Up @@ -9,6 +9,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.nio.charset.Charset;
import java.sql.*;

import static org.junit.Assert.*;
Expand Down Expand Up @@ -552,14 +553,14 @@ private void binTest2Result(ResultSet rs, byte[] buf) throws SQLException, IOExc
for (int i = 0; i < 1000000; i++) {
assertEquals((byte) i, buf2[i]);
}
assertEquals(rs.getString(1), new String(buf));
assertEquals(rs.getString(1), new String(buf, Charset.forName("UTF-8")));

if (rs.next()) {
buf2 = rs.getBytes(1);
for (int i = 0; i < 1000000; i++) {
assertEquals((byte) i, buf2[i]);
}
assertEquals(rs.getString(1), new String(buf));
assertEquals(rs.getString(1), new String(buf, Charset.forName("UTF-8")));
if (rs.next()) {
fail();
}
Expand Down

0 comments on commit 123f02a

Please sign in to comment.