Jim Thomason > Basset > Basset::DB::Table::View

Download:
Basset-1.04.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
Report a bug
Module Version: 1.00   Source  

NAME ^

Basset::DB::Table::View - used to define virtual views to your objects.

AUTHOR ^

Jim Thomason, jim@jimandkoka.com

SYNOPSIS ^

For example,

 my $table = Basset::DB::Table::View->new(
        'name'                          => 'user',
        'primary_column'        => 'id',
        'select_query' => <<'   eSQL',
                select
                        user.id,
                        name,
                        count(*) as movies
                from
                        user, movies
                where
                        user.id = movies.user
                        and user.id = ?
                group by
                        user.id, name
        eSQL
        'definition'            => {
                'id'            => 'SQL_INTEGER',
                'name'          => 'SQL_VARCHAR',
                'movies'        => 'SQL_INTEGER',
        }
 );
 
 Some::Class->add_primarytable($table);
 
 my $object = Some::Class->load(1);     #load by user 1
 print $object->id, "\n"; #id (user id)
 print $object->name, "\n"; #"Jack Sprat"
 print $object->movies, "\n"; #145 (he owns 145 movies)

DESCRIPTION ^

Basset::DB::Table::View provides an abstract and consistent location for defining database views. Normally, your objects are mapped to tables (most frequently in a 1-1 manner), but sometimes it's convenient to hide a view of data behind an object. This way you can access a complex data query as if it were an object.

Basset::DB::Table::View as your primary table allows you to do that.

Naturally, by virtue of the fact that these are potentially complex queries, objects that use view tables are read-only.

ATTRIBUTES ^

select_query

In view tables, the select_query is an attribute, not a method. You should explicitly define the select query that is used by this table view.

 $table->select_query('select * from somewhere');

my $o = __PACKAGE__->new(); $test->ok($o, "Got object"); $test->is(scalar(__PACKAGE__->select_query), undef, "could not call object method as class method"); $test->is(__PACKAGE__->errcode, "BO-08", "proper error code"); $test->is(scalar($o->select_query), undef, 'select_query is undefined'); $test->is($o->select_query('abc'), 'abc', 'set select_query to abc'); $test->is($o->select_query(), 'abc', 'read value of select_query - abc'); my $h = {}; $test->ok($h, 'got hashref'); $test->is($o->select_query($h), $h, 'set select_query to hashref'); $test->is($o->select_query(), $h, 'read value of select_query - hashref'); my $a = []; $test->ok($a, 'got arrayref'); $test->is($o->select_query($a), $a, 'set select_query to arrayref'); $test->is($o->select_query(), $a, 'read value of select_query - arrayref');

my $o = __PACKAGE__->new(); $test->ok($o, "got object"); $test->ok(! $o->insert_query, "Cannot call insert_query"); $test->is($o->errcode, "BDTV-01", "proper error code");

my $o = __PACKAGE__->new(); $test->ok($o, "got object"); $test->ok(! $o->replace_query, "Cannot call replace_query"); $test->is($o->errcode, "BDTV-02", "proper error code");

my $o = __PACKAGE__->new(); $test->ok($o, "got object"); $test->ok(! $o->update_query, "Cannot call update_query"); $test->is($o->errcode, "BDTV-03", "proper error code");

my $o = __PACKAGE__->new(); $test->ok($o, "got object"); $test->ok(! $o->delete_query, "Cannot call delete_query"); $test->is($o->errcode, "BDTV-04", "proper error code");

my $o = __PACKAGE__->new(); $test->ok($o, "got object"); $test->ok(! $o->multiselect_query, "Cannot call multiselect_query"); $test->is($o->errcode, "BDTV-05", "proper error code");

my $o = __PACKAGE__->new(); $test->ok($o, "got object"); $test->ok(! $o->attach_to_query, "cannot attach to query w/o query"); $test->is($o->errcode, "BDT-02", "proper error code"); $test->is($o->attach_to_query('foo'), 'foo', 'query returns query'); $test->is($o->attach_to_query('foo', {}), 'foo', 'query returns query'); $test->is($o->attach_to_query('foo', {'where' => '2 > 1'}), 'foo', 'query returns query');