GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
DBIx::Class::Helper::Row::OnColumnMissing(3) User Contributed Perl Documentation DBIx::Class::Helper::Row::OnColumnMissing(3)

DBIx::Class::Helper::Row::OnColumnMissing - Configurably handle access of missing columns

 package MyApp::Schema::Result::Account;

 use parent 'DBIx::Class::Core';

 __PACKAGE__->load_components(qw(Helper::Row::OnColumnMissing));

 __PACKAGE__->table('Account');

 __PACKAGE__->add_columns(
    id => {
       data_type         => 'integer',
       is_auto_increment => 1,
    },
    name => {
       data_type => 'varchar',
       size => 25,
    },
    book => { data_type => 'text' },
 );

 sub on_column_missing { 'die' }

 1;

Or with DBIx::Class::Candy:

 package MyApp::Schema::Result::Account;

 use DBIx::Class::Candy -components => ['Helper::Row::OnColumnMissing'];

 table 'Account';

 column id => {
    data_type         => 'integer',
    is_auto_increment => 1,
 };

 column amount => {
    data_type          => 'float',
    keep_storage_value => 1,
 };

 column book => { data_type => 'text' };

 sub on_column_missing { 'die' }

 1;

Elsewhere:

 my $row = $rs->search(undef, { columns => [qw( id name )] })->one_row;

 $row->book # dies

This module is written to handle the odd condition where you have limited the columns retrieved from the database but accidentally access one of the ones not included. It is configurable by tweaking the "on_column_missing" return value.

You specify the "mode" by returning the "mode" from the "on_column_missing" method. By default the "mode" returned is "warn".

The predefined modes are:

"die"
Dies with "Column $name has not been loaded".
"warn"
Warns with "Column $name has not been loaded".
"nothing"
Does nothing

You can predefine more modes by defining methods named "on_column_$mode", and also override the default modes by overriding the corresponding methods. If you need ad-hoc behavior you can return a code reference and that will be called as a method on the object.

If for some reason you find that you need to change your "mode" at runtime, you can always replace the "on_column_missing" with an accessor. For example:

 __PACKAGE__->mk_group_accessors(inherited => 'on_column_missing');
 __PACKAGE__->on_column_missing('warn');

Elsewhere:

 $row->on_column_missing('die');

If you are especially crazy you could even do something like this:

 $row->on_column_missing(sub {
    my ($self, $column) = @_;

    $self
       ->result_source
       ->resultset
       ->search({ id => $self->id })
       ->get_column($column)
       ->single
 });

Though if you do that I would make it a named mode (maybe "retrieve"?)

Thanks ZipRecruiter <https://www.ziprecruiter.com> for funding the development of this module.

Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>

This software is copyright (c) 2020 by Arthur Axel "fREW" Schmidt.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

2020-03-28 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.