From 81f19f31af193434388343bb355e91988dbb1fd0 Mon Sep 17 00:00:00 2001 From: miker Date: Wed, 28 Jun 2006 06:28:49 +0000 Subject: [PATCH] add "random" interface git-svn-id: svn://svn.open-ils.org/ILS/trunk@4809 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../OpenILS/Application/Storage/Publisher.pm | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm b/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm index 849fa4bddf..fb685d69b2 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm @@ -122,6 +122,29 @@ sub cachable_wrapper { return undef; } +sub random_object { + my $self = shift; + my $client = shift; + + my $cdbi = $self->{cdbi}; + my $table = $cdbi->table; + my $sql = <<" SQL"; + SELECT id + FROM $table + WHERE id IN (( SELECT (RANDOM() * (SELECT MAX(id) FROM $table))::INT LIMIT 1 )); + SQL + + my $trys = 100; + while ($trys--) { + + my $id = $cdbi->db_Main->selectcol_arrayref($sql); + next unless (@$id); + + return ($cdbi->fast_fieldmapper(@$id))[0]; + } + return undef; +} + sub retrieve_node { my $self = shift; my $client = shift; @@ -498,6 +521,17 @@ for my $fmclass ( (Fieldmapper->classes) ) { =cut + # Create the random method + unless ( __PACKAGE__->is_registered( $api_prefix.'.random' ) ) { + __PACKAGE__->register_method( + api_name => $api_prefix.'.random', + method => 'random_object', + api_level => 1, + cdbi => $cdbi, + argc => 0, + ); + } + # Create the retrieve method unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) { __PACKAGE__->register_method( -- 2.11.0