Skip to content

Commit

Permalink
deprecated Mojo::Pg::Database::do in favor of Mojo::Pg::Database::query
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 12, 2015
1 parent 4e20fbd commit d930ef5
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,6 +1,6 @@

1.08 2015-02-12
- Improved do method in Mojo::Pg::Database to accept placeholder values.
- Deprecated Mojo::Pg::Database::do in favor of Mojo::Pg::Database::query.

1.07 2015-01-03
- Added support for encoding and decoding of JSON values.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Pg.pm
Expand Up @@ -96,7 +96,7 @@ Mojo::Pg - Mojolicious ♥ PostgreSQL
# Create a table
my $pg = Mojo::Pg->new('postgresql://postgres@/test');
$pg->db->do('create table if not exists names (name text)');
$pg->db->query('create table if not exists names (name text)');
# Insert a few rows
my $db = $pg->db;
Expand Down
28 changes: 10 additions & 18 deletions lib/Mojo/Pg/Database.pm
Expand Up @@ -7,6 +7,7 @@ use Mojo::IOLoop;
use Mojo::JSON 'encode_json';
use Mojo::Pg::Results;
use Mojo::Pg::Transaction;
use Mojo::Util 'deprecated';
use Scalar::Util 'weaken';

has [qw(dbh pg)];
Expand Down Expand Up @@ -35,8 +36,10 @@ sub disconnect {
}

sub do {
my ($self, $query) = (shift, shift);
$self->dbh->do($query, undef, _values(@_));
deprecated 'Mojo::Pg::Database::do is DEPRECATED'
. ' in favor of Mojo::Pg::Database::query';
my $self = shift;
$self->dbh->do(@_);
$self->_notifications;
return $self;
}
Expand All @@ -50,7 +53,7 @@ sub listen {

my $dbh = $self->dbh;
local $dbh->{AutoCommit} = 1;
$dbh->do('listen ' . $dbh->quote_identifier($name))
$self->query('listen ' . $dbh->quote_identifier($name))
unless $self->{listen}{$name}++;
$self->_watch;

Expand All @@ -70,7 +73,9 @@ sub ping { shift->dbh->ping }
sub query {
my ($self, $query) = (shift, shift);
my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
my @values = _values(@_);

# JSON
my @values = map { _json($_) ? encode_json $_->{json} : $_ } @_;

# Dollar only
my $dbh = $self->dbh;
Expand All @@ -95,7 +100,7 @@ sub unlisten {

my $dbh = $self->dbh;
local $dbh->{AutoCommit} = 1;
$dbh->do('unlisten' . $dbh->quote_identifier($name));
$self->query('unlisten' . $dbh->quote_identifier($name));
$name eq '*' ? delete($self->{listen}) : delete($self->{listen}{$name});
$self->_unwatch unless $self->backlog || $self->is_listening;

Expand All @@ -122,10 +127,6 @@ sub _unwatch {
Mojo::IOLoop->singleton->reactor->remove($self->{handle});
}

sub _values {
map { _json($_) ? encode_json $_->{json} : $_ } @_;
}

sub _watch {
my $self = shift;

Expand Down Expand Up @@ -246,15 +247,6 @@ L<Mojo::Pg::Transaction/"commit"> has been called before it is destroyed.
Disconnect L</"dbh"> and prevent it from getting cached again.
=head2 do
$db = $db->do('create table foo (bar text)');
$db = $db->do('insert into foo values (?, ?, ?)', @values);
Execute a statement and discard its result.
$db->do('insert into foo values (?)', {json => {bar => 'baz'}});
=head2 dollar_only
$db = $db->dollar_only;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Pg/Migrations.pm
Expand Up @@ -50,7 +50,7 @@ sub migrate {
# Lock migrations table and check version again
local @{$db->dbh}{qw(AutoCommit RaiseError)} = (1, 1);
my $tx = $db->begin;
$db->do('lock table mojo_migrations in exclusive mode');
$db->query('lock table mojo_migrations in exclusive mode');
return $self if (my $active = $self->_active($db)) == $target;

# Up
Expand Down
2 changes: 1 addition & 1 deletion t/database.t
Expand Up @@ -215,7 +215,7 @@ Mojo::IOLoop->delay(
my ($delay, $name, $pid, $payload) = @_;
push @notifications, [$name, $pid, $payload];
$db2->listen('bar')->once(notification => $delay->begin);
Mojo::IOLoop->next_tick(sub { $db2->do("notify bar, 'baz'") });
Mojo::IOLoop->next_tick(sub { $db2->query("notify bar, 'baz'") });
},
sub {
my ($delay, $name, $pid, $payload) = @_;
Expand Down
2 changes: 1 addition & 1 deletion t/migrations.t
Expand Up @@ -91,7 +91,7 @@ is $pg2->migrations->migrate(0)->active, 0, 'active version is 0';
eval { $pg->migrations->migrate(23) };
like $@, qr/Version 23 has no migration/, 'right error';

$pg->db->do('drop table mojo_migrations');
$pg->db->query('drop table mojo_migrations');

done_testing();

Expand Down
2 changes: 1 addition & 1 deletion t/pod_coverage.t
Expand Up @@ -7,4 +7,4 @@ plan skip_all => 'set TEST_POD to enable this test (developer only!)'
plan skip_all => 'Test::Pod::Coverage 1.04 required for this test!'
unless eval 'use Test::Pod::Coverage 1.04; 1';

all_pod_coverage_ok();
all_pod_coverage_ok({also_private => ['do']});
58 changes: 24 additions & 34 deletions t/results.t
Expand Up @@ -10,68 +10,58 @@ plan skip_all => 'set TEST_ONLINE to enable this test'
use Mojo::Pg;

my $pg = Mojo::Pg->new($ENV{TEST_ONLINE});
my $db = $pg->db->do(
my $db = $pg->db;
$db->query(
'create table if not exists results_test (
id serial primary key,
name text,
info json
name text
)'
);
$db->do('insert into results_test (name, info) values (?, ?)',
$_, {json => {$_ => $_}})
for qw(foo bar);
$db->query('insert into results_test (name) values (?)', $_) for qw(foo bar);

# Result methods
is_deeply $db->query('select * from results_test')->rows, 2, 'two rows';
is_deeply $db->query('select * from results_test')->columns,
['id', 'name', 'info'], 'right structure';
is_deeply $db->query('select * from results_test')->array,
[1, 'foo', '{"foo":"foo"}'], 'right structure';
is_deeply $db->query('select * from results_test')->arrays->to_array,
[[1, 'foo', '{"foo":"foo"}'], [2, 'bar', '{"bar":"bar"}']],
is_deeply $db->query('select * from results_test')->columns, ['id', 'name'],
'right structure';
is_deeply $db->query('select * from results_test')->array, [1, 'foo'],
'right structure';
is_deeply $db->query('select * from results_test')->arrays->to_array,
[[1, 'foo'], [2, 'bar']], 'right structure';
is_deeply $db->query('select * from results_test')->hash,
{id => 1, name => 'foo', info => '{"foo":"foo"}'}, 'right structure';
my $results = [
{id => 1, name => 'foo', info => {foo => 'foo'}},
{id => 2, name => 'bar', info => {bar => 'bar'}}
];
is_deeply $db->query('select * from results_test')->expand->hashes->to_array,
$results, 'right structure';
is $pg->db->query('select * from results_test')->text,
qq/1 foo {"foo":"foo"}\n2 bar {"bar":"bar"}\n/, 'right text';
{id => 1, name => 'foo'}, 'right structure';
is_deeply $db->query('select * from results_test')->hashes->to_array,
[{id => 1, name => 'foo'}, {id => 2, name => 'bar'}], 'right structure';
is $pg->db->query('select * from results_test')->text, "1 foo\n2 bar\n",
'right text';

# Transactions
{
my $tx = $db->begin;
$db->do("insert into results_test (name) values ('tx1')")
->do("insert into results_test (name) values ('tx1')");
$db->query("insert into results_test (name) values ('tx1')");
$db->query("insert into results_test (name) values ('tx1')");
$tx->commit;
};
$results = [
{id => 3, name => 'tx1', info => undef},
{id => 4, name => 'tx1', info => undef}
];
is_deeply $db->query('select * from results_test where name = ?', 'tx1')
->hashes->to_array, $results, 'right structure';
->hashes->to_array, [{id => 3, name => 'tx1'}, {id => 4, name => 'tx1'}],
'right structure';
{
my $tx = $db->begin;
$db->do("insert into results_test (name) values ('tx2')")
->do("insert into results_test (name) values ('tx2')");
$db->query("insert into results_test (name) values ('tx2')");
$db->query("insert into results_test (name) values ('tx2')");
};
is_deeply $db->query('select * from results_test where name = ?', 'tx2')
->hashes->to_array, [], 'no results';
eval {
my $tx = $db->begin;
$db->do("insert into results_test (name) values ('tx3')")
->do("insert into results_test (name) values ('tx3')")
->do('does_not_exist');
$db->query("insert into results_test (name) values ('tx3')");
$db->query("insert into results_test (name) values ('tx3')");
$db->query('does_not_exist');
$tx->commit;
};
like $@, qr/does_not_exist/, 'right error';
is_deeply $db->query('select * from results_test where name = ?', 'tx3')
->hashes->to_array, [], 'no results';

$db->do('drop table results_test');
$db->query('drop table results_test');

done_testing();

0 comments on commit d930ef5

Please sign in to comment.