Skip to content

Commit

Permalink
add --where helper for select queries
Browse files Browse the repository at this point in the history
Now we can add some basic filtering when using the query helpers.

Refs #136
  • Loading branch information
preaction committed Nov 25, 2015
1 parent 41dcbf6 commit 965f459
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
8 changes: 8 additions & 0 deletions bin/ysql
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ Instead of C<SELECT * FROM person>, we can use the C<--select> helper:
$ ysql db_name --select person
And we can combine it with the C<--where> helper:
$ ysql db_name --select person --where 'dept = "Science"'
And for simple inserts, we can use the C<--insert> helper:
# input.yml
Expand Down Expand Up @@ -150,6 +154,10 @@ Generate an C<INSERT INTO table ( fields ) VALUES ( values )> query for the
given C<table>. For each document read on STDIN, the correct fields and values
will be used.
=head2 --where <clause>
Add a C<WHERE> clause to a C<--select> query.
=head2 --config
View, add, and edit database configuration.
Expand Down
3 changes: 2 additions & 1 deletion lib/ETL/Yertl/Command/ysql.pm
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sub main {
'edit|e=s',
'select=s',
'insert=s',
'where=s',
);
#; use Data::Dumper;
#; say Dumper \@args;
Expand Down Expand Up @@ -199,7 +200,7 @@ sub main {
# Other queries that do not require special handling
my $query;
if ( $opt{select} ) {
$query = $sql->select( $opt{select} );
$query = $sql->select( $opt{select}, '*', $opt{where} );
}
else {
$query = shift @args;
Expand Down
18 changes: 18 additions & 0 deletions t/bin/ysql.t
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,24 @@ subtest 'query' => sub {
email => '[email protected]',
},
);

subtest '--where' => sub {
my ( $stdout, $stderr, $exit ) = capture {
ysql->main( 'testdb', '--select', 'person', '--where', 'id >= 2' );
};
is $exit, 0;
ok !$stderr, 'nothing on stderr' or diag $stderr;

open my $fh, '<', \$stdout;
my $yaml_fmt = ETL::Yertl::Format::yaml->new( input => $fh );
cmp_deeply [ $yaml_fmt->read ], bag(
{
id => 2,
name => 'Quentin Quinn',
email => '[email protected]',
},
);
};
};

subtest '--insert' => sub {
Expand Down

0 comments on commit 965f459

Please sign in to comment.