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;
63 use Class::DBI::Replication;
64 use base qw/Class::DBI::Replication OpenILS::Application::Storage/;
66 use OpenSRF::EX qw/:try/;
67 use OpenSRF::Utils::Logger qw/:level/;
68 my $log = 'OpenSRF::Utils::Logger';
70 __PACKAGE__->set_sql( retrieve_limited => 'SELECT * FROM __TABLE__ ORDER BY id LIMIT ?' );
77 $_db_params = [ $_db_params ] unless (ref($_db_params) eq 'ARRAY');
79 my %attrs = ( %{$self->_default_attributes},
80 RootClass => 'DBIx::ContextualFetch',
81 ShowErrorStatement => 1,
87 FetchHashKeyName => 'NAME_lc',
92 for my $db (@$_db_params) {
93 if ($db->{type} eq 'master') {
94 __PACKAGE__->set_master("dbi:Pg:host=$$db{host};dbname=$$db{db}",$$db{user},$$db{pw}, \%attrs);
96 push @slaves, ["dbi:Pg:host=$$db{host};dbname=$$db{db}",$$db{user},$$db{pw}, \%attrs];
99 __PACKAGE__->set_slaves(@slaves);
101 $log->debug("Running child_init inside ".__PACKAGE__, INTERNAL);
105 return __PACKAGE->db_Slaves->quote(@_)
108 sub tsearch2_trigger {
110 return unless ($self->value);
112 $self->db_Slaves->selectrow_array(
113 "SELECT to_tsvector('default',?);",
124 if ($_xact_session && OpenSRF::AppSession->find($_xact_session)) {
125 return $self->db_Main;
128 return $self->SUPER::db_Slaves
131 sub pg_begin_xaction {
135 $_xact_session = $client->session->session_id;
137 $client->session->register_callback( disconnect => sub { __PACKAGE__->commit_xaction($client); } )
138 if ($self->api_name =~ /autocommit$/o);
140 $client->session->register_callback( death => sub { __PACKAGE__->rollback_xaction($client); } );
142 return $self->begin_xaction;
144 __PACKAGE__->register_method(
145 method => 'pg_begin_xaction',
146 api_name => 'open-ils.storage.transaction.begin',
150 __PACKAGE__->register_method(
151 method => 'pg_begin_xaction',
152 api_name => 'open-ils.storage.transaction.begin.autocommit',
157 sub pg_commit_xaction {
159 $_xact_session = undef;
160 return $self->commit_xaction(@_);
162 __PACKAGE__->register_method(
163 method => 'pg_commit_xaction',
164 api_name => 'open-ils.storage.transaction.commit',
169 sub pg_rollback_xaction {
171 $_xact_session = undef;
172 return $self->rollback_xaction(@_);
174 __PACKAGE__->register_method(
175 method => 'pg_rollback_xaction',
176 api_name => 'open-ils.storage.transaction.rollback',
184 #---------------------------------------------------------------------
185 package asset::call_number;
187 asset::call_number->table( 'asset.call_number' );
188 asset::call_number->sequence( 'asset.call_number_id_seq' );
190 #---------------------------------------------------------------------
193 asset::copy->table( 'asset.copy' );
194 asset::copy->sequence( 'asset.copy_id_seq' );
196 #---------------------------------------------------------------------
197 package biblio::record_entry;
199 biblio::record_entry->table( 'biblio.record_entry' );
200 biblio::record_entry->sequence( 'biblio.record_entry_id_seq' );
202 #---------------------------------------------------------------------
203 package biblio::record_node;
205 biblio::record_node->table( 'biblio.record_data' );
206 biblio::record_node->sequence( 'biblio.record_data_id_seq' );
208 #---------------------------------------------------------------------
209 package biblio::record_note;
211 biblio::record_note->table( 'biblio.record_note' );
212 biblio::record_note->sequence( 'biblio.record_note_id_seq' );
214 #---------------------------------------------------------------------
217 actor::user->table( 'actor.usr' );
218 actor::user->sequence( 'actor.usr_id_seq' );
220 #---------------------------------------------------------------------
221 package actor::org_unit_type;
223 actor::org_unit_type->table( 'actor.org_unit_type' );
224 actor::org_unit_type->sequence( 'actor.org_unit_type_id_seq' );
226 #---------------------------------------------------------------------
228 #-------------------------------------------------------------------------------
229 package metabib::metarecord;
231 metabib::metarecord->table( 'metabib.metarecord' );
232 metabib::metarecord->sequence( 'metabib.metarecord_id_seq' );
234 #-------------------------------------------------------------------------------
236 #-------------------------------------------------------------------------------
237 package metabib::title_field_entry;
239 metabib::title_field_entry->table( 'metabib.title_field_entry' );
240 metabib::title_field_entry->sequence( 'metabib.title_field_entry_id_seq' );
241 metabib::title_field_entry->columns( Primary => qw/id/ );
242 metabib::title_field_entry->columns( Essential => qw/id/ );
243 metabib::title_field_entry->columns( Others => qw/field value index_vector/ );
245 metabib::title_field_entry->add_trigger(
246 before_create => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
248 metabib::title_field_entry->add_trigger(
249 before_update => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
252 #-------------------------------------------------------------------------------
254 #-------------------------------------------------------------------------------
255 package metabib::author_field_entry;
257 metabib::author_field_entry->table( 'metabib.author_field_entry' );
258 metabib::author_field_entry->sequence( 'metabib.author_field_entry_id_seq' );
259 metabib::author_field_entry->columns( Primary => qw/id/ );
260 metabib::author_field_entry->columns( Essential => qw/id/ );
261 metabib::author_field_entry->columns( Others => qw/field value index_vector/ );
263 metabib::author_field_entry->add_trigger(
264 before_create => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
266 metabib::author_field_entry->add_trigger(
267 before_update => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
270 #-------------------------------------------------------------------------------
272 #-------------------------------------------------------------------------------
273 package metabib::subject_field_entry;
275 metabib::subject_field_entry->table( 'metabib.subject_field_entry' );
276 metabib::subject_field_entry->sequence( 'metabib.subject_field_entry_id_seq' );
278 metabib::subject_field_entry->add_trigger(
279 before_create => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
281 metabib::subject_field_entry->add_trigger(
282 before_update => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
285 #-------------------------------------------------------------------------------
287 #-------------------------------------------------------------------------------
288 package metabib::keyword_field_entry;
290 metabib::keyword_field_entry->table( 'metabib.keyword_field_entry' );
291 metabib::keyword_field_entry->sequence( 'metabib.keyword_field_entry_id_seq' );
293 metabib::keyword_field_entry->add_trigger(
294 before_create => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
296 metabib::keyword_field_entry->add_trigger(
297 before_update => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
300 #-------------------------------------------------------------------------------
302 #-------------------------------------------------------------------------------
303 package metabib::title_field_entry_source_map;
305 metabib::title_field_entry_source_map->table( 'metabib.title_field_entry_source_map' );
306 metabib::title_field_entry_source_map->table( 'metabib.title_field_entry_source_map_id_seq' );
308 #-------------------------------------------------------------------------------
310 #-------------------------------------------------------------------------------
311 package metabib::author_field_entry_source_map;
313 metabib::author_field_entry_source_map->table( 'metabib.author_field_entry_source_map' );
314 metabib::author_field_entry_source_map->sequence( 'metabib.author_field_entry_source_map_id_seq' );
316 #-------------------------------------------------------------------------------
318 #-------------------------------------------------------------------------------
319 package metabib::subject_field_entry_source_map;
321 metabib::subject_field_entry_source_map->table( 'metabib.subject_field_entry_source_map' );
322 metabib::subject_field_entry_source_map->sequence( 'metabib.subject_field_entry_source_map_id_seq' );
324 #-------------------------------------------------------------------------------