1
0
Fork 0
mirror of https://github.com/DBD-SQLite/DBD-SQLite synced 2025-06-07 14:19:10 -04:00

Merge pull request #23 from ranvis/fix-empty-blob

fix zero-length BLOB value is retrieved as undef
This commit is contained in:
Kenichi Ishigaki 2017-03-14 01:00:36 +09:00 committed by GitHub
commit 0b86a0451f
2 changed files with 22 additions and 10 deletions

View file

@ -1176,7 +1176,8 @@ sqlite_st_fetch(SV *sth, imp_sth_t *imp_sth)
case SQLITE_BLOB: case SQLITE_BLOB:
sqlite_trace(sth, imp_sth, 5, form("fetch column %d as blob", i)); sqlite_trace(sth, imp_sth, 5, form("fetch column %d as blob", i));
len = sqlite3_column_bytes(imp_sth->stmt, i); len = sqlite3_column_bytes(imp_sth->stmt, i);
sv_setpvn(AvARRAY(av)[i], sqlite3_column_blob(imp_sth->stmt, i), len); val = (char*)sqlite3_column_blob(imp_sth->stmt, i);
sv_setpvn(AvARRAY(av)[i], len ? val : "", len);
SvUTF8_off(AvARRAY(av)[i]); SvUTF8_off(AvARRAY(av)[i]);
break; break;
default: default:

View file

@ -10,7 +10,7 @@ BEGIN {
} }
use t::lib::SQLiteTest; use t::lib::SQLiteTest;
use Test::More tests => 10; use Test::More tests => 17;
use Test::NoWarnings; use Test::NoWarnings;
use DBI ':sql_types'; use DBI ':sql_types';
@ -42,7 +42,7 @@ $dbh->{sqlite_handle_binary_nulls} = 1;
ok( $dbh->do(<<'END_SQL'), 'CREATE TABLE' ); ok( $dbh->do(<<'END_SQL'), 'CREATE TABLE' );
CREATE TABLE one ( CREATE TABLE one (
id INTEGER NOT NULL, id INTEGER NOT NULL,
name BLOB (128) NOT NULL name BLOB (128)
) )
END_SQL END_SQL
@ -58,20 +58,31 @@ for ( my $i = 0; $i < 128; $i++ ) {
# Insert a row into the test table # Insert a row into the test table
SCOPE: { SCOPE: {
my $sth = $dbh->prepare("INSERT INTO one VALUES ( 1, ? )"); my $sth = $dbh->prepare("INSERT INTO one VALUES ( ?, ? )");
isa_ok( $sth, 'DBI::st' ); isa_ok( $sth, 'DBI::st' );
ok( $sth->bind_param(1, $blob, SQL_BLOB), '->bind_param' ); ok( $sth->bind_param(1, 1), '->bind_param' );
ok( $sth->bind_param(2, $blob, SQL_BLOB), '->bind_param' );
ok( $sth->execute, '->execute' );
ok( $sth->bind_param(1, 2), '->bind_param' );
ok( $sth->bind_param(2, '', SQL_BLOB), '->bind_param' );
ok( $sth->execute, '->execute' );
ok( $sth->bind_param(1, 3), '->bind_param' );
ok( $sth->bind_param(2, undef, SQL_BLOB), '->bind_param' );
ok( $sth->execute, '->execute' ); ok( $sth->execute, '->execute' );
} }
# Now, try SELECT'ing the row out. # Now, try SELECT'ing the row out.
SCOPE: { SCOPE: {
my $sth = $dbh->prepare("SELECT * FROM one WHERE id = 1"); my $sth = $dbh->prepare("SELECT * FROM one ORDER BY id");
isa_ok( $sth, 'DBI::st' ); isa_ok( $sth, 'DBI::st' );
ok( $sth->execute, '->execute' ); ok( $sth->execute, '->execute' );
ok( my $rows = $sth->fetchall_arrayref;
$sth->fetchrow_arrayref->[1] eq $blob, is_deeply( $rows, [
'Got the blob back ok', [ 1, $blob ],
); [ 2, '' ],
[ 3, undef ],
], 'Got the blob back ok' );
ok( $sth->finish, '->finish' ); ok( $sth->finish, '->finish' );
} }