Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Mojo - Prepare cached query

by Anonymous Monk
on Mar 03, 2020 at 12:50 UTC ( [id://11113699]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

app->attr(dbh => sub { my $self = shift; my $dsn = 'dbi:Oracle:schema'; my $usr = 'Scott'; my $pas = 'tiger'; my $dbh = $DBI->connect_cached( $dsn, $usr, $pas ); return $dbh; } # later get '/query' => sub{ my $c = shift; my $dbh = $c->app->dbh; my $sql = 'SQL QUERY HERE'; my $sth = $dbh->pepare_cached( $sql ); my $details = $dbh->selectall_arrayref( $sql, { Slice = {} }); # and so on };
IN above example, I do prepare cached, but don't know how to call the cached query each time the route is visitied. 1. is my initial DBH setup optimal? 2. How do I make use of prepare_cached? Thanks

Replies are listed 'Best First'.
Re: Mojo - Prepare cached query
by tobyink (Canon) on Mar 03, 2020 at 13:37 UTC

    I've not really used Mojo, but I think you probably want something like this...

    app->attr(dbh => sub { my $self = shift; my $dsn = 'dbi:Oracle:schema'; my $usr = 'Scott'; my $pas = 'tiger'; my $dbh = DBI->connect_cached( $dsn, $usr, $pas ); return $dbh; }); app->attr(cached_query => sub { my $self = shift; my $sql = 'SQL QUERY HERE'; return $self->dbh->prepare_cached( $sql ); }); # later get '/query' => sub{ my $c = shift; my $dbh = $c->app->dbh; my $sth = $c->app->cached_query; # probably pull @values out from GET query or POST data my @values = do { ... }; my $results = $dbh->selectall_arrayref($sth, {}, @values); # and so on };
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11113699]
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-19 19:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found