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 if ($client and $client->session) {
241 $client->session->session_data( disconnect_cb => $dc_cb );
248 __PACKAGE__->register_method(
249 method => 'pg_begin_xaction',
250 api_name => 'open-ils.storage.transaction.begin',
254 __PACKAGE__->register_method(
255 method => 'pg_begin_xaction',
256 api_name => 'open-ils.storage.transaction.begin.autocommit',
261 sub pg_commit_xaction {
266 $log->debug("Committing trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
267 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
272 $log->debug("Failed to commit trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
276 $pg->current_xact_session->unregister_callback(
277 $pg->current_xact_session->session_data( 'death_cb' )
280 if ($pg->current_xact_is_auto) {
281 $pg->current_xact_session->unregister_callback(
282 $pg->current_xact_session->session_data( 'disconnect_cb' )
286 $pg->unset_xact_session;
291 __PACKAGE__->register_method(
292 method => 'pg_commit_xaction',
293 api_name => 'open-ils.storage.transaction.commit',
298 sub pg_rollback_xaction {
301 my $xact_id = $pg->current_xact_id;
303 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
304 $log->debug("Rolling back a trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
309 $log->debug("Failed to roll back trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
313 $pg->current_xact_session->unregister_callback(
314 $pg->current_xact_session->session_data( 'death_cb' )
317 if ($pg->current_xact_is_auto) {
318 $pg->current_xact_session->unregister_callback(
319 $pg->current_xact_session->session_data( 'disconnect_cb' )
323 $pg->unset_xact_session;
327 __PACKAGE__->register_method(
328 method => 'pg_rollback_xaction',
329 api_name => 'open-ils.storage.transaction.rollback',
337 #---------------------------------------------------------------------
338 package asset::call_number;
340 asset::call_number->table( 'asset.call_number' );
341 asset::call_number->sequence( 'asset.call_number_id_seq' );
343 #---------------------------------------------------------------------
346 asset::copy->table( 'asset.copy' );
347 asset::copy->sequence( 'asset.copy_id_seq' );
349 #---------------------------------------------------------------------
350 package biblio::record_entry;
352 biblio::record_entry->table( 'biblio.record_entry' );
353 biblio::record_entry->sequence( 'biblio.record_entry_id_seq' );
355 #---------------------------------------------------------------------
356 package biblio::record_node;
358 biblio::record_node->table( 'biblio.record_data' );
359 biblio::record_node->sequence( 'biblio.record_data_id_seq' );
361 #---------------------------------------------------------------------
362 package biblio::record_marc;
364 biblio::record_marc->table( 'biblio.record_marc' );
365 biblio::record_marc->sequence( 'biblio.record_marc_id_seq' );
367 #---------------------------------------------------------------------
368 package biblio::record_mods;
370 biblio::record_mods->table( 'biblio.record_mods' );
371 biblio::record_mods->sequence( 'biblio.record_mods_id_seq' );
373 #---------------------------------------------------------------------
374 package biblio::record_note;
376 biblio::record_note->table( 'biblio.record_note' );
377 biblio::record_note->sequence( 'biblio.record_note_id_seq' );
379 #---------------------------------------------------------------------
382 actor::user->table( 'actor.usr' );
383 actor::user->sequence( 'actor.usr_id_seq' );
385 #---------------------------------------------------------------------
386 package actor::org_unit_type;
388 actor::org_unit_type->table( 'actor.org_unit_type' );
389 actor::org_unit_type->sequence( 'actor.org_unit_type_id_seq' );
391 #---------------------------------------------------------------------
392 package actor::org_unit;
394 actor::org_unit_type->table( 'actor.org_unit' );
395 actor::org_unit_type->sequence( 'actor.org_unit_id_seq' );
397 #---------------------------------------------------------------------
399 #-------------------------------------------------------------------------------
400 package metabib::metarecord;
402 metabib::metarecord->table( 'metabib.metarecord' );
403 metabib::metarecord->sequence( 'metabib.metarecord_id_seq' );
405 #-------------------------------------------------------------------------------
407 #-------------------------------------------------------------------------------
408 package metabib::title_field_entry;
410 metabib::title_field_entry->table( 'metabib.title_field_entry' );
411 metabib::title_field_entry->sequence( 'metabib.title_field_entry_id_seq' );
413 # metabib::title_field_entry->add_trigger(
414 # before_create => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
416 # metabib::title_field_entry->add_trigger(
417 # before_update => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
420 #-------------------------------------------------------------------------------
422 #-------------------------------------------------------------------------------
423 package metabib::author_field_entry;
425 metabib::author_field_entry->table( 'metabib.author_field_entry' );
426 metabib::author_field_entry->sequence( 'metabib.author_field_entry_id_seq' );
428 #-------------------------------------------------------------------------------
430 #-------------------------------------------------------------------------------
431 package metabib::subject_field_entry;
433 metabib::subject_field_entry->table( 'metabib.subject_field_entry' );
434 metabib::subject_field_entry->sequence( 'metabib.subject_field_entry_id_seq' );
436 #-------------------------------------------------------------------------------
438 #-------------------------------------------------------------------------------
439 package metabib::keyword_field_entry;
441 metabib::keyword_field_entry->table( 'metabib.keyword_field_entry' );
442 metabib::keyword_field_entry->sequence( 'metabib.keyword_field_entry_id_seq' );
444 #-------------------------------------------------------------------------------
446 #-------------------------------------------------------------------------------
447 package metabib::title_field_entry_source_map;
449 metabib::title_field_entry_source_map->table( 'metabib.title_field_entry_source_map' );
451 #-------------------------------------------------------------------------------
453 #-------------------------------------------------------------------------------
454 package metabib::author_field_entry_source_map;
456 metabib::author_field_entry_source_map->table( 'metabib.author_field_entry_source_map' );
458 #-------------------------------------------------------------------------------
460 #-------------------------------------------------------------------------------
461 package metabib::subject_field_entry_source_map;
463 metabib::subject_field_entry_source_map->table( 'metabib.subject_field_entry_source_map' );
465 #-------------------------------------------------------------------------------
467 #-------------------------------------------------------------------------------
468 package metabib::keyword_field_entry_source_map;
470 metabib::keyword_field_entry_source_map->table( 'metabib.keyword_field_entry_source_map' );
472 #-------------------------------------------------------------------------------
474 #-------------------------------------------------------------------------------
475 package metabib::metarecord_source_map;
477 metabib::metarecord_source_map->table( 'metabib.full_rec' );
479 #-------------------------------------------------------------------------------
481 #-------------------------------------------------------------------------------
482 package metabib::full_rec;
484 metabib::full_rec->table( 'metabib.full_rec' );
485 metabib::full_rec->sequence( 'metabib.full_rec_id_seq' );
487 #-------------------------------------------------------------------------------