Changeset 32588

Show
Ignore:
Timestamp:
08.11.2018 19:09:57 (12 days ago)
Author:
ak19
Message:

Tidying up

Files:
1 modified

Legend:

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

    r32585 r32588  
    3333use DBI; # the central package for this module used by GreenstoneSQL Plugout and Plugin 
    3434 
     35################# 
     36# Database functions that use the perl DBI module (with the DBD driver module for mysql) 
     37# https://metacpan.org/pod/release/TIMB/DBI-1.634_50/DBI.pm 
     38# https://metacpan.org/pod/DBD::mysql 
     39################# 
    3540 
    3641############################## 
    3742 
    38 # TODO: add infrastructure for db_port, AutoCommit etc 
     43# TODO Q: If disconnect is automatically called when object destroyed, what does that mean 
     44# for our file-global handle object, is disconnect only called at end of perl process? 
     45# Does that mean we don't need to explicitly call disconnect in gssql object's destroy during 
     46# the GLOBAL destruction phase? 
     47# https://perldoc.perl.org/perlobj.html#Destructors 
     48 
     49#+ TODO: add infrastructure for db_port, AutoCommit etc 
    3950# For port, see https://stackoverflow.com/questions/2248665/perl-script-to-connect-to-mysql-server-port-3307 
    4051 
    4152# + TODO: remove unnecessary warn() since PrintError is active 
    42 # https://perldoc.perl.org/perlobj.html#Destructors 
    4353 
    4454# TODO: drop table if exists and create table if exists are available in MySQL. Use those cmds 
    4555# instead of always first checking for existence ourselves? Only when subclassing to specific 
    46 # mysql class. 
     56# mysql class? 
     57 
     58 
     59# + TODO Q: What on cancelling a build: delete table? But what if it was a rebuild and the rebuild is cancelled (not the original build)? 
     60# Do we create a copy of the orig database as backup, then start populating current db, and if cancelled, delete current db and RENAME backup table to current?  
     61# https://stackoverflow.com/questions/3280006/duplicating-a-mysql-table-indexes-and-data 
     62# BUT what if the table is HUGE? (Think of a collection with millions of docs.) Huge overhead in copying? 
     63# The alternative is we just quit on cancel, but then: cancel could leave the table in a partial committed state, with no way of rolling back. 
     64# Unless they do a full rebuild, which will recreate the table from scratch? 
     65# SOLUTION-> rollback transaction on error, see https://www.effectiveperlprogramming.com/2010/07/set-custom-dbi-error-handlers/ 
     66# In that case, should set AutoCommit to off on connection, and remember to commit at end. 
     67 
     68# + TODO: Consider AutoCommit status (and Autocommit off allowing commit or rollback for GS coll build cancel) later 
     69 
     70 
    4771############################## 
    4872 
     
    6488# - db_name (which is the GS3 sitename, or "greenstone2" for GS2) 
    6589 
    66  
    67  
     90# Database access related functions 
     91# http://g2pc1.bu.edu/~qzpeng/manual/MySQL%20Commands.htm 
     92# https://www.guru99.com/insert-into.html 
     93 
     94# Add signal handlers to cleanup and disconnect from db on sudden termination, incl cancel build 
     95# https://www.perl.com/article/37/2013/8/18/Catch-and-Handle-Signals-in-Perl/ 
    6896$SIG{INT}  = \&finish_signal_handler; 
    6997$SIG{TERM}  = \&finish_signal_handler; 
     
    75103    if ($_dbh_instance) { # database handle (note, using singleton) still active. 
    76104     
    77     # TODO: If autocommit wasn't set, then this is a cancel operation. 
     105    # If autocommit wasn't set, then this is a cancel operation. 
    78106    # If we've not disconnected from the sql db yet and if we've not committed 
    79107    # transactions yet, then cancel means we do a rollback here 
     
    126154# 
    127155# Useful: https://perldoc.perl.org/perlobj.html#Destructors 
     156# For more on when destroy is called, see https://www.perlmonks.org/?node_id=1020920 
    128157# 
    129158# https://metacpan.org/pod/release/TIMB/DBI-1.634_50/DBI.pm#disconnect 
     
    169198    # "Always include a call to $self->SUPER::DESTROY in our destructors (even if we don't yet have any base/parent classes). (p. 145)" 
    170199    # Superclass and destroy, call to SUPER: https://www.perlmonks.org/?node_id=879920 
    171     # also covers multiple-inheritance (MI)     
     200    # discussion also covers multiple-inheritance (MI)     
    172201    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY"); 
    173202 
    174203} 
    175204 
    176 ################################# 
    177  
    178 # Database access related functions 
    179 # http://g2pc1.bu.edu/~qzpeng/manual/MySQL%20Commands.htm 
    180 # https://www.guru99.com/insert-into.html 
    181  
    182 # TODO Q: What on cancelling a build: delete table? But what if it was a rebuild and the rebuild is cancelled (not the original build)? 
    183 # Do we create a copy of the orig database as backup, then start populating current db, and if cancelled, delete current db and RENAME backup table to current?  
    184 # https://stackoverflow.com/questions/3280006/duplicating-a-mysql-table-indexes-and-data 
    185 # BUT what if the table is HUGE? (Think of a collection with millions of docs.) Huge overhead in copying? 
    186 # The alternative is we just quit on cancel, but then: cancel could leave the table in a partial committed state, with no way of rolling back. 
    187 # Unless they do a full rebuild, which will recreate the table from scratch? 
    188 # SOLUTION-> rollback transaction on error, see https://www.effectiveperlprogramming.com/2010/07/set-custom-dbi-error-handlers/ 
    189 # But then should set AutoCommit to off on connection, and remember to commit every time 
    190  
    191 ################# 
    192 # Database functions that use the perl DBI module (with the DBD driver module for mysql) 
    193 ################# 
     205 
    194206 
    195207################### BASIC DB OPERATIONS ################## 
     
    198210# NOTE: FULLTEXT is a reserved keyword in (My)SQL. So we can't name a table or any of its columns "fulltext". 
    199211# https://dev.mysql.com/doc/refman/5.5/en/keywords.html 
    200  
    201 # TODO: Consider AutoCommit status (and Autocommit off allowing commit or rollback for GS coll build cancel) later 
    202212 
    203213 
     
    279289    # DBI AutoCommit connection param is on/1 by default, so if a value for this is not defined 
    280290    # as a method parameter to _get_connection_instance, then fallback to the default of on/1 
     291    # More: https://www.oreilly.com/library/view/programming-the-perl/1565926994/re44.html 
    281292    my $autocommit = (defined $params_map->{'autocommit'}) ? $params_map->{'autocommit'} : 1; 
    282293