From e5c67e779e165c38b9f6849224761922f54bfa40 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Sun, 15 Jul 2018 15:49:57 +0000 Subject: [PATCH] dblayeroo : Manage the GROUP_CONCAT in PgSQL without error of casting git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4289 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- Tests/dblayerooComplet.php | 53 +++++++++++++++++++++++++++++--------- dblayeroo.php | 11 ++++++++ 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/Tests/dblayerooComplet.php b/Tests/dblayerooComplet.php index b80d81a..b7d48af 100644 --- a/Tests/dblayerooComplet.php +++ b/Tests/dblayerooComplet.php @@ -550,7 +550,6 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase ->displayAdd ("group") ->joinLeft ($tbl2, array ("group"=>"group")) ->orderAdd ("group", "ASC") -->debug (1) ->execute (); $tbl1->disconnect (); $tbl2->disconnect (); @@ -768,7 +767,9 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase $tbl1->disconnect (); if ($this->engine === "pgsql") $this->assertSame (array ( - "order1" => "string_agg(".$tbl1->sep()."group".$tbl1->sep().")"), $res); + "order1" => "string_agg(".$tbl1->sep()."group".$tbl1->sep(). + "::character varying, ',' order by \"group\"". + ")"), $res); else $this->assertSame (array ( "order1" => "GROUP_CONCAT(".$tbl1->sep()."group".$tbl1->sep().")"), $res); @@ -783,7 +784,9 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase if ($this->engine === "pgsql") $this->assertSame (array ( "order1" => "string_agg(DISTINCT ". - $tbl1->sep()."group".$tbl1->sep().")"), $res); + $tbl1->sep()."group".$tbl1->sep(). + "::character varying, ',' order by \"group\"". + ")"), $res); else $this->assertSame (array ( "order1" => "GROUP_CONCAT(DISTINCT ". @@ -800,7 +803,10 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase if ($this->engine === "pgsql") $this->assertSame (array ( "order1" => "string_agg(DISTINCT ". - $tbl1->sep()."group".$tbl1->sep().") AS ". + $tbl1->sep()."group".$tbl1->sep(). + "::character varying, ',' order by ". + $tbl1->sep()."group".$tbl1->sep(). + ") AS ". $tbl1->sep()."Group Alias".$tbl1->sep()), $res); else $this->assertSame (array ( @@ -841,7 +847,9 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase if ($this->engine === "pgsql") $this->assertSame (array ( "order1" => "string_agg(".$tbl1->sep()."groupedoo".$tbl1->sep().".". - $tbl1->sep()."group".$tbl1->sep().")"), $res); + $tbl1->sep()."group".$tbl1->sep(). + "::character varying, ',' order by \"group\"". + ")"), $res); else $this->assertSame (array ( "order1" => "GROUP_CONCAT(".$tbl1->sep()."groupedoo".$tbl1->sep().".". @@ -858,7 +866,9 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase $this->assertSame (array ( "order1" => "string_agg(DISTINCT ". $tbl1->sep()."groupedoo".$tbl1->sep().".". - $tbl1->sep()."group".$tbl1->sep().")"), $res); + $tbl1->sep()."group".$tbl1->sep(). + "::character varying, ',' order by \"group\"". + ")"), $res); else $this->assertSame (array ( "order1" => "GROUP_CONCAT(DISTINCT ". @@ -877,7 +887,9 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase $this->assertSame (array ( "order1" => "string_agg(DISTINCT ". $tbl1->sep()."groupedoo".$tbl1->sep().".". - $tbl1->sep()."group".$tbl1->sep().") AS ". + $tbl1->sep()."group".$tbl1->sep(). + "::character varying, ',' order by \"group\"". + ") AS ". $tbl1->sep()."Group Alias".$tbl1->sep()), $res); else $this->assertSame (array ( @@ -931,10 +943,6 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase $tbl1 = $this->tbl1 (); $tbl1->displayAdd ("group_concat (group ),group ", "Group Alias,Group2"); - // PGSQL generate an exception : - // function string_agg(character varying) does not exist - if ($this->engine === "pgsql") - $this->setExpectedException ("PDOException"); $res = $tbl1->select () ->execute (); $tbl1->disconnect (); @@ -1010,7 +1018,6 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase $res = $tbl2->select () ->displayAdd ("MAX(uid)") ->joinInner ($tbl1, array ("group"=>"group")) -->debug (1) ->execute (); $tbl2->disconnect (); $this->assertSame (array (array ( @@ -1018,4 +1025,26 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase 'MAX(usersoo.uid)' => 6, )), $res); } + + public function test_GROUPCONCATwithAlias1 () + { + $tbl1 = $this->tbl1 (); + $res = $tbl1->select () + ->displayAdd ("GROUP_CONCAT(group)", "groups") + ->execute (); + $this->assertSame (array (array ( + 'groups' => 'group1,group2' + )), $res); + } + + public function test_GROUPCONCATwithAlias2 () + { + $tbl1 = $this->tbl1 (); + $res = $tbl1->select () + ->displayAdd ("GROUP_CONCAT(group,',')", "groups") + ->execute (); + $this->assertSame (array (array ( + 'groups' => 'group1,group2' + )), $res); + } } diff --git a/dblayeroo.php b/dblayeroo.php index 432069c..bd979cf 100644 --- a/dblayeroo.php +++ b/dblayeroo.php @@ -1621,6 +1621,17 @@ class dblayeroo if (! isset ($func)) $this->displayColumn[$getSortOrder] = $distinct.$this->sep.$fieldName.$this->sep; + elseif ($func === "string_agg") + { + // For Postgres, the entry must be : + // string_agg(distinct "group"::character varying, ',' order by "group") + if ($separator == "") + $separator = ", ','"; + $this->displayColumn[$getSortOrder] = + "$func($distinct$this->sep$fieldName$this->sep". + "::character varying$separator ". + "order by $this->sep$fieldName$this->sep)"; + } else $this->displayColumn[$getSortOrder] = "$func($distinct$this->sep$fieldName$this->sep$separator)";