1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#!perl
#
# $Id: xblob.t,v 1.12 2007/03/01 17:17:44 mpeppler Exp $
use lib 't';
use strict;
use _test;
use Test::More tests=>11; #qw(no_plan);
use vars qw($Pwd $Uid $Srv $Db $loaded);
BEGIN { use_ok('DBI');
use_ok('DBD::Sybase');}
($Uid, $Pwd, $Srv, $Db) = _test::get_info();
#DBI->trace(3);
my $dbh = DBI->connect("dbi:Sybase:$Srv;database=$Db", $Uid, $Pwd, {PrintError=>1});
#exit;
ok($dbh, 'Connect');
if(!$dbh) {
warn "No connection - did you set the user, password and server name correctly in PWD?\n";
for (4 .. 11) {
ok(0);
}
exit(0);
}
$dbh->do("if object_id('blob_test') != NULL drop table blob_test");
my $rc = $dbh->do("create table blob_test(id int, data image null, foo varchar(30))");
ok($rc, 'Create table');
open(IN, "t/screen.jpg") || die "Can't open t/screen.jpg: $!";
binmode(IN);
my $image;
{
local $/;
$image = <IN>;
}
close(IN);
my $heximg = unpack('H*', $image);
$rc = $dbh->do("insert blob_test(id, data, foo) values(1, '', 'screen.jpg')");
ok($rc, 'Insert image');
#DBI->trace(3);
my $sth = $dbh->prepare("select id, data from blob_test");
#$sth->{syb_no_bind_blob} = 1;
$sth->execute;
while($sth->fetch) {
# my $d;
# $sth->func(2, \$d, 0, 'ct_get_data');
$sth->func('CS_GET', 2, 'ct_data_info') || print $sth->errstr, "\n";
}
$sth->func('ct_prepare_send') || print $sth->errstr, "\n";
$sth->func('CS_SET', 2, {total_txtlen => length($image), log_on_update=>1}, 'ct_data_info') || print $sth->errstr, "\n";
$sth->func($image, length($image), 'ct_send_data') || print $sth->errstr, "\n";
$sth->func('ct_finish_send') || print $sth->errstr, "\n";
$dbh->{LongReadLen} = 100000;
$sth = $dbh->prepare("select id, data from blob_test");
#$dbh->{LongReadLen} = 100000;
#DBI->trace(3);
$sth->{syb_no_bind_blob} = 1;
$sth->execute;
my $heximg2 = '';
my $size = 0;
while(my $d = $sth->fetch) {
my $data;
# open(OUT, ">/tmp/mp_conf.jpg") || die "Can't open /tmp/mp_conf.jpg: $!";
while(1) {
my $read = $sth->func(2, \$data, 1024, 'ct_get_data');
$heximg2 .= unpack('H*', $data);
$size += $read;
last unless $read == 1024;
# print OUT $data;
}
# close(OUT);
}
#warn "Got $size bytes\n";
ok($heximg eq $heximg2, 'Images are the same');
mkdir("./tmp", 0755);
open(ONE, ">./tmp/hex1");
binmode(ONE);
print ONE $heximg;
close(ONE);
open(TWO, ">./tmp/hex2");
binmode(TWO);
print TWO $heximg2;
close(TWO);
$rc = $dbh->do("drop table blob_test");
ok($rc, 'Drop table');
SKIP: {
skip 'Requires DBI 1.34', 4 unless $DBI::VERSION >= 1.34;
my $rc = $dbh->do("create table blob_test(id int, data image null, foo varchar(30))");
ok($rc, 'Creat table');
open(IN, "t/screen.jpg") || die "Can't open t/screen.jpg: $!";
binmode(IN);
my $image;
{
local $/;
$image = <IN>;
}
close(IN);
my $heximg = unpack('H*', $image);
$rc = $dbh->do("insert blob_test(id, data, foo) values(1, '', 'screen.jpg')");
ok($rc, 'Insert image');
#DBI->trace(3);
my $sth = $dbh->prepare("select id, data from blob_test");
#$sth->{syb_no_bind_blob} = 1;
$sth->execute;
while($sth->fetch) {
# my $d;
# $sth->func(2, \$d, 0, 'ct_get_data');
$sth->syb_ct_data_info('CS_GET', 2) || print $sth->errstr, "\n";
}
$sth->syb_ct_prepare_send() || print $sth->errstr, "\n";
$sth->syb_ct_data_info('CS_SET', 2, {total_txtlen => length($image), log_on_update=>1}) || print $sth->errstr, "\n";
$sth->syb_ct_send_data($image, length($image)) || print $sth->errstr, "\n";
$sth->syb_ct_finish_send() || print $sth->errstr, "\n";
#DBI->trace(4);
$dbh->{LongReadLen} = 100000;
$sth = $dbh->prepare("select id, data from blob_test");
#$dbh->{LongReadLen} = 100000;
#DBI->trace(0);
#DBI->trace(3);
$sth->{syb_no_bind_blob} = 1;
$sth->execute;
my $heximg2 = '';
my $size = 0;
while(my $d = $sth->fetch) {
my $data;
# open(OUT, ">/tmp/mp_conf.jpg") || die "Can't open /tmp/mp_conf.jpg: $!";
while(1) {
my $read = $sth->syb_ct_get_data(2, \$data, 1024);
$heximg2 .= unpack('H*', $data);
$size += $read;
last unless $read == 1024;
# print OUT $data;
}
# close(OUT);
}
#warn "Got $size bytes\n";
ok($heximg eq $heximg2, 'Images are the same');
mkdir("./tmp");
open(ONE, ">./tmp/hex1");
binmode(ONE);
print ONE $heximg;
close(ONE);
open(TWO, ">./tmp/hex2");
binmode(TWO);
print TWO $heximg2;
close(TWO);
$rc = $dbh->do("drop table blob_test");
ok($rc, 'Drop table');
}
|