Mojo-mysql
view release on metacpan or search on metacpan
lib/Mojo/mysql/Database.pm view on Meta::CPAN
$db->insert(some_table => {foo => 'bar'} => sub {
my ($db, $err, $results) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 insert_p
my $promise = $db->insert_p($table, \@values || \%fieldvals, \%options);
Same as L</"insert">, but performs all operations non-blocking and returns a
L<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;
=head2 pid
my $pid = $db->pid;
Return the connection id of the backend server process.
=head2 ping
my $bool = $db->ping;
Check database connection.
=head2 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 L<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 C<json>, will be encoded to
JSON text with L<Mojo::JSON/"to_json">. To accomplish the reverse, you can use
the method L<Mojo::mysql::Results/"expand">, 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 C<type> and C<value> can be
used to bind specific L<DBI> data types (see L<DBI/"DBI Constants">) to
placeholders. This is needed to pass binary data in parameters; see
L<DBD::mysql/"mysql_enable_utf8"> for more information.
# Insert binary data
use DBI ':sql_types';
$db->query('insert into bar values (?)', {type => SQL_BLOB, value => $bytes});
=head2 query_p
my $promise = $db->query_p('select * from foo');
Same as L</"query">, but performs all operations non-blocking and returns a
L<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;
=head2 quote
my $escaped = $db->quote($str);
Quote a string literal for use as a literal value in an SQL statement.
=head2 quote_id
my $escaped = $db->quote_id($id);
Quote an identifier (table name etc.) for use in an SQL statement.
=head2 select
my $results = $db->select($source, $fields, $where, $order);
Generate a C<SELECT> statement with L<Mojo::mysql/"abstract"> (usually an
L<SQL::Abstract> object) and execute it with L</"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;
=head2 select_p
my $promise = $db->select_p($source, $fields, $where, $order);
Same as L</"select">, but performs all operations non-blocking and returns a
L<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;
( run in 0.456 second using v1.01-cache-2.11-cpan-d8267643d1d )