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