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.
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.
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 UNIVERSAL::require;
19 'Class::DBI::Frozen::301'->use or 'Class::DBI'->use or die $@;
21 use base qw/Class::DBI OpenILS::Application::Storage/;
23 use OpenSRF::EX qw/:try/;
24 use OpenSRF::DomainObject::oilsResponse;
25 use OpenSRF::Utils::Logger qw/:level/;
26 my $log = 'OpenSRF::Utils::Logger';
28 __PACKAGE__->set_sql( retrieve_limited => 'SELECT * FROM __TABLE__ ORDER BY id LIMIT ?' );
29 __PACKAGE__->set_sql( copy_start => 'COPY %s (%s) FROM STDIN;' );
30 __PACKAGE__->set_sql( copy_end => '\.' );
37 return ($master_db, @slave_dbs);
44 $log->debug("Running child_init inside ".__PACKAGE__, INTERNAL);
46 $_db_params = [ $_db_params ] unless (ref($_db_params) eq 'ARRAY');
48 my %attrs = ( %{$self->_default_attributes},
49 RootClass => 'DBIx::ContextualFetch',
50 ShowErrorStatement => 1,
55 #TraceLevel => "1|SQL",
57 pg_server_prepare => 0,
58 FetchHashKeyName => 'NAME_lc',
62 my $master = shift @$_db_params;
63 $$master{port} ||= '5432';
64 $$master{host} ||= 'localhost';
65 $$master{db} ||= 'openils';
67 $log->debug("Attempting to connect to $$master{db} at $$master{host}", INFO);
70 $master_db = DBI->connect(
72 "host=$$master{host};".
73 "port=$$master{port};".
74 "dbname=$$master{db}",
81 "host=$$master{host};".
82 "port=$$master{port};".
83 "dbname=$$master{db}",
87 || throw OpenSRF::EX::ERROR
88 ("Couldn't connect to $$master{db}".
89 " on $$master{host}::$$master{port}".
90 " as $$master{user}!!");
93 $log->debug("Error connecting to database:\n\t$e\n\t$DBI::errstr", ERROR);
97 $log->debug("Connected to MASTER db $$master{db} at $$master{host}", INFO);
99 $master_db->do("SET NAMES '$$master{client_encoding}';") if ($$master{client_encoding});
101 for my $db (@$_db_params) {
103 push @slave_dbs, DBI->connect("dbi:Pg:host=$$db{host};port=$$db{port};dbname=$$db{db}",$$db{user},$$db{pw}, \%attrs)
104 || do { sleep(1); DBI->connect("dbi:Pg:host=$$db{host};port=$$db{port};dbname=$$db{db}",$$db{user},$$db{pw}, \%attrs) }
105 || throw OpenSRF::EX::ERROR
106 ("Couldn't connect to $$db{db}".
107 " on $$db{host}::$$db{port}".
111 $log->debug("Error connecting to database:\n\t$e\n\t$DBI::errstr", ERROR);
115 $slave_dbs[-1]->do("SET NAMES '$$db{client_encoding}';") if ($$master{client_encoding});
117 $log->debug("Connected to MASTER db '$$master{db} at $$master{host}", INFO);
120 $log->debug("All is well on the western front", INTERNAL);
125 return $master_db if ($self->current_xact_session || $OpenILS::Application::Storage::WRITE);
126 return $master_db unless (@slave_dbs);
127 return ($master_db, @slave_dbs)[rand(scalar(@slave_dbs))];
132 return $self->db_Main->quote(@_)
135 # sub tsearch2_trigger {
137 # return unless ($self->value);
138 # $self->index_vector(
139 # $self->db_Slaves->selectrow_array(
140 # "SELECT to_tsvector('default',?);",
150 sub current_xact_session {
152 if (defined($_xact_session)) {
153 return $_xact_session;
158 sub current_audit_session {
160 if (defined($_audit_session)) {
161 return $_audit_session;
166 sub current_xact_is_auto {
169 if (defined($_xact_session) and ref($_xact_session)) {
171 $_xact_session->session_data(autocommit => $auto);
173 return $_xact_session->session_data('autocommit');
177 sub current_xact_id {
179 if (defined($_xact_session) and ref($_xact_session)) {
180 return $_xact_session->session_id;
185 sub set_xact_session {
188 if (!defined($ses)) {
191 $_xact_session = $ses;
192 return $_xact_session;
195 sub set_audit_session {
198 if (!defined($ses)) {
201 $_audit_session = $ses;
202 return $_audit_session;
205 sub unset_xact_session {
207 my $ses = $_xact_session;
208 undef $_xact_session;
212 sub unset_audit_session {
214 my $ses = $_audit_session;
215 undef $_audit_session;