1 package OpenILS::Application::Storage::CDBI;
2 use base qw/Class::DBI/;
4 use Class::DBI::AbstractSearch;
6 use OpenILS::Application::Storage::CDBI::actor;
7 use OpenILS::Application::Storage::CDBI::action;
8 use OpenILS::Application::Storage::CDBI::asset;
9 use OpenILS::Application::Storage::CDBI::authority;
10 use OpenILS::Application::Storage::CDBI::biblio;
11 use OpenILS::Application::Storage::CDBI::config;
12 use OpenILS::Application::Storage::CDBI::metabib;
13 use OpenILS::Application::Storage::CDBI::money;
14 use OpenILS::Application::Storage::CDBI::permission;
17 use OpenSRF::Utils::Logger;
18 use OpenSRF::EX qw/:try/;
21 my $log = 'OpenSRF::Utils::Logger';
26 $log->debug("Creating ImaDBI Querys", DEBUG);
27 __PACKAGE__->set_sql( 'OILSFastSearch', <<" SQL", 'Main');
33 __PACKAGE__->set_sql( 'OILSFastOrderedSearchLike', <<" SQL", 'Main');
40 __PACKAGE__->set_sql( 'OILSFastOrderedSearch', <<" SQL", 'Main');
47 $log->debug("Calling Driver child_init", DEBUG);
48 $self->SUPER::child_init(@_);
54 $class = ref($class) || $class;
62 if (!(defined($order) and ref($order) and ref($order) eq 'HASH')) {
63 if (defined($value) and ref($value) and ref($value) eq 'HASH') {
67 $order = { order_by => $class->columns('Primary') }
71 unless (defined $value) {
73 ($field) = $class->columns('Primary');
76 unless (defined $field) {
77 ($field) = $class->columns('Primary');
80 unless ($order->{order_by}) {
81 $order = { order_by => $class->columns('Primary') }
84 my $fm_class = 'Fieldmapper::'.$class;
85 my $field_list = join ',', $class->columns('All');
89 $sth = $class->sql_OILSFastOrderedSearch( $field_list, $class->table, $field, $order->{order_by});
91 $sth = $class->sql_OILSFastOrderedSearchLike( $field_list, $class->table, $field, $order->{order_by});
93 $sth->execute($value);
99 return map $class->construct($_), $self->fast_flesh_sth(@_)->fetchall_hash;
102 sub fast_fieldmapper {
108 my $class = ref($self) || $self;
109 my $fm_class = 'Fieldmapper::'.$class;
111 $log->debug("fast_fieldmapper() ==> Retrieving $fm_class", INTERNAL);
113 for my $hash ($self->fast_flesh_sth( $col, "$id", { order_by => $col }, $like )->fetchall_hash) {
114 my $fm = $fm_class->new;
115 for my $field ( $fm_class->real_fields ) {
116 $fm->$field( $$hash{$field} );
121 my $search_type = 'search';
123 $search_type = 'search_fts'
124 } elsif ($like == 3) {
125 $search_type = 'search_regex'
128 for my $obj ($class->$search_type({ $col => $id}, $options)) {
129 push @fms, $obj->to_fieldmapper;
139 (UNIVERSAL::isa($arg => 'Fieldmapper') ||
140 UNIVERSAL::isa($arg => 'Class::DBI')) ) {
141 my ($col) = $self->primary_column;
142 $log->debug("Using field $col as the primary key", INTERNAL);
145 my ($col) = $self->primary_column;
146 $log->debug("Using field $col as the primary key", INTERNAL);
150 $log->debug("Retrieving $self with $arg", INTERNAL);
153 $rec = $self->SUPER::retrieve("$arg");
155 $log->debug("Could not retrieve $self with $arg! -- ".shift(), DEBUG);
163 my $class = ref($obj) || $obj;
165 my $fm_class = 'Fieldmapper::'.$class;
166 my $fm = $fm_class->new;
169 for my $field ( $fm->real_fields ) {
170 $fm->$field( $obj->$field );
182 delete $$arg{$_} for (keys %$search);
184 $log->debug("CDBI->merge: \$search is $search (".ref($search)." : ".join(',',map{"$_ => $$search{$_}"}keys(%$search)).")",DEBUG);
185 $log->debug("CDBI->merge: \$arg is $arg (".ref($arg)." : ".join(',',map{"$_ => $$arg{$_}"}keys(%$arg)).")",DEBUG);
188 @objs = $self->search_where($search) unless (ref $self);
191 return $objs[0]->update($arg);
192 } elsif (@objs == 0) {
193 return $self->create({%$search,%$arg});
195 throw OpenSRF::EX::WARN ("Non-unique search key for merge. Perhaps you meant to use remote_update?");
204 delete $$arg{$_} for (keys %$search);
206 $log->debug("CDBI->remote_update: \$search is $search (".ref($search)." : ".join(',',map{"$_ => $$search{$_}"}keys(%$search)).")",DEBUG);
207 $log->debug("CDBI->remote_update: \$arg is $arg (".ref($arg)." : ".join(',',map{"$_ => $$arg{$_}"}keys(%$arg)).")",DEBUG);
209 # my @objs = $self->search_where($search);
210 # throw OpenSRF::EX::WARN ("No objects found for remote_update. Perhaps you meant to use merge?")
213 # $_->update($arg) for (@objs);
214 # return scalar(@objs);
216 my @finds = sort keys %$search;
217 my @sets = sort keys %$arg;
219 my @find_vals = @$search{@finds};
220 my @set_vals = @$arg{@sets};
222 my $sql = 'UPDATE %s SET %s WHERE %s';
224 my $table = $self->table;
225 my $set = join(', ', map { "$_=?" } @sets);
226 my $where = join(', ', map { "$_=?" } @finds);
228 my $sth = $self->db_Main->prepare(sprintf($sql, $table, $set, $where));
229 $sth->execute(@set_vals,@find_vals);
238 $log->debug("CDBI->create: \$arg is $arg (".ref($arg)." : ".JSON->perl2JSON($arg).")",DEBUG);
240 if (ref($arg) && UNIVERSAL::isa($arg => 'Fieldmapper')) {
241 return $self->create_from_fieldmapper($arg,@_);
244 return $self->SUPER::create($arg,@_);
247 sub create_from_fieldmapper {
252 $log->debug("Creating node of type ".ref($fm), DEBUG);
254 my $class = ref($obj) || $obj;
255 my ($primary) = $class->columns('Primary');
257 if (ref($fm) &&UNIVERSAL::isa($fm => 'Fieldmapper')) {
258 my %hash = map { defined $fm->$_ ?
261 } grep { $_ ne $primary } $class->columns('All');
263 if ($class->find_column( 'last_xact_id' )) {
264 my $xact_id = $class->current_xact_id;
265 throw Error unless ($xact_id);
266 $hash{last_xact_id} = $xact_id;
269 return $class->create( \%hash, @params );
280 my $class = ref($self) || $self;
282 $self = $self->retrieve($arg) if (!ref($self));
283 unless (defined $self) {
284 $log->debug("ARG! Couldn't retrieve record ".$arg->id, DEBUG);
285 throw OpenSRF::EX::WARN ("ARG! Couldn't retrieve record ");
288 if ($class->find_column( 'last_xact_id' )) {
289 my $xact_id = $self->current_xact_id;
291 throw Error ("Deleting from $class requires a transaction be established")
294 throw Error ("The row you are attempting to delete has been changed since you read it")
295 unless ( $orig->last_xact_id eq $self->last_xact_id);
297 $self->last_xact_id( $class->current_xact_id );
298 $self->SUPER::update;
301 $self->SUPER::delete;
310 $string .= "Object type:\t".ref($obj)."\n";
311 $string .= "Object string:\t$obj\n";
313 if (ref($obj) && ref($obj) =~ /Fieldmapper/o ) {
314 $string .= "Object fields:\n";
315 for my $col ($obj->real_fields()) {
316 $string .= "\t$col\t=>".$obj->$col."\n";
318 } elsif (ref($obj)) {
319 $string .= "Object cols:\n";
320 for my $col ($obj->columns('All')) {
321 $string .= "\t$col\t=>".$obj->$col."\n";
327 $log->debug($string,DEBUG);
335 $log->debug("Attempting to update using $arg", DEBUG) if ($arg);
338 $self = $self->modify_from_fieldmapper($arg);
339 $log->debug("Modification of $self seems to have failed....", DEBUG);
340 return undef unless (defined $self);
343 $log->debug("Calling Class::DBI->update on modified object $self", DEBUG);
347 return $self->SUPER::update if ($self->is_changed);
351 sub modify_from_fieldmapper {
359 $log->debug("Modifying object using fieldmapper", DEBUG);
361 my $class = ref($obj) || $obj;
362 my ($primary) = $class->columns('Primary');
366 $obj = $class->retrieve($fm);
369 $log->debug("Retrieve of $class using $fm (".$fm->id.") failed! -- ".shift(), ERROR);
370 throw OpenSRF::EX::WARN ("No $class with id of ".$fm->id."!!");
376 if (ref($fm) and UNIVERSAL::isa($fm => 'Fieldmapper')) {
377 %hash = map { defined $fm->$_ ?
380 } grep { $_ ne $primary } $class->columns('All');
385 my $au = $obj->autoupdate;
390 for my $field ( keys %hash ) {
391 $obj->$field( $hash{$field} ) if ($obj->$field ne $hash{$field});
392 $log->debug("Setting field $field on $obj to $hash{$field}",INTERNAL);
395 if ($class->find_column( 'last_xact_id' ) and $obj->is_changed) {
396 my ($xact_id) = OpenILS::Application::Storage->method_lookup('open-ils.storage.transaction.current')->run();
397 throw Error ("Updating $class requires a transaction be established")
399 throw Error ("The row you are attempting to delete has been changed since you read it")
400 unless ( $fm->last_xact_id eq $obj->last_xact_id);
401 $obj->last_xact_id( $xact_id );
403 $obj->autoupdate($au)
411 #-------------------------------------------------------------------------------
412 actor::user->has_a( home_ou => 'actor::org_unit' );
413 actor::user->has_a( card => 'actor::card' );
414 actor::user->has_a( standing => 'config::standing' );
415 actor::user->has_a( profile => 'actor::profile' );
416 actor::user->has_a( mailing_address => 'actor::user_address' );
417 actor::user->has_a( billing_address => 'actor::user_address' );
418 actor::user->has_a( ident_type => 'config::identification_type' );
419 actor::user->has_a( ident_type2 => 'config::identification_type' );
420 actor::user->has_a( net_access_level => 'config::net_access_level' );
422 actor::user_address->has_a( usr => 'actor::user' );
424 actor::card->has_a( usr => 'actor::user' );
426 actor::org_unit->has_a( parent_ou => 'actor::org_unit' );
427 actor::org_unit->has_a( ou_type => 'actor::org_unit_type' );
428 #actor::org_unit->has_a( address => 'actor::org_address' );
430 actor::stat_cat_entry->has_a( stat_cat => 'actor::stat_cat' );
431 actor::stat_cat->has_many( entries => 'actor::stat_cat_entry' );
432 actor::stat_cat_entry_user_map->has_a( stat_cat => 'actor::stat_cat' );
433 actor::stat_cat_entry_user_map->has_a( stat_cat_entry => 'actor::stat_cat_entry' );
434 actor::stat_cat_entry_user_map->has_a( target_usr => 'actor::user' );
436 asset::stat_cat_entry->has_a( stat_cat => 'asset::stat_cat' );
437 asset::stat_cat->has_many( entries => 'asset::stat_cat_entry' );
438 asset::stat_cat_entry_copy_map->has_a( stat_cat => 'asset::stat_cat' );
439 asset::stat_cat_entry_copy_map->has_a( stat_cat_entry => 'asset::stat_cat_entry' );
440 asset::stat_cat_entry_copy_map->has_a( owning_copy => 'asset::copy' );
442 action::survey_response->has_a( usr => 'actor::user' );
443 action::survey_response->has_a( survey => 'action::survey' );
444 action::survey_response->has_a( question => 'action::survey_question' );
445 action::survey_response->has_a( answer => 'action::survey_answer' );
447 action::survey_question->has_a( survey => 'action::survey' );
449 action::survey_answer->has_a( question => 'action::survey' );
451 asset::copy_note->has_a( owning_copy => 'asset::copy' );
453 actor::user->has_many( stat_cat_entries => [ 'actor::stat_cat_entry_user_map' => 'stat_cat_entry' ] );
454 actor::user->has_many( stat_cat_entry_user_maps => 'actor::stat_cat_entry_user_map' );
456 asset::copy->has_many( stat_cat_entries => [ 'asset::stat_cat_entry_copy_map' => 'stat_cat_entry' ] );
457 asset::copy->has_many( stat_cat_entry_copy_maps => 'asset::stat_cat_entry_copy_map' );
459 asset::copy->has_a( call_number => 'asset::call_number' );
460 asset::copy->has_a( creator => 'actor::user' );
461 asset::copy->has_a( editor => 'actor::user' );
462 asset::copy->has_a( status => 'config::copy_status' );
463 asset::copy->has_a( location => 'asset::copy_location' );
464 asset::copy->has_a( circ_lib => 'actor::org_unit' );
466 asset::call_number_note->has_a( call_number => 'asset::call_number' );
468 asset::call_number->has_a( record => 'biblio::record_entry' );
469 asset::call_number->has_a( creator => 'actor::user' );
470 asset::call_number->has_a( editor => 'actor::user' );
472 authority::record_note->has_a( record => 'authority::record_entry' );
473 biblio::record_note->has_a( record => 'biblio::record_entry' );
475 authority::record_entry->has_a( creator => 'actor::user' );
476 authority::record_entry->has_a( editor => 'actor::user' );
477 biblio::record_entry->has_a( creator => 'actor::user' );
478 biblio::record_entry->has_a( editor => 'actor::user' );
480 metabib::metarecord->has_a( master_record => 'biblio::record_entry' );
482 authority::record_descriptor->has_a( record => 'authority::record_entry' );
483 metabib::record_descriptor->has_a( record => 'biblio::record_entry' );
485 authority::full_rec->has_a( record => 'authority::record_entry' );
486 metabib::full_rec->has_a( record => 'biblio::record_entry' );
488 metabib::title_field_entry->has_a( source => 'biblio::record_entry' );
489 metabib::title_field_entry->has_a( field => 'config::metabib_field' );
491 metabib::author_field_entry->has_a( source => 'biblio::record_entry' );
492 metabib::author_field_entry->has_a( field => 'config::metabib_field' );
494 metabib::subject_field_entry->has_a( source => 'biblio::record_entry' );
495 metabib::subject_field_entry->has_a( field => 'config::metabib_field' );
497 metabib::keyword_field_entry->has_a( source => 'biblio::record_entry' );
498 metabib::keyword_field_entry->has_a( field => 'config::metabib_field' );
500 metabib::series_field_entry->has_a( source => 'biblio::record_entry' );
501 metabib::series_field_entry->has_a( field => 'config::metabib_field' );
503 metabib::metarecord_source_map->has_a( metarecord => 'metabib::metarecord' );
504 metabib::metarecord_source_map->has_a( source => 'biblio::record_entry' );
506 action::circulation->has_a( usr => 'actor::user' );
507 action::circulation->has_a( target_copy => 'asset::copy' );
508 action::circulation->has_a( circ_lib => 'actor::org_unit' );
510 money::billable_transaction->has_a( usr => 'actor::user' );
513 #-------------------------------------------------------------------------------
514 actor::user->has_many( survey_responses => 'action::survey_response' );
515 actor::user->has_many( addresses => 'actor::user_address' );
516 actor::user->has_many( cards => 'actor::card' );
518 actor::org_unit->has_many( users => 'actor::user' );
519 actor::profile->has_many( users => 'actor::user' );
521 action::survey->has_many( questions => 'action::survey_question' );
522 action::survey->has_many( responses => 'action::survey_response' );
524 action::survey_question->has_many( answers => 'action::survey_answer' );
525 action::survey_question->has_many( responses => 'action::survey_response' );
527 action::survey_answer->has_many( responses => 'action::survey_response' );
529 asset::copy->has_many( notes => 'asset::copy_note' );
530 asset::call_number->has_many( copies => 'asset::copy' );
531 asset::call_number->has_many( notes => 'asset::call_number_note' );
533 authority::record_entry->has_many( record_descriptor => 'authority::record_descriptor' );
534 authority::record_entry->has_many( notes => 'authority::record_note' );
536 biblio::record_entry->has_many( record_descriptor => 'metabib::record_descriptor' );
537 biblio::record_entry->has_many( notes => 'biblio::record_note' );
538 biblio::record_entry->has_many( call_numbers => 'asset::call_number' );
539 biblio::record_entry->has_many( full_record_entries => 'metabib::full_rec' );
540 biblio::record_entry->has_many( title_field_entries => 'metabib::title_field_entry' );
541 biblio::record_entry->has_many( author_field_entries => 'metabib::author_field_entry' );
542 biblio::record_entry->has_many( subject_field_entries => 'metabib::subject_field_entry' );
543 biblio::record_entry->has_many( keyword_field_entries => 'metabib::keyword_field_entry' );
544 biblio::record_entry->has_many( series_field_entries => 'metabib::series_field_entry' );
546 metabib::metarecord->has_many( source_records => [ 'metabib::metarecord_source_map' => 'source'] );
548 money::billable_transaction->has_many( billings => 'money::billing' );
549 money::billable_transaction->has_many( payments => 'money::payment' );
551 money::billing->has_a( xact => 'money::billable_transaction' );
552 money::payment->has_a( xact => 'money::billable_transaction' );
554 money::cash_payment->has_a( xact => 'money::billable_transaction' );
555 money::cash_payment->has_a( accepting_usr => 'actor::user' );
557 money::check_payment->has_a( xact => 'money::billable_transaction' );
558 money::check_payment->has_a( accepting_usr => 'actor::user' );
560 money::credit_card_payment->has_a( xact => 'money::billable_transaction' );
561 money::credit_card_payment->has_a( accepting_usr => 'actor::user' );
563 money::forgive_payment->has_a( xact => 'money::billable_transaction' );
564 money::forgive_payment->has_a( accepting_usr => 'actor::user' );
566 money::work_payment->has_a( xact => 'money::billable_transaction' );
567 money::work_payment->has_a( accepting_usr => 'actor::user' );
569 money::credit_payment->has_a( xact => 'money::billable_transaction' );
570 money::credit_payment->has_a( accepting_usr => 'actor::user' );
572 permission::grp_tree->has_a( parent => 'permission::grp_tree' );
574 permission::grp_perm_map->has_a( grp => 'permission::grp_tree' );
575 permission::grp_perm_map->has_a( perm => 'permission::perm_list' );
576 permission::grp_perm_map->has_a( depth => 'actor::org_unit_type' );
578 permission::usr_perm_map->has_a( usr => 'actor::user' );
579 permission::usr_perm_map->has_a( perm => 'permission::perm_list' );
580 permission::usr_perm_map->has_a( depth => 'actor::org_unit_type' );
582 permission::usr_grp_map->has_a( usr => 'actor::user' );
583 permission::usr_grp_map->has_a( grp => 'permission::grp_tree' );
585 action::hold_notification->has_a( hold => 'action::hold_request' );
587 action::hold_copy_map->has_a( hold => 'action::hold_request' );
588 action::hold_copy_map->has_a( target_copy => 'asset::copy' );
590 action::hold_request->has_a( current_copy => 'asset::copy' );
591 action::hold_request->has_a( requestor => 'actor::user' );
592 action::hold_request->has_a( usr => 'actor::user' );
593 action::hold_request->has_a( pickup_lib => 'actor::org_unit' );
595 action::hold_request->has_many( notifications => 'action::hold_notification' );
596 action::hold_request->has_many( copy_maps => 'action::hold_copy_map' );
598 asset::copy->has_many( hold_maps => 'action::hold_copy_map' );