- Timestamp:
- 2018-11-08T19:09:57+13:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/perllib/gssql.pm
r32585 r32588 33 33 use DBI; # the central package for this module used by GreenstoneSQL Plugout and Plugin 34 34 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 ################# 35 40 36 41 ############################## 37 42 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 39 50 # For port, see https://stackoverflow.com/questions/2248665/perl-script-to-connect-to-mysql-server-port-3307 40 51 41 52 # + TODO: remove unnecessary warn() since PrintError is active 42 # https://perldoc.perl.org/perlobj.html#Destructors43 53 44 54 # TODO: drop table if exists and create table if exists are available in MySQL. Use those cmds 45 55 # 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 47 71 ############################## 48 72 … … 64 88 # - db_name (which is the GS3 sitename, or "greenstone2" for GS2) 65 89 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/ 68 96 $SIG{INT} = \&finish_signal_handler; 69 97 $SIG{TERM} = \&finish_signal_handler; … … 75 103 if ($_dbh_instance) { # database handle (note, using singleton) still active. 76 104 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. 78 106 # If we've not disconnected from the sql db yet and if we've not committed 79 107 # transactions yet, then cancel means we do a rollback here … … 126 154 # 127 155 # Useful: https://perldoc.perl.org/perlobj.html#Destructors 156 # For more on when destroy is called, see https://www.perlmonks.org/?node_id=1020920 128 157 # 129 158 # https://metacpan.org/pod/release/TIMB/DBI-1.634_50/DBI.pm#disconnect … … 169 198 # "Always include a call to $self->SUPER::DESTROY in our destructors (even if we don't yet have any base/parent classes). (p. 145)" 170 199 # 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) 172 201 $self->SUPER::DESTROY if $self->can("SUPER::DESTROY"); 173 202 174 203 } 175 204 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 194 206 195 207 ################### BASIC DB OPERATIONS ################## … … 198 210 # NOTE: FULLTEXT is a reserved keyword in (My)SQL. So we can't name a table or any of its columns "fulltext". 199 211 # 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) later202 212 203 213 … … 279 289 # DBI AutoCommit connection param is on/1 by default, so if a value for this is not defined 280 290 # 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 281 292 my $autocommit = (defined $params_map->{'autocommit'}) ? $params_map->{'autocommit'} : 1; 282 293
Note:
See TracChangeset
for help on using the changeset viewer.