query_builder.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. }