]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/CStoreEditor.pm
go one deeper in the caller stack so we're not repeating
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Utils / CStoreEditor.pm
1 use strict; use warnings;
2 package OpenILS::Utils::CStoreEditor;
3 use OpenILS::Application::AppUtils;
4 use OpenSRF::AppSession;
5 use OpenSRF::EX qw(:try);
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Event;
8 use Data::Dumper;
9 use OpenSRF::Utils::JSON;
10 use OpenSRF::Utils::Logger qw($logger);
11 my $U = "OpenILS::Application::AppUtils";
12 my %PERMS;
13 my $cache;
14 my %xact_ed_cache;
15
16 our $always_xact = 0;
17 our $_loaded = 1;
18
19 #my %PERMS = (
20 #       'biblio.record_entry'   => { update => 'UPDATE_MARC' },
21 #       'asset.copy'                            => { update => 'UPDATE_COPY'},
22 #       'asset.call_number'             => { update => 'UPDATE_VOLUME'},
23 #       'action.circulation'            => { retrieve => 'VIEW_CIRCULATIONS'},
24 #);
25
26 sub flush_forced_xacts {
27     for my $k ( keys %xact_ed_cache ) {
28         $xact_ed_cache{$k}->rollback;
29         delete $xact_ed_cache{$k};
30     }
31 }
32
33 # -----------------------------------------------------------------------------
34 # Export some useful functions
35 # -----------------------------------------------------------------------------
36 use vars qw(@EXPORT_OK %EXPORT_TAGS);
37 use Exporter;
38 use base qw/Exporter/;
39 push @EXPORT_OK, ( 'new_editor', 'new_rstore_editor' );
40 %EXPORT_TAGS = ( funcs => [ qw/ new_editor new_rstore_editor / ] );
41
42 sub new_editor { return OpenILS::Utils::CStoreEditor->new(@_); }
43
44 sub new_rstore_editor { 
45         my $e = OpenILS::Utils::CStoreEditor->new(@_); 
46         $e->app('open-ils.reporter-store');
47         return $e;
48 }
49
50
51 # -----------------------------------------------------------------------------
52 # Log levels
53 # -----------------------------------------------------------------------------
54 use constant E => 'error';
55 use constant W => 'warn';
56 use constant I => 'info';
57 use constant D => 'debug';
58 use constant A => 'activity';
59
60
61
62 # -----------------------------------------------------------------------------
63 # Params include:
64 #       xact=><true> : creates a storage transaction
65 #       authtoken=>$token : the login session key
66 # -----------------------------------------------------------------------------
67 sub new {
68         my( $class, %params ) = @_;
69         $class = ref($class) || $class;
70         my $self = bless( \%params, $class );
71         $self->{checked_perms} = {};
72         return $self;
73 }
74
75
76 sub app {
77         my( $self, $app ) = @_;
78         $self->{app} = $app if $app;
79         $self->{app} = 'open-ils.cstore' unless $self->{app};
80         return $self->{app};
81 }
82
83
84 # -----------------------------------------------------------------------------
85 # Log the editor metadata along with the log string
86 # -----------------------------------------------------------------------------
87 sub log {
88         my( $self, $lev, $str ) = @_;
89         my $s = "editor[";
90     if ($always_xact) {
91         $s .= "!|";
92     } elsif ($self->{xact}) {
93         $s .= "1|";
94     } else {
95             $s .= "0|";
96     }
97         $s .= "0" unless $self->requestor;
98         $s .= $self->requestor->id if $self->requestor;
99         $s .= "]";
100         $logger->$lev("$s $str");
101 }
102
103 # -----------------------------------------------------------------------------
104 # Verifies the auth token and fetches the requestor object
105 # -----------------------------------------------------------------------------
106 sub checkauth {
107         my $self = shift;
108         $self->log(D, "checking auth token ".$self->authtoken);
109         my ($reqr, $evt) = $U->checkses($self->authtoken);
110         $self->event($evt) if $evt;
111         return $self->{requestor} = $reqr;
112 }
113
114
115 =head test
116 sub checkauth {
117         my $self = shift;
118         $cache = OpenSRF::Utils::Cache->new('global') unless $cache;
119         $self->log(D, "checking cached auth token ".$self->authtoken);
120         my $user = $cache->get_cache("oils_auth_".$self->authtoken);
121         return $self->{requestor} = $user->{userobj} if $user;
122         $self->event(OpenILS::Event->new('NO_SESSION'));
123         return undef;
124 }
125 =cut
126
127
128 # -----------------------------------------------------------------------------
129 # Returns the last generated event
130 # -----------------------------------------------------------------------------
131 sub event {
132         my( $self, $evt ) = @_;
133         $self->{event} = $evt if $evt;
134         return $self->{event};
135 }
136
137 # -----------------------------------------------------------------------------
138 # Destroys the transaction and disconnects where necessary,
139 # then returns the last event that occurred
140 # -----------------------------------------------------------------------------
141 sub die_event {
142         my $self = shift;
143         $self->rollback;
144         return $self->event;
145 }
146
147
148 # -----------------------------------------------------------------------------
149 # Clears the last caught event
150 # -----------------------------------------------------------------------------
151 sub clear_event {
152         my $self = shift;
153         $self->{event} = undef;
154 }
155
156 sub authtoken {
157         my( $self, $auth ) = @_;
158         $self->{authtoken} = $auth if $auth;
159         return $self->{authtoken};
160 }
161
162 sub timeout {
163     my($self, $to) = @_;
164     $self->{timeout} = $to if defined $to;
165     return defined($self->{timeout}) ? $self->{timeout} : 60;
166 }
167
168 # -----------------------------------------------------------------------------
169 # fetches the session, creating if necessary.  If 'xact' is true on this
170 # object, a db session is created
171 # -----------------------------------------------------------------------------
172 sub session {
173         my( $self, $session ) = @_;
174         $self->{session} = $session if $session;
175
176         if(!$self->{session}) {
177                 $self->{session} = OpenSRF::AppSession->create($self->app);
178
179                 if( ! $self->{session} ) {
180                         my $str = "Error creating cstore session with OpenSRF::AppSession->create()!";
181                         $self->log(E, $str);
182                         throw OpenSRF::EX::ERROR ($str);
183                 }
184
185                 $self->{session}->connect if $self->{xact} or $self->{connect} or $always_xact;
186                 $self->xact_start if $self->{xact} or $always_xact;
187         }
188
189     $xact_ed_cache{$self->{xact_id}} = $self if $always_xact;
190         return $self->{session};
191 }
192
193
194 # -----------------------------------------------------------------------------
195 # Starts a storage transaction
196 # -----------------------------------------------------------------------------
197 sub xact_start {
198         my $self = shift;
199         $self->log(D, "starting new db session");
200         my $stat = $self->request($self->app . '.transaction.begin') unless $self->{xact_id};
201         $self->log(E, "error starting database transaction") unless $stat;
202     $self->{xact_id} = $stat;
203         return $stat;
204 }
205
206 # -----------------------------------------------------------------------------
207 # Commits a storage transaction
208 # -----------------------------------------------------------------------------
209 sub xact_commit {
210         my $self = shift;
211         $self->log(D, "comitting db session");
212         my $stat = $self->request($self->app.'.transaction.commit') if $self->{xact_id};
213         $self->log(E, "error comitting database transaction") unless $stat;
214     delete $self->{xact_id};
215         return $stat;
216 }
217
218 # -----------------------------------------------------------------------------
219 # Rolls back a storage stransaction
220 # -----------------------------------------------------------------------------
221 sub xact_rollback {
222         my $self = shift;
223    return unless $self->{session};
224         $self->log(I, "rolling back db session");
225         my $stat = $self->request($self->app.".transaction.rollback") if $self->{xact_id};
226         $self->log(E, "error rolling back database transaction") unless $stat;
227     delete $self->{xact_id};
228         return $stat;
229 }
230
231
232
233 # -----------------------------------------------------------------------------
234 # Rolls back the transaction and disconnects
235 # -----------------------------------------------------------------------------
236 sub rollback {
237         my $self = shift;
238         $self->xact_rollback;
239    delete $self->{xact};
240         $self->disconnect;
241 }
242
243 sub disconnect {
244         my $self = shift;
245         $self->session->disconnect if $self->{session};
246    delete $self->{session};
247 }
248
249
250 # -----------------------------------------------------------------------------
251 # commits the db session and destroys the session
252 # -----------------------------------------------------------------------------
253 sub commit {
254         my $self = shift;
255         return unless $self->{xact_id};
256         $self->xact_commit;
257         $self->session->disconnect;
258         $self->{session} = undef;
259 }
260
261 # -----------------------------------------------------------------------------
262 # clears all object data. Does not commit the db transaction.
263 # -----------------------------------------------------------------------------
264 sub reset {
265         my $self = shift;
266         $self->disconnect;
267         $$self{$_} = undef for (keys %$self);
268 }
269
270
271 # -----------------------------------------------------------------------------
272 # commits and resets
273 # -----------------------------------------------------------------------------
274 sub finish {
275         my $self = shift;
276         $self->commit;
277         $self->reset;
278 }
279
280
281
282 # -----------------------------------------------------------------------------
283 # Does a simple storage request
284 # -----------------------------------------------------------------------------
285 sub request {
286         my( $self, $method, @params ) = @_;
287
288     my $val;
289         my $err;
290         my $argstr = __arg_to_string( (scalar(@params)) == 1 ? $params[0] : \@params);
291         my $locale = $self->session->session_locale;
292
293         $self->log(I, "request $locale $method $argstr");
294
295         if( ($self->{xact} or $always_xact) and 
296                         $self->session->state != OpenSRF::AppSession::CONNECTED() ) {
297                 #$logger->error("CStoreEditor lost it's connection!!");
298                 throw OpenSRF::EX::ERROR ("CStore connection timed out - transaction cannot continue");
299         }
300
301
302         try {
303
304         my $req = $self->session->request($method, @params);
305
306         if($self->substream) {
307             $self->log(D,"running in substream mode");
308             $val = [];
309             while( my $resp = $req->recv(timeout => $self->timeout) ) {
310                 push(@$val, $resp->content) if $resp->content;
311             }
312
313         } else {
314             my $resp = $req->recv(timeout => $self->timeout);
315             $val = $resp->content;
316         }
317
318         $req->finish;
319
320         } catch Error with {
321                 $err = shift;
322                 $self->log(E, "request error $method : $argstr : $err");
323         };
324
325         throw $err if $err;
326         return $val;
327 }
328
329 sub substream {
330    my( $self, $bool ) = @_;
331    $self->{substream} = $bool if defined $bool;
332    return $self->{substream};
333 }
334
335
336 # -----------------------------------------------------------------------------
337 # Sets / Returns the requestor object.  This is set when checkauth succeeds.
338 # -----------------------------------------------------------------------------
339 sub requestor {
340         my($self, $requestor) = @_;
341         $self->{requestor} = $requestor if $requestor;
342         return $self->{requestor};
343 }
344
345
346
347 # -----------------------------------------------------------------------------
348 # Holds the last data received from a storage call
349 # -----------------------------------------------------------------------------
350 sub data {
351         my( $self, $data ) = @_;
352         $self->{data} = $data if defined $data;
353         return $self->{data};
354 }
355
356
357 # -----------------------------------------------------------------------------
358 # True if this perm has already been checked at this org
359 # -----------------------------------------------------------------------------
360 sub perm_checked {
361         my( $self, $perm, $org ) = @_;
362         $self->{checked_perms}->{$org} = {}
363                 unless $self->{checked_perms}->{$org};
364         my $checked = $self->{checked_perms}->{$org}->{$perm};
365         if(!$checked) {
366                 $self->{checked_perms}->{$org}->{$perm} = 1;
367                 return 0;
368         }
369         return 1;
370 }
371
372
373
374 # -----------------------------------------------------------------------------
375 # Returns true if the requested perm is allowed.  If the perm check fails,
376 # $e->event is set and undef is returned
377 # The perm user is $e->requestor->id and perm org defaults to the requestor's
378 # ws_ou
379 # if perm is an array of perms, method will return true at the first allowed
380 # permission.  If none of the perms are allowed, the perm_failure event
381 # is created with the last perm to fail
382 # -----------------------------------------------------------------------------
383 my $PERM_QUERY = {
384     select => {
385         au => [ {
386             transform => 'permission.usr_has_perm',
387             alias => 'has_perm',
388             column => 'id',
389             params => []
390         } ]
391     },
392     from => 'au',
393     where => {},
394 };
395
396 my $OBJECT_PERM_QUERY = {
397     select => {
398         au => [ {
399             transform => 'permission.usr_has_object_perm',
400             alias => 'has_perm',
401             column => 'id',
402             params => []
403         } ]
404     },
405     from => 'au',
406     where => {},
407 };
408
409 sub allowed {
410         my( $self, $perm, $org, $object, $hint ) = @_;
411         my $uid = $self->requestor->id;
412         $org ||= $self->requestor->ws_ou;
413
414     my $perms = (ref($perm) eq 'ARRAY') ? $perm : [$perm];
415
416     for $perm (@$perms) {
417             $self->log(I, "checking perms user=$uid, org=$org, perm=$perm");
418     
419         if($object) {
420             my $params;
421             if(ref $object) {
422                 # determine the ID field and json_hint from the object
423                 my $id_field = $object->Identity;
424                 $params = [$perm, $object->json_hint, $object->$id_field];
425             } else {
426                 # we were passed an object-id and json_hint
427                 $params = [$perm, $hint, $object];
428             }
429             push(@$params, $org) if $org;
430             $OBJECT_PERM_QUERY->{select}->{au}->[0]->{params} = $params;
431             $OBJECT_PERM_QUERY->{where}->{id} = $uid;
432             return 1 if $U->is_true($self->json_query($OBJECT_PERM_QUERY)->[0]->{has_perm});
433
434         } else {
435             $PERM_QUERY->{select}->{au}->[0]->{params} = [$perm, $org];
436             $PERM_QUERY->{where}->{id} = $uid;
437             return 1 if $U->is_true($self->json_query($PERM_QUERY)->[0]->{has_perm});
438         }
439     }
440
441     # set the perm failure event if the permission check returned false
442         my $e = OpenILS::Event->new('PERM_FAILURE', ilsperm => $perm, ilspermloc => $org);
443         $self->event($e);
444         return undef;
445 }
446
447
448 # -----------------------------------------------------------------------------
449 # Returns the list of object IDs this user has object-specific permissions for
450 # -----------------------------------------------------------------------------
451 sub objects_allowed {
452     my($self, $perm, $obj_type) = @_;
453
454     my $perms = (ref($perm) eq 'ARRAY') ? $perm : [$perm];
455     my @ids;
456
457     for $perm (@$perms) {
458         my $query = {
459             select => {puopm => ['object_id']},
460             from => {
461                 puopm => {
462                     ppl => {field => 'id',fkey => 'perm'}
463                 }
464             },
465             where => {
466                 '+puopm' => {usr => $self->requestor->id, object_type => $obj_type},
467                 '+ppl' => {code => $perm}
468             }
469         };
470     
471         my $list = $self->json_query($query);
472         push(@ids, 0+$_->{object_id}) for @$list;
473     }
474
475    my %trim;
476    $trim{$_} = 1 for @ids;
477    return [ keys %trim ];
478 }
479
480
481 # -----------------------------------------------------------------------------
482 # checks the appropriate perm for the operation
483 # -----------------------------------------------------------------------------
484 sub _checkperm {
485         my( $self, $ptype, $action, $org ) = @_;
486         $org ||= $self->requestor->ws_ou;
487         my $perm = $PERMS{$ptype}{$action};
488         if( $perm ) {
489                 return undef if $self->perm_checked($perm, $org);
490                 return $self->event unless $self->allowed($perm, $org);
491         } else {
492                 $self->log(I, "no perm provided for $ptype.$action");
493         }
494         return undef;
495 }
496
497
498
499 # -----------------------------------------------------------------------------
500 # Logs update actions to the activity log
501 # -----------------------------------------------------------------------------
502 sub log_activity {
503         my( $self, $type, $action, $arg ) = @_;
504         my $str = "$type.$action";
505         $str .= _prop_string($arg);
506         $self->log(A, $str);
507 }
508
509
510
511 sub _prop_string {
512         my $obj = shift;
513         my @props = $obj->properties;
514         my $str = "";
515         for(@props) {
516                 my $prop = $obj->$_() || "";
517                 $prop = substr($prop, 0, 128) . "..." if length $prop > 131;
518                 $str .= " $_=$prop";
519         }
520         return $str;
521 }
522
523
524 sub __arg_to_string {
525         my $arg = shift;
526         return "" unless defined $arg;
527         if( UNIVERSAL::isa($arg, "Fieldmapper") ) {
528                 return (defined $arg->id) ? $arg->id : '<new object>';
529         }
530         return OpenSRF::Utils::JSON->perl2JSON($arg);
531         return "";
532 }
533
534
535 # -----------------------------------------------------------------------------
536 # This does the actual storage query.
537 #
538 # 'search' calls become search_where calls and $arg can be a search hash or
539 # an array-ref of storage search options.  
540 #
541 # 'retrieve' expects an id
542 # 'update' expects an object
543 # 'create' expects an object
544 # 'delete' expects an object
545 #
546 # All methods return true on success and undef on failure.  On failure, 
547 # $e->event is set to the generated event.  
548 # Note: this method assumes that updating a non-changed object and 
549 # thereby receiving a 0 from storage, is a successful update.  
550 #
551 # The method will therefore return true so the caller can just do 
552 # $e->update_blah($x) or return $e->event;
553 # The true value returned from storage for all methods will be stored in 
554 # $e->data, until the next method is called.
555 #
556 # not-found events are generated on retrieve and serach methods.
557 # action=search methods will return [] (==true) if no data is found.  If the
558 # caller is interested in the not found event, they can do:  
559 # return $e->event unless @$results; 
560 # -----------------------------------------------------------------------------
561 sub runmethod {
562         my( $self, $action, $type, $arg, $options ) = @_;
563
564    $options ||= {};
565
566         if( $action eq 'retrieve' ) {
567                 if(! defined($arg) ) {
568                         $self->log(W,"$action $type called with no ID...");
569                         $self->event(_mk_not_found($type, $arg));
570                         return undef;
571                 } elsif( ref($arg) =~ /Fieldmapper/ ) {
572                         $self->log(E,"$action $type called with an object.. attempting ID retrieval..");
573                         $arg = $arg->id;
574                 }
575         }
576
577         my @arg = ( ref($arg) eq 'ARRAY' ) ? @$arg : ($arg);
578         my $method = $self->app.".direct.$type.$action";
579
580         if( $action eq 'search' ) {
581                 $method .= '.atomic';
582
583         } elsif( $action eq 'batch_retrieve' ) {
584                 $action = 'search';
585                 @arg = ( { id => $arg } );
586                 $method =~ s/batch_retrieve/search/o;
587                 $method .= '.atomic';
588
589         } elsif( $action eq 'retrieve_all' ) {
590                 $action = 'search';
591                 $method =~ s/retrieve_all/search/o;
592                 my $tt = $type;
593                 $tt =~ s/\./::/og;
594                 my $fmobj = "Fieldmapper::$tt";
595                 @arg = ( { $fmobj->Identity => { '!=' => undef } } );
596                 $method .= '.atomic';
597         }
598
599         $method =~ s/search/id_list/o if $options->{idlist};
600
601     $method =~ s/\.atomic$//o if $self->substream($$options{substream} || 0);
602     $self->timeout($$options{timeout});
603
604         # remove any stale events
605         $self->clear_event;
606
607         if( $action eq 'update' or $action eq 'delete' or $action eq 'create' ) {
608                 if(!($self->{xact} or $always_xact)) {
609                         $logger->error("Attempt to update DB while not in a transaction : $method");
610                         throw OpenSRF::EX::ERROR ("Attempt to update DB while not in a transaction : $method");
611                 }
612                 $self->log_activity($type, $action, $arg);
613         }
614
615         if($$options{checkperm}) {
616                 my $a = ($action eq 'search') ? 'retrieve' : $action;
617                 my $e = $self->_checkperm($type, $a, $$options{permorg});
618                 if($e) {
619                         $self->event($e);
620                         return undef;
621                 }
622         }
623
624         my $obj; 
625         my $err;
626
627         try {
628                 $obj = $self->request($method, @arg);
629         } catch Error with { $err = shift; };
630         
631
632         if(!defined $obj) {
633                 $self->log(I, "request returned no data : $method");
634
635                 if( $action eq 'retrieve' ) {
636                         $self->event(_mk_not_found($type, $arg));
637
638                 } elsif( $action eq 'update' or 
639                                 $action eq 'delete' or $action eq 'create' ) {
640                         my $evt = OpenILS::Event->new(
641                                 'DATABASE_UPDATE_FAILED', payload => $arg, debug => "$err" );
642                         $self->event($evt);
643                 }
644
645                 if( $err ) {
646                         $self->event( 
647                                 OpenILS::Event->new( 'DATABASE_QUERY_FAILED', 
648                                         payload => $arg, debug => "$err" ));
649                         return undef;
650                 }
651
652                 return undef;
653         }
654
655         if( $action eq 'create' and $obj == 0 ) {
656                 my $evt = OpenILS::Event->new(
657                         'DATABASE_UPDATE_FAILED', payload => $arg, debug => "$err" );
658                 $self->event($evt);
659                 return undef;
660         }
661
662         # If we havn't dealt with the error in a nice way, go ahead and throw it
663         if( $err ) {
664                 $self->event( 
665                         OpenILS::Event->new( 'DATABASE_QUERY_FAILED', 
666                                 payload => $arg, debug => "$err" ));
667                 return undef;
668         }
669
670         if( $action eq 'search' ) {
671                 $self->log(I, "$type.$action : returned ".scalar(@$obj). " result(s)");
672                 $self->event(_mk_not_found($type, $arg)) unless @$obj;
673         }
674
675         if( $action eq 'create' ) {
676                 $self->log(I, "created a new $type object with ID " . $obj->id);
677                 $arg->id($obj->id);
678         }
679
680         $self->data($obj); # cache the data for convenience
681
682         return ($obj) ? $obj : 1;
683 }
684
685
686 sub _mk_not_found {
687         my( $type, $arg ) = @_;
688         (my $t = $type) =~ s/\./_/og;
689         $t = uc($t);
690         return OpenILS::Event->new("${t}_NOT_FOUND", payload => $arg);
691 }
692
693
694
695 # utility method for loading
696 sub __fm2meth { 
697         my $str = shift;
698         my $sep = shift;
699         $str =~ s/Fieldmapper:://o;
700         $str =~ s/::/$sep/g;
701         return $str;
702 }
703
704
705 # -------------------------------------------------------------
706 # Load up the methods from the FM classes
707 # -------------------------------------------------------------
708 my $map = $Fieldmapper::fieldmap;
709 for my $object (keys %$map) {
710         my $obj = __fm2meth($object,'_');
711         my $type = __fm2meth($object, '.');
712
713         my $update = "update_$obj";
714         my $updatef = 
715                 "sub $update {return shift()->runmethod('update', '$type', \@_);}";
716         eval $updatef;
717
718         my $retrieve = "retrieve_$obj";
719         my $retrievef = 
720                 "sub $retrieve {return shift()->runmethod('retrieve', '$type', \@_);}";
721         eval $retrievef;
722
723         my $search = "search_$obj";
724         my $searchf = 
725                 "sub $search {return shift()->runmethod('search', '$type', \@_);}";
726         eval $searchf;
727
728         my $create = "create_$obj";
729         my $createf = 
730                 "sub $create {return shift()->runmethod('create', '$type', \@_);}";
731         eval $createf;
732
733         my $delete = "delete_$obj";
734         my $deletef = 
735                 "sub $delete {return shift()->runmethod('delete', '$type', \@_);}";
736         eval $deletef;
737
738         my $bretrieve = "batch_retrieve_$obj";
739         my $bretrievef = 
740                 "sub $bretrieve {return shift()->runmethod('batch_retrieve', '$type', \@_);}";
741         eval $bretrievef;
742
743         my $retrieveall = "retrieve_all_$obj";
744         my $retrieveallf = 
745                 "sub $retrieveall {return shift()->runmethod('retrieve_all', '$type', \@_);}";
746         eval $retrieveallf;
747 }
748
749 sub json_query {
750     my( $self, $arg, $options ) = @_;
751     $options ||= {};
752         my @arg = ( ref($arg) eq 'ARRAY' ) ? @$arg : ($arg);
753     my $method = $self->app.'.json_query.atomic';
754     $method =~ s/\.atomic$//o if $self->substream($$options{substream} || 0);
755         $self->clear_event;
756     my $obj;
757     my $err;
758     
759     try {
760         $obj = $self->request($method, @arg);
761     } catch Error with { $err = shift; };
762
763     if( $err ) {
764         $self->event(
765             OpenILS::Event->new( 'DATABASE_QUERY_FAILED',
766             payload => $arg, debug => "$err" ));
767         return undef;
768     }
769
770     $self->log(I, "json_query : returned ".scalar(@$obj). " result(s)") if (ref($obj));
771     return $obj;
772 }
773
774
775
776 1;
777
778