Design/Database/Pg.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Design.pm ↑ Summary Design/Util/Filer.pm »
1 package WWW::Garden::Design::Database::Pg;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 with 'WWW::Garden::Design::Database';
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Data::Dumper::Concise; # For Dumper().
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Mojo::Pg;
Subroutine BEGIN: called CC=1 SCAR=2.6
15  
16 our $VERSION = '0.97';
17  
18 # -----------------------------------------------
19  
20 sub BUILD
21 {
22 my($self) = @_;
23 my($config) = $self -> config;
24  
25 $self -> db(Mojo::Pg -> new("postgres://$$config{username}:$$config{password}\@localhost/flowers") -> db);
26 $self -> init_title_font($config); # Uses db()!
27  
28 } # End of BUILD;
29  
30 # -----------------------------------------------
31 # Return a list.
32  
33 sub autocomplete_feature_list
34 {
35 my($self, $key) = @_;
36 $key =~ s/\'/\'\'/g; # Since we're using Pg.
37 my($sql) = "select distinct name from features where upper(name) like '%$key%'";
38  
39 $self -> logger -> debug("Database.Pg.autocomplete_feature_list(key: $key). Entered");
40  
41 return $self -> find_unique_items($sql); # Yes, despite 'distinct' above.
42  
43 } # End of _autocomplete_feature_list.
44  
45 # -----------------------------------------------
46 # Return a list.
47  
48 sub autocomplete_flower_list
49 {
50 my($self, $key) = @_;
51 $key =~ s/\'/\'\'/g; # Since we're using Pg.
52 my($sql) = "select distinct concat(scientific_name, '/', common_name) from flowers "
53 . "where upper(scientific_name) like '%$key%' "
54 . "or upper(common_name) like '%$key%' "
55 . "or upper(aliases) like '%$key%'";
56  
57 $self -> logger -> debug("Database.Pg.autocomplete_flower_list(key: $key). Entered");
58  
59 return $self -> find_unique_items($sql); # Yes, despite 'distinct' above.
60  
61 } # End of autocomplete_flower_list.
62  
63 # -----------------------------------------------
64 # Return the shortest item in the list.
65  
66 sub autocomplete_item
67 {
68 my($self, $context, $key, $type) = @_;
69 $key =~ s/\'/\'\'/g; # Since we're using Pg.
70  
71 $self -> logger -> debug("Database.Pg.autocomplete_item(... key: $key. type: $type). Entered");
72  
73 my(@item);
74 my(@result);
75 my($sql);
76  
77 for my $index (keys %$context)
78 {
79 my($search_column) = $$context{$index}[0];
80 my($table_name) = $$context{$index}[1];
81  
82 # $search_column is a special case. See AutoComplete.pm and autocomplete_flower_list() above.
83  
84 next if ($search_column eq '*');
85  
86 # If we're not searching then we're processing the Add screen.
87 # In that case, we're only interested in one $index at a time.
88  
89 if ( ($type ne 'search') && ($index ne $type) )
90 {
91 next;
92 }
93  
94 $sql = "select distinct $search_column from $table_name where upper($search_column) like '%$key%'";
95 @item = $self -> db -> query($sql) -> hashes -> each;
96  
97 if ($#item >= 0)
98 {
99 push @result, $item[0]{$search_column};
100 }
101 }
102  
103 my($min_length) = 99; # Arbitrary.
104  
105 my($min_value);
106  
107 for (@result)
108 {
109 if (length($_) < $min_length)
110 {
111 $min_length = length($_);
112 $min_value = $_;
113 }
114 }
115  
116 if ($min_value)
117 {
118 $self -> logger -> info("Return <$min_value>");
119  
120 return [$min_value];
121 }
122 else
123 {
124 return [];
125 }
126  
127 } # End of autocomplete_item.
128  
129 # -----------------------------------------------
130 # Return a list.
131  
132 sub autocomplete_list
133 {
134 my($self, $context, $key, $type) = @_;
135 $key =~ s/\'/\'\'/g; # Since we're using Pg.
136  
137 $self -> logger -> debug("Database.Pg.autocomplete_list(... key: $key. type: $type). Entered");
138  
139 my(@list);
140 my(@result);
141 my($sql, %seen);
142  
143 for my $index (keys %$context)
144 {
145 my($search_column) = $$context{$index}[0];
146 my($table_name) = $$context{$index}[1];
147  
148 # $search_column is a special case. See AutoComplete.pm and autocomplete_flower_list() above.
149  
150 next if ($search_column eq '*');
151  
152 # If we're not searching then we're processing the Add screen.
153 # In that case, we're only interested in one $index at a time.
154  
155 if ( ($type ne 'search') && ($index ne $type) )
156 {
157 next;
158 }
159  
160 # Using 'select distinct ...' did not weed out duplicates.
161  
162 $sql = "select $search_column from $table_name where upper($search_column) like '%$key%'";
163 @list = map{$$_[0]} $self -> db -> query($sql) -> arrays -> each;
164  
165 push @result, grep{! $seen{$_} } @list;
166  
167 $seen{$_} = 1 for @list;
168 }
169  
170 return [sort @result];
171  
172 } # End of autocomplete_list.
173  
174 # --------------------------------------------------
175  
176 sub get_feature_by_name
177 {
178 my($self, $key) = @_;
179 my($constants) = $self -> constants;
180 $key =~ s/\'/\'\'/g; # Since we're using Pg.
181 $key = "\U%$key"; # \U => Convert to upper-case.
182 my($sql) = "select name from features where upper(name) like ?";
183 my(@result) = $self -> db -> query($sql, $key) -> hashes -> each;
184 my($file_name) = ($#result >= 0) ? $self -> clean_up_feature_name($result[0]{name}) : '';
185  
186 return length($file_name) > 0 ? "$$constants{homepage_url}$$constants{feature_url}/$file_name.png" : '';
187  
188 } # End of get_feature_by_name.
189  
190 # --------------------------------------------------
191  
192 sub get_flower_by_both_names
193 {
194 my($self, $key) = @_;
195 my($constants) = $self -> constants;
196 $key =~ s/\'/\'\'/g; # Since we're using Pg.
197 $key = uc $key;
198 my(@key) = split('/', $key);
199 my($sql) = "select pig_latin from flowers where upper(scientific_name) like ? and upper(common_name) like ?";
200 my(@result) = $self -> db -> query($sql, $key[0], $key[1]) -> hashes -> each;
201 my($file_name) = ($#result >= 0) ? $result[0]{pig_latin} : '';
202  
203 return length($file_name) > 0 ? "$$constants{homepage_url}$$constants{image_url}/$file_name.0.jpg" : '';
204  
205 } # End of get_flower_by_both_names.
206  
207 # -----------------------------------------------
208  
209 sub insert_hashref
210 {
211 my($self, $table_name, $hashref) = @_;
212  
213 return ${$self -> db -> insert
214 (
215 $table_name, {map{($_ => $$hashref{$_})} keys %$hashref}, {returning => ['id']}
216 ) -> hash}{id};
217  
218 } # End of insert_hashref.
219  
220 # --------------------------------------------------
221  
222 sub read_feature_dependencies
223 {
224 my($self, $table_name, $feature_id) = @_;
225  
226 # Return an arrayref of hashrefs.
227  
228 return [$self -> db -> query("select * from $table_name where feature_id = $feature_id") -> hashes -> each];
229  
230 } # End of read_feature_dependencies.
231  
232 # --------------------------------------------------
233  
234 sub read_flower_dependencies
235 {
236 my($self, $table_name, $flower_id) = @_;
237  
238 # Return an arrayref of hashrefs.
239  
240 return [$self -> db -> query("select * from $table_name where flower_id = $flower_id") -> hashes -> each];
241  
242 } # End of read_flower_dependencies.
243  
244 # --------------------------------------------------
245  
246 sub read_garden_dependencies
247 {
248 my($self, $table_name, $garden_id) = @_;
249  
250 # Return an arrayref of hashrefs.
251  
252 return [$self -> db -> query("select * from $table_name where garden_id = $garden_id") -> hashes -> each];
253  
254 } # End of read_garden_dependencies.
255  
256 # --------------------------------------------------
257  
258 sub read_table
259 {
260 my($self, $table_name) = @_;
261  
262 # Return an arrayref of hashrefs.
263  
264 return [$self -> db -> query("select * from $table_name") -> hashes -> each];
265  
266 } # End of read_table.
267  
268 # --------------------------------------------------
269  
270 1;
271  
272 =pod
273  
274 =head1 NAME
275  
276 WWW::Garden::Design::Database::Pg - Manage the 'flowers' database
277  
278 =head1 Machine-Readable Change Log
279  
280 The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>.
281  
282 =head1 Version Numbers
283  
284 Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions.
285  
286 =head1 Repository
287  
288 L<https://github.com/ronsavage/WWW-Garden-Design>
289  
290 =head1 Support
291  
292 Email the author, or log a bug on RT:
293  
294 L<https://rt.cpan.org/Public/Dist/Display.html?Name=WWW::Garden::Design>.
295  
296 =head1 Author
297  
298 L<WWW::Garden::Design> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2014.
299  
300 My homepage: L<https://savage.net.au/>.
301  
302 =head1 Copyright
303  
304 Australian copyright (c) 2018, Ron Savage.
305  
306 All Programs of mine are 'OSI Certified Open Source Software';
307 you can redistribute them and/or modify them under the terms of
308 The Perl License, a copy of which is available at:
309 http://dev.perl.org/licenses/
310  
311 =cut
« Design.pm ↑ Summary Design/Util/Filer.pm »