]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage.pm
reworking of the old storage module for OpenSRF
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage.pm
1 use Class::DBI;
2
3 package Class::DBI;
4
5 {
6         no warnings;
7         no strict;
8         sub _do_search {
9                 my ($proto, $search_type, @args) = @_;
10                 my $class = ref $proto || $proto;
11                 
12                 @args = %{ $args[0] } if ref $args[0] eq "HASH";
13
14                 my (@cols, @vals);
15                 my $search_opts = @args % 2 ? pop @args : {};
16
17                 $search_opts->{offset} = int($search_opts->{page}) * int($search_opts->{page_size})  if ($search_opts->{page_size});
18                 $search_opts->{_placeholder} ||= '?';
19
20                 while (my ($col, $val) = splice @args, 0, 2) {
21                         my $column = $class->find_column($col)
22                                 || (List::Util::first { $_->accessor eq $col } $class->columns)
23                                 || $class->_croak("$col is not a column of $class");
24
25                         push @cols, $column;
26                         push @vals, $class->_deflated_column($column, $val);
27                 }
28
29                 my $frag = join " AND ",
30                 map defined($vals[$_]) ? "$cols[$_] $search_type $$search_opts{_placeholder}" : "$cols[$_] IS NULL",
31                         0 .. $#cols;
32
33                 $frag .= " ORDER BY $search_opts->{order_by}"
34                         if $search_opts->{order_by};
35                 $frag .= " LIMIT $search_opts->{limit}"
36                         if $search_opts->{limit};
37                 $frag .= " OFFSET $search_opts->{offset}"
38                         if ($search_opts->{limit} && defined($search_opts->{offset}));
39
40                 return $class->sth_to_objects($class->sql_Retrieve($frag),
41                         [ grep defined, @vals ]);
42         }
43 }
44
45 sub search_fti {
46         my $self = shift;
47         my @args = @_;
48         if (ref($args[-1]) eq 'HASH') {
49                 $args[-1]->{_placeholder} = "to_tsquery('default',?)";
50         } else {
51                 push @args, {_placeholder => "to_tsquery('default',?)"};
52         }
53         $self->_do_search("@@"  => @args);
54 }
55
56
57
58 #-------------------------------------------------------------------------------
59 package OpenILS::Application::Storage;
60 use OpenSRF::Application;
61 use base qw/OpenSRF::Application/;
62
63 use OpenSRF::EX qw/:try/;
64 use OpenSRF::Utils::Logger qw/:level/;
65
66 my $log = "OpenSRF::Utils::Logger";
67
68 sub DESTROY {};
69
70 our $_db_driver;
71 our $_db_params;
72
73
74 sub initialize {
75         return $_db_driver if (defined $_db_driver);
76         my $conf = OpenSRF::Utils::SettingsClieng->new;
77
78         $log->debug('Initializing ' . __PACKAGE__ . '...', DEBUG);
79
80         my $driver = $conf->get_value( apps => storage => app_settings => databases => 'driver');
81         my $_db_params = $conf->get_value( apps => storage => app_settings => databases => 'database');
82
83         $_db_driver = "OpenILS::App::Storage::$driver";
84
85
86         eval "use $_db_driver;";
87         throw OpenILS::EX::Config ( "Can't load $_db_driver!  :  $@" ) if ($@);
88
89         $_db_driver->initialize if ($_db_driver->can('initialize'));
90
91         push @OpenILS::Application::Storage::CDBI::ISA, $_db_driver;
92
93 }
94
95 sub child_init {
96
97         $log->debug('Running child_init for ' . __PACKAGE__ . '...', DEBUG);
98         $_db_driver->child_init if ($_db_driver->can('child_init'));
99         
100         return 1 if ($_db_driver->db_Main($_db_params));
101         return 0;
102 }
103
104 sub getBiblioFieldMaps {
105         my $self = shift;
106         my $client = shift;
107         my $id = shift;
108         $log->debug(" Executing [".$self->method."] as [".$self->api_name."]",INTERNAL);
109         
110         if ($self->api_name =~ /by_class$/) {
111                 if ($id) {
112                         return _cdbi2Hash( config::metarecord_field_map->search( fieldclass => $id ) );
113                 } else {
114                         throw OpenSRF::EX::InvalidArg ('Please give me a Class to look up!');
115                 }
116         } else {
117                 if ($id) {
118                         return _cdbi2Hash( config::metarecord_field_map->retrieve( $id ) );
119                 } else {
120                         return _cdbi_list2AoH( config::metarecord_field_map->retrieve_all );
121                 }
122         }
123 }       
124 __PACKAGE__->register_method(
125         method          => 'getBiblioFieldMaps',
126         api_name        => 'open-ils.storage.config.metarecord_field',
127         argc            => 1,
128 );
129 __PACKAGE__->register_method(
130         method          => 'getBiblioFieldMaps',
131         api_name        => 'open-ils.storage.config.metarecord_field.list',
132         argc            => 0,
133 );
134 __PACKAGE__->register_method(
135         method          => 'getBiblioFieldMaps',
136         api_name        => 'open-ils.storage.config.metarecord_field.list.by_class',
137         argc            => 0,
138 );
139
140
141 sub getBiblioFieldMapClasses {
142         my $self = shift;
143         my $client = shift;
144         my $id = shift;
145
146         $log->debug(" Executing [".$self->method."] as [".$self->api_name."]",INTERNAL);
147
148         if ($id) {
149                 return _cdbi2Hash( config::metarecord_field_class_map->retrieve( $id ) );
150         } else {
151                 return _cdbi_list2AoH( config::metarecord_field_class_map->retrieve_all );
152         }
153 }       
154 __PACKAGE__->register_method(
155         method          => 'getBiblioFieldMapClasses',
156         api_name        => 'open-ils.storage.config.metarecord_field_class',
157         argc            => 1,
158 );
159 __PACKAGE__->register_method(
160         method          => 'getBiblioFieldMapClasses',
161         api_name        => 'open-ils.storage.config.metarecord_field_class.list',
162         argc            => 0,
163 );
164
165 sub _cdbi2Hash {
166         my $obj = shift;
167         return { map { ( $_ => $obj->$_ ) } $obj->columns };
168 }
169
170 sub _cdbi_list2AoH {
171         my @objs = @_;
172         return [ map { _cdbi2oilsHash($_) } @objs ];
173 }
174
175 #-------------------------------------------------------------------------------
176 package OpenILS::App::Storage::CDBI;
177 use vars qw/@ISA/;
178
179 1;