1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-04 21:28:02 +02:00

* new: DibiResult::fetchAssoc() supports "object" mode via @ descriptor

* fixed mysqli_set_charset in PHP < 5.1.5
This commit is contained in:
David Grudl
2007-12-07 16:51:17 +00:00
parent dfacb48449
commit 2c8906e7c4
2 changed files with 33 additions and 15 deletions

View File

@@ -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();
}

View File

@@ -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());