2 { # The driver package itself just needs a db_Main method.
4 # Any other fixups can go in here too... Also, the drivers should subclass the
5 # DBI driver that they are wrapping, or provide a 'quote()' method that calls
6 # the DBD::xxx::quote() method on FTI's behalf.
8 # The dirver MUST be a subclass of Class::DBI and
9 # OpenILS::Application::Storage.
10 #-------------------------------------------------------------------------------
11 package OpenILS::Application::Storage::Driver::Pg;
12 # The following modules add, or use, subroutines in modules that
13 # are not available when this module is compiled. We therefore
14 # "require" these modules rather than "use" them. Everything is
15 # available at run time.
16 require OpenILS::Application::Storage::Driver::Pg::cdbi;
17 require OpenILS::Application::Storage::Driver::Pg::fts;
18 require OpenILS::Application::Storage::Driver::Pg::storage;
19 require OpenILS::Application::Storage::Driver::Pg::dbi;
21 use UNIVERSAL::require;
23 'Class::DBI::Frozen::301'->use or 'Class::DBI'->use or die $@;
25 use base qw/Class::DBI OpenILS::Application::Storage/;
27 use OpenSRF::EX qw/:try/;
28 use OpenSRF::DomainObject::oilsResponse;
29 use OpenSRF::Utils::Logger qw/:level/;
30 my $log = 'OpenSRF::Utils::Logger';
32 __PACKAGE__->set_sql( retrieve_limited => 'SELECT * FROM __TABLE__ ORDER BY id LIMIT ?' );
33 __PACKAGE__->set_sql( copy_start => 'COPY %s (%s) FROM STDIN;' );
34 __PACKAGE__->set_sql( copy_end => '\.' );
41 return ($primary_db, @standby_dbs);
48 $log->debug("Running child_init inside ".__PACKAGE__, INTERNAL);
50 $_db_params = [ $_db_params ] unless (ref($_db_params) eq 'ARRAY');
52 my %attrs = ( %{$self->_default_attributes},
53 RootClass => 'DBIx::ContextualFetch',
54 ShowErrorStatement => 1,
59 #TraceLevel => "1|SQL",
61 pg_server_prepare => 0,
62 FetchHashKeyName => 'NAME_lc',
66 my $primary = shift @$_db_params;
67 $$primary{port} ||= '5432';
68 $$primary{host} ||= 'localhost';
69 $$primary{db} ||= 'openils';
71 $log->debug("Attempting to connect to $$primary{db} at $$primary{host}", INFO);
74 $primary_db = DBI->connect(
76 "host=$$primary{host};".
77 "port=$$primary{port};".
78 "dbname=$$primary{db}".
79 ($$primary{application_name} ? ";application_name='$$primary{application_name}'": ""),
86 "host=$$primary{host};".
87 "port=$$primary{port};".
88 "dbname=$$primary{db}".
89 ($$primary{application_name} ? ";application_name='$$primary{application_name}'": ""),
93 || throw OpenSRF::EX::ERROR
94 ("Couldn't connect to $$primary{db}".
95 " on $$primary{host}::$$primary{port}".
96 " as $$primary{user}!!");
99 $log->debug("Error connecting to database:\n\t$e\n\t$DBI::errstr", ERROR);
103 $log->debug("Connected to primary db $$primary{db} at $$primary{host}", INFO);
105 $primary_db->do("SET NAMES '$$primary{client_encoding}';") if ($$primary{client_encoding});
107 for my $db (@$_db_params) {
109 push @standby_dbs, DBI->connect("dbi:Pg:host=$$db{host};port=$$db{port};dbname=$$db{db}". ($$db{application_name} ? ";application_name='$$db{application_name}'" : ""),$$db{user},$$db{pw}, \%attrs)
110 || do { sleep(1); DBI->connect("dbi:Pg:host=$$db{host};port=$$db{port};dbname=$$db{db}". ($$db{application_name} ? ";application_name='$$db{application_name}'" : ""),$$db{user},$$db{pw}, \%attrs) }
111 || throw OpenSRF::EX::ERROR
112 ("Couldn't connect to $$db{db}".
113 " on $$db{host}::$$db{port}".
117 $log->debug("Error connecting to database:\n\t$e\n\t$DBI::errstr", ERROR);
121 $standby_dbs[-1]->do("SET NAMES '$$db{client_encoding}';") if ($$primary{client_encoding});
123 $log->debug("Connected to primary db '$$primary{db} at $$primary{host}", INFO);
126 $log->debug("All is well on the western front", INTERNAL);
131 return $primary_db if ($self->current_xact_session || $OpenILS::Application::Storage::WRITE);
132 return $primary_db unless (@standby_dbs);
133 return ($primary_db, @standby_dbs)[rand(scalar(@standby_dbs))];
138 return $self->db_Main->quote(@_)
144 sub current_xact_session {
146 if (defined($_xact_session)) {
147 return $_xact_session;
152 sub current_audit_session {
154 if (defined($_audit_session)) {
155 return $_audit_session;
160 sub current_xact_is_auto {
163 if (defined($_xact_session) and ref($_xact_session)) {
165 $_xact_session->session_data(autocommit => $auto);
167 return $_xact_session->session_data('autocommit');
171 sub current_xact_id {
173 if (defined($_xact_session) and ref($_xact_session)) {
174 return $_xact_session->session_id;
179 sub set_xact_session {
182 if (!defined($ses)) {
185 $_xact_session = $ses;
186 return $_xact_session;
189 sub set_audit_session {
192 if (!defined($ses)) {
195 $_audit_session = $ses;
196 return $_audit_session;
199 sub unset_xact_session {
201 my $ses = $_xact_session;
202 undef $_xact_session;
206 sub unset_audit_session {
208 my $ses = $_audit_session;
209 undef $_audit_session;