dbayeroo : manage correctely the GROUP_CONCAT and the commas on string

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4290 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2018-07-15 18:29:51 +00:00
parent e5c67e779e
commit daee67d401
2 changed files with 47 additions and 7 deletions

View File

@@ -1047,4 +1047,15 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase
'groups' => 'group1,group2'
)), $res);
}
public function test_GROUPCONCATwithAlias3 ()
{
$tbl1 = $this->tbl1 ();
$res = $tbl1->select ()
->displayAdd ("GROUP_CONCAT(group,' ')", "groups")
->execute ();
$this->assertSame (array (array (
'groups' => 'group1 group2'
)), $res);
}
}

View File

@@ -1552,10 +1552,37 @@ class dblayeroo
"Invalid aliasNames provided (not string and not array)");
if (is_string ($columnNames))
{
// Remove the GROUP_CONCAT(col,',') as ',' is the default and the comma
// will be counted in error
$columnNames = str_replace (",','", "", $columnNames);
$columnNames = explode (",", $columnNames);
// A string must be separated by comma. But comma can be also in function
// parameter like GROUP_CONCAT(group,','),field1,field2
// This block will explode on comma but only if not in a parenthesis block
$tmpArr = array ();
$parenthesis = false;
$tmp = "";
for ($i = 0 ; $i < strlen ($columnNames) ; $i++)
{
$char = $columnNames{$i};
if ($parenthesis === true)
{
$tmp .= $char;
if ($char === ")")
$parenthesis = false;
}
elseif ($char === "(")
{
$tmp .= $char;
$parenthesis = true;
}
elseif ($char === ",")
{
$tmpArr[] = $tmp;
$tmp = "";
}
else
$tmp .= $char;
}
if ($tmp !== "")
$tmpArr[] = $tmp;
$columnNames = $tmpArr;
}
if (is_string ($aliasNames))
$aliasNames = explode (",", $aliasNames);
@@ -1598,13 +1625,17 @@ class dblayeroo
// There is a comma: the developper add the separator string
$separator = addslashes (substr ($name, $pos + 2, -1));
$name = substr ($name, 0, $pos);
if ($this->driver === "sqlite" || $this->driver === "psql")
if ($this->driver === "sqlite" || $this->driver === "pgsql")
$separator = ",'$separator'";
elseif ($this->driver === "mysql")
$separator = " SEPARATOR '$separator'";
}
if ($func === "GROUP_CONCAT" && $this->driver === "pgsql")
{
$func = "string_agg";
if ($separator === "")
$separator = ", ','";
}
}
}
$fieldName = $name;
@@ -1625,8 +1656,6 @@ class dblayeroo
{
// 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 ".