IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 38975


Ignore:
Timestamp:
Oct 27, 2015, 2:41:20 PM (11 years ago)
Author:
eugene
Message:

fits_to_mysql basically works

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20150625/Ohana/src/tools/src/fits_to_mysql.c

    r38967 r38975  
    1818    tmp = Pin[3]; Pin[3] = Pin[4]; Pin[4] = tmp; }
    1919
    20 # define DEBUG 0
     20# define DEBUG 1
    2121# define NMAX    0x100
    2222# define NBUFFER 0x100000
     
    6363int CopyIOBuffer (IOBuffer *output, IOBuffer *input);
    6464
     65void replace_dot_with_underscore (char *string);
     66
     67void dump_on_failure (IOBuffer *buffer);
    6568MYSQL *mysql_connect (MYSQL *mysqlBase);
    6669int mysql_commit_buffer (IOBuffer *buffer, MYSQL *mysql);
     
    242245    column[Ncolumn].rawname = strcreate (tlabel);
    243246    column[Ncolumn].format = strcreate (format);
     247    replace_dot_with_underscore (column[Ncolumn].colname);
    244248
    245249    char type[16];
     
    268272}
    269273
     274void PrintColumns (IOBuffer *buffer, char *typename, char *rootname, int Nval) {
     275  if (Nval > 1) {
     276    int j;
     277    char colname[64];
     278    for (j = 0; j < Nval; j++) {
     279      snprintf (colname, 64, "%s_%d", rootname, j + 1);
     280      PrintIOBuffer (buffer, "%s %s", colname, typename);
     281      if (j < Nval - 1) {
     282        PrintIOBuffer (buffer, ",\n");
     283      }
     284    }
     285  } else {
     286    PrintIOBuffer (buffer, "%s %s", rootname, typename);
     287  }
     288  return;
     289}
     290
    270291// the 'insert' buffer will be used to generate the insert lines downstream (call InitIOBuffer on it first)
    271292int create_table (ColumnDef *columns, int Ncolumns, char *extname, IOBuffer *insert, MYSQL *mysql) {
     
    277298
    278299  char *tablename = strcreate (extname);
    279 
    280   // convert "." in tablename to _
    281   char *p = tablename;
    282   while (p && *p) {
    283     if (*p == '.') *p = '_';
    284     p++;
    285   }
     300  replace_dot_with_underscore (tablename);
    286301
    287302  // drop the table if it exists
     
    291306    fprintf (stderr, "failed to drop table:\n");
    292307    fprintf (stderr, "%s\n", mysql_error(mysql));
     308    dump_on_failure (&buffer);
    293309    return FALSE;
    294310  }
     
    306322    switch (columns[i].type) {
    307323      case TYPE_BYTE:
    308         PrintIOBuffer (&buffer, "%s TINYINT", columns[i].colname);
     324        PrintColumns (&buffer, "SMALLINT", columns[i].colname, columns[i].Nval);
    309325        break;
    310326      case TYPE_SHORT:
    311         PrintIOBuffer (&buffer, "%s SMALLINT", columns[i].colname);
     327        PrintColumns (&buffer, "SMALLINT", columns[i].colname, columns[i].Nval);
    312328        break;
    313329      case TYPE_INT:
    314         PrintIOBuffer (&buffer, "%s INT", columns[i].colname);
     330        PrintColumns (&buffer, "INT", columns[i].colname, columns[i].Nval);
    315331        break;
    316332      case TYPE_INT64:
    317         PrintIOBuffer (&buffer, "%s BIGINT", columns[i].colname);
     333        PrintColumns (&buffer, "BIGINT", columns[i].colname, columns[i].Nval);
    318334        break;
    319335      case TYPE_FLOAT:
    320         PrintIOBuffer (&buffer, "%s FLOAT", columns[i].colname);
     336        PrintColumns (&buffer, "FLOAT", columns[i].colname, columns[i].Nval);
    321337        break;
    322338      case TYPE_DOUBLE:
    323         PrintIOBuffer (&buffer, "%s DOUBLE", columns[i].colname);
     339        PrintColumns (&buffer, "DOUBLE", columns[i].colname, columns[i].Nval);
    324340        break;
    325341      case TYPE_CHAR:
     
    330346    }
    331347
    332     PrintIOBuffer (insert, "%s", columns[i].colname);
     348    if ((columns[i].Nval > 1) && (columns[i].type != TYPE_CHAR)) {
     349      int j;
     350      char colname[64];
     351      for (j = 0; j < columns[i].Nval; j++) {
     352        snprintf (colname, 64, "%s_%d", columns[i].colname, j + 1);
     353        PrintIOBuffer (insert, "%s", colname);
     354        if (j < columns[i].Nval - 1) {
     355          PrintIOBuffer (insert, ",\n");
     356        }
     357      }
     358    } else {
     359      PrintIOBuffer (insert, "%s", columns[i].colname);
     360    }
     361
    333362    if (i == Ncolumns - 1)  {
    334363      PrintIOBuffer (&buffer, ")\n");
     
    340369  }
    341370
    342   if (DEBUG) fprintf (stderr, "%s\n", buffer.buffer);
    343371  status = mysql_query(mysql, buffer.buffer);
    344372  if (status) {
     
    346374    fprintf (stderr, "%s\n", mysql_error(mysql));
    347375    fprintf (stderr, "Nbuffer: %d\n", buffer.Nbuffer);
     376    dump_on_failure (&buffer);
    348377  }
    349378
     
    388417          memset (line, 0, 128);
    389418          memcpy (line, &value[i*columns[Nf].Nval], columns[Nf].Nval);
    390           PrintIOBuffer (&output, "%s", line);
     419          PrintIOBuffer (&output, "'%s'", line);
    391420          break;
    392421        }
     
    395424          for (j = 0; j < columns[Nf].Nval; j++) {
    396425            PrintIOBuffer (&output, "%hhd", value[i*columns[Nf].Nval + j]);
     426            if (j < columns[Nf].Nval - 1) {
     427              PrintIOBuffer (&output, ",\n");
     428            }
    397429          }
    398430          break;
     
    402434          for (j = 0; j < columns[Nf].Nval; j++) {
    403435            PrintIOBuffer (&output, "%hd", value[i*columns[Nf].Nval + j]);
     436            if (j < columns[Nf].Nval - 1) {
     437              PrintIOBuffer (&output, ",\n");
     438            }
    404439          }
    405440          break;
     
    409444          for (j = 0; j < columns[Nf].Nval; j++) {
    410445            PrintIOBuffer (&output, "%d", value[i*columns[Nf].Nval + j]);
     446            if (j < columns[Nf].Nval - 1) {
     447              PrintIOBuffer (&output, ",\n");
     448            }
    411449          }
    412450          break;
     
    416454          for (j = 0; j < columns[Nf].Nval; j++) {
    417455            PrintIOBuffer (&output, "%lld", (long long int) value[i*columns[Nf].Nval + j]);
     456            if (j < columns[Nf].Nval - 1) {
     457              PrintIOBuffer (&output, ",\n");
     458            }
    418459          }
    419460          break;
     
    422463          float *value = data[Nf];
    423464          for (j = 0; j < columns[Nf].Nval; j++) {
    424             PrintIOBuffer (&output, "%e", value[i*columns[Nf].Nval + j]);
     465            float myValue = value[i*columns[Nf].Nval + j];
     466            if (isfinite(myValue)) {
     467              PrintIOBuffer (&output, "%e", myValue);
     468            } else {
     469              PrintIOBuffer (&output, "-999");
     470            }
     471            if (j < columns[Nf].Nval - 1) {
     472              PrintIOBuffer (&output, ",\n");
     473            }
    425474          }
    426475          break;
     
    430479          for (j = 0; j < columns[Nf].Nval; j++) {
    431480            PrintIOBuffer (&output, "%e", value[i*columns[Nf].Nval + j]);
     481            if (j < columns[Nf].Nval - 1) {
     482              PrintIOBuffer (&output, ",\n");
     483            }
    432484          }
    433485          break;
     
    443495    }
    444496    if (output.Nbuffer > MAX_BUFFER) {
    445       if (DEBUG) fprintf (stderr, "%s\n", output.buffer);
    446497      mysql_commit_buffer (&output, mysql);
    447498      CopyIOBuffer (&output, insert);
     
    449500  }
    450501
    451   if (DEBUG) fprintf (stderr, "%s\n", output.buffer);
    452502  mysql_commit_buffer (&output, mysql);
    453503  FreeIOBuffer (&output);
     
    730780  if (mysql) {
    731781    int status;
    732     if (DEBUG) fprintf (stderr, "%s\n", buffer->buffer);
    733782    status = mysql_query(mysql, buffer->buffer);
    734783    if (status) {
     
    736785      fprintf (stderr, "%s\n", mysql_error(mysql));
    737786      fprintf (stderr, "Nbuffer: %d\n", buffer->Nbuffer);
     787      dump_on_failure (buffer);
    738788    }
    739789    result = mysql_store_result (mysql);
     
    743793  return TRUE;
    744794}
     795
     796void dump_on_failure (IOBuffer *buffer) {
     797
     798  if (!DEBUG) return;
     799
     800  FILE *f = fopen ("dvopsps.dump.sql", "w");
     801  fwrite (buffer->buffer, 1, buffer->Nbuffer, f);
     802  fclose (f);
     803  exit (1);
     804}
     805
     806void replace_dot_with_underscore (char *string) {
     807
     808  // convert "." in string to "_"
     809  char *p = string;
     810  while (p && *p) {
     811    if (*p == '.') *p = '_';
     812    p++;
     813  }
     814
     815  return;
     816}
Note: See TracChangeset for help on using the changeset viewer.