66
77use Google \Cloud \BigQuery \BigQueryClient ;
88use Keboola \TableBackendUtils \Escaping \Bigquery \BigqueryQuote ;
9+ use Keboola \TableBackendUtils \ReflectionException ;
910use Keboola \TableBackendUtils \Schema \SchemaReflectionInterface ;
11+ use Keboola \TableBackendUtils \TableNotExistsReflectionException ;
1012use LogicException ;
1113
1214class 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}
0 commit comments