]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/CDBI.pm
fixing regex and fts searching
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / CDBI.pm
1 package OpenILS::Application::Storage::CDBI;
2 use base qw/Class::DBI/;
3 use Class::DBI;
4
5 use OpenILS::Application::Storage::CDBI::actor;
6 use OpenILS::Application::Storage::CDBI::action;
7 use OpenILS::Application::Storage::CDBI::asset;
8 use OpenILS::Application::Storage::CDBI::biblio;
9 use OpenILS::Application::Storage::CDBI::config;
10 use OpenILS::Application::Storage::CDBI::metabib;
11 use OpenILS::Application::Storage::CDBI::money;
12 use OpenILS::Application::Storage::CDBI::permission;
13
14 use OpenSRF::Utils::Logger;
15 use OpenSRF::EX qw/:try/;
16
17 our $VERSION = undef;
18 my $log = 'OpenSRF::Utils::Logger';
19
20 sub child_init {
21         my $self = shift;
22
23         $log->debug("Creating ImaDBI Querys", DEBUG);
24         __PACKAGE__->set_sql( 'OILSFastSearch', <<"     SQL", 'Main');
25                 SELECT  %s
26                   FROM  %s
27                   WHERE %s = ?
28         SQL
29
30         __PACKAGE__->set_sql( 'OILSFastOrderedSearchLike', <<"  SQL", 'Main');
31                 SELECT  %s
32                   FROM  %s
33                   WHERE %s LIKE ?
34                   ORDER BY %s
35         SQL
36
37         __PACKAGE__->set_sql( 'OILSFastOrderedSearch', <<"      SQL", 'Main');
38                 SELECT  %s
39                   FROM  %s
40                   WHERE %s = ?
41                   ORDER BY %s
42         SQL
43
44         $log->debug("Calling Driver child_init", DEBUG);
45         $self->SUPER::child_init(@_);
46
47 }
48
49 sub fast_flesh_sth {
50         my $class = shift;
51         $class = ref($class) || $class;
52
53         my $field = shift;
54         my $value = shift;
55         my $order = shift;
56         my $like = shift;
57
58
59         if (!(defined($order) and ref($order) and ref($order) eq 'HASH')) {
60                 if (defined($value) and ref($value) and ref($value) eq 'HASH') {
61                         $order = $value;
62                         $value = undef;
63                 } else {
64                         $order = { order_by => $class->columns('Primary') }
65                 }
66         }
67
68         unless (defined $value) {
69                 $value = $field;
70                 $field = $class->primary_column;
71         }
72
73         unless (defined $field) {
74                 $field = $class->primary_column;
75         }
76
77         unless ($order->{order_by}) {
78                 $order = { order_by => $class->columns('Primary') }
79         }
80
81         my $fm_class = 'Fieldmapper::'.$class;
82         my $field_list = join ',', $class->columns('All');
83         
84         my $sth;
85         if (!$like) {
86                 $sth = $class->sql_OILSFastOrderedSearch( $field_list, $class->table, $field, $order->{order_by});
87         } else {
88                 $sth = $class->sql_OILSFastOrderedSearchLike( $field_list, $class->table, $field, $order->{order_by});
89         }
90         $sth->execute($value);
91         return $sth;
92 }
93
94 sub fast_flesh {
95         my $self = shift;
96         return map $class->construct($_), $self->fast_flesh_sth(@_)->fetchall_hash;
97 }
98
99 sub fast_fieldmapper {
100         my $self = shift;
101         my $id = shift;
102         my $col = shift;
103         my $like = shift;
104         my $class = ref($self) || $self;
105         my $fm_class = 'Fieldmapper::'.$class;
106         my @fms;
107         $log->debug("fast_fieldmapper() ==> Retrieving $fm_class", INTERNAL);
108         if ($like < 2) {
109                 for my $hash ($self->fast_flesh_sth( $col, "$id", { order_by => $col }, $like )->fetchall_hash) {
110                         my $fm = $fm_class->new;
111                         for my $field ( $fm_class->real_fields ) {
112                                 $fm->$field( $$hash{$field} );
113                         }
114                         push @fms, $fm;
115                 }
116         } else {
117                 my $search_type = 'search';
118                 if ($like == 2) {
119                         $search_type = 'search_fts'
120                 } elsif ($like == 3) {
121                         $search_type = 'search_regex'
122                 }
123
124                 for my $obj ($class->$search_type({ $col => $id})) {
125                         push @fms, $obj->to_fieldmapper;
126                 }
127         }
128         return @fms;
129 }
130
131 sub retrieve {
132         my $self = shift;
133         my $arg = shift;
134         if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
135                 $arg = $arg->id;
136         }
137         $log->debug("Retrieving $self with $arg", INTERNAL);
138         my $rec;
139         try {
140                 $rec = $self->SUPER::retrieve("$arg");
141         } catch Error with {
142                 $log->debug("Could not retrieve $self with $arg! -- ".shift(), DEBUG);
143                 return undef;
144         };
145         return $rec;
146 }
147
148 sub to_fieldmapper {
149         my $obj = shift;
150         my $class = ref($obj) || $obj;
151
152         my $fm_class = 'Fieldmapper::'.$class;
153         my $fm = $fm_class->new;
154
155         if (ref($obj)) {
156                 for my $field ( $fm->real_fields ) {
157                         $fm->$field( $obj->$field );
158                 }
159         }
160
161         return $fm;
162 }
163
164 sub create {
165         my $self = shift;
166         my $arg = shift;
167
168         $log->debug("\$arg is $arg (".ref($arg).")",DEBUG);
169
170         if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
171                 return $self->create_from_fieldmapper($arg,@_);
172         }
173
174         return $self->SUPER::create($arg,@_);
175 }
176
177 sub create_from_fieldmapper {
178         my $obj = shift;
179         my $fm = shift;
180         my @params = @_;
181
182         $log->debug("Creating node of type ".ref($fm), DEBUG);
183
184         my $class = ref($obj) || $obj;
185         my $primary = $class->primary_column;
186
187         if (ref $fm) {
188                 my %hash = map { defined $fm->$_ ?
189                                         ($_ => $fm->$_) :
190                                         ()
191                                 } grep { $_ ne $primary } $class->columns('All');
192
193                 if ($class->find_column( 'last_xact_id' )) {
194                         my $xact_id = $class->current_xact_id;
195                         throw Error unless ($xact_id);
196                         $hash{last_xact_id} = $xact_id;
197                 }
198
199                 return $class->create( \%hash, @params );
200         } else {
201                 return undef;
202         }
203 }
204
205 sub delete {
206         my $self = shift;
207         my $arg = shift;
208
209         my $class = ref($self) || $self;
210
211         if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
212                 $self = $self->retrieve($arg);
213                 unless (defined $self) {
214                         $log->debug("ARG! Couldn't retrieve record ".$arg->id, DEBUG);
215                         throw OpenSRF::EX::WARN ("ARG! Couldn't retrieve record ");
216                 }
217         }
218
219         if ($class->find_column( 'last_xact_id' )) {
220                 my $xact_id = $self->current_xact_id;
221                 throw Error unless ($xact_id);
222                 $self->last_xact_id( $class->current_xact_id );
223                 $self->SUPER::update;
224         }
225
226         $self->SUPER::delete;
227         return 1;
228 }
229
230 sub update {
231         my $self = shift;
232         my $arg = shift;
233
234         $log->debug("Attempting to update using $arg", DEBUG) if ($arg);
235
236         if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
237                 $self = $self->modify_from_fieldmapper($arg);
238                 $log->debug("Modification of $self seems to have failed....", DEBUG);
239                 return undef unless (defined $self);
240         }
241
242         $log->debug("Calling Class::DBI->update on modified object $self", DEBUG);
243         return $self->SUPER::update if ($self->is_changed);
244         return 0;
245 }
246
247 sub modify_from_fieldmapper {
248         my $obj = shift;
249         my $fm = shift;
250
251         $log->debug("Modifying object using fieldmapper", DEBUG);
252
253         my $class = ref($obj) || $obj;
254
255         if (!ref($obj)) {
256                 $obj = $class->retrieve($fm);
257                 unless ($obj) {
258                         $log->debug("Retrieve of $class using $fm (".$fm->id.") failed! -- ".shift(), ERROR);
259                         throw OpenSRF::EX::WARN ("No $class with id of ".$fm->id."!!");
260                 }
261         }
262
263         my %hash = map { defined $fm->$_ ?
264                                 ($_ => ''.$fm->$_) :
265                                 ()
266                         } $fm->real_fields;
267
268         my $au = $obj->autoupdate;
269         $obj->autoupdate(0);
270         
271         for my $field ( keys %hash ) {
272                 $obj->$field( $hash{$field} ) if ($obj->$field ne $hash{$field});
273                 $log->debug("Setting field $field on $obj to $hash{$field}",INTERNAL);
274         }
275
276         if ($class->find_column( 'last_xact_id' ) and $obj->is_changed) {
277                 my $xact_id = $obj->current_xact_id;
278                 throw Error unless ($xact_id);
279                 $obj->last_xact_id( $xact_id );
280         } else {
281                 $obj->autoupdate($au)
282         }
283
284         return $obj;
285 }
286
287
288
289         #-------------------------------------------------------------------------------
290         actor::user->has_a( home_ou => 'actor::org_unit' );
291         actor::user->has_a( card => 'actor::card' );
292         actor::user->has_a( standing => 'config::standing' );
293         actor::user->has_a( profile => 'actor::profile' );
294         actor::user->has_a( mailing_address => 'actor::user_address' );
295         actor::user->has_a( billing_address => 'actor::user_address' );
296         actor::user->has_a( ident_type => 'config::identification_type' );
297         actor::user->has_a( ident_type2 => 'config::identification_type' );
298         actor::user->has_a( net_access_level => 'config::net_access_level' );
299
300         actor::user_address->has_a( usr => 'actor::user' );
301         
302         actor::card->has_a( usr => 'actor::user' );
303         
304         config::rules::age_hold_protect->has_a( radius => 'actor::org_unit_type' );
305         
306         actor::org_unit->has_a( parent_ou => 'actor::org_unit' );
307         actor::org_unit->has_a( ou_type => 'actor::org_unit_type' );
308         #actor::org_unit->has_a( address => 'actor::org_address' );
309
310         actor::stat_cat_entry->has_a( stat_cat => 'actor::stat_cat' );
311         actor::stat_cat->has_many( entries => 'actor::stat_cat_entry' );
312         actor::stat_cat_entry_user_map->has_a( stat_cat => 'actor::stat_cat' );
313         actor::stat_cat_entry_user_map->has_a( stat_cat_entry => 'actor::stat_cat_entry' );
314         actor::stat_cat_entry_user_map->has_a( target_usr => 'actor::user' );
315
316         asset::stat_cat_entry->has_a( stat_cat => 'asset::stat_cat' );
317         asset::stat_cat->has_many( entries => 'asset::stat_cat_entry' );
318         asset::stat_cat_entry_copy_map->has_a( stat_cat => 'asset::stat_cat' );
319         asset::stat_cat_entry_copy_map->has_a( stat_cat_entry => 'asset::stat_cat_entry' );
320         asset::stat_cat_entry_copy_map->has_a( owning_copy => 'asset::copy' );
321
322         action::survey_response->has_a( usr => 'actor::user' );
323         action::survey_response->has_a( survey => 'action::survey' );
324         action::survey_response->has_a( question => 'action::survey_question' );
325         action::survey_response->has_a( answer => 'action::survey_answer' );
326
327         action::survey_question->has_a( survey => 'action::survey' );
328
329         action::survey_answer->has_a( question => 'action::survey' );
330
331         asset::copy_note->has_a( owning_copy => 'asset::copy' );
332
333         actor::user->has_many( stat_cat_entries => [ 'actor::stat_cat_entry_user_map' => 'stat_cat_entry' ] );
334         actor::user->has_many( stat_cat_entry_user_maps => 'actor::stat_cat_entry_user_map' );
335
336         asset::copy->has_many( stat_cat_entries => [ 'asset::stat_cat_entry_copy_map' => 'stat_cat_entry' ] );
337         asset::copy->has_many( stat_cat_entry_copy_maps => 'asset::stat_cat_entry_copy_map' );
338
339         asset::copy->has_a( call_number => 'asset::call_number' );
340         asset::copy->has_a( creator => 'actor::user' );
341         asset::copy->has_a( editor => 'actor::user' );
342         asset::copy->has_a( status => 'config::copy_status' );
343         asset::copy->has_a( location => 'asset::copy_location' );
344         asset::copy->has_a( circ_lib => 'actor::org_unit' );
345
346         asset::call_number_note->has_a( call_number => 'asset::call_number' );
347
348         asset::call_number->has_a( record => 'biblio::record_entry' );
349         asset::call_number->has_a( creator => 'actor::user' );
350         asset::call_number->has_a( editor => 'actor::user' );
351
352         biblio::record_note->has_a( record => 'biblio::record_entry' );
353         
354         biblio::record_entry->has_a( creator => 'actor::user' );
355         biblio::record_entry->has_a( editor => 'actor::user' );
356         
357         metabib::metarecord->has_a( master_record => 'biblio::record_entry' );
358         
359         metabib::record_descriptor->has_a( record => 'biblio::record_entry' );
360         
361         metabib::full_rec->has_a( record => 'biblio::record_entry' );
362         
363         metabib::title_field_entry->has_a( source => 'biblio::record_entry' );
364         metabib::title_field_entry->has_a( field => 'config::metabib_field' );
365         
366         metabib::author_field_entry->has_a( source => 'biblio::record_entry' );
367         metabib::author_field_entry->has_a( field => 'config::metabib_field' );
368         
369         metabib::subject_field_entry->has_a( source => 'biblio::record_entry' );
370         metabib::subject_field_entry->has_a( field => 'config::metabib_field' );
371         
372         metabib::keyword_field_entry->has_a( source => 'biblio::record_entry' );
373         metabib::keyword_field_entry->has_a( field => 'config::metabib_field' );
374         
375         metabib::series_field_entry->has_a( source => 'biblio::record_entry' );
376         metabib::series_field_entry->has_a( field => 'config::metabib_field' );
377         
378         metabib::metarecord_source_map->has_a( metarecord => 'metabib::metarecord' );
379         metabib::metarecord_source_map->has_a( source => 'biblio::record_entry' );
380
381         action::circulation->has_a( usr => 'actor::user' );
382         action::circulation->has_a( target_copy => 'asset::copy' );
383         action::circulation->has_a( circ_lib => 'actor::org_unit' );
384
385         money::billable_transaction->has_a( usr => 'actor::user' );
386         
387         
388         #-------------------------------------------------------------------------------
389         actor::user->has_many( survey_responses => 'action::survey_response' );
390         actor::user->has_many( addresses => 'actor::user_address' );
391         actor::user->has_many( cards => 'actor::card' );
392
393         actor::org_unit->has_many( users => 'actor::user' );
394         actor::profile->has_many( users => 'actor::user' );
395
396         action::survey->has_many( questions => 'action::survey_question' );
397         action::survey->has_many( responses => 'action::survey_response' );
398         
399         action::survey_question->has_many( answers => 'action::survey_answer' );
400         action::survey_question->has_many( responses => 'action::survey_response' );
401
402         action::survey_answer->has_many( responses => 'action::survey_response' );
403
404         asset::copy->has_many( notes => 'asset::copy_note' );
405         asset::call_number->has_many( copies => 'asset::copy' );
406         asset::call_number->has_many( notes => 'asset::call_number_note' );
407
408         biblio::record_entry->has_many( record_descriptor => 'metabib::record_descriptor' );
409         biblio::record_entry->has_many( notes => 'biblio::record_note' );
410         biblio::record_entry->has_many( call_numbers => 'asset::call_number' );
411         biblio::record_entry->has_many( full_record_entries => 'metabib::full_rec' );
412         biblio::record_entry->has_many( title_field_entries => 'metabib::title_field_entry' );
413         biblio::record_entry->has_many( author_field_entries => 'metabib::author_field_entry' );
414         biblio::record_entry->has_many( subject_field_entries => 'metabib::subject_field_entry' );
415         biblio::record_entry->has_many( keyword_field_entries => 'metabib::keyword_field_entry' );
416         biblio::record_entry->has_many( series_field_entries => 'metabib::series_field_entry' );
417
418         metabib::metarecord->has_many( source_records => [ 'metabib::metarecord_source_map' => 'source'] );
419
420         money::billable_transaction->has_many( billings => 'money::billing' );
421         money::billable_transaction->has_many( payments => 'money::payment' );
422
423         money::billing->has_a( xact => 'money::billable_transaction' );
424         money::payment->has_a( xact => 'money::billable_transaction' );
425
426         money::cash_payment->has_a( xact => 'money::billable_transaction' );
427         money::cash_payment->has_a( accepting_usr => 'actor::user' );
428
429         money::check_payment->has_a( xact => 'money::billable_transaction' );
430         money::check_payment->has_a( accepting_usr => 'actor::user' );
431
432         money::credit_card_payment->has_a( xact => 'money::billable_transaction' );
433         money::credit_card_payment->has_a( accepting_usr => 'actor::user' );
434
435         money::forgive_payment->has_a( xact => 'money::billable_transaction' );
436         money::forgive_payment->has_a( accepting_usr => 'actor::user' );
437
438         money::work_payment->has_a( xact => 'money::billable_transaction' );
439         money::work_payment->has_a( accepting_usr => 'actor::user' );
440
441         money::credit_payment->has_a( xact => 'money::billable_transaction' );
442         money::credit_payment->has_a( accepting_usr => 'actor::user' );
443
444         permission::grp_tree->has_a( parent => 'permission::grp_tree' );
445
446         permission::grp_perm_map->has_a( grp => 'permission::grp_tree' );
447         permission::grp_perm_map->has_a(  perm => 'permission::perm_list' );
448         permission::grp_perm_map->has_a(  depth => 'actor::org_unit_type' );
449         
450         permission::usr_perm_map->has_a( usr => 'actor::user' );
451         permission::usr_perm_map->has_a(  perm => 'permission::perm_list' );
452         permission::usr_perm_map->has_a(  depth => 'actor::org_unit_type' );
453         
454         permission::usr_grp_map->has_a(  usr => 'actor::user' );
455         permission::usr_grp_map->has_a(  grp => 'permission::grp_tree' );
456
457         action::hold_notification->has_a(  hold => 'action::hold_request' );
458         
459         action::hold_copy_map->has_a(  hold => 'action::hold_request' );
460         action::hold_copy_map->has_a(  target_copy => 'asset::copy' );
461
462         action::hold_request->has_a(  current_copy => 'asset::copy' );
463         action::hold_request->has_a(  requestor => 'actor::user' );
464         action::hold_request->has_a(  usr => 'actor::user' );
465         action::hold_request->has_a(  pickup_lib => 'actor::org_unit' );
466
467         action::hold_request->has_many(  notifications => 'action::hold_notification' );
468         action::hold_request->has_many(  copy_maps => 'action::hold_copy_map' );
469
470         asset::copy->has_many(  hold_maps => 'action::hold_copy_map' );
471
472 1;