Skip to content

Commit 87a60dc

Browse files
committed
Structure: added columns analyze for views
1 parent 323ab1d commit 87a60dc

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

src/Database/Structure.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,6 @@ public function loadStructure()
167167
$structure['tables'] = $driver->getTables();
168168

169169
foreach ($structure['tables'] as $tablePair) {
170-
if ($tablePair['view']) {
171-
continue;
172-
}
173-
174170
if (isset($tablePair['fullName'])) {
175171
$table = $tablePair['fullName'];
176172
$structure['aliases'][strtolower($tablePair['name'])] = strtolower($table);
@@ -179,8 +175,11 @@ public function loadStructure()
179175
}
180176

181177
$structure['columns'][strtolower($table)] = $columns = $driver->getColumns($table);
182-
$structure['primary'][strtolower($table)] = $this->analyzePrimaryKey($columns);
183-
$this->analyzeForeignKeys($structure, $table);
178+
179+
if (!$tablePair['view']) {
180+
$structure['primary'][strtolower($table)] = $this->analyzePrimaryKey($columns);
181+
$this->analyzeForeignKeys($structure, $table);
182+
}
184183
}
185184

186185
if (isset($structure['hasMany'])) {

tests/Database/Structure.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ class StructureTestCase extends TestCase
7373
['name' => 'book_id', 'primary' => TRUE, 'vendor' => []],
7474
['name' => 'tag_id', 'primary' => TRUE, 'vendor' => []],
7575
]);
76+
$this->driver->shouldReceive('getColumns')->with('books_view')->once()->andReturn([
77+
['name' => 'id', 'primary' => FALSE, 'vendor' => []],
78+
['name' => 'title', 'primary' => FALSE, 'vendor' => []],
79+
]);
7680
$this->connection->shouldReceive('getSupplementalDriver')->times(4)->andReturn($this->driver);
7781
$this->driver->shouldReceive('getForeignKeys')->with('authors')->once()->andReturn([]);
7882
$this->driver->shouldReceive('getForeignKeys')->with('Books')->once()->andReturn([
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* @dataProvider? ../../databases.ini
5+
*/
6+
7+
use Tester\Assert;
8+
9+
require __DIR__ . '/../../connect.inc.php'; // create $connection
10+
11+
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../../files/{$driverName}-nette_test1.sql");
12+
13+
$context->query('CREATE VIEW books_view AS SELECT * FROM book');
14+
15+
test(function () use ($context) {
16+
$selection = $context->table('books_view')->where('id', 1);
17+
Assert::same(1, $selection->count());
18+
});
19+
20+
test(function () use ($connection) {
21+
$driver = $connection->getSupplementalDriver();
22+
$columns = $driver->getColumns('books_view');
23+
$columnsNames = array_map(function ($item) {
24+
return $item['name'];
25+
}, $columns);
26+
Assert::same(['id', 'author_id', 'translator_id', 'title', 'next_volume'], $columnsNames);
27+
});
28+
29+
$context->query('DROP VIEW books_view');

0 commit comments

Comments
 (0)