NAME

UR - The base module for the UR framework

VERSION

This document describes UR version 0.02

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/DB.pm

package CdExample::DataSource::DB;
use CdExample;

class CdExample::DataSource::DB {
    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::DB',
    table_name => 'CDS',
};
1;

You can then use these classes in your application code

  use CdExample;  # Enables auto-loading for modules in this Namespace
  
  # get back all Artist objects
  my @all_artists = CdExample::Artist->get();
  # Iterate through Artist objects
  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 1997
  my @cds_1997 = CdExample::Cd->get(year => 1997);
  
  # 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(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->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.

DOCUMENTATION

UR::Manual lists the other documentation pages in the UR distribution

Environment Variables

UR uses several environment variables to change its behavior. 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.