Make Evergreen Perl modules installable via Module::Build to match OpenSRF
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Storage.pm
1 package OpenILS::Application::Storage;
2 use OpenILS::Application;
3 use base qw/OpenILS::Application/;
4
5 use OpenSRF::EX qw/:try/;
6 use OpenSRF::Utils::Logger qw/:level/;
7
8 # Pull this in so we can adjust it's @ISA
9 use OpenILS::Application::Storage::CDBI (1);
10 use OpenILS::Application::Storage::FTS;
11
12
13 # the easy way to get to the logger...
14 my $log = "OpenSRF::Utils::Logger";
15
16 our $QParser;
17 our $WRITE = 0;
18 our $IGNORE_XACT_ID_FAILURE = 0;
19
20 sub DESTROY {};
21
22 sub initialize {
23
24         my $conf = OpenSRF::Utils::SettingsClient->new;
25
26         $log->debug('Initializing ' . __PACKAGE__ . '...', DEBUG);
27
28         my $db_driver = $conf->config_value( apps => 'open-ils.storage' => app_settings => databases => 'driver');
29         my $driver = "OpenILS::Application::Storage::Driver::$db_driver";
30
31         $log->debug("Attempting to load $driver ...", DEBUG);
32
33         $driver->use;
34         if ($@) {
35                 $log->debug( "Can't load $driver!  :  $@", ERROR );
36                 $log->error( "Can't load $driver!  :  $@");
37                 throw OpenSRF::EX::PANIC ( "Can't load $driver!  :  $@" );
38         }
39
40         $log->debug("$driver loaded successfully", DEBUG);
41
42         # Suck in the method publishing modules
43         @OpenILS::Application::Storage::CDBI::ISA = ( $driver );
44
45         OpenILS::Application::Storage::Publisher->use;
46         if ($@) {
47                 $log->debug("FAILURE LOADING Publisher!  $@", ERROR);
48                 throw OpenSRF::EX::PANIC ( "FAILURE LOADING Publisher!  :  $@" );
49         }
50
51         $log->debug("We seem to be OK...",DEBUG);
52 }
53
54 sub child_init {
55
56         $log->debug('Running child_init for ' . __PACKAGE__ . '...', DEBUG);
57
58         my $conf = OpenSRF::Utils::SettingsClient->new;
59
60         $log->debug('Calling the Driver child_init', DEBUG);
61         OpenILS::Application::Storage::CDBI->child_init(
62                 $conf->config_value( apps => 'open-ils.storage' => app_settings => databases => 'database')
63         );
64
65         if (OpenILS::Application::Storage::CDBI->db_Main()) {
66                 $log->debug("Success initializing driver!", DEBUG);
67
68         my $db_driver = $conf->config_value( apps => 'open-ils.storage' => app_settings => databases => 'driver');
69         $QParser = 'OpenILS::Application::Storage::Driver::'.$db_driver.'::QueryParser';
70         $QParser->use;
71
72         if($@) {
73                 $log->debug( "Can't load $QParser!  :  $@", ERROR );
74                 $log->error( "Can't load $QParser!  :  $@");
75         } else {
76                     return 1;
77         }
78         }
79
80         $log->debug("FAILURE initializing driver!", ERROR);
81         return 0;
82 }
83
84 sub begin_xaction {
85         my $self = shift;
86         my $client = shift;
87
88         local $WRITE = 1;
89
90         $log->debug(" XACT --> 'BEGIN'ing transaction for session ".$client->session->session_id,DEBUG);
91         try {
92                 OpenILS::Application::Storage::CDBI->db_Main->begin_work;
93                 $client->session->session_data( xact_id => $client->session->session_id );
94         } catch Error with {
95                 throw OpenSRF::DomainObject::oilsException->new(
96                         statusCode => 500,
97                         status => "Could not BEGIN transaction!",
98                 );
99         };
100         return 1;
101
102 }
103 __PACKAGE__->register_method(
104         method          => 'begin_xaction',
105         api_name        => 'open-ils.storage.transaction.begin',
106         api_level       => 1,
107         argc            => 0,
108 );
109
110 sub savepoint_placeholder {
111         return 1;
112 }
113 __PACKAGE__->register_method(
114         method          => 'savepoint_placeholder',
115         api_name        => 'open-ils.storage.savepoint.set',
116         api_level       => 1,
117         argc            => 1,
118 );
119 __PACKAGE__->register_method(
120         method          => 'savepoint_placeholder',
121         api_name        => 'open-ils.storage.savepoint.release',
122         api_level       => 1,
123         argc            => 1,
124 );
125 __PACKAGE__->register_method(
126         method          => 'savepoint_placeholder',
127         api_name        => 'open-ils.storage.savepoint.rollback',
128         api_level       => 1,
129         argc            => 1,
130 );
131
132 sub commit_xaction {
133         my $self = shift;
134         my $client = shift;
135
136         local $WRITE = 1;
137
138         try {
139                 OpenILS::Application::Storage::CDBI->db_Main->commit;
140                 $client->session->session_data( xact_id => '' );
141         } catch Error with {
142                 throw OpenSRF::DomainObject::oilsException->new(
143                         statusCode => 500,
144                         status => "Could not COMMIT  transaction!",
145                 );
146         };
147         return 1;
148 }
149 __PACKAGE__->register_method(
150         method          => 'commit_xaction',
151         api_name        => 'open-ils.storage.transaction.commit',
152         api_level       => 1,
153         argc            => 0,
154 );
155
156
157 sub current_xact {
158         my $self = shift;
159         my $client = shift;
160
161         return $client->session->session_data( 'xact_id' );
162 }
163 __PACKAGE__->register_method(
164         method          => 'current_xact',
165         api_name        => 'open-ils.storage.transaction.current',
166         api_level       => 1,
167         argc            => 0,
168 );
169
170 sub rollback_xaction {
171         my $self = shift;
172         my $client = shift;
173
174         local $WRITE = 1;
175
176         $log->debug(" XACT --> 'ROLLBACK'ing transaction for session ".$client->session->session_id,DEBUG);
177         $client->session->session_data( xact_id => '' );
178         return OpenILS::Application::Storage::CDBI->db_Main->rollback;
179 }
180 __PACKAGE__->register_method(
181         method          => 'rollback_xaction',
182         api_name        => 'open-ils.storage.transaction.rollback',
183         api_level       => 1,
184         argc            => 0,
185 );
186
187
188 sub _cdbi2Hash {
189         my $self = shift;
190         my $obj = shift;
191         return { map { ( $_ => $obj->$_ ) } ($obj->columns('All')) };
192 }
193
194 sub _cdbi_list2AoH {
195         my $self = shift;
196         my @objs = @_;
197         return [ map { $self->_cdbi2Hash($_) } @objs ];
198 }
199
200 1;