Changeset 32563 for main/trunk/greenstone2/perllib/gssql.pm
- Timestamp:
- 2018-11-02T19:07:16+13:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/perllib/gssql.pm
r32561 r32563 44 44 45 45 # Parameterise (one or more methods may use them): 46 # - build_mode (like removeold)47 46 # - db_name (which is the GS3 sitename) 48 47 … … 134 133 print STDERR "Away to make connection to $db_driver database with:\n"; 135 134 print STDERR " - hostname $db_host; username: $db_user"; 136 print STDERR "; and the p assword provided" if $db_pwd;135 print STDERR "; and the provided password" if $db_pwd; 137 136 print STDERR "\nAssuming the mysql server has been started with: --character_set_server=utf8mb4\n" if $db_driver eq "mysql"; 138 137 } … … 188 187 } 189 188 190 # will attempt to load the specified db and the <coll>_metadata and <coll>_fulltxt for this 191 # collection, or create any of these (db, tables) that don't yet exist. At the end 192 # it will have loaded the requested database (in MySQL: "use <db>;") 193 sub load_db_and_tables { 194 my $self= shift (@_); 195 my ($db_name, $build_mode) = @_; 189 # Load the designated database, i.e. 'use <dbname>;'. 190 # If the database doesn't yet exist, creates it and loads it. 191 # (Don't create the collection's tables yet, though) 192 # At the end it will have loaded the requested database (in MySQL: "use <db>;") on success. 193 # As usual, returns success or failure value that can be evaluated in a boolean context. 194 sub use_db { 195 my $self= shift (@_); 196 my ($db_name) = @_; 196 197 my $dbh = $self->{'db_handle'}; 197 198 $db_name = $self->sanitize_name($db_name); … … 215 216 # once more attempt to use db, now that it exists 216 217 $dbh->do("use $db_name") || return 0; 217 #$dbh->do("use localsite") or die "Error (code" . $dbh->err ."): " . $dbh->errstr . "\n"; 218 219 # attempt to create tables in current db 220 $self->create_metadata_table() || return 0; 221 $self->create_fulltext_table() || return 0; 218 #$dbh->do("use $db_name") or die "Error (code" . $dbh->err ."): " . $dbh->errstr . "\n"; 222 219 223 220 $success = 1; … … 227 224 228 225 print STDERR "@@@ DATABASE $db_name EXISTED\n" if($self->{'verbosity'} > 2); 229 230 231 # build_mode can be removeold or incremental. We only do something special on removeold:232 # deleting the existing tables for this collection and recreating empty ones233 if($build_mode eq "removeold") {234 print STDERR " Building with removeold option set, so deleting current collection's tables if they exist\n" if($self->{'verbosity'});235 $self->delete_collection_tables();236 }237 238 # use existing tables if any239 # attempt to create tables in current db240 if($build_mode eq "removeold" || !$self->table_exists($self->get_metadata_table_name())) {241 $self->create_metadata_table() || return 0;242 } else {243 print STDERR "@@@ Meta table exists\n" if($self->{'verbosity'} > 2);244 }245 if($build_mode eq "removeold" || !$self->table_exists($self->get_fulltext_table_name())) {246 $self->create_fulltext_table() || return 0;247 } else {248 print STDERR "@@@ Fulltxt table exists\n" if($self->{'verbosity'} > 2);249 }250 251 226 } 252 227 … … 254 229 } 255 230 256 # GreenstoneSQLPlugin calls this method to load an existing db. 257 # This will terminate if the db does not exist. Unlike load_db_and_tables() above, used by 258 # GreenstoneSQLPlugout, this method will not attempt to create the requested db (nor its tables) 259 # TODO: GS SQLPlugin is called before GS SQLPlugout and attempts to use_db() - called in plugin's 260 # init() method. This will fail if the db does not exist. Ideally want the gssqlplugin only called 261 # during buildcol.pl 262 sub use_db { 263 my $self= shift (@_); 264 my ($db_name) = @_; 265 my $dbh = $self->{'db_handle'}; 266 $db_name = $self->sanitize_name($db_name); 267 268 269 print STDERR "Loading database $db_name\n" if($self->{'verbosity'} > 1); 270 271 # perl DBI switch database: https://www.perlmonks.org/?node_id=995434 272 # do() returns undef on error. 273 # connection succeeded, try to load our database. If that didn't work, attempt to create db 274 return $dbh->do("use $db_name") || warn(); 231 # We should already have done "use <database>;" if this gets called. 232 # Just load this collection's metatable 233 sub ensure_meta_table_exists { 234 my $self = shift (@_); 235 236 my $tablename = $self->get_metadata_table_name(); 237 if(!$self->table_exists($tablename)) { 238 #print STDERR " Creating metadata table $tablename\n" if($self->{'verbosity'} > 1); 239 $self->create_metadata_table() || return 0; 240 } else { 241 print STDERR "@@@ Meta table exists\n" if($self->{'verbosity'} > 2); 242 } 243 return 1; 244 } 245 246 # We should already have done "use <database>;" if this gets called. 247 # Just load this collection's metatable 248 sub ensure_fulltxt_table_exists { 249 my $self = shift (@_); 250 251 my $tablename = $self->get_fulltext_table_name(); 252 if(!$self->table_exists($tablename)) { 253 #print STDERR " Creating fulltxt table $tablename\n" if($self->{'verbosity'} > 1); 254 $self->create_fulltext_table() || return 0; 255 } else { 256 print STDERR "@@@ Fulltxt table exists\n" if($self->{'verbosity'} > 2); 257 } 258 return 1; 275 259 } 276 260
Note:
See TracChangeset
for help on using the changeset viewer.