Skip to content

Commit 63b07c3

Browse files
Merge pull request #85 from keboola/big-51-roman-import-from-ws
Big 51 roman import from ws
2 parents d1e2452 + 58edff8 commit 63b07c3

5 files changed

Lines changed: 127 additions & 4 deletions

File tree

src/Schema/Bigquery/BigquerySchemaReflection.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
use Google\Cloud\BigQuery\BigQueryClient;
88
use Keboola\TableBackendUtils\Escaping\Bigquery\BigqueryQuote;
9+
use Keboola\TableBackendUtils\ReflectionException;
910
use Keboola\TableBackendUtils\Schema\SchemaReflectionInterface;
11+
use Keboola\TableBackendUtils\TableNotExistsReflectionException;
1012
use LogicException;
1113

1214
class BigquerySchemaReflection implements SchemaReflectionInterface
@@ -26,9 +28,13 @@ public function __construct(BigQueryClient $bqClient, string $datasetName)
2628
*/
2729
public function getTablesNames(): array
2830
{
31+
$isDatasetExist = $this->bqClient->dataset($this->datasetName)->exists();
32+
if ($isDatasetExist === false) {
33+
throw new ReflectionException(sprintf('Dataset "%s" not found.', $this->datasetName));
34+
}
2935
$query = $this->bqClient->query(
3036
sprintf(
31-
'SELECT * FROM %s.INFORMATION_SCHEMA.TABLES;',
37+
'SELECT * FROM %s.INFORMATION_SCHEMA.TABLES WHERE `table_type` != \'VIEW\';',
3238
BigqueryQuote::quoteSingleIdentifier($this->datasetName),
3339
)
3440
);
@@ -65,6 +71,41 @@ public function getTablesNames(): array
6571
*/
6672
public function getViewsNames(): array
6773
{
68-
throw new LogicException('Not implemented');
74+
$isDatasetExist = $this->bqClient->dataset($this->datasetName)->exists();
75+
if ($isDatasetExist === false) {
76+
throw new ReflectionException(sprintf('Dataset "%s" not found', $this->datasetName));
77+
}
78+
$query = $this->bqClient->query(
79+
sprintf(
80+
'SELECT * FROM %s.INFORMATION_SCHEMA.VIEWS;',
81+
BigqueryQuote::quoteSingleIdentifier($this->datasetName),
82+
)
83+
);
84+
$queryResults = $this->bqClient->runQuery($query);
85+
86+
$tables = [];
87+
/**
88+
* @var array{
89+
* table_catalog: string,
90+
* table_schema: string,
91+
* table_name: string,
92+
* table_type: string,
93+
* is_insertable_into: string,
94+
* is_typed: string,
95+
* creation_time: string,
96+
* base_table_catalog: string,
97+
* base_table_schema: string,
98+
* base_table_name: string,
99+
* snapshot_time_ms: string,
100+
* ddl: string,
101+
* snapshot_time_ms: string,
102+
* default_collation_name: string,
103+
* upsert_stream_apply_watermark: string,
104+
* } $row
105+
*/
106+
foreach ($queryResults as $row) {
107+
$tables[] = $row['table_name'];
108+
}
109+
return $tables;
69110
}
70111
}

src/Table/Bigquery/BigqueryTableReflection.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public function __construct(BigQueryClient $bqClient, string $datasetName, strin
3535
/** @return string[] */
3636
public function getColumnsNames(): array
3737
{
38+
if ($this->exists() === false) {
39+
throw new TableNotExistsReflectionException(sprintf('Table "%s" not found.', $this->tableName));
40+
}
3841
$query = $this->bqClient->query(
3942
sprintf(
4043
'SELECT column_name FROM %s.INFORMATION_SCHEMA.COLUMNS WHERE table_name = %s',
@@ -71,6 +74,9 @@ public function getColumnsNames(): array
7174

7275
public function getColumnsDefinitions(): ColumnCollection
7376
{
77+
if ($this->exists() === false) {
78+
throw new TableNotExistsReflectionException(sprintf('Table "%s" not found.', $this->tableName));
79+
}
7480
$query = $this->bqClient->query(
7581
sprintf(
7682
'SELECT * EXCEPT(is_generated, generation_expression, is_stored, is_updatable)
@@ -110,6 +116,9 @@ public function getColumnsDefinitions(): ColumnCollection
110116

111117
public function getRowsCount(): int
112118
{
119+
if ($this->exists() === false) {
120+
throw new TableNotExistsReflectionException(sprintf('Table "%s" not found.', $this->tableName));
121+
}
113122
$query = $this->bqClient->query(sprintf(
114123
'SELECT COUNT(*) AS NumberOfRows FROM %s.%s',
115124
BigqueryQuote::quoteSingleIdentifier($this->datasetName),
@@ -177,6 +186,6 @@ public function getTableDefinition(): TableDefinitionInterface
177186

178187
public function exists(): bool
179188
{
180-
throw new LogicException('Not implemented');
189+
return $this->bqClient->dataset($this->datasetName)->table($this->tableName)->exists();
181190
}
182191
}

tests/Functional/Bigquery/Schema/BigquerySchemaReflectionTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Generator;
88
use Keboola\TableBackendUtils\Column\Bigquery\BigqueryColumn;
99
use Keboola\TableBackendUtils\Column\ColumnCollection;
10+
use Keboola\TableBackendUtils\ReflectionException;
1011
use Keboola\TableBackendUtils\Schema\Bigquery\BigquerySchemaReflection;
1112
use Keboola\TableBackendUtils\Table\Bigquery\BigqueryTableDefinition;
1213
use Keboola\TableBackendUtils\Table\Bigquery\BigqueryTableQueryBuilder;
@@ -35,6 +36,10 @@ public function testListTables(): void
3536
$dbName = $this->getDatasetName();
3637
$sql = <<<EOT
3738
CREATE TABLE $dbName.`nopitable` (`amount` STRING (32000));
39+
EOT;
40+
$this->bqClient->runQuery($this->bqClient->query($sql));
41+
$sql = <<<EOT
42+
CREATE VIEW $dbName.`nopiview` AS SELECT * FROM $dbName.`nopitable`;
3843
EOT;
3944
$this->bqClient->runQuery($this->bqClient->query($sql));
4045
$expectedTables = [self::TABLE_GENERIC, 'nopitable'];
@@ -43,6 +48,35 @@ public function testListTables(): void
4348
$this->assertCount(0, array_diff($actualTables, $expectedTables));
4449
}
4550

51+
public function testListViews(): void
52+
{
53+
$this->initTable($this->getDatasetName(), self::TABLE_GENERIC, false);
54+
$dbName = $this->getDatasetName();
55+
$sql = <<<EOT
56+
CREATE TABLE $dbName.`nopitable` (`amount` STRING (32000));
57+
EOT;
58+
$this->bqClient->runQuery($this->bqClient->query($sql));
59+
$sql = <<<EOT
60+
CREATE VIEW $dbName.`nopiview` AS SELECT * FROM $dbName.`nopitable`;
61+
EOT;
62+
$this->bqClient->runQuery($this->bqClient->query($sql));
63+
$expectedTables = ['nopiview'];
64+
$actualTables = $this->schemaRef->getViewsNames();
65+
$this->assertCount(0, array_diff($expectedTables, $actualTables));
66+
$this->assertCount(0, array_diff($actualTables, $expectedTables));
67+
}
68+
69+
70+
public function testListTablesOnNonExistingDatasetThrowException(): void
71+
{
72+
$this->initTable($this->getDatasetName(), self::TABLE_GENERIC, false);
73+
74+
$this->expectException(ReflectionException::class);
75+
$this->expectExceptionMessage('Dataset "notExisting" not found.');
76+
$schemaRef = new BigquerySchemaReflection($this->bqClient, 'notExisting');
77+
$schemaRef->getTablesNames();
78+
}
79+
4680
/**
4781
* @dataProvider createTableTestFromDefinitionSqlProvider
4882
*/

tests/Functional/Bigquery/Table/BigqueryTableQueryBuilderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Keboola\TableBackendUtils\Column\ColumnCollection;
1212
use Keboola\TableBackendUtils\Table\Bigquery\BigqueryTableQueryBuilder;
1313
use Keboola\TableBackendUtils\Table\Bigquery\BigqueryTableReflection;
14+
use Keboola\TableBackendUtils\TableNotExistsReflectionException;
1415
use Tests\Keboola\TableBackendUtils\Functional\Bigquery\BigqueryBaseCase;
1516

1617
class BigqueryTableQueryBuilderTest extends BigqueryBaseCase
@@ -102,7 +103,7 @@ public function testGetDropTableCommand(): void
102103
$this->bqClient->runQuery($this->bqClient->query($sql));
103104

104105
// test NON existence of old table via counting
105-
$this->expectException(NotFoundException::class);
106+
$this->expectException(TableNotExistsReflectionException::class);
106107
$ref->getRowsCount();
107108
}
108109
}

tests/Functional/Bigquery/Table/BigqueryTableReflectionTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Keboola\TableBackendUtils\Escaping\Bigquery\BigqueryQuote;
1111
use Keboola\TableBackendUtils\Table\Bigquery\BigqueryTableReflection;
1212
use Keboola\TableBackendUtils\Table\TableStats;
13+
use Keboola\TableBackendUtils\TableNotExistsReflectionException;
1314
use Tests\Keboola\TableBackendUtils\Functional\Bigquery\BigqueryBaseCase;
1415

1516
class BigqueryTableReflectionTest extends BigqueryBaseCase
@@ -287,4 +288,41 @@ public function testGetRowsCount(): void
287288
}
288289
self::assertEquals(2, $ref->getRowsCount());
289290
}
291+
292+
public function testIfTableExists(): void
293+
{
294+
$this->initTable();
295+
296+
$ref = new BigqueryTableReflection($this->bqClient, self::TEST_SCHEMA, self::TABLE_GENERIC);
297+
self::assertTrue($ref->exists());
298+
}
299+
300+
public function testIfTableDoesNotExists(): void
301+
{
302+
$this->initTable();
303+
304+
$ref = new BigqueryTableReflection($this->bqClient, self::TEST_SCHEMA, 'notExisting');
305+
self::assertFalse($ref->exists());
306+
307+
try {
308+
$ref->getColumnsNames();
309+
$this->fail('Should failed!');
310+
} catch (TableNotExistsReflectionException $e) {
311+
$this->assertSame('Table "notExisting" not found.', $e->getMessage());
312+
}
313+
314+
try {
315+
$ref->getColumnsDefinitions();
316+
$this->fail('Should failed!');
317+
} catch (TableNotExistsReflectionException $e) {
318+
$this->assertSame('Table "notExisting" not found.', $e->getMessage());
319+
}
320+
321+
try {
322+
$ref->getRowsCount();
323+
$this->fail('Should failed!');
324+
} catch (TableNotExistsReflectionException $e) {
325+
$this->assertSame('Table "notExisting" not found.', $e->getMessage());
326+
}
327+
}
290328
}

0 commit comments

Comments
 (0)