From 945fcc75868ec1ba750c3f0497e7edb13eb64710 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur <eldy@destailleur.fr> Date: Sat, 25 Jun 2016 14:38:10 +0200 Subject: [PATCH] FIX Better support of GROUP_CONCAT for postgresql --- htdocs/core/db/pgsql.class.php | 13 ++++++------- htdocs/exports/class/export.class.php | 4 ++-- test/phpunit/PgsqlTest.php | 12 +++++++++--- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/htdocs/core/db/pgsql.class.php b/htdocs/core/db/pgsql.class.php index 4e14c6987c0..5a9a446596f 100644 --- a/htdocs/core/db/pgsql.class.php +++ b/htdocs/core/db/pgsql.class.php @@ -163,10 +163,13 @@ class DoliDBPgsql extends DoliDB } 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'; @@ -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 $line=preg_replace('/FROM\s*\((([a-z_]+)\s+as\s+([a-z_]+)\s*)\)/i','FROM \\1',$line); //print $line."\n"; diff --git a/htdocs/exports/class/export.class.php b/htdocs/exports/class/export.class.php index 3bf2f87c3b1..496092b7f73 100644 --- a/htdocs/exports/class/export.class.php +++ b/htdocs/exports/class/export.class.php @@ -241,7 +241,7 @@ class Export // 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; @@ -256,7 +256,7 @@ class Export // 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]); } } diff --git a/test/phpunit/PgsqlTest.php b/test/phpunit/PgsqlTest.php index f25ea3ea72f..05efcaeb30b 100644 --- a/test/phpunit/PgsqlTest.php +++ b/test/phpunit/PgsqlTest.php @@ -162,11 +162,17 @@ class PgsqlTest extends PHPUnit_Framework_TestCase 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; } -- GitLab