| 1 | package Oryx::Class; | ||
| 2 | |||
| 3 | use Carp qw(carp croak); | ||
| 4 | use UNIVERSAL qw(isa can); | ||
| 5 | use Scalar::Util qw(weaken); | ||
| 6 | use warnings; | ||
| 7 | use strict; | ||
| 8 | no strict 'refs'; | ||
| 9 | |||
| 10 | use base qw(Class::Data::Inheritable Class::Observable); | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Oryx::Class - abstract base class for Oryx classes | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | # define a persistent class | ||
| 19 | package CMS::Page; | ||
| 20 | use base qw(Oryx::Class); | ||
| 21 | |||
| 22 | # ... class meta-data here (see DEFINING CLASS META-DATA below) ... | ||
| 23 | |||
| 24 | 1; | ||
| 25 | |||
| 26 | #=========================================================================== | ||
| 27 | # use a persistent class | ||
| 28 | use CMS::Page; | ||
| 29 | |||
| 30 | $page = CMS::Page->create({title => 'Life in the Metaverse'}); | ||
| 31 | $page = CMS::Page->retrieve($id); | ||
| 32 | |||
| 33 | $page->update; | ||
| 34 | $page->delete; | ||
| 35 | |||
| 36 | @pages = CMS::Page->search({author => 'Richard Hun%'}, \@order, $limit, $offset); | ||
| 37 | |||
| 38 | #=========================================================================== | ||
| 39 | # commit your changes | ||
| 40 | $page->dbh->commit; # or simply ... | ||
| 41 | $page->commit; | ||
| 42 | |||
| 43 | #=========================================================================== | ||
| 44 | # attribute mutator | ||
| 45 | $page->title('The Metamanic Mechanic'); | ||
| 46 | $tite = $page->title; | ||
| 47 | |||
| 48 | #=========================================================================== | ||
| 49 | # reference association mutator | ||
| 50 | $template_obj = $page->template; | ||
| 51 | $page->template( $template_obj ); | ||
| 52 | |||
| 53 | #=========================================================================== | ||
| 54 | # array association accessor | ||
| 55 | $page->paragraphs->[0] = $intro_para; | ||
| 56 | $paragraph = $page->paragraphs->[42]; | ||
| 57 | |||
| 58 | #=========================================================================== | ||
| 59 | # array association operators | ||
| 60 | $concl = pop @{$page->paragraphs}; | ||
| 61 | $intro = shift @{$page->paragraphs}; | ||
| 62 | push @{$page->paragraphs}, $concl; | ||
| 63 | unshift @{$page->paragraphs}, $new_intro; | ||
| 64 | splice @{$page->paragraphs}, 1, 4, ($summary); | ||
| 65 | |||
| 66 | #=========================================================================== | ||
| 67 | # hash association accessor | ||
| 68 | $image_obj = $page->images->{logo}; | ||
| 69 | $page->images->{mug_shot} = $my_ugly_mug; | ||
| 70 | @keys = keys %{$page->images}; | ||
| 71 | @values = values %{$page->images}; | ||
| 72 | |||
| 73 | #=========================================================================== | ||
| 74 | # support for Class::Observable | ||
| 75 | Page->add_observer(sub { | ||
| 76 | my ($item, $action) = @_; | ||
| 77 | #... | ||
| 78 | }); | ||
| 79 | $page->add_observer(...); # instance | ||
| 80 | |||
| 81 | =head1 DESCRIPTION | ||
| 82 | |||
| 83 | Abstract base class for Oryx persistent classes. | ||
| 84 | |||
| 85 | =head1 ABSTRACT METHODS | ||
| 86 | |||
| 87 | These methods are overridden by the implementing Class class, i.e. | ||
| 88 | L<Oryx::DBI::Class> or L<Oryx::DBM::Class>, for example, but the | ||
| 89 | interfaces stay the same, so they are documented here. | ||
| 90 | |||
| 91 | =over | ||
| 92 | |||
| 93 | =item B<create( \%proto )> | ||
| 94 | |||
| 95 | creates a persistent object using C<\%proto> to set up the initial state. | ||
| 96 | |||
| 97 | =item B<retrieve( $oid )> | ||
| 98 | |||
| 99 | retrieves an object from storage by its object id | ||
| 100 | |||
| 101 | =item B<update> | ||
| 102 | |||
| 103 | updates storage to persist and reflect changes in the object | ||
| 104 | |||
| 105 | =item B<delete> | ||
| 106 | |||
| 107 | deletes the object from storage | ||
| 108 | |||
| 109 | =item B<search( \%param, [ \@order, $limit, $offset ] )> | ||
| 110 | |||
| 111 | searches for objects with fields matching C<\%param>. SQL style | ||
| 112 | C<%> wildcards are supported. C<\@order>, C<$limit> and C<$offset> are | ||
| 113 | optional. C<\@order> is a list of columns which are used to sort the | ||
| 114 | results, C<$limit> is an integer which is used to limit the number of | ||
| 115 | results, and C<$offset> is used to exclude the first results up to | ||
| 116 | that number. These last two arguments are useful for paging through | ||
| 117 | search results. | ||
| 118 | |||
| 119 | =item B<commit> | ||
| 120 | |||
| 121 | commits the transaction if your database supports it and AutoCommit | ||
| 122 | is disabled, then you must do this. | ||
| 123 | |||
| 124 | =back | ||
| 125 | |||
| 126 | =head2 Observers | ||
| 127 | |||
| 128 | Oryx::Class objects now unherit from L<Class::Observable> thereby | ||
| 129 | implementing a publish/subscribe system similar to triggers. | ||
| 130 | |||
| 131 | The signals are named according to the 6 interface methods prefixed with | ||
| 132 | I<before_*> and I<after_*>, so the following signals are sent: | ||
| 133 | |||
| 134 | =over 4 | ||
| 135 | |||
| 136 | =item before_create | ||
| 137 | |||
| 138 | Handler is passed a hashref as argument with fields: C<param>, the search parameters, and C<query>, the L<SQL::Abstract> where clause | ||
| 139 | |||
| 140 | =item after_create | ||
| 141 | |||
| 142 | Handler is passed a hashref as argument with fields: C<param>, the search parameters, and C<proto>, the hashref which will be blessed into an instance of this class (during 'construct') | ||
| 143 | |||
| 144 | =item before_retrieve | ||
| 145 | |||
| 146 | Handler is passed a hashref as argument with fields: C<id>, the id of the object to fetch, and C<query>, the L<SQL::Abstract> where clause | ||
| 147 | |||
| 148 | =item after_retrieve | ||
| 149 | |||
| 150 | Handler is passed a hashref as argument with fields: C<proto>, the hashref which will be blessed into an instance of this class (during 'construct') | ||
| 151 | |||
| 152 | =item before_update | ||
| 153 | |||
| 154 | Handler is passed a hashref as argument with fields: C<query>, the L<SQL::Abstract> where clause. | ||
| 155 | |||
| 156 | =item after_update | ||
| 157 | |||
| 158 | Handler takes no arguments. | ||
| 159 | |||
| 160 | =item before_delete | ||
| 161 | |||
| 162 | Handler is passed a hashref as argument with fields: C<query>, the L<SQL::Abstract> where clause. | ||
| 163 | |||
| 164 | =item after_delete | ||
| 165 | |||
| 166 | Handler takes no arguments. | ||
| 167 | |||
| 168 | =item before_search | ||
| 169 | |||
| 170 | Handler is passed a hashref as argument with fields: C<query>, the L<SQL::Abstract> where clause, C<param>, the search parameters, the C<order> and C<limit> parameters. | ||
| 171 | |||
| 172 | =item after_search | ||
| 173 | |||
| 174 | Handler is passed a hashref as argument with fields: C<query>, the L<SQL::Abstract> where clause, C<param>, the search parameters, the C<order> and C<limit> parameters, and C<objects>, an arrayref of objects returned by the search. | ||
| 175 | |||
| 176 | =item before_construct | ||
| 177 | |||
| 178 | Handler is passed a hashref as argument with fields: C<proto>, the hashref which will be blessed into an instance of this class. | ||
| 179 | |||
| 180 | =item after_construct | ||
| 181 | |||
| 182 | Handler is passed a hashref as argument with fields: C<object>, the persistent object. | ||
| 183 | |||
| 184 | =back | ||
| 185 | |||
| 186 | |||
| 187 | =cut | ||
| 188 | |||
| 189 | use vars qw($XML_DOM_Lite_Is_Available); | ||
| 190 | |||
| 191 | BEGIN { | ||
| 192 | __PACKAGE__->mk_classdata("auto_deploy"); | ||
| 193 | __PACKAGE__->mk_classdata("dont_cache"); | ||
| 194 | |||
| 195 | $XML_DOM_Lite_Is_Available = 1; | ||
| 196 | eval "use XML::DOM::Lite qw(Parser Node :constants);"; | ||
| 197 | $XML_DOM_Lite_Is_Available = 0 if $@; | ||
| 198 | } | ||
| 199 | |||
| 200 | our $DEBUG = 0; | ||
| 201 | our $PARSER; | ||
| 202 | |||
| 203 | sub parser { | ||
| 204 | $PARSER = Parser->new( whitespace => 'strip' ) unless defined $PARSER; | ||
| 205 | $PARSER; | ||
| 206 | } | ||
| 207 | |||
| 208 | our %Orphans; | ||
| 209 | |||
| 210 | # Object Cache | ||
| 211 | our %Live_Objects; | ||
| 212 | |||
| 213 | =head1 METHODS | ||
| 214 | |||
| 215 | These methods are concrete. | ||
| 216 | |||
| 217 | =over | ||
| 218 | |||
| 219 | =item init | ||
| 220 | |||
| 221 | Initialises the class data (see L<Class::Data::Inheritable>) | ||
| 222 | |||
| 223 | =cut | ||
| 224 | |||
| 225 | sub init { | ||
| 226 | my $class = shift; | ||
| 227 | $DEBUG && $class->_carp('initializing class data'); | ||
| 228 | |||
| 229 | # set up class data accessors : | ||
| 230 | $class->mk_classdata("_meta"); | ||
| 231 | $class->mk_classdata("attributes"); | ||
| 232 | $class->mk_classdata("associations"); | ||
| 233 | $class->mk_classdata("methods"); | ||
| 234 | $class->mk_classdata("parents"); | ||
| 235 | |||
| 236 | # DATA section cache | ||
| 237 | $class->mk_classdata('dataNode'); | ||
| 238 | |||
| 239 | $class->meta({}); | ||
| 240 | $class->attributes({}); | ||
| 241 | $class->associations({}); | ||
| 242 | $class->methods({}); | ||
| 243 | $class->parents([]); | ||
| 244 | |||
| 245 | ${ $class.'::__initialized' } = 1; | ||
| 246 | } | ||
| 247 | |||
| 248 | =item import | ||
| 249 | |||
| 250 | Does the work of constructing the class' meta-instances | ||
| 251 | (L<Oryx::Attribute>, L<Oryx::Association> and L<Oryx::Parent> | ||
| 252 | instances) from the C<$shema> class variable or defined | ||
| 253 | in the DATA section of the module if you have L<XML::DOM::Lite> | ||
| 254 | installed. | ||
| 255 | |||
| 256 | =cut | ||
| 257 | |||
| 258 | sub import { | ||
| 259 | my $class = shift; | ||
| 260 | my %param = @_; | ||
| 261 | |||
| 262 | $DEBUG>1 && $class->_carp("importing..."); | ||
| 263 | |||
| 264 | if ($class eq __PACKAGE__ and defined $param{auto_deploy}) { | ||
| 265 | $class->auto_deploy($param{auto_deploy}); | ||
| 266 | return; # not interested in doing anything further | ||
| 267 | } | ||
| 268 | return if $class eq __PACKAGE__ | ||
| 269 | or $class eq 'Oryx::MetaClass' | ||
| 270 | or $class =~ /Oryx::[^:]+::Class/; | ||
| 271 | |||
| 272 | if (${ $class.'::__initialized' }) { | ||
| 273 | $DEBUG>1 && $class->_carp('already initialized, returning'); | ||
| 274 | return; | ||
| 275 | } | ||
| 276 | |||
| 277 | if (can($class, 'storage') and $class->storage) { | ||
| 278 | if (%Orphans) { | ||
| 279 | foreach (keys %Orphans) { | ||
| 280 | $DEBUG>1 && $_->_carp('YAY! I am no longer an Orphan'); | ||
| 281 | $class->storage->schema->addClass($_) | ||
| 282 | } | ||
| 283 | %Orphans = (); | ||
| 284 | } | ||
| 285 | $class->storage->schema->addClass($class); | ||
| 286 | } else { | ||
| 287 | $DEBUG && $class->_carp("no storage available Orphaning"); | ||
| 288 | $Orphans{$class}++; | ||
| 289 | $class->auto_deploy($param{auto_deploy}) | ||
| 290 | if defined $param{auto_deploy}; | ||
| 291 | return; | ||
| 292 | } | ||
| 293 | |||
| 294 | # initialise class data | ||
| 295 | $class->init; | ||
| 296 | $DEBUG && $class->_carp("setting up..."); | ||
| 297 | |||
| 298 | # first set up parent relationships (this doesn't *have* to be | ||
| 299 | # done first, but I believe that the chicken came before the | ||
| 300 | # egg... the are, as always, good semantic and performance reasons | ||
| 301 | # behind this belief... if not behind this particular fragment of | ||
| 302 | # code being here instead of at the bottom of this function). | ||
| 303 | foreach (@{$class.'::ISA'}) { | ||
| 304 | # only if the superclass is a subclass of Oryx::DBx::Class | ||
| 305 | if (isa($_, __PACKAGE__) | ||
| 306 | and $_ ne __PACKAGE__ | ||
| 307 | and $_ !~ /Oryx::[^:]+::Class/) { | ||
| 308 | $class->addParent($_); | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | my $schema; | ||
| 313 | unless ($schema = ${$class.'::schema'}) { | ||
| 314 | my $xmldata = $class->parseDataIO; | ||
| 315 | eval(q{use Oryx::Schema::Generator}); | ||
| 316 | die $@ if $@; | ||
| 317 | $schema = Oryx::Schema::Generator->generate($class, $xmldata); | ||
| 318 | } | ||
| 319 | if ($schema) { | ||
| 320 | $class->name($schema->{name}) if defined $schema->{name}; | ||
| 321 | foreach (@{$schema->{attributes}}) { | ||
| 322 | $class->addAttribute($_); | ||
| 323 | } | ||
| 324 | foreach (@{$schema->{associations}}) { | ||
| 325 | $class->addAssociation($_); | ||
| 326 | } | ||
| 327 | foreach (@{$schema->{methods}}) { | ||
| 328 | $class->addMethod($_); | ||
| 329 | } | ||
| 330 | } | ||
| 331 | if ($class->auto_deploy or $param{auto_deploy}) { | ||
| 332 | unless ($class->storage->util->table_exists( | ||
| 333 | $class->dbh, $class->table)) { | ||
| 334 | $class->storage->deploy_class($class); | ||
| 335 | } | ||
| 336 | } | ||
| 337 | if ($param{dont_cache}) { $class->dont_cache(1) } | ||
| 338 | } | ||
| 339 | |||
| 340 | =item meta | ||
| 341 | |||
| 342 | Simple accessor to the class meta data. | ||
| 343 | |||
| 344 | =cut | ||
| 345 | |||
| 346 | sub meta { | ||
| 347 | my $class = shift; | ||
| 348 | $class->_meta(shift) if @_; | ||
| 349 | $class->_meta; | ||
| 350 | } | ||
| 351 | |||
| 352 | =item construct( $class, $proto ) | ||
| 353 | |||
| 354 | This is typically called from within the C<create> and C<retrieve> | ||
| 355 | methods of the implementation class (L<Oryx::DBI::Class> or | ||
| 356 | <Oryx::DBM::Class, for example) which then blesses C<$proto> | ||
| 357 | into C<$class> and then allows each class meta-instance to frobnicate | ||
| 358 | it in turn if they have any need to, before handing the instance to | ||
| 359 | you. | ||
| 360 | |||
| 361 | =cut | ||
| 362 | |||
| 363 | sub construct { | ||
| 364 | my ($class, $proto) = @_; | ||
| 365 | |||
| 366 | my $object; | ||
| 367 | my $key = $class->_mk_cache_key($proto->{id}); | ||
| 368 | return $object if ($object = $Live_Objects{$key}); | ||
| 369 | |||
| 370 | $class->notify_observers('before_construct', { proto => $proto }); | ||
| 371 | $object = bless $proto, $class; | ||
| 372 | |||
| 373 | $_->construct($object) foreach $class->members; | ||
| 374 | $_->construct($object) foreach @{$class->parents}; | ||
| 375 | |||
| 376 | $class->notify_observers('after_construct', { object => $object }); | ||
| 377 | $DEBUG && $class->_carp("constructing $object id => ".$object->id); | ||
| 378 | |||
| 379 | weaken($Live_Objects{$key} = $object) unless $object->dont_cache; | ||
| 380 | return $object; | ||
| 381 | } | ||
| 382 | |||
| 383 | =item addAttribute( $meta ) | ||
| 384 | |||
| 385 | Creates an Attribute meta-instance and associates it with the class. | ||
| 386 | |||
| 387 | =cut | ||
| 388 | |||
| 389 | sub addAttribute { | ||
| 390 | my ($class, $meta) = @_; | ||
| 391 | my $attrib = | ||
| 392 | (ref($class->storage).'::Attribute')->new($meta, $class); | ||
| 393 | $class->attributes->{$attrib->name} = $attrib; | ||
| 394 | } | ||
| 395 | |||
| 396 | =item addAssociation( $meta ) | ||
| 397 | |||
| 398 | Creates an Association meta-instance and associates it with the class. | ||
| 399 | |||
| 400 | =cut | ||
| 401 | |||
| 402 | sub addAssociation { | ||
| 403 | my ($class, $meta) = @_; | ||
| 404 | my $assoc = | ||
| 405 | (ref($class->storage).'::Association')->new($meta, $class); | ||
| 406 | $class->associations->{$assoc->role} = $assoc; | ||
| 407 | } | ||
| 408 | |||
| 409 | =item addMethod( $meta ) | ||
| 410 | |||
| 411 | Does nothing at the moment as I cannot decide what such a method | ||
| 412 | would be used for exactly. | ||
| 413 | |||
| 414 | =cut | ||
| 415 | |||
| 416 | sub addMethod { | ||
| 417 | my ($class, $meta) = @_; | ||
| 418 | my $methd = | ||
| 419 | (ref($class->storage).'::Method')->new($meta, $class); | ||
| 420 | $class->methods->{$methd->name} = $methd; | ||
| 421 | } | ||
| 422 | |||
| 423 | =item addParent( $super ) | ||
| 424 | |||
| 425 | Creates a Parent meta-instance and associates it with the class. | ||
| 426 | |||
| 427 | =cut | ||
| 428 | |||
| 429 | sub addParent { | ||
| 430 | my ($class, $super) = @_; | ||
| 431 | push @{$class->parents}, | ||
| 432 | (ref($class->storage).'::Parent')->new($super, $class); | ||
| 433 | } | ||
| 434 | |||
| 435 | =item id | ||
| 436 | |||
| 437 | Returns the object id. | ||
| 438 | |||
| 439 | =cut | ||
| 440 | |||
| 441 | sub id { $_[0]->{id} } | ||
| 442 | |||
| 443 | =item is_abstract | ||
| 444 | |||
| 445 | True if the class does not define any attributes. This is used | ||
| 446 | for creating a special table for sharing sequences accross subclasses | ||
| 447 | and for instantiating the correct subclass instance if C<retrieve()> | ||
| 448 | is called on an abstract class. | ||
| 449 | |||
| 450 | =cut | ||
| 451 | |||
| 452 | sub is_abstract { | ||
| 453 | my $class = shift; | ||
| 454 | return not %{$class->attributes}; | ||
| 455 | } | ||
| 456 | |||
| 457 | =item table | ||
| 458 | |||
| 459 | Returns the table name for this class. | ||
| 460 | |||
| 461 | =cut | ||
| 462 | |||
| 463 | sub table { | ||
| 464 | my $class = shift; | ||
| 465 | unless (defined $class->meta->{table}) { | ||
| 466 | $class->meta->{table} = $class->schema->prefix.$class->name; | ||
| 467 | } | ||
| 468 | $class->meta->{table}; | ||
| 469 | } | ||
| 470 | |||
| 471 | =item name([ $name ]) | ||
| 472 | |||
| 473 | Get or set the C<name> meta-attribute for the class. | ||
| 474 | |||
| 475 | =cut | ||
| 476 | |||
| 477 | sub name { | ||
| 478 | my $class = shift; | ||
| 479 | my $name = shift; | ||
| 480 | $class->setMetaAttribute("name", $name) if $name; | ||
| 481 | unless (defined $class->getMetaAttribute("name")) { | ||
| 482 | $class =~ /([^:]+)$/; | ||
| 483 | $class->setMetaAttribute("name", lc("$1")); | ||
| 484 | } | ||
| 485 | $class->getMetaAttribute("name"); | ||
| 486 | } | ||
| 487 | |||
| 488 | =item members | ||
| 489 | |||
| 490 | Return a list of all meta-instances (Attribute, Association, Method | ||
| 491 | and Parent instances). | ||
| 492 | |||
| 493 | =cut | ||
| 494 | |||
| 495 | sub members { | ||
| 496 | my $class = shift; | ||
| 497 | return ( | ||
| 498 | values %{$class->attributes}, | ||
| 499 | values %{$class->associations}, | ||
| 500 | values %{$class->methods}, | ||
| 501 | # not really members, but we'll treat the same | ||
| 502 | #@{$class->parents}, | ||
| 503 | ); | ||
| 504 | } | ||
| 505 | |||
| 506 | =item commit | ||
| 507 | |||
| 508 | calls $self->dbh->commit to commit the trasaction | ||
| 509 | |||
| 510 | =cut | ||
| 511 | |||
| 512 | sub commit { $_[0]->dbh->commit } | ||
| 513 | |||
| 514 | =item schema | ||
| 515 | |||
| 516 | shortcut for $self->storage->schema. Read only. | ||
| 517 | |||
| 518 | =cut | ||
| 519 | |||
| 520 | sub schema { $_[0]->storage->schema } | ||
| 521 | |||
| 522 | sub parseDataIO { | ||
| 523 | my ($class) = @_; | ||
| 524 | unless ($XML_DOM_Lite_Is_Available) { | ||
| 525 | $class->_carp('XML DATA schemas are not supported unless' | ||
| 526 | .' you have XML::DOM::Lite installed'); | ||
| 527 | return undef; | ||
| 528 | } | ||
| 529 | my $stream = $class->loadDataIO; | ||
| 530 | if ($stream) { | ||
| 531 | return $class->parser->parse($stream)->documentElement; | ||
| 532 | } else { | ||
| 533 | return undef; | ||
| 534 | } | ||
| 535 | } | ||
| 536 | |||
| 537 | sub loadDataIO { | ||
| 538 | my $class = shift; | ||
| 539 | my $fh = *{"$class\::DATA"}{IO}; | ||
| 540 | return undef unless $fh; | ||
| 541 | local $/ = undef; | ||
| 542 | my $stream = <$fh>; | ||
| 543 | return $stream; | ||
| 544 | } | ||
| 545 | |||
| 546 | =item remove_from_cache | ||
| 547 | |||
| 548 | Object method to remove it from the memory cache. | ||
| 549 | |||
| 550 | =cut | ||
| 551 | |||
| 552 | sub remove_from_cache { | ||
| 553 | my $self = shift; | ||
| 554 | my $key = $self->_mk_cache_key($self->id); | ||
| 555 | CORE::delete( $Live_Objects{$key} ); | ||
| 556 | } | ||
| 557 | |||
| 558 | sub _mk_cache_key { | ||
| 559 | my $class = ref($_[0]) || $_[0]; | ||
| 560 | my $id = $_[1]; | ||
| 561 | return join('|', ( $class, $id )); | ||
| 562 | } | ||
| 563 | |||
| 564 | sub _carp { | ||
| 565 | my $thing = ref($_[0]) ? ref($_[0]) : $_[0]; | ||
| 566 | carp("[$thing] $_[1]"); | ||
| 567 | } | ||
| 568 | |||
| 569 | sub _croak { | ||
| 570 | my $thing = ref($_[0]) ? ref($_[0]) : $_[0]; | ||
| 571 | croak("[$thing] $_[1]"); | ||
| 572 | } | ||
| 573 | |||
| 574 | sub DESTROY { $_[0]->remove_from_cache } | ||
| 575 | |||
| 576 | 1; | ||
| 577 | __END__ |