[open-ils-commits] r17290 - trunk/Open-ILS/src/perlmods/OpenILS/Application/Cat (dbs)

svn at svn.open-ils.org svn at svn.open-ils.org
Fri Aug 20 14:38:39 EDT 2010


Author: dbs
Date: 2010-08-20 14:38:35 -0400 (Fri, 20 Aug 2010)
New Revision: 17290

Modified:
   trunk/Open-ILS/src/perlmods/OpenILS/Application/Cat/Authority.pm
Log:
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
  }
]


Modified: trunk/Open-ILS/src/perlmods/OpenILS/Application/Cat/Authority.pm
===================================================================
--- trunk/Open-ILS/src/perlmods/OpenILS/Application/Cat/Authority.pm	2010-08-20 18:33:25 UTC (rev 17289)
+++ trunk/Open-ILS/src/perlmods/OpenILS/Application/Cat/Authority.pm	2010-08-20 18:38:35 UTC (rev 17290)
@@ -98,4 +98,51 @@
     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;



More information about the open-ils-commits mailing list