]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Driver/Pg.pm
general bug fixes; column name fixups and whatnot
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Driver / Pg.pm
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         #-------------------------------------------------------------------------------
4         use Class::DBI;
5         package Class::DBI;
6
7         sub search_fti {
8                 my $self = shift;
9                 my @args = @_;
10                 if (ref($args[-1]) eq 'HASH') {
11                         $args[-1]->{_placeholder} = "to_tsquery('default',?)";
12                 } else {
13                         push @args, {_placeholder => "to_tsquery('default',?)"};
14                 }
15                 $self->_do_search("@@"  => @args);
16         }
17 }
18
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';
25
26         sub compile {
27                 my $self = shift;
28                 my $term = shift;
29
30                 $self = ref($self) || $self;
31                 $self = bless {} => $self;
32
33                 $self->decompose($term);
34
35                 my $newterm = join('&', $self->words);
36
37                 if ($self->nots) {
38                         $newterm = '('.$newterm.')&('. join('|', $self->nots) . ')';
39                 }
40
41                 $newterm = OpenILS::Application::Storage->driver->quote($newterm);
42
43                 $self->{fts_query} = ["to_tsquery('default',$newterm)"];
44                 $self->{fts_query_nots} = [];
45                 $self->{fts_op} = '@@';
46
47                 return $self;
48         }
49 }
50
51
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.
54   #
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.
58   #
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/;
65         use DBI;
66         use OpenSRF::EX qw/:try/;
67         use OpenSRF::Utils::Logger qw/:level/;
68         my $log = 'OpenSRF::Utils::Logger';
69
70         __PACKAGE__->set_sql( retrieve_limited => 'SELECT * FROM __TABLE__ ORDER BY id LIMIT ?' );
71
72         my $_db_params;
73         sub child_init {
74                 my $self = shift;
75                 $_db_params = shift;
76
77                 $_db_params = [ $_db_params ] unless (ref($_db_params) eq 'ARRAY');
78
79                 my %attrs = (   %{$self->_default_attributes},
80                                 RootClass => 'DBIx::ContextualFetch',
81                                 ShowErrorStatement => 1,
82                                 RaiseError => 1,
83                                 AutoCommit => 1,
84                                 PrintError => 1,
85                                 Taint => 1,
86                                 pg_enable_utf8 => 1,
87                                 FetchHashKeyName => 'NAME_lc',
88                                 ChopBlanks => 1,
89                 );
90
91                 my ($master,@slaves);
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);
95                         }
96                         push @slaves, ["dbi:Pg:host=$$db{host};dbname=$$db{db}",$$db{user},$$db{pw}, \%attrs];
97                 }
98
99                 __PACKAGE__->set_slaves(@slaves);
100
101                 $log->debug("Running child_init inside ".__PACKAGE__, INTERNAL);
102         }
103
104         sub quote {
105                 return __PACKAGE->db_Slaves->quote(@_)
106         }
107
108         sub tsearch2_trigger {
109                 my $self = shift;
110                 return unless ($self->value);
111                 $self->index_vector(
112                         $self->db_Slaves->selectrow_array(
113                                 "SELECT to_tsvector('default',?);",
114                                 {},
115                                 $self->value
116                         )
117                 );
118         }
119
120         my $_xact_session;
121         sub db_Slaves { 
122                 my $self = shift;
123
124                 if ($_xact_session && OpenSRF::AppSession->find($_xact_session)) {
125                         return $self->db_Main;
126                 }
127
128                 return $self->SUPER::db_Slaves
129         }
130
131         sub pg_begin_xaction {
132                 my $self = shift;
133                 my $client = shift;
134
135                 $_xact_session = $client->session->session_id;
136
137                 $client->session->register_callback( disconnect => sub { __PACKAGE__->commit_xaction($client); } )
138                         if ($self->api_name =~ /autocommit$/o);
139
140                 $client->session->register_callback( death => sub { __PACKAGE__->rollback_xaction($client); } );
141
142                 return $self->begin_xaction;
143         }
144         __PACKAGE__->register_method(
145                 method          => 'pg_begin_xaction',
146                 api_name        => 'open-ils.storage.transaction.begin',
147                 api_level       => 1,
148                 argc            => 0,
149         );
150         __PACKAGE__->register_method(
151                 method          => 'pg_begin_xaction',
152                 api_name        => 'open-ils.storage.transaction.begin.autocommit',
153                 api_level       => 1,
154                 argc            => 0,
155         );
156
157         sub pg_commit_xaction {
158
159                 $_xact_session = undef;
160                 return $self->commit_xaction(@_);
161         }
162         __PACKAGE__->register_method(
163                 method          => 'pg_commit_xaction',
164                 api_name        => 'open-ils.storage.transaction.commit',
165                 api_level       => 1,
166                 argc            => 0,
167         );
168
169         sub pg_rollback_xaction {
170
171                 $_xact_session = undef;
172                 return $self->rollback_xaction(@_);
173         }
174         __PACKAGE__->register_method(
175                 method          => 'pg_rollback_xaction',
176                 api_name        => 'open-ils.storage.transaction.rollback',
177                 api_level       => 1,
178                 argc            => 0,
179         );
180
181 }
182
183 {
184         #---------------------------------------------------------------------
185         package asset::call_number;
186         
187         asset::call_number->table( 'asset.call_number' );
188         asset::call_number->sequence( 'asset.call_number_id_seq' );
189         
190         #---------------------------------------------------------------------
191         package asset::copy;
192         
193         asset::copy->table( 'asset.copy' );
194         asset::copy->sequence( 'asset.copy_id_seq' );
195         
196         #---------------------------------------------------------------------
197         package biblio::record_entry;
198         
199         biblio::record_entry->table( 'biblio.record_entry' );
200         biblio::record_entry->sequence( 'biblio.record_entry_id_seq' );
201
202         #---------------------------------------------------------------------
203         package biblio::record_node;
204         
205         biblio::record_node->table( 'biblio.record_data' );
206         biblio::record_node->sequence( 'biblio.record_data_id_seq' );
207         
208         #---------------------------------------------------------------------
209         package biblio::record_note;
210         
211         biblio::record_note->table( 'biblio.record_note' );
212         biblio::record_note->sequence( 'biblio.record_note_id_seq' );
213         
214         #---------------------------------------------------------------------
215         package actor::user;
216         
217         actor::user->table( 'actor.usr' );
218         actor::user->sequence( 'actor.usr_id_seq' );
219         
220         #---------------------------------------------------------------------
221         package actor::org_unit_type;
222         
223         actor::org_unit_type->table( 'actor.org_unit_type' );
224         actor::org_unit_type->sequence( 'actor.org_unit_type_id_seq' );
225         
226         #---------------------------------------------------------------------
227         
228         #-------------------------------------------------------------------------------
229         package metabib::metarecord;
230
231         metabib::metarecord->table( 'metabib.metarecord' );
232         metabib::metarecord->sequence( 'metabib.metarecord_id_seq' );
233
234         #-------------------------------------------------------------------------------
235
236         #-------------------------------------------------------------------------------
237         package metabib::title_field_entry;
238
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/ );
244
245         metabib::title_field_entry->add_trigger(
246                 before_create => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
247         );
248         metabib::title_field_entry->add_trigger(
249                 before_update => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
250         );
251
252         #-------------------------------------------------------------------------------
253
254         #-------------------------------------------------------------------------------
255         package metabib::author_field_entry;
256
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/ );
262
263         metabib::author_field_entry->add_trigger(
264                 before_create => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
265         );
266         metabib::author_field_entry->add_trigger(
267                 before_update => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
268         );
269
270         #-------------------------------------------------------------------------------
271
272         #-------------------------------------------------------------------------------
273         package metabib::subject_field_entry;
274
275         metabib::subject_field_entry->table( 'metabib.subject_field_entry' );
276         metabib::subject_field_entry->sequence( 'metabib.subject_field_entry_id_seq' );
277
278         metabib::subject_field_entry->add_trigger(
279                 before_create => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
280         );
281         metabib::subject_field_entry->add_trigger(
282                 before_update => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
283         );
284
285         #-------------------------------------------------------------------------------
286
287         #-------------------------------------------------------------------------------
288         package metabib::keyword_field_entry;
289
290         metabib::keyword_field_entry->table( 'metabib.keyword_field_entry' );
291         metabib::keyword_field_entry->sequence( 'metabib.keyword_field_entry_id_seq' );
292
293         metabib::keyword_field_entry->add_trigger(
294                 before_create => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
295         );
296         metabib::keyword_field_entry->add_trigger(
297                 before_update => \&OpenILS::Application::Storage::Driver::Pg::tsearch2_trigger
298         );
299
300         #-------------------------------------------------------------------------------
301
302         #-------------------------------------------------------------------------------
303         package metabib::title_field_entry_source_map;
304
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' );
307
308         #-------------------------------------------------------------------------------
309
310         #-------------------------------------------------------------------------------
311         package metabib::author_field_entry_source_map;
312
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' );
315
316         #-------------------------------------------------------------------------------
317
318         #-------------------------------------------------------------------------------
319         package metabib::subject_field_entry_source_map;
320
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' );
323
324         #-------------------------------------------------------------------------------
325 }
326
327 1;