From 28d403a1f6bd70762261a34ee0750d0ffeabc407 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 25 Aug 2020 15:38:26 +0000 Subject: [PATCH] Bug 25624: Add 'operator' option to allow for different comparisons This patch adds the option to specify the operators fper field specified in the script We also add support for testing 'null' fields To test: 1 - Run the script with no parameters and verify the help explains the parameters 2 - Add a patron in category PT (or your choice) with email 'a@b.com' 3 - Add a patron in category PT (or as above) with email 'c@d.com' 4 - Try running with options perl misc/cronjobs/update_patrons_category.pl -f PT -t s --field email=a@b.com (first patron returned) perl misc/cronjobs/update_patrons_category.pl -f PT -t s --field email=a@b.com -o email=e (first patron returned) perl misc/cronjobs/update_patrons_category.pl -f PT -t s --field email=a@b.com -o email=gt (both patrons returned) perl misc/cronjobs/update_patrons_category.pl -f PT -t s --field email=a@b.com -o email=gte (second patron returned) perl misc/cronjobs/update_patrons_category.pl -f PT -t s --field email=a@b% -o email=l (first patron returned) perl misc/cronjobs/update_patrons_category.pl -f PT -t s --field email=a@b% -o email=nl (second patron returned) perl misc/cronjobs/update_patrons_category.pl -f PT -t s --field email=null -o email=e (neither patron returned) perl misc/cronjobs/update_patrons_category.pl -f PT -t s --field email=null -o email=ne (both patrons returned) 5 - Test other combination 6 - Test with --confirm and ensure updates suceed as before patches https://bugs.koha-community.org/show_bug.cgi?id=25264 --- misc/cronjobs/update_patrons_category.pl | 44 +++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/misc/cronjobs/update_patrons_category.pl b/misc/cronjobs/update_patrons_category.pl index 207f9b6cd9..380b6625f8 100755 --- a/misc/cronjobs/update_patrons_category.pl +++ b/misc/cronjobs/update_patrons_category.pl @@ -55,7 +55,8 @@ Options: -fu=X --fineunder=X update if fines under X amount -rb=date --regbefore update if registration date is before given date -ra=date --regafter update if registration date is after a given date - -d --dbfield name=value where is a column in the borrowers table, patrons will be updated if the field is equal to given + -d --field name=value where is a column in the borrowers table, patrons will be updated if the field is equal to given + -o --operator name=value where is a column in the borrowers table specified in a --dbfield param and is the operator to use from (l,le,e,ge,e) -v -verbose verbose mode -c --confirm commit changes to db, no action will be taken unless this switch is included -b --branch only deal with patrons from this library/branch @@ -127,6 +128,26 @@ e.g. --field dateexpiry=2016-01-01 will update all patrons who expired on that date, useful for schools etc. +=item B<--operator column=value | -o column=value> + +Use this flag to specify a column in the borrowers table from a --field param and a comparison operator using options: + +lt for < or 'less than' +lte for <= or 'less than or equal' +e for = or 'equal' +gte for >= ir 'greater than or equal' +gt for > or 'greater than' +ne for != or 'not equal' +l for LIKE, use % for wildcard +nl for NOT LIKE, use % for wildcard + +e.g. +--field email=null --operator email=ne +will update all patrons who do not have a null email + +Note: 'null' will be treated as a string for any operator other then e or ne +i.e. --field email=null --operator email=l will search patrons with email like the word 'null' + =back =head1 DESCRIPTION @@ -163,6 +184,7 @@ my $reg_bef; my $reg_aft; my $branch_lim; my %fields; +my %operators = (); GetOptions( 'help|?' => \$help, @@ -178,7 +200,8 @@ GetOptions( 'rb|regbefore=s' => \$reg_bef, 'ra|regafter=s' => \$reg_aft, 'b|branch=s' => \$branch_lim, - 'd|field=s' => \%fields + 'd|field=s' => \%fields, + 'o|operator=s' => \%operators ); pod2usage(1) if $help; @@ -234,9 +257,22 @@ if ($verbose) { } } +my %operator_values = ( + lt => '<', + lte => '<=', + e => '=', + gte => '>=', + gt => '>', + ne => '!=', + l => 'LIKE', + nl => 'NOT LIKE' +); + while ( my ( $key, $value ) = each %fields ) { - $verbose and print " Borrower column $key is equal to $value\n"; - $params{ "me." . $key } = $value; + my $operator = $operator_values{ $operators{$key} } // '='; + $verbose and print " Borrower column $key is $operator to $value\n"; + $value = undef if lc($value) eq 'null' && ($operator eq '=' || $operator eq '!='); + $params{ "me." . $key } = { $operator => $value }; } my $target_patrons = Koha::Patrons->search(\%params)->search_patrons_to_update_category( { -- 2.11.0