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} = '@@';
52 sub sql_where_clause {
58 for my $fts ( $self->fts_query ) {
59 push @output, join(' ', $column, $self->{fts_op}, $fts);
60 push @ranks, "rank($column, $fts)";
62 $self->{fts_rank} = \@ranks;
64 return join(' AND ', @output);
70 { # The driver package itself just needs a db_Main method (or db_Slaves if
71 #Class::DBI::Replication is in use) for Class::DBI to call.
73 # Any other fixups can go in here too... Also, the drivers should subclass the
74 # DBI driver that they are wrapping, or provide a 'quote()' method that calls
75 # the DBD::xxx::quote() method on FTI's behalf.
77 # The dirver MUST be a subclass of Class::DBI(::Replication) and
78 # OpenILS::Application::Storage.
79 #-------------------------------------------------------------------------------
80 package OpenILS::Application::Storage::Driver::Pg;
82 use base qw/Class::DBI OpenILS::Application::Storage/;
84 use OpenSRF::EX qw/:try/;
85 use OpenSRF::DomainObject::oilsResponse;
86 use OpenSRF::Utils::Logger qw/:level/;
87 my $log = 'OpenSRF::Utils::Logger';
89 __PACKAGE__->set_sql( retrieve_limited => 'SELECT * FROM __TABLE__ ORDER BY id LIMIT ?' );
98 $log->debug("Running child_init inside ".__PACKAGE__, INTERNAL);
100 $_db_params = [ $_db_params ] unless (ref($_db_params) eq 'ARRAY');
102 my %attrs = ( %{$self->_default_attributes},
103 RootClass => 'DBIx::ContextualFetch',
104 ShowErrorStatement => 1,
110 FetchHashKeyName => 'NAME_lc',
114 my $master = shift @$_db_params;
115 $master_db = DBI->connect("dbi:Pg:host=$$master{host};dbname=$$master{db}",$$master{user},$$master{pw}, \%attrs);
116 $master_db->do("SET NAMES '$$master{client_encoding}';") if ($$master{client_encoding});
118 $log->debug("Connected to MASTER db '$$master{db} at $$master{host}", INFO);
120 for my $db (@$_db_params) {
121 push @slave_dbs, DBI->connect("dbi:Pg:host=$$db{host};dbname=$$db{db}",$$db{user},$$db{pw}, \%attrs);
122 $slave_dbs[-1]->do("SET NAMES '$$db{client_encoding}';") if ($$master{client_encoding});
124 $log->debug("Connected to MASTER db '$$master{db} at $$master{host}", INFO);
127 $log->debug("All is well on the western front", INTERNAL);
132 return $master_db if ($self->current_xact_session);
133 return $master_db unless (@slave_dbs);
134 return ($master_db, @slave_dbs)[rand(scalar(@slave_dbs))];
139 return $self->db_Main->quote(@_)
142 # sub tsearch2_trigger {
144 # return unless ($self->value);
145 # $self->index_vector(
146 # $self->db_Slaves->selectrow_array(
147 # "SELECT to_tsvector('default',?);",
156 sub current_xact_session {
158 if (defined($_xact_session)) {
159 return $_xact_session;
164 sub current_xact_is_auto {
166 return $_xact_session->session_data(autocommit => shift());
169 sub current_xact_id {
171 if (defined($_xact_session) and ref($_xact_session)) {
172 return $_xact_session->session_id;
177 sub set_xact_session {
180 if (!defined($ses)) {
183 $_xact_session = $ses;
184 return $_xact_session;
187 sub unset_xact_session {
189 my $ses = $_xact_session;
190 undef $_xact_session;
198 package OpenILS::Application::Storage;
199 use OpenSRF::Utils::Logger;
200 my $log = 'OpenSRF::Utils::Logger';
202 my $pg = 'OpenILS::Application::Storage::Driver::Pg';
204 sub pg_begin_xaction {
208 if (my $old_xact = $pg->current_xact_session) {
209 if ($pg->current_xact_is_auto) {
210 $log->debug("Commiting old autocommit transaction with Open-ILS XACT-ID [$old_xact]", INFO);
211 $self->pg_commit_xaction($client);
213 $log->debug("Rolling back old NON-autocommit transaction with Open-ILS XACT-ID [$old_xact]", INFO);
214 $self->pg_rollback_xaction($client);
215 return new OpenSRF::DomainObject::oilsException (
217 status => "Previous transaction rolled back!",
222 $pg->set_xact_session( $client->session );
223 my $xact_id = $pg->current_xact_id;
225 $log->debug("Beginning a new trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
227 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
234 $log->debug("Failed to begin a new trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
239 my $death_cb = $client->session->register_callback(
241 __PACKAGE__->pg_rollback_xaction;
245 $log->debug("Registered 'death' callback [$death_cb] for new trasaction with Open-ILS XACT-ID [$xact_id]", DEBUG);
247 $client->session->session_data( death_cb => $death_cb );
249 if ($self->api_name =~ /autocommit$/o) {
250 $pg->current_xact_is_auto(1);
251 my $dc_cb = $client->session->register_callback(
254 $ses->unregister_callback($death_cb);
255 __PACKAGE__->pg_commit_xaction;
258 $log->debug("Registered 'disconnect' callback [$dc_cb] for new trasaction with Open-ILS XACT-ID [$xact_id]", DEBUG);
259 if ($client and $client->session) {
260 $client->session->session_data( disconnect_cb => $dc_cb );
267 __PACKAGE__->register_method(
268 method => 'pg_begin_xaction',
269 api_name => 'open-ils.storage.transaction.begin',
273 __PACKAGE__->register_method(
274 method => 'pg_begin_xaction',
275 api_name => 'open-ils.storage.transaction.begin.autocommit',
280 sub pg_commit_xaction {
285 $log->debug("Committing trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
286 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
291 $log->debug("Failed to commit trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
295 $pg->current_xact_session->unregister_callback(
296 $pg->current_xact_session->session_data( 'death_cb' )
299 if ($pg->current_xact_is_auto) {
300 $pg->current_xact_session->unregister_callback(
301 $pg->current_xact_session->session_data( 'disconnect_cb' )
305 $pg->unset_xact_session;
310 __PACKAGE__->register_method(
311 method => 'pg_commit_xaction',
312 api_name => 'open-ils.storage.transaction.commit',
317 sub pg_rollback_xaction {
320 my $xact_id = $pg->current_xact_id;
322 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
323 $log->debug("Rolling back a trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
328 $log->debug("Failed to roll back trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
332 $pg->current_xact_session->unregister_callback(
333 $pg->current_xact_session->session_data( 'death_cb' )
336 if ($pg->current_xact_is_auto) {
337 $pg->current_xact_session->unregister_callback(
338 $pg->current_xact_session->session_data( 'disconnect_cb' )
342 $pg->unset_xact_session;
346 __PACKAGE__->register_method(
347 method => 'pg_rollback_xaction',
348 api_name => 'open-ils.storage.transaction.rollback',
356 #---------------------------------------------------------------------
357 package asset::call_number;
359 asset::call_number->table( 'asset.call_number' );
360 asset::call_number->sequence( 'asset.call_number_id_seq' );
362 #---------------------------------------------------------------------
365 asset::copy->table( 'asset.copy' );
366 asset::copy->sequence( 'asset.copy_id_seq' );
368 #---------------------------------------------------------------------
369 package biblio::record_entry;
371 biblio::record_entry->table( 'biblio.record_entry' );
372 biblio::record_entry->sequence( 'biblio.record_entry_id_seq' );
374 #---------------------------------------------------------------------
375 package biblio::record_node;
377 biblio::record_node->table( 'biblio.record_data' );
378 biblio::record_node->sequence( 'biblio.record_data_id_seq' );
380 #---------------------------------------------------------------------
381 package biblio::record_marc;
383 biblio::record_marc->table( 'biblio.record_marc' );
384 biblio::record_marc->sequence( 'biblio.record_marc_id_seq' );
386 #---------------------------------------------------------------------
387 package biblio::record_mods;
389 biblio::record_mods->table( 'biblio.record_mods' );
390 biblio::record_mods->sequence( 'biblio.record_mods_id_seq' );
392 #---------------------------------------------------------------------
393 package biblio::record_note;
395 biblio::record_note->table( 'biblio.record_note' );
396 biblio::record_note->sequence( 'biblio.record_note_id_seq' );
398 #---------------------------------------------------------------------
401 actor::user->table( 'actor.usr' );
402 actor::user->sequence( 'actor.usr_id_seq' );
404 #---------------------------------------------------------------------
405 package actor::org_unit_type;
407 actor::org_unit_type->table( 'actor.org_unit_type' );
408 actor::org_unit_type->sequence( 'actor.org_unit_type_id_seq' );
410 #---------------------------------------------------------------------
411 package actor::org_unit;
413 actor::org_unit_type->table( 'actor.org_unit' );
414 actor::org_unit_type->sequence( 'actor.org_unit_id_seq' );
416 #---------------------------------------------------------------------
418 #-------------------------------------------------------------------------------
419 package metabib::metarecord;
421 metabib::metarecord->table( 'metabib.metarecord' );
422 metabib::metarecord->sequence( 'metabib.metarecord_id_seq' );
424 #-------------------------------------------------------------------------------
426 #-------------------------------------------------------------------------------
427 package metabib::title_field_entry;
429 metabib::title_field_entry->table( 'metabib.title_field_entry' );
430 metabib::title_field_entry->sequence( 'metabib.title_field_entry_id_seq' );
431 metabib::title_field_entry->columns( 'FTS' => 'index_vector' );
433 # metabib::title_field_entry->add_trigger(
434 # before_create => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
436 # metabib::title_field_entry->add_trigger(
437 # before_update => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
440 #-------------------------------------------------------------------------------
442 #-------------------------------------------------------------------------------
443 package metabib::author_field_entry;
445 metabib::author_field_entry->table( 'metabib.author_field_entry' );
446 metabib::author_field_entry->sequence( 'metabib.author_field_entry_id_seq' );
447 metabib::author_field_entry->columns( 'FTS' => 'index_vector' );
449 #-------------------------------------------------------------------------------
451 #-------------------------------------------------------------------------------
452 package metabib::subject_field_entry;
454 metabib::subject_field_entry->table( 'metabib.subject_field_entry' );
455 metabib::subject_field_entry->sequence( 'metabib.subject_field_entry_id_seq' );
456 metabib::subject_field_entry->columns( 'FTS' => 'index_vector' );
458 #-------------------------------------------------------------------------------
460 #-------------------------------------------------------------------------------
461 package metabib::keyword_field_entry;
463 metabib::keyword_field_entry->table( 'metabib.keyword_field_entry' );
464 metabib::keyword_field_entry->sequence( 'metabib.keyword_field_entry_id_seq' );
465 metabib::keyword_field_entry->columns( 'FTS' => 'index_vector' );
467 #-------------------------------------------------------------------------------
469 #-------------------------------------------------------------------------------
470 package metabib::title_field_entry_source_map;
472 metabib::title_field_entry_source_map->table( 'metabib.title_field_entry_source_map' );
474 #-------------------------------------------------------------------------------
476 #-------------------------------------------------------------------------------
477 package metabib::author_field_entry_source_map;
479 metabib::author_field_entry_source_map->table( 'metabib.author_field_entry_source_map' );
481 #-------------------------------------------------------------------------------
483 #-------------------------------------------------------------------------------
484 package metabib::subject_field_entry_source_map;
486 metabib::subject_field_entry_source_map->table( 'metabib.subject_field_entry_source_map' );
488 #-------------------------------------------------------------------------------
490 #-------------------------------------------------------------------------------
491 package metabib::keyword_field_entry_source_map;
493 metabib::keyword_field_entry_source_map->table( 'metabib.keyword_field_entry_source_map' );
495 #-------------------------------------------------------------------------------
497 #-------------------------------------------------------------------------------
498 package metabib::metarecord_source_map;
500 metabib::metarecord_source_map->table( 'metabib.full_rec' );
502 #-------------------------------------------------------------------------------
504 #-------------------------------------------------------------------------------
505 package metabib::full_rec;
507 metabib::full_rec->table( 'metabib.full_rec' );
508 metabib::full_rec->sequence( 'metabib.full_rec_id_seq' );
509 metabib::full_rec->columns( 'FTS' => 'index_vector' );
511 #-------------------------------------------------------------------------------