Index: main/trunk/greenstone2/perllib/plugouts/GreenstoneSQLPlugout.pm
===================================================================
--- main/trunk/greenstone2/perllib/plugouts/GreenstoneSQLPlugout.pm	(revision 32534)
+++ main/trunk/greenstone2/perllib/plugouts/GreenstoneSQLPlugout.pm	(revision 32536)
@@ -40,5 +40,5 @@
 
 # TODO: SIGTERM rollback and disconnect?
-
+# TODO Q: what about verbosity for debugging
 
 # this plugout does not output xml to a file, but outputs rows into a mysql table
@@ -146,7 +146,9 @@
     if(!$gs_sql->load_db_and_tables($db_name, $build_mode)) {
 	
-	# This is fatal for the plugout, let's terminate here
-	# PrintError would already have displayed the warning message on connection fail	
-	die("Could not use db or prepare its tables. Can't proceed.\n");
+	# This is fatal for the plugout, let's terminate here after disconnecting again
+	# PrintError would already have displayed the warning message on load fail
+	$gs_sql->disconnect_from_db()
+	    || warn("Unable to disconnect from database " . $self->{'site_name'} . "\n");
+	die("Could not use db $db_name and/or prepare its tables. Can't proceed.\n");
     }
 
@@ -184,17 +186,13 @@
     $self->{'gs_sql'}->disconnect_from_db() || warn("Unable to disconnect from database " . $self->{'site_name'} . "\n"); # disconnect_from_db() will also issue a warning, but this may be clearer
 }
- 
-# produce files called docsql.xml instead of doc.xml
-sub get_short_doc_file {
+
+# produce files called docsql-<OID>.xml instead of doc.xml
+sub get_doc_xml_filename {
     my $self = shift (@_);
-    my ($doc_dir) = @_;
-    return &FileUtils::filenameConcatenate($doc_dir, "docsql.xml");
-}
-
-# produce files called docsql.xml instead of doc.xml
-sub get_output_file {
-    my $self = shift (@_);
-    my ($doc_dir) = @_;
-    return &FileUtils::filenameConcatenate($self->{'output_dir'}, $doc_dir, "docsql.xml");
+    my ($doc_obj) = @_;
+    
+    my $doc_id = $doc_obj->get_OID();
+    my $filename = $doc_id ? "docsql-".$doc_id.".xml" : "docsql.xml";
+    return $filename;
 }
    
@@ -204,5 +202,5 @@
     my ($doc_obj, $doc_dir) = @_;
 
-    print STDERR "\n\n@@@ In saveas\n\n";
+#    print STDERR "\n\n@@@ In saveas\n\n";
     
     my $proc_mode = $self->{'process_mode'};
@@ -291,4 +289,6 @@
 	foreach my $data (@{$section_ptr->{'metadata'}}) {
 	    my $meta_name = $data->[0];
+	    # TODO: does it need to be stored escaped, as it requires unescaping when read back in
+	    # from db (unlike for reading back in from doc.xml)
 	    my $escaped_meta_value = &docprint::escape_text($data->[1]);
 
Index: main/trunk/greenstone2/perllib/plugouts/GreenstoneXMLPlugout.pm
===================================================================
--- main/trunk/greenstone2/perllib/plugouts/GreenstoneXMLPlugout.pm	(revision 32534)
+++ main/trunk/greenstone2/perllib/plugouts/GreenstoneXMLPlugout.pm	(revision 32536)
@@ -94,5 +94,6 @@
 	    $outhandler = $self->{'group_outhandler'};
 	} else {
-	    $output_file = $self->get_output_file($doc_dir); #&FileUtils::filenameConcatenate($self->{'output_dir'}, $doc_dir, "doc.xml");
+	    $output_file = &FileUtils::filenameConcatenate(
+		$self->{'output_dir'}, $doc_dir, $self->get_doc_xml_filename($doc_obj));
 	    # open the new handle
 	    $self->open_xslt_pipe($output_file, $self->{'xslt_file'});
@@ -134,5 +135,6 @@
 	}
     }
-    $self->{'short_doc_file'} = $self->get_short_doc_file($doc_dir); #&FileUtils::filenameConcatenate($doc_dir, "doc.xml");  
+    $self->{'short_doc_file'} = &FileUtils::filenameConcatenate(
+	$doc_dir, $self->get_doc_xml_filename($doc_obj));  
     
     $self->store_output_info_reference($doc_obj);
@@ -142,16 +144,8 @@
 # can be overridden in subclasses, for instance by GreenstoneSQLPlugout, to produce a different filename
 # like docsql.xml
-sub get_short_doc_file {
-    my $self = shift (@_);
-    my ($doc_dir) = @_;
-    return &FileUtils::filenameConcatenate($doc_dir, "doc.xml");
-}
-
-# can be overridden in subclasses, for instance by GreenstoneSQLPlugout, to produce a different filename
-# like docsql.xml
-sub get_output_file {
-    my $self = shift (@_);
-    my ($doc_dir) = @_;
-    return &FileUtils::filenameConcatenate($self->{'output_dir'}, $doc_dir, "doc.xml");
+sub get_doc_xml_filename {
+    my $self = shift (@_);
+    my ($doc_obj) = @_;
+    return "doc.xml";
 }
 
@@ -175,5 +169,6 @@
 	    $outhandler = $self->{'group_outhandler'};
 	} else {
-	    $output_file = $self->get_output_file($doc_dir); #&FileUtils::filenameConcatenate($self->{'output_dir'}, $doc_dir, "doc.xml");
+	    $output_file = $output_file = &FileUtils::filenameConcatenate(
+		$self->{'output_dir'}, $doc_dir, $self->get_doc_xml_filename($doc_obj));
 	    # open the new handle
 	    $self->open_xslt_pipe($output_file, $self->{'xslt_file'});
@@ -235,5 +230,6 @@
 	}
     }
-    $self->{'short_doc_file'} = $self->get_short_doc_file($doc_dir); #&FileUtils::filenameConcatenate($doc_dir, "doc.xml");  
+    $self->{'short_doc_file'} = &FileUtils::filenameConcatenate(
+	$doc_dir, $self->get_doc_xml_filename($doc_obj));
     
     $self->store_output_info_reference($doc_obj);    
