Skip to content

Commit 930abd6

Browse files
committed
Merge pull request #83 from Unlink/table-not-exists-exception
Fixed missing table exception
2 parents 4f07ab3 + e847050 commit 930abd6

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

src/Database/Structure.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ public function getColumns($table)
4747
$this->needStructure();
4848
$table = $this->resolveFQTableName($table);
4949

50-
if (!isset($this->structure['columns'][$table])) {
51-
throw new Nette\InvalidArgumentException("Table '$table' does not exist.");
52-
}
53-
5450
return $this->structure['columns'][$table];
5551
}
5652

@@ -141,7 +137,6 @@ public function rebuild()
141137
{
142138
$this->structure = $this->loadStructure();
143139
$this->cache->save('structure', $this->structure);
144-
$this->isRebuilt = TRUE;
145140
}
146141

147142

@@ -196,6 +191,8 @@ public function loadStructure()
196191
}
197192
}
198193

194+
$this->isRebuilt = TRUE;
195+
199196
return $structure;
200197
}
201198

@@ -245,7 +242,12 @@ protected function resolveFQTableName($table)
245242
return $this->structure['aliases'][$name];
246243
}
247244

248-
return $name;
245+
if (!$this->isRebuilt()) {
246+
$this->rebuild();
247+
return $this->resolveFQTableName($table);
248+
}
249+
250+
throw new Nette\InvalidArgumentException("Table '$name' does not exist.");
249251
}
250252

251253
}

tests/Database/Structure.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ class StructureTestCase extends TestCase
122122
{
123123
Assert::same('id', $this->structure->getPrimaryKey('books'));
124124
Assert::same(['book_id', 'tag_id'], $this->structure->getPrimaryKey('Books_x_tags'));
125-
Assert::null($this->structure->getPrimaryKey('invalid'));
125+
Assert::exception(function() {
126+
$this->structure->getPrimaryKey('invalid');
127+
}, 'Nette\InvalidArgumentException', "Table 'invalid' does not exist.");
126128
}
127129

128130

tests/Database/Table/Selection.get().phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ test(function () use ($context) {
2222
'title' => '1001 tipu a triku pro PHP',
2323
'next_volume' => NULL,
2424
], $book->toArray());
25+
26+
Assert::exception(function() use ($context){
27+
$context->table('not_existing_table')->get(1);
28+
}, 'Nette\InvalidArgumentException', "Table 'not_existing_table' does not exist.");
2529
});

tests/Database/Table/bugs/bug49.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require __DIR__ . '/../../connect.inc.php';
1111

1212
$context->query('CREATE DATABASE IF NOT EXISTS nette_test');
1313
$context->query('USE nette_test');
14+
$context->query('CREATE TABLE `TABLE 30` (id int)');
1415

1516
Assert::same(
1617
reformat('SELECT * FROM `TABLE 30`'),

0 commit comments

Comments
 (0)