Skip to content

Commit f14bd22

Browse files
committed
Validate if table or dataset not exist and throw exception
1 parent d1e2452 commit f14bd22

3 files changed

Lines changed: 56 additions & 3 deletions

File tree

src/Schema/Bigquery/BigquerySchemaReflection.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Google\Cloud\BigQuery\BigQueryClient;
88
use Keboola\TableBackendUtils\Escaping\Bigquery\BigqueryQuote;
99
use Keboola\TableBackendUtils\Schema\SchemaReflectionInterface;
10+
use Keboola\TableBackendUtils\TableNotExistsReflectionException;
1011
use LogicException;
1112

1213
class BigquerySchemaReflection implements SchemaReflectionInterface
@@ -26,6 +27,10 @@ public function __construct(BigQueryClient $bqClient, string $datasetName)
2627
*/
2728
public function getTablesNames(): array
2829
{
30+
$isDatasetExist = $this->bqClient->dataset($this->datasetName)->exists();
31+
if ($isDatasetExist === false) {
32+
throw new ReflectionException(sprintf('Dataset "%s" not found', $this->datasetName));
33+
}
2934
$query = $this->bqClient->query(
3035
sprintf(
3136
'SELECT * FROM %s.INFORMATION_SCHEMA.TABLES;',
@@ -65,6 +70,41 @@ public function getTablesNames(): array
6570
*/
6671
public function getViewsNames(): array
6772
{
68-
throw new LogicException('Not implemented');
73+
$isDatasetExist = $this->bqClient->dataset($this->datasetName)->exists();
74+
if ($isDatasetExist === false) {
75+
throw new TableNotExistsReflectionException(sprintf('Dataset "%s" not found', $this->datasetName));
76+
}
77+
$query = $this->bqClient->query(
78+
sprintf(
79+
'SELECT * FROM %s.INFORMATION_SCHEMA.VIEWS;',
80+
BigqueryQuote::quoteSingleIdentifier($this->datasetName),
81+
)
82+
);
83+
$queryResults = $this->bqClient->runQuery($query);
84+
85+
$tables = [];
86+
/**
87+
* @var array{
88+
* table_catalog: string,
89+
* table_schema: string,
90+
* table_name: string,
91+
* table_type: string,
92+
* is_insertable_into: string,
93+
* is_typed: string,
94+
* creation_time: string,
95+
* base_table_catalog: string,
96+
* base_table_schema: string,
97+
* base_table_name: string,
98+
* snapshot_time_ms: string,
99+
* ddl: string,
100+
* snapshot_time_ms: string,
101+
* default_collation_name: string,
102+
* upsert_stream_apply_watermark: string,
103+
* } $row
104+
*/
105+
foreach ($queryResults as $row) {
106+
$tables[] = $row['table_name'];
107+
}
108+
return $tables;
69109
}
70110
}

src/Table/Bigquery/BigqueryTableReflection.php

Lines changed: 13 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),
@@ -131,6 +140,9 @@ public function getPrimaryKeysNames(): array
131140

132141
public function getTableStats(): TableStatsInterface
133142
{
143+
if ($this->exists() === false) {
144+
throw new TableNotExistsReflectionException(sprintf('Table "%s" not found', $this->tableName));
145+
}
134146
$sql = sprintf(
135147
'SELECT size_bytes FROM %s.__TABLES__ WHERE table_id=%s',
136148
BigqueryQuote::quoteSingleIdentifier($this->datasetName),
@@ -177,6 +189,6 @@ public function getTableDefinition(): TableDefinitionInterface
177189

178190
public function exists(): bool
179191
{
180-
throw new LogicException('Not implemented');
192+
return $this->bqClient->dataset($this->datasetName)->table($this->tableName)->exists();
181193
}
182194
}

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
}

0 commit comments

Comments
 (0)