Ignore:
Timestamp:
2018-11-02T19:07:16+13:00 (5 years ago)
Author:
ak19
Message:
  1. Overhaul of GreenstoneSQLPlugs to handle removeold and incremental delete correctly. And now code also automatically handles 'non-incremental delete' (see mention in ArchivesInfPlugin). The new version no longer does lazy loading for getting the sql db connection in the GS SQL Plugin, as now the connection needs to be active since the start of the plugin to run SQL delete statements on remove_old. So the db connection code for the GS SQL plugin has moved back into its init() method. Lots of changes to gssql.pm (and some flow on effects to the GS SQL Plugout) as when database tables exist and need to be created have changed. 2. Undoing most of the changes of changeset 32555 since we're doing incremental delete and removeold differently and in the correct way now when using the GreenstoneSQLPlugs.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/perllib/gssql.pm

    r32561 r32563  
    4444
    4545# Parameterise (one or more methods may use them):
    46 # - build_mode (like removeold)
    4746# - db_name (which is the GS3 sitename)
    4847
     
    134133    print STDERR "Away to make connection to $db_driver database with:\n";
    135134    print STDERR " - hostname $db_host; username: $db_user";
    136     print STDERR "; and the password provided" if $db_pwd;
     135    print STDERR "; and the provided password" if $db_pwd;
    137136    print STDERR "\nAssuming the mysql server has been started with: --character_set_server=utf8mb4\n" if $db_driver eq "mysql";
    138137    }
     
    188187}
    189188
    190 # will attempt to load the specified db and the <coll>_metadata and <coll>_fulltxt for this
    191 # collection, or create any of these (db, tables) that don't yet exist. At the end
    192 # it will have loaded the requested database (in MySQL: "use <db>;")
    193 sub load_db_and_tables {
    194     my $self= shift (@_);
    195     my ($db_name, $build_mode) = @_;
     189# Load the designated database, i.e. 'use <dbname>;'.
     190# If the database doesn't yet exist, creates it and loads it.
     191# (Don't create the collection's tables yet, though)
     192# At the end it will have loaded the requested database (in MySQL: "use <db>;") on success.
     193# As usual, returns success or failure value that can be evaluated in a boolean context.
     194sub use_db {
     195    my $self= shift (@_);
     196    my ($db_name) = @_;
    196197    my $dbh = $self->{'db_handle'};
    197198    $db_name = $self->sanitize_name($db_name);
     
    215216    # once more attempt to use db, now that it exists
    216217    $dbh->do("use $db_name") || return 0;
    217     #$dbh->do("use localsite") or die "Error (code" . $dbh->err ."): " . $dbh->errstr . "\n";
    218 
    219     # attempt to create tables in current db
    220     $self->create_metadata_table() || return 0;
    221     $self->create_fulltext_table() || return 0;
     218    #$dbh->do("use $db_name") or die "Error (code" . $dbh->err ."): " . $dbh->errstr . "\n";
    222219
    223220    $success = 1;
     
    227224
    228225    print STDERR "@@@ DATABASE $db_name EXISTED\n" if($self->{'verbosity'} > 2);
    229    
    230 
    231     # build_mode can be removeold or incremental. We only do something special on removeold:
    232     # deleting the existing tables for this collection and recreating empty ones
    233     if($build_mode eq "removeold") {
    234         print STDERR "   Building with removeold option set, so deleting current collection's tables if they exist\n" if($self->{'verbosity'});
    235         $self->delete_collection_tables();
    236     }
    237 
    238     # use existing tables if any
    239     # attempt to create tables in current db   
    240     if($build_mode eq "removeold" || !$self->table_exists($self->get_metadata_table_name())) {
    241         $self->create_metadata_table() || return 0;
    242     } else {
    243         print STDERR "@@@ Meta table exists\n" if($self->{'verbosity'} > 2);
    244     }
    245     if($build_mode eq "removeold" || !$self->table_exists($self->get_fulltext_table_name())) {
    246         $self->create_fulltext_table() || return 0;
    247     } else {
    248         print STDERR "@@@ Fulltxt table exists\n" if($self->{'verbosity'} > 2);
    249     }
    250    
    251226    }
    252227   
     
    254229}
    255230
    256 # GreenstoneSQLPlugin calls this method to load an existing db.
    257 # This will terminate if the db does not exist. Unlike load_db_and_tables() above, used by
    258 # GreenstoneSQLPlugout, this method will not attempt to create the requested db (nor its tables)
    259 # TODO: GS SQLPlugin is called before GS SQLPlugout and attempts to use_db() - called in plugin's
    260 # init() method. This will fail if the db does not exist. Ideally want the gssqlplugin only called
    261 # during buildcol.pl
    262 sub use_db {
    263     my $self= shift (@_);
    264     my ($db_name) = @_;
    265     my $dbh = $self->{'db_handle'};
    266     $db_name = $self->sanitize_name($db_name);
    267 
    268    
    269     print STDERR "Loading database $db_name\n" if($self->{'verbosity'} > 1);
    270    
    271     # perl DBI switch database: https://www.perlmonks.org/?node_id=995434
    272     # do() returns undef on error.
    273     # connection succeeded, try to load our database. If that didn't work, attempt to create db
    274     return $dbh->do("use $db_name") || warn();
     231# We should already have done "use <database>;" if this gets called.
     232# Just load this collection's metatable
     233sub ensure_meta_table_exists {
     234    my $self = shift (@_);
     235   
     236    my $tablename = $self->get_metadata_table_name();
     237    if(!$self->table_exists($tablename)) {
     238    #print STDERR "   Creating metadata table $tablename\n" if($self->{'verbosity'} > 1);
     239    $self->create_metadata_table() || return 0;
     240    } else {
     241    print STDERR "@@@ Meta table exists\n" if($self->{'verbosity'} > 2);
     242    }
     243    return 1;
     244}
     245
     246# We should already have done "use <database>;" if this gets called.
     247# Just load this collection's metatable
     248sub ensure_fulltxt_table_exists {
     249    my $self = shift (@_);
     250   
     251    my $tablename = $self->get_fulltext_table_name();   
     252    if(!$self->table_exists($tablename)) {
     253    #print STDERR "   Creating fulltxt table $tablename\n" if($self->{'verbosity'} > 1);
     254    $self->create_fulltext_table() || return 0;
     255    } else {
     256    print STDERR "@@@ Fulltxt table exists\n" if($self->{'verbosity'} > 2);
     257    }
     258    return 1;
    275259}
    276260
Note: See TracChangeset for help on using the changeset viewer.