| 1 | |||
| 2 | package Xmldoom::Object; | ||
| 3 | |||
| 4 | use Xmldoom::Definition; | ||
| 5 | use Xmldoom::Object::Property; | ||
| 6 | use Xmldoom::Object::Attribute; | ||
| 7 | use Xmldoom::Object::LinkAttribute; | ||
| 8 | use Xmldoom::ResultSet; | ||
| 9 | use DBIx::Romani::Query::Function::Now; | ||
| 10 | use DBIx::Romani::Query::Function::Count; | ||
| 11 | use DBIx::Romani::Query::SQL::Literal; | ||
| 12 | use DBIx::Romani::Query::SQL::Null; | ||
| 13 | use Exception::Class::DBI; | ||
| 14 | use Exception::Class::TryCatch; | ||
| 15 | use Scalar::Util qw(weaken isweak); | ||
| 16 | use Module::Runtime qw/ use_module /; | ||
| 17 | use strict; | ||
| 18 | |||
| 19 | # define our exceptions: | ||
| 20 | use Exception::Class qw( Xmldoom::Object::RollbackException ); | ||
| 21 | |||
| 22 | use Data::Dumper; | ||
| 23 | |||
| 24 | # Connects registered class names to object definitions. We can do this | ||
| 25 | # because in Perl the class namespace is global. | ||
| 26 | our %OBJECTS; | ||
| 27 | |||
| 28 | # this will bind this class to this table | ||
| 29 | sub BindToObject | ||
| 30 | { | ||
| 31 | my $class = shift; | ||
| 32 | my $object = shift; | ||
| 33 | |||
| 34 | # assign this class name to this object | ||
| 35 | $object->set_class( $class ); | ||
| 36 | |||
| 37 | # store the definition to classname connection for future reference | ||
| 38 | $OBJECTS{$class} = $object; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub load | ||
| 42 | { | ||
| 43 | my $class = shift; | ||
| 44 | |||
| 45 | # The object definition does all of the actual work with regard to | ||
| 46 | # querying the database and getting the data. We just pass it along | ||
| 47 | # to the correct Perl class. | ||
| 48 | |||
| 49 | if ( not defined $OBJECTS{$class} ) | ||
| 50 | { | ||
| 51 | die "Cannot load() $class: No definition attached to Perl class"; | ||
| 52 | } | ||
| 53 | |||
| 54 | my $definition = $OBJECTS{$class}; | ||
| 55 | my $data = $definition->load( @_ ); | ||
| 56 | |||
| 57 | my $result = $class->new(undef, { data => $data }); | ||
| 58 | |||
| 59 | # call user hook | ||
| 60 | $result->_on_load(); | ||
| 61 | |||
| 62 | return $result; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub load_or_new | ||
| 66 | { | ||
| 67 | my ($class, $args) = @_; | ||
| 68 | |||
| 69 | my $obj; | ||
| 70 | |||
| 71 | try eval | ||
| 72 | { | ||
| 73 | $obj = $class->load( $args ); | ||
| 74 | }; | ||
| 75 | if ( my $err = catch ) | ||
| 76 | { | ||
| 77 | $obj = $class->new(); | ||
| 78 | |||
| 79 | foreach my $key_name ( @{$obj->_get_key_names()} ) | ||
| 80 | { | ||
| 81 | if ( defined $args->{$key_name} ) | ||
| 82 | { | ||
| 83 | $obj->_set_attr( $key_name, $args->{$key_name} ); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | return $obj; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub SearchRS | ||
| 92 | { | ||
| 93 | my $class = shift; | ||
| 94 | my $criteria = shift; | ||
| 95 | |||
| 96 | # if no criteria, then we want to get all items | ||
| 97 | if ( not defined $criteria ) | ||
| 98 | { | ||
| 99 | $criteria = Xmldoom::Criteria->new(); | ||
| 100 | } | ||
| 101 | |||
| 102 | # The object definition is responsible for performing the actual query | ||
| 103 | # and getting a Roma result-set back for us. | ||
| 104 | |||
| 105 | my $definition = $OBJECTS{$class}; | ||
| 106 | my $rs = $definition->search_rs( $criteria ); | ||
| 107 | |||
| 108 | # return our fully prepared result set | ||
| 109 | return Xmldoom::ResultSet->new({ | ||
| 110 | class => $class, | ||
| 111 | result => $rs, | ||
| 112 | conn => $rs->get_conn(), | ||
| 113 | parent => $criteria->get_parent() | ||
| 114 | }); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub Search | ||
| 118 | { | ||
| 119 | my $class = shift; | ||
| 120 | my $rs = $class->SearchRS( @_ ); | ||
| 121 | |||
| 122 | my @ret; | ||
| 123 | |||
| 124 | # unravel our result set | ||
| 125 | while ( $rs->next() ) | ||
| 126 | { | ||
| 127 | push @ret, $rs->get_object(); | ||
| 128 | } | ||
| 129 | |||
| 130 | return wantarray ? @ret : \@ret; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub SearchAttrsRS | ||
| 134 | { | ||
| 135 | my $class = shift; | ||
| 136 | my $criteria = shift; | ||
| 137 | |||
| 138 | # if no criteria, then we want to get all items | ||
| 139 | if ( not defined $criteria ) | ||
| 140 | { | ||
| 141 | $criteria = Xmldoom::Criteria->new(); | ||
| 142 | } | ||
| 143 | |||
| 144 | return $OBJECTS{$class}->search_attrs_rs( $criteria, @_ ); | ||
| 145 | } | ||
| 146 | |||
| 147 | sub SearchAttrs | ||
| 148 | { | ||
| 149 | my $class = shift; | ||
| 150 | my $rs = $class->SearchAttrsRS( @_ ); | ||
| 151 | |||
| 152 | my @ret; | ||
| 153 | |||
| 154 | # unravel our result set | ||
| 155 | while ( $rs->next() ) | ||
| 156 | { | ||
| 157 | push @ret, $rs->get_row(); | ||
| 158 | } | ||
| 159 | |||
| 160 | # TODO: Some reference is being held somewhere! I can't | ||
| 161 | # seem to figure this one out. | ||
| 162 | $rs->{conn}->disconnect(); | ||
| 163 | #$rs = undef; | ||
| 164 | |||
| 165 | return wantarray ? @ret : \@ret; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub SearchDistinctAttrsRS | ||
| 169 | { | ||
| 170 | my $class = shift; | ||
| 171 | my $criteria = shift; | ||
| 172 | |||
| 173 | # if no criteria, then we want to get all items | ||
| 174 | if ( not defined $criteria ) | ||
| 175 | { | ||
| 176 | $criteria = Xmldoom::Criteria->new(); | ||
| 177 | } | ||
| 178 | |||
| 179 | return $OBJECTS{$class}->search_distinct_attrs_rs( $criteria, @_ ); | ||
| 180 | } | ||
| 181 | |||
| 182 | sub SearchDistinctAttrs | ||
| 183 | { | ||
| 184 | my $class = shift; | ||
| 185 | my $rs = $class->SearchDistinctAttrsRS( @_ ); | ||
| 186 | |||
| 187 | my @ret; | ||
| 188 | |||
| 189 | # unravel our result set | ||
| 190 | while ( $rs->next() ) | ||
| 191 | { | ||
| 192 | push @ret, $rs->get_row(); | ||
| 193 | } | ||
| 194 | |||
| 195 | return wantarray ? @ret : \@ret; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub Count | ||
| 199 | { | ||
| 200 | my $class = shift; | ||
| 201 | my $criteria = shift; | ||
| 202 | |||
| 203 | # if no criteria, then we want to get all items | ||
| 204 | if ( not defined $criteria ) | ||
| 205 | { | ||
| 206 | $criteria = Xmldoom::Criteria->new(); | ||
| 207 | } | ||
| 208 | |||
| 209 | return $OBJECTS{$class}->count( $criteria ); | ||
| 210 | } | ||
| 211 | |||
| 212 | sub new | ||
| 213 | { | ||
| 214 | my $class = shift; | ||
| 215 | my $public_args = shift; | ||
| 216 | my $private_args = shift; | ||
| 217 | |||
| 218 | my $parent; | ||
| 219 | my $parent_link; | ||
| 220 | my $data; | ||
| 221 | my $sets; | ||
| 222 | |||
| 223 | if ( ref($private_args) eq "HASH" ) | ||
| 224 | { | ||
| 225 | $parent = $private_args->{parent}; | ||
| 226 | $parent_link = $private_args->{parent_link}; | ||
| 227 | $data = $private_args->{data}; | ||
| 228 | } | ||
| 229 | if ( ref($public_args) eq "HASH" ) | ||
| 230 | { | ||
| 231 | $sets = $public_args; | ||
| 232 | } | ||
| 233 | |||
| 234 | my $self = { | ||
| 235 | parent => $parent, | ||
| 236 | definition => $OBJECTS{$class}, | ||
| 237 | dependents => [ ], | ||
| 238 | original => { }, | ||
| 239 | info => { }, | ||
| 240 | key => { }, | ||
| 241 | props => [ ], | ||
| 242 | callbacks => { }, | ||
| 243 | new => 1, | ||
| 244 | }; | ||
| 245 | |||
| 246 | # weaken reference to parent | ||
| 247 | if ( defined $self->{parent} ) | ||
| 248 | { | ||
| 249 | weaken( $self->{parent} ); | ||
| 250 | } | ||
| 251 | |||
| 252 | # we are now an object | ||
| 253 | bless $self, $class; | ||
| 254 | |||
| 255 | # if we have data, then copy it into the info and key hashes. Otherwise | ||
| 256 | # we should set all the default values. | ||
| 257 | if ( defined $data ) | ||
| 258 | { | ||
| 259 | foreach my $column ( @{$self->{definition}->get_table()->get_columns()} ) | ||
| 260 | { | ||
| 261 | my $col_name = $column->get_name(); | ||
| 262 | |||
| 263 | # put in their places | ||
| 264 | $self->{info}->{$col_name} = Xmldoom::Object::Attribute->new( $data->{$col_name} ); | ||
| 265 | if ( $column->is_primary_key() ) | ||
| 266 | { | ||
| 267 | # we need to store the keys twice so that we can pivot | ||
| 268 | # on the key, if we need to change it. | ||
| 269 | $self->{key}->{$col_name} = $data->{$col_name}; | ||
| 270 | } | ||
| 271 | } | ||
| 272 | |||
| 273 | # copy info into original | ||
| 274 | $self->{original} = { %$data }; | ||
| 275 | |||
| 276 | # this is not a new object | ||
| 277 | $self->{new} = 0; | ||
| 278 | } | ||
| 279 | else | ||
| 280 | { | ||
| 281 | # set our defaults | ||
| 282 | foreach my $column ( @{$self->{definition}->get_table()->get_columns()} ) | ||
| 283 | { | ||
| 284 | $self->{info}->{$column->{name}} = Xmldoom::Object::Attribute->new( $column->{default} ); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | |||
| 288 | # link our attributes to the appropriate connections in the parent | ||
| 289 | if ( $self->{parent} ) | ||
| 290 | { | ||
| 291 | if ( not defined $parent_link ) | ||
| 292 | { | ||
| 293 | # if they aren't specified then we guess... | ||
| 294 | $parent_link = $self->{definition}->find_links( $self->{parent}->_get_object_name() )->[0]; | ||
| 295 | } | ||
| 296 | |||
| 297 | # TODO: a hack for inter_table where we won't have a parent link for now! | ||
| 298 | if ( defined $parent_link and $parent_link->get_count() == 1 ) | ||
| 299 | { | ||
| 300 | foreach my $pconn ( @{$parent_link->get_column_names()} ) | ||
| 301 | { | ||
| 302 | $self->_link_attr( $pconn->{local_column}, $self->{parent}, $pconn->{foreign_column} ); | ||
| 303 | } | ||
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 307 | # setup the properties | ||
| 308 | foreach my $prop ( @{$self->{definition}->get_properties()} ) | ||
| 309 | { | ||
| 310 | push @{$self->{props}}, Xmldoom::Object::Property->new( $prop, $self ); | ||
| 311 | } | ||
| 312 | |||
| 313 | # set the initial values | ||
| 314 | if ( defined $sets ) | ||
| 315 | { | ||
| 316 | $self->set($sets); | ||
| 317 | } | ||
| 318 | |||
| 319 | return $self; | ||
| 320 | } | ||
| 321 | |||
| 322 | sub copy | ||
| 323 | { | ||
| 324 | my $self = shift; | ||
| 325 | |||
| 326 | my $class = ref($self); | ||
| 327 | my $copy = $class->new(); | ||
| 328 | |||
| 329 | foreach my $name ( @{$self->{definition}->get_table()->get_column_names({ data_only => 1 })} ) | ||
| 330 | { | ||
| 331 | $copy->_set_attr( $name, $self->_get_attr($name) ); | ||
| 332 | } | ||
| 333 | |||
| 334 | return $copy; | ||
| 335 | } | ||
| 336 | |||
| 337 | sub _get_definition { return shift->{definition}; } | ||
| 338 | sub _get_database { return shift->{definition}->get_database(); } | ||
| 339 | sub _get_object_name { return shift->{definition}->get_name(); } | ||
| 340 | sub _get_table { return shift->{definition}->get_table(); }; | ||
| 341 | sub _get_key_names { return shift->_get_table()->get_column_names({ primary_key => 1 }); } | ||
| 342 | sub _get_data_names { return shift->_get_table()->get_column_names({ data_only => 1 }); } | ||
| 343 | sub _get_properties { return shift->{props}; } | ||
| 344 | sub _get_original { return shift->{original}; } | ||
| 345 | sub _get_key { return shift->{key}; } | ||
| 346 | |||
| 347 | sub _get_attributes | ||
| 348 | { | ||
| 349 | my $self = shift; | ||
| 350 | |||
| 351 | my $data = { }; | ||
| 352 | while ( my ($name, $attr) = each %{$self->{info}} ) | ||
| 353 | { | ||
| 354 | $data->{$name} = $attr->get(); | ||
| 355 | } | ||
| 356 | return $data; | ||
| 357 | } | ||
| 358 | |||
| 359 | sub _get_property | ||
| 360 | { | ||
| 361 | my ($self, $name) = @_; | ||
| 362 | |||
| 363 | foreach my $prop ( @{$self->_get_properties()} ) | ||
| 364 | { | ||
| 365 | if ( $prop->get_name() eq $name ) | ||
| 366 | { | ||
| 367 | return $prop; | ||
| 368 | } | ||
| 369 | } | ||
| 370 | |||
| 371 | die "There is no property named '$name' on this object"; | ||
| 372 | } | ||
| 373 | |||
| 374 | sub _get_property_recursive | ||
| 375 | { | ||
| 376 | my ($self, $name) = @_; | ||
| 377 | |||
| 378 | my $object = $self; | ||
| 379 | my @stack = split /\//, $name; | ||
| 380 | |||
| 381 | # go through all the sub-properties | ||
| 382 | while ( @stack > 1 ) | ||
| 383 | { | ||
| 384 | # get the next name | ||
| 385 | $name = shift @stack; | ||
| 386 | |||
| 387 | foreach my $prop ( @{$object->_get_properties()} ) | ||
| 388 | { | ||
| 389 | if ( $prop->get_name() eq $name ) | ||
| 390 | { | ||
| 391 | if ( not $prop->get_definition()->isa('Xmldoom::Definition::Property::Object') or | ||
| 392 | $prop->get_type() ne 'inherent' ) | ||
| 393 | { | ||
| 394 | die "Cannot _get_property() recursively through '$name' because it is not an inherent object property."; | ||
| 395 | } | ||
| 396 | |||
| 397 | # recurse, yo! | ||
| 398 | $object = $prop->get(); | ||
| 399 | } | ||
| 400 | } | ||
| 401 | } | ||
| 402 | |||
| 403 | # get the final property! | ||
| 404 | my $name = shift @stack; | ||
| 405 | my $prop = $object->_get_property($name); | ||
| 406 | |||
| 407 | # NOTE: we must return both the property and the object, because the property | ||
| 408 | # will cease to be valid as soon as the object goes out of scope. | ||
| 409 | return ( $object, $prop ); | ||
| 410 | } | ||
| 411 | |||
| 412 | sub _get_property_value | ||
| 413 | { | ||
| 414 | my $self = shift; | ||
| 415 | my $args = shift; | ||
| 416 | |||
| 417 | my $name; | ||
| 418 | my $pretty = 0; | ||
| 419 | |||
| 420 | if ( ref($args) eq 'HASH' ) | ||
| 421 | { | ||
| 422 | $name = $args->{name}; | ||
| 423 | $pretty = $args->{pretty}; | ||
| 424 | } | ||
| 425 | else | ||
| 426 | { | ||
| 427 | $name = $args; | ||
| 428 | $pretty = shift; | ||
| 429 | } | ||
| 430 | |||
| 431 | my ($object, $prop) = $self->_get_property_recursive($name); | ||
| 432 | |||
| 433 | if ( $pretty ) | ||
| 434 | { | ||
| 435 | return $prop->get_pretty(); | ||
| 436 | } | ||
| 437 | else | ||
| 438 | { | ||
| 439 | return $prop->get(); | ||
| 440 | } | ||
| 441 | } | ||
| 442 | |||
| 443 | sub _get_attr | ||
| 444 | { | ||
| 445 | my ($self, $name) = @_; | ||
| 446 | |||
| 447 | my $col = $self->{definition}->get_table()->get_column( $name ); | ||
| 448 | if ( not defined $col ) | ||
| 449 | { | ||
| 450 | die "Cannot get non-existant attribute \"$name\"."; | ||
| 451 | } | ||
| 452 | |||
| 453 | return $self->{info}->{$name}->get(); | ||
| 454 | } | ||
| 455 | |||
| 456 | sub _set_attr | ||
| 457 | { | ||
| 458 | my ($self, $name, $value) = @_; | ||
| 459 | |||
| 460 | my $col = $self->{definition}->get_table()->get_column( $name ); | ||
| 461 | if ( not defined $col ) | ||
| 462 | { | ||
| 463 | die "Cannot set non-existant attribute \"$name\"."; | ||
| 464 | } | ||
| 465 | |||
| 466 | # TODO: validate the attribute. | ||
| 467 | |||
| 468 | if ( $self->{info}->{$name}->is_local() ) | ||
| 469 | { | ||
| 470 | # we can only set attributes that are local to us. | ||
| 471 | $self->{info}->{$name}->set( $value ); | ||
| 472 | } | ||
| 473 | else | ||
| 474 | { | ||
| 475 | # if we are manually setting a link attribute, then this | ||
| 476 | # overrides it setting a local attribute. | ||
| 477 | $self->{info}->{$name} = Xmldoom::Object::Attribute->new( $value ); | ||
| 478 | } | ||
| 479 | |||
| 480 | # we are changed! | ||
| 481 | $self->_changed(); | ||
| 482 | } | ||
| 483 | |||
| 484 | sub _link_attr | ||
| 485 | { | ||
| 486 | my ($self, $local_name, $object, $foreign_name) = @_; | ||
| 487 | |||
| 488 | $self->{info}->{$local_name} = Xmldoom::Object::LinkAttribute->new( $object->{info}->{$foreign_name} ); | ||
| 489 | } | ||
| 490 | |||
| 491 | sub _register_callback | ||
| 492 | { | ||
| 493 | my ($self, $name, $cb) = @_; | ||
| 494 | |||
| 495 | if ( not defined $self->{callbacks}->{$name} ) | ||
| 496 | { | ||
| 497 | $self->{callbacks}->{$name} = [ $cb ]; | ||
| 498 | } | ||
| 499 | else | ||
| 500 | { | ||
| 501 | push @{$self->{callbacks}->{$name}}, $cb; | ||
| 502 | } | ||
| 503 | } | ||
| 504 | |||
| 505 | sub _unregister_callback | ||
| 506 | { | ||
| 507 | my ($self, $name, $cb) = @_; | ||
| 508 | |||
| 509 | if ( defined $self->{callbacks}->{$name} ) | ||
| 510 | { | ||
| 511 | for( my $i = 0; $i < scalar @{$self->{callbacks}->{$name}}; $i++ ) | ||
| 512 | { | ||
| 513 | if ( $self->{callbacks}->{$name}->[$i] == $cb ) | ||
| 514 | { | ||
| 515 | splice @{$self->{callbacks}->{$name}}, $i, 1; | ||
| 516 | last; | ||
| 517 | } | ||
| 518 | } | ||
| 519 | } | ||
| 520 | } | ||
| 521 | |||
| 522 | sub _execute_callback | ||
| 523 | { | ||
| 524 | my $self = shift; | ||
| 525 | my $name = shift; | ||
| 526 | |||
| 527 | if ( defined $self->{callbacks}->{$name} ) | ||
| 528 | { | ||
| 529 | foreach my $cb ( @{$self->{callbacks}->{$name}} ) | ||
| 530 | { | ||
| 531 | $cb->call( $cb, @_ ); | ||
| 532 | } | ||
| 533 | } | ||
| 534 | } | ||
| 535 | |||
| 536 | sub save | ||
| 537 | { | ||
| 538 | my $self = shift; | ||
| 539 | my $args = shift; | ||
| 540 | |||
| 541 | my $commit = 1; | ||
| 542 | my $conn; | ||
| 543 | |||
| 544 | if ( ref($args) eq 'HASH' ) | ||
| 545 | { | ||
| 546 | $conn = $args->{conn}; | ||
| 547 | $commit = $args->{commit} if defined $args->{commit}; | ||
| 548 | } | ||
| 549 | else | ||
| 550 | { | ||
| 551 | $conn = $args; | ||
| 552 | |||
| 553 | # DRS: dumb dumb kludge -- I hate you, Perl ... | ||
| 554 | my $tmp = shift; | ||
| 555 | $commit = $tmp if defined $tmp; | ||
| 556 | } | ||
| 557 | |||
| 558 | my $status = $self->{new} ? 'insert' : 'update'; | ||
| 559 | my $conn_owner = 0; | ||
| 560 | |||
| 561 | if ( not defined $conn ) | ||
| 562 | { | ||
| 563 | $conn = $self->{definition}->create_db_connection(); | ||
| 564 | $conn->begin(); | ||
| 565 | |||
| 566 | # we are the connection owner (or, ALL YOUR CONNECTION ARE BELONG TO US) | ||
| 567 | $conn_owner = 1; | ||
| 568 | $commit = 1; | ||
| 569 | } | ||
| 570 | |||
| 571 | try eval | ||
| 572 | { | ||
| 573 | # call the user handler | ||
| 574 | $self->_before_save( $status ); | ||
| 575 | |||
| 576 | # save yourself! | ||
| 577 | $self->do_save( $conn ); | ||
| 578 | |||
| 579 | # loop through child references and call save() | ||
| 580 | if ( defined $self->{dependents} ) | ||
| 581 | { | ||
| 582 | while ( scalar @{$self->{dependents}} ) | ||
| 583 | { | ||
| 584 | #my $child = shift @{$self->{dependents}}; | ||
| 585 | my $child = $self->{dependents}->[0]; | ||
| 586 | $child->save({ conn => $conn, commit => 0 }); | ||
| 587 | |||
| 588 | shift @{$self->{dependents}}; | ||
| 589 | } | ||
| 590 | } | ||
| 591 | |||
| 592 | # if an exception isn't thrown, we assume that all is well and commit | ||
| 593 | $conn->commit() if $commit; | ||
| 594 | }; | ||
| 595 | |||
| 596 | |||
| 597 | my $error = catch; | ||
| 598 | if ( $error ) | ||
| 599 | { | ||
| 600 | # make sure we are not attempting to rollback multiple times from the | ||
| 601 | # same transaction. | ||
| 602 | if ( not $error->isa( 'Xmldoom::Object::RollbackException' ) ) | ||
| 603 | { | ||
| 604 | # on the condition of error, we rollback() !! | ||
| 605 | $conn->rollback() if $conn; | ||
| 606 | |||
| 607 | # change the error to RollbackException so that the calling code knows | ||
| 608 | # that we have already rollback()'d. | ||
| 609 | $error = Xmldoom::Object::RollbackException->new( error => $error ); | ||
| 610 | } | ||
| 611 | } | ||
| 612 | |||
| 613 | $conn->disconnect() if $conn and $conn_owner; | ||
| 614 | $error->rethrow() if $error; | ||
| 615 | |||
| 616 | # call the user handler | ||
| 617 | $self->_on_save( $status ); | ||
| 618 | |||
| 619 | # copy current values into the orginals stuff | ||
| 620 | $self->{original} = $self->_get_attributes(); | ||
| 621 | |||
| 622 | # call the user callbacks | ||
| 623 | $self->_execute_callback("onsave", $self, $status); | ||
| 624 | } | ||
| 625 | |||
| 626 | sub do_save | ||
| 627 | { | ||
| 628 | my ($self, $conn) = @_; | ||
| 629 | |||
| 630 | my $definition = $self->{definition}; | ||
| 631 | my $table = $definition->get_table(); | ||
| 632 | my $table_name = $definition->get_table_name(); | ||
| 633 | |||
| 634 | my $query; | ||
| 635 | my $values = { }; | ||
| 636 | my $id_gen = { }; | ||
| 637 | |||
| 638 | if ( $self->{new} ) | ||
| 639 | { | ||
| 640 | $query = $definition->get_insert_query(); | ||
| 641 | foreach my $column ( @{$table->get_columns()} ) | ||
| 642 | { | ||
| 643 | my $col_name = $column->get_name(); | ||
| 644 | |||
| 645 | if ( $self->{info}->{$col_name}->is_local() and | ||
| 646 | not defined $self->{info}->{$col_name}->get() ) | ||
| 647 | { | ||
| 648 | # if the value is not defined, special behavior is required for | ||
| 649 | # some special types. | ||
| 650 | if ( $column->is_primary_key() and | ||
| 651 | ($column->is_auto_increment or $column->get_id_generator()) ) | ||
| 652 | { | ||
| 653 | if ( $column->is_auto_increment() ) | ||
| 654 | { | ||
| 655 | # use the default connection id generator | ||
| 656 | $id_gen->{$col_name} = $conn->create_id_generator(); | ||
| 657 | } | ||
| 658 | else | ||
| 659 | { | ||
| 660 | # use the module, yo! | ||
| 661 | use_module($column->get_id_generator()); | ||
| 662 | |||
| 663 | # use the custom id generator | ||
| 664 | $id_gen->{$col_name} = $column->get_id_generator()->new({ | ||
| 665 | conn => $conn, | ||
| 666 | object => $self, | ||
| 667 | table_name => $table_name, | ||
| 668 | column_name => $col_name | ||
| 669 | }); | ||
| 670 | } | ||
| 671 | |||
| 672 | if ( $id_gen->{$col_name}->is_before_insert() ) | ||
| 673 | { | ||
| 674 | my $id = $id_gen->{$col_name}->get_id(); | ||
| 675 | |||
| 676 | # stash the contents of the id in the info hash | ||
| 677 | $self->{info}->{$col_name}->set( $id ); | ||
| 678 | |||
| 679 | # put our newly found value into the query | ||
| 680 | $values->{$col_name} = DBIx::Romani::Query::SQL::Literal->new( $id ); | ||
| 681 | |||
| 682 | # discard the id generator because this is already | ||
| 683 | # taken care of. | ||
| 684 | $id_gen->{$col_name} = undef; | ||
| 685 | } | ||
| 686 | else | ||
| 687 | { | ||
| 688 | # insert null, and grab the id from the id generator | ||
| 689 | # after the insert. | ||
| 690 | $values->{$col_name} = DBIx::Romani::Query::SQL::Null->new(); | ||
| 691 | } | ||
| 692 | } | ||
| 693 | elsif ( $column->get_timestamp() ) | ||
| 694 | { | ||
| 695 | $values->{$col_name} = DBIx::Romani::Query::Function::Now->new(); | ||
| 696 | } | ||
| 697 | else | ||
| 698 | { | ||
| 699 | # else, insert a NULL! | ||
| 700 | $values->{$col_name} = DBIx::Romani::Query::SQL::Null->new(); | ||
| 701 | } | ||
| 702 | } | ||
| 703 | else | ||
| 704 | { | ||
| 705 | # straigt simple value... | ||
| 706 | $values->{$col_name} = DBIx::Romani::Query::SQL::Literal->new( $self->_get_attr($col_name) ); | ||
| 707 | } | ||
| 708 | } | ||
| 709 | } | ||
| 710 | else | ||
| 711 | { | ||
| 712 | $query = $definition->get_update_query(); | ||
| 713 | foreach my $column ( @{$table->get_columns()} ) | ||
| 714 | { | ||
| 715 | my $col_name = $column->get_name(); | ||
| 716 | |||
| 717 | # add the primary key | ||
| 718 | if ( $column->is_primary_key() ) | ||
| 719 | { | ||
| 720 | $values->{"key.$col_name"} = DBIx::Romani::Query::SQL::Literal->new( $self->{key}->{$col_name} ); | ||
| 721 | } | ||
| 722 | |||
| 723 | if ( $column->get_timestamp() eq 'current' ) | ||
| 724 | { | ||
| 725 | $values->{$col_name} = DBIx::Romani::Query::Function::Now->new(); | ||
| 726 | } | ||
| 727 | else | ||
| 728 | { | ||
| 729 | # ... and the normal values | ||
| 730 | $values->{$col_name} = DBIx::Romani::Query::SQL::Literal->new( $self->_get_attr($col_name) ); | ||
| 731 | } | ||
| 732 | } | ||
| 733 | } | ||
| 734 | |||
| 735 | # execute, yo! | ||
| 736 | #printf "save(): %s\n", $conn->generate_sql( $query, $values ); | ||
| 737 | $conn->prepare( $query )->execute( $values ); | ||
| 738 | |||
| 739 | # copy from the info, into the key, either for a newly db'd object or | ||
| 740 | # for the primary key pivot. | ||
| 741 | foreach my $col_name ( @{$table->get_column_names({ primary_key => 1 })} ) | ||
| 742 | { | ||
| 743 | if ( defined $id_gen->{$col_name} ) | ||
| 744 | { | ||
| 745 | # we saved the id generator because its a get | ||
| 746 | # after insert. So, get, now... | ||
| 747 | |||
| 748 | my $id = $id_gen->{$col_name}->get_id(); | ||
| 749 | $self->{key}->{$col_name} = $id; | ||
| 750 | $self->{info}->{$col_name}->set( $id ); | ||
| 751 | } | ||
| 752 | else | ||
| 753 | { | ||
| 754 | $self->{key}->{$col_name} = $self->{info}->{$col_name}->get(); | ||
| 755 | } | ||
| 756 | } | ||
| 757 | |||
| 758 | if ( $self->{new} ) | ||
| 759 | { | ||
| 760 | $self->{new} = 0; | ||
| 761 | } | ||
| 762 | } | ||
| 763 | |||
| 764 | sub _before_save | ||
| 765 | { | ||
| 766 | my ($self, $type) = @_; | ||
| 767 | |||
| 768 | # Virtual. | ||
| 769 | } | ||
| 770 | |||
| 771 | sub _on_save | ||
| 772 | { | ||
| 773 | my ($self, $type) = @_; | ||
| 774 | |||
| 775 | # Virtual. | ||
| 776 | } | ||
| 777 | |||
| 778 | sub _on_load | ||
| 779 | { | ||
| 780 | my ($self, $type) = @_; | ||
| 781 | |||
| 782 | # Virtual. | ||
| 783 | } | ||
| 784 | |||
| 785 | sub delete | ||
| 786 | { | ||
| 787 | my $self = shift; | ||
| 788 | |||
| 789 | # TODO: cascading deletes are cool too... | ||
| 790 | |||
| 791 | my $definition = $self->{definition}; | ||
| 792 | my $table = $definition->get_table(); | ||
| 793 | |||
| 794 | my $query = $definition->get_delete_query(); | ||
| 795 | |||
| 796 | my %values; | ||
| 797 | foreach my $column ( @{$table->get_columns({ primary_key => 1 })} ) | ||
| 798 | { | ||
| 799 | $values{$column->{name}} = DBIx::Romani::Query::SQL::Literal->new( $self->{key}->{$column->{name}} ); | ||
| 800 | } | ||
| 801 | |||
| 802 | my $conn = $definition->create_db_connection(); | ||
| 803 | |||
| 804 | # TODO: add error checking if ever we implement cascading deletes | ||
| 805 | |||
| 806 | $conn->prepare( $query )->execute( \%values ); | ||
| 807 | |||
| 808 | $conn->disconnect(); | ||
| 809 | } | ||
| 810 | |||
| 811 | # a private function that adds a child to list of dependent objects. This should only | ||
| 812 | # be called by the child itself when it has changed. | ||
| 813 | sub _add_dependent | ||
| 814 | { | ||
| 815 | my ($self, $child) = @_; | ||
| 816 | |||
| 817 | # don't double add children | ||
| 818 | foreach my $dep ( @{$self->{dependents}} ) | ||
| 819 | { | ||
| 820 | if ( $child == $dep ) | ||
| 821 | { | ||
| 822 | return; | ||
| 823 | } | ||
| 824 | } | ||
| 825 | push @{$self->{dependents}}, $child; | ||
| 826 | } | ||
| 827 | |||
| 828 | # manually marks this object as changed | ||
| 829 | sub _changed | ||
| 830 | { | ||
| 831 | my $self = shift; | ||
| 832 | |||
| 833 | # we tell our parent that we are modified | ||
| 834 | if ( defined $self->{parent} ) | ||
| 835 | { | ||
| 836 | $self->{parent}->_add_dependent($self); | ||
| 837 | } | ||
| 838 | } | ||
| 839 | |||
| 840 | sub set | ||
| 841 | { | ||
| 842 | my $self = shift; | ||
| 843 | my $args = shift; | ||
| 844 | |||
| 845 | foreach my $prop ( @{$self->{props}} ) | ||
| 846 | { | ||
| 847 | my $prop_name = $prop->get_name(); | ||
| 848 | if ( exists $args->{$prop_name} ) | ||
| 849 | { | ||
| 850 | $prop->set( $args->{$prop_name} ); | ||
| 851 | delete $args->{$prop_name}; | ||
| 852 | } | ||
| 853 | } | ||
| 854 | |||
| 855 | if ( scalar keys %$args ) | ||
| 856 | { | ||
| 857 | my $unknown = join ", ", keys %$args; | ||
| 858 | die "Unknown properties: $unknown"; | ||
| 859 | } | ||
| 860 | } | ||
| 861 | |||
| 862 | sub get | ||
| 863 | { | ||
| 864 | my $self = shift; | ||
| 865 | |||
| 866 | my $values = { }; | ||
| 867 | |||
| 868 | foreach my $prop ( @{$self->{props}} ) | ||
| 869 | { | ||
| 870 | $values->{$prop->get_name()} = $prop->get(); | ||
| 871 | } | ||
| 872 | |||
| 873 | return $values; | ||
| 874 | } | ||
| 875 | |||
| 876 | sub AUTOLOAD | ||
| 877 | { | ||
| 878 | my $self = shift; | ||
| 879 | my $function = our $AUTOLOAD; | ||
| 880 | |||
| 881 | if ( defined $self and $self->isa('Xmldoom::Object') ) | ||
| 882 | { | ||
| 883 | # remove the package name | ||
| 884 | $function =~ s/.*:://; | ||
| 885 | |||
| 886 | foreach my $prop ( @{$self->{props}} ) | ||
| 887 | { | ||
| 888 | foreach my $autoload_name ( @{$prop->get_autoload_list()} ) | ||
| 889 | { | ||
| 890 | if ( $function eq $autoload_name ) | ||
| 891 | { | ||
| 892 | return $prop->autoload( $function, @_ ); | ||
| 893 | } | ||
| 894 | } | ||
| 895 | } | ||
| 896 | } | ||
| 897 | |||
| 898 | die sprintf "%s not a valid property function of %s.", $function, ref($self); | ||
| 899 | } | ||
| 900 | |||
| 901 | sub DESTROY | ||
| 902 | { | ||
| 903 | # TODO: some kind of clean-up? | ||
| 904 | } | ||
| 905 | |||
| 906 | 1; | ||
| 907 | |||
| 908 | __END__ |