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 $newterm = OpenILS::Application::Storage->driver->quote($newterm);
43 $self->{fts_query} = ["to_tsquery('default',$newterm)"];
44 $self->{fts_query_nots} = [];
45 $self->{fts_op} = '@@';
52 { # The driver package itself just needs a db_Main method (or db_Slaves if
53 #Class::DBI::Replication is in use) for Class::DBI to call.
55 # Any other fixups can go in here too... Also, the drivers should subclass the
56 # DBI driver that they are wrapping, or provide a 'quote()' method that calls
57 # the DBD::xxx::quote() method on FTI's behalf.
59 # The dirver MUST be a subclass of Class::DBI(::Replication) and
60 # OpenILS::Application::Storage.
61 #-------------------------------------------------------------------------------
62 package OpenILS::Application::Storage::Driver::Pg;
64 use base qw/Class::DBI OpenILS::Application::Storage/;
66 use OpenSRF::EX qw/:try/;
67 use OpenSRF::DomainObject::oilsResponse;
68 use OpenSRF::Utils::Logger qw/:level/;
69 my $log = 'OpenSRF::Utils::Logger';
71 __PACKAGE__->set_sql( retrieve_limited => 'SELECT * FROM __TABLE__ ORDER BY id LIMIT ?' );
80 $log->debug("Running child_init inside ".__PACKAGE__, INTERNAL);
82 $_db_params = [ $_db_params ] unless (ref($_db_params) eq 'ARRAY');
84 my %attrs = ( %{$self->_default_attributes},
85 RootClass => 'DBIx::ContextualFetch',
86 ShowErrorStatement => 1,
92 FetchHashKeyName => 'NAME_lc',
96 my $master = shift @$_db_params;
97 $master_db = DBI->connect("dbi:Pg:host=$$master{host};dbname=$$master{db}",$$master{user},$$master{pw}, \%attrs);
98 $master_db->do("SET NAMES '$$master{client_encoding}';") if ($$master{client_encoding});
100 $log->debug("Connected to MASTER db '$$master{db} at $$master{host}", INFO);
102 for my $db (@$_db_params) {
103 push @slave_dbs, DBI->connect("dbi:Pg:host=$$db{host};dbname=$$db{db}",$$db{user},$$db{pw}, \%attrs);
104 $slave_dbs[-1]->do("SET NAMES '$$db{client_encoding}';") if ($$master{client_encoding});
106 $log->debug("Connected to MASTER db '$$master{db} at $$master{host}", INFO);
109 $log->debug("All is well on the western front", INTERNAL);
114 return $master_db if ($self->current_xact_session);
115 return $master_db unless (@slave_dbs);
116 return ($master_db, @slave_dbs)[rand(scalar(@slave_dbs))];
120 return __PACKAGE__->db_Main->quote(@_)
123 # sub tsearch2_trigger {
125 # return unless ($self->value);
126 # $self->index_vector(
127 # $self->db_Slaves->selectrow_array(
128 # "SELECT to_tsvector('default',?);",
137 sub current_xact_session {
139 if (defined($_xact_session)) {
140 return $_xact_session;
145 sub current_xact_is_auto {
147 return $_xact_session->session_data(autocommit => shift());
150 sub current_xact_id {
152 if (defined($_xact_session) and ref($_xact_session)) {
153 return $_xact_session->session_id;
158 sub set_xact_session {
161 if (!defined($ses)) {
164 $_xact_session = $ses;
165 return $_xact_session;
168 sub unset_xact_session {
170 my $ses = $_xact_session;
171 undef $_xact_session;
179 package OpenILS::Application::Storage;
180 use OpenSRF::Utils::Logger;
181 my $log = 'OpenSRF::Utils::Logger';
183 my $pg = 'OpenILS::Application::Storage::Driver::Pg';
185 sub pg_begin_xaction {
189 if (my $old_xact = $pg->current_xact_session) {
190 if ($pg->current_xact_is_auto) {
191 $log->debug("Commiting old autocommit transaction with Open-ILS XACT-ID [$old_xact]", INFO);
192 $self->pg_commit_xaction($client);
194 $log->debug("Rolling back old NON-autocommit transaction with Open-ILS XACT-ID [$old_xact]", INFO);
195 $self->pg_rollback_xaction($client);
196 return new OpenSRF::DomainObject::oilsException (
198 status => "Previous transaction rolled back!",
203 $pg->set_xact_session( $client->session );
204 my $xact_id = $pg->current_xact_id;
206 $log->debug("Beginning a new trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
208 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
215 $log->debug("Failed to begin a new trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
220 my $death_cb = $client->session->register_callback(
222 __PACKAGE__->pg_rollback_xaction;
226 $log->debug("Registered 'death' callback [$death_cb] for new trasaction with Open-ILS XACT-ID [$xact_id]", DEBUG);
228 $client->session->session_data( death_cb => $death_cb );
230 if ($self->api_name =~ /autocommit$/o) {
231 $pg->current_xact_is_auto(1);
232 my $dc_cb = $client->session->register_callback(
235 $ses->unregister_callback($death_cb);
236 __PACKAGE__->pg_commit_xaction;
239 $log->debug("Registered 'disconnect' callback [$dc_cb] for new trasaction with Open-ILS XACT-ID [$xact_id]", DEBUG);
240 $client->session->session_data( disconnect_cb => $dc_cb );
246 __PACKAGE__->register_method(
247 method => 'pg_begin_xaction',
248 api_name => 'open-ils.storage.transaction.begin',
252 __PACKAGE__->register_method(
253 method => 'pg_begin_xaction',
254 api_name => 'open-ils.storage.transaction.begin.autocommit',
259 sub pg_commit_xaction {
264 $log->debug("Committing trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
265 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
270 $log->debug("Failed to commit trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
274 $pg->current_xact_session->unregister_callback(
275 $pg->current_xact_session->session_data( 'death_cb' )
278 if ($pg->current_xact_is_auto) {
279 $pg->current_xact_session->unregister_callback(
280 $pg->current_xact_session->session_data( 'disconnect_cb' )
284 $pg->unset_xact_session;
289 __PACKAGE__->register_method(
290 method => 'pg_commit_xaction',
291 api_name => 'open-ils.storage.transaction.commit',
296 sub pg_rollback_xaction {
299 my $xact_id = $pg->current_xact_id;
301 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
302 $log->debug("Rolling back a trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
307 $log->debug("Failed to roll back trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
311 $pg->current_xact_session->unregister_callback(
312 $pg->current_xact_session->session_data( 'death_cb' )
315 if ($pg->current_xact_is_auto) {
316 $pg->current_xact_session->unregister_callback(
317 $pg->current_xact_session->session_data( 'disconnect_cb' )
321 $pg->unset_xact_session;
325 __PACKAGE__->register_method(
326 method => 'pg_rollback_xaction',
327 api_name => 'open-ils.storage.transaction.rollback',
335 #---------------------------------------------------------------------
336 package asset::call_number;
338 asset::call_number->table( 'asset.call_number' );
339 asset::call_number->sequence( 'asset.call_number_id_seq' );
341 #---------------------------------------------------------------------
344 asset::copy->table( 'asset.copy' );
345 asset::copy->sequence( 'asset.copy_id_seq' );
347 #---------------------------------------------------------------------
348 package biblio::record_entry;
350 biblio::record_entry->table( 'biblio.record_entry' );
351 biblio::record_entry->sequence( 'biblio.record_entry_id_seq' );
353 #---------------------------------------------------------------------
354 package biblio::record_node;
356 biblio::record_node->table( 'biblio.record_data' );
357 biblio::record_node->sequence( 'biblio.record_data_id_seq' );
359 #---------------------------------------------------------------------
360 package biblio::record_mods;
362 biblio::record_mods->table( 'biblio.record_mods' );
363 biblio::record_mods->sequence( 'biblio.record_mods_id_seq' );
365 #---------------------------------------------------------------------
366 package biblio::record_note;
368 biblio::record_note->table( 'biblio.record_note' );
369 biblio::record_note->sequence( 'biblio.record_note_id_seq' );
371 #---------------------------------------------------------------------
374 actor::user->table( 'actor.usr' );
375 actor::user->sequence( 'actor.usr_id_seq' );
377 #---------------------------------------------------------------------
378 package actor::org_unit_type;
380 actor::org_unit_type->table( 'actor.org_unit_type' );
381 actor::org_unit_type->sequence( 'actor.org_unit_type_id_seq' );
383 #---------------------------------------------------------------------
384 package actor::org_unit;
386 actor::org_unit_type->table( 'actor.org_unit' );
387 actor::org_unit_type->sequence( 'actor.org_unit_id_seq' );
389 #---------------------------------------------------------------------
391 #-------------------------------------------------------------------------------
392 package metabib::metarecord;
394 metabib::metarecord->table( 'metabib.metarecord' );
395 metabib::metarecord->sequence( 'metabib.metarecord_id_seq' );
397 #-------------------------------------------------------------------------------
399 #-------------------------------------------------------------------------------
400 package metabib::title_field_entry;
402 metabib::title_field_entry->table( 'metabib.title_field_entry' );
403 metabib::title_field_entry->sequence( 'metabib.title_field_entry_id_seq' );
405 # metabib::title_field_entry->add_trigger(
406 # before_create => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
408 # metabib::title_field_entry->add_trigger(
409 # before_update => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
412 #-------------------------------------------------------------------------------
414 #-------------------------------------------------------------------------------
415 package metabib::author_field_entry;
417 metabib::author_field_entry->table( 'metabib.author_field_entry' );
418 metabib::author_field_entry->sequence( 'metabib.author_field_entry_id_seq' );
420 #-------------------------------------------------------------------------------
422 #-------------------------------------------------------------------------------
423 package metabib::subject_field_entry;
425 metabib::subject_field_entry->table( 'metabib.subject_field_entry' );
426 metabib::subject_field_entry->sequence( 'metabib.subject_field_entry_id_seq' );
428 #-------------------------------------------------------------------------------
430 #-------------------------------------------------------------------------------
431 package metabib::keyword_field_entry;
433 metabib::keyword_field_entry->table( 'metabib.keyword_field_entry' );
434 metabib::keyword_field_entry->sequence( 'metabib.keyword_field_entry_id_seq' );
436 #-------------------------------------------------------------------------------
438 #-------------------------------------------------------------------------------
439 package metabib::title_field_entry_source_map;
441 metabib::title_field_entry_source_map->table( 'metabib.title_field_entry_source_map' );
443 #-------------------------------------------------------------------------------
445 #-------------------------------------------------------------------------------
446 package metabib::author_field_entry_source_map;
448 metabib::author_field_entry_source_map->table( 'metabib.author_field_entry_source_map' );
450 #-------------------------------------------------------------------------------
452 #-------------------------------------------------------------------------------
453 package metabib::subject_field_entry_source_map;
455 metabib::subject_field_entry_source_map->table( 'metabib.subject_field_entry_source_map' );
457 #-------------------------------------------------------------------------------
459 #-------------------------------------------------------------------------------
460 package metabib::keyword_field_entry_source_map;
462 metabib::keyword_field_entry_source_map->table( 'metabib.keyword_field_entry_source_map' );
464 #-------------------------------------------------------------------------------
466 #-------------------------------------------------------------------------------
467 package metabib::metarecord_source_map;
469 metabib::metarecord_source_map->table( 'metabib.full_rec' );
471 #-------------------------------------------------------------------------------
473 #-------------------------------------------------------------------------------
474 package metabib::full_rec;
476 metabib::full_rec->table( 'metabib.full_rec' );
477 metabib::full_rec->sequence( 'metabib.full_rec_id_seq' );
479 #-------------------------------------------------------------------------------