Skip to content

Commit

Permalink
FIX Better support of GROUP_CONCAT for postgresql
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Jun 25, 2016
1 parent f090100 commit 945fcc7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
13 changes: 6 additions & 7 deletions htdocs/core/db/pgsql.class.php
Expand Up @@ -163,10 +163,13 @@ static function convertSQLFromMysql($line,$type='auto',$unescapeslashquot=false)
}
if ($line != "")
{
// group_concat support (PgSQL >= 9.1)
$line = preg_replace('/GROUP_CONCAT/i', 'STRING_AGG', $line);
// group_concat support (PgSQL >= 9.0)
// Replace group_concat(x) or group_concat(x SEPARATOR ',') with string_agg(x, ',')
$line = preg_replace('/GROUP_CONCAT/i', 'STRING_AGG', $line);
$line = preg_replace('/ SEPARATOR/i', ',', $line);

$line = preg_replace('/STRING_AGG\(([^,\)]+)\)/i', 'STRING_AGG(\\1, \',\')', $line);
//print $line."\n";

if ($type == 'auto')
{
if (preg_match('/ALTER TABLE/i',$line)) $type='dml';
Expand Down Expand Up @@ -315,10 +318,6 @@ static function convertSQLFromMysql($line,$type='auto',$unescapeslashquot=false)
}
}

// Replace group_concat(x) with string_agg(x, ',')
$line=preg_replace('/GROUP_CONCAT\(([^\)]+)\)/i','STRING_AGG(\\1, \',\')',$line);
//print $line."\n";

// Remove () in the tables in FROM if 1 table
$line=preg_replace('/FROM\s*\((([a-z_]+)\s+as\s+([a-z_]+)\s*)\)/i','FROM \\1',$line);
//print $line."\n";
Expand Down
4 changes: 2 additions & 2 deletions htdocs/exports/class/export.class.php
Expand Up @@ -241,7 +241,7 @@ function build_sql($indice, $array_selected, $array_filterValue)
// Loop on each condition to add
foreach ($array_filterValue as $key => $value)
{
if (preg_match('/group_concat/', $key)) continue;
if (preg_match('/GROUP_CONCAT/i', $key)) continue;
if ($value != '') $sqlWhere.=" and ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
}
$sql.=$sqlWhere;
Expand All @@ -256,7 +256,7 @@ function build_sql($indice, $array_selected, $array_filterValue)
// Loop on each condition to add
foreach ($array_filterValue as $key => $value)
{
if (preg_match('/group_concat/', $key) and $value != '') $sql.=" HAVING ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
if (preg_match('/GROUP_CONCAT/i', $key) and $value != '') $sql.=" HAVING ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
}
}

Expand Down
12 changes: 9 additions & 3 deletions test/phpunit/PgsqlTest.php
Expand Up @@ -162,11 +162,17 @@ public function testConvertSQLFromMysql()
print __METHOD__." result=".$result."\n";
$this->assertEquals($result, $sql.' DEFERRABLE INITIALLY IMMEDIATE;');

// Create a constraint
$sql='SELECT a.b, GROUP_CONCAT(a.c) FROM table GROUP BY a.b';
// Test GROUP_CONCAT (without SEPARATOR)
$sql="SELECT a.b, GROUP_CONCAT(a.c) FROM table GROUP BY a.b";
$result=DoliDBPgsql::convertSQLFromMysql($sql);
print __METHOD__." result=".$result."\n";
$this->assertEquals($result, "SELECT a.b, STRING_AGG(a.c, ',') FROM table GROUP BY a.b", 'Test GROUP_CONCAT (without SEPARATOR)');

// Test GROUP_CONCAT (with SEPARATOR)
$sql="SELECT a.b, GROUP_CONCAT(a.c SEPARATOR ',') FROM table GROUP BY a.b";
$result=DoliDBPgsql::convertSQLFromMysql($sql);
print __METHOD__." result=".$result."\n";
$this->assertEquals($result, "SELECT a.b, STRING_AGG(a.c, ',') FROM table GROUP BY a.b");
$this->assertEquals($result, "SELECT a.b, STRING_AGG(a.c, ',') FROM table GROUP BY a.b", 'Test GROUP_CONCAT (with SEPARATOR)');

return $result;
}
Expand Down

0 comments on commit 945fcc7

Please sign in to comment.