The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

UR - rich declarative non-hierarchical transactional objects

VERSION

This document describes UR version 0.6.

SYNOPSIS

First create a Namespace class for your application, CdExample.pm

    package CdExample;
    use UR;

    class CdExample {
        is => 'UR::Namespace'
    };

    1;

Next, define a data source representing your database, CdExample/DataSource/DB1.pm

    package CdExample::DataSource::DB1;
    use CdExample;
    
    class CdExample::DataSource::DB1 {
        is => ['UR::DataSource::Mysql'],
        has_constant => [
            server  => { value => 'mysql.example.com' },
            login   => { value => 'mysqluser' },
            auth    => { value => 'mysqlpasswd' },
        ]
    };
    
    1;

Create a class to represent artists, who have many CDs, in CdExample/Artist.pm

    package CdExample::Artist;
    use CdExample;

    class CdExample::Artist {
        id_by => 'artist_id',
        has => [ 
            name => { is => 'Text' },
            cds  => { is => 'CdExample::Cd', is_many => 1, reverse_as => 'artist' }
        ],
        data_source => 'CdExample::DataSource::DB1',
        table_name => 'ARTISTS',
    };
    1;

Create a class to represent CDs, in CdExample/Cd.pm

    package CdExample::Cd;
    use CdExample;
            
    class CdExample::Cd {
        id_by => 'cd_id',
        has => [
            artist => { is => 'CdExample::Artist', id_by => 'artist_id' },
            title  => { is => 'Text' },
            year   => { is => 'Integer' },
            artist_name => { via => 'artist', to => 'name' },
        ],
        data_source => 'CdExample::DataSource::DB1',
        table_name => 'CDS',
    };
    1;

You can then use these classes in your application code

    # Enables auto-loading for modules in this Namespace
    use CdExample;  
    
    # this would get back all Artist objects
    my @all_artists = CdExample::Artist->get();

    # after the above, further requests would be cached
    # if that set were large though, you might want to iterate gradually:
    my $artist_iter = CdExample::Artist->create_iterator();

    # Get the first object off of the iterator
    my $first_artist = $artist_iter->next();

    # Get all the CDs published in 2007
    my @cds_2007 = CdExample::Cd->get(year => 2007);
    
    # Get a list of Artist objects where the name starts with 'John'
    my @some_artists = CdExample::Artist->get(
        name => { operator => 'like', value => 'John%' }
    );

    # Alternate syntax for non-equality operators
    my @same_some_artists = CdExample::Artist->get('name like' => 'John%');
    
    # This will use a JOIN with the ARTISTS table internally to filter
    # the data in the database.  @some_cds will contain CdExample::Cd objects.
    # As a side effect, related Artist objects will be loaded into the cache
    my @some_cds = CdExample::Cd->get(i
        year => '2001', 
        artist_name => { operator => 'like', value => 'Bob%' }
    );

    my @artists_for_some_cds = map { $_->artist } @some_cds;
    
    # This will use a join to prefetch Artist objects related to the
    # Cds that match the filter
    my @other_cds = CdExample::Cd->get(
        title => { operator => 'like',
        value => '%White%' },
        -hints => ['artist']
    );
    my $other_artist_0 = $other_cds[0]->artist;  # already loaded so no query
    
    # create() instantiates a new object in the cache, but does not save 
    # it in the database.  It will autogenerate its own cd_id
    my $new_cd = CdExample::Cd->create(
        title => 'Cool Album',
        year  => 2009
    );

    # Assign it to an artist; fills in the artist_id field of $new_cd
    $first_artist->add_cd($new_cd);
    
    # Save all changes back to the database
    UR::Context->current->commit;
  

DESCRIPTION

UR is a class framework and object/relational mapper for Perl. It starts with the familiar Perl meme of the blessed hash reference as the basis for object instances, and extends its capabilities with ORM (object-relational mapping) capabilities, object cache, in-memory transactions, more formal class definitions, metadata, documentation system, iterators, command line tools, etc.

UR can handle multiple column primary and foreign keys, SQL joins involving class inheritance and relationships, and does its best to avoid querying the database unless the requested data has not been loaded before. It has support for SQLite, Oracle, Mysql and Postgres databases, and the ability to use a text file as a table.

UR uses the same syntax to define non-persistent objects, and supports in-memory transactions for both.

DOCUMENTATION

Manuals

UR::Manual::Overview - UR from Ten Thousand Feet

UR::Manual::Tutorial - Getting started with UR

UR::Manual::Presentation - Slides for a presentation on UR

UR::Manual::Cookbook - Recepies for getting stuff working

UR::Manual::Metadata - UR's metadata system

UR::Object::Type::Initializer - Defining classes

UR::Manual::UR - UR's command line tool

Basic Entities

UR::Object - Pretty much everything is-a UR::Object

UR::Object::Type - Metadata class for Classes

UR::Object::Property - Metadata class for Properties

UR::Namespace - Manage packages and classes

UR::Context - Software transactions and More!

UR::DataSource - How and where to get data

Environment Variables

UR uses several environment variables to do things like run with database commits disabled, dump SQL, control cache size, etc.

See UR::Env.

DEPENDENCIES

Class::Autouse Cwd Data::Dumper Date::Calc Date::Parse DBI File::Basename FindBin FreezeThaw Path::Class Scalar::Util Sub::Installer Sub::Name Sys::Hostname Text::Diff Time::HiRes XML::Simple

AUTHORS

UR was built by the software development team at the Washington University Genome Center. Incarnations of it run laboratory automation and analysis systems for high-throughput genomics.

 Scott Smith        sakoht@cpan.org     Orginal Architecture
 Anthony Brummett   brummett@cpan.org   Primary Development

 Craig Pohl
 Todd Hepler
 Ben Oberkfell
 Kevin Crouse
 Adam Dukes
 Indraniel Das
 Shin Leong
 Eddie Belter
 Ken Swanson
 Scott Abbott
 Alice Diec
 William Schroeder
 Eric Clark
 Shawn Leonard
 Lynn Carmichael
 Jason Walker
 Amy Hawkins
 Gabe Sanderson
 James Weible
 James Eldred
 Michael Kiwala
 Mark Johnson
 Kyung Kim
 Jon Schindler
 Justin Lolofie
 Chris Harris
 Jerome Peirick
 Ryan Richt
 John Osborne
 David Dooling
 

LICENCE AND COPYRIGHT

Copyright (C) 2002-2009 Washington University in St. Louis, MO.

This sofware is licensed under the same terms as Perl itself. See the LICENSE file in this distribution.