meta_query.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. export class MysqlMetaQuery {
  2. constructor(private target, private queryModel) {}
  3. getOperators(datatype: string) {
  4. switch (datatype) {
  5. case 'float4':
  6. case 'float8': {
  7. return ['=', '!=', '<', '<=', '>', '>='];
  8. }
  9. case 'text':
  10. case 'varchar':
  11. case 'char': {
  12. return ['=', '!=', '<', '<=', '>', '>=', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', '~', '~*', '!~', '!~*'];
  13. }
  14. default: {
  15. return ['=', '!=', '<', '<=', '>', '>=', 'IN', 'NOT IN'];
  16. }
  17. }
  18. }
  19. // quote identifier as literal to use in metadata queries
  20. quoteIdentAsLiteral(value) {
  21. return this.queryModel.quoteLiteral(this.queryModel.unquoteIdentifier(value));
  22. }
  23. findMetricTable() {
  24. // query that returns first table found that has a timestamp(tz) column and a float column
  25. const query = `
  26. SELECT
  27. table_name as table_name,
  28. ( SELECT
  29. column_name as column_name
  30. FROM information_schema.columns c
  31. WHERE
  32. c.table_schema = t.table_schema AND
  33. c.table_name = t.table_name AND
  34. c.data_type IN ('timestamp', 'datetime')
  35. ORDER BY ordinal_position LIMIT 1
  36. ) AS time_column,
  37. ( SELECT
  38. column_name AS column_name
  39. FROM information_schema.columns c
  40. WHERE
  41. c.table_schema = t.table_schema AND
  42. c.table_name = t.table_name AND
  43. c.data_type IN('float', 'int', 'bigint')
  44. ORDER BY ordinal_position LIMIT 1
  45. ) AS value_column
  46. FROM information_schema.tables t
  47. WHERE
  48. EXISTS
  49. ( SELECT 1
  50. FROM information_schema.columns c
  51. WHERE
  52. c.table_schema = t.table_schema AND
  53. c.table_name = t.table_name AND
  54. c.data_type IN ('timestamp', 'datetime')
  55. ) AND
  56. EXISTS
  57. ( SELECT 1
  58. FROM information_schema.columns c
  59. WHERE
  60. c.table_schema = t.table_schema AND
  61. c.table_name = t.table_name AND
  62. c.data_type IN('float', 'int', 'bigint')
  63. )
  64. LIMIT 1
  65. ;`;
  66. return query;
  67. }
  68. buildTableConstraint(table: string) {
  69. let query = '';
  70. // check for schema qualified table
  71. if (table.includes('.')) {
  72. const parts = table.split('.');
  73. query = 'table_schema = ' + this.quoteIdentAsLiteral(parts[0]);
  74. query += ' AND table_name = ' + this.quoteIdentAsLiteral(parts[1]);
  75. return query;
  76. } else {
  77. query = ' table_name = ' + this.quoteIdentAsLiteral(table);
  78. return query;
  79. }
  80. }
  81. buildTableQuery() {
  82. return "SELECT table_name FROM information_schema.tables WHERE table_schema <> 'information_schema' ORDER BY table_name";
  83. }
  84. buildColumnQuery(type?: string) {
  85. let query = 'SELECT column_name FROM information_schema.columns WHERE ';
  86. query += this.buildTableConstraint(this.target.table);
  87. switch (type) {
  88. case 'time': {
  89. query += " AND data_type IN ('timestamp','datetime','bigint','int','float')";
  90. break;
  91. }
  92. case 'metric': {
  93. query += " AND data_type IN ('text' 'tinytext','mediumtext', 'longtext', 'varchar')";
  94. break;
  95. }
  96. case 'value': {
  97. query +=
  98. " AND data_type IN ('bigint','int','float','smallint', 'mediumint', 'tinyint', 'double', 'decimal', 'float')";
  99. query += ' AND column_name <> ' + this.quoteIdentAsLiteral(this.target.timeColumn);
  100. break;
  101. }
  102. case 'group': {
  103. query += " AND data_type IN ('text' 'tinytext','mediumtext', 'longtext', 'varchar')";
  104. break;
  105. }
  106. }
  107. query += ' ORDER BY column_name';
  108. return query;
  109. }
  110. buildValueQuery(column: string) {
  111. let query = 'SELECT DISTINCT QUOTE(' + column + ')';
  112. query += ' FROM ' + this.target.table;
  113. query += ' WHERE $__timeFilter(' + this.target.timeColumn + ')';
  114. query += ' ORDER BY 1 LIMIT 100';
  115. return query;
  116. }
  117. buildDatatypeQuery(column: string) {
  118. let query = `
  119. SELECT data_type
  120. FROM information_schema.columns
  121. WHERE `;
  122. query += ' table_name = ' + this.quoteIdentAsLiteral(this.target.table);
  123. query += ' AND column_name = ' + this.quoteIdentAsLiteral(column);
  124. return query;
  125. }
  126. }