1 { # Based on the change to Class::DBI in OpenILS::Application::Storage. This will
2 # allow us to use TSearch2 via a simple cdbi "search" interface.
3 #-------------------------------------------------------------------------------
10 if (ref($args[-1]) eq 'HASH') {
11 $args[-1]->{_placeholder} = "to_tsquery('default',?)";
13 push @args, {_placeholder => "to_tsquery('default',?)"};
15 $self->_do_search("@@" => @args);
19 { # Every driver needs to provide a 'compile()' method to OpenILS::Application::Storage::FTS.
20 # If that driver wants to support FTI, that is...
21 #-------------------------------------------------------------------------------
22 package OpenILS::Application::Storage::FTS;
23 use OpenSRF::Utils::Logger qw/:level/;
24 my $log = 'OpenSRF::Utils::Logger';
30 $self = ref($self) || $self;
31 $self = bless {} => $self;
33 $self->decompose($term);
35 my $newterm = join('&', $self->words);
38 $newterm = '('.$newterm.')&('. join('|', $self->nots) . ')';
41 $log->debug("Compiled term is [$newterm]", DEBUG);
42 $newterm = OpenILS::Application::Storage::Driver::Pg->quote($newterm);
43 $log->debug("Quoted term is [$newterm]", DEBUG);
45 $self->{fts_query} = ["to_tsquery('default',$newterm)"];
46 $self->{fts_query_nots} = [];
47 $self->{fts_op} = '@@';
48 $self->{text_col} = shift;
49 $self->{fts_col} = shift;
54 sub sql_where_clause {
56 my $column = $self->fts_col;
60 for my $fts ( $self->fts_query ) {
61 push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
62 push @ranks, "rank($column, $fts)";
64 $self->{fts_rank} = \@ranks;
66 my $phrase_match = $self->sql_exact_phrase_match();
67 return join(' AND ', @output) . $phrase_match;
73 { # The driver package itself just needs a db_Main method (or db_Slaves if
74 #Class::DBI::Replication is in use) for Class::DBI to call.
76 # Any other fixups can go in here too... Also, the drivers should subclass the
77 # DBI driver that they are wrapping, or provide a 'quote()' method that calls
78 # the DBD::xxx::quote() method on FTI's behalf.
80 # The dirver MUST be a subclass of Class::DBI(::Replication) and
81 # OpenILS::Application::Storage.
82 #-------------------------------------------------------------------------------
83 package OpenILS::Application::Storage::Driver::Pg;
85 use base qw/Class::DBI OpenILS::Application::Storage/;
87 use OpenSRF::EX qw/:try/;
88 use OpenSRF::DomainObject::oilsResponse;
89 use OpenSRF::Utils::Logger qw/:level/;
90 my $log = 'OpenSRF::Utils::Logger';
92 __PACKAGE__->set_sql( retrieve_limited => 'SELECT * FROM __TABLE__ ORDER BY id LIMIT ?' );
93 __PACKAGE__->set_sql( copy_start => 'COPY %s (%s) FROM STDIN;' );
94 __PACKAGE__->set_sql( copy_end => '\.' );
103 $log->debug("Running child_init inside ".__PACKAGE__, INTERNAL);
105 $_db_params = [ $_db_params ] unless (ref($_db_params) eq 'ARRAY');
107 my %attrs = ( %{$self->_default_attributes},
108 RootClass => 'DBIx::ContextualFetch',
109 ShowErrorStatement => 1,
115 FetchHashKeyName => 'NAME_lc',
119 my $master = shift @$_db_params;
120 $master_db = DBI->connect("dbi:Pg:host=$$master{host};dbname=$$master{db}",$$master{user},$$master{pw}, \%attrs);
121 $master_db->do("SET NAMES '$$master{client_encoding}';") if ($$master{client_encoding});
123 $log->debug("Connected to MASTER db '$$master{db} at $$master{host}", INFO);
125 for my $db (@$_db_params) {
126 push @slave_dbs, DBI->connect("dbi:Pg:host=$$db{host};dbname=$$db{db}",$$db{user},$$db{pw}, \%attrs);
127 $slave_dbs[-1]->do("SET NAMES '$$db{client_encoding}';") if ($$master{client_encoding});
129 $log->debug("Connected to MASTER db '$$master{db} at $$master{host}", INFO);
132 $log->debug("All is well on the western front", INTERNAL);
137 return $master_db if ($self->current_xact_session);
138 return $master_db unless (@slave_dbs);
139 return ($master_db, @slave_dbs)[rand(scalar(@slave_dbs))];
144 return $self->db_Main->quote(@_)
147 # sub tsearch2_trigger {
149 # return unless ($self->value);
150 # $self->index_vector(
151 # $self->db_Slaves->selectrow_array(
152 # "SELECT to_tsvector('default',?);",
161 sub current_xact_session {
163 if (defined($_xact_session)) {
164 return $_xact_session;
169 sub current_xact_is_auto {
172 if (defined($_xact_session) and ref($_xact_session)) {
174 $_xact_session->session_data(autocommit => $auto);
176 return $_xact_session->session_data('autocommit');
180 sub current_xact_id {
182 if (defined($_xact_session) and ref($_xact_session)) {
183 return $_xact_session->session_id;
188 sub set_xact_session {
191 if (!defined($ses)) {
194 $_xact_session = $ses;
195 return $_xact_session;
198 sub unset_xact_session {
200 my $ses = $_xact_session;
201 undef $_xact_session;
209 package OpenILS::Application::Storage;
210 use OpenSRF::Utils::Logger;
211 my $log = 'OpenSRF::Utils::Logger';
213 my $pg = 'OpenILS::Application::Storage::Driver::Pg';
219 return $pg->current_xact_id;
221 __PACKAGE__->register_method(
222 method => 'current_xact',
223 api_name => 'open-ils.storage.transaction.current',
229 sub pg_begin_xaction {
233 if (my $old_xact = $pg->current_xact_session) {
234 if ($pg->current_xact_is_auto) {
235 $log->debug("Commiting old autocommit transaction with Open-ILS XACT-ID [$old_xact]", INFO);
236 $self->pg_commit_xaction($client);
238 $log->debug("Rolling back old NON-autocommit transaction with Open-ILS XACT-ID [$old_xact]", INFO);
239 $self->pg_rollback_xaction($client);
240 throw OpenSRF::DomainObject::oilsException->new(
242 status => "Previous transaction rolled back!",
247 $pg->set_xact_session( $client->session );
248 my $xact_id = $pg->current_xact_id;
250 $log->debug("Beginning a new trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
252 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
259 $log->debug("Failed to begin a new trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
264 my $death_cb = $client->session->register_callback(
266 __PACKAGE__->pg_rollback_xaction;
270 $log->debug("Registered 'death' callback [$death_cb] for new trasaction with Open-ILS XACT-ID [$xact_id]", DEBUG);
272 $client->session->session_data( death_cb => $death_cb );
274 if ($self->api_name =~ /autocommit$/o) {
275 $pg->current_xact_is_auto(1);
276 my $dc_cb = $client->session->register_callback(
279 $ses->unregister_callback(death => $death_cb);
280 __PACKAGE__->pg_commit_xaction;
283 $log->debug("Registered 'disconnect' callback [$dc_cb] for new trasaction with Open-ILS XACT-ID [$xact_id]", DEBUG);
284 if ($client and $client->session) {
285 $client->session->session_data( disconnect_cb => $dc_cb );
292 __PACKAGE__->register_method(
293 method => 'pg_begin_xaction',
294 api_name => 'open-ils.storage.transaction.begin',
298 __PACKAGE__->register_method(
299 method => 'pg_begin_xaction',
300 api_name => 'open-ils.storage.transaction.begin.autocommit',
305 sub pg_commit_xaction {
308 my $xact_id = $pg->current_xact_id;
311 $log->debug("Committing trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
312 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
317 $log->debug("Failed to commit trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
321 $pg->current_xact_session->unregister_callback( death =>
322 $pg->current_xact_session->session_data( 'death_cb' )
323 ) if ($pg->current_xact_session);
325 if ($pg->current_xact_is_auto) {
326 $pg->current_xact_session->unregister_callback( disconnect =>
327 $pg->current_xact_session->session_data( 'disconnect_cb' )
331 $pg->unset_xact_session;
336 __PACKAGE__->register_method(
337 method => 'pg_commit_xaction',
338 api_name => 'open-ils.storage.transaction.commit',
343 sub pg_rollback_xaction {
346 my $xact_id = $pg->current_xact_id;
348 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
349 $log->debug("Rolling back a trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
354 $log->debug("Failed to roll back trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
358 $pg->current_xact_session->unregister_callback( death =>
359 $pg->current_xact_session->session_data( 'death_cb' )
360 ) if ($pg->current_xact_session);
362 if ($pg->current_xact_is_auto) {
363 $pg->current_xact_session->unregister_callback( disconnect =>
364 $pg->current_xact_session->session_data( 'disconnect_cb' )
368 $pg->unset_xact_session;
372 __PACKAGE__->register_method(
373 method => 'pg_rollback_xaction',
374 api_name => 'open-ils.storage.transaction.rollback',
384 return undef unless ($pg->current_xact_session);
386 my $cdbi = $self->{cdbi};
388 my $pri = $cdbi->columns('Primary');
390 my @cols = grep {$_ ne $pri} $cdbi->columns('All');
392 my $col_list = join ',', @cols;
394 $log->debug('Starting COPY import for '.$cdbi->table." ($col_list)", DEBUG);
395 $cdbi->sql_copy_start($cdbi->table, $col_list)->execute;
397 my $dbh = $cdbi->db_Main;
398 for my $node ( @fm_nodes ) {
400 my $line = join("\t", map { defined($node->$_()) ? $node->$_() : '\N' } @cols);
401 $log->debug("COPY line: [$line]",DEBUG);
402 $dbh->func($line."\n", 'putline');
405 $dbh->func('endcopy');
407 return scalar(@fm_nodes);
413 #---------------------------------------------------------------------
414 package action::survey;
416 action::survey->table( 'action.survey' );
417 action::survey->sequence( 'action.survey_id_seq' );
419 #---------------------------------------------------------------------
420 package action::survey_question;
422 action::survey_question->table( 'action.survey_question' );
423 action::survey_question->sequence( 'action.survey_question_id_seq' );
425 #---------------------------------------------------------------------
426 package action::survey_answer;
428 action::survey_answer->table( 'action.survey_answer' );
429 action::survey_answer->sequence( 'action.survey_answer_id_seq' );
431 #---------------------------------------------------------------------
432 package action::survey_response;
434 action::survey_response->table( 'action.survey_response' );
435 action::survey_response->sequence( 'action.survey_response_id_seq' );
437 #---------------------------------------------------------------------
438 package config::standing;
440 config::standing->table( 'config.standing' );
441 config::standing->sequence( 'config.standing_id_seq' );
443 #---------------------------------------------------------------------
444 package config::metabib_field;
446 config::metabib_field->table( 'config.metabib_field' );
447 config::metabib_field->sequence( 'config.metabib_field_id_seq' );
449 #---------------------------------------------------------------------
450 package config::bib_source;
452 config::bib_source->table( 'config.bib_source' );
453 config::bib_source->sequence( 'config.bib_source_id_seq' );
455 #---------------------------------------------------------------------
456 package config::identification_type;
458 config::identification_type->table( 'config.identification_type' );
459 config::identification_type->sequence( 'config.identification_type_id_seq' );
461 #---------------------------------------------------------------------
462 package asset::call_number_note;
464 asset::call_number->table( 'asset.call_number_note' );
465 asset::call_number->sequence( 'asset.call_number_note_id_seq' );
467 #---------------------------------------------------------------------
468 package asset::copy_note;
470 asset::copy->table( 'asset.copy_note' );
471 asset::copy->sequence( 'asset.copy_note_id_seq' );
473 #---------------------------------------------------------------------
474 package asset::call_number;
476 asset::call_number->table( 'asset.call_number' );
477 asset::call_number->sequence( 'asset.call_number_id_seq' );
479 #---------------------------------------------------------------------
482 asset::copy->table( 'asset.copy' );
483 asset::copy->sequence( 'asset.copy_id_seq' );
485 #---------------------------------------------------------------------
486 package asset::stat_cat;
488 asset::stat_cat->table( 'asset.stat_cat' );
489 asset::stat_cat->sequence( 'asset.stat_cat_id_seq' );
491 #---------------------------------------------------------------------
492 package asset::stat_cat_entry;
494 asset::stat_cat_entry->table( 'asset.stat_cat_entry' );
495 asset::stat_cat_entry->sequence( 'asset.stat_cat_entry_id_seq' );
497 #---------------------------------------------------------------------
498 package asset::stat_cat_entry_copy_map;
500 asset::stat_cat_entry_copy_map->table( 'asset.stat_cat_entry_copy_map' );
501 asset::stat_cat_entry_copy_map->sequence( 'asset.stat_cat_entry_copy_map_id_seq' );
503 #---------------------------------------------------------------------
504 package biblio::record_entry;
506 biblio::record_entry->table( 'biblio.record_entry' );
507 biblio::record_entry->sequence( 'biblio.record_entry_id_seq' );
509 #---------------------------------------------------------------------
510 #package biblio::record_marc;
512 #biblio::record_marc->table( 'biblio.record_marc' );
513 #biblio::record_marc->sequence( 'biblio.record_marc_id_seq' );
515 #---------------------------------------------------------------------
516 package biblio::record_note;
518 biblio::record_note->table( 'biblio.record_note' );
519 biblio::record_note->sequence( 'biblio.record_note_id_seq' );
521 #---------------------------------------------------------------------
524 actor::user->table( 'actor.usr' );
525 actor::user->sequence( 'actor.usr_id_seq' );
527 #---------------------------------------------------------------------
528 package actor::user_address;
530 actor::user_address->table( 'actor.usr_address' );
531 actor::user_address->sequence( 'actor.usr_address_id_seq' );
533 #---------------------------------------------------------------------
534 package actor::profile;
536 actor::profile->table( 'actor.profile' );
537 actor::profile->sequence( 'actor.profile_id_seq' );
539 #---------------------------------------------------------------------
540 package actor::org_unit_type;
542 actor::org_unit_type->table( 'actor.org_unit_type' );
543 actor::org_unit_type->sequence( 'actor.org_unit_type_id_seq' );
545 #---------------------------------------------------------------------
546 package actor::org_unit;
548 actor::org_unit->table( 'actor.org_unit' );
549 actor::org_unit->sequence( 'actor.org_unit_id_seq' );
551 #---------------------------------------------------------------------
552 package actor::stat_cat;
554 actor::stat_cat->table( 'actor.stat_cat' );
555 actor::stat_cat->sequence( 'actor.stat_cat_id_seq' );
557 #---------------------------------------------------------------------
558 package actor::stat_cat_entry;
560 actor::stat_cat_entry->table( 'actor.stat_cat_entry' );
561 actor::stat_cat_entry->sequence( 'actor.stat_cat_entry_id_seq' );
563 #---------------------------------------------------------------------
564 package actor::stat_cat_entry_user_map;
566 actor::stat_cat_entry_user_map->table( 'actor.stat_cat_entry_copy_map' );
567 actor::stat_cat_entry_user_map->sequence( 'actor.stat_cat_entry_usr_map_id_seq' );
569 #---------------------------------------------------------------------
572 actor::card->table( 'actor.card' );
573 actor::card->sequence( 'actor.card_id_seq' );
576 #---------------------------------------------------------------------
578 #-------------------------------------------------------------------------------
579 package metabib::metarecord;
581 metabib::metarecord->table( 'metabib.metarecord' );
582 metabib::metarecord->sequence( 'metabib.metarecord_id_seq' );
584 OpenILS::Application::Storage->register_method(
585 api_name => 'open-ils.storage.direct.metabib.metarecord.batch.create',
586 method => 'copy_create',
588 'package' => 'OpenILS::Application::Storage',
589 cdbi => 'metabib::metarecord',
593 #-------------------------------------------------------------------------------
595 #-------------------------------------------------------------------------------
596 package metabib::title_field_entry;
598 metabib::title_field_entry->table( 'metabib.title_field_entry' );
599 metabib::title_field_entry->sequence( 'metabib.title_field_entry_id_seq' );
600 metabib::title_field_entry->columns( 'FTS' => 'index_vector' );
602 # metabib::title_field_entry->add_trigger(
603 # before_create => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
605 # metabib::title_field_entry->add_trigger(
606 # before_update => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
609 OpenILS::Application::Storage->register_method(
610 api_name => 'open-ils.storage.direct.metabib.title_field_entry.batch.create',
611 method => 'copy_create',
613 'package' => 'OpenILS::Application::Storage',
614 cdbi => 'metabib::title_field_entry',
617 #-------------------------------------------------------------------------------
619 #-------------------------------------------------------------------------------
620 package metabib::author_field_entry;
622 metabib::author_field_entry->table( 'metabib.author_field_entry' );
623 metabib::author_field_entry->sequence( 'metabib.author_field_entry_id_seq' );
624 metabib::author_field_entry->columns( 'FTS' => 'index_vector' );
626 OpenILS::Application::Storage->register_method(
627 api_name => 'open-ils.storage.direct.metabib.author_field_entry.batch.create',
628 method => 'copy_create',
630 'package' => 'OpenILS::Application::Storage',
631 cdbi => 'metabib::author_field_entry',
634 #-------------------------------------------------------------------------------
636 #-------------------------------------------------------------------------------
637 package metabib::subject_field_entry;
639 metabib::subject_field_entry->table( 'metabib.subject_field_entry' );
640 metabib::subject_field_entry->sequence( 'metabib.subject_field_entry_id_seq' );
641 metabib::subject_field_entry->columns( 'FTS' => 'index_vector' );
643 OpenILS::Application::Storage->register_method(
644 api_name => 'open-ils.storage.direct.metabib.subject_field_entry.batch.create',
645 method => 'copy_create',
647 'package' => 'OpenILS::Application::Storage',
648 cdbi => 'metabib::subject_field_entry',
651 #-------------------------------------------------------------------------------
653 #-------------------------------------------------------------------------------
654 package metabib::keyword_field_entry;
656 metabib::keyword_field_entry->table( 'metabib.keyword_field_entry' );
657 metabib::keyword_field_entry->sequence( 'metabib.keyword_field_entry_id_seq' );
658 metabib::keyword_field_entry->columns( 'FTS' => 'index_vector' );
660 OpenILS::Application::Storage->register_method(
661 api_name => 'open-ils.storage.direct.metabib.keyword_field_entry.batch.create',
662 method => 'copy_create',
664 'package' => 'OpenILS::Application::Storage',
665 cdbi => 'metabib::keyword_field_entry',
668 #-------------------------------------------------------------------------------
670 #-------------------------------------------------------------------------------
671 #package metabib::title_field_entry_source_map;
673 #metabib::title_field_entry_source_map->table( 'metabib.title_field_entry_source_map' );
675 #-------------------------------------------------------------------------------
677 #-------------------------------------------------------------------------------
678 #package metabib::author_field_entry_source_map;
680 #metabib::author_field_entry_source_map->table( 'metabib.author_field_entry_source_map' );
682 #-------------------------------------------------------------------------------
684 #-------------------------------------------------------------------------------
685 #package metabib::subject_field_entry_source_map;
687 #metabib::subject_field_entry_source_map->table( 'metabib.subject_field_entry_source_map' );
689 #-------------------------------------------------------------------------------
691 #-------------------------------------------------------------------------------
692 #package metabib::keyword_field_entry_source_map;
694 #metabib::keyword_field_entry_source_map->table( 'metabib.keyword_field_entry_source_map' );
696 #-------------------------------------------------------------------------------
698 #-------------------------------------------------------------------------------
699 package metabib::metarecord_source_map;
701 metabib::metarecord_source_map->table( 'metabib.metarecord_source_map' );
702 OpenILS::Application::Storage->register_method(
703 api_name => 'open-ils.storage.direct.metabib.metarecord_source_map.batch.create',
704 method => 'copy_create',
706 'package' => 'OpenILS::Application::Storage',
707 cdbi => 'metabib::metarecord_source_map',
711 #-------------------------------------------------------------------------------
712 package metabib::record_descriptor;
714 metabib::record_descriptor->table( 'metabib.rec_descriptor' );
715 metabib::record_descriptor->sequence( 'metabib.rec_descriptor_id_seq' );
717 OpenILS::Application::Storage->register_method(
718 api_name => 'open-ils.storage.direct.metabib.record_descriptor.batch.create',
719 method => 'copy_create',
721 'package' => 'OpenILS::Application::Storage',
722 cdbi => 'metabib::record_descriptor',
725 #-------------------------------------------------------------------------------
728 #-------------------------------------------------------------------------------
729 package metabib::full_rec;
731 metabib::full_rec->table( 'metabib.full_rec' );
732 metabib::full_rec->sequence( 'metabib.full_rec_id_seq' );
733 metabib::full_rec->columns( 'FTS' => 'index_vector' );
735 OpenILS::Application::Storage->register_method(
736 api_name => 'open-ils.storage.direct.metabib.full_rec.batch.create',
737 method => 'copy_create',
739 'package' => 'OpenILS::Application::Storage',
740 cdbi => 'metabib::full_rec',
744 #-------------------------------------------------------------------------------