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;
18 use base qw/Class::DBI OpenILS::Application::Storage/;
20 use OpenSRF::EX qw/:try/;
21 use OpenSRF::DomainObject::oilsResponse;
22 use OpenSRF::Utils::Logger qw/:level/;
23 my $log = 'OpenSRF::Utils::Logger';
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 => '\.' );
36 $log->debug("Running child_init inside ".__PACKAGE__, INTERNAL);
38 $_db_params = [ $_db_params ] unless (ref($_db_params) eq 'ARRAY');
40 my %attrs = ( %{$self->_default_attributes},
41 RootClass => 'DBIx::ContextualFetch',
42 ShowErrorStatement => 1,
47 #TraceLevel => "1|SQL",
49 pg_server_prepare => 0,
50 FetchHashKeyName => 'NAME_lc',
54 my $master = shift @$_db_params;
55 $$master{port} ||= '5432';
56 $$master{host} ||= 'localhost';
57 $$master{db} ||= 'openils';
59 $log->debug("Attmpting to connet to $$master{db} at $$master{host}", INFO);
62 $master_db = DBI->connect(
64 "host=$$master{host};".
65 "port=$$master{port};".
66 "dbname=$$master{db}",
70 throw OpenSRF::EX::ERROR
71 ("Couldn't connect to $$master{db}".
72 " on $$master{host}::$$master{port}".
73 " as $$master{user}!!");
76 $log->debug("Error connecting to database:\n\t$e\n\t$DBI::errstr", ERROR);
80 $log->debug("Connected to MASTER db $$master{db} at $$master{host}", INFO);
82 $master_db->do("SET NAMES '$$master{client_encoding}';") if ($$master{client_encoding});
84 for my $db (@$_db_params) {
85 push @slave_dbs, DBI->connect("dbi:Pg:host=$$db{host};dbname=$$db{db}",$$db{user},$$db{pw}, \%attrs);
86 $slave_dbs[-1]->do("SET NAMES '$$db{client_encoding}';") if ($$master{client_encoding});
88 $log->debug("Connected to MASTER db '$$master{db} at $$master{host}", INFO);
91 $log->debug("All is well on the western front", INTERNAL);
96 return $master_db if ($self->current_xact_session);
97 return $master_db unless (@slave_dbs);
98 return ($master_db, @slave_dbs)[rand(scalar(@slave_dbs))];
103 return $self->db_Main->quote(@_)
106 # sub tsearch2_trigger {
108 # return unless ($self->value);
109 # $self->index_vector(
110 # $self->db_Slaves->selectrow_array(
111 # "SELECT to_tsvector('default',?);",
120 sub current_xact_session {
122 if (defined($_xact_session)) {
123 return $_xact_session;
128 sub current_xact_is_auto {
131 if (defined($_xact_session) and ref($_xact_session)) {
133 $_xact_session->session_data(autocommit => $auto);
135 return $_xact_session->session_data('autocommit');
139 sub current_xact_id {
141 if (defined($_xact_session) and ref($_xact_session)) {
142 return $_xact_session->session_id;
147 sub set_xact_session {
150 if (!defined($ses)) {
153 $_xact_session = $ses;
154 return $_xact_session;
157 sub unset_xact_session {
159 my $ses = $_xact_session;
160 undef $_xact_session;