PEPPLER.ORG
Michael Peppler
Sybase Consulting
Menu
Home
Sybase on Linux
Install Guide for Sybase on Linux
General Sybase Resources
General Perl Resources
Freeware
Sybperl
Sybase::Simple
DBD::Sybase
BCP Tool
Bug Tracker
Mailing List Archive
Downloads Directory
FAQs
Sybase on Linux FAQ
Sybperl FAQ
Personal
Michael Peppler's resume

sybperl-l Archive

Up    Prev    Next    

From: j_mcallister at dtint dot com
Subject: help w/dbreadtext and core dumps
Date: Aug 13 1998 10:36PM

Sybperl Gurus,

I've been beating my head against the following subroutine.
It's intended to read a large blob of image data from a table and save it
to disk. My
problem is that the subroutine gets a segmentation fault and core dumps
unless I put
a print statement in the inner while loop. As long as the print statement
includes a
variable (any) and a return, the script runs fine. What kind of weird
side-effect does
the print statement have that fixes this??????

Thanks in advance!

James McAllister
Digital Technology Intl

sub db2file {

        my($path, $dbh, $table, $id, $id_col, $image_col) = @_;

        my $size                = 0;    # Size of the image data.
        my $bytes_read          = 0;    # Bytes read from db in one chunk.
        my $bytes_written       = 0;    # Bytes written to disk in one
chunk.
        my $total_written       = 0;    # Total bytes written to disk.
        my $buffer              = '';   # The image buffer.

        # Open a file to hold the image.
        #
        if (! open(IMAGE, ">$path")) {

                complain("db2file: Unable to open $path ($!).");
                return 0;
        }

        # Unbuffer writes to the image file.
        #
        my $old_fh = select(IMAGE);
        $| = 1;
        select($old_fh);

        # Find the amount (in bytes) of image data to move.
        #
        $query = "select datalength($image_col) from $table where $id_col =
 $id";
        ($row) = $dbh->sql($query);
        $size  = $$row[0] if $dbh->DBCOUNT == 1;

        # Set DBTEXTSIZE to that amount so we don't accidentally truncate
it
        # to the default of 32Kb.
        #
        $dbh->dbsetopt(DBTEXTSIZE, "$size");

        $query = "select $image_col from $table where $id_col = $id";
        $dbh->dbcmd($query);
        $dbh->dbsqlexec;
        while ($dbh->dbresults != NO_MORE_RESULTS) {

                while (($bytes_read = $dbh->dbreadtext($buffer,
$g_buff_size)) !
= NO_MORE_ROWS) {

                        ###################################################
#####
                        # IF THE FOLLOWING IS COMMENTED, THE SCRIPT DUMPS
CORE.
                        print "$bytes_read\n";
                        ###################################################
#####

                        # Test if dbreadtext failed.
                        #
                        if ($bytes_read == -1) {

                                complain("db2file: dbreadtext() returned
-1.");
                        }

                        # Otherwise, save the data to the file.
                        #
                        elsif ($bytes_read > 0) {

                                # Dump the buffer to disk.
                                #
                                $bytes_written = syswrite(IMAGE, $buffer,
$bytes_read);

                                # Bail if the amount written doesn't jive
with
                                # the amount read.
                                #
                                if ($bytes_read != $bytes_written) {

                                        complain("db2file: Read $bytes_read
 bytes but only wrote $bytes_written bytes.");
                                        $dbh->canquery;
                                        return 0;
                                }
                                $total_written += $bytes_written;
                        }
                }
        }

        close(IMAGE);

        # Complain if the total file size doesn't match the total amount
        # of image data for that row in the database.
        #
        complain("db2file: File size $total_written bytes doesn't match the
 expected $size bytes returned from \'$query\'")
        if $total_written != $size;

        # Set the DBTEXTSIZE option back to the default.
        #
        $dbh->dbsetopt(DBTEXTSIZE, "0");

        # Return success.
        #
        1;
}