Index: main/trunk/greenstone2/perllib/gssql.pm
===================================================================
--- main/trunk/greenstone2/perllib/gssql.pm	(revision 32556)
+++ main/trunk/greenstone2/perllib/gssql.pm	(revision 32557)
@@ -110,5 +110,6 @@
     my $self= shift (@_);
     my ($params_map) = @_;
-    my $db_enc = $self->{'db_encoding'} || "utf8";
+    #my $db_enc = $self->{'db_encoding'} || "utf8";
+    my $db_enc = "utf8mb4";
 
     # these are the params for connecting to MySQL
@@ -130,9 +131,10 @@
 			       PrintError => 1, # on by default, but being explicit
 			       RaiseError => 0, # off by default, but being explicit
-			       AutoCommit => 1 # on by default, but being explicit
+			       AutoCommit => 1, # on by default, but being explicit
+			       mysql_enable_utf8mb4 => 1 # tells MySQL to use UTF-8 for communication and tells DBD::mysql to decode the data, see https://stackoverflow.com/questions/46727362/perl-mysql-utf8mb4-issue-possible-bug 
 			   });
 
     if(!$dbh) {
-	# NOTE, despite handle dbh being undefined, error code will be in DBI->err
+	# NOTE, despite handle dbh being undefined, error code will be in DBI->err (note caps)
 	return 0;	
     }
@@ -141,8 +143,31 @@
     # https://dev.mysql.com/doc/refman/5.7/en/charset.html
     # https://dev.mysql.com/doc/refman/5.7/en/charset-conversion.html
-    # Setting the encoding at db server level.
-    # Not sure if this command is mysql specific:
-    my $stmt = "set NAMES '" . $db_enc . "'";
-    $dbh->do($stmt) || warn("Unable to set charset encoding at db server level to: " . $db_enc . "\n");
+    # Setting the encoding at db server level: $dbh->do("set NAMES '" . $db_enc . "'");
+    # HOWEVER:
+    # It turned out insufficient setting the encoding to utf8, as that only supports utf8 chars that
+    # need up to 3 bytes. We may need up to 4 bytes per utf8 character, e.g. chars with macron,
+    # and for that, we need the encoding to be set to utf8mb4.
+    # To set up a MySQL db to use utf8mb4 requires configuration on the server side too.
+    # https://stackoverflow.com/questions/10957238/incorrect-string-value-when-trying-to-insert-utf-8-into-mysql-via-jdbc
+    # https://stackoverflow.com/questions/46727362/perl-mysql-utf8mb4-issue-possible-bug
+    # To set up the db for utf8mb4, therefore, 
+    # the MySQL server needs to be configured for that char encoding by running the server as:
+    # mysql-5.7.23-linux-glibc2.12-x86_64/bin>./mysqld_safe --datadir=/Scratch/ak19/mysql/data --character_set_server=utf8mb4
+    # AND when connecting to the server, we can can either set mysql_enable_utf8mb4 => 1
+    # as a connection option
+    # OR we need to do both "set NAMES utf8mb4" AND "$dbh->{mysql_enable_utf8mb4} = 1;" after connecting
+    #
+    # Search results for DBI Set Names imply the "SET NAMES '<enc>'" command is mysql specific too,
+    # so setting the mysql specific option during connection above as "mysql_enable_utf8mb4 => 1"
+    # is no more objectionable. It has the advantage of cutting out the 2 extra lines of doing
+    # set NAMES '<enc>' and $dbh->{mysql_enable_utf8mb4} = 1 here.
+    # These lines may be preferred if more db_driver options are to be supported in future:
+    # then a separate method called set_db_encoding($enc) can work out what db_driver we're using
+    # and if mysql and enc=utfy, then it can do the following whereas it will issue other do stmts
+    # for other db_drivers, see https://www.perlmonks.org/?node_id=259456:
+    
+    #my $stmt = "set NAMES '" . $db_enc . "'";
+    #$dbh->do($stmt) || warn("Unable to set charset encoding at db server level to: " . $db_enc . "\n"); # tells MySQL to use UTF-8 for communication
+    #$dbh->{mysql_enable_utf8mb4} = 1; # tells DBD::mysql to decode the data
     
     # if we're here, then connection succeeded, store handle
@@ -248,5 +273,5 @@
 sub create_db {
     my $self= shift (@_);
-    my $db_name = $self->{'db_name'};
+    my ($db_name) = @_;
     my $dbh = $self->{'db_handle'};
     
@@ -294,7 +319,11 @@
     # drop table <tablename>
     my $table = $self->get_metadata_table_name();
-    $dbh->do("drop table $table") || warn("@@@ Couldn't delete $table");
+    if($self->table_exists($table)) {
+	$dbh->do("drop table $table") || warn("@@@ Couldn't delete $table");
+    }
     $table = $self->get_fulltext_table_name();
-    $dbh->do("drop table $table") || warn("@@@ Couldn't delete $table");
+    if($self->table_exists($table)) {
+	$dbh->do("drop table $table") || warn("@@@ Couldn't delete $table");
+    }
 }
 
