| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Basset::DB::Table::View; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | #Basset::DB::Table::View, copyright and (c) 2004 James A Thomason III | 
| 4 |  |  |  |  |  |  | #Basset::DB::Table is distributed under the terms of the Perl Artistic License. | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | $VERSION = '1.00'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 3673 | use Basset::DB::Table; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 52 |  | 
| 9 |  |  |  |  |  |  | @ISA = qw(Basset::DB::Table); | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 1 |  |  | 1 |  | 12 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 35 |  | 
| 12 | 1 |  |  | 1 |  | 6 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 261 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | =pod | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | =head1 NAME | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | Basset::DB::Table::View - used to define virtual views to your objects. | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | =head1 AUTHOR | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | Jim Thomason, jim@jimandkoka.com | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | For example, | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | my $table = Basset::DB::Table::View->new( | 
| 29 |  |  |  |  |  |  | 'name'				=> 'user', | 
| 30 |  |  |  |  |  |  | 'primary_column'	=> 'id', | 
| 31 |  |  |  |  |  |  | 'select_query' => <<'	eSQL', | 
| 32 |  |  |  |  |  |  | select | 
| 33 |  |  |  |  |  |  | user.id, | 
| 34 |  |  |  |  |  |  | name, | 
| 35 |  |  |  |  |  |  | count(*) as movies | 
| 36 |  |  |  |  |  |  | from | 
| 37 |  |  |  |  |  |  | user, movies | 
| 38 |  |  |  |  |  |  | where | 
| 39 |  |  |  |  |  |  | user.id = movies.user | 
| 40 |  |  |  |  |  |  | and user.id = ? | 
| 41 |  |  |  |  |  |  | group by | 
| 42 |  |  |  |  |  |  | user.id, name | 
| 43 |  |  |  |  |  |  | eSQL | 
| 44 |  |  |  |  |  |  | 'definition'		=> { | 
| 45 |  |  |  |  |  |  | 'id'		=> 'SQL_INTEGER', | 
| 46 |  |  |  |  |  |  | 'name'		=> 'SQL_VARCHAR', | 
| 47 |  |  |  |  |  |  | 'movies'	=> 'SQL_INTEGER', | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  | ); | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | Some::Class->add_primarytable($table); | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | my $object = Some::Class->load(1);	#load by user 1 | 
| 54 |  |  |  |  |  |  | print $object->id, "\n"; #id (user id) | 
| 55 |  |  |  |  |  |  | print $object->name, "\n"; #"Jack Sprat" | 
| 56 |  |  |  |  |  |  | print $object->movies, "\n"; #145 (he owns 145 movies) | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | Basset::DB::Table::View provides an abstract and consistent location for defining database views. Normally, | 
| 62 |  |  |  |  |  |  | your objects are mapped to tables (most frequently in a 1-1 manner), but sometimes it's convenient to hide | 
| 63 |  |  |  |  |  |  | a view of data behind an object. This way you can access a complex data query as if it were an object. | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | Basset::DB::Table::View as your primary table allows you to do that. | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | Naturally, by virtue of the fact that these are potentially complex queries, objects that use view tables | 
| 68 |  |  |  |  |  |  | are read-only. | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | =cut | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | =pod | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | =head1 ATTRIBUTES | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | =over | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | =cut | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | =pod | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | =item select_query | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | In view tables, the select_query is an attribute, not a method. You should explicitly define the select query that is | 
| 85 |  |  |  |  |  |  | used by this table view. | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | $table->select_query('select * from somewhere'); | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =cut | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | __PACKAGE__->add_attr('select_query'); | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | =pod | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | =begin btest select_query | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | my $o = __PACKAGE__->new(); | 
| 98 |  |  |  |  |  |  | $test->ok($o, "Got object"); | 
| 99 |  |  |  |  |  |  | $test->is(scalar(__PACKAGE__->select_query), undef, "could not call object method as class method"); | 
| 100 |  |  |  |  |  |  | $test->is(__PACKAGE__->errcode, "BO-08", "proper error code"); | 
| 101 |  |  |  |  |  |  | $test->is(scalar($o->select_query), undef, 'select_query is undefined'); | 
| 102 |  |  |  |  |  |  | $test->is($o->select_query('abc'), 'abc', 'set select_query to abc'); | 
| 103 |  |  |  |  |  |  | $test->is($o->select_query(), 'abc', 'read value of select_query - abc'); | 
| 104 |  |  |  |  |  |  | my $h = {}; | 
| 105 |  |  |  |  |  |  | $test->ok($h, 'got hashref'); | 
| 106 |  |  |  |  |  |  | $test->is($o->select_query($h), $h, 'set select_query to hashref'); | 
| 107 |  |  |  |  |  |  | $test->is($o->select_query(), $h, 'read value of select_query  - hashref'); | 
| 108 |  |  |  |  |  |  | my $a = []; | 
| 109 |  |  |  |  |  |  | $test->ok($a, 'got arrayref'); | 
| 110 |  |  |  |  |  |  | $test->is($o->select_query($a), $a, 'set select_query to arrayref'); | 
| 111 |  |  |  |  |  |  | $test->is($o->select_query(), $a, 'read value of select_query  - arrayref'); | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =end btest | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | =back | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | =cut | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | sub insert_query { | 
| 120 | 0 |  |  | 0 | 1 |  | return shift->error("Views cannot insert", "BDTV-01"); | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | =pod | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | =begin btest insert_query | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | my $o = __PACKAGE__->new(); | 
| 128 |  |  |  |  |  |  | $test->ok($o, "got object"); | 
| 129 |  |  |  |  |  |  | $test->ok(! $o->insert_query, "Cannot call insert_query"); | 
| 130 |  |  |  |  |  |  | $test->is($o->errcode, "BDTV-01", "proper error code"); | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | =end btest | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | =cut | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | sub replace_query { | 
| 137 | 0 |  |  | 0 | 1 |  | return shift->error("Views cannot replace", "BDTV-02"); | 
| 138 |  |  |  |  |  |  | } | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =pod | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =begin btest replace_query | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | my $o = __PACKAGE__->new(); | 
| 145 |  |  |  |  |  |  | $test->ok($o, "got object"); | 
| 146 |  |  |  |  |  |  | $test->ok(! $o->replace_query, "Cannot call replace_query"); | 
| 147 |  |  |  |  |  |  | $test->is($o->errcode, "BDTV-02", "proper error code"); | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | =end btest | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | =cut | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | sub update_query { | 
| 155 | 0 |  |  | 0 | 1 |  | return shift->error("Views cannot update", "BDTV-03"); | 
| 156 |  |  |  |  |  |  | } | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | =pod | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | =begin btest update_query | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | my $o = __PACKAGE__->new(); | 
| 163 |  |  |  |  |  |  | $test->ok($o, "got object"); | 
| 164 |  |  |  |  |  |  | $test->ok(! $o->update_query, "Cannot call update_query"); | 
| 165 |  |  |  |  |  |  | $test->is($o->errcode, "BDTV-03", "proper error code"); | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | =end btest | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | =cut | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | sub delete_query { | 
| 172 | 0 |  |  | 0 | 1 |  | return shift->error("Views cannot delete", "BDTV-04"); | 
| 173 |  |  |  |  |  |  | } | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | =pod | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | =begin btest delete_query | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | my $o = __PACKAGE__->new(); | 
| 180 |  |  |  |  |  |  | $test->ok($o, "got object"); | 
| 181 |  |  |  |  |  |  | $test->ok(! $o->delete_query, "Cannot call delete_query"); | 
| 182 |  |  |  |  |  |  | $test->is($o->errcode, "BDTV-04", "proper error code"); | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | =end btest | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | =cut | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | sub multiselect_query { | 
| 189 | 0 |  |  | 0 | 1 |  | return shift->error("Views cannot multiselect", "BDTV-05"); | 
| 190 |  |  |  |  |  |  | } | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | =pod | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | =begin btest multiselect_query | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | my $o = __PACKAGE__->new(); | 
| 197 |  |  |  |  |  |  | $test->ok($o, "got object"); | 
| 198 |  |  |  |  |  |  | $test->ok(! $o->multiselect_query, "Cannot call multiselect_query"); | 
| 199 |  |  |  |  |  |  | $test->is($o->errcode, "BDTV-05", "proper error code"); | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | =end btest | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | =cut | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  | sub attach_to_query { | 
| 206 | 0 |  |  | 0 | 1 |  | my $self	= shift; | 
| 207 | 0 |  |  |  |  |  | my $query	= shift; | 
| 208 |  |  |  |  |  |  |  | 
| 209 | 0 |  |  |  |  |  | return $self->SUPER::attach_to_query($query); | 
| 210 |  |  |  |  |  |  | } | 
| 211 |  |  |  |  |  |  |  | 
| 212 |  |  |  |  |  |  | =pod | 
| 213 |  |  |  |  |  |  |  | 
| 214 |  |  |  |  |  |  | =begin btest attach_to_query | 
| 215 |  |  |  |  |  |  |  | 
| 216 |  |  |  |  |  |  | my $o = __PACKAGE__->new(); | 
| 217 |  |  |  |  |  |  | $test->ok($o, "got object"); | 
| 218 |  |  |  |  |  |  | $test->ok(! $o->attach_to_query, "cannot attach to query w/o query"); | 
| 219 |  |  |  |  |  |  | $test->is($o->errcode, "BDT-02", "proper error code"); | 
| 220 |  |  |  |  |  |  | $test->is($o->attach_to_query('foo'), 'foo', 'query returns query'); | 
| 221 |  |  |  |  |  |  | $test->is($o->attach_to_query('foo', {}), 'foo', 'query returns query'); | 
| 222 |  |  |  |  |  |  | $test->is($o->attach_to_query('foo', {'where' => '2 > 1'}), 'foo', 'query returns query'); | 
| 223 |  |  |  |  |  |  |  | 
| 224 |  |  |  |  |  |  | =end btest | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  | =cut | 
| 227 |  |  |  |  |  |  |  | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | 1; |