From a63acfd68cf2d25e035d3f949fc713d6e138b7b5 Mon Sep 17 00:00:00 2001 From: dbs Date: Fri, 20 Aug 2010 18:38:35 +0000 Subject: [PATCH] Add an API for counting the number of bibs linked to each authority record in the input list srfsh# request open-ils.cat open-ils.cat.authority.records.count_linked_bibs [1,2,3,4,5,6] Received Data: [ { "bibs":1, "authority":1 }, { "bibs":1, "authority":2 }, { "bibs":2, "authority":3 } ] git-svn-id: svn://svn.open-ils.org/ILS/trunk@17290 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../perlmods/OpenILS/Application/Cat/Authority.pm | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Cat/Authority.pm b/Open-ILS/src/perlmods/OpenILS/Application/Cat/Authority.pm index f46d85483..88bd5cc34 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Cat/Authority.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Cat/Authority.pm @@ -98,4 +98,51 @@ sub batch_retrieve_authority_record { return undef; } +__PACKAGE__->register_method( + method => 'count_linked_bibs', + api_name => 'open-ils.cat.authority.records.count_linked_bibs', + signature => q/ + Counts the number of bib records linked to each authority record in the input list + @param records Array of authority records to return counts + @return A list of hashes containing the authority record ID ("id") and linked bib count ("bibs") + / +); + +sub count_linked_bibs { + my( $self, $conn, $records ) = @_; + + my $editor = new_editor(); + + my $link_count; + my @clean_records; + for my $auth ( @$records ) { + # Protection against SQL injection? Might be overkill. + my $intauth = int($auth); + if ($intauth) { + push(@clean_records, $intauth); + } + } + return $link_count if !@clean_records; + + $link_count = $editor->json_query({ + "select" => { + "abl" => [ + { + "column" => "authority" + }, + { + "alias" => "bibs", + "transform" => "count", + "column" => "bib", + "aggregate" => 1 + } + ] + }, + "from" => "abl", + "where" => { "authority" => \@clean_records } + }); + + return $link_count; +} + 1; -- 2.11.0