]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Driver/Pg.pm
disabling true server-side prepared queries (until we test them more)
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Driver / Pg.pm
1
2 { # The driver package itself just needs a db_Main method (or db_Slaves if
3   #Class::DBI::Replication is in use) for Class::DBI to call.
4   #
5   # Any other fixups can go in here too... Also, the drivers should subclass the
6   # DBI driver that they are wrapping, or provide a 'quote()' method that calls
7   # the DBD::xxx::quote() method on FTI's behalf.
8   #
9   # The dirver MUST be a subclass of Class::DBI(::Replication) and
10   # OpenILS::Application::Storage.
11   #-------------------------------------------------------------------------------
12         package OpenILS::Application::Storage::Driver::Pg;
13         use OpenILS::Application::Storage::Driver::Pg::cdbi;
14         use OpenILS::Application::Storage::Driver::Pg::fts;
15         use OpenILS::Application::Storage::Driver::Pg::storage;
16         use OpenILS::Application::Storage::Driver::Pg::dbi;
17         use Class::DBI;
18         use base qw/Class::DBI OpenILS::Application::Storage/;
19         use DBI;
20         use OpenSRF::EX qw/:try/;
21         use OpenSRF::DomainObject::oilsResponse;
22         use OpenSRF::Utils::Logger qw/:level/;
23         my $log = 'OpenSRF::Utils::Logger';
24
25         __PACKAGE__->set_sql( retrieve_limited => 'SELECT * FROM __TABLE__ ORDER BY id LIMIT ?' );
26         __PACKAGE__->set_sql( copy_start => 'COPY %s (%s) FROM STDIN;' );
27         __PACKAGE__->set_sql( copy_end => '\.' );
28
29         my $master_db;
30         my @slave_dbs;
31         my $_db_params;
32         sub child_init {
33                 my $self = shift;
34                 $_db_params = shift;
35
36                 $log->debug("Running child_init inside ".__PACKAGE__, INTERNAL);
37
38                 $_db_params = [ $_db_params ] unless (ref($_db_params) eq 'ARRAY');
39
40                 my %attrs = (   %{$self->_default_attributes},
41                                 RootClass => 'DBIx::ContextualFetch',
42                                 ShowErrorStatement => 1,
43                                 RaiseError => 1,
44                                 AutoCommit => 1,
45                                 PrintError => 1,
46                                 Taint => 1,
47                                 pg_enable_utf8 => 1,
48                                 pg_server_prepare => 0,
49                                 FetchHashKeyName => 'NAME_lc',
50                                 ChopBlanks => 1,
51                 );
52
53                 my $master = shift @$_db_params;
54                 $log->debug("Attmpting to connet to $$master{db} at $$master{host}", INFO);
55
56                 try {
57                         $master_db = DBI->connect("dbi:Pg:host=$$master{host};dbname=$$master{db}",$$master{user},$$master{pw}, \%attrs) ||
58                                 throw OpenSRF::EX::ERROR ("Couldn't connect to $$master{db} on $$master{host} as $$master{user}!!");
59                 } catch Error with {
60                         my $e = shift;
61                         $log->debug("Error connecting to database:\n\t$e\n\t$DBI::errstr", ERROR);
62                         throw $e;
63                 };
64
65                 $log->debug("Connected to MASTER db $$master{db} at $$master{host}", INFO);
66                 
67                 $master_db->do("SET NAMES '$$master{client_encoding}';") if ($$master{client_encoding});
68
69                 for my $db (@$_db_params) {
70                         push @slave_dbs, DBI->connect("dbi:Pg:host=$$db{host};dbname=$$db{db}",$$db{user},$$db{pw}, \%attrs);
71                         $slave_dbs[-1]->do("SET NAMES '$$db{client_encoding}';") if ($$master{client_encoding});
72
73                         $log->debug("Connected to MASTER db '$$master{db} at $$master{host}", INFO);
74                 }
75
76                 $log->debug("All is well on the western front", INTERNAL);
77         }
78
79         sub db_Main {
80                 my $self = shift;
81                 return $master_db if ($self->current_xact_session);
82                 return $master_db unless (@slave_dbs);
83                 return ($master_db, @slave_dbs)[rand(scalar(@slave_dbs))];
84         }
85
86         sub quote {
87                 my $self = shift;
88                 return $self->db_Main->quote(@_)
89         }
90
91 #       sub tsearch2_trigger {
92 #               my $self = shift;
93 #               return unless ($self->value);
94 #               $self->index_vector(
95 #                       $self->db_Slaves->selectrow_array(
96 #                               "SELECT to_tsvector('default',?);",
97 #                               {},
98 #                               $self->value
99 #                       )
100 #               );
101 #       }
102
103         my $_xact_session;
104
105         sub current_xact_session {
106                 my $self = shift;
107                 if (defined($_xact_session)) {
108                         return $_xact_session;
109                 }
110                 return undef;
111         }
112
113         sub current_xact_is_auto {
114                 my $self = shift;
115                 my $auto = shift;
116                 if (defined($_xact_session) and ref($_xact_session)) {
117                         if (defined $auto) {
118                                 $_xact_session->session_data(autocommit => $auto);
119                         }
120                         return $_xact_session->session_data('autocommit'); 
121                 }
122         }
123
124         sub current_xact_id {
125                 my $self = shift;
126                 if (defined($_xact_session) and ref($_xact_session)) {
127                         return $_xact_session->session_id;
128                 }
129                 return undef;
130         }
131
132         sub set_xact_session {
133                 my $self = shift;
134                 my $ses = shift;
135                 if (!defined($ses)) {
136                         return undef;
137                 }
138                 $_xact_session = $ses;
139                 return $_xact_session;
140         }
141
142         sub unset_xact_session {
143                 my $self = shift;
144                 my $ses = $_xact_session;
145                 undef $_xact_session;
146                 return $ses;
147         }
148
149 }
150
151 1;