Mojo::mysql::Database - Database
use Mojo::mysql::Database; my $db = Mojo::mysql::Database->new(mysql => $mysql, dbh => $dbh);
Mojo::mysql::Database is a container for database handles used by Mojo::MySQL.
Mojo::mysql::Database implements the following attributes.
my $dbh = $db->dbh; $db = $db->dbh(DBI->new);
Database handle used for all queries.
my $mysql = $db->mysql; $db = $db->mysql(Mojo::mysql->new);
Mojo::mysql object this database belongs to.
$class = $db->results_class; $db = $db->results_class("MyApp::Results");
Class to be used by "query", defaults to Mojo::mysql::Results. Note that this class needs to have already been loaded before "query" is called.
Mojo::mysql::Database inherits all methods from Mojo::EventEmitter and implements the following new ones.
my $num = $db->backlog;
Number of waiting non-blocking queries.
my $tx = $db->begin;
Begin transaction and return Mojo::mysql::Transaction object, which will automatically roll back the transaction unless "commit" in Mojo::mysql::Transaction bas been called before it is destroyed.
# Add names in a transaction eval { my $tx = $db->begin; $db->query('insert into names values (?)', 'Baerbel'); $db->query('insert into names values (?)', 'Wolfgang'); $tx->commit; }; say $@ if $@;
my $results = $db->delete($table, \%where);
Generate a DELETE statement with "abstract" in Mojo::mysql (usually an SQL::Abstract object) and execute it with "query". You can also append a callback to perform operations non-blocking.
DELETE
$db->delete(some_table => sub { my ($db, $err, $results) = @_; ... }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
$db->disconnect;
Disconnect database handle and prevent it from getting cached again.
my $results = $db->insert($table, \@values || \%fieldvals, \%options);
Generate an INSERT statement with "abstract" in Mojo::mysql (usually an SQL::Abstract object) and execute it with "query". You can also append a callback to perform operations non-blocking.
INSERT
$db->insert(some_table => {foo => 'bar'} => sub { my ($db, $err, $results) = @_; ... }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
my $pid = $db->pid;
Return the connection id of the backend server process.
my $bool = $db->ping;
Check database connection.
my $results = $db->query('select * from foo'); my $results = $db->query('insert into foo values (?, ?, ?)', @values);
Execute a blocking statement and return a Mojo::mysql::Results object with the results. You can also append a callback to perform operation non-blocking.
$db->query('select * from foo' => sub { my ($db, $err, $results) = @_; ... }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
my $escaped = $db->quote($str);
Quote a string literal for use as a literal value in an SQL statement.
my $escaped = $db->quote_id($id);
Quote an identifier (table name etc.) for use in an SQL statement.
my $results = $db->select($source, $fields, $where, $order);
Generate a SELECT statement with "abstract" in Mojo::mysql (usually an SQL::Abstract object) and execute it with "query". You can also append a callback to perform operations non-blocking.
SELECT
$db->select(some_table => ['foo'] => sub { my ($db, $err, $results) = @_; ... }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
my $results = $db->update($table, \%fieldvals, \%where);
Generate an UPDATE statement with "abstract" in Mojo::mysql (usually an SQL::Abstract object) and execute it with "query". You can also append a callback to perform operations non-blocking.
UPDATE
$db->update(some_table => {foo => 'baz'} => {foo => 'bar'} => sub { my ($db, $err, $results) = @_; ... }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Mojo::mysql.
To install Mojo::mysql, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojo::mysql
CPAN shell
perl -MCPAN -e shell install Mojo::mysql
For more information on module installation, please visit the detailed CPAN module installation guide.