MAN page from OpenSuSE perl-Mojo-SQLite-2.002-10.1.noarch.rpm
Mojo::SQLite::Database
Section: User Contributed Perl Documentation (3pm)
Updated: 2017-06-01
Index NAME
Mojo::SQLite::Database - Database
SYNOPSIS
use Mojo::SQLite::Database; my $db = Mojo::SQLite::Database->new(sqlite => $sql, dbh => $dbh); $db->query('select * from foo') ->hashes->map(sub { $_->{bar} })->join("\n")->say;
DESCRIPTION
Mojo::SQLite::Database is a container for DBD::SQLite database handlesused by Mojo::SQLite.
EVENTS
Mojo::SQLite::Database inherits all events from Mojo::EventEmitter andcan emit the following new ones.
close
$db->on(close => sub { my $db = shift; ... });
Emitted when the database connection gets closed while waiting fornotifications.
notification
$db->on(notification => sub { my ($db, $name, $payload) = @_; ... });
Emitted when a notification has been received.
ATTRIBUTES
Mojo::SQLite::Database implements the following attributes.
dbh
my $dbh = $db->dbh; $db = $db->dbh($dbh);
DBD::SQLite database handle used for all queries.
# Use DBI utility methods my $quoted = $db->dbh->quote_identifier('foo.bar');
notification_poll_interval
This attribute is
DEPRECATED.
results_class
my $class = $db->results_class; $db = $db->results_class('MyApp::Results');
Class to be used by ``query'', defaults to Mojo::SQLite::Results. Notethat this class needs to have already been loaded before ``query'' is called.
sqlite
my $sql = $db->sqlite; $db = $db->sqlite(Mojo::SQLite->new);
Mojo::SQLite object this database belongs to.
METHODS
Mojo::SQLite::Database inherits all methods from Mojo::Base andimplements the following new ones.
begin
my $tx = $db->begin; my $tx = $db->begin('exclusive');
Begin transaction and return Mojo::SQLite::Transaction object, which willautomatically roll back the transaction unless``commit'' in Mojo::SQLite::Transaction has been called before it is destroyed.
# Insert rows in a transaction eval { my $tx = $db->begin; $db->insert('frameworks', {name => 'Catalyst'}); $db->insert('frameworks', {name => 'Mojolicious'}); $tx->commit; }; say $@ if $@;
A transaction locking behavior of "deferred", "immediate", or "exclusive"may optionally be passed; the default in DBD::SQLite is currently"immediate". See ``Transaction and Database Locking'' in DBD::SQLite for moredetails.
delete
my $results = $db->delete($table, \%where);
Generate a "DELETE" statement with ``abstract'' in Mojo::SQLite (usually anSQL::Abstract object) and execute it with ``query''. You can also append acallback for API compatibility with Mojo::Pg; the query is still executed ina blocking manner.
$db->delete(some_table => sub { my ($db, $err, $results) = @_; ... }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Use all the same argument variations you would pass to the "delete" method ofSQL::Abstract.
# "delete from some_table" $db->delete('some_table'); # "delete from some_table where foo = 'bar'" $db->delete('some_table', {foo => 'bar'}); # "delete from some_table where foo like '%test%'" $db->delete('some_table', {foo => {-like => '%test%'}});
disconnect
$db->disconnect;
Disconnect ``dbh'' and prevent it from getting reused.
insert
my $results = $db->insert($table, \@values || \%fieldvals, \%options);
Generate an "INSERT" statement with ``abstract'' in Mojo::SQLite (usually anSQL::Abstract object) and execute it with ``query''. You can also append acallback for API compatibility with Mojo::Pg; the query is still executed ina blocking manner.
$db->insert(some_table => {foo => 'bar'} => sub { my ($db, $err, $results) = @_; ... }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Use all the same argument variations you would pass to the "insert" method ofSQL::Abstract.
# "insert into some_table (foo, baz) values ('bar', 'yada')" $db->insert('some_table', {foo => 'bar', baz => 'yada'});
is_listening
This method is
DEPRECATED.
listen
This method is
DEPRECATED.
notify
This method is
DEPRECATED.
ping
my $bool = $db->ping;
Check database connection.
query
my $results = $db->query('select * from foo'); my $results = $db->query('insert into foo values (?, ?, ?)', @values); my $results = $db->query('select ? as img', {type => SQL_BLOB, value => slurp 'img.jpg'}); my $results = $db->query('select ? as foo', {json => {bar => 'baz'}});
Execute a blocking SQL <http://www.postgresql.org/docs/current/static/sql.html>statement and return a results object based on ``results_class'' (which isusually Mojo::SQLite::Results) with the query results. The DBD::SQLitestatement handle will be automatically reused when it is not active anymore, toincrease the performance of future queries. You can also append a callback forAPI compatibility with Mojo::Pg; the query is still executed in a blockingmanner.
$db->query('insert into foo values (?, ?, ?)' => @values => sub { my ($db, $err, $results) = @_; ... }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Hash reference arguments containing "type" and "value" elements will use thespecified bind type for the parameter, using types from ``DBI Constants'' in DBI;see ``Blobs'' in DBD::SQLite and the subsequent section for more information.
Hash reference arguments containing a value named "json" will be encoded toJSON text <http://sqlite.org/json1.html> with ``to_json'' in Mojo::JSON. Toaccomplish the reverse, you can use the method ``expand'' in Mojo::SQLite::Resultsto decode JSON text fields to Perl values with ``from_json'' in Mojo::JSON.
# "I X SQLite!" $db->query('select ? as foo', {json => {bar => 'I X SQLite!'}}) ->expand(json => 'foo')->hash->{foo}{bar};
select
my $results = $db->select($source, $fields, $where, $order);
Generate a "SELECT" statement with ``abstract'' in Mojo::SQLite (usually anSQL::Abstract object) and execute it with ``query''. You can also append acallback for API compatibility with Mojo::Pg; the query is still executed ina blocking manner.
$db->select(some_table => ['foo'] => {bar => 'yada'} => sub { my ($db, $err, $results) = @_; ... }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Use all the same argument variations you would pass to the "select" method ofSQL::Abstract.
# "select * from some_table" $db->select('some_table'); # "select id, foo from some_table" $db->select('some_table', ['id', 'foo']); # "select * from some_table where foo = 'bar'" $db->select('some_table', undef, {foo => 'bar'}); # "select * from some_table where foo = 'bar' order by id desc" $db->select('some_table', undef, {foo => 'bar'}, {-desc => 'id'}); # "select * from some_table where foo like '%test%'" $db->select('some_table', undef, {foo => {-like => '%test%'}});
tables
my $tables = $db->tables;
Return table and view names for this database, that are visible to the currentuser and not internal, as an array reference. Names will be quoted and prefixedby a schema name of "main" for standard tables, "temp" for temporarytables, and the appropriate schema name forattached databases <http://sqlite.org/lang_attach.html>.
# Names of all tables say for @{$db->tables};
unlisten
This method is
DEPRECATED.
update
my $results = $db->update($table, \%fieldvals, \%where);
Generate an "UPDATE" statement with ``abstract'' in Mojo::SQLite (usually anSQL::Abstract object) and execute it with ``query''. You can also append acallback for API compatibility with Mojo::Pg; the query is still executed ina blocking manner.
$db->update(some_table => {foo => 'baz'} => {foo => 'bar'} => sub { my ($db, $err, $results) = @_; ... }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Use all the same argument variations you would pass to the "update" method ofSQL::Abstract.
# "update some_table set foo = 'bar' where id = 23" $db->update('some_table', {foo => 'bar'}, {id => 23}); # "update some_table set foo = 'bar' where foo like '%test%'" $db->update('some_table', {foo => 'bar'}, {foo => {-like => '%test%'}});
BUGS
Report any issues on the public bugtracker.
AUTHOR
Dan Book,
"dbookAATTcpan.org" COPYRIGHT AND LICENSE
Copyright 2015, Dan Book.
This library is free software; you may redistribute it and/or modify it underthe terms of the Artistic License version 2.0.
SEE ALSO
Mojo::SQLite
Index
- NAME
- SYNOPSIS
- DESCRIPTION
- EVENTS
- close
- notification
- ATTRIBUTES
- dbh
- notification_poll_interval
- results_class
- sqlite
- METHODS
- begin
- delete
- disconnect
- insert
- is_listening
- listen
- notify
- ping
- query
- select
- tables
- unlisten
- update
- BUGS
- AUTHOR
- COPYRIGHT AND LICENSE
- SEE ALSO
This document was created byman2html,using the manual pages.