meta_query.ts 4.1 KB

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