Ignore:
Timestamp:
2018-10-19T20:54:17+13:00 (6 years 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.

File:
1 edited

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 (@_);
Note: See TracChangeset for help on using the changeset viewer.