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';
215 sub pg_begin_xaction {
219 if (my $old_xact = $pg->current_xact_session) {
220 if ($pg->current_xact_is_auto) {
221 $log->debug("Commiting old autocommit transaction with Open-ILS XACT-ID [$old_xact]", INFO);
222 $self->pg_commit_xaction($client);
224 $log->debug("Rolling back old NON-autocommit transaction with Open-ILS XACT-ID [$old_xact]", INFO);
225 $self->pg_rollback_xaction($client);
226 throw OpenSRF::DomainObject::oilsException->new(
228 status => "Previous transaction rolled back!",
233 $pg->set_xact_session( $client->session );
234 my $xact_id = $pg->current_xact_id;
236 $log->debug("Beginning a new trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
238 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
245 $log->debug("Failed to begin a new trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
250 my $death_cb = $client->session->register_callback(
252 __PACKAGE__->pg_rollback_xaction;
256 $log->debug("Registered 'death' callback [$death_cb] for new trasaction with Open-ILS XACT-ID [$xact_id]", DEBUG);
258 $client->session->session_data( death_cb => $death_cb );
260 if ($self->api_name =~ /autocommit$/o) {
261 $pg->current_xact_is_auto(1);
262 my $dc_cb = $client->session->register_callback(
265 $ses->unregister_callback(death => $death_cb);
266 __PACKAGE__->pg_commit_xaction;
269 $log->debug("Registered 'disconnect' callback [$dc_cb] for new trasaction with Open-ILS XACT-ID [$xact_id]", DEBUG);
270 if ($client and $client->session) {
271 $client->session->session_data( disconnect_cb => $dc_cb );
278 __PACKAGE__->register_method(
279 method => 'pg_begin_xaction',
280 api_name => 'open-ils.storage.transaction.begin',
284 __PACKAGE__->register_method(
285 method => 'pg_begin_xaction',
286 api_name => 'open-ils.storage.transaction.begin.autocommit',
291 sub pg_commit_xaction {
294 my $xact_id = $pg->current_xact_id;
297 $log->debug("Committing trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
298 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
303 $log->debug("Failed to commit trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
307 $pg->current_xact_session->unregister_callback( death =>
308 $pg->current_xact_session->session_data( 'death_cb' )
309 ) if ($pg->current_xact_session);
311 if ($pg->current_xact_is_auto) {
312 $pg->current_xact_session->unregister_callback( disconnect =>
313 $pg->current_xact_session->session_data( 'disconnect_cb' )
317 $pg->unset_xact_session;
322 __PACKAGE__->register_method(
323 method => 'pg_commit_xaction',
324 api_name => 'open-ils.storage.transaction.commit',
329 sub pg_rollback_xaction {
332 my $xact_id = $pg->current_xact_id;
334 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
335 $log->debug("Rolling back a trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
340 $log->debug("Failed to roll back trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
344 $pg->current_xact_session->unregister_callback( death =>
345 $pg->current_xact_session->session_data( 'death_cb' )
346 ) if ($pg->current_xact_session);
348 if ($pg->current_xact_is_auto) {
349 $pg->current_xact_session->unregister_callback( disconnect =>
350 $pg->current_xact_session->session_data( 'disconnect_cb' )
354 $pg->unset_xact_session;
358 __PACKAGE__->register_method(
359 method => 'pg_rollback_xaction',
360 api_name => 'open-ils.storage.transaction.rollback',
370 warn 'Inside copy_create...';
372 return undef unless ($pg->current_xact_session);
374 my $cdbi = $self->{cdbi};
376 my $pri = $cdbi->columns('Primary');
378 my @cols = grep {$_ ne $pri} $cdbi->columns('All');
380 my $col_list = join ',', @cols;
382 $log->debug('Starting COPY import for '.$cdbi->table." ($col_list)", DEBUG);
383 $cdbi->sql_copy_start($cdbi->table, $col_list)->execute;
385 my $dbh = $cdbi->db_Main;
386 for my $node ( @fm_nodes ) {
388 my $line = join("\t", map { defined($node->$_()) ? $node->$_() : '\N' } @cols);
389 $log->debug("COPY line: [$line]",DEBUG);
390 $dbh->func($line."\n", 'putline');
393 $dbh->func('endcopy');
395 return scalar(@fm_nodes);
401 #---------------------------------------------------------------------
402 package action::survey;
404 action::survey->table( 'action.survey' );
405 action::survey->sequence( 'action.survey_id_seq' );
407 #---------------------------------------------------------------------
408 package action::survey_question;
410 action::survey_question->table( 'action.survey_question' );
411 action::survey_question->sequence( 'action.survey_question_id_seq' );
413 #---------------------------------------------------------------------
414 package action::survey_answer;
416 action::survey_answer->table( 'action.survey_answer' );
417 action::survey_answer->sequence( 'action.survey_answer_id_seq' );
419 #---------------------------------------------------------------------
420 package action::survey_response;
422 action::survey_response->table( 'action.survey_response' );
423 action::survey_response->sequence( 'action.survey_response_id_seq' );
425 #---------------------------------------------------------------------
426 package config::metabib_field;
428 config::metabib_field->table( 'config.metabib_field' );
429 config::metabib_field->sequence( 'config.metabib_field_id_seq' );
431 #---------------------------------------------------------------------
432 package config::bib_source;
434 config::bib_source->table( 'config.bib_source' );
435 config::bib_source->sequence( 'config.bib_source_id_seq' );
437 #---------------------------------------------------------------------
438 package config::identification_type;
440 config::identification_type->table( 'config.identification_type' );
441 config::identification_type->sequence( 'config.identification_type_id_seq' );
443 #---------------------------------------------------------------------
444 package asset::call_number_note;
446 asset::call_number->table( 'asset.call_number_note' );
447 asset::call_number->sequence( 'asset.call_number_note_id_seq' );
449 #---------------------------------------------------------------------
450 package asset::copy_note;
452 asset::copy->table( 'asset.copy_note' );
453 asset::copy->sequence( 'asset.copy_note_id_seq' );
455 #---------------------------------------------------------------------
456 package asset::call_number;
458 asset::call_number->table( 'asset.call_number' );
459 asset::call_number->sequence( 'asset.call_number_id_seq' );
461 #---------------------------------------------------------------------
464 asset::copy->table( 'asset.copy' );
465 asset::copy->sequence( 'asset.copy_id_seq' );
467 #---------------------------------------------------------------------
468 package asset::stat_cat;
470 asset::stat_cat->table( 'asset.stat_cat' );
471 asset::stat_cat->sequence( 'asset.stat_cat_id_seq' );
473 #---------------------------------------------------------------------
474 package asset::stat_cat_entry;
476 asset::stat_cat_entry->table( 'asset.stat_cat_entry' );
477 asset::stat_cat_entry->sequence( 'asset.stat_cat_entry_id_seq' );
479 #---------------------------------------------------------------------
480 package asset::stat_cat_entry_copy_map;
482 asset::stat_cat_entry_copy_map->table( 'asset.stat_cat_entry_copy_map' );
483 asset::stat_cat_entry_copy_map->sequence( 'asset.stat_cat_entry_copy_map_id_seq' );
485 #---------------------------------------------------------------------
486 package biblio::record_entry;
488 biblio::record_entry->table( 'biblio.record_entry' );
489 biblio::record_entry->sequence( 'biblio.record_entry_id_seq' );
491 #---------------------------------------------------------------------
492 #package biblio::record_marc;
494 #biblio::record_marc->table( 'biblio.record_marc' );
495 #biblio::record_marc->sequence( 'biblio.record_marc_id_seq' );
497 #---------------------------------------------------------------------
498 package biblio::record_note;
500 biblio::record_note->table( 'biblio.record_note' );
501 biblio::record_note->sequence( 'biblio.record_note_id_seq' );
503 #---------------------------------------------------------------------
506 actor::user->table( 'actor.usr' );
507 actor::user->sequence( 'actor.usr_id_seq' );
509 #---------------------------------------------------------------------
510 package actor::org_unit_type;
512 actor::org_unit_type->table( 'actor.org_unit_type' );
513 actor::org_unit_type->sequence( 'actor.org_unit_type_id_seq' );
515 #---------------------------------------------------------------------
516 package actor::org_unit;
518 actor::org_unit->table( 'actor.org_unit' );
519 actor::org_unit->sequence( 'actor.org_unit_id_seq' );
521 #---------------------------------------------------------------------
522 package actor::stat_cat;
524 actor::stat_cat->table( 'actor.stat_cat' );
525 actor::stat_cat->sequence( 'actor.stat_cat_id_seq' );
527 #---------------------------------------------------------------------
528 package actor::stat_cat_entry;
530 actor::stat_cat_entry->table( 'actor.stat_cat_entry' );
531 actor::stat_cat_entry->sequence( 'actor.stat_cat_entry_id_seq' );
533 #---------------------------------------------------------------------
534 package actor::stat_cat_entry_user_map;
536 actor::stat_cat_entry_user_map->table( 'actor.stat_cat_entry_copy_map' );
537 actor::stat_cat_entry_user_map->sequence( 'actor.stat_cat_entry_usr_map_id_seq' );
539 #---------------------------------------------------------------------
542 actor::card->table( 'actor.card' );
543 actor::card->sequence( 'actor.card_id_seq' );
546 #---------------------------------------------------------------------
548 #-------------------------------------------------------------------------------
549 package metabib::metarecord;
551 metabib::metarecord->table( 'metabib.metarecord' );
552 metabib::metarecord->sequence( 'metabib.metarecord_id_seq' );
554 #-------------------------------------------------------------------------------
556 #-------------------------------------------------------------------------------
557 package metabib::title_field_entry;
559 metabib::title_field_entry->table( 'metabib.title_field_entry' );
560 metabib::title_field_entry->sequence( 'metabib.title_field_entry_id_seq' );
561 metabib::title_field_entry->columns( 'FTS' => 'index_vector' );
563 # metabib::title_field_entry->add_trigger(
564 # before_create => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
566 # metabib::title_field_entry->add_trigger(
567 # before_update => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
570 OpenILS::Application::Storage->register_method(
571 api_name => 'open-ils.storage.direct.metabib.title_field_entry.batch.create',
572 method => 'copy_create',
574 'package' => 'OpenILS::Application::Storage',
575 cdbi => 'metabib::title_field_entry',
578 #-------------------------------------------------------------------------------
580 #-------------------------------------------------------------------------------
581 package metabib::author_field_entry;
583 metabib::author_field_entry->table( 'metabib.author_field_entry' );
584 metabib::author_field_entry->sequence( 'metabib.author_field_entry_id_seq' );
585 metabib::author_field_entry->columns( 'FTS' => 'index_vector' );
587 OpenILS::Application::Storage->register_method(
588 api_name => 'open-ils.storage.direct.metabib.author_field_entry.batch.create',
589 method => 'copy_create',
591 'package' => 'OpenILS::Application::Storage',
592 cdbi => 'metabib::author_field_entry',
595 #-------------------------------------------------------------------------------
597 #-------------------------------------------------------------------------------
598 package metabib::subject_field_entry;
600 metabib::subject_field_entry->table( 'metabib.subject_field_entry' );
601 metabib::subject_field_entry->sequence( 'metabib.subject_field_entry_id_seq' );
602 metabib::subject_field_entry->columns( 'FTS' => 'index_vector' );
604 OpenILS::Application::Storage->register_method(
605 api_name => 'open-ils.storage.direct.metabib.subject_field_entry.batch.create',
606 method => 'copy_create',
608 'package' => 'OpenILS::Application::Storage',
609 cdbi => 'metabib::subject_field_entry',
612 #-------------------------------------------------------------------------------
614 #-------------------------------------------------------------------------------
615 package metabib::keyword_field_entry;
617 metabib::keyword_field_entry->table( 'metabib.keyword_field_entry' );
618 metabib::keyword_field_entry->sequence( 'metabib.keyword_field_entry_id_seq' );
619 metabib::keyword_field_entry->columns( 'FTS' => 'index_vector' );
621 OpenILS::Application::Storage->register_method(
622 api_name => 'open-ils.storage.direct.metabib.keyword_field_entry.batch.create',
623 method => 'copy_create',
625 'package' => 'OpenILS::Application::Storage',
626 cdbi => 'metabib::keyword_field_entry',
629 #-------------------------------------------------------------------------------
631 #-------------------------------------------------------------------------------
632 #package metabib::title_field_entry_source_map;
634 #metabib::title_field_entry_source_map->table( 'metabib.title_field_entry_source_map' );
636 #-------------------------------------------------------------------------------
638 #-------------------------------------------------------------------------------
639 #package metabib::author_field_entry_source_map;
641 #metabib::author_field_entry_source_map->table( 'metabib.author_field_entry_source_map' );
643 #-------------------------------------------------------------------------------
645 #-------------------------------------------------------------------------------
646 #package metabib::subject_field_entry_source_map;
648 #metabib::subject_field_entry_source_map->table( 'metabib.subject_field_entry_source_map' );
650 #-------------------------------------------------------------------------------
652 #-------------------------------------------------------------------------------
653 #package metabib::keyword_field_entry_source_map;
655 #metabib::keyword_field_entry_source_map->table( 'metabib.keyword_field_entry_source_map' );
657 #-------------------------------------------------------------------------------
659 #-------------------------------------------------------------------------------
660 package metabib::metarecord_source_map;
662 metabib::metarecord_source_map->table( 'metabib.metarecord_source_map' );
664 #-------------------------------------------------------------------------------
665 package metabib::record_descriptor;
667 metabib::record_descriptor->table( 'metabib.rec_descriptor' );
668 metabib::record_descriptor->sequence( 'metabib.rec_descriptor_id_seq' );
670 OpenILS::Application::Storage->register_method(
671 api_name => 'open-ils.storage.direct.metabib.record_descriptor.batch.create',
672 method => 'copy_create',
674 'package' => 'OpenILS::Application::Storage',
675 cdbi => 'metabib::record_descriptor',
678 #-------------------------------------------------------------------------------
681 #-------------------------------------------------------------------------------
682 package metabib::full_rec;
684 metabib::full_rec->table( 'metabib.full_rec' );
685 metabib::full_rec->sequence( 'metabib.full_rec_id_seq' );
686 metabib::full_rec->columns( 'FTS' => 'index_vector' );
688 OpenILS::Application::Storage->register_method(
689 api_name => 'open-ils.storage.direct.metabib.full_rec.batch.create',
690 method => 'copy_create',
692 'package' => 'OpenILS::Application::Storage',
693 cdbi => 'metabib::full_rec',
697 #-------------------------------------------------------------------------------