From ad26a319c50753053009fcb8b539cf118662e051 Mon Sep 17 00:00:00 2001 From: Sven Klemm Date: Fri, 27 Jul 2018 14:02:12 +0200 Subject: [PATCH] refactor schema query generation --- .../plugins/datasource/postgres/meta_query.ts | 55 ++++++++++++------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/public/app/plugins/datasource/postgres/meta_query.ts b/public/app/plugins/datasource/postgres/meta_query.ts index 64271c022cc..c2fc8647137 100644 --- a/public/app/plugins/datasource/postgres/meta_query.ts +++ b/public/app/plugins/datasource/postgres/meta_query.ts @@ -55,31 +55,46 @@ LIMIT 1 return query; } - buildTableQuery() { + buildSchemaConstraint() { let query = ` -SELECT quote_ident(table_name) -FROM information_schema.tables -WHERE - table_schema IN ( - SELECT CASE WHEN trim(unnest) = \'"$user"\' THEN user ELSE trim(unnest) END - FROM unnest(string_to_array(current_setting(\'search_path\'),\',\')) - ) -ORDER BY table_name`; +table_schema IN ( + SELECT CASE WHEN trim(unnest) = \'"$user"\' THEN user ELSE trim(unnest) END + FROM unnest(string_to_array(current_setting(\'search_path\'),\',\')) +)`; + return query; + } + + buildTableConstraint(table: string) { + let query = ''; + + // check for schema qualified table + if (table.includes('.')) { + let parts = table.split('.'); + query = 'table_schema = ' + this.quoteIdentAsLiteral(parts[0]); + query += ' AND table_name = ' + this.quoteIdentAsLiteral(parts[1]); + return query; + } else { + query = ` +table_schema IN ( + SELECT CASE WHEN trim(unnest) = \'"$user"\' THEN user ELSE trim(unnest) END + FROM unnest(string_to_array(current_setting(\'search_path\'),\',\')) +)`; + query += ' AND table_name = ' + this.quoteIdentAsLiteral(table); + + return query; + } + } + + buildTableQuery() { + let query = 'SELECT quote_ident(table_name) FROM information_schema.tables WHERE '; + query += this.buildSchemaConstraint(); + query += ' ORDER BY table_name'; return query; } buildColumnQuery(type?: string) { - let query = ` -SELECT quote_ident(column_name) -FROM information_schema.columns -WHERE - table_schema IN ( - SELECT CASE WHEN trim(unnest) = \'"$user"\' THEN user ELSE trim(unnest) END - FROM unnest(string_to_array(current_setting(\'search_path\'),\',\')) - LIMIT 1 - ) -`; - query += ' AND table_name = ' + this.quoteIdentAsLiteral(this.target.table); + let query = 'SELECT quote_ident(column_name) FROM information_schema.columns WHERE '; + query += this.buildTableConstraint(this.target.table); switch (type) { case 'time': {