Mojo::...tabase(3pm)

🌸 NAME 🌸

Mojo::SQLite::Database β€” Database. Yep, that's the whole NAME line bestie. This is a User Contributed Perl Documentation page, and the manual name is Mojo::...tabase(3pm), so you're in exactly the right spot. πŸ’…


πŸŽ€ 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 literally a container for DBD::SQLite database handles used by Mojo::SQLite. That's the vibe: it holds the database handle and gives you all the query methods. πŸ’–


✨ ATTRIBUTES ✨

Mojo::SQLite::Database implements the following attributes. Slay. πŸ‘‘

🌟 dbh

my $dbh = $db->dbh;
$db     = $db->dbh($dbh);

This is the DBD::SQLite database handle used for all queries. Main character energy for sure. πŸ”₯

# Use DBI utility methods
my $quoted = $db->dbh->quote_identifier('foo.bar');

🌟 results_class

my $class = $db->results_class;
$db       = $db->results_class('MyApp::Results');

This is the class that query uses, and it defaults to Mojo::SQLite::Results. Important note, bestie: this class needs to have already been loaded before query is called. No cap. 🫢

🌟 sqlite

my $sql = $db->sqlite;
$db     = $db->sqlite(Mojo::SQLite->new);

This is the Mojo::SQLite object this database belongs to. It's giving β€œmy database parent”. πŸ’œ


πŸ’… METHODS πŸ’…

Mojo::SQLite::Database inherits all methods from Mojo::Base and implements the following new ones. Periodt. ✨

πŸŒ€ begin

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

Begin a transaction and return a Mojo::SQLite::Transaction object. That object will automatically roll back the transaction unless commit in Mojo::SQLite::Transaction has been called before it is destroyed. So yes, if you forget to commit, it undoes the whole thing. Iconic safety behavior. 😭

# 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. More details are in the β€œTransaction and Database Locking” section of DBD::SQLite, and at <https://sqlite.org/lang_transaction.html>. πŸ¦‹

πŸŒ€ delete

my $results = $db->delete($table, \%where);

Generate a DELETE statement with abstract in Mojo::SQLite (usually an SQL::Abstract::Pg object) and execute it with query. You can also append a callback for API compatibility with Mojo::Pg; the query is still executed in a blocking manner. πŸ’€

$db->delete(some_table => sub ($db, $err, $results) {
  ...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

Use all the same argument variations you would pass to the delete method of SQL::Abstract. Bestie, it's flexible like that. πŸ’–

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

πŸŒ€ delete_p

my $promise = $db->delete_p($table, \%where, \%options);

Same as delete but returns a Mojo::Promise object instead of accepting a callback. For API compatibility with Mojo::Pg; the query is still executed in a blocking manner. Promise girlies, this one's for you. πŸŽ€

$db->delete_p('some_table')->then(sub ($results) {
  ...
})->catch(sub ($err) {
  ...
})->wait;

πŸŒ€ disconnect

$db->disconnect;

Disconnect dbh and prevent it from getting reused. Bye-bye, database handle. πŸ‘‹

πŸŒ€ insert

my $results = $db->insert($table, \@values || \%fieldvals, \%options);

Generate an INSERT statement with abstract in Mojo::SQLite (usually an SQL::Abstract::Pg object) and execute it with query. You can also append a callback for API compatibility with Mojo::Pg; the query is still executed in a blocking manner. πŸ”₯

$db->insert(some_table => {foo => 'bar'} => sub ($db, $err, $results) {
  ...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

Use all the same argument variations you would pass to the insert method of SQL::Abstract. πŸ’…

# "insert into some_table (foo, baz) values ('bar', 'yada')"
$db->insert('some_table', {foo => 'bar', baz => 'yada'});

πŸŒ€ insert_p

my $promise = $db->insert_p($table, \@values || \%fieldvals, \%options);

Same as insert but returns a Mojo::Promise object instead of accepting a callback. For API compatibility with Mojo::Pg; the query is still executed in a blocking manner. 🌈

$db->insert_p(some_table => {foo => 'bar'})->then(sub ($results) {
  ...
})->catch(sub ($err) {
  ...
})->wait;

πŸŒ€ ping

my $bool = $db->ping;

Check database connection. Little vibe check for your database. 🫢

πŸŒ€ 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 statement and return a results object based on results_class (which is usually Mojo::SQLite::Results) with the query results. The original points at SQL <http://www.postgresql.org/docs/current/static/sql.html> for the SQL part. The DBD::SQLite statement handle will be automatically reused when it is not active anymore, to increase the performance of future queries. That's lowkey efficient. ⚑

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 ($db, $err, $results) {
  ...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

Hash reference arguments containing type and value elements will use the specified bind type for the parameter, using types from the β€œDBI Constants” section in DBI; see β€œBlobs” in DBD::SQLite and the subsequent section for more information. πŸ’–

Hash reference arguments containing a value named json or -json will be encoded to JSON text (<http://sqlite.org/json1.html>) with to_json in Mojo::JSON. To accomplish the reverse, you can use the method expand in Mojo::SQLite::Results to decode JSON text fields to Perl values with from_json in Mojo::JSON. Obsessed. 🌸

# "I β™₯ SQLite!"
$db->query('select ? as foo', {json => {bar => 'I β™₯ SQLite!'}})
  ->expand(json => 'foo')->hash->{foo}{bar};

πŸŒ€ query_p

my $promise = $db->query_p('SELECT * FROM foo');

Same as query but returns a Mojo::Promise object instead of accepting a callback. For API compatibility with Mojo::Pg; the query is still executed in a blocking manner. Promise bestie approved. πŸŽ€

$db->query_p('INSERT INTO foo VALUES (?, ?, ?)' => @values)->then(sub ($results) {
  ...
})->catch(sub ($err) {
  ...
})->wait;

πŸŒ€ select

my $results = $db->select($source, $fields, $where, $order);

Generate a SELECT statement with abstract in Mojo::SQLite (usually an SQL::Abstract::Pg object) and execute it with query. You can also append a callback for API compatibility with Mojo::Pg; the query is still executed in a blocking manner. πŸ‘‘

$db->select(some_table => ['foo'] => {bar => 'yada'} => sub ($db, $err, $results) {
  ...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

Use all the same argument variations you would pass to the select method of SQL::Abstract. The way this handles query building is honestly iconic. ✨

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

πŸŒ€ select_p

my $promise = $db->select_p($source, $fields, $where, \%options);

Same as select but returns a Mojo::Promise object instead of accepting a callback. For API compatibility with Mojo::Pg; the query is still executed in a blocking manner. πŸ’œ

$db->select_p(some_table => ['foo'] => {bar => 'yada'})->then(sub ($results) {
  ...
})->catch(sub ($err) {
  ...
})->wait;

πŸŒ€ tables

my $tables = $db->tables;

Return table and view names for this database, that are visible to the current user and not internal, as an array reference. Names will be quoted and prefixed by a schema name of main for standard tables, temp for temporary tables, and the appropriate schema name for attached databases <http://sqlite.org/lang_attach.html>. 🌈

# Names of all tables
say for @{$db->tables};

πŸŒ€ update

my $results = $db->update($table, \%fieldvals, \%where);

Generate an UPDATE statement with abstract in Mojo::SQLite (usually an SQL::Abstract::Pg object) and execute it with query. You can also append a callback for API compatibility with Mojo::Pg; the query is still executed in a blocking manner. πŸ”₯

$db->update(some_table => {foo => 'baz'} => {foo => 'bar'} => sub ($db, $err, $results) {
  ...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

Use all the same argument variations you would pass to the update method of SQL::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%'}});

πŸŒ€ update_p

my $promise = $db->update_p($table, \%fieldvals, \%where, \%options);

Same as update but returns a Mojo::Promise object instead of accepting a callback. For API compatibility with Mojo::Pg; the query is still executed in a blocking manner. 🫢

$db->update_p(some_table => {foo => 'baz'} => {foo => 'bar'})->then(sub ($results) {
  ...
})->catch(sub ($err) {
  ...
})->wait;

πŸ› BUGS πŸ›

Report any issues on the public bugtracker. If something's busted, tell the bugtracker, not the group chat. 😭


πŸ‘‘ AUTHOR πŸ‘‘

Dan Book, "dbook@cpan.org". Author icon. πŸ’œ


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. Slay, legal bestie. πŸŽ€


🌟 SEE ALSO 🌟

Mojo::SQLite. That's the see-also. Go click around, iykyk. πŸ¦‹


Signed off with perl v5.42.0, dated 2022-03-23, from the Mojo::...tabase(3pm) manual. πŸ«ΆπŸ’–βœ¨

πŸ“¦ where this page comes from