query_builder.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. buildDatatypeQuery(column: string) {
  42. var query = "SELECT data_type FROM information_schema.columns WHERE ";
  43. query += " table_schema = " + this.queryModel.quoteLiteral(this.target.schema);
  44. query += " AND table_name = " + this.queryModel.quoteLiteral(this.target.table);
  45. query += " AND column_name = " + this.queryModel.quoteLiteral(column);
  46. return query;
  47. }
  48. buildAggregateQuery() {
  49. var query = "SELECT DISTINCT proname FROM pg_aggregate ";
  50. query += "INNER JOIN pg_proc ON pg_aggregate.aggfnoid = pg_proc.oid ";
  51. query += "INNER JOIN pg_type ON pg_type.oid=pg_proc.prorettype ";
  52. query += "WHERE pronargs=1 AND typname IN ('int8','float8') AND aggkind='n' ORDER BY 1";
  53. return query;
  54. }
  55. }