mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
refactor schema query generation
This commit is contained in:
parent
734118de86
commit
ad26a319c5
@ -55,31 +55,46 @@ LIMIT 1
|
|||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
buildTableQuery() {
|
buildSchemaConstraint() {
|
||||||
let query = `
|
let query = `
|
||||||
SELECT quote_ident(table_name)
|
|
||||||
FROM information_schema.tables
|
|
||||||
WHERE
|
|
||||||
table_schema IN (
|
table_schema IN (
|
||||||
SELECT CASE WHEN trim(unnest) = \'"$user"\' THEN user ELSE trim(unnest) END
|
SELECT CASE WHEN trim(unnest) = \'"$user"\' THEN user ELSE trim(unnest) END
|
||||||
FROM unnest(string_to_array(current_setting(\'search_path\'),\',\'))
|
FROM unnest(string_to_array(current_setting(\'search_path\'),\',\'))
|
||||||
)
|
)`;
|
||||||
ORDER BY table_name`;
|
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;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
buildColumnQuery(type?: string) {
|
buildColumnQuery(type?: string) {
|
||||||
let query = `
|
let query = 'SELECT quote_ident(column_name) FROM information_schema.columns WHERE ';
|
||||||
SELECT quote_ident(column_name)
|
query += this.buildTableConstraint(this.target.table);
|
||||||
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);
|
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'time': {
|
case 'time': {
|
||||||
|
Loading…
Reference in New Issue
Block a user