Small problem in contrib/dbase/dbf2pg.c - Mailing list pgsql-bugs

From Thomas Behr
Subject Small problem in contrib/dbase/dbf2pg.c
Date
Msg-id 3F16D780.5880.23B6DDB@localhost
Whole thread Raw
Responses Re: Small problem in contrib/dbase/dbf2pg.c
List pgsql-bugs
Your name               : Thomas Behr
Your email address      : ------.----@bnv-bamberg.de
System Configuration
---------------------
  Architecture (example: Intel Pentium)         :   ALL
  Operating System (example: Linux 2.0.26 ELF)  :   ALL
  PostgreSQL version (example: PostgreSQL-7.3.3):   PostgreSQL-7.3.3
  Compiler used (example:  gcc 2.95.2)          :   WorkShop Compilers 5.0 98/12/15 C 5.0
Please enter a FULL description of your problem:
------------------------------------------------
  dbf2pg  -  Insert  xBase-style  .dbf-files  into  a PostgreSQL-table
  There is an option "-s oldname=newname", which changes the old field name of
  the dbf-file to the newname in PostgeSQL. If the length of the new name is 0,
  the field is skiped. If you want to skip the first field of the dbf-file,
  you get the wildest error-messages from the backend.
  dbf2pg load the dbf-file via "COPY tablename FROM STDIN". If you skip the
  first field, it is an \t to much in STDIN.
Please describe a way to repeat the problem.   Please try to provide a
concise reproducible example, if at all possible:
----------------------------------------------------------------------
  dbf2pg -s first_field_name=,other_fieldname=,reserved_fieldname=new_fieldname
 -c -d testdb -t testtable -h dbhost -U testuser -F IBM437 -T ISO-8859-1 -vv
test.dbf

If you know how this problem might be fixed, list the solution below:
---------------------------------------------------------------------

    440     /* build line and submit */
    441     result = dbf_get_record(dbh, fields, i);
    442     if (result == DBF_VALID)
    443     {
    444       query[0] = '\0';
    445       for (h = 0; h < dbh->db_nfields; h++)
    446       {
    447         if (!strlen(fields[h].db_name))
    448           continue;
    449
    450         if (h != 0)   /* not for the first field! */
    451           strcat(query, "\t");    /* COPY statement field
    452                          * separator */



A fix could be an counter j=0, which increments only, if a field is imported
(IF (strlen(fields[h].db_name)> 0) j++. And only if j > 1 (if an other field is
imported) the \t is printed.

...
int j;
...

 if (result == DBF_VALID)
    {
      query[0] = '\0';
      j = 0;
      for (h = 0; h < dbh->db_nfields; h++)
      {
        if (!strlen(fields[h].db_name))
        {
          continue;
        }
        else
        {
          j++;
        }

        if (j > 1)   /* not for the first field! */
          strcat(query, "\t");    /* COPY statement field
                         * separator */


An other small bug in the README:
-s start
              Specify  the  first record-number in the xBase-file
              we will insert.
should be
-e start
              Specify  the  first record-number in the xBase-file
              we will insert.

pgsql-bugs by date:

Previous
From: Troels Arvin
Date:
Subject: pg_dump handling of trim()
Next
From: Tom Lane
Date:
Subject: Re: [GENERAL] INSTEAD rule bug?