Index: /branches/eam_branches/ipp-20150625/Ohana/src/tools/src/fits_to_mysql.c
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/tools/src/fits_to_mysql.c	(revision 38974)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/tools/src/fits_to_mysql.c	(revision 38975)
@@ -18,5 +18,5 @@
     tmp = Pin[3]; Pin[3] = Pin[4]; Pin[4] = tmp; }
 
-# define DEBUG 0
+# define DEBUG 1
 # define NMAX    0x100
 # define NBUFFER 0x100000
@@ -63,4 +63,7 @@
 int CopyIOBuffer (IOBuffer *output, IOBuffer *input);
 
+void replace_dot_with_underscore (char *string);
+
+void dump_on_failure (IOBuffer *buffer);
 MYSQL *mysql_connect (MYSQL *mysqlBase);
 int mysql_commit_buffer (IOBuffer *buffer, MYSQL *mysql);
@@ -242,4 +245,5 @@
     column[Ncolumn].rawname = strcreate (tlabel);
     column[Ncolumn].format = strcreate (format);
+    replace_dot_with_underscore (column[Ncolumn].colname);
 
     char type[16];
@@ -268,4 +272,21 @@
 }
 
+void PrintColumns (IOBuffer *buffer, char *typename, char *rootname, int Nval) {
+  if (Nval > 1) {
+    int j;
+    char colname[64];
+    for (j = 0; j < Nval; j++) {
+      snprintf (colname, 64, "%s_%d", rootname, j + 1);
+      PrintIOBuffer (buffer, "%s %s", colname, typename);
+      if (j < Nval - 1) {
+	PrintIOBuffer (buffer, ",\n");
+      }
+    }
+  } else {
+    PrintIOBuffer (buffer, "%s %s", rootname, typename);
+  }
+  return;
+}
+
 // the 'insert' buffer will be used to generate the insert lines downstream (call InitIOBuffer on it first)
 int create_table (ColumnDef *columns, int Ncolumns, char *extname, IOBuffer *insert, MYSQL *mysql) {
@@ -277,11 +298,5 @@
 
   char *tablename = strcreate (extname);
-
-  // convert "." in tablename to _
-  char *p = tablename;
-  while (p && *p) {
-    if (*p == '.') *p = '_';
-    p++;
-  }
+  replace_dot_with_underscore (tablename);
 
   // drop the table if it exists
@@ -291,4 +306,5 @@
     fprintf (stderr, "failed to drop table:\n");
     fprintf (stderr, "%s\n", mysql_error(mysql));
+    dump_on_failure (&buffer);
     return FALSE;
   }
@@ -306,20 +322,20 @@
     switch (columns[i].type) {
       case TYPE_BYTE:
-	PrintIOBuffer (&buffer, "%s TINYINT", columns[i].colname);
+	PrintColumns (&buffer, "SMALLINT", columns[i].colname, columns[i].Nval);
 	break;
       case TYPE_SHORT:
-	PrintIOBuffer (&buffer, "%s SMALLINT", columns[i].colname);
+	PrintColumns (&buffer, "SMALLINT", columns[i].colname, columns[i].Nval);
 	break;
       case TYPE_INT:
-	PrintIOBuffer (&buffer, "%s INT", columns[i].colname);
+	PrintColumns (&buffer, "INT", columns[i].colname, columns[i].Nval);
 	break;
       case TYPE_INT64:
-	PrintIOBuffer (&buffer, "%s BIGINT", columns[i].colname);
+	PrintColumns (&buffer, "BIGINT", columns[i].colname, columns[i].Nval);
 	break;
       case TYPE_FLOAT:
-	PrintIOBuffer (&buffer, "%s FLOAT", columns[i].colname);
+	PrintColumns (&buffer, "FLOAT", columns[i].colname, columns[i].Nval);
 	break;
       case TYPE_DOUBLE:
-	PrintIOBuffer (&buffer, "%s DOUBLE", columns[i].colname);
+	PrintColumns (&buffer, "DOUBLE", columns[i].colname, columns[i].Nval);
 	break;
       case TYPE_CHAR:
@@ -330,5 +346,18 @@
     }
 
-    PrintIOBuffer (insert, "%s", columns[i].colname);
+    if ((columns[i].Nval > 1) && (columns[i].type != TYPE_CHAR)) {
+      int j;
+      char colname[64];
+      for (j = 0; j < columns[i].Nval; j++) {
+	snprintf (colname, 64, "%s_%d", columns[i].colname, j + 1);
+	PrintIOBuffer (insert, "%s", colname);
+	if (j < columns[i].Nval - 1) {
+	  PrintIOBuffer (insert, ",\n");
+	}
+      }
+    } else {
+      PrintIOBuffer (insert, "%s", columns[i].colname);
+    }
+
     if (i == Ncolumns - 1)  {
       PrintIOBuffer (&buffer, ")\n");
@@ -340,5 +369,4 @@
   }
 
-  if (DEBUG) fprintf (stderr, "%s\n", buffer.buffer);
   status = mysql_query(mysql, buffer.buffer); 
   if (status) {
@@ -346,4 +374,5 @@
     fprintf (stderr, "%s\n", mysql_error(mysql));
     fprintf (stderr, "Nbuffer: %d\n", buffer.Nbuffer);
+    dump_on_failure (&buffer);
   }
 
@@ -388,5 +417,5 @@
 	  memset (line, 0, 128);
 	  memcpy (line, &value[i*columns[Nf].Nval], columns[Nf].Nval);
-	  PrintIOBuffer (&output, "%s", line);
+	  PrintIOBuffer (&output, "'%s'", line);
 	  break;
 	} 
@@ -395,4 +424,7 @@
 	  for (j = 0; j < columns[Nf].Nval; j++) {
 	    PrintIOBuffer (&output, "%hhd", value[i*columns[Nf].Nval + j]);
+	    if (j < columns[Nf].Nval - 1) {
+	      PrintIOBuffer (&output, ",\n");
+	    }
 	  }
 	  break;
@@ -402,4 +434,7 @@
 	  for (j = 0; j < columns[Nf].Nval; j++) {
 	    PrintIOBuffer (&output, "%hd", value[i*columns[Nf].Nval + j]);
+	    if (j < columns[Nf].Nval - 1) {
+	      PrintIOBuffer (&output, ",\n");
+	    }
 	  }
 	  break;
@@ -409,4 +444,7 @@
 	  for (j = 0; j < columns[Nf].Nval; j++) {
 	    PrintIOBuffer (&output, "%d", value[i*columns[Nf].Nval + j]);
+	    if (j < columns[Nf].Nval - 1) {
+	      PrintIOBuffer (&output, ",\n");
+	    }
 	  }
 	  break;
@@ -416,4 +454,7 @@
 	  for (j = 0; j < columns[Nf].Nval; j++) {
 	    PrintIOBuffer (&output, "%lld", (long long int) value[i*columns[Nf].Nval + j]);
+	    if (j < columns[Nf].Nval - 1) {
+	      PrintIOBuffer (&output, ",\n");
+	    }
 	  }
 	  break;
@@ -422,5 +463,13 @@
 	  float *value = data[Nf];
 	  for (j = 0; j < columns[Nf].Nval; j++) {
-	    PrintIOBuffer (&output, "%e", value[i*columns[Nf].Nval + j]);
+	    float myValue = value[i*columns[Nf].Nval + j];
+	    if (isfinite(myValue)) {
+	      PrintIOBuffer (&output, "%e", myValue);
+	    } else {
+	      PrintIOBuffer (&output, "-999");
+	    }
+	    if (j < columns[Nf].Nval - 1) {
+	      PrintIOBuffer (&output, ",\n");
+	    }
 	  }
 	  break;
@@ -430,4 +479,7 @@
 	  for (j = 0; j < columns[Nf].Nval; j++) {
 	    PrintIOBuffer (&output, "%e", value[i*columns[Nf].Nval + j]);
+	    if (j < columns[Nf].Nval - 1) {
+	      PrintIOBuffer (&output, ",\n");
+	    }
 	  }
 	  break;
@@ -443,5 +495,4 @@
     }
     if (output.Nbuffer > MAX_BUFFER) {
-      if (DEBUG) fprintf (stderr, "%s\n", output.buffer);
       mysql_commit_buffer (&output, mysql);
       CopyIOBuffer (&output, insert);
@@ -449,5 +500,4 @@
   }
 
-  if (DEBUG) fprintf (stderr, "%s\n", output.buffer);
   mysql_commit_buffer (&output, mysql);
   FreeIOBuffer (&output);
@@ -730,5 +780,4 @@
   if (mysql) {
     int status;
-    if (DEBUG) fprintf (stderr, "%s\n", buffer->buffer);
     status = mysql_query(mysql, buffer->buffer); 
     if (status) {
@@ -736,4 +785,5 @@
       fprintf (stderr, "%s\n", mysql_error(mysql));
       fprintf (stderr, "Nbuffer: %d\n", buffer->Nbuffer);
+      dump_on_failure (buffer);
     }
     result = mysql_store_result (mysql);
@@ -743,2 +793,24 @@
   return TRUE;
 }
+
+void dump_on_failure (IOBuffer *buffer) {
+
+  if (!DEBUG) return;
+
+  FILE *f = fopen ("dvopsps.dump.sql", "w");
+  fwrite (buffer->buffer, 1, buffer->Nbuffer, f);
+  fclose (f);
+  exit (1);
+}
+
+void replace_dot_with_underscore (char *string) {
+
+  // convert "." in string to "_"
+  char *p = string;
+  while (p && *p) {
+    if (*p == '.') *p = '_';
+    p++;
+  }
+
+  return;
+}
