Changeset 32530

Show
Ignore:
Timestamp:
19.10.2018 20:54:17 (4 weeks ago)
Author:
ak19
Message:

Some more tidying up: some params passed by GreenstoneSQLPlugout to gssql should be instance variables, others are connection specific and now restricted to the gssql::connect_to_db() method.

Location:
main/trunk/greenstone2/perllib
Files:
2 modified

Legend:

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

    r32529 r32530  
    11########################################################################### 
    22# 
    3 # gssql.pm -- DBI for SQL related utility functions used by GreenstoneSQL 
    4 # Plugin and Plugout. 
     3# gssql.pm -- DBI for SQL related utility functions used by 
     4# GreenstoneSQLPlugout and hereafter by GreenstoneSQLPlugin too. 
    55# A component of the Greenstone digital library software 
    66# from the New Zealand Digital Library Project at the  
     
    3838# Need params_map keys: 
    3939# - collection_name 
    40 # X parameterise: - build_mode 
    41 # For MySQL: 
    42 #  - db_encoding,  
     40# - db_encoding (db content encoding) - MySQL can set this at server, db, table levels. For MySQL 
     41# we set the enc during connect at server level. Not sure whether other DB's support it at the 
     42# same levels. 
     43 
     44# For connection to MySQL, need: 
    4345#  - db_driver, db_client_user, db_client_pwd, db_host, (db_port not used at present) 
    44 # X parameterise: - db_name (which is the GS3 sitename),  
     46# So these will be parameterised, but in a hashmap, for just the connect method. 
     47 
     48# Parameterise (one or more methods may use them): 
     49# - build_mode (like removeold) 
     50# - db_name (which is the GS3 sitename) 
     51 
    4552# TODO: add infrastructure for db_port, AutoCommit etc 
    4653# For port, see https://stackoverflow.com/questions/2248665/perl-script-to-connect-to-mysql-server-port-3307 
     
    94101sub connect_to_db { 
    95102    my $self= shift (@_); 
    96     #my ($db_name) = @_; 
    97     my $db_driver = $self->{'db_driver'} || "mysql"; 
    98     my $db_user = $self->{'db_client_user'} || "root"; 
    99     my $db_pwd = $self->{'db_client_pwd'}; # even if undef, we'll see a sensible error message 
     103    my ($params_map) = @_; 
     104    my $db_enc = $self->{'db_encoding'} || "utf8"; 
     105 
     106    # these are the params for connecting to MySQL 
     107    my $db_driver = $params_map->{'db_driver'} || "mysql"; 
     108    my $db_user = $params_map->{'db_client_user'} || "root"; 
     109    my $db_pwd = $params_map->{'db_client_pwd'}; # even if undef, we'll see a sensible error message 
    100110                                           # when connect fails 
    101     my $db_host = $self->{'db_host'} || "127.0.0.1"; 
    102     my $db_enc = $self->{'db_encoding'} || "utf8"; 
    103      
    104     # try connecting to the mysql db, if that fails it will die 
    105     # so don't bother preparing GreenstoneXMLPlugout by calling superclass' begin() 
    106  
     111    my $db_host = $params_map->{'db_host'} || "127.0.0.1"; 
    107112    # localhost doesn't work for us, but 127.0.0.1 works 
    108113    # https://metacpan.org/pod/DBD::mysql 
     
    111116    # on the local machine via TCP, you must specify the loopback IP address (127.0.0.1) as the host." 
    112117    #my $connect_str = "dbi:$db_driver:database=$db_name;host=$db_host"; 
    113     my $connect_str = "dbi:$db_driver:host=$db_host"; # don't provide db, so we can check the db is there 
     118    my $connect_str = "dbi:$db_driver:host=$db_host"; # don't provide db - allows checking the db exists 
    114119    my $dbh = DBI->connect("$connect_str", $db_user, $db_pwd, 
    115120               { 
     
    194199# this will terminate if the db does not exist 
    195200# it will not attempt to create the requested db (nor its tables) 
     201# The upcoming GreenstoneSQLPlugin can use this. 
    196202sub use_db { 
    197203    my $self= shift (@_); 
  • main/trunk/greenstone2/perllib/plugouts/GreenstoneSQLPlugout.pm

    r32529 r32530  
    119119 
    120120    my $db_params = { 
    121     'collection_name' => $ENV{'GSDLCOLLECTION'}, 
    122     'db_driver' => $self->{'db_driver'}, 
    123     'db_client_user' => $self->{'db_client_user'}, 
    124     'db_client_pwd' => $self->{'db_client_pwd'}, 
    125     'db_host' => $self->{'db_host'}, 
     121    'collection_name' => $ENV{'GSDLCOLLECTION'},     
    126122    'db_encoding' => $self->{'db_encoding'} 
    127123    #'db_name' => $self->{'site_name'}, 
     
    131127    my $gs_sql = new gssql($db_params); 
    132128     
    133     if(!$gs_sql->connect_to_db()) { 
     129    # try connecting to the mysql db, if that fails it will die 
     130    # so don't bother preparing GreenstoneXMLPlugout by calling superclass' begin() 
     131    if(!$gs_sql->connect_to_db({ 
     132    'db_driver' => $self->{'db_driver'}, 
     133    'db_client_user' => $self->{'db_client_user'}, 
     134    'db_client_pwd' => $self->{'db_client_pwd'}, 
     135    'db_host' => $self->{'db_host'} 
     136                   }) 
     137    ) 
     138    { 
    134139    # This is fatal for the plugout, let's terminate here 
    135140    # PrintError would already have displayed the warning message on connection fail