From d8c7756489bd26a5e80c3cef22dd545323cb6308 Mon Sep 17 00:00:00 2001 From: Sven Klemm Date: Thu, 5 Jul 2018 21:36:39 +0200 Subject: [PATCH] dont autoquote, suggest quoted values if requried --- .../datasource/postgres/postgres_query.ts | 8 ++++ .../datasource/postgres/query_builder.ts | 48 ++++++++++--------- .../plugins/datasource/postgres/sql_part.ts | 2 +- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/public/app/plugins/datasource/postgres/postgres_query.ts b/public/app/plugins/datasource/postgres/postgres_query.ts index 7e6f9432c98..9cc8de7b491 100644 --- a/public/app/plugins/datasource/postgres/postgres_query.ts +++ b/public/app/plugins/datasource/postgres/postgres_query.ts @@ -31,6 +31,14 @@ export default class PostgresQuery { this.updateProjection(); } + unquoteIdentifier(value) { + if (value[0] === '"') { + return value.substring(1, value.length - 1).replace('""', '"'); + } else { + return value; + } + } + quoteIdentifier(value) { return '"' + value.replace('"', '""') + '"'; } diff --git a/public/app/plugins/datasource/postgres/query_builder.ts b/public/app/plugins/datasource/postgres/query_builder.ts index dd6f95b550c..843f18b0c17 100644 --- a/public/app/plugins/datasource/postgres/query_builder.ts +++ b/public/app/plugins/datasource/postgres/query_builder.ts @@ -1,35 +1,39 @@ - export class PostgresQueryBuilder { constructor(private target, private queryModel) {} buildSchemaQuery() { - var query = "SELECT schema_name FROM information_schema.schemata WHERE"; + var query = 'SELECT quote_ident(schema_name) FROM information_schema.schemata WHERE'; query += " schema_name NOT LIKE 'pg_%' AND schema_name NOT LIKE '\\_%' AND schema_name <> 'information_schema';"; return query; } buildTableQuery() { - var query = "SELECT table_name FROM information_schema.tables WHERE "; - query += "table_schema = " + this.queryModel.quoteLiteral(this.target.schema); + var query = 'SELECT quote_ident(table_name) FROM information_schema.tables WHERE '; + query += 'table_schema = ' + this.quoteLiteral(this.target.schema); return query; } + quoteLiteral(value) { + return this.queryModel.quoteLiteral(this.queryModel.unquoteIdentifier(value)); + } + buildColumnQuery(type?: string) { - var query = "SELECT column_name FROM information_schema.columns WHERE "; - query += "table_schema = " + this.queryModel.quoteLiteral(this.target.schema); - query += " AND table_name = " + this.queryModel.quoteLiteral(this.target.table); + var query = 'SELECT quote_ident(column_name) FROM information_schema.columns WHERE '; + query += 'table_schema = ' + this.quoteLiteral(this.target.schema); + query += ' AND table_name = ' + this.quoteLiteral(this.target.table); switch (type) { - case "time": { - query += " AND data_type IN ('timestamp without time zone','timestamp with time zone','bigint','integer','double precision','real')"; + case 'time': { + query += + " AND data_type IN ('timestamp without time zone','timestamp with time zone','bigint','integer','double precision','real')"; break; } - case "metric": { + case 'metric': { query += " AND data_type IN ('text','char','varchar')"; break; } - case "value": { + case 'value': { query += " AND data_type IN ('bigint','integer','double precision','real')"; break; } @@ -39,27 +43,25 @@ export class PostgresQueryBuilder { } buildValueQuery(column: string) { - var query = "SELECT DISTINCT quote_literal(" + this.queryModel.quoteIdentifier(column) + ")"; - query += " FROM " + this.queryModel.quoteIdentifier(this.target.schema); - query += "." + this.queryModel.quoteIdentifier(this.target.table); - query += " ORDER BY 1 LIMIT 100"; + var query = 'SELECT DISTINCT quote_literal(' + column + ')'; + query += ' FROM ' + this.target.schema + '.' + this.target.table; + query += ' ORDER BY 1 LIMIT 100'; return query; } buildDatatypeQuery(column: string) { - var query = "SELECT data_type FROM information_schema.columns WHERE "; - query += " table_schema = " + this.queryModel.quoteLiteral(this.target.schema); - query += " AND table_name = " + this.queryModel.quoteLiteral(this.target.table); - query += " AND column_name = " + this.queryModel.quoteLiteral(column); + var query = 'SELECT data_type FROM information_schema.columns WHERE '; + query += ' table_schema = ' + this.quoteLiteral(this.target.schema); + query += ' AND table_name = ' + this.quoteLiteral(this.target.table); + query += ' AND column_name = ' + this.quoteLiteral(column); return query; } buildAggregateQuery() { - var query = "SELECT DISTINCT proname FROM pg_aggregate "; - query += "INNER JOIN pg_proc ON pg_aggregate.aggfnoid = pg_proc.oid "; - query += "INNER JOIN pg_type ON pg_type.oid=pg_proc.prorettype "; + var query = 'SELECT DISTINCT proname FROM pg_aggregate '; + query += 'INNER JOIN pg_proc ON pg_aggregate.aggfnoid = pg_proc.oid '; + query += 'INNER JOIN pg_type ON pg_type.oid=pg_proc.prorettype '; query += "WHERE pronargs=1 AND typname IN ('int8','float8') AND aggkind='n' ORDER BY 1"; return query; } - } diff --git a/public/app/plugins/datasource/postgres/sql_part.ts b/public/app/plugins/datasource/postgres/sql_part.ts index 4cda0d47d0d..bb29fde9fb4 100644 --- a/public/app/plugins/datasource/postgres/sql_part.ts +++ b/public/app/plugins/datasource/postgres/sql_part.ts @@ -25,7 +25,7 @@ function aggregateRenderer(part, innerExpr) { } function columnRenderer(part, innerExpr) { - return '"' + part.params[0] + '"'; + return part.params[0]; } function replaceAggregationAddStrategy(selectParts, partModel) {