Changeset 32561

Show
Ignore:
Timestamp:
31.10.2018 20:12:48 (3 weeks ago)
Author:
ak19
Message:

Not just tables, but database names can't have hyphens in them when using MySQL.

Files:
1 modified

Legend:

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

    r32560 r32561  
    5151 
    5252sub new 
    53 { 
    54    
     53 
    5554    my $class = shift(@_); 
    5655     
     
    7069    }; 
    7170 
    72     # (My)SQL doesn't like tables with - (hyphens) in their names 
    73     my $coll_name = $params_map->{'collection_name'}; 
    74     $coll_name =~ s/-/_/g; 
    75     $self->{'tablename_prefix'} = $coll_name; 
    76  
    7771    # The db_encoding option is presently not passed in to this constructor as parameter. 
    7872    # Placed here to indicate it's sort of optional. 
     
    8175    $self->{'db_encoding'} = $params_map->{'db_encoding'} || "utf8"; 
    8276     
    83     return bless($self, $class); 
     77    $self = bless($self, $class);     
     78 
     79    $self->{'tablename_prefix'} = $self->sanitize_name($params_map->{'collection_name'}); 
     80     
     81    return $self; 
    8482} 
    8583 
     
    197195    my ($db_name, $build_mode) = @_; 
    198196    my $dbh = $self->{'db_handle'}; 
     197    $db_name = $self->sanitize_name($db_name); 
    199198     
    200199    print STDERR "Attempting to use database $db_name\n" if($self->{'verbosity'}); 
     
    207206    if(!$success && $dbh->err == 1049) { # "Unknown database" error has code 1049 (mysql only?) meaning db doesn't exist yet 
    208207 
    209     print STDERR "Database $db_name didn't exist, creating it and the tables for the current collection...\n" if($self->{'verbosity'}); 
     208    print STDERR "Database $db_name didn't exist, creating it along with the tables for the current collection...\n" if($self->{'verbosity'}); 
    210209     
    211210    # attempt to create the db and its tables 
     
    265264    my ($db_name) = @_; 
    266265    my $dbh = $self->{'db_handle'}; 
    267  
     266    $db_name = $self->sanitize_name($db_name); 
     267 
     268     
    268269    print STDERR "Loading database $db_name\n" if($self->{'verbosity'} > 1); 
    269270     
     
    299300    my ($db_name) = @_; 
    300301    my $dbh = $self->{'db_handle'}; 
     302    $db_name = $self->sanitize_name($db_name); 
    301303     
    302304    # https://stackoverflow.com/questions/5025768/how-can-i-create-a-mysql-database-from-a-perl-script 
     
    358360    my ($db_name) = @_; 
    359361    my $dbh = $self->{'db_handle'}; 
    360  
     362    $db_name = $self->sanitize_name($db_name); 
     363     
    361364    print STDERR "!!! Deleting database $db_name\n" if($self->{'verbosity'}); 
    362365     
     
    511514} 
    512515 
     516# Attempt to make sure the name parameter (for db or table name) is acceptable syntax 
     517# for the db in question, e.g. for mysql. For example, (My)SQL doesn't like tables or 
     518# databases with '-' (hyphens) in their names 
     519sub sanitize_name { 
     520    my $self= shift (@_); 
     521    my ($name) = @_; 
     522    $name =~ s/-/_/g; 
     523    return $name; 
     524} 
     525 
    513526 
    514527# I can get my version of table_exists to work, but it's not so ideal