diff --git a/bin/ysql b/bin/ysql index 9aaa097..6e0b5a2 100755 --- a/bin/ysql +++ b/bin/ysql @@ -48,6 +48,7 @@ __END__ ysql --select ysql --insert
+ ysql --delete
ysql --save ysql --edit @@ -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 @@ -154,6 +159,11 @@ Generate an C query for the given C
. For each document read on STDIN, the correct fields and values will be used. +=head2 --delete
+ +Generate a C query for the given C
. Also accepts +the C<--where> option. + =head2 --where Add a C clause to a C<--select> query. diff --git a/lib/ETL/Yertl/Command/ysql.pm b/lib/ETL/Yertl/Command/ysql.pm index fe286a9..3bc7fa8 100644 --- a/lib/ETL/Yertl/Command/ysql.pm +++ b/lib/ETL/Yertl/Command/ysql.pm @@ -35,6 +35,7 @@ sub main { 'edit|e=s', 'select=s', 'insert=s', + 'delete=s', 'where=s', 'order|order-by|sort=s', ); @@ -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; diff --git a/t/bin/ysql.t b/t/bin/ysql.t index 74011f2..1d6cad0 100644 --- a/t/bin/ysql.t +++ b/t/bin/ysql.t @@ -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', 'hank@example.com' ], + ); + }; + + }; }; subtest 'saved queries' => sub {