The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

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.

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.

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.

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.

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) = @_;
    ...
  });

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