dont autoquote, suggest quoted values if requried

This commit is contained in:
Sven Klemm 2018-07-05 21:36:39 +02:00
parent 3f614e635b
commit d8c7756489
3 changed files with 34 additions and 24 deletions

View File

@ -31,6 +31,14 @@ export default class PostgresQuery {
this.updateProjection(); this.updateProjection();
} }
unquoteIdentifier(value) {
if (value[0] === '"') {
return value.substring(1, value.length - 1).replace('""', '"');
} else {
return value;
}
}
quoteIdentifier(value) { quoteIdentifier(value) {
return '"' + value.replace('"', '""') + '"'; return '"' + value.replace('"', '""') + '"';
} }

View File

@ -1,35 +1,39 @@
export class PostgresQueryBuilder { export class PostgresQueryBuilder {
constructor(private target, private queryModel) {} constructor(private target, private queryModel) {}
buildSchemaQuery() { 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';"; query += " schema_name NOT LIKE 'pg_%' AND schema_name NOT LIKE '\\_%' AND schema_name <> 'information_schema';";
return query; return query;
} }
buildTableQuery() { buildTableQuery() {
var query = "SELECT table_name FROM information_schema.tables WHERE "; var query = 'SELECT quote_ident(table_name) FROM information_schema.tables WHERE ';
query += "table_schema = " + this.queryModel.quoteLiteral(this.target.schema); query += 'table_schema = ' + this.quoteLiteral(this.target.schema);
return query; return query;
} }
quoteLiteral(value) {
return this.queryModel.quoteLiteral(this.queryModel.unquoteIdentifier(value));
}
buildColumnQuery(type?: string) { buildColumnQuery(type?: string) {
var query = "SELECT column_name FROM information_schema.columns WHERE "; var query = 'SELECT quote_ident(column_name) FROM information_schema.columns WHERE ';
query += "table_schema = " + this.queryModel.quoteLiteral(this.target.schema); query += 'table_schema = ' + this.quoteLiteral(this.target.schema);
query += " AND table_name = " + this.queryModel.quoteLiteral(this.target.table); query += ' AND table_name = ' + this.quoteLiteral(this.target.table);
switch (type) { switch (type) {
case "time": { case 'time': {
query += " AND data_type IN ('timestamp without time zone','timestamp with time zone','bigint','integer','double precision','real')"; query +=
" AND data_type IN ('timestamp without time zone','timestamp with time zone','bigint','integer','double precision','real')";
break; break;
} }
case "metric": { case 'metric': {
query += " AND data_type IN ('text','char','varchar')"; query += " AND data_type IN ('text','char','varchar')";
break; break;
} }
case "value": { case 'value': {
query += " AND data_type IN ('bigint','integer','double precision','real')"; query += " AND data_type IN ('bigint','integer','double precision','real')";
break; break;
} }
@ -39,27 +43,25 @@ export class PostgresQueryBuilder {
} }
buildValueQuery(column: string) { buildValueQuery(column: string) {
var query = "SELECT DISTINCT quote_literal(" + this.queryModel.quoteIdentifier(column) + ")"; var query = 'SELECT DISTINCT quote_literal(' + column + ')';
query += " FROM " + this.queryModel.quoteIdentifier(this.target.schema); query += ' FROM ' + this.target.schema + '.' + this.target.table;
query += "." + this.queryModel.quoteIdentifier(this.target.table); query += ' ORDER BY 1 LIMIT 100';
query += " ORDER BY 1 LIMIT 100";
return query; return query;
} }
buildDatatypeQuery(column: string) { buildDatatypeQuery(column: string) {
var query = "SELECT data_type FROM information_schema.columns WHERE "; var query = 'SELECT data_type FROM information_schema.columns WHERE ';
query += " table_schema = " + this.queryModel.quoteLiteral(this.target.schema); query += ' table_schema = ' + this.quoteLiteral(this.target.schema);
query += " AND table_name = " + this.queryModel.quoteLiteral(this.target.table); query += ' AND table_name = ' + this.quoteLiteral(this.target.table);
query += " AND column_name = " + this.queryModel.quoteLiteral(column); query += ' AND column_name = ' + this.quoteLiteral(column);
return query; return query;
} }
buildAggregateQuery() { buildAggregateQuery() {
var query = "SELECT DISTINCT proname FROM pg_aggregate "; var query = 'SELECT DISTINCT proname FROM pg_aggregate ';
query += "INNER JOIN pg_proc ON pg_aggregate.aggfnoid = pg_proc.oid "; 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 += '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"; query += "WHERE pronargs=1 AND typname IN ('int8','float8') AND aggkind='n' ORDER BY 1";
return query; return query;
} }
} }

View File

@ -25,7 +25,7 @@ function aggregateRenderer(part, innerExpr) {
} }
function columnRenderer(part, innerExpr) { function columnRenderer(part, innerExpr) {
return '"' + part.params[0] + '"'; return part.params[0];
} }
function replaceAggregationAddStrategy(selectParts, partModel) { function replaceAggregationAddStrategy(selectParts, partModel) {