/usr/share/perl5/Alzabo/Runtime/Row.pm is in libalzabo-perl 0.92-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | package Alzabo::Runtime::Row;
use strict;
use vars qw($VERSION);
use Alzabo;
use Alzabo::Exceptions ( abbr => [ qw( logic_exception no_such_row_exception
params_exception storable_exception ) ] );
use Alzabo::Runtime;
use Alzabo::Runtime::RowState::Deleted;
use Alzabo::Runtime::RowState::Live;
use Alzabo::Runtime::RowState::Potential;
use Alzabo::Utils;
use Params::Validate qw( validate validate_with UNDEF SCALAR HASHREF BOOLEAN );
Params::Validate::validation_options
( on_fail => sub { params_exception join '', @_ } );
use Storable ();
$VERSION = 2.0;
BEGIN
{
no strict 'refs';
foreach my $meth ( qw( select select_hash update refresh delete
id_as_string is_live is_potential is_deleted ) )
{
*{ __PACKAGE__ . "::$meth" } =
sub { my $s = shift;
$s->{state}->$meth( $s, @_ ) };
}
}
use constant NEW_SPEC => { table => { isa => 'Alzabo::Runtime::Table' },
pk => { type => SCALAR | HASHREF,
optional => 1,
},
prefetch => { type => UNDEF | HASHREF,
optional => 1,
},
state => { type => SCALAR,
default => 'Alzabo::Runtime::RowState::Live',
},
potential_row => { isa => 'Alzabo::Runtime::Row',
optional => 1,
},
values => { type => HASHREF,
default => {},
},
no_cache => { type => BOOLEAN, default => 0 },
};
sub new
{
my $proto = shift;
my $class = ref $proto || $proto;
my %p =
validate( @_, NEW_SPEC );
my $self = $p{potential_row} ? $p{potential_row} : {};
bless $self, $class;
$self->{table} = $p{table};
$self->{state} = $p{state};
$self->{state}->_init($self, @_) or return;
return $self;
}
sub table
{
my $self = shift;
return $self->{table};
}
sub schema
{
my $self = shift;
return $self->table->schema;
}
sub set_state { $_[0]->{state} = $_[1] };
use constant ROWS_BY_FOREIGN_KEY_SPEC => { foreign_key => { isa => 'Alzabo::ForeignKey' } };
sub rows_by_foreign_key
{
my $self = shift;
my %p = validate_with( params => \@_,
spec => ROWS_BY_FOREIGN_KEY_SPEC,
allow_extra => 1,
);
my $fk = delete $p{foreign_key};
if ($p{where})
{
$p{where} = [ $p{where} ] unless Alzabo::Utils::is_arrayref( $p{where}[0] );
}
push @{ $p{where} },
map { [ $_->[1], '=', $self->select( $_->[0]->name ) ] } $fk->column_pairs;
# if the relationship is not 1..n, then only one row can be
# returned (or referential integrity has been hosed in the
# database).
return $fk->is_one_to_many ? $fk->table_to->rows_where(%p) : $fk->table_to->one_row(%p);
}
# class method
sub id_as_string_ext
{
my $class = shift;
my %p = @_;
my $id_hash = $class->_make_id_hash(%p);
local $^W; # weirdly, enough there are code paths that can
# lead here that'd lead to $id_hash having some
# values that are undef
return join ';:;_;:;', ( $p{table}->schema->name,
$p{table}->name,
map { $_, $id_hash->{$_} } sort keys %$id_hash );
}
sub _make_id_hash
{
my $self = shift;
my %p = @_;
return $p{pk} if ref $p{pk};
return { ($p{table}->primary_key)[0]->name => $p{pk} };
}
sub _update_pk_hash
{
my $self = shift;
my @pk = keys %{ $self->{pk} };
@{ $self->{pk} }{ @pk } = @{ $self->{data} }{ @pk };
delete $self->{id_string};
}
sub make_live
{
my $self = shift;
logic_exception "Can only call make_live on potential rows"
unless $self->{state}->is_potential;
my %p = @_;
my %values;
foreach ( $self->table->columns )
{
next unless exists $p{values}->{ $_->name } || exists $self->{data}->{ $_->name };
$values{ $_->name } = ( exists $p{values}->{ $_->name } ?
$p{values}->{ $_->name } :
$self->{data}->{ $_->name } );
}
my $table = $self->table;
delete @{ $self }{keys %$self}; # clear out everything
$table->insert( @_,
potential_row => $self,
%values ? ( values => \%values ) : (),
);
}
sub _cached_data_is_same
{
my $self = shift;
my ( $key, $val ) = @_;
# The convolutions here are necessary to avoid avoid treating
# undef as being equal to 0 or ''. Stupid NULLs.
return 1
if ( exists $self->{data}{$key} &&
( ( ! defined $val && ! defined $self->{data}{$key} ) ||
( defined $val &&
defined $self->{data}{$key} &&
( $val eq $self->{data}{$key} )
)
)
);
return 0;
}
sub _no_such_row_error
{
my $self = shift;
my $err = 'Unable to find a row in ' . $self->table->name . ' where ';
my @vals;
while ( my( $k, $v ) = each %{ $self->{pk} } )
{
$v = '<NULL>' unless defined $v;
my $val = "$k = $v";
push @vals, $val;
}
$err .= join ', ', @vals;
no_such_row_exception $err;
}
sub STORABLE_freeze
{
my $self = shift;
my $cloning = shift;
my %data = %$self;
my $table = delete $data{table};
$data{schema} = $table->schema->name;
$data{table_name} = $table->name;
my $ser = eval { Storable::nfreeze(\%data) };
storable_exception $@ if $@;
return $ser;
}
sub STORABLE_thaw
{
my ( $self, $cloning, $ser ) = @_;
my $data = eval { Storable::thaw($ser) };
storable_exception $@ if $@;
%$self = %$data;
my $s = Alzabo::Runtime::Schema->load_from_file( name => delete $self->{schema} );
$self->{table} = $s->table( delete $self->{table_name} );
return $self;
}
BEGIN
{
# dumb hack to fix bugs in Storable 2.00 - 2.03 w/ a non-threaded
# Perl
#
# Basically, Storable somehow screws up the hooks business the
# _first_ time an object from a class with hooks is stored. So
# we'll just _force_ it do it once right away.
if ( $Storable::VERSION >= 2 && $Storable::VERSION <= 2.03 )
{
eval <<'EOF';
{ package ___name; sub name { 'foo' } }
{ package ___table; @table::ISA = '___name'; sub schema { bless {}, '___name' } }
my $row = bless { table => bless {}, '___table' }, __PACKAGE__;
Storable::thaw(Storable::nfreeze($row));
EOF
}
}
1;
__END__
=head1 NAME
Alzabo::Runtime::Row - Row objects
=head1 SYNOPSIS
use Alzabo::Runtime::Row;
my $row = $table->row_by_pk( pk => 1 );
$row->select('foo');
$row->update( bar => 5 );
$row->delete;
=head1 DESCRIPTION
These objects represent actual rows from the database containing
actual data. In general, you will want to use the
L<C<Alzabo::Runtime::Table>|Alzabo::Runtime::Table> object to retrieve
rows. The L<C<Alzabo::Runtime::Table>|Alzabo::Runtime::Table> object
can return either single rows or L<row
cursors|Alzabo::Runtime::RowCursor>.
=head1 ROW STATES
Row objects can have a variety of states. Most row objects are
"live", which means they represent an actual row object. A row can be
changed to the "deleted" state by calling its C<delete()> method.
This is a row that no longer exists in the database. Most method
calls on rows in this state cause an exception.
There is also a "potential" state, for objects which do not represent
actual database rows. You can call L<C<make_live()>|make_live> on
these rows in order to change their state to "live".
Finally, there is an "in cache" state, which is identical to the
"live" state, except that it is used for object's that are cached via
the
L<C<Alzabo::Runtime::UniqueRowCache>|Alzabo::Runtime::UniqueRowCache>
class.
=head1 METHODS
Row objects offer the following methods:
=head2 select (@list_of_column_names)
Returns a list of values matching the specified columns in a list
context. In scalar context it returns only a single value (the first
column specified).
If no columns are specified, it will return the values for all of the
columns in the table, in the order that are returned by
L<C<Alzabo::Runtime::Table-E<gt>columns>|Alzabo::Runtime::Table/columns>.
This method throws an
L<C<Alzabo::Runtime::NoSuchRowException>|Alzabo::Exceptions> if called
on a deleted row.
=head2 select_hash (@list_of_column_names)
Returns a hash of column names to values matching the specified
columns.
If no columns are specified, it will return the values for all of the
columns in the table.
This method throws an
L<C<Alzabo::Runtime::NoSuchRowException>|Alzabo::Exceptions> if called
on a deleted row.
=head2 update (%hash_of_columns_and_values)
Given a hash of columns and values, attempts to update the database to
and the object to represent these new values.
It returns a boolean value indicating whether or not any data was
actually modified.
This method throws an
L<C<Alzabo::Runtime::NoSuchRowException>|Alzabo::Exceptions> if called
on a deleted row.
=head2 refresh
Refreshes the object against the database. This can be used when you
want to ensure that a row object is up to date in regards to the
database state.
This method throws an
L<C<Alzabo::Runtime::NoSuchRowException>|Alzabo::Exceptions> if called
on a deleted row.
=head2 delete
Deletes the row from the RDBMS and changes the object's state to
deleted.
For potential rows, this method simply changes the object's state.
This method throws an
L<C<Alzabo::Runtime::NoSuchRowException>|Alzabo::Exceptions> if called
on a deleted row.
=head2 id_as_string
Returns the row's id value as a string. This can be passed to the
L<C<Alzabo::Runtime::Table-E<gt>row_by_id>|Alzabo::Runtime::Table/row_by_id>
method to recreate the row later.
For potential rows, this method always return an empty string.
This method throws an
L<C<Alzabo::Runtime::NoSuchRowException>|Alzabo::Exceptions> if called
on a deleted row.
=head2 is_live
Indicates whether or not the given row represents an actual row in the
database.
=head2 is_potential
Indicates whether or not the given row represents an actual row in the
datatbase.
=head2 is_deleted
Indicates whether or not the given row has been deleted
=head2 table
Returns the L<C<Alzabo::Runtime::Table>|Alzabo::Runtime::Table> object
that this row belongs to.
=head2 schema
Returns the L<C<Alzabo::Runtime::Schema>|Alzabo::Runtime::Schema>
object that this row's table belongs to. This is a shortcut for C<<
$row->table->schema >>.
=head2 rows_by_foreign_key
This method is used to retrieve row objects from other tables by
"following" a relationship between two tables.
It takes the following parameters:
=over 4
=item * foreign_key => C<Alzabo::Runtime::ForeignKey> object
=back
Given a foreign key object, this method returns either a row object or
a row cursor object the row(s) in the table to which the relationship
exist.
The type of object returned is based on the cardinality of the
relationship. If the relationship says that there could only be one
matching row, then a row object is returned, otherwise it returns a
cursor.
=head1 POTENTIAL ROWS
The "potential" row state is used for rows which do not yet exist in
the database. These are created via the L<C<<
Alzabo::Runtime::Table->potential_row
>>|Alzabo::Runtime::Table/potential_row> method.
They are useful when you need a placeholder object which you can
update and select from, but you don't actually want to commit the data
to the database.
These objects are not cached.
Once L<C<make_live()>|/make_live> is called, the object's state
becomes "live".
Potential rows have looser constraints for column values than regular
rows. When creating a new potential row, it is ok if none of the
columns are defined. If a column has a default, and a value for that
column is not given, then the default will be used. However, you
cannot update a column in a potential row to undef (NULL) if the
column is not nullable.
No attempt is made to enforce L<referential integrity
constraints|Alzabo/Referential Integrity> on these objects.
You cannot set a column's value to a database function like "NOW()",
because this requires interaction with the database.
=head2 make_live
This method inserts the row into the database and changes the object's
state to "live".
This means that all references to the potential row object will now be
references to the real object (which is a good thing).
This method can take any parameters that can be passed to the
L<C<Alzabo::Runtime::Table-E<gt>insert>|Alzabo::Runtime::Table/insert>
method.
Any columns already set will be passed to the C<insert> method,
including primary key values. However, these will be overridden, on
a column by column basis, by a "pk" or "values" parameters given to
the C<(make_live()> method.
Calling this method on a row object that is not in the "potential"
state will cause an
L<C<Alzabo::Runtime::LogicException>|Alzabo::Exceptions>
=head1 AUTHOR
Dave Rolsky, <autarch@urth.org>
=cut
|