Changeset 32524 for main/trunk
- Timestamp:
- 2018-10-18T21:40:29+13:00 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/perllib/plugouts/MySQLPlugout.pm
r32523 r32524 35 35 use docprint; 36 36 37 use DBI; # the central package for this plugout 38 37 39 use IPC::Open2; 38 40 use POSIX ":sys_wait_h"; # for waitpid, http://perldoc.perl.org/functions/waitpid.html 41 39 42 40 43 # TODO: SIGTERM rollback and disconnect? … … 104 107 $self->{'db_client_user'} = "root"; 105 108 $self->{'db_client_pwd'} = "6reenstone3"; 109 $self->{'build_mode'} = "removeold"; 106 110 #$self->{'db_host'} = "127.0.0.1"; 107 111 #$self->{'db_encoding'} = "utf8"; 108 #TODO: proc_mode is also a saveas option 112 #TODO: proc_mode is also a saveas option 109 113 110 114 ############ LOAD NECESSARY OPTIONS ########### 111 115 print STDERR "########## COLLECTION: ". $ENV{'GSDLCOLLECTION'}."\n"; 112 116 $self->{'collection_name'} = $ENV{'GSDLCOLLECTION'}; 117 print STDERR "***** MySQLPlugout process mode = \"", $self->{'process_mode'}, "\"\n"; 113 118 114 119 if(!$self->connect_to_db()) { … … 117 122 die("Could not connect to db. Can't proceed.\n"); 118 123 } 119 if(!$self->load_db_and_tables()) { 124 125 my $db_name = $self->{'site_name'} || "localsite"; # one database per GS3 site 126 my $build_mode = $self->{'build_mode'} || "removeold"; 127 if(!$self->load_db_and_tables($db_name, $build_mode)) { 128 120 129 # This is fatal for the plugout, let's terminate here 121 130 # PrintError would already have displayed the warning message on connection fail … … 133 142 } 134 143 144 print STDERR "#### Meta stmt: " . $self->{'metadata_prepared_insert_statement_handle'}->{'Statement'} . "\n"; 145 print STDERR "#### Full stmt: " . $self->{'fulltxt_prepared_insert_statement_handle'}->{'Statement'} . "\n"; 146 135 147 # if setting up to work with sql db failed, we'd have terminated and wouldn't come up to here: 136 148 # won't bother preparing GreenstoneXMLPlugout by calling superclass' begin() … … 157 169 my ($doc_obj, $doc_dir) = @_; 158 170 171 print STDERR "\n\n@@@ In saveas\n\n"; 172 159 173 my $proc_mode = $self->{'process_mode'}; 160 174 … … 216 230 my $fulltxt_table_sth = $self->{'fulltxt_prepared_insert_statement_handle'}; 217 231 218 $self->recursive_write_meta_and_text($doc_obj, $ root_section, $metadata_table_sth, $fulltxt_table_sth);232 $self->recursive_write_meta_and_text($doc_obj, $doc_oid, $root_section, $metadata_table_sth, $fulltxt_table_sth); 219 233 } 220 234 … … 233 247 234 248 my $debug_out = $self->{'debug_outhandle'}; 249 print STDERR "#### Meta stmt: " . $metadata_table_sth->{'Statement'} . "\n"; 250 print STDERR "#### Full stmt: " . $fulltxt_table_sth->{'Statement'} . "\n"; 235 251 236 252 #my $proc_mode = $self->{'process_mode'}; … … 240 256 foreach my $data (@{$section_ptr->{'metadata'}}) { 241 257 my $meta_name = $data->[0]; 242 my $escaped_meta_value = & escape_text($data->[1]);258 my $escaped_meta_value = &docprint::escape_text($data->[1]); 243 259 244 260 # Write out the current section's meta to collection db's METADATA table … … 249 265 # just print the statement we were going to execute 250 266 251 print $debug_out $metadata_table_sth-> Statement. "($doc_oid, $section_name, $meta_name, $escaped_meta_value)\n";267 print $debug_out $metadata_table_sth->{'Statement'} . "($doc_oid, $section_name, $meta_name, $escaped_meta_value)\n"; 252 268 } 253 269 else { 254 270 255 $metadata_table_sth->execute($doc_oid, $section_name, $meta_name, $escaped_meta_value); 256 #|| warn ("Unable to write metadata row to db:\n\tOID $doc_oid, section $section_name,\n\tmeta name: $meta_name, val: $escaped_meta_value"); 271 $metadata_table_sth->execute($doc_oid, $section_name, $meta_name, $escaped_meta_value) || warn ("Unable to write metadata row to db:\n\tOID $doc_oid, section $section_name,\n\tmeta name: $meta_name, val: $escaped_meta_value"); 257 272 # Execution failure will print out info anyway: since db connection sets PrintError 258 273 } … … 266 281 # just print the statement we were going to execute, minus the fulltxt value 267 282 my $txt_repr = $section_ptr->{'text'} ? "<TXT>" : "NULL"; 268 print $debug_out $fulltxt_table_sth-> Statement. "($doc_oid, $section_name, $txt_repr)\n";283 print $debug_out $fulltxt_table_sth->{'Statement'} . "($doc_oid, $section_name, $txt_repr)\n"; 269 284 } else { 270 my $section_text = & escape_text($section_ptr->{'text'});285 my $section_text = &docprint::escape_text($section_ptr->{'text'}); 271 286 272 287 # fulltxt column can be SQL NULL. undef value gets written out as NULL: … … 274 289 275 290 # Write out the current section's text to collection db's FULLTeXT table 276 $fulltxt_table_sth->execute($doc_oid, $section_name, $section_text); 277 #|| warn ("Unable to write fulltxt row to db for row:\n\tOID $doc_oid, section $section_name"); 291 $fulltxt_table_sth->execute($doc_oid, $section_name, $section_text) || warn ("Unable to write fulltxt row to db for row:\n\tOID $doc_oid, section $section_name"); 278 292 # Execution failure will print out info anyway: since db connection sets PrintError 279 293 } … … 337 351 PrintError => 1, # on by default, but being explicit 338 352 RaiseError => 0, # off by default, but being explicit 339 AutoCommit => 1 ,# on by default, but being explicit353 AutoCommit => 1 # on by default, but being explicit 340 354 }); 341 355 … … 360 374 sub load_db_and_tables { 361 375 my $self= shift (@_); 362 my $db_name = $self->{'site_name'}; # one database per GS3 site376 my ($db_name, $build_mode) = @_; 363 377 my $dbh = $self->{'db_handle'}; 364 378 … … 372 386 $self->create_db($db_name) || return 0; 373 387 388 print STDERR "@@@ CREATED DATABASE $db_name\n"; 389 374 390 # once more attempt to use db, now that it exists 375 391 $dbh->do("use $db_name") || return 0; … … 384 400 elsif($success) { # database existed and loaded successfully, but 385 401 # before proceeding check that the current collection's tables exist 402 403 print STDERR "@@@ DATABASE $db_name EXISTED\n"; 386 404 387 # attempt to create tables in current db 388 if(!$self->table_exists($self->{'collection_name'} . "metadata")) { 405 406 if($build_mode eq "removeold") { 407 $self->delete_collection_tables(); 408 } 409 410 # use existing tables if any 411 # attempt to create tables in current db 412 if($build_mode eq "removeold" || !$self->table_exists($self->get_metadata_table_name())) { 389 413 $self->create_metadata_table() || return 0; 414 } else { 415 print STDERR "@@@ Meta table exists\n"; 390 416 } 391 if( !$self->table_exists($self->{'collection_name'} . "fulltxt")) {417 if($build_mode eq "removeold" || !$self->table_exists($self->get_fulltext_table_name())) { 392 418 $self->create_fulltext_table() || return 0; 419 } else { 420 print STDERR "@@@ Fulltxt table exists\n"; 393 421 } 422 394 423 } 395 424 … … 425 454 } 426 455 456 427 457 sub create_metadata_table { 428 458 my $self= shift (@_); 429 459 my $dbh = $self->{'db_handle'}; 430 460 431 my $table_name = $self-> {'collection_name'} . "metadata";461 my $table_name = $self->get_metadata_table_name(); 432 462 433 463 # If using an auto incremented primary key: … … 444 474 my $dbh = $self->{'db_handle'}; 445 475 446 my $table_name = $self-> {'collection_name'} . "fulltxt";476 my $table_name = $self->get_fulltext_table_name(); 447 477 448 478 # If using an auto incremented primary key: … … 469 499 sub prepare_insert_metadata_row_stmthandle { 470 500 my $self = shift (@_); 471 my ($did, $sid, $metaname, $metavalue) = @_;472 my $dbh = $self->{'db_handle'}; 473 474 my $tablename = $self-> {'colname'}."_metadata";501 #my ($did, $sid, $metaname, $metavalue) = @_; 502 my $dbh = $self->{'db_handle'}; 503 504 my $tablename = $self->get_metadata_table_name(); 475 505 476 506 #my $stmt = "INSERT INTO $tablename (did, sid, metaname, metavalue) VALUES ('$did', '$sid', '$metaname', '$metavalue');"; # ?, ?, ?, ? … … 478 508 # using qq{} since we want $tablename placeholder to be filled in 479 509 # returns Statement Handle object! 480 my $sth = $dbh->prepare(qq{INSERT INTO $tablename (did, sid, metaname, metavalue) VALUES (?, ?, ?, ?)"}) || warn("Could not prepare insert statement for metadata table\n"); 510 my $sth = $dbh->prepare(qq{INSERT INTO $tablename (did, sid, metaname, metavalue) VALUES (?, ?, ?, ?)}) || warn("Could not prepare insert statement for metadata table\n"); 511 512 print STDERR "@@@@ Prepared meta insert statement: ".$sth->{'Statement'}."\n"; 481 513 482 514 return $sth; … … 485 517 sub prepare_insert_fulltxt_row_stmthandle { 486 518 my $self = shift (@_); 487 my ($did, $sid, $fulltext) = @_;488 my $dbh = $self->{'db_handle'}; 489 490 my $tablename = $self-> {'colname'}."_fulltxt";519 #my ($did, $sid, $fulltext) = @_; 520 my $dbh = $self->{'db_handle'}; 521 522 my $tablename = $self->get_fulltext_table_name(); 491 523 492 524 #my $stmt = "INSERT INTO $tablename (did, sid, fulltxt) VALUES ('$did', '$sid', '$fulltext');"; ?, ?, ? … … 494 526 # using qq{} since we want $tablename placeholder to be filled in 495 527 # returns Statement Handle object! 496 my $sth = $dbh->prepare(qq{INSERT INTO $tablename (did, sid, fulltxt) VALUES (?, ?, ?)"}) || warn("Could not prepare insert statement for fulltxt table\n"); 528 my $sth = $dbh->prepare(qq{INSERT INTO $tablename (did, sid, fulltxt) VALUES (?, ?, ?)}) || warn("Could not prepare insert statement for fulltxt table\n"); 529 530 print STDERR "@@@@ Prepared fulltext insert statement: ".$sth->{'Statement'}."\n"; 497 531 498 532 return $sth; 533 } 534 535 # "IF EXISTS is used to prevent an error from occurring if the database does not exist. ... DROP DATABASE returns the number of tables that were removed. The DROP DATABASE statement removes from the given database directory those files and directories that MySQL itself may create during normal operation.Jun 20, 2012" 536 #MySQL 8.0 Reference Manual :: 13.1.22 DROP DATABASE Syntax 537 # https://dev.mysql.com/doc/en/drop-database.html 538 sub delete_collection_tables { 539 my $self= shift (@_); 540 my $dbh = $self->{'db_handle'}; 541 542 print STDERR "### Build mode is removeold, so deleting tables for current collection\n"; 543 544 # drop table <tablename> 545 my $table = $self->get_metadata_table_name(); 546 $dbh->do("drop table $table") || warn("@@@ Couldn't delete $table"); 547 $table = $self->get_fulltext_table_name(); 548 $dbh->do("drop table $table") || warn("@@@ Couldn't delete $table"); 549 } 550 551 # Don't call this: it will delete the meta and full text tables for ALL collections in $db_name (localsite by default)! 552 # this is just for debugging 553 sub _delete_database { 554 my $self= shift (@_); 555 my ($db_name) = @_; 556 my $dbh = $self->{'db_handle'}; 557 558 # "drop database dbname" 559 $dbh->do("drop database $db_name") || return 0; 560 561 return 1; 562 } 563 564 # More basic helper methods 565 sub get_metadata_table_name { 566 my $self= shift (@_); 567 my $table_name = $self->{'collection_name'} . "_metadata"; 568 return $table_name; 569 } 570 571 # FULLTEXT is a reserved keyword in (My)SQL. https://dev.mysql.com/doc/refman/5.5/en/keywords.html 572 # So we can't name a table or any of its columns "fulltext". We use "fulltxt" instead. 573 sub get_fulltext_table_name { 574 my $self= shift (@_); 575 my $table_name = $self->{'collection_name'} . "_fulltxt"; 576 return $table_name; 499 577 } 500 578 … … 505 583 # Couldn't get the first solution at https://www.perlmonks.org/bare/?node_id=500050 to work though 506 584 sub table_exists { 507 my ($dbh,$table_name) = @_; 585 my $self = shift (@_); 586 my $dbh = $self->{'db_handle'}; 587 my ($table_name) = @_; 508 588 509 589 my @table_list = $dbh->tables;
Note:
See TracChangeset
for help on using the changeset viewer.