Working with blobs

From ClassDBI

Jump to: navigation, search

Class::DBI contains an undocumented feature for specifying that your column is going to contain data of a certain type.

For, for example, you can say:

 Class->data_type(column => DBI::SQL_BINARY);

This is useful for dealing with BLOBs, or to work around the problem whereby the DBI thinks that the first type of data passed to a field is what to expect in future (so if you pass a number to VARCHAR column, it won't quote any future arguments).

For DBD::Oracle if your table/class has more than one BLOB you also need an ora_field specifier:

       package My::BinaryFile;
       use base qw/My::CDBI::Oracle::SubClass/;
       
       __PACKAGE__->primary('bin_id');
       __PACKAGE__->columns('Executable' => 'executable');
       __PACKAGE__->columns('HelpFile' => 'helpfile');
       
       use DBD::Oracle qw(:ora_types);
       my $Excutable_type = {ora_type => ORA_BLOB, ora_field =>'executable'};
       my $HelpFile_type = {ora_type  => ORA_BLOB, ora_field =>'helpfile'};
       
       __PACKAGE__->data_type(executable => $Excutable_type );
       __PACKAGE__->data_type(helpfile   => $HelpFile_type );

This would be great if I could use it in the retrieve_from_sql method to solve my problem with CdbiPager.

-- ZbigniewLukasiak


For PostgreSQL, you should use the following to specify a BYTEA column:

       use DBD::Pg;
       __PACKAGE__->data_type(column_name => {pg_type => DBD::Pg::PG_BYTEA});

However, the current version (1.32) of DBD::Pg has a bug with pre-bound columns. You'll need to apply the following patch to get it working.

-- StepanRiha


The data_type is an accessor method of the Class::DBI package. This means that declaring the data type of a column declares it for all classes derived from Class::DBI. This is most likely not what you want.

You can limit the column binding to your package with the following code:

       # Override data_type accessor for inherited class
       __PACKAGE__->__data_type({});
       
       # Now bind data types to columns
       __PACKAGE__->data_type(foo => $type1 );
       __PACKAGE__->data_type(bar => $type2 );

-- StepanRiha

Personal tools