2018-02-08 03:19:43 -06:00
|
|
|
export class PostgresQueryBuilder {
|
|
|
|
constructor(private target, private queryModel) {}
|
|
|
|
|
|
|
|
buildSchemaQuery() {
|
2018-07-05 14:36:39 -05:00
|
|
|
var query = 'SELECT quote_ident(schema_name) FROM information_schema.schemata WHERE';
|
2018-02-08 03:19:43 -06:00
|
|
|
query += " schema_name NOT LIKE 'pg_%' AND schema_name NOT LIKE '\\_%' AND schema_name <> 'information_schema';";
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
buildTableQuery() {
|
2018-07-05 14:36:39 -05:00
|
|
|
var query = 'SELECT quote_ident(table_name) FROM information_schema.tables WHERE ';
|
2018-07-06 05:38:43 -05:00
|
|
|
query += 'table_schema = ' + this.quoteIdentAsLiteral(this.target.schema);
|
2018-02-08 03:19:43 -06:00
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2018-07-06 05:38:43 -05:00
|
|
|
// quote identifier as literal to use in metadata queries
|
|
|
|
quoteIdentAsLiteral(value) {
|
2018-07-05 14:36:39 -05:00
|
|
|
return this.queryModel.quoteLiteral(this.queryModel.unquoteIdentifier(value));
|
|
|
|
}
|
|
|
|
|
2018-02-08 03:19:43 -06:00
|
|
|
buildColumnQuery(type?: string) {
|
2018-07-05 14:36:39 -05:00
|
|
|
var query = 'SELECT quote_ident(column_name) FROM information_schema.columns WHERE ';
|
2018-07-06 05:38:43 -05:00
|
|
|
query += 'table_schema = ' + this.quoteIdentAsLiteral(this.target.schema);
|
|
|
|
query += ' AND table_name = ' + this.quoteIdentAsLiteral(this.target.table);
|
2018-02-08 03:19:43 -06:00
|
|
|
|
|
|
|
switch (type) {
|
2018-07-05 14:36:39 -05:00
|
|
|
case 'time': {
|
|
|
|
query +=
|
|
|
|
" AND data_type IN ('timestamp without time zone','timestamp with time zone','bigint','integer','double precision','real')";
|
2018-02-08 03:19:43 -06:00
|
|
|
break;
|
|
|
|
}
|
2018-07-05 14:36:39 -05:00
|
|
|
case 'metric': {
|
2018-02-08 03:19:43 -06:00
|
|
|
query += " AND data_type IN ('text','char','varchar')";
|
|
|
|
break;
|
|
|
|
}
|
2018-07-05 14:36:39 -05:00
|
|
|
case 'value': {
|
2018-02-08 03:19:43 -06:00
|
|
|
query += " AND data_type IN ('bigint','integer','double precision','real')";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
buildValueQuery(column: string) {
|
2018-07-05 14:36:39 -05:00
|
|
|
var query = 'SELECT DISTINCT quote_literal(' + column + ')';
|
|
|
|
query += ' FROM ' + this.target.schema + '.' + this.target.table;
|
2018-07-13 02:31:39 -05:00
|
|
|
query += ' WHERE $__timeFilter(' + this.target.timeColumn + ')';
|
2018-07-05 14:36:39 -05:00
|
|
|
query += ' ORDER BY 1 LIMIT 100';
|
2018-02-08 03:19:43 -06:00
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2018-03-13 17:06:39 -05:00
|
|
|
buildDatatypeQuery(column: string) {
|
2018-07-05 14:36:39 -05:00
|
|
|
var query = 'SELECT data_type FROM information_schema.columns WHERE ';
|
2018-07-06 05:38:43 -05:00
|
|
|
query += ' table_schema = ' + this.quoteIdentAsLiteral(this.target.schema);
|
|
|
|
query += ' AND table_name = ' + this.quoteIdentAsLiteral(this.target.table);
|
|
|
|
query += ' AND column_name = ' + this.quoteIdentAsLiteral(column);
|
2018-03-13 17:06:39 -05:00
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2018-03-14 16:59:48 -05:00
|
|
|
buildAggregateQuery() {
|
2018-07-05 14:36:39 -05:00
|
|
|
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 ';
|
2018-03-14 16:59:48 -05:00
|
|
|
query += "WHERE pronargs=1 AND typname IN ('int8','float8') AND aggkind='n' ORDER BY 1";
|
|
|
|
return query;
|
|
|
|
}
|
2018-02-08 03:19:43 -06:00
|
|
|
}
|