@@ -40,8 +40,15 @@ const connect = async (
4040 name,
4141 cloudPlatform,
4242 queryRequestTimeout,
43+ databaseName,
4344 } ,
4445) => {
46+ if ( connection ) {
47+ logger . log ( 'info' , 'connection already exists' , 'Connection' ) ;
48+
49+ return connection ;
50+ }
51+
4552 const account = getAccount ( host ) ;
4653 const accessUrl = getAccessUrl ( account ) ;
4754 const timeout = _ . toNumber ( queryRequestTimeout ) || 2 * 60 * 1000 ;
@@ -54,7 +61,8 @@ const connect = async (
5461 `Auth type: ${ authType } \n` +
5562 `Username: ${ username } \n` +
5663 `Warehouse: ${ warehouse } \n` +
57- `Role: ${ role } ` ,
64+ `Role: ${ role } \n` +
65+ `Database name: ${ databaseName } ` ,
5866 'Connection' ,
5967 ) ;
6068
@@ -483,15 +491,20 @@ const showTablesByDatabases = async databases =>
483491 databases . map ( database => execute ( `SHOW TABLES IN DATABASE "${ removeQuotes ( database . name ) } ";` ) ) ,
484492 ) ;
485493
494+ const showSchemasByDatabase = async databaseName =>
495+ databaseName ? showSchemasInDatabase ( databaseName ) : showSchemas ( ) ;
496+
486497const showDatabases = ( ) => execute ( 'SHOW DATABASES;' ) ;
487498
488499const showSchemas = ( ) => execute ( 'SHOW SCHEMAS;' ) ;
489500
490- const showExternalTables = ( ) => execute ( 'SHOW EXTERNAL TABLES;' ) ;
501+ const showSchemasInDatabase = databaseName => execute ( `SHOW SCHEMAS IN DATABASE "${ removeQuotes ( databaseName ) } ";` ) ;
502+
503+ const showExternalTables = ( { options = '' } = { } ) => execute ( `SHOW EXTERNAL TABLES${ options } ;` ) ;
491504
492- const showViews = ( ) => execute ( ' SHOW VIEWS;' ) ;
505+ const showViews = ( { options = '' } = { } ) => execute ( ` SHOW VIEWS${ options } ;` ) ;
493506
494- const showMaterializedViews = ( ) => execute ( ' SHOW MATERIALIZED VIEWS;' ) ;
507+ const showMaterializedViews = ( { options = '' } = { } ) => execute ( ` SHOW MATERIALIZED VIEWS${ options } ;` ) ;
495508
496509const showIcebergTables = ( { options = '' } = { } ) => execute ( `SHOW ICEBERG TABLES${ options } ;` ) ;
497510
@@ -507,8 +520,8 @@ const splitEntityNames = names => {
507520
508521const isView = name => name . slice ( - 4 ) === ' (v)' ;
509522
510- const getSchemasInfo = async ( ) => {
511- const schemas = await showSchemas ( ) . catch ( err => [ { status : 'error' , message : err . message } ] ) ;
523+ const getSchemasInfo = async ( { databaseName } ) => {
524+ const schemas = await showSchemasByDatabase ( databaseName ) . catch ( err => [ { status : 'error' , message : err . message } ] ) ;
512525
513526 if ( schemas [ 0 ] ?. status === 'error' ) {
514527 return schemas ;
@@ -592,19 +605,19 @@ const logTablesMeta = async ({ logger, tables = [], icebergTables = [] }) => {
592605 logger . log ( 'info' , combinedMeta , 'Tables metadata' ) ;
593606} ;
594607
595- const getEntitiesNames = async ( { logger } ) => {
608+ const getEntitiesNames = async ( { databaseName , logger } ) => {
596609 const logError = logErrorAndReturnEmptyArray ( { logger, query : 'SHOW' } ) ;
597-
598- const databases = await showDatabases ( ) . catch ( logError ) ;
610+ const databaseQueryOptions = databaseName ? ` IN DATABASE " ${ removeQuotes ( databaseName ) } "` : '' ;
611+ const databases = databaseName ? [ { name : databaseName } ] : await showDatabases ( ) . catch ( logError ) ;
599612 const tablesRows = await showTablesByDatabases ( databases ) . catch ( logError ) ;
600613 const flatTableRows = tablesRows . flatMap ( row => row . value ) . filter ( Boolean ) ;
601- const icebergTables = await showIcebergTables ( ) . catch ( logError ) ;
614+ const icebergTables = await showIcebergTables ( { options : databaseQueryOptions } ) . catch ( logError ) ;
602615
603616 await logTablesMeta ( { logger, tables : flatTableRows , icebergTables } ) ;
604617
605- const externalTableRows = await showExternalTables ( ) . catch ( logError ) ;
606- const viewsRows = await showViews ( ) . catch ( logError ) ;
607- const materializedViewsRows = await showMaterializedViews ( ) . catch ( logError ) ;
618+ const externalTableRows = await showExternalTables ( { options : databaseQueryOptions } ) . catch ( logError ) ;
619+ const viewsRows = await showViews ( { options : databaseQueryOptions } ) . catch ( logError ) ;
620+ const materializedViewsRows = await showMaterializedViews ( { options : databaseQueryOptions } ) . catch ( logError ) ;
608621
609622 const entitiesRows = [
610623 ...flatTableRows ,
0 commit comments