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 2, 2016
1 parent da6764d commit 95ead5a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
28 changes: 28 additions & 0 deletions src/Database/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,32 @@ 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'] : '';
if (isset($cols[$meta['name']])) {
$cols[$meta['name']][] = $tableName;
}
else {
$cols[$meta['name']] = [$tableName];
}
}
$duplicates = '';
foreach ($cols as $name => $tables) {
if (count($tables) > 1) {
$duplicates .= ($duplicates === '' ? '' : '; ')."'$name'".(($tmp = implode(', ', array_unique($tables))) != '' ? " from $tmp" : '');
}
}
return $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
19 changes: 18 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,20 @@ 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)');
if ($driverName == 'mysql') {
$errorMsg = "'id' from book, author; 'name' from author, translator";
}
else if ($driverName == 'sqlite') {
$errorMsg = "'id' from book, author; 'name' from author";
}
else {
$errorMsg = "'id'; 'name'";
}
Assert::error(function() use($result) {
iterator_to_array($result);
}, E_USER_NOTICE, "Found duplicate columns in database result set: $errorMsg.");
});
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 95ead5a

Please sign in to comment.