Skip to content

Commit

Permalink
add the --delete helper option to ysql
Browse files Browse the repository at this point in the history
The `--delete` helper option allows us to generate `DELETE FROM` queries,
optionally with a `WHERE` clause (the `--where` option).

Refs #136
  • Loading branch information
preaction committed Nov 26, 2015
1 parent 1cb0f6e commit 1f6597d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
10 changes: 10 additions & 0 deletions bin/ysql
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ __END__
ysql <db_name> --select <table>
ysql <db_name> --insert <table>
ysql <db_name> --delete <table>
ysql <db_name> --save <query_name> <query>
ysql <db_name> --edit <query_name>
Expand Down Expand Up @@ -128,6 +129,10 @@ And for simple inserts, we can use the C<--insert> helper:
$ ysql db_name --insert person < input.yml
There is a C<--delete> helper as well, which accepts the C<--where> option:
$ ysql db_name --delete person --where 'dept = "Wildlife"'
=head1 ARGUMENTS
=head2 db_name
Expand All @@ -154,6 +159,11 @@ 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 --delete <table>
Generate a C<DELETE FROM table> query for the given C<table>. Also accepts
the C<--where> option.
=head2 --where <clause>
Add a C<WHERE> clause to a C<--select> query.
Expand Down
4 changes: 4 additions & 0 deletions 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',
'delete=s',
'where=s',
'order|order-by|sort=s',
);
Expand Down Expand Up @@ -203,6 +204,9 @@ sub main {
if ( $opt{select} ) {
$query = $sql->select( $opt{select}, '*', $opt{where}, $opt{order} );
}
elsif ( $opt{delete} ) {
$query = $sql->delete( $opt{delete}, $opt{where} );
}
else {
$query = shift @args;

Expand Down
34 changes: 34 additions & 0 deletions t/bin/ysql.t
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,40 @@ subtest 'query' => sub {
);
};

subtest '--delete' => sub {
my ( $home, $dbh ) = $setup->();
local $ENV{HOME} = "$home";

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

cmp_deeply
$dbh->selectall_arrayref( 'SELECT id,name,email FROM person' ),
[];

subtest '--where' => sub {
my ( $home, $dbh ) = $setup->();
local $ENV{HOME} = "$home";

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

cmp_deeply
$dbh->selectall_arrayref( 'SELECT id,name,email FROM person' ),
bag(
[ 1, 'Hazel Murphy', '[email protected]' ],
);
};

};
};

subtest 'saved queries' => sub {
Expand Down

0 comments on commit 1f6597d

Please sign in to comment.