From 2c8906e7c4ca827e94cbf967876aa5a66ed8f852 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 7 Dec 2007 16:51:17 +0000 Subject: [PATCH] * new: DibiResult::fetchAssoc() supports "object" mode via @ descriptor * fixed mysqli_set_charset in PHP < 5.1.5 --- dibi/drivers/mysqli.php | 7 +++++-- dibi/libs/DibiResult.php | 41 +++++++++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index b86c0052..f84c5bfc 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -108,8 +108,11 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface } if (isset($config['charset'])) { - // affects the character set used by mysql_real_escape_string() (was added in MySQL 5.0.7 and PHP 5.0.5) - $ok = @mysqli_set_charset($this->connection, $config['charset']); + $ok = FALSE; + if (version_compare(PHP_VERSION , '5.1.5', '>=')) { + // affects the character set used by mysql_real_escape_string() (was added in MySQL 5.0.7 and PHP 5.0.5, fixed in PHP 5.1.5) + $ok = @mysqli_set_charset($this->connection, $config['charset']); + } if (!$ok) $ok = @mysqli_query($this->connection, "SET NAMES '$config[charset]'"); if (!$ok) $this->throwException(); } diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index 3f63f28a..3880aa6d 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -293,8 +293,8 @@ class DibiResult extends NObject implements IteratorAggregate, Countable /** * Fetches all records from table and returns associative tree - * Associative descriptor: assoc1,#,assoc2,=,assco3 - * builds a tree: $data[value1][index][value2]['assoc3'][value3] = {record} + * Associative descriptor: assoc1,#,assoc2,=,assoc3,@ + * builds a tree: $data[assoc1][index][assoc2]['assoc3']->value = {record} * * @param string associative descriptor * @return array @@ -311,21 +311,24 @@ class DibiResult extends NObject implements IteratorAggregate, Countable // check columns foreach ($assoc as $as) { - if ($as !== '#' && $as !== '=' && !array_key_exists($as, $row)) { + if ($as !== '#' && $as !== '=' && $as !== '@' && !array_key_exists($as, $row)) { throw new InvalidArgumentException("Unknown column '$as' in associative descriptor"); } } - if (count($assoc) === 1) { // speed-up - $as = $assoc[0]; - do { - $data[ $row[$as] ] = $row; - } while ($row = $this->fetch()); - return $data; - } - + // strip leading = and @ + $assoc[] = '='; // gap $last = count($assoc) - 1; - if ($assoc[$last] === '=') unset($assoc[$last]); + while ($assoc[$last] === '=' || $assoc[$last] === '@') { + $leaf = $assoc[$last]; + unset($assoc[$last]); + $last--; + + if ($last < 0) { + $assoc[] = '#'; + break; + } + } // make associative tree do { @@ -345,12 +348,24 @@ class DibiResult extends NObject implements IteratorAggregate, Countable $x = & $x[ $assoc[$i+1] ]; } + } elseif ($as === '@') { // "object" node + if ($x === NULL) { + $x = (object) $row; + $x = & $x->{$assoc[$i+1]}; + $x = NULL; // prepare child node + } else { + $x = & $x->{$assoc[$i+1]}; + } + + } else { // associative-array node $x = & $x[ $row[ $as ] ]; } } - if ($x === NULL) $x = $row; // build leaf + if ($x === NULL) { // build leaf + if ($leaf === '=') $x = $row; else $x = (object) $row; + } } while ($row = $this->fetch());