Skip to content

Commit

Permalink
Merge pull request #126 from archiecobbs/master
Browse files Browse the repository at this point in the history
Fix incorrect behavior when reading a BLOB via ResultSet.getBytes().
  • Loading branch information
gitblit authored Jun 22, 2016
2 parents d36de44 + 231d092 commit 4484854
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
14 changes: 11 additions & 3 deletions src/main/java/org/sqlite/core/NativeDB.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,13 +579,21 @@ JNIEXPORT jstring JNICALL Java_org_sqlite_core_NativeDB_column_1text(
JNIEXPORT jbyteArray JNICALL Java_org_sqlite_core_NativeDB_column_1blob(
JNIEnv *env, jobject this, jlong stmt, jint col)
{
jsize length;
jbyteArray jBlob;
jbyte *a;
// The value returned by sqlite3_column_type() is only meaningful if no type conversions have occurred
int type = sqlite3_column_type(toref(stmt), col);
const void *blob = sqlite3_column_blob(toref(stmt), col);
if (!blob) return NULL;
jsize length = sqlite3_column_bytes(toref(stmt), col);
if (!blob) {
if (type == SQLITE_NULL) {
return NULL;
} else if (length == 0) {
// The return value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
return (*env)->NewByteArray(env, 0);
}
}

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

Expand Down
Binary file not shown.
Binary file not shown.
52 changes: 48 additions & 4 deletions src/test/java/org/sqlite/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
//--------------------------------------
package org.sqlite;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.sql.Connection;
import java.sql.DriverManager;
Expand All @@ -19,10 +22,8 @@
import java.sql.Statement;
import java.util.Date;

import org.sqlite.date.FastDateFormat;

import org.junit.BeforeClass;
import org.junit.Test;
import org.sqlite.date.FastDateFormat;

public class QueryTest
{
Expand Down Expand Up @@ -103,6 +104,49 @@ public void dateTimeTest() throws Exception {
stmt.setDate(1, new java.sql.Date(now.getTime()));
}

@Test
public void notEmptyBlob() throws Exception {
Connection conn = getConnection();

conn.createStatement().execute("create table sample (b blob not null)");

conn.createStatement().execute("insert into sample values(zeroblob(5))");

ResultSet rs = conn.createStatement().executeQuery("select * from sample");
assertTrue(rs.next());
assertEquals(5, rs.getBytes(1).length);
assertFalse(rs.wasNull());
}

@Test
public void emptyBlob() throws Exception {
Connection conn = getConnection();

conn.createStatement().execute("create table sample (b blob null)");

conn.createStatement().execute("insert into sample values(zeroblob(0))");

ResultSet rs = conn.createStatement().executeQuery("select * from sample");
assertTrue(rs.next());
assertEquals(0, rs.getBytes(1).length);
assertFalse(rs.wasNull());
}

@Test
public void nullBlob() throws Exception {
Connection conn = getConnection();

conn.createStatement().execute("create table sample (b blob null)");

conn.createStatement().execute("insert into sample values(null)");

ResultSet rs = conn.createStatement().executeQuery("select * from sample");
assertTrue(rs.next());
assertNull(rs.getBytes(1));
assertTrue(rs.wasNull());
}


@Test
public void viewTest() throws Exception {
Connection conn = getConnection();
Expand Down

0 comments on commit 4484854

Please sign in to comment.