Tiny Mojolicious Wrapper for Sqlite
Mojo::SQLite is a tiny wrapper around DBD::SQLite that makes at
https://www.sqlite.org/ a lot of fun to use with the at https://mojolico.us
real-time web framework.
Database and statement handles are cached automatically, so they can be
reused transparently to increase performance. And you can handle connection
timeouts gracefully by holding on to them only for short amounts of time.
use Mojolicious::Lite;
use Mojo::SQLite;
helper sqlite => sub { state $sql = Mojo::SQLite->new('sqlite:sqlite.db') };
get '/' => sub {
my $c = shift;
my $db = $c->sqlite->db;
$c->render(json => $db->query('select datetime("now","localtime") as now')->hash);
};
app->start;
All I/O and queries are performed synchronously. However, the "Write-Ahead
Log" journal is enabled for all connections, allowing multiple processes to
read and write concurrently to the same database file (but only one can
write at a time). See http://sqlite.org/wal.html for more information.
# Performed concurrently
my $pid = fork || die $!;
say $sql->db->query('select datetime("now","localtime") as time')->hash->{time};
exit unless $pid;
All cached database handles will be reset automatically if a new process
has been forked, this allows multiple processes to share the same
Mojo::SQLite object safely.
Any database errors will throw an exception as 'RaiseError' is
automatically enabled, so use 'eval' or Try::Tiny to catch them. This makes
transactions with Mojo::SQLite::Database/"begin" easy.
While passing a file path of ':memory:' (or a custom "dsn" with
'mode=memory') will create a temporary database, in-memory databases cannot
be shared between connections, so subsequent calls to "db" may return
connections to completely different databases. For a temporary database
that can be shared between connections and processes, pass a file path of
':temp:' to store the database in a temporary directory (this is the
default), or consider constructing a temporary directory yourself with
File::Temp if you need to reuse the filename. A temporary directory allows
SQLite to create at https://www.sqlite.org/tempfiles.html safely.
use File::Spec::Functions 'catfile';
use File::Temp;
use Mojo::SQLite;
my $tempdir = File::Temp->newdir; # Deleted when object goes out of scope
my $tempfile = catfile $tempdir, 'sqlite.db';
my $sql = Mojo::SQLite->new->from_filename($tempfile);
- Sources inherited from project openSUSE:Leap:15.2
- Download package
-
Checkout Package
osc -A https://api.opensuse.org checkout openSUSE:Leap:15.2:Update/perl-Mojo-SQLite && cd $_
- Create Badge
Source Files
Filename | Size | Changed |
---|---|---|
Mojo-SQLite-3.000.tar.gz | 0000041355 40.4 KB | |
cpanspec.yml | 0000000669 669 Bytes | |
perl-Mojo-SQLite.changes | 0000003594 3.51 KB | |
perl-Mojo-SQLite.spec | 0000005671 5.54 KB |
Revision 3 (latest revision is 10)
branch from 15.0
Comments 0