]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Driver/Pg.pm
lots of cleanup... no testing yet, no data to test
[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 for Class::DBI to call.
53   # any other fixups can go in here too... Also, the drivers should subclass the
54   # DBI driver that they are wrapping, or provide a 'quote()' method that calls
55   # the DBD::xxx::quote() method on FTI's behalf.
56   #
57   # The dirver MUST be a subclass of Class::DBI.
58         #-------------------------------------------------------------------------------
59         package OpenILS::Application::Storage::Driver::Pg;
60         use base qw/Class::DBI OpenILS::Application::Storage/;
61         use DBI;
62         use DBD::Pg;
63         use OpenSRF::Utils::Logger qw/:level/;
64
65         __PACKAGE__->set_sql( retrieve_limited => 'SELECT * FROM __TABLE__ ORDER BY id LIMIT ?' );
66
67         my $_db_params;
68         sub child_init {
69                 my $self = shift;
70                 $_db_params = shift;
71         }
72
73         my $_dbh;
74         sub db_Main {   
75                 my $self = shift;
76
77                 return $_dbh if (defined $_dbh and $_dbh->ping);
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                 $log->debug(" Default attributes for this DB connection are:\n\t".join("\n\t",map { "$_\t==> $attrs{$_}" } keys %attrs), INTERNAL);
92
93                 $_dbh = DBI->connect( "dbi:Pg:host=$_db_params{host};dbname=$_db_params{database}",$_db_params{user},$_db_params{pw}, \%attrs );
94                 $_dbh->do("SET CLIENT_ENCODING TO 'SQL_ASCII';");
95
96                 return $_dbh;
97         }
98
99         sub quote {
100                 return $_dbh->quote(@_)
101         }
102 }
103
104 {
105         #---------------------------------------------------------------------
106         package asset::call_number;
107         
108         __PACKAGE__->table( 'asset.call_number' );
109         __PACKAGE__->sequence( 'asset.call_number_id_seq' );
110         
111         #---------------------------------------------------------------------
112         package asset::copy;
113         
114         __PACKAGE__->table( 'asset.copy' );
115         __PACKAGE__->sequence( 'asset.copy_id_seq' );
116         
117         #---------------------------------------------------------------------
118         package biblio::record_entry;
119         
120         __PACKAGE__->table( 'biblio.record_entry' );
121         __PACKAGE__->sequence( 'biblio.record_entry_id_seq' );
122         
123         #---------------------------------------------------------------------
124         package biblio::record_node;
125         
126         __PACKAGE__->table( 'biblio.record_data' );
127         __PACKAGE__->sequence( 'biblio.record_data_id_seq' );
128         
129         #---------------------------------------------------------------------
130         package biblio::record_note;
131         
132         __PACKAGE__->table( 'biblio.record_note' );
133         __PACKAGE__->sequence( 'biblio.record_note_id_seq' );
134         
135         #---------------------------------------------------------------------
136         package actor::usr;
137         
138         __PACKAGE__->table( 'actor.usr' );
139         __PACKAGE__->sequence( 'actor.usr_id_seq' );
140         
141         #---------------------------------------------------------------------
142         package actor::org_unit_type;
143         
144         __PACKAGE__->table( 'actor.org_unit_type' );
145         __PACKAGE__->sequence( 'actor.org_unit_type_id_seq' );
146         
147         #---------------------------------------------------------------------
148         
149 }
150
151 1;