query_builder.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. export class PostgresQueryBuilder {
  2. constructor(private target, private queryModel) {}
  3. buildSchemaQuery() {
  4. var query = "SELECT schema_name FROM information_schema.schemata WHERE";
  5. query += " schema_name NOT LIKE 'pg_%' AND schema_name NOT LIKE '\\_%' AND schema_name <> 'information_schema';";
  6. return query;
  7. }
  8. buildTableQuery() {
  9. var query = "SELECT table_name FROM information_schema.tables WHERE ";
  10. query += "table_schema = " + this.queryModel.quoteLiteral(this.target.schema);
  11. return query;
  12. }
  13. buildColumnQuery(type?: string) {
  14. var query = "SELECT column_name FROM information_schema.columns WHERE ";
  15. query += "table_schema = " + this.queryModel.quoteLiteral(this.target.schema);
  16. query += " AND table_name = " + this.queryModel.quoteLiteral(this.target.table);
  17. switch (type) {
  18. case "time": {
  19. query += " AND data_type IN ('timestamp without time zone','timestamp with time zone','bigint','integer','double precision','real')";
  20. break;
  21. }
  22. case "metric": {
  23. query += " AND data_type IN ('text','char','varchar')";
  24. break;
  25. }
  26. case "value": {
  27. query += " AND data_type IN ('bigint','integer','double precision','real')";
  28. break;
  29. }
  30. }
  31. return query;
  32. }
  33. buildValueQuery(column: string) {
  34. var query = "SELECT DISTINCT " + this.queryModel.quoteIdentifier(column) + "::text";
  35. query += " FROM " + this.queryModel.quoteIdentifier(this.target.schema);
  36. query += "." + this.queryModel.quoteIdentifier(this.target.table);
  37. query += " ORDER BY " + this.queryModel.quoteIdentifier(column);
  38. query += " LIMIT 100";
  39. return query;
  40. }
  41. }