=head1 NAME Tangram::Storage - persistent object database =head1 SYNOPSIS use Tangram; $storage = Tangram::Storage->connect( $schema, $data_source, $username, $password ); $oid = $storage->insert( $obj ); @oids = $storage->insert( @objs ); $storage->update( $obj ); $storage->update( @objs ); $obj = $storage->load( $oid ); @objs = $storage->load( @oids ); @objs = $storage->select( $class ); @objs = $storage->select( $remote, $filter ); $cursor = $storage->cursor( $remote, $filter ); if ($storage->oid_isa($oid, "ClassName")) { # oid $oid is a ClassName } $storage->disconnect(); =head1 DESCRIPTION A Tangram::Storage object is a connection to a database configured for use with Tangram. =head1 MEMORY MANAGEMENT Starting with version 1.18, Tangram attempts to use the support for weak reference that was introduced in Perl 5.6. Whether that support is found or not has a major impact on how Storage influences object lifetime. If weakref support I available, Storage uses weak references to keep track of objects that have already been loaded. This does I prevent the objects from being reclaimed by Perl. IOW, the I code decides how long an object remains in memory. If weakref support I available, Storage uses normal, 'strong' references. Storage will pin in memory all the objects that have been loaded I inserted through it, until you call L<"disconnect"> or L<"unload">. In either case, Tangram will I break circular structures for you. Note that caching objects between transactions is a great way to ruin the transactional guarantees that your database (hopefully) provides. That being said, be sure to check out the C method. =head1 INTERNAL CONNECTION Except in the implementation of cursor(), Tangram uses a single DBI connection in its operations. That connection is called the 'internal' connection. Since, in general, database managers do not allow multiple result sets on the same connection, the internal connection can be used only to carray a single task at a time. Ls returned by cursor() do not suffer from this limitation because they use a separate DBI connection. =head1 CLASS METHODS =head2 connect $storage = connect( $schema, $data_source, $username, $auth, \%options ) Connects to a storage and return a handle object. Dies in case of failure. $schema is an L object consistent with the database. $data_source, $username and $auth are passed directly to DBI::connect(). \%options is a reference to a hash that may contain the following fields: =over 4 =item * dbh Pass in an already connected DBI handle =item * no_tx Specify explicitly whether or not transactions are possible. If they are not, then Tangram can guarantee consistency by serialising transaction updates - which guarantees poor performance and means that you can never use C<$storage-Erollback>. If you are using MySQL, you should consider using the InnoDB table type to avoid this problem. Also note that you will explicitly have to set this option if you have InnoDB tables configured, as there is no real way of telling if transactions are available for any given query without either trying to do a rollback, or querying the table types for every table. Which I don't think it's Tangram's duty to do! =item * no_subselects Functions that need to perform sub-selects will die immediately or attempt to emulate the functionality required, rather than relying on the RDBMS to return a failure. This is currently ignored, but that's not functionally relevant :-). It can be read as C<$storage-E{no_subselects}> however, as the correct value is automatically detected on connection. =back All fields are optional. C can be used to connect a Storage via an existing DBI handle. $data_source, $username and $auth are still needed because Tangram may need to open extra connections (see below). =head1 INSTANCE METHODS =head2 insert $storage->insert( @objs ); Inserts objects in storage. Returns the ID(s) assigned to the object(s). This method is valid in both L. The inserted objects must be of a class described in the schema associated to the storage. Attempting to insert an object that is already persistent in the storage is an error. Tangram will automatically insert any object that is refered by $obj if it is not already present in storage. In the following example: my $homer = NaturalPerson->new( firstName => 'Homer', name => 'Simpson', children => Set::Object->new( NaturalPerson->new( firstName => 'Bart', name => 'Simpson' ), NaturalPerson->new( firstName => 'Lisa', name => 'Simpson' ), NaturalPerson->new( firstName => 'Maggie', name => 'Simpson' ) ) ); $storage->insert( $homer ); ...Tangram automatically inserts the kids along with Homer. =head2 update $storage->update( @objs ); Save objects to storage. This method is valid in both L. The objects must be of a class described in the schema associated to the storage. Attempting to update an object that is not already present in the storage is an error. Tangram will automatically insert any object that is refered by an inserted object if it is not already present in storage. It will not automatically update the refered objects that are already stored. In the following example: my $homer = NaturalPerson->new( firstName => 'Homer', name => 'Simpson' ); $storage->insert( $homer ); my $marge = NaturalPerson->new( firstName => 'Marge', name => 'Simpson', age => 34 ); $storage->insert( $marge ); $marge->{age} = 35; $homer->{partner} = $marge; $homer->{children} = Set::Object->new( NaturalPerson->new( firstName => 'Bart', name => 'Simpson' ), NaturalPerson->new( firstName => 'Lisa', name => 'Simpson' ), NaturalPerson->new( firstName => 'Maggie', name => 'Simpson' ) ); $storage->update( $homer ); ...Tangram automatically inserts the kids when their father is updated. OTOH, $marge will not be automatically inserted nor updated; her age will remain '34' in persistent storage. Tangram does not perform any deadlock detection on updates. You have to rely on your database back-end for that. =head2 id $id = $storage->id( $obj ); @id = $storage->id( @obj ); Returns the IDs of the given objects. If an object is not persistent in storage yet, its corresponding ID is undef(). This method is valid in both L. =head2 oid_isa if ($storage->oid_isa($id, "ClassName")) { ... } Checks that the passed Object ID, C<$id>, is a "ClassName" according to the schema. This check relies solely on the information in the schema, not Perl's idea of C<-Eisa> relationships. =head2 load $obj = $storage->load( $id ); @obj = $storage->load( @id ); Returns a list of objects given their IDs. Dies if any ID has no corresponding persistent object in storage. This method is valid in both L. =head2 remote @remote = $storage->remote( @classes ); Returns a list of C objects of given classes. See L for a more detailed description. These objects are called I objects in the documentation. =head2 select @objs = $storage->select( $remote ); @objs = $storage->select( $remote, $filter ); @objs = $storage->select( $remote, opt1 => val1, opt2 => val2, ...); Valid only in list context. Returns a list containing all the objects that satisfy C<$filter>. $remote can be either a I object of an array of I objects. If it is a single I object, a list of objects is returned. If it is an array, a list of arrays of objects is returned. If one argument is passed, return all the objects of the given type. If two arguments are passed, the second argument must be a Filter. C returns the objects that satisfy C<$filter> and are type-compatible with the corresponding I object. If more than two arguments are passed, the arguments after C<$remote> are treated as key/value pairs. Currently Tangram recognizes the following directives: =over 4 =item * filter =item * distinct =item * order =item * desc =item * distinct =item * limit =item * outer_filter =item * force_outer =back C specifies a Filter that can be used to restrict the result set. Filters are based on simple Perl expressions involving I objects. The expression is eventually compiled into its SQL equivalent, becoming part of a WHERE-CLAUSE. For example: my $remote_person = $storage->remote('Foo::Person'); my @martians = $storage->select( $remote_person, filter => ($remote_person->{location} eq 'Mars') ); Would retrieve all martians from the database. Note that the fields are accessed as hash reference keys instead of the (expected) method calls. In the previous example, C<-E{location}> is seen as a scalar from Perl and as some derivative of a VARCHAR/TEXT field on the database side. But filters can operate on many other types, including references to other persistent objects. For instance: # instantiate the obj and add it to the DB my $mars = Foo::Location->new( name => 'Mars'); $storage->insert($mars); my $remote_person = $storage->remote('Foo::Person'); my @martians = $storage->select( $remote_person, filter => ($remote_person->{location} == $mars) ); In this case, having a reference to the persistent object C<$mars> handy allows us to look for all objects that reference it. Keep in mind that these are introductory examples - the relationship between two classes of objects and how they behave depends on defined relationships between them - whether it's a C, an C, etc -- see L and L for more information on relationship types. Filters can also be joined together with boolean expressions: my $r_user = $storage->remote('My::Users'); my @active_premium_users = $storage->select( $r_user, filter => (# "&" is not a typo - see below ($r_user->{is_logged_in} eq 'Y') & ($r_user->{is_premium} eq 'Y' ) ) ); This select retrieves all the users currently logged in who also have a premium account. Note the use of C<&> instead of C<&&> (or C) - this is due to a problem in the way Perl handles operator overloading (C<&&> may not be overloaded). For the basic boolean operators, use C<&> as AND, C<|> as OR and C as NOT. Other overloaded bits that work as expected are: + - * / == eq != ne < lt <= le > gt >= ge cos sin acos ...which are translated to their SQL counterparts as closely as possible. Tip: Filters can also be created beforehand by using this simple syntax: my $new_filter = ($r_user->{is_logged_in} eq 'Y'); Then you can add expressions to it by doing (for example): $new_filter &= (r_user->{is_premium} eq 'Y'); and use it in the expression like so: my @active_premium_users = $storage->select ( $r_user, filter => $new_filter ); As of Tangram 2.08_02, The scalar value C<1> may be used as an "identity" filter. See also C. C specifies that each object in the result set must be unique (Tangram generates a SELECT DISTINCT). C specifies attributes in terms of one or more of the remote objects - any that are being selected, or any that appear in the filter. As of Tangram 2.09, you can also directly use SQL expressions in C expressions, though you should consider how portable this may or may not be. C specifies that the order should be descending. For example: $storage->select( $object, filter => (...), order => [ $remote_foo->{field1} ], desc => 1 ); would order DESC (descending, high to low) all the fields listed in the C clause. Passing: desc => 0 would order all the fields ASC (ascending, low to high). To specify which fields should be ordered DESC and which ones should be ordered ASC, pass an array ref to C, like this: $storage->select( $object, filter => (...), order => [ $remote_foo->{field1}, $remote_foo->{field2}, $remote_foo->{field3}, ], desc => [ 1, 0, 1 ] ); This will order C and C descending, and C ascending. C is a boolean; a true value specifies that the same object should ocur only once in the result set. In general, this is a good idea; C is a maximum number of rows to retrieve; in fact, with some databases you can give two numbers to this to get the rows between N and M of a select. See your RDBMS manual for more. If you want to specify more than one number, you may use the following syntax: $storage->select( $object, filter => (...), limit => [ 5, 10 ] ); The above example would return rows 6 through 15 on a MySQL database. The select method is valid only in list context. C and C are EXPERIMENTAL API features. If you pass any filter conditions into C instead of C, then any mentioned tables are connected by an outer join. What this means is that the object does not necessarily have to be present for the select to return a row; it may also be C. The C option expects an array ref of L objects. These tables are joined with an outer join clause. The outer join related code is extremely hairy, and you are advised to ensure that you test each outer join query that you are going to use with new versions of Tangram. Do not try to combine inheritance and outer joins if you want to run your application on toy databases, currently this means SQLite and MySQL. SQLite does not parse SQL nested join syntax and MySQL just gets the join all wrong. At least, on my testbed system. YMMV. =head2 sum( $expr, [$filter] ) Returns the total of the remote expression ($expr) for all rows that match $filter, as summed by the RDBMS. $filter is optional, and if not passed the implication is to sum the value for ALL objects of that type. my $r_thing = $storage->remote("Thing"); $sum = $storage->sum( $r_thing->{field}, ($r_thing->{foo} eq "bar") ); It is also possible to pass a list of fields to sum, as an array ref: ($sum_expr1, $sum_expr2) = $storage->sum( [ $expr1, $expr2 ], $filter ); =head2 count( $expr, [$filter] ) Works as B, but returns the count of the given objects or columns instead of the sum. This function does I support counting multiple columns by passing an array ref. However, this can be achieved using the C<-Ecount()> remote expression function (see L). =head2 cursor $cursor = $storage->cursor( $remote ); $cursor = $storage->cursor( $remote, $filter ); $cursor = cursor( $remote, opt1 => val1, op2 => val2, ...); Valid only in scalar context. Returns a Cursor on the objects that are type-compatible with $remote. If one argument is passed, the cursor returns all the objects of the given type. If two arguments are passed, the second argument must be a Filter. The cursor returns the objects that satisfy $filter and are type-compatible with the corresponding Remote. If more than two arguments are passed, the arguments after $remote are treated as key/value pairs. Currently Tangram recognizes the following directives: =over 4 =item * filter =item * order =item * desc =item * distinct =item * retrieve =back For options C, C, C and C, see C