Skip to content

Commit 4484854

Browse files
authored
Merge pull request #126 from archiecobbs/master
Fix incorrect behavior when reading a BLOB via ResultSet.getBytes().
2 parents d36de44 + 231d092 commit 4484854

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

src/main/java/org/sqlite/core/NativeDB.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,13 +579,21 @@ JNIEXPORT jstring JNICALL Java_org_sqlite_core_NativeDB_column_1text(
579579
JNIEXPORT jbyteArray JNICALL Java_org_sqlite_core_NativeDB_column_1blob(
580580
JNIEnv *env, jobject this, jlong stmt, jint col)
581581
{
582-
jsize length;
583582
jbyteArray jBlob;
584583
jbyte *a;
584+
// The value returned by sqlite3_column_type() is only meaningful if no type conversions have occurred
585+
int type = sqlite3_column_type(toref(stmt), col);
585586
const void *blob = sqlite3_column_blob(toref(stmt), col);
586-
if (!blob) return NULL;
587+
jsize length = sqlite3_column_bytes(toref(stmt), col);
588+
if (!blob) {
589+
if (type == SQLITE_NULL) {
590+
return NULL;
591+
} else if (length == 0) {
592+
// The return value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
593+
return (*env)->NewByteArray(env, 0);
594+
}
595+
}
587596

588-
length = sqlite3_column_bytes(toref(stmt), col);
589597
jBlob = (*env)->NewByteArray(env, length);
590598
if (!jBlob) { throwex_outofmemory(env); return 0; }
591599

Binary file not shown.
Binary file not shown.

src/test/java/org/sqlite/QueryTest.java

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
//--------------------------------------
1010
package org.sqlite;
1111

12-
import static org.junit.Assert.*;
12+
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertFalse;
14+
import static org.junit.Assert.assertNull;
15+
import static org.junit.Assert.assertTrue;
1316

1417
import java.sql.Connection;
1518
import java.sql.DriverManager;
@@ -19,10 +22,8 @@
1922
import java.sql.Statement;
2023
import java.util.Date;
2124

22-
import org.sqlite.date.FastDateFormat;
23-
24-
import org.junit.BeforeClass;
2525
import org.junit.Test;
26+
import org.sqlite.date.FastDateFormat;
2627

2728
public class QueryTest
2829
{
@@ -103,6 +104,49 @@ public void dateTimeTest() throws Exception {
103104
stmt.setDate(1, new java.sql.Date(now.getTime()));
104105
}
105106

107+
@Test
108+
public void notEmptyBlob() throws Exception {
109+
Connection conn = getConnection();
110+
111+
conn.createStatement().execute("create table sample (b blob not null)");
112+
113+
conn.createStatement().execute("insert into sample values(zeroblob(5))");
114+
115+
ResultSet rs = conn.createStatement().executeQuery("select * from sample");
116+
assertTrue(rs.next());
117+
assertEquals(5, rs.getBytes(1).length);
118+
assertFalse(rs.wasNull());
119+
}
120+
121+
@Test
122+
public void emptyBlob() throws Exception {
123+
Connection conn = getConnection();
124+
125+
conn.createStatement().execute("create table sample (b blob null)");
126+
127+
conn.createStatement().execute("insert into sample values(zeroblob(0))");
128+
129+
ResultSet rs = conn.createStatement().executeQuery("select * from sample");
130+
assertTrue(rs.next());
131+
assertEquals(0, rs.getBytes(1).length);
132+
assertFalse(rs.wasNull());
133+
}
134+
135+
@Test
136+
public void nullBlob() throws Exception {
137+
Connection conn = getConnection();
138+
139+
conn.createStatement().execute("create table sample (b blob null)");
140+
141+
conn.createStatement().execute("insert into sample values(null)");
142+
143+
ResultSet rs = conn.createStatement().executeQuery("select * from sample");
144+
assertTrue(rs.next());
145+
assertNull(rs.getBytes(1));
146+
assertTrue(rs.wasNull());
147+
}
148+
149+
106150
@Test
107151
public void viewTest() throws Exception {
108152
Connection conn = getConnection();

0 commit comments

Comments
 (0)