Skip to content

Commit

Permalink
ResultSet: improved error message on duplicated names in select state…
Browse files Browse the repository at this point in the history
…ment
  • Loading branch information
Unlink committed Jun 10, 2016
1 parent da6764d commit 5ed5dfd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/Database/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,28 @@ public static function toPairs(array $rows, $key = NULL, $value = NULL)
return $return;
}


/**
* Finds duplicate columns in select statement
* @param \PDOStatement
* @return string
*/
public static function findDuplicates(\PDOStatement $statement)
{
$cols = [];
for ($i=0; $i<$statement->columnCount(); $i++) {
$meta = $statement->getColumnMeta($i);
$tableName = isset($meta['table']) ? $meta['table'] : '';
$cols[$meta['name']][] = $tableName;
}
$duplicates = [];
foreach ($cols as $name => $tables) {
if (count($tables) > 1) {
$tableNames = implode(', ', array_unique($tables));
$duplicates[] = "'$name'".($tableNames !== '' ? " from $tableNames" : '');
}
}
return implode('; ', $duplicates);
}

}
3 changes: 2 additions & 1 deletion src/Database/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ public function fetch()
}

if ($this->result === NULL && count($data) !== $this->pdoStatement->columnCount()) {
trigger_error('Found duplicate columns in database result set.', E_USER_NOTICE);
$duplicates = Helpers::findDuplicates($this->pdoStatement);
trigger_error("Found duplicate columns in database result set: $duplicates.", E_USER_NOTICE);
}

$this->resultKey++;
Expand Down
11 changes: 10 additions & 1 deletion tests/Database/ResultSet.fetch().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test(function () use ($context) {

Assert::error(function () use ($res) {
$res->fetch();
}, E_USER_NOTICE, 'Found duplicate columns in database result set.');
}, E_USER_NOTICE);

$res->fetch();
});
Expand All @@ -35,3 +35,12 @@ test(function () use ($context, $driverName) { // tests closeCursor()
foreach ($res as $row) {}
}
});

test(function () use ($context, $driverName) {

$result = $context->query('SELECT book.id, author.id, author.name, translator.name FROM book JOIN author ON (author.id = book.author_id) JOIN author translator ON (translator.id = book.translator_id)');
//Found duplicate columns in database result set: 'id' from book, author; 'name' from author, translator.
Assert::error(function() use($result) {
iterator_to_array($result);
}, E_USER_NOTICE);
});
4 changes: 2 additions & 2 deletions tests/Database/Table/Table.join.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test(function () use ($context) {
});


test(function () use ($connection, $structure) {
test(function () use ($connection, $structure, $driverName) {
$context = new Nette\Database\Context(
$connection,
$structure,
Expand All @@ -92,5 +92,5 @@ test(function () use ($connection, $structure) {
$books = $context->table('book')->select('book.*, author.name, translator.name');
Assert::error(function() use($books) {
iterator_to_array($books);
}, E_USER_NOTICE, 'Found duplicate columns in database result set.');
}, E_USER_NOTICE);
});

0 comments on commit 5ed5dfd

Please sign in to comment.