- Timestamp:
- 2018-11-09T20:00:17+13:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/perllib/gsmysql.pm
r32592 r32593 445 445 446 446 my $tablename = $self->get_metadata_table_name(); 447 if(!$self->table_exists($tablename)) { 448 $self->create_metadata_table() || return 0; 449 } else { 450 print STDERR "@@@ Meta table exists\n" if($self->{'verbosity'} > 2); 451 } 447 # if(!$self->table_exists($tablename)) { 448 # $self->create_metadata_table() || return 0; 449 # } else { 450 # print STDERR "@@@ Meta table exists\n" if($self->{'verbosity'} > 2); 451 # } 452 $self->create_metadata_table() || return 0; # will now only create it if it doesn't already exist 452 453 return 1; 453 454 } … … 459 460 460 461 my $tablename = $self->get_fulltext_table_name(); 461 if(!$self->table_exists($tablename)) { 462 $self->create_fulltext_table() || return 0; 463 } else { 464 print STDERR "@@@ Fulltxt table exists\n" if($self->{'verbosity'} > 2); 465 } 462 # if(!$self->table_exists($tablename)) { 463 # $self->create_fulltext_table() || return 0; 464 # } else { 465 # print STDERR "@@@ Fulltxt table exists\n" if($self->{'verbosity'} > 2); 466 # } 467 $self->create_fulltext_table() || return 0; # will now only create it if it doesn't already exist 466 468 return 1; 467 469 } … … 478 480 } 479 481 480 482 ## NOTE: these 2 create_table methods use mysql specific "CREATE TABLE IF NOT EXISTS" syntax 483 ## vs general SQL CREATE TABLE syntax which would produce an error message if the table 484 ## already existed 485 ## And unless do() fails, these two create methods will now always return true, 486 ## even if table existed and didn't need to be created. 481 487 sub create_metadata_table { 482 488 my $self= shift (@_); … … 484 490 485 491 my $table_name = $self->get_metadata_table_name(); 486 print STDERR " Creating table $table_name\n" if($self->{'verbosity'} > 1);492 print STDERR " Will create table $table_name if it doesn't exist\n" if($self->{'verbosity'} > 2); 487 493 488 494 # If using an auto incremented primary key: 489 my $stmt = "CREATE TABLE $table_name (id INT NOT NULL AUTO_INCREMENT, did VARCHAR(63) NOT NULL, sid VARCHAR(63) NOT NULL, metaname VARCHAR(127) NOT NULL, metavalue VARCHAR(1023) NOT NULL, PRIMARY KEY(id));";495 my $stmt = "CREATE TABLE IF NOT EXISTS $table_name (id INT NOT NULL AUTO_INCREMENT, did VARCHAR(63) NOT NULL, sid VARCHAR(63) NOT NULL, metaname VARCHAR(127) NOT NULL, metavalue VARCHAR(1023) NOT NULL, PRIMARY KEY(id));"; 490 496 return $dbh->do($stmt); 491 497 } … … 500 506 501 507 my $table_name = $self->get_fulltext_table_name(); 502 print STDERR " Creating table $table_name\n" if($self->{'verbosity'} > 1);508 print STDERR " Will create table $table_name if it doesn't exist\n" if($self->{'verbosity'} > 2); 503 509 504 510 # If using an auto incremented primary key: 505 my $stmt = "CREATE TABLE $table_name (id INT NOT NULL AUTO_INCREMENT, did VARCHAR(63) NOT NULL, sid VARCHAR(63) NOT NULL, fulltxt LONGTEXT, PRIMARY KEY(id));";511 my $stmt = "CREATE TABLE IF NOT EXISTS $table_name (id INT NOT NULL AUTO_INCREMENT, did VARCHAR(63) NOT NULL, sid VARCHAR(63) NOT NULL, fulltxt LONGTEXT, PRIMARY KEY(id));"; 506 512 return $dbh->do($stmt); 507 513 508 514 } 509 515 510 516 ## NOTE: this method uses mysql specific "DROP TABLE IF EXISTS" syntax vs general SQL DROP TABLE 517 ## syntax which would produce an error message if the table didn't exist 511 518 sub delete_collection_tables { 512 519 my $self= shift (@_); … … 514 521 515 522 # drop table <tablename> 516 my $table = $self->get_metadata_table_name(); 517 if($self->table_exists($table)) { 518 $dbh->do("drop table $table");# || warn("@@@ Couldn't delete $table"); 519 } 523 # my $table = $self->get_metadata_table_name(); 524 # if($self->table_exists($table)) { 525 # $dbh->do("drop table $table"); 526 # } 527 # $table = $self->get_fulltext_table_name(); 528 # if($self->table_exists($table)) { 529 # $dbh->do("drop table $table"); 530 # } 531 my $table = $self->get_metadata_table_name(); 532 $dbh->do("drop table if exists $table"); 533 520 534 $table = $self->get_fulltext_table_name(); 521 if($self->table_exists($table)) { 522 $dbh->do("drop table $table");# || warn("@@@ Couldn't delete $table"); 523 } 524 525 # + TODO Q: commit here, so that future select statements work? 535 $dbh->do("drop table if exists $table"); 536 537 # If prepared select statement handles already exist, would need to commit here 538 # so that future select statements using those prepared handles work. 526 539 # See https://metacpan.org/pod/release/TIMB/DBI-1.634_50/DBI.pm#Transactions 527 540 }
Note:
See TracChangeset
for help on using the changeset viewer.