IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 27, 2005, 6:32:21 PM (21 years ago)
Author:
jhoblitt
Message:

add do_transaction(), add_new(), & new2newed()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/PS-IPP-MetaDB/lib/PS/IPP/MetaDB.pm

    r5157 r5167  
    11# Copyright (C) 2005  Joshua Hoblitt
    22#
    3 # $Id: MetaDB.pm,v 1.1.1.1 2005-09-28 01:21:27 jhoblitt Exp $
     3# $Id: MetaDB.pm,v 1.2 2005-09-28 04:32:21 jhoblitt Exp $
    44
    55package PS::IPP::MetaDB;
     
    1313use base qw( Class::DBI::mysql );
    1414
     15use Carp qw( croak );
     16
    1517__PACKAGE__->set_db('Main', 'dbi:mysql:test', 'test', '');
     18
     19# lifted from Class::DBI docs (modified)
     20sub do_transaction {
     21    my $class = shift;
     22    my ( $code ) = @_;
     23
     24    # Turn off AutoCommit for this scope.
     25    # A commit will occur at the exit of this block automatically,
     26    # when the local AutoCommit goes out of scope.
     27    local $class->db_Main->{ AutoCommit };
     28
     29    # Execute the required code inside the transaction.
     30    eval { $code->() };
     31    if ( $@ ) {
     32        my $commit_error = $@;
     33        eval { $class->dbi_rollback }; # might also die!
     34        croak $commit_error;
     35    }
     36}
     37
     38sub add_new {
     39    my $class = shift;
     40    my %p = @_;
     41
     42    PS::IPP::MetaDB::Image::New->create({
     43        exp_id      => $p{exp_id},
     44        class_id    => $p{class_id},
     45        camera      => $p{camera},
     46        class       => $p{class},
     47        url         => $p{url},
     48    });
     49}
     50
     51
     52sub new2newed {
     53    my $class = shift;
     54    my %p = @_;
     55
     56    $class->do_transaction(sub { $class->_new2newed(%p) });
     57}
     58
     59sub _new2newed {
     60    my $class = shift;
     61    my %p = @_;
     62
     63    "${class}::Image::New"->search(
     64        exp_id      => $p{exp_id},
     65        class_id    => $p{class_id},
     66    )->delete_all;
     67
     68    "${class}::Image::Newed"->create({
     69        exp_id      => $p{exp_id},
     70        class_id    => $p{class_id},
     71        url         => $p{url},
     72    });
     73}
     74
    1675
    1776#package PS::IPP::MetaDB::Image::Prepared;
Note: See TracChangeset for help on using the changeset viewer.