Rose-DB
view release on metacpan or search on metacpan
lib/Rose/DB.pm view on Meta::CPAN
$db = Rose::DB->new(type => 'aux');
$db = Rose::DB->new('aux'); # same thing
Each L<Rose::DB> object is associated with a particular data source, defined by the L<type|/type> and L<domain|/domain> values. If these are not part of PARAMS, then the default values are used. If you do not want to use the default values for the ...
The default L<type|/type> and L<domain|/domain> can be set using the L<default_type|/default_type> and L<default_domain|/default_domain> class methods. See the L<"Data Source Abstraction"> section for more information on data sources.
Object attributes are set based on the L<registry|/registry> entry specified by the C<type> and C<domain> parameters. This registry entry must exist or a fatal error will occur (with one exception; see below). Any additional PARAMS will override th...
If C<type> and C<domain> parameters are not passed, but a C<driver> parameter is passed, then a new "empty" object will be returned. Examples:
# This is ok, even if no registered data sources exist
$db = Rose::DB->new(driver => 'sqlite');
The object returned by L<new|/new> will be derived from a database-specific driver class, chosen based on the L<driver|/driver> value of the selected data source. If there is no registered data source for the specified L<type|/type> and L<domain|/do...
The default driver-to-class mapping is as follows:
pg -> Rose::DB::Pg
mysql -> Rose::DB::MySQL
mariadb -> Rose::DB::MariaDB
informix -> Rose::DB::Informix
oracle -> Rose::DB::Oracle
sqlite -> Rose::DB::SQLite
You can change this mapping with the L<driver_class|/driver_class> class method.
=item B<new_or_cached PARAMS>
Constructs or returns a L<Rose::DB> object based on PARAMS, where PARAMS are any name/value pairs that can be passed to the L<new|/new> method. If the L<db_cache|/db_cache>'s L<get_db|Rose::DB::Cache/get_db> method returns an existing L<Rose::DB> ob...
See the L<Rose::DB::Cache> documentation to learn about the cache API and the default implementation.
=back
=head1 OBJECT METHODS
=over 4
=item B<begin_work>
Attempt to start a transaction by calling the L<begin_work|DBI/begin_work> method on the L<DBI> database handle.
If necessary, the database handle will be constructed and connected to the current data source. If this fails, undef is returned. If there is no registered data source for the current C<type> and C<domain>, a fatal error will occur.
If the "AutoCommit" database handle attribute is false, the handle is assumed to already be in a transaction and L<Rose::DB::Constants::IN_TRANSACTION|Rose::DB::Constants> (-1) is returned. If the call to L<DBI>'s L<begin_work|DBI/begin_work> method...
=item B<commit>
Attempt to commit the current transaction by calling the L<commit|DBI/commit> method on the L<DBI> database handle. If the L<DBI> database handle does not exist or is not connected, 0 is returned.
If the "AutoCommit" database handle attribute is true, the handle is assumed to not be in a transaction and L<Rose::DB::Constants::IN_TRANSACTION|Rose::DB::Constants> (-1) is returned. If the call to L<DBI>'s L<commit|DBI/commit> method succeeds, 1 ...
=item B<connect>
Constructs and connects the L<DBI> database handle for the current data source, calling L<dbi_connect|/dbi_connect> to create a new L<DBI> database handle if none exists. If there is no registered data source for the current L<type|/type> and L<doma...
If any L<post_connect_sql|/post_connect_sql> statement failed to execute, the database handle is disconnected and then discarded.
If the database handle returned by L<dbi_connect|/dbi_connect> was originally connected by another L<Rose::DB>-derived object (e.g., if a subclass's custom implementation of L<dbi_connect|/dbi_connect> calls L<DBI>'s L<connect_cached|DBI/connect_cach...
Returns true if the database handle was connected successfully and all L<post_connect_sql|/post_connect_sql> statements (if any) were run successfully, false otherwise.
=item B<connect_option NAME [, VALUE]>
Get or set a single connection option. Example:
$val = $db->connect_option('RaiseError'); # get
$db->connect_option(AutoCommit => 1); # set
Connection options are name/value pairs that are passed in a hash reference as the fourth argument to the call to L<DBI-E<gt>connect()|DBI/connect>. See the L<DBI> documentation for descriptions of the various options.
=item B<connect_options [HASHREF | PAIRS]>
Get or set the L<DBI> connect options hash. If a reference to a hash is passed, it replaces the connect options hash. If a series of name/value pairs are passed, they are added to the connect options hash.
Returns a reference to the connect options has in scalar context, or a list of name/value pairs in list context.
=item B<dbh [DBH]>
Get or set the L<DBI> database handle connected to the current data source. If the database handle does not exist or was created in another process or thread, this method will discard the old database handle (if any) and L<dbi_connect|/dbi_connect> ...
Returns undef if the database handle could not be constructed and connected. If there is no registered data source for the current C<type> and C<domain>, a fatal error will occur.
Note: when setting this attribute, you I<must> pass in a L<DBI> database handle that has the same L<driver|/driver> as the object. For example, if the L<driver|/driver> is C<mysql> then the L<DBI> database handle must be connected to a MySQL databas...
=item B<dbi_connect [ARGS]>
This method calls L<DBI-E<gt>connect(...)|DBI/connect>, passing all ARGS and returning all values. This method has no affect on the internal state of the object. Use the L<connect|/connect> method to create and store a new L<database handle|/dbh> i...
Override this method in your L<Rose::DB> subclass if you want to use a different method (e.g. L<DBI-E<gt>connect_cached()|DBI/connect_cached>) to create database handles.
=item B<disconnect>
Decrements the reference count for the database handle and disconnects it if the reference count is zero and if the database handle was originally connected by this object. (This may not be the case if, say, a subclass's custom implementation of L<d...
Returns true if all L<pre_disconnect_sql|/pre_disconnect_sql> statements (if any) were run successfully and the database handle was disconnected successfully (or if it was simply set to undef), false otherwise.
The database handle will not be disconnected if any L<pre_disconnect_sql|/pre_disconnect_sql> statement fails to execute, and the L<pre_disconnect_sql|/pre_disconnect_sql> is not run unless the handle is going to be disconnected.
=item B<do_transaction CODE [, ARGS]>
Execute arbitrary code within a single transaction, rolling back if any of the code fails, committing if it succeeds. CODE should be a code reference. It will be called with any arguments passed to L<do_transaction|/do_transaction> after the code r...
# Transfer $100 from account id 5 to account id 9
$db->do_transaction(sub
{
my($amt, $id1, $id2) = @_;
my $dbh = $db->dbh or die $db->error;
# Transfer $amt from account id $id1 to account id $id2
$dbh->do("UPDATE acct SET bal = bal - $amt WHERE id = $id1");
$dbh->do("UPDATE acct SET bal = bal + $amt WHERE id = $id2");
},
100, 5, 9) or warn "Transfer failed: ", $db->error;
If the CODE block threw an exception or the transaction could not be started and committed successfully, then undef is returned and the exception thrown is available in the L<error|/error> attribute. Otherwise, a true value is returned.
=item B<error [MSG]>
( run in 1.847 second using v1.01-cache-2.11-cpan-995e09ba956 )