| 1 | package WWW::Garden::Design::Util::Create; | ||
| 2 | |||
| 3 | use Moo; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use DBI; | ||
| 9 | |||
| 10 | use DBIx::Admin::CreateTable; | ||
| 11 | |||
| 12 | use Mojo::Log; | ||
| 13 | |||
| 14 | use Moo; | ||
| 15 | |||
| 16 | use Types::Standard qw/ArrayRef HashRef Object Str/; | ||
| 17 | |||
| 18 | use WWW::Garden::Design::Util::Config; | ||
| 19 | |||
| 20 | has config => | ||
| 21 | ( | ||
| 22 | default => sub{WWW::Garden::Design::Util::Config -> new -> config}, | ||
| 23 | is => 'rw', | ||
| 24 | isa => HashRef, | ||
| 25 | required => 0, | ||
| 26 | ); | ||
| 27 | |||
| 28 | has creator => | ||
| 29 | ( | ||
| 30 | is => 'rw', | ||
| 31 | isa => Object, # 'DBIx::Admin::CreateTable'. | ||
| 32 | required => 0, | ||
| 33 | ); | ||
| 34 | |||
| 35 | has dbh => | ||
| 36 | ( | ||
| 37 | is => 'rw', | ||
| 38 | isa => Object, | ||
| 39 | required => 0, | ||
| 40 | ); | ||
| 41 | |||
| 42 | has engine => | ||
| 43 | ( | ||
| 44 | default => sub{return ''}, | ||
| 45 | is => 'rw', | ||
| 46 | isa => Str, | ||
| 47 | required => 0, | ||
| 48 | ); | ||
| 49 | |||
| 50 | has logger => | ||
| 51 | ( | ||
| 52 | is => 'rw', | ||
| 53 | isa => Object, | ||
| 54 | required => 0, | ||
| 55 | ); | ||
| 56 | |||
| 57 | has time_option => | ||
| 58 | ( | ||
| 59 | default => sub{return ''}, | ||
| 60 | is => 'rw', | ||
| 61 | isa => Str, | ||
| 62 | required => 0, | ||
| 63 | ); | ||
| 64 | |||
| 65 | our $VERSION = '0.97'; | ||
| 66 | |||
| 67 | # ----------------------------------------------- | ||
| 68 | |||
| 69 | sub BUILD | ||
| 70 | { | ||
| 71 | my($self) = @_; | ||
| 72 | my($config) = $self -> config; | ||
| 73 | my($attr) = | ||
| 74 | { | ||
| 75 | AutoCommit => $$config{AutoCommit}, | ||
| 76 | mysql_enable_utf8 => $$config{mysql_enable_utf8}, #Ignored if not using MySQL. | ||
| 77 | RaiseError => $$config{RaiseError}, | ||
| 78 | sqlite_unicode => $$config{sqlite_unicode}, #Ignored if not using SQLite. | ||
| 79 | }; | ||
| 80 | |||
| 81 | $self -> dbh(DBI -> connect($$config{dsn}, $$config{username}, $$config{password}, $attr) ); | ||
| 82 | $self -> dbh -> do('PRAGMA foreign_keys = ON') if ($$config{dsn} =~ /SQLite/i); | ||
| 83 | |||
| 84 | $self -> creator | ||
| 85 | ( | ||
| 86 | DBIx::Admin::CreateTable -> new | ||
| 87 | ( | ||
| 88 | dbh => $self -> dbh, | ||
| 89 | verbose => 0, | ||
| 90 | ) | ||
| 91 | ); | ||
| 92 | |||
| 93 | $self -> engine | ||
| 94 | ( | ||
| 95 | $self -> creator -> db_vendor =~ /(?:Mysql)/i ? 'engine=innodb' : '' | ||
| 96 | ); | ||
| 97 | $self -> logger | ||
| 98 | ( | ||
| 99 | Mojo::Log -> new(path => $$config{log_path}) | ||
| 100 | ); | ||
| 101 | |||
| 102 | $self -> time_option | ||
| 103 | ( | ||
| 104 | $self -> creator -> db_vendor =~ /(?:MySQL|Postgres)/i ? '(0) without time zone' : '' | ||
| 105 | ); | ||
| 106 | |||
| 107 | } # End of BUILD. | ||
| 108 | |||
| 109 | # ----------------------------------------------- | ||
| 110 | |||
| 111 | sub create_all_tables | ||
| 112 | { | ||
| 113 | my($self) = @_; | ||
| 114 | |||
| 115 | # Warning: The order is important. | ||
| 116 | |||
| 117 | my($method); | ||
| 118 | |||
| 119 | for my $table_name | ||
| 120 | (qw/ | ||
| 121 | log | ||
| 122 | constants | ||
| 123 | flowers | ||
| 124 | attribute_types | ||
| 125 | attributes | ||
| 126 | properties | ||
| 127 | gardens | ||
| 128 | flower_locations | ||
| 129 | features | ||
| 130 | feature_locations | ||
| 131 | notes | ||
| 132 | images | ||
| 133 | urls | ||
| 134 | /) | ||
| 135 | { | ||
| 136 | $method = "create_${table_name}_table"; | ||
| 137 | |||
| 138 | $self -> $method; | ||
| 139 | } | ||
| 140 | |||
| 141 | # Return 0 for OK and 1 for error. | ||
| 142 | |||
| 143 | return 0; | ||
| 144 | |||
| 145 | } # End of create_all_tables. | ||
| 146 | |||
| 147 | # -------------------------------------------------- | ||
| 148 | |||
| 149 | sub create_attributes_table | ||
| 150 | { | ||
| 151 | my($self) = @_; | ||
| 152 | my($table_name) = 'attributes'; | ||
| 153 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 154 | my($engine) = $self -> engine; | ||
| 155 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 156 | create table $table_name | ||
| 157 | ( | ||
| 158 | id $primary_key, | ||
| 159 | attribute_type_id int references attribute_types(id), | ||
| 160 | flower_id int references flowers(id), | ||
| 161 | range text not null | ||
| 162 | ) $engine | ||
| 163 | SQL | ||
| 164 | $self -> report($table_name, 'Created', $result); | ||
| 165 | |||
| 166 | } # End of create_attributes_table. | ||
| 167 | |||
| 168 | # -------------------------------------------------- | ||
| 169 | |||
| 170 | sub create_attribute_types_table | ||
| 171 | { | ||
| 172 | my($self) = @_; | ||
| 173 | my($table_name) = 'attribute_types'; | ||
| 174 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 175 | my($engine) = $self -> engine; | ||
| 176 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 177 | create table $table_name | ||
| 178 | ( | ||
| 179 | id $primary_key, | ||
| 180 | name text not null, | ||
| 181 | range text not null, | ||
| 182 | sequence integer not null | ||
| 183 | ) $engine | ||
| 184 | SQL | ||
| 185 | $self -> report($table_name, 'Created', $result); | ||
| 186 | |||
| 187 | } # End of create_attribute_types_table. | ||
| 188 | |||
| 189 | # -------------------------------------------------- | ||
| 190 | |||
| 191 | sub create_constants_table | ||
| 192 | { | ||
| 193 | my($self) = @_; | ||
| 194 | my($table_name) = 'constants'; | ||
| 195 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 196 | my($engine) = $self -> engine; | ||
| 197 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 198 | create table $table_name | ||
| 199 | ( | ||
| 200 | id $primary_key, | ||
| 201 | name text not null, | ||
| 202 | value text not null | ||
| 203 | ) $engine | ||
| 204 | SQL | ||
| 205 | $self -> report($table_name, 'Created', $result); | ||
| 206 | |||
| 207 | } # End of create_constants_table. | ||
| 208 | |||
| 209 | # -------------------------------------------------- | ||
| 210 | |||
| 211 | sub create_feature_locations_table | ||
| 212 | { | ||
| 213 | my($self) = @_; | ||
| 214 | my($table_name) = 'feature_locations'; | ||
| 215 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 216 | my($engine) = $self -> engine; | ||
| 217 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 218 | create table $table_name | ||
| 219 | ( | ||
| 220 | id $primary_key, | ||
| 221 | feature_id int references features(id), | ||
| 222 | garden_id int references gardens(id), | ||
| 223 | property_id int references properties(id), | ||
| 224 | x integer not null, | ||
| 225 | y integer not null | ||
| 226 | ) $engine | ||
| 227 | SQL | ||
| 228 | $self -> report($table_name, 'Created', $result); | ||
| 229 | |||
| 230 | } # End of create_feature_locations_table. | ||
| 231 | |||
| 232 | # -------------------------------------------------- | ||
| 233 | |||
| 234 | sub create_features_table | ||
| 235 | { | ||
| 236 | my($self) = @_; | ||
| 237 | my($table_name) = 'features'; | ||
| 238 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 239 | my($engine) = $self -> engine; | ||
| 240 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 241 | create table $table_name | ||
| 242 | ( | ||
| 243 | id $primary_key, | ||
| 244 | hex_color text not null, | ||
| 245 | name text not null, | ||
| 246 | publish text not null | ||
| 247 | ) $engine | ||
| 248 | SQL | ||
| 249 | $self -> report($table_name, 'Created', $result); | ||
| 250 | |||
| 251 | } # End of create_features_table. | ||
| 252 | |||
| 253 | # -------------------------------------------------- | ||
| 254 | |||
| 255 | sub create_flower_locations_table | ||
| 256 | { | ||
| 257 | my($self) = @_; | ||
| 258 | my($table_name) = 'flower_locations'; | ||
| 259 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 260 | my($engine) = $self -> engine; | ||
| 261 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 262 | create table $table_name | ||
| 263 | ( | ||
| 264 | id $primary_key, | ||
| 265 | flower_id int references flowers(id), | ||
| 266 | garden_id int references gardens(id), | ||
| 267 | property_id int references properties(id), | ||
| 268 | x integer not null, | ||
| 269 | y integer not null | ||
| 270 | ) $engine | ||
| 271 | SQL | ||
| 272 | $self -> report($table_name, 'Created', $result); | ||
| 273 | |||
| 274 | } # End of create_flower_locations_table. | ||
| 275 | |||
| 276 | # -------------------------------------------------- | ||
| 277 | |||
| 278 | sub create_flowers_table | ||
| 279 | { | ||
| 280 | my($self) = @_; | ||
| 281 | my($table_name) = 'flowers'; | ||
| 282 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 283 | my($engine) = $self -> engine; | ||
| 284 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 285 | create table $table_name | ||
| 286 | ( | ||
| 287 | id $primary_key, | ||
| 288 | aliases text not null, | ||
| 289 | common_name text not null, | ||
| 290 | height text not null, | ||
| 291 | kind text not null, | ||
| 292 | max_height text not null, | ||
| 293 | max_width text not null, | ||
| 294 | min_height text not null, | ||
| 295 | min_width text not null, | ||
| 296 | pig_latin text not null, | ||
| 297 | planted text not null, | ||
| 298 | publish text not null, | ||
| 299 | scientific_name text not null, | ||
| 300 | thumbnail text not null, | ||
| 301 | width text not null | ||
| 302 | ) $engine | ||
| 303 | SQL | ||
| 304 | $self -> report($table_name, 'Created', $result); | ||
| 305 | |||
| 306 | } # End of create_flowers_table. | ||
| 307 | |||
| 308 | # -------------------------------------------------- | ||
| 309 | |||
| 310 | sub create_gardens_table | ||
| 311 | { | ||
| 312 | my($self) = @_; | ||
| 313 | my($table_name) = 'gardens'; | ||
| 314 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 315 | my($engine) = $self -> engine; | ||
| 316 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 317 | create table $table_name | ||
| 318 | ( | ||
| 319 | id $primary_key, | ||
| 320 | property_id int references properties(id), | ||
| 321 | description text not null, | ||
| 322 | name text not null, | ||
| 323 | publish text not null | ||
| 324 | ) $engine | ||
| 325 | SQL | ||
| 326 | $self -> report($table_name, 'Created', $result); | ||
| 327 | |||
| 328 | } # End of create_gardens_table. | ||
| 329 | |||
| 330 | # -------------------------------------------------- | ||
| 331 | |||
| 332 | sub create_images_table | ||
| 333 | { | ||
| 334 | my($self) = @_; | ||
| 335 | my($table_name) = 'images'; | ||
| 336 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 337 | my($engine) = $self -> engine; | ||
| 338 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 339 | create table $table_name | ||
| 340 | ( | ||
| 341 | id $primary_key, | ||
| 342 | flower_id int references flowers(id), | ||
| 343 | description text not null, | ||
| 344 | file_name text not null | ||
| 345 | ) $engine | ||
| 346 | SQL | ||
| 347 | $self -> report($table_name, 'Created', $result); | ||
| 348 | |||
| 349 | } # End of create_images_table. | ||
| 350 | |||
| 351 | # -------------------------------------------------- | ||
| 352 | # In the log table: | ||
| 353 | # o action => 'add', 'delete', 'export', 'import', 'update'. | ||
| 354 | # o context => 'flower', etc. | ||
| 355 | # o key => Either 0 or a primary key associated with the context. | ||
| 356 | # o name => The name of the thing. | ||
| 357 | # o note => Any text. May contain other primary keys, e.g. when garden also has a property. | ||
| 358 | # o outcome => 'Success' or 'Error'. | ||
| 359 | |||
| 360 | sub create_log_table | ||
| 361 | { | ||
| 362 | my($self) = @_; | ||
| 363 | my($table_name) = 'log'; | ||
| 364 | my($time_option) = $self -> time_option; | ||
| 365 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 366 | my($engine) = $self -> engine; | ||
| 367 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 368 | create table $table_name | ||
| 369 | ( | ||
| 370 | id $primary_key, | ||
| 371 | action text not null, | ||
| 372 | context text not null, | ||
| 373 | file_name text not null, | ||
| 374 | key integer not null, | ||
| 375 | name text not null, | ||
| 376 | note text not null, | ||
| 377 | outcome text not null, | ||
| 378 | timestamp timestamp $time_option not null default current_timestamp | ||
| 379 | ) $engine | ||
| 380 | SQL | ||
| 381 | $self -> report($table_name, 'Created', $result); | ||
| 382 | |||
| 383 | } # End of create_log_table. | ||
| 384 | |||
| 385 | # -------------------------------------------------- | ||
| 386 | |||
| 387 | sub create_notes_table | ||
| 388 | { | ||
| 389 | my($self) = @_; | ||
| 390 | my($table_name) = 'notes'; | ||
| 391 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 392 | my($engine) = $self -> engine; | ||
| 393 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 394 | create table $table_name | ||
| 395 | ( | ||
| 396 | id $primary_key, | ||
| 397 | flower_id int references flowers(id), | ||
| 398 | note text not null | ||
| 399 | ) $engine | ||
| 400 | SQL | ||
| 401 | $self -> report($table_name, 'Created', $result); | ||
| 402 | |||
| 403 | } # End of create_notes_table. | ||
| 404 | |||
| 405 | # -------------------------------------------------- | ||
| 406 | |||
| 407 | sub create_properties_table | ||
| 408 | { | ||
| 409 | my($self) = @_; | ||
| 410 | my($table_name) = 'properties'; | ||
| 411 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 412 | my($engine) = $self -> engine; | ||
| 413 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 414 | create table $table_name | ||
| 415 | ( | ||
| 416 | id $primary_key, | ||
| 417 | description text not null, | ||
| 418 | name text not null, | ||
| 419 | publish text not null | ||
| 420 | ) $engine | ||
| 421 | SQL | ||
| 422 | $self -> report($table_name, 'Created', $result); | ||
| 423 | |||
| 424 | } # End of create_properties_table. | ||
| 425 | |||
| 426 | # -------------------------------------------------- | ||
| 427 | |||
| 428 | sub create_urls_table | ||
| 429 | { | ||
| 430 | my($self) = @_; | ||
| 431 | my($table_name) = 'urls'; | ||
| 432 | my($primary_key) = $self -> creator -> generate_primary_key_sql($table_name); | ||
| 433 | my($engine) = $self -> engine; | ||
| 434 | my($result) = $self -> creator -> create_table(<<SQL); | ||
| 435 | create table $table_name | ||
| 436 | ( | ||
| 437 | id $primary_key, | ||
| 438 | flower_id int references flowers(id), | ||
| 439 | url text not null | ||
| 440 | ) $engine | ||
| 441 | SQL | ||
| 442 | $self -> report($table_name, 'Created', $result); | ||
| 443 | |||
| 444 | } # End of create_urls_table. | ||
| 445 | |||
| 446 | # ----------------------------------------------- | ||
| 447 | |||
| 448 | sub drop_all_tables | ||
| 449 | { | ||
| 450 | my($self) = @_; | ||
| 451 | |||
| 452 | my($table_name); | ||
| 453 | |||
| 454 | for $table_name | ||
| 455 | (qw/ | ||
| 456 | urls | ||
| 457 | images | ||
| 458 | notes | ||
| 459 | feature_locations | ||
| 460 | features | ||
| 461 | flower_locations | ||
| 462 | gardens | ||
| 463 | properties | ||
| 464 | attributes | ||
| 465 | attribute_types | ||
| 466 | flowers | ||
| 467 | constants | ||
| 468 | log | ||
| 469 | /) | ||
| 470 | { | ||
| 471 | $self -> drop_table($table_name); | ||
| 472 | } | ||
| 473 | |||
| 474 | # Return 0 for OK and 1 for error. | ||
| 475 | |||
| 476 | return 0; | ||
| 477 | |||
| 478 | } # End of drop_all_tables. | ||
| 479 | |||
| 480 | # ----------------------------------------------- | ||
| 481 | |||
| 482 | sub drop_table | ||
| 483 | { | ||
| 484 | my($self, $table_name) = @_; | ||
| 485 | |||
| 486 | $self -> creator -> drop_table($table_name); | ||
| 487 | |||
| 488 | $self -> report($table_name, 'Dropped', ''); | ||
| 489 | |||
| 490 | } # End of drop_table. | ||
| 491 | |||
| 492 | # ----------------------------------------------- | ||
| 493 | |||
| 494 | sub report | ||
| 495 | { | ||
| 496 | my($self, $table_name, $message, $result) = @_; | ||
| 497 | |||
| 498 | if ($result) | ||
| 499 | { | ||
| 500 | die "Table '$table_name' $result. \n"; | ||
| 501 | } | ||
| 502 | else | ||
| 503 | { | ||
| 504 | $self -> logger -> debug("$message table '$table_name'"); | ||
| 505 | } | ||
| 506 | |||
| 507 | } # End of report. | ||
| 508 | |||
| 509 | # ----------------------------------------------- | ||
| 510 | |||
| 511 | 1; | ||
| 512 | |||
| 513 | =pod | ||
| 514 | |||
| 515 | =head1 NAME | ||
| 516 | |||
| 517 | WWW::Garden::Design::Util::Create - Manage the 'flowers' database | ||
| 518 | |||
| 519 | =head1 Machine-Readable Change Log | ||
| 520 | |||
| 521 | The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>. | ||
| 522 | |||
| 523 | =head1 Version Numbers | ||
| 524 | |||
| 525 | Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions. | ||
| 526 | |||
| 527 | =head1 Repository | ||
| 528 | |||
| 529 | L<https://github.com/ronsavage/WWW-Garden-Design> | ||
| 530 | |||
| 531 | =head1 Support | ||
| 532 | |||
| 533 | Email the author, or log a bug on RT: | ||
| 534 | |||
| 535 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=WWW::Garden::Design>. | ||
| 536 | |||
| 537 | =head1 Author | ||
| 538 | |||
| 539 | L<WWW::Garden::Design> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2014. | ||
| 540 | |||
| 541 | My homepage: L<https://savage.net.au/>. | ||
| 542 | |||
| 543 | =head1 Copyright | ||
| 544 | |||
| 545 | Australian copyright (c) 2018, Ron Savage. | ||
| 546 | |||
| 547 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 548 | you can redistribute them and/or modify them under the terms of | ||
| 549 | The Perl License, a copy of which is available at: | ||
| 550 | http://dev.perl.org/licenses/ | ||
| 551 | |||
| 552 | =cut |