UR - the base module for the UR framework
This document describes UR version 0.02
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;
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::Manual lists the other documentation pages in the UR distribution
UR uses several environment variables to change its behavior.
When true, has the effect of turning any die() into a Carp::confess, meaning a stack dump will be printed after the die message.
When true, has the effect of turning any warn() into a Carp::cluck, meaning a stack dump will be printed after the warn message.
The name of the Root context to instantiate when the program initializes. The default is UR::Context::DefaultRoot. Other Root Contexts can be used, for example, to connect to alternate databases when running in test mode.
This value only changes in a sub-process which goes to its parent process for object I/O instead of the root (which is the default value for the base context in an application).
Set the object count highwater mark for the object cache pruner. See also "object_cache_size_highwater" in UR::Context
Set the object count lowwater mark for the object cache pruner. See also "object_cache_size_lowwater" in UR::Context
If this is true, most interactions with data sources such as connecting, disconnecting and querying will print messages to STDERR. Same as UR::DBI->monitor_sql(). Note that this affects non-DBI data sources as well, such as file-based data sources, which will render file I/O information instead of SQL.
UR::DBI->monitor_sql()
Used in conjunction with UR_DBI_MONITOR_SQL, tells the data sources to also print messages to STDERR for each row fetched from the underlying data source. Same as UR::DBI->monitor_every_fetch().
UR::DBI->monitor_every_fetch()
Print a message to STDERR only when connecting to an underlying data source. Same as UR::DBI->dump_stack_on_connect()
UR::DBI->dump_stack_on_connect()
If the query to a data source matches the given string (interpreted as a regex), then it will attempt to do an "explain plan" and print the results before executing the query. Same as UR::DBI->explain_sql_match()
UR::DBI->explain_sql_match()
If the time between a prepare and the first fetch of a query is longer than the given number of seconds, then it will do an "explain plan" and print the results. Same as UR::DBI->explain_sql_slow()
UR::DBI->explain_sql_slow()
Used in conjunction with UR_DBI_EXPLAIN_SQL_MATCH and UR_DBI_EXPLAIN_SQL_SLOW, prints a stack trace with Carp::longmess. Same as UR::DBI->explain_sql_callstack()
UR::DBI->explain_sql_callstack()
Like UR_DBI_MONITOR_SQL, but only prints information during data-altering statements, like INSERT, UPDATE or DELETE. Same as UR::DBI->monitor_dml()
UR::DBI->monitor_dml()
If true, data source commits will be ignored. Note that saving still occurs. If you are working with a RDBMS database, this means During UR::Context->commit(), the insert, update and delete SQL statements will be issued, but the changes will not be committed. Useful for testing. Same as UR::DBI->no_commit()
UR::DBI->no_commit()
If true, objects created without ID params will use a special algorithm to generate IDs. Objects with these special IDs will never be saved to a data source. Useful during testing. Same as UR::DataSource->use_dummy_autogenerated_ids
UR::DataSource->use_dummy_autogenerated_ids
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
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
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.
To install UR, copy and paste the appropriate command in to your terminal.
cpanm
cpanm UR
CPAN shell
perl -MCPAN -e shell install UR
For more information on module installation, please visit the detailed CPAN module installation guide.