Skip to content

Instantly share code, notes, and snippets.

/abstract.diff Secret

Created March 11, 2015 21:19
Show Gist options
  • Save anonymous/24be8f1f4a378bac6f66 to your computer and use it in GitHub Desktop.
Save anonymous/24be8f1f4a378bac6f66 to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/Pg/Database.pm b/lib/Mojo/Pg/Database.pm
index 12bb1a8..7767134 100644
--- a/lib/Mojo/Pg/Database.pm
+++ b/lib/Mojo/Pg/Database.pm
@@ -9,6 +9,7 @@ use Mojo::Pg::Results;
use Mojo::Pg::Transaction;
use Mojo::Util 'deprecated';
use Scalar::Util 'weaken';
+use SQL::Abstract;
has [qw(dbh pg)];
@@ -47,6 +48,11 @@ sub do {
sub dollar_only { ++$_[0]{dollar_only} and return $_[0] }
+sub insert {
+ my $self = shift;
+ return $self->query($self->_abstract->insert(@_));
+}
+
sub is_listening { !!keys %{shift->{listen} || {}} }
sub listen {
@@ -102,6 +108,11 @@ sub query {
$self->$_ for qw(_next _watch);
}
+sub select {
+ my $self = shift;
+ return $self->query($self->_abstract->select(@_));
+}
+
sub unlisten {
my ($self, $name) = @_;
@@ -114,6 +125,8 @@ sub unlisten {
return $self;
}
+sub _abstract { shift->{abstract} ||= SQL::Abstract->new }
+
sub _json { ref $_[0] eq 'HASH' && (keys %{$_[0]})[0] eq 'json' }
sub _next {
@@ -272,6 +285,8 @@ to be used as an operator.
Check if L</"dbh"> is listening for notifications.
+=head2 insert
+
=head2 listen
$db = $db->listen('foo');
@@ -313,6 +328,8 @@ results. You can also append a callback to perform operation non-blocking.
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
+=head2 select
+
=head2 unlisten
$db = $db->unlisten('foo');
diff --git a/t/results.t b/t/results.t
index 64cf2b7..629b706 100644
--- a/t/results.t
+++ b/t/results.t
@@ -17,31 +17,29 @@ $db->query(
name text
)'
);
-$db->query('insert into results_test (name) values (?)', $_) for qw(foo bar);
+$db->insert(results_test => {name => $_}) 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'],
+is_deeply $db->select('results_test')->rows, 2, 'two rows';
+is_deeply $db->select('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,
+is_deeply $db->select('results_test')->array, [1, 'foo'], 'right structure';
+is_deeply $db->select('results_test')->arrays->to_array,
[[1, 'foo'], [2, 'bar']], 'right structure';
-is_deeply $db->query('select * from results_test')->hash,
- {id => 1, name => 'foo'}, 'right structure';
-is_deeply $db->query('select * from results_test')->hashes->to_array,
+is_deeply $db->select('results_test')->hash, {id => 1, name => 'foo'},
+ 'right structure';
+is_deeply $db->select('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';
+is $pg->db->select('results_test')->text, "1 foo\n2 bar\n", 'right text';
# Transactions
{
my $tx = $db->begin;
- $db->query("insert into results_test (name) values ('tx1')");
- $db->query("insert into results_test (name) values ('tx1')");
+ $db->insert(results_test => {name => 'tx1'});
+ $db->insert(results_test => {name => 'tx1'});
$tx->commit;
};
-is_deeply $db->query('select * from results_test where name = ?', 'tx1')
+is_deeply $db->select(results_test => '*' => {name => 'tx1'})
->hashes->to_array, [{id => 3, name => 'tx1'}, {id => 4, name => 'tx1'}],
'right structure';
{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment