NAME

Mojo::mysql::Database - Database

SYNOPSIS

use Mojo::mysql::Database;

my $db = Mojo::mysql::Database->new(mysql => $mysql, dbh => $dbh);

DESCRIPTION

Mojo::mysql::Database is a container for database handles used by Mojo::mysql.

ATTRIBUTES

Mojo::mysql::Database implements the following attributes.

dbh

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

Database handle used for all queries.

mysql

my $mysql = $db->mysql;
$db       = $db->mysql(Mojo::mysql->new);

Mojo::mysql object this database belongs to.

results_class

$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.

METHODS

Mojo::mysql::Database inherits all methods from Mojo::EventEmitter and implements the following new ones.

backlog

my $num = $db->backlog;

Number of waiting non-blocking queries.

begin

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 $@;

delete

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.

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

delete_p

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

Same as L</"delete">, but performs all operations non-blocking and returns a
L<Mojo::Promise> object instead of accepting a callback.

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

disconnect

$db->disconnect;

Disconnect database handle and prevent it from getting cached again.

insert

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.

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

insert_p

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

Same as "insert", but performs all operations non-blocking and returns a Mojo::Promise object instead of accepting a callback.

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

pid

my $pid = $db->pid;

Return the connection id of the backend server process.

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('insert into foo values (?)', {json => {bar => 'baz'}});
my $results = $db->query('insert into foo values (?)', {type => SQL_INTEGER, value => 42});

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;

Hash reference arguments containing a value named json, will be encoded to JSON text with "to_json" in Mojo::JSON. To accomplish the reverse, you can use the method "expand" in Mojo::mysql::Results, which automatically decodes data back to Perl data structures.

$db->query('insert into foo values (x) values (?)', {json => {bar => 'baz'}});
$db->query('select * from foo')->expand->hash->{x}{bar}; # baz

Hash reference arguments containing values named type and value can be used to bind specific DBI data types (see "DBI Constants" in DBI) to placeholders. This is needed to pass binary data in parameters; see "mysql_enable_utf8" in DBD::mysql for more information.

# Insert binary data
use DBI ':sql_types';
$db->query('insert into bar values (?)', {type => SQL_BLOB, value => $bytes});

query_p

my $promise = $db->query_p('select * from foo');

Same as "query", but performs all operations non-blocking and returns a Mojo::Promise object instead of accepting a callback.

$db->query_p('insert into foo values (?, ?, ?)' => @values)->then(sub {
  my $results = shift;
  ...
})->catch(sub {
  my $err = shift;
  ...
})->wait;

quote

my $escaped = $db->quote($str);

Quote a string literal for use as a literal value in an SQL statement.

quote_id

my $escaped = $db->quote_id($id);

Quote an identifier (table name etc.) for use in an SQL statement.

select

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.

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

select_p

my $promise = $db->select_p($source, $fields, $where, $order);

Same as "select", but performs all operations non-blocking and returns a Mojo::Promise object instead of accepting a callback.

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

update

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.

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

update_p

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

Same as "update", but performs all operations non-blocking and returns a Mojo::Promise object instead of accepting a callback.

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

tables

my $tables = $db->tables;

Return an array reference with table names for this database.

SEE ALSO

Mojo::mysql.