query_builder.ts 2.3 KB

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