FIX Better support of GROUP_CONCAT for postgresql

This commit is contained in:
Laurent Destailleur 2016-06-25 14:38:10 +02:00
parent f090100790
commit 945fcc7586
3 changed files with 17 additions and 12 deletions

View File

@ -163,10 +163,13 @@ class DoliDBPgsql extends DoliDB
} }
if ($line != "") if ($line != "")
{ {
// group_concat support (PgSQL >= 9.1) // group_concat support (PgSQL >= 9.0)
$line = preg_replace('/GROUP_CONCAT/i', 'STRING_AGG', $line); // 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('/ SEPARATOR/i', ',', $line);
$line = preg_replace('/STRING_AGG\(([^,\)]+)\)/i', 'STRING_AGG(\\1, \',\')', $line);
//print $line."\n";
if ($type == 'auto') if ($type == 'auto')
{ {
if (preg_match('/ALTER TABLE/i',$line)) $type='dml'; if (preg_match('/ALTER TABLE/i',$line)) $type='dml';
@ -315,10 +318,6 @@ class DoliDBPgsql extends DoliDB
} }
} }
// 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 // 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); $line=preg_replace('/FROM\s*\((([a-z_]+)\s+as\s+([a-z_]+)\s*)\)/i','FROM \\1',$line);
//print $line."\n"; //print $line."\n";

View File

@ -241,7 +241,7 @@ class Export
// Loop on each condition to add // Loop on each condition to add
foreach ($array_filterValue as $key => $value) 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]); if ($value != '') $sqlWhere.=" and ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
} }
$sql.=$sqlWhere; $sql.=$sqlWhere;
@ -256,7 +256,7 @@ class Export
// Loop on each condition to add // Loop on each condition to add
foreach ($array_filterValue as $key => $value) 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]);
} }
} }

View File

@ -162,11 +162,17 @@ class PgsqlTest extends PHPUnit_Framework_TestCase
print __METHOD__." result=".$result."\n"; print __METHOD__." result=".$result."\n";
$this->assertEquals($result, $sql.' DEFERRABLE INITIALLY IMMEDIATE;'); $this->assertEquals($result, $sql.' DEFERRABLE INITIALLY IMMEDIATE;');
// Create a constraint // Test GROUP_CONCAT (without SEPARATOR)
$sql='SELECT a.b, GROUP_CONCAT(a.c) FROM table GROUP BY a.b'; $sql="SELECT a.b, GROUP_CONCAT(a.c) FROM table GROUP BY a.b";
$result=DoliDBPgsql::convertSQLFromMysql($sql); $result=DoliDBPgsql::convertSQLFromMysql($sql);
print __METHOD__." result=".$result."\n"; 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 (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", 'Test GROUP_CONCAT (with SEPARATOR)');
return $result; return $result;
} }