The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

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 handles used by Mojo::SQLite.

EVENTS

Mojo::SQLite::Database inherits all events from Mojo::EventEmitter and can emit the following new ones.

close

  $db->on(close => sub {
    my $db = shift;
    ...
  });

Emitted when the database connection gets closed while waiting for notifications.

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(DBI->new);

DBD::SQLite database handle used for all queries.

notification_poll_interval

  my $interval = $db->notification_poll_interval;
  $db          = $db->notification_poll_interval(1);

Interval in seconds to poll for notifications from "notify", defaults to 0.5.

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 and implements the following new ones.

new

  my $db = Mojo::SQLite::Database->new;
  my $db = Mojo::SQLite::Database->new(dbh => $dbh, sqlite => Mojo::SQLite->new);
  my $db = Mojo::SQLite::Database->new({dbh => $dbh, sqlite => Mojo::SQLite->new);

Construct a new Mojo::SQLite::Database object.

begin

  my $tx = $db->begin;
  my $tx = $db->begin('exclusive');

Begin transaction and return Mojo::SQLite::Transaction object, which will automatically 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->query('insert into frameworks values (?)', 'Catalyst');
    $db->query('insert into frameworks values (?)', '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 more details.

disconnect

  $db->disconnect;

Disconnect "dbh" and prevent it from getting cached again.

is_listening

  my $bool = $db->is_listening;

Check if "dbh" is listening for notifications.

listen

  $db = $db->listen('foo');

Subscribe to a channel and receive "notification" events when the Mojo::IOLoop event loop is running.

notify

  $db = $db->notify('foo');
  $db = $db->notify(foo => 'bar');

Notify a channel.

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'});

Execute a blocking statement and return a Mojo::SQLite::Results object with the results. The DBD::SQLite statement handle will be automatically reused when it is not active anymore, to increase the performance of future queries. Pass a hash reference containing type and value elements to specify the bind type of the parameter, using types from "DBI Constants" in DBI; see "Blobs" in DBD::SQLite and the subsequent section for more information. You can also append a callback for API compatibility with Mojo::Pg; the query is still executed in a blocking manner.

  $db->query('insert into foo values (?, ?, ?)' => @values => sub {
    my ($db, $err, $results) = @_;
    ...
  });
  Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

unlisten

  $db = $db->unlisten('foo');
  $db = $db->unlisten('*');

Unsubscribe from a channel, * can be used to unsubscribe from all channels.

DEBUGGING

You can set the MOJO_PUBSUB_DEBUG environment variable to get some advanced diagnostics information printed to STDERR.

  MOJO_PUBSUB_DEBUG=1

BUGS

Report any issues on the public bugtracker.

AUTHOR

Dan Book, dbook@cpan.org

COPYRIGHT AND LICENSE

Copyright 2015, Dan Book.

This library is free software; you may redistribute it and/or modify it under the terms of the Artistic License version 2.0.

SEE ALSO

Mojo::SQLite