From 09459e0372859bf5e94f3547a0e069ca1ef2595a Mon Sep 17 00:00:00 2001 From: Unlink Date: Sat, 25 Jul 2015 21:55:28 +0200 Subject: [PATCH 1/2] Structure: throws proper exception when table doesn't exists - [fixes #79] --- src/Database/Structure.php | 6 +----- tests/Database/Structure.phpt | 4 +++- tests/Database/Table/Selection.get().phpt | 4 ++++ tests/Database/Table/bugs/bug49.phpt | 1 + 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Database/Structure.php b/src/Database/Structure.php index 2b4f447cb..0b77e8b3c 100644 --- a/src/Database/Structure.php +++ b/src/Database/Structure.php @@ -47,10 +47,6 @@ public function getColumns($table) $this->needStructure(); $table = $this->resolveFQTableName($table); - if (!isset($this->structure['columns'][$table])) { - throw new Nette\InvalidArgumentException("Table '$table' does not exist."); - } - return $this->structure['columns'][$table]; } @@ -245,7 +241,7 @@ protected function resolveFQTableName($table) return $this->structure['aliases'][$name]; } - return $name; + throw new Nette\InvalidArgumentException("Table '$name' does not exist."); } } diff --git a/tests/Database/Structure.phpt b/tests/Database/Structure.phpt index ab251c677..516ba48ff 100644 --- a/tests/Database/Structure.phpt +++ b/tests/Database/Structure.phpt @@ -122,7 +122,9 @@ class StructureTestCase extends TestCase { Assert::same('id', $this->structure->getPrimaryKey('books')); Assert::same(['book_id', 'tag_id'], $this->structure->getPrimaryKey('Books_x_tags')); - Assert::null($this->structure->getPrimaryKey('invalid')); + Assert::exception(function() { + $this->structure->getPrimaryKey('invalid'); + }, 'Nette\InvalidArgumentException', "Table 'invalid' does not exist."); } diff --git a/tests/Database/Table/Selection.get().phpt b/tests/Database/Table/Selection.get().phpt index a09aad97d..27bc083a6 100644 --- a/tests/Database/Table/Selection.get().phpt +++ b/tests/Database/Table/Selection.get().phpt @@ -22,4 +22,8 @@ test(function () use ($context) { 'title' => '1001 tipu a triku pro PHP', 'next_volume' => NULL, ], $book->toArray()); + + Assert::exception(function() use ($context){ + $context->table('not_existing_table')->get(1); + }, 'Nette\InvalidArgumentException', "Table 'not_existing_table' does not exist."); }); diff --git a/tests/Database/Table/bugs/bug49.phpt b/tests/Database/Table/bugs/bug49.phpt index 8a6f1d5fc..1784e1abf 100644 --- a/tests/Database/Table/bugs/bug49.phpt +++ b/tests/Database/Table/bugs/bug49.phpt @@ -11,6 +11,7 @@ require __DIR__ . '/../../connect.inc.php'; $context->query('CREATE DATABASE IF NOT EXISTS nette_test'); $context->query('USE nette_test'); +$context->query('CREATE TABLE `TABLE 30` (id int)'); Assert::same( reformat('SELECT * FROM `TABLE 30`'), From e847050912b3d27f8dd79a6ae3e8ea4fb9c85648 Mon Sep 17 00:00:00 2001 From: Unlink Date: Sat, 25 Jul 2015 21:57:16 +0200 Subject: [PATCH 2/2] Structure: added rebuild when table not exists in cache -[fixed #79] --- src/Database/Structure.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Database/Structure.php b/src/Database/Structure.php index 0b77e8b3c..52565d56d 100644 --- a/src/Database/Structure.php +++ b/src/Database/Structure.php @@ -137,7 +137,6 @@ public function rebuild() { $this->structure = $this->loadStructure(); $this->cache->save('structure', $this->structure); - $this->isRebuilt = TRUE; } @@ -192,6 +191,8 @@ public function loadStructure() } } + $this->isRebuilt = TRUE; + return $structure; } @@ -241,6 +242,11 @@ protected function resolveFQTableName($table) return $this->structure['aliases'][$name]; } + if (!$this->isRebuilt()) { + $this->rebuild(); + return $this->resolveFQTableName($table); + } + throw new Nette\InvalidArgumentException("Table '$name' does not exist."); }