In function "countries_api_csv_import_countries()" in "countries_api.module" each field has a test for empty fields from the CSV file, eg.:

$iso3 = ($row[3]) != 'NULL') ? $row[3] : "";

But as far as i can tell, empty fields in the CSV file have the entry "NULL", e.g.:

AQ,ANTARCTICA,Antarctica,NULL,NULL

Meaning, for empty fields, NULL or "NULL" will be inserted into the table "countries_api_countries" instead of an empty string. This won't work on PostgreSQL, because the field "iso3" was defined as "char(3)" in "countries_api.install".

A fix could be:

$iso2 = ($row[0] && strtoupper($row[0]) != 'NULL') ? $row[0] : "";
$name = ($row[1] && strtoupper($row[1]) != 'NULL') ? $row[1] : "";
$printable_name = ($row[2] && strtoupper($row[2]) != 'NULL') ? $row[2] : "";
$iso3 = ($row[3] && strtoupper($row[3]) != 'NULL') ? $row[3] : "";
$numcode = ($row[4] && strtoupper($row[4]) != 'NULL') ? $row[4] : "NULL";

Could you please fix this problem in a next release?

Thanks in advance.

CommentFileSizeAuthor
#1 countries_api_pgsql.patch2.68 KBneilnz

Comments

neilnz’s picture

Status: Active » Needs review
StatusFileSize
new2.68 KB

I came across the same problem on Postgres.

I've crafted a patch to use Schema API's drupal_write_record() to insert into the table instead of constructing the query manually. I've also changed the schema def to make iso3 properly nullable.

Patch attached.

mrfelton’s picture

Status: Needs review » Fixed

@neilnz: your patch contains a slight bug in line 259 was making a couple of the tests fail. I have corrected and committed to CVS (http://drupal.org/cvs?commit=345512)

In countries_api_csv_import_countries()

'iso3' => ($row[3] !== 'NULL') ? $row[0] : NULL,

Should have read:

'iso3' => ($row[3] !== 'NULL') ? $row[3] : NULL,

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

ahtih’s picture

Status: Closed (fixed) » Active

Reopening this.

The code with drupal_write_record() does not work during fresh install of the module. This is because drupal_write_record() only works for schemas defined in already fully installed modules. Since we have still not returned from countries_api_install() function at that point, drupal_write_record() will silently fail and no data gets inserted into the table.

claar’s picture

I ran into this or a similar bug on enabling this module with postgres. 10+ lines like this:

                                                                                         [error]
query: INSERT INTO countries_api_countries (iso2, name, printable_name, iso3,numcode)
         VALUES('HM', 'HEARD ISLAND AND MCDONALD ISLANDS',
'Heard Island and Mcdonald Islands', 'NULL', NULL) in
/www/drupal/sites/all/modules/countries_api/countries_api.module on line 265.
pg_query(): Query failed: ERROR:  value too long for type character(3) in                [warning]
/www/drupal/includes/database.pgsql.inc on line 139.
johnv’s picture

Title: Install code is flawed (and won't insert some countries in the db on PostgreSQL) » Install code is flawed (and won't insert some countries in the db on PostgreSQL/MySQL)
Status: Active » Needs review

I have MySQL running, and the install-script doesn't work for me, either, because of this commited patch.
I think the .csv file should work with empty columns, too, instead of using NULL-values.
I made the following 'patch' in countries_api_csv_import_countries().

 while (($row = fgetcsv($handle, 1024, ",")) !== FALSE) {
    //Create row variables
    $record = array(
      'iso2' => $row[0],
      'name' => $row[1],
      'printable_name' => $row[2],
      'iso3'           => ($row[3] !== 'NULL') ? $row[3] : NULL,
-      'numcode' => ($row[4]) ? $row[4] : NULL
+      'numcode'        => ($row[4] !== 'NULL') ? $row[4] : NULL,
    );
    if ($index > $offset) {
+//drupal_write_record() does not work while installing modules. Even better would be using db-insert().
-     drupal_write_record('countries_api_countries', $record);
+      db_query(
+        "INSERT INTO {countries_api_countries} 
+            (iso2, name, printable_name, iso3, numcode) 
+             VALUES('%s', '%s', '%s', '%s', '%s')",
+//             $iso2, $name, $printable_name, $iso3, $numcode
+             $record[iso2], $record[name], $record[printable_name], $record[iso3], $record[numcode]
+      );
mrfelton’s picture

Status: Needs review » Fixed

This should be resolved in git now.
Thanks.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.