DBIx::Handler (original) (raw)

NAME

DBIx::Handler - fork-safe and easy transaction handling DBI handler

SYNOPSIS

use DBIx::Handler;
my <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>h</mi><mi>a</mi><mi>n</mi><mi>d</mi><mi>l</mi><mi>e</mi><mi>r</mi><mo>=</mo><mi>D</mi><mi>B</mi><mi>I</mi><mi>x</mi><mo>:</mo><mo>:</mo><mi>H</mi><mi>a</mi><mi>n</mi><mi>d</mi><mi>l</mi><mi>e</mi><mi>r</mi><mo>−</mo><mo>&gt;</mo><mi>n</mi><mi>e</mi><mi>w</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">handler = DBIx::Handler-&gt;new(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">han</span><span class="mord mathnormal">d</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.02778em;">D</span><span class="mord mathnormal" style="margin-right:0.05017em;">B</span><span class="mord mathnormal" style="margin-right:0.07847em;">I</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">::</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal" style="margin-right:0.08125em;">H</span><span class="mord mathnormal">an</span><span class="mord mathnormal">d</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">&gt;</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">n</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mopen">(</span></span></span></span>dsn, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>u</mi><mi>s</mi><mi>e</mi><mi>r</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">user, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">u</span><span class="mord mathnormal" style="margin-right:0.02778em;">ser</span><span class="mpunct">,</span></span></span></span>pass, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi><mi>b</mi><msub><mi>i</mi><mi>o</mi></msub><mi>p</mi><mi>t</mi><mi>s</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">dbi_opts, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">d</span><span class="mord mathnormal">b</span><span class="mord"><span class="mord mathnormal">i</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">o</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">pt</span><span class="mord mathnormal">s</span><span class="mpunct">,</span></span></span></span>opts);
my <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi><mi>b</mi><mi>h</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">dbh = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">d</span><span class="mord mathnormal">bh</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>handler->dbh;
$dbh->do(...);

DESCRIPTION

DBIx::Handler is fork-safe and easy transaction handling DBI handler.

DBIx::Handler provide scope base transaction, fork safe dbh handling, simple.

METHODS

my handler=DBIx::Handler−>new(handler = DBIx::Handler->new(handler=DBIx::Handler>new(dsn, user,user, user,pass, dbiopts,dbi_opts, dbiopts,opts);

get database handling instance.

Options:

on_connect_do : CodeRef|ArrayRef[Str]|Str

on_disconnect_do : CodeRef|ArrayRef[Str]|Str

Execute SQL or CodeRef when connected/disconnected.

result_class : ClassName

This is a query method's result class. If this value is defined, $result_class-new($handler, $sth)> is called in query() and query() returns the instance.

trace_query : Bool

Enables to inject a caller information as SQL comment.

trace_ignore_if : CodeRef

Ignore to inject the SQL comment when trace_ignore_if's return value is true.

no_ping : Bool

By default, ping before each executing query. If it affect performance then you can set to true for ping stopping.

dbi_class : ClassName

By default, this module uses generally DBI class. For example, if you want to use another custom class compatibility with DBI, you can use it with this option.

prepare_method : Str

By default, this module uses generally prepare method. For example, if you want to use prepare_cached method or other custom method compatibility with prepare method, you can use it with this option.

my handler=DBIx::Handler−>connect(handler = DBIx::Handler->connect(handler=DBIx::Handler>connect(dsn, user,user, user,pass, $opts);

connect method is alias for new method.

my dbh=dbh = dbh=handler->dbh;

get fork safe DBI handle.

$handler->disconnect;

disconnect current database handle.

my txnguard=txn_guard = txnguard=handler->txn_scope

Creates a new transaction scope guard object.

do {
    my <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>x</mi><msub><mi>n</mi><mi>g</mi></msub><mi>u</mi><mi>a</mi><mi>r</mi><mi>d</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">txn_guard = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9805em;vertical-align:-0.2861em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">x</span><span class="mord"><span class="mord mathnormal">n</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.03588em;">g</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mord mathnormal">u</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>handler->txn_scope;
        # some process
    $txn_guard->commit;
}

If an exception occurs, or the guard object otherwise leaves the scope before $txn->commit is called, the transaction will be rolled back by an explicit "txn_rollback" call. In essence this is akin to using a "txn_begin"/"txn_commit" pair, without having to worry about calling "txn_rollback" at the right places. Note that since there is no defined code closure, there will be no retries and other magic upon database disconnection. txnmanager=txn_manager = txnmanager=handler->txn_manager

Get the DBIx::TransactionManager instance.

$handler->txn_begin

start new transaction.

$handler->txn_commit

commit transaction.

$handler->txn_rollback

rollback transaction.

$handler->in_txn

are you in transaction?

my @result = handler−>txn(handler->txn(handler>txn(coderef);

execute $coderef in auto transaction scope.

begin transaction before coderefexecute,docoderef execute, do coderefexecute,docoderef with database handle, after commit or rollback transaction.

$handler->txn(sub {
    my $dbh = shift;
    $dbh->do(...);
});

equals to:

$handler->txn_begin;
    my <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi><mi>b</mi><mi>h</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">dbh = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">d</span><span class="mord mathnormal">bh</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>handler->dbh;
    $dbh->do(...);
$handler->txn_rollback;

my @result = handler−>run(handler->run(handler>run(coderef);

execute $coderef.

my <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>s</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">rs = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">rs</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>handler->run(sub {
    my $dbh = shift;
    $dbh->selectall_arrayref(...);
});

or

my @result = $handler->run(sub {
    my $dbh = shift;
    $dbh->selectrow_array('...');
});

my sth=sth = sth=handler->query($sql, [\@bind | \%bind]);

execute query. return database statement handler.

my sql=sql = sql=handler->trace_query_set_comment($sql);

inject a caller information as a SQL comment to $sql when trace_query is true.

ACCESSORS

The setters and the getters for options.

result_class

trace_query

trace_ignore_if

no_ping

on_connect_do

on_disconnect_do

Atsushi Kobayashi <nekokak _at_ gmail _dot_ com>

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.