.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Data::ObjectDriver 3pm" .TH Data::ObjectDriver 3pm "2023-02-05" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Data::ObjectDriver \- Simple, transparent data interface, with caching .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 9 \& ## Set up your database driver code. \& package FoodDriver; \& sub driver { \& Data::ObjectDriver::Driver::DBI\->new( \& dsn => \*(Aqdbi:mysql:dbname\*(Aq, \& username => \*(Aqusername\*(Aq, \& password => \*(Aqpassword\*(Aq, \& ) \& } \& \& ## Set up the classes for your recipe and ingredient objects. \& package Recipe; \& use base qw( Data::ObjectDriver::BaseObject ); \& _\|_PACKAGE_\|_\->install_properties({ \& columns => [ \*(Aqrecipe_id\*(Aq, \*(Aqtitle\*(Aq ], \& datasource => \*(Aqrecipe\*(Aq, \& primary_key => \*(Aqrecipe_id\*(Aq, \& driver => FoodDriver\->driver, \& }); \& \& package Ingredient; \& use base qw( Data::ObjectDriver::BaseObject ); \& _\|_PACKAGE_\|_\->install_properties({ \& columns => [ \*(Aqingredient_id\*(Aq, \*(Aqrecipe_id\*(Aq, \*(Aqname\*(Aq, \*(Aqquantity\*(Aq ], \& datasource => \*(Aqingredient\*(Aq, \& primary_key => [ \*(Aqrecipe_id\*(Aq, \*(Aqingredient_id\*(Aq ], \& driver => FoodDriver\->driver, \& }); \& \& ## And now, use them! \& my $recipe = Recipe\->new; \& $recipe\->title(\*(AqBanana Milkshake\*(Aq); \& $recipe\->save; \& \& my $ingredient = Ingredient\->new; \& $ingredient\->recipe_id($recipe\->id); \& $ingredient\->name(\*(AqBananas\*(Aq); \& $ingredient\->quantity(5); \& $ingredient\->save; \& \& ## Needs more bananas! \& $ingredient\->quantity(10); \& $ingredient\->save; \& \& ## Shorthand constructor \& my $ingredient = Ingredient\->new(recipe_id=> $recipe\->id, \& name => \*(AqMilk\*(Aq, \& quantity => 2); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" \&\fIData::ObjectDriver\fR is an object relational mapper, meaning that it maps object-oriented design concepts onto a relational database. .PP It's inspired by, and descended from, the \fIMT::ObjectDriver\fR classes in Six Apart's Movable Type and TypePad weblogging products. But it adds in caching and partitioning layers, allowing you to spread data across multiple physical databases, without your application code needing to know where the data is stored. .SH "METHODOLOGY" .IX Header "METHODOLOGY" \&\fIData::ObjectDriver\fR provides you with a framework for building database-backed applications. It provides built-in support for object caching and database partitioning, and uses a layered approach to allow building very sophisticated database interfaces without a lot of code. .PP You can build a driver that uses any number of caching layers, plus a partitioning layer, then a final layer that actually knows how to load data from a backend datastore. .PP For example, the following code: .PP .Vb 8 \& my $driver = Data::ObjectDriver::Driver::Cache::Memcached\->new( \& cache => Cache::Memcached\->new( \& servers => [ \*(Aq127.0.0.1:11211\*(Aq ], \& ), \& fallback => Data::ObjectDriver::Driver::Partition\->new( \& get_driver => \e&get_driver, \& ), \& ); .Ve .PP creates a new driver that supports both caching (using memcached) and partitioning. .PP It's useful to demonstrate the flow of a sample request through this driver framework. The following code: .PP .Vb 1 \& my $ingredient = Ingredient\->lookup([ $recipe\->recipe_id, 1 ]); .Ve .PP would take the following path through the \fIData::ObjectDriver\fR framework: .IP "1." 4 The caching layer would look up the object with the given primary key in all of the specified memcached servers. .Sp If the object was found in the cache, it would be returned immediately. .Sp If the object was not found in the cache, the caching layer would fall back to the driver listed in the \fIfallback\fR setting: the partitioning layer. .IP "2." 4 The partitioning layer does not know how to look up objects by itself\*(--all it knows how to do is to give back a driver that \fIdoes\fR know how to look up objects in a backend datastore. .Sp In our example above, imagine that we're partitioning our ingredient data based on the recipe that the ingredient is found in. For example, all of the ingredients for a \*(L"Banana Milkshake\*(R" would be found in one partition; all of the ingredients for a \*(L"Chocolate Sundae\*(R" might be found in another partition. .Sp So the partitioning layer needs to tell us which partition to look in to load the ingredients for \fI\f(CI$recipe\fI\->recipe_id\fR. If we store a \&\fIpartition_id\fR column along with each \fI\f(CI$recipe\fI\fR object, that information can be loaded very easily, and the partitioning layer will then instantiate a \fI\s-1DBI\s0\fR driver that knows how to load an ingredient from that recipe. .IP "3." 4 Using the \fI\s-1DBI\s0\fR driver that the partitioning layer created, \&\fIData::ObjectDriver\fR can look up the ingredient with the specified primary key. It will return that key back up the chain, giving each layer a chance to do something with it. .IP "4." 4 The caching layer, when it receives the object loaded in Step 3, will store the object in memcached. .IP "5." 4 The object will be passed back to the caller. Subsequent lookups of that same object will come from the cache. .SH "HOW IS IT DIFFERENT?" .IX Header "HOW IS IT DIFFERENT?" \&\fIData::ObjectDriver\fR differs from other similar frameworks (e.g. Class::DBI) in a couple of ways: .IP "\(bu" 4 It has built-in support for caching. .IP "\(bu" 4 It has built-in support for data partitioning. .IP "\(bu" 4 Drivers are attached to classes, not to the application as a whole. .Sp This is essential for partitioning, because your partition drivers need to know how to load a specific class of data. .Sp But it can also be useful for caching, because you may find that it doesn't make sense to cache certain classes of data that change constantly. .IP "\(bu" 4 The driver class != the base object class. .Sp All of the object classes you declare will descend from \&\fIData::ObjectDriver::BaseObject\fR, and all of the drivers you instantiate or subclass will descend from \fIData::ObjectDriver\fR itself. .Sp This provides a useful distinction between your data/classes, and the drivers that describe how to \fBact\fR on that data, meaning that an object based on \fIData::ObjectDriver::BaseObject\fR is not tied to any particular type of driver. .SH "USAGE" .IX Header "USAGE" .SS "Class\->lookup($id)" .IX Subsection "Class->lookup($id)" Looks up/retrieves a single object with the primary key \fI\f(CI$id\fI\fR, and returns the object. .PP \&\fI\f(CI$id\fI\fR can be either a scalar or a reference to an array, in the case of a class with a multiple column primary key. .SS "Class\->lookup_multi(\e@ids)" .IX Subsection "Class->lookup_multi(@ids)" Looks up/retrieves multiple objects with the IDs \fI\e@ids\fR, which should be a reference to an array of IDs. As in the case of \fIlookup\fR, an \s-1ID\s0 can be either a scalar or a reference to an array. .PP Returns a reference to an array of objects \fBin the same order\fR as the IDs you passed in. Any objects that could not successfully be loaded will be represented in that array as an \f(CW\*(C`undef\*(C'\fR element. .PP So, for example, if you wanted to load 2 objects with the primary keys \&\f(CW\*(C`[ 5, 3 ]\*(C'\fR and \f(CW\*(C`[ 4, 2 ]\*(C'\fR, you'd call \fIlookup_multi\fR like this: .PP .Vb 4 \& Class\->lookup_multi([ \& [ 5, 3 ], \& [ 4, 2 ], \& ]); .Ve .PP And if the first object in that list could not be loaded successfully, you'd get back a reference to an array like this: .PP .Vb 4 \& [ \& undef, \& $object \& ] .Ve .PP where \fI\f(CI$object\fI\fR is an instance of \fIClass\fR. .SS "Class\->search(\e%terms [, \e%options ])" .IX Subsection "Class->search(%terms [, %options ])" Searches for objects matching the terms \fI\f(CI%terms\fI\fR. In list context, returns an array of matching objects; in scalar context, returns a reference to a subroutine that acts as an iterator object, like so: .PP .Vb 4 \& my $iter = Ingredient\->search({ recipe_id => 5 }); \& while (my $ingredient = $iter\->()) { \& ... \& } .Ve .PP \&\f(CW$iter\fR is blessed in Data::ObjectDriver::Iterator package, so the above could also be written: .PP .Vb 4 \& my $iter = Ingredient\->search({ recipe_id => 5 }); \& while (my $ingredient = $iter\->next()) { \& ... \& } .Ve .PP The keys in \fI\f(CI%terms\fI\fR should be column names for the database table modeled by \fIClass\fR (and the values should be the desired values for those columns). .PP \&\fI\f(CI%options\fI\fR can contain: .IP "\(bu" 4 sort .Sp The name of a column to use to sort the result set. .Sp Optional. .IP "\(bu" 4 direction .Sp The direction in which you want to sort the result set. Must be either \&\f(CW\*(C`ascend\*(C'\fR or \f(CW\*(C`descend\*(C'\fR. .Sp Optional. .IP "\(bu" 4 limit .Sp The value for a \fI\s-1LIMIT\s0\fR clause, to limit the size of the result set. .Sp Optional. .IP "\(bu" 4 offset .Sp The offset to start at when limiting the result set. .Sp Optional. .IP "\(bu" 4 fetchonly .Sp A reference to an array of column names to fetch in the \fI\s-1SELECT\s0\fR statement. .Sp Optional; the default is to fetch the values of all of the columns. .IP "\(bu" 4 for_update .Sp If set to a true value, the \fI\s-1SELECT\s0\fR statement generated will include a \&\fI\s-1FOR UPDATE\s0\fR clause. .IP "\(bu" 4 comment .Sp A sql comment to watermark the \s-1SQL\s0 query. .IP "\(bu" 4 window_size .Sp Used when requesting an iterator for the search method and selecting a large result set or a result set of unknown size. In such a case, no \s-1LIMIT\s0 clause is assigned, which can load all available objects into memory. Specifying \f(CW\*(C`window_size\*(C'\fR will load objects in manageable chunks. This will also cause any caching driver to be bypassed for issuing the search itself. Objects are still placed into the cache upon load. .Sp This attribute is ignored when the search method is invoked in an array context, or if a \f(CW\*(C`limit\*(C'\fR attribute is also specified that is smaller than the \f(CW\*(C`window_size\*(C'\fR. .SS "Class\->search(\e@terms [, \e%options ])" .IX Subsection "Class->search(@terms [, %options ])" This is an alternative calling signature for the search method documented above. When providing an array of terms, it allows for constructing complex expressions that mix 'and' and 'or' clauses. For example: .PP .Vb 5 \& my $iter = Ingredient\->search([ { recipe_id => 5 }, \& \-or => { calories => { value => 300, op => \*(Aq<\*(Aq } } ]); \& while (my $ingredient = $iter\->()) { \& ... \& } .Ve .PP Supported logic operators are: '\-and', '\-or', '\-and_not', '\-or_not'. .SS "Class\->add_trigger($trigger, \e&callback)" .IX Subsection "Class->add_trigger($trigger, &callback)" Adds a trigger to all objects of class \fIClass\fR, such that when the event \&\fI\f(CI$trigger\fI\fR occurs to any of the objects, subroutine \f(CW&callback\fR is run. Note that triggers will not occur for instances of \fIsubclasses\fR of \fIClass\fR, only of \fIClass\fR itself. See \s-1TRIGGERS\s0 for the available triggers. .SS "Class\->call_trigger($trigger, [@callback_params])" .IX Subsection "Class->call_trigger($trigger, [@callback_params])" Invokes the triggers watching class \fIClass\fR. The parameters to send to the callbacks (in addition to \fIClass\fR) are specified in \fI\f(CI@callback_params\fI\fR. See \&\s-1TRIGGERS\s0 for the available triggers. .ie n .SS "$obj\->save" .el .SS "\f(CW$obj\fP\->save" .IX Subsection "$obj->save" Saves the object \fI\f(CI$obj\fI\fR to the database. .PP If the object is not yet in the database, \fIsave\fR will automatically generate a primary key and insert the record into the database table. Otherwise, it will update the existing record. .PP If an error occurs, \fIsave\fR will \fIcroak\fR. .PP Internally, \fIsave\fR calls \fIupdate\fR for records that already exist in the database, and \fIinsert\fR for those that don't. .ie n .SS "$obj\->remove" .el .SS "\f(CW$obj\fP\->remove" .IX Subsection "$obj->remove" Removes the object \fI\f(CI$obj\fI\fR from the database. .PP If an error occurs, \fIremove\fR will \fIcroak\fR. .SS "Class\->remove(\e%terms, \e%args)" .IX Subsection "Class->remove(%terms, %args)" Removes objects found with the \fI\f(CI%terms\fI\fR. So it's a shortcut of: .PP .Vb 4 \& my @obj = Class\->search(\e%terms, \e%args); \& for my $obj (@obj) { \& $obj\->remove; \& } .Ve .PP However, when you pass \f(CW\*(C`nofetch\*(C'\fR option set to \f(CW%args\fR, it won't create objects with \f(CW\*(C`search\*(C'\fR, but issues \fI\s-1DELETE\s0\fR \s-1SQL\s0 directly to the database. .PP .Vb 2 \& ## issues "DELETE FROM tbl WHERE user_id = 2" \& Class\->remove({ user_id => 2 }, { nofetch => 1 }); .Ve .PP This might be much faster and useful for tables without Primary Key, but beware that in this case \fBTriggers won't be fired\fR because no objects are instantiated. .SS "Class\->bulk_insert([col1, col2], [[d1,d2], [d1,d2]]);" .IX Subsection "Class->bulk_insert([col1, col2], [[d1,d2], [d1,d2]]);" Bulk inserts data into the underlying table. The first argument is an array reference of columns names as specified in install_properties .ie n .SS "$obj\->add_trigger($trigger, \e&callback)" .el .SS "\f(CW$obj\fP\->add_trigger($trigger, \e&callback)" .IX Subsection "$obj->add_trigger($trigger, &callback)" Adds a trigger to the object \fI\f(CI$obj\fI\fR, such that when the event \fI\f(CI$trigger\fI\fR occurs to the object, subroutine \f(CW&callback\fR is run. See \s-1TRIGGERS\s0 for the available triggers. Triggers are invoked in the order in which they are added. .ie n .SS "$obj\->call_trigger($trigger, [@callback_params])" .el .SS "\f(CW$obj\fP\->call_trigger($trigger, [@callback_params])" .IX Subsection "$obj->call_trigger($trigger, [@callback_params])" Invokes the triggers watching all objects of \fI\f(CI$obj\fI\fR's class and the object \&\fI\f(CI$obj\fI\fR specifically for trigger event \fI\f(CI$trigger\fI\fR. The additional parameters besides \fI\f(CI$obj\fI\fR, if any, are passed as \fI\f(CI@callback_params\fI\fR. See \s-1TRIGGERS\s0 for the available triggers. .SH "TRIGGERS" .IX Header "TRIGGERS" \&\fIData::ObjectDriver\fR provides a trigger mechanism by which callbacks can be called at certain points in the life cycle of an object. These can be set on a class as a whole or individual objects (see \s-1USAGE\s0). .PP Triggers can be added and called for these events: .IP "\(bu" 4 pre_save \-> ($obj, \f(CW$orig_obj\fR) .Sp Callbacks on the \fIpre_save\fR trigger are called when the object is about to be saved to the database. For example, use this callback to translate special code strings into numbers for storage in an integer column in the database. Note that this hook is also called when you \f(CW\*(C`remove\*(C'\fR the object. .Sp Modifications to \fI\f(CI$obj\fI\fR will affect the values passed to subsequent triggers and saved in the database, but not the original object on which the \fIsave\fR method was invoked. .IP "\(bu" 4 post_save \-> ($obj, \f(CW$orig_obj\fR) .Sp Callbaks on the \fIpost_save\fR triggers are called after the object is saved to the database. Use this trigger when your hook needs primary key which is automatically assigned (like auto_increment and sequence). Note that this hooks is \fB\s-1NOT\s0\fR called when you remove the object. .IP "\(bu" 4 pre_insert / post_insert / pre_update / post_update / pre_remove / post_remove \-> ($obj, \f(CW$orig_obj\fR) .Sp Those triggers are fired before and after \f(CW$obj\fR is created, updated and deleted. .IP "\(bu" 4 post_load \-> ($obj) .Sp Callbacks on the \fIpost_load\fR trigger are called when an object is being created from a database query, such as with the \fIlookup\fR and \fIsearch\fR class methods. For example, use this callback to translate the numbers your \&\fIpre_save\fR callback caused to be saved \fIback\fR into string codes. .Sp Modifications to \fI\f(CI$obj\fI\fR will affect the object passed to subsequent triggers and returned from the loading method. .Sp Note \fIpre_load\fR should only be used as a trigger on a class, as the object to which the load is occurring was not previously available for triggers to be added. .IP "\(bu" 4 pre_search \-> ($class, \f(CW$terms\fR, \f(CW$args\fR) .Sp Callbacks on the \fIpre_search\fR trigger are called when a content addressed query for objects of class \fI\f(CI$class\fI\fR is performed with the \fIsearch\fR method. For example, use this callback to translate the entry in \fI\f(CI$terms\fI\fR for your code string field to its appropriate integer value. .Sp Modifications to \fI\f(CI$terms\fI\fR and \fI\f(CI$args\fI\fR will affect the parameters to subsequent triggers and what objects are loaded, but not the original hash references used in the \fIsearch\fR query. .Sp Note \fIpre_search\fR should only be used as a trigger on a class, as \fIsearch\fR is never invoked on specific objects. .RS 4 .Sp .RS 4 The return values from your callbacks are ignored. .Sp Note that the invocation of callbacks is the responsibility of the object driver. If you implement a driver that does not delegate to \&\fIData::ObjectDriver::Driver::DBI\fR, it is \fIyour\fR responsibility to invoke the appropriate callbacks with the \fIcall_trigger\fR method. .RE .RE .RS 4 .RE .SH "PROFILING" .IX Header "PROFILING" For performance tuning, you can turn on query profiling by setting \&\fI\f(CI$Data::ObjectDriver::PROFILE\fI\fR to a true value. Or, alternatively, you can set the \fI\s-1DOD_PROFILE\s0\fR environment variable to a true value before starting your application. .PP To obtain the profile statistics, get the global \&\fIData::ObjectDriver::Profiler\fR instance: .PP .Vb 1 \& my $profiler = Data::ObjectDriver\->profiler; .Ve .PP Then see the documentation for \fIData::ObjectDriver::Profiler\fR to see the methods on that class. .PP In some applications there are phases of execution in which no I/O operations should occur, but sometimes it's difficult to tell when, where, or if those I/O operations are happening. One approach to surfacing these situations is to set, either globally or locally, the \f(CW$Data::ObjectDriver::RESTRICT_IO\fR flag. If set, this will tell Data::ObjectDriver to die with some context rather than executing network calls for data. .SH "TRANSACTIONS" .IX Header "TRANSACTIONS" Transactions are supported by Data::ObjectDriver's default drivers. So each Driver is capable to deal with transactional state independently. Additionally class know how to turn transactions switch on for all objects. .PP In the case of a global transaction all drivers used during this time are put in a transactional state until the end of the transaction. .SS "Example" .IX Subsection "Example" .Vb 2 \& ## start a transaction \& Data::ObjectDriver::BaseObject\->begin_work; \& \& $recipe = Recipe\->new; \& $recipe\->title(\*(Aqlasagnes\*(Aq); \& $recipe\->save; \& \& my $ingredient = Ingredient\->new; \& $ingredient\->recipe_id($recipe\->recipe_id); \& $ingredient\->name("more layers"); \& $ingredient\->insert; \& $ingredient\->remove; \& \& if ($you_are_sure) { \& Data::ObjectDriver::BaseObject\->commit; \& } \& else { \& ## erase all trace of the above \& Data::ObjectDriver::BaseObject\->rollback; \& } .Ve .SS "Driver implementation" .IX Subsection "Driver implementation" Drivers have to implement the following methods: .IP "\(bu" 4 begin_work to initialize a transaction .IP "\(bu" 4 rollback .IP "\(bu" 4 commit .SS "Nested transactions" .IX Subsection "Nested transactions" Are not supported and will result in warnings and the inner transactions to be ignored. Be sure to \fBend\fR each transaction and not to let et long running transaction open (i.e you should execute a rollback or commit for each open begin_work). .SS "Transactions and \s-1DBI\s0" .IX Subsection "Transactions and DBI" In order to make transactions work properly you have to make sure that the \f(CW$dbh\fR for each \s-1DBI\s0 drivers are shared among drivers using the same database (basically dsn). .PP One way of doing that is to define a \fBget_dbh()\fR subref in each \s-1DBI\s0 driver to return the same dbh if the dsn and attributes of the connection are identical. .PP The other way is to use the new configuration flag on the \s-1DBI\s0 driver that has been added specifically for this purpose: \f(CW\*(C`reuse_dbh\*(C'\fR. .PP .Vb 10 \& ## example coming from the test suite \& _\|_PACKAGE_\|_\->install_properties({ \& columns => [ \*(Aqrecipe_id\*(Aq, \*(Aqpartition_id\*(Aq, \*(Aqtitle\*(Aq ], \& datasource => \*(Aqrecipes\*(Aq, \& primary_key => \*(Aqrecipe_id\*(Aq, \& driver => Data::ObjectDriver::Driver::Cache::Cache\->new( \& cache => Cache::Memory\->new, \& fallback => Data::ObjectDriver::Driver::DBI\->new( \& dsn => \*(Aqdbi:SQLite:dbname=global.db\*(Aq, \& reuse_dbh => 1, ## be sure that the corresponding dbh is shared \& ), \& ), \& }); .Ve .SH "EXAMPLES" .IX Header "EXAMPLES" .SS "A Partitioned, Caching Driver" .IX Subsection "A Partitioned, Caching Driver" .Vb 3 \& package Ingredient; \& use strict; \& use base qw( Data::ObjectDriver::BaseObject ); \& \& use Data::ObjectDriver::Driver::DBI; \& use Data::ObjectDriver::Driver::Partition; \& use Data::ObjectDriver::Driver::Cache::Cache; \& use Cache::Memory; \& use Carp; \& \& our $IDs; \& \& _\|_PACKAGE_\|_\->install_properties({ \& columns => [ \*(Aqingredient_id\*(Aq, \*(Aqrecipe_id\*(Aq, \*(Aqname\*(Aq, \*(Aqquantity\*(Aq, ], \& datasource => \*(Aqingredients\*(Aq, \& primary_key => [ \*(Aqrecipe_id\*(Aq, \*(Aqingredient_id\*(Aq ], \& driver => \& Data::ObjectDriver::Driver::Cache::Cache\->new( \& cache => Cache::Memory\->new( namespace => _\|_PACKAGE_\|_ ), \& fallback => \& Data::ObjectDriver::Driver::Partition\->new( \& get_driver => \e&get_driver, \& pk_generator => \e&generate_pk, \& ), \& ), \& }); \& \& sub get_driver { \& my($terms) = @_; \& my $recipe; \& if (ref $terms eq \*(AqHASH\*(Aq) { \& my $recipe_id = $terms\->{recipe_id} \& or Carp::croak("recipe_id is required"); \& $recipe = Recipe\->lookup($recipe_id); \& } elsif (ref $terms eq \*(AqARRAY\*(Aq) { \& $recipe = Recipe\->lookup($terms\->[0]); \& } \& Carp::croak("Unknown recipe") unless $recipe; \& Data::ObjectDriver::Driver::DBI\->new( \& dsn => \*(Aqdbi:mysql:database=cluster\*(Aq . $recipe\->cluster_id, \& username => \*(Aqfoo\*(Aq, \& pk_generator => \e&generate_pk, \& ); \& } \& \& sub generate_pk { \& my($obj) = @_; \& $obj\->ingredient_id(++$IDs{$obj\->recipe_id}); \& 1; \& } \& \& 1; .Ve .SH "FORK SAFETY" .IX Header "FORK SAFETY" As of version 0.21, \fIData::ObjectDriver\fR resets internal database handles after \fI\f(BIfork\fI\|(2)\fR is called, but only if POSIX::AtFork module is installed. Otherwise, \fIData::ObjectDriver\fR is not fork-safe. .SH "SUPPORTED DATABASES" .IX Header "SUPPORTED DATABASES" \&\fIData::ObjectDriver\fR is very modular and it's not very difficult to add new drivers. .IP "\(bu" 4 MySQL is well supported and has been heavily tested. .IP "\(bu" 4 PostgreSQL has been used in production and should just work, too. .IP "\(bu" 4 SQLite is supported, but \s-1YMMV\s0 depending on the version. This is the backend used for the test suite. .IP "\(bu" 4 Oracle support has been added in 0.06 .SH "LICENSE" .IX Header "LICENSE" \&\fIData::ObjectDriver\fR is free software; you may redistribute it and/or modify it under the same terms as Perl itself. .SH "AUTHOR & COPYRIGHT" .IX Header "AUTHOR & COPYRIGHT" Except where otherwise noted, \fIData::ObjectDriver\fR is Copyright 2005\-2006 Six Apart, cpan@sixapart.com. All rights reserved.