Fey::Object::Schema - Base class for schema-based objects


Fey-ORM documentation Contained in the Fey-ORM distribution.

Index


Code Index:

NAME

Top

Fey::Object::Schema - Base class for schema-based objects

VERSION

Top

version 0.43

SYNOPSIS

Top

  package MyApp::Schema;

  use Fey::ORM::Schema;

  has_schema(...);

DESCRIPTION

Top

This class is a the base class for all schema-based objects.

METHODS

Top

This class provides the following methods:

$class->EnableObjectCaches()

Enables the object class for all of the table classes associated with this class's schema.

$class->DisableObjectCaches()

Disables the object class for all of the table classes associated with this class's schema.

$class->ClearObjectCaches()

Clears the object class for all of the table classes associated with this class's schema.

$class->RunInTransaction( $coderef, $source )

Given a code ref, this method will begin a transaction and execute the coderef. If the coderef runs normally (no exceptions), it commits, otherwise it rolls back and rethrows the error.

This method will handle nested transactions gracefully if your DBMS does not. It doesn't emulate actual partial commits, but it does prevent DBI from throwing an error.

The second argument can be a Fey::DBIManager::Source object. If no source is specified, then this method will use the default source.

$class->Schema()

Returns the Fey::Schema object associated with the class.

$class->DBIManager()

Returns the Fey::Schema object associated with the class.

$class->SetDBIManager($manager)

Set the Fey::DBIManager object associated with the class. If you don't set one explicitly, then the first call to $class->DBIManager() will simply create one by calling Fey::DBIManager->new().

$class->SQLFactoryClass()

Returns the SQL factory class associated with the class. This defaults to Fey::SQL.

$class->SetSQLFactoryClass()

Set the SQL factory class associated with the class.

AUTHOR

Top

Dave Rolsky <autarch@urth.org>

COPYRIGHT AND LICENSE

Top


Fey-ORM documentation Contained in the Fey-ORM distribution.

package Fey::Object::Schema;
BEGIN {
  $Fey::Object::Schema::VERSION = '0.43';
}

use strict;
use warnings;
use namespace::autoclean;

use Fey::Meta::Class::Table;

use Moose;

sub EnableObjectCaches {
    my $class = shift;

    $_->EnableObjectCache() for $class->_TableClasses();
}

sub DisableObjectCaches {
    my $class = shift;

    $_->DisableObjectCache() for $class->_TableClasses();
}

sub ClearObjectCaches {
    my $class = shift;

    $_->ClearObjectCache() for $class->_TableClasses();
}

sub _TableClasses {
    my $class = shift;

    my $schema = $class->Schema();

    return Fey::Meta::Class::Table->ClassForTable( $schema->tables() );
}

sub RunInTransaction {
    my $class  = shift;
    my $sub    = shift;
    my $source = shift || $class->DBIManager()->default_source();

    my $in_tran;

    my $dbh = $source->dbh();

    unless ( $source->allows_nested_transactions()
        || $dbh->{AutoCommit} ) {
        $in_tran = 1;
    }

    my $wantarray = wantarray;

    my @r;

    eval {
        $dbh->begin_work()
            unless $in_tran;

        if ($wantarray) {
            @r = $sub->();
        }
        elsif ( defined $wantarray ) {
            $r[0] = $sub->();
        }
        else {
            $sub->();
        }

        $dbh->commit()
            unless $in_tran;
    };

    if ( my $e = $@ ) {
        $dbh->rollback
            unless $in_tran;
        die $e;
    }

    return unless defined $wantarray;

    return $wantarray ? @r : $r[0];
}

sub Schema {
    my $class = shift;

    return $class->meta()->schema();
}

sub DBIManager {
    my $class = shift;

    return $class->meta()->dbi_manager();
}

sub SetDBIManager {
    my $class = shift;

    $class->meta()->set_dbi_manager(@_);
}

sub SQLFactoryClass {
    my $class = shift;

    return $class->meta()->sql_factory_class();
}

sub SetSQLFactoryClass {
    my $class = shift;

    $class->meta()->set_sql_factory_class(@_);
}

__PACKAGE__->meta()->make_immutable( inline_constructor => 0 );

1;

# ABSTRACT: Base class for schema-based objects




__END__