7
In this tour, we add persistence to a simple Person design.
9
A Person is either a NaturalPerson or a LegalPerson. Persons (in
10
general) have a collection of addresses.
12
An address consists in a type (a string) and a city (also a string).
14
NaturalPerson - a subclass of Person - represents persons of flesh and
15
blood. NaturalPersons have a name and a firstName (both strings) and
16
an age (an integer). NaturalPersons sometimes have a partner (another
17
NaturalPerson) and even children (a collection of NaturalPersons).
19
LegalPerson - another subclass of Person - represents companies and
20
other entities that the law regards as 'persons'. A LegalPerson has a
21
name (a string) and a manager (a NaturalPerson).
23
All this is expressed in the following UML diagram:
26
+---------------------+ +--------------+
27
| Person | | Address |
28
| { abstract } |1<>-->-*|--------------|
29
|---------------------| | kind: string |
30
+---------------------+ | city: string |
33
+--------------A--------------+
35
+-------------------+ +---------------+
36
+--*| NaturalPerson | | LegalPerson |
37
| |-------------------|manager |---------------|
38
V | firstName: string |1---<-----1| name: string |
39
| | name: string | +---------------+
41
children +-------------------+
47
B<Note that Tangram does I<not> create the corresponding Perl
48
packages!>. That's up to the user. However, to facilitate
49
experimentation, Tangram comes with a module that implements the
50
necessary classes. For more information see L<Tangram::Springfield>.
52
Before we can actually store objects we must complete two steps:
66
=head2 Creating a Schema
68
A Schema object contains information about the persistent
69
aspects of a system of classes.
71
It also gives a degree of control over the way Tangram performs the
72
object-relational mapping, but in this tour we will use all the defaults.
74
Here is the Schema for Springfield:
76
$schema = Tangram::Relational->schema( {
85
addresses => { class => 'Address', aggreg => 1 } }
91
string => [ qw( kind city ) ],
97
bases => [ qw( Person ) ],
100
string => [ qw( firstName name ) ],
101
int => [ qw( age ) ],
102
ref => [ qw( partner ) ],
103
array => { children => 'NaturalPerson' },
108
bases => [ qw( Person ) ],
111
string => [ qw( name ) ],
112
ref => [ qw( manager ) ],
117
The Schema lists all the classes that need persistence, along with
118
their attributes and the inheritance relationships. We must provide
119
type information for the attributes, because SQL is more typed than
120
Perl. We also tell Tangram that C<Person> is an abstract class, so it
121
wastes no time attempting to retrieve objects of that exact class.
123
Note that Tangram cannot deduce this information by itself. While Perl
124
makes it possible to extract the list of all the classes in an
125
application, in general not all classes will need to persist. A class
126
may have both persistent and non-persistent bases. As for attributes,
127
Perl's most typical representation for objects - a hash - even allows
128
two objects of the same class to have a different set of attributes.
130
For more information on creating Schemas, see L<Tangram::Relational>
131
and L<Tangram::Schema>.
133
=head2 Setting up a database
135
Now we create a database. The simplest way is to create an
136
empty database and let Tangram initialize it:
142
Tangram::Relational->deploy($schema, $dbh );
146
Tangram::Relational is the vanilla object-relational backend. It
147
assumes that the database understands standard SQL, and that both the
148
database and the related DBI driver fully implements the DBI
151
Tangram also comes with vendor-specific backends for Mysql and
152
Sybase. When a vendor-specific backend exists, it should be used in
153
place of the vanilla backend.
155
For more information, see L<Tangram::Relational>, L<Tangram::Sybase>
156
and L<Tangram::mysql>.
158
=head2 Connecting to a database
160
We are now ready to store objects. First we connect to the database,
161
using the class method Tangram::Relational::connect (or
162
Tangram::mysql::connect for Mysql).
164
The first argument of connect() the schema object; the others are
165
passed directly to DBI::connect. The method returns a Tangram::Storage
166
object that will be used to communicate with the database.
170
$storage = Tangram::Relational->connect( $schema,
173
connects to a database named Springfield via the vanilla Relational
174
backend, using a specific account and password.
176
For more information on connecting to databases, see L<Tangram::Relational> and
179
=head2 Inserting objects
181
Now we can populate the database:
183
$storage->insert( NaturalPerson->new(
184
firstName => 'Montgomery', name => 'Burns' ) );
186
This inserts a single NaturalPerson object into the database. We can
187
insert several objects in one call:
190
NaturalPerson->new( firstName => 'Patty', name => 'Bouvier' ),
191
NaturalPerson->new( firstName => 'Selma', name => 'Bouvier' ) );
193
Sometimes Tangram saves objects implicitly:
196
NaturalPerson->new( firstName => 'Bart', name => 'Simpson' ),
197
NaturalPerson->new( firstName => 'Lisa', name => 'Simpson' ) );
199
$marge = NaturalPerson->new(
200
firstName => 'Marge', name => 'Simpson',
203
kind => 'residence', city => 'Springfield' ) ],
204
children => [ @kids ] );
206
$homer = NaturalPerson->new( firstName => 'Homer', name => 'Simpson',
209
kind => 'residence', city => 'Springfield' ),
211
kind => 'work', city => 'Springfield' ) ],
212
children => [ @kids ] );
214
$homer->{partner} = $marge;
215
$marge->{partner} = $homer;
217
$homer_id = $storage->insert( $homer );
219
In the process of saving Homer, Tangram detects that it contains
220
references to objects that are not persistent yet (Marge, the
221
addresses and the kids), and inserts them automatically. Note that
222
Tangram can handle cycles: Homer and Marge refer to each other.
224
insert() returns an object id, or a list of object ids, that uniquely
225
identify the object(s) that have been inserted.
227
For more information on inserting objects, see L<Tangram::Storage>.
229
=head2 Updating objects
231
Updating works pretty much the same as inserting:
233
my $maggie = NaturalPerson->new(
234
firstName => 'Maggie', name => 'Simpson' );
236
push @{ $homer->{children} }, $maggie;
237
push @{ $marge->{children} }, $maggie;
239
$storage->update( $homer, $marge );
241
Here again Tangram detects that Maggie is not already persistent in
242
$storage and automatically inserts it. Note that we need to update
243
Marge explicitly because she was already persistent.
245
For more information on updating objects, see L<Tangram::Storage>.
247
=head2 Memory management
249
...is still up to you. Tangram won't break in-memory cycles, it's a
250
persistence tool, not a memory management tool. Let's make sure we
253
$homer->{partner} = undef; # do this before $homer goes out of scope
255
Also, when we're finished with a storage, we can explicitly disconnect it:
257
$storage->disconnect();
259
Whether it's important or not to disconnect the Storage depends on
260
what version of Perl you use. If it's prior to 5.6, you I<must>
261
disconnect the storage explicitly (or at least call unload())
262
otherwise the Storage will prevent the objects it controls from being
263
reclaimed by Perl. For more information see see L<Tangram::Storage>.
265
=head2 Finding objects
267
After reconnecting to Springfield, we now want to retrieve some objects.
268
But how do we find them? Basically there are three options
278
We obtain them from another object.
288
When an object is inserted, Tangram assigns an identifier to it.
289
IDs are numbers that uniquely identify objects in the database.
290
C<insert> returns the ID(s) of the object(s) it was passed:
292
$storage = Tangram::Relational->connect( $schema,
295
$ned_id = $storage->insert( NaturalPerson->new(
296
firstNname => 'Ned', name => 'Flanders' ) );
298
@sisters_id = $storage->insert(
299
NaturalPerson->new( firstName => 'Patty', name => 'Bouvier' ),
300
NaturalPerson->new( firstName => 'Selma', name => 'Bouvier' ) );
302
This enables us to retrieve the objects:
304
$ned = $storage->load( $ned_id );
305
@sisters = $storage->load( @sisters_id );
307
For more information on loading objects by id, see L<Tangram::Storage>.
309
=head2 Obtaining objects from other objects
311
Once Homer has been restored to his previous state, including his relations
312
with his family. Thus we can say:
314
$storage = Tangram::Relational->connect( $schema,
317
$homer = $storage->load( $homer_id ); # load by id
319
$marge = $homer->{partner};
320
@kids = @{ $homer->{children} };
322
Actually, when Tangram loads an object that contains references to
323
other persistent objects, it doesn't retrieve the referenced objects
324
immediately. Marge is retrieved only when Homer's 'partner' field is
325
accessed. This mechanism is almost totally transparent, we'd have to
326
use C<tied> to observe a non-present collection or reference.
328
For more information on relationships, see L<Tangram::Schema>,
329
L<Tangram::Ref>, L<Tangram::Array>, L<Tangram::IntrArray>,
330
L<Tangram::Set> and L<Tangram::IntrSet>.
334
To retrieve all the objects of a given class, we use C<select>:
336
$storage = Tangram::Relational->connect( $schema,
339
my @people = $storage->select( 'NaturalPerson' );
341
Tangram supports polymorphic retrieval. Let's first insert a
344
$storage->insert( LegalPerson->new(
345
name => 'Springfield Nuclear Power Plant', manager => $burns ) );
348
Now we can retrieve all the Persons - Natural or Legal - by making a
349
single call to select(), passing it the base class name:
351
my @all = $storage->select( 'Person' );
353
For more information on select(), see L<Tangram::Storage>.
357
Usually we won't want to load I<all> the NaturalPersons, only those
358
objects that satisfy some condition. Say, for example, that we want to
359
load only the NaturalPersons whose name field is 'Simpson'. Here's how
362
my $person = $storage->remote( 'NaturalPerson' );
363
my @simpsons = $storage->select( $person, $person->{name} eq 'Simpson' );
365
This will bring in memory only the Simpsons; Burns or the Bouvier
366
sisters won't turn up. The filtering happens on the database server
367
side, not in Perl space. Internally, Tangram translates the
368
C<$person->{name} eq 'Simpson'> clause into a piece of SQL code that
369
is passed down to the database.
371
The above example only begins to scratch the surface of Tangram's
372
filtering capabilities. The following examples are all legal and working code:
374
# find all the persons *not* named Simpson
376
my $person = $storage->remote( 'NaturalPerson' );
377
my @others = $storage->select( $person, $person->{name} ne 'Simpson' );
379
# same thing in a different way
381
my $person = $storage->remote( 'NaturalPerson' );
382
my @others = $storage->select( $person, !($person->{name} eq 'Simpson') );
384
# find all the persons who are older than me
386
my $person = $storage->remote( 'NaturalPerson' );
387
my @elders = $storage->select( $person, $person->{age} > 35 );
389
# find all the Simpsons older than me
391
my $person = $storage->remote( 'NaturalPerson' );
392
my @simpsons = $storage->select( $person,
393
$person->{name} eq 'Simpson' & $person->{age} > 35 );
395
# find Homer's wife - note that select *must* be called in list context
397
my ($person1, $person2) = $storage->remote(
398
qw( NaturalPerson NaturalPerson ));
400
my ($marge) = $storage->select( $person1,
401
$person1->{partner} == $person2
402
& $person2->{firstName} eq 'Homer' & $person2->{name} eq 'Simpson' );
404
# find Homer's wife - this time Homer is already in memory
406
my $homer = $storage->load( $homer_id );
407
my $person = $storage->remote( 'NaturalPerson' );
409
my ($marge) = $storage->select( $person,
410
$person->{partner} == $homer );
412
# find everybody who works in Springfield
414
my $address = $storage->remote( 'Address' );
416
my @population = $storage->select( $person,
417
$person->{addresses}->includes( $address )
418
& $address->{kind} eq 'work'
419
& $address->{city} eq 'Springfield');
421
# find the parents of Bart Simpson
423
my ($person1, $person2) = $storage->remote(
424
qw( NaturalPerson NaturalPerson ));
426
my @parents = $storage->select( $person1,
427
$person1->{children}->includes( $person2 )
428
& $person2->{firstName} eq 'Bart'
429
& $person2->{name} eq 'Simpson' );
432
my ($bart) = $storage->select( $person1, $person1->{firstName} eq 'Bart');
434
# find the parents of Bart, this time given an object already loaded
435
my $person = $storage->remote( 'NaturalPerson' );
437
@parents = $storage->select( $person,
438
$person->{children}->includes( $bart ) );
440
Note that Tangram uses a single ampersand (&) or vertical bar (|) to
441
represent logical conjunction or disjunction, not the usual && or
442
||. This is due to a limitation in Perl's operator overloading
443
mechanism. Make sure you never forget this, because, unfortunately,
444
using && or || in place of & or | is not even a syntax error :(
446
Finally, Tangram make it possible to retrieve tuples of related objects:
448
my ($parent, $child) = $storage->remote('NaturalPerson', 'NaturalPerson');
450
@pairs = $storage->select( [ $parent, $child ],
451
$parent->{children}->includes($child) );
453
@pairs contains a list of references to arrays of size two; each array
454
contains a pair of parent and child.
456
For more information on filters, see L<Tangram::Expr> and L<Tangram::Remote>.
460
Cursors provide a way of retrieving objects one at a time. This is
461
important is the result set is potentially large. cursor() takes the
462
same arguments as select() and returns a Cursor objects that can be
463
used to iterate over the result set via methods current() and next():
465
$storage = Tangram::Relational->connect( $schema,
468
# iterate over all the NaturalPersons in storage
470
my $cursor = $storage->cursor( 'NaturalPerson' );
472
while (my $person = $cursor->current())
480
The Cursor will be automatically closed when $cursor is garbage-collected,
481
but Perl doesn't define just when that may happen :( Thus it's a good idea to
482
explicitly close the cursor.
484
Each Cursor uses a separate connection to the database. Consequently you can
485
have several cursors open at the same, all with pending results. Of course,
486
mixing reads and writes to the same tables can result in deadlocks.
488
For more information on cursors, see L<Tangram::Storage> and
491
=head2 Remote objects
493
At this point, most people wonder what $person I<exactly> is and how
494
it all works. This section attempts to give an idea of the mechanisms
497
In Tangram terminology, $person a I<remote> object. Its Perl class is
498
Tangram::Remote, but it's really a placeholder for an object of class
499
C<NaturalPerson> I<in the database>, much like a table alias in
502
When you request a remote object of a given class, Tangram arranges
503
that the remote object I<looks like> an object of the said class. It
504
I<seems> to have the same fields as a regular object, but don't be
505
misled, it's not the real thing, it's just a way of providing a nice
508
If you dig it, you'll find out that a Remote is just a hash of
509
Tangram::Expr objects. When you say $homer->{name}, an Expr is
510
returned, which, most of the time, can be used like any ordinary Perl
511
scalar. However, an Expr represents a value I<in the database>, it's
512
the equivalent of Remote, only for expressions, not for objects.
514
Expr objects that represent scalar values (e.g. ints, floats, strings)
515
can be compared between them, or compared with straight Perl
516
scalars. Reference-like Exprs can be compared between themselves and
519
Expr objects that represent collections have an C<include> methods
520
that take a persistent object, a Remote object or an ID.
522
The result of comparing Exprs (or calling C<include>) is a
523
Tangram::Filter that will translate into part of the SQL where-clause
524
that will be passed to the RDBMS.
526
For more information on remote objects, see L<Tangram::Remote>.
528
=head2 Multiple loads
530
What happens when we load the same object twice? Consider:
532
my $person = $storage->remote( 'NaturalPerson' );
533
my @simpsons = $storage->select( $person, $person->{name} eq 'Simpson' );
535
my @people = $storage->select( 'NaturalPerson' );
537
Obviously Homer Simpson will be retrieved by both selects. Are there
538
two Homers in memory now? Fortunately not. There is only one copy of
539
Homer in memory. When Tangram load an object, it checks whether an
540
object with the same ID is alredy present. If yes, it keeps the old
541
copy, which is desirable, since we may have changed it already.
543
Incidentally, this explains why a Storage will hold objects in memory
544
- until disconnected (again, this will change when Perl supports weak
549
Tangram wraps database transactions in a object-oriented interface:
551
$storage->tx_start();
552
$homer->{partner} = $marge;
553
$marge->{partner} = $homer;
554
$storage->update( $homer, $marge );
555
$storage->tx_commit();
557
Both Marge and Homer will be updated, or none will. tx_rollback() drops
560
Tangram does not emulate transactions for databases that do not
561
support them (like earlier versions of mySql).
563
Unlike DBI, Tangram allows the nested transactions:
565
$storage->tx_start();
568
$storage->tx_start();
569
$patty->{partner} = $selma;
570
$selma->{partner} = $patty;
571
$storage->tx_commit();
574
$homer->{partner} = $marge;
575
$marge->{partner} = $homer;
576
$storage->update( $homer, $marge );
578
$storage->tx_commit();
580
Tangram uses a single database transaction, but commits it only when
581
the tx_commit()s exactly balance the tx_start()s. Thanks to this
582
feature any piece of code can open all the transactions it needs and
583
still cooperate smoothly with the rest of the application. If a DBI
584
transaction is already active, it will be reused; otherwise a new one
587
Tangram offer a more robust alternative to the start/commit code
588
sandwich. tx_do() calls CODEREF in a transaction. If the CODEREF
589
dies, the transaction is rolled back; otherwise it's committed. The
590
first example can be rewritten:
592
$storage->tx_do( sub {
593
$homer->{partner} = $marge;
594
$marge->{partner} = $homer;
595
$storage->update( $homer, $marge };
598
For more information on transactions, see L<Tangram::Storage>.