mysql_query_model.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import { find, map } from 'lodash';
  2. import { ScopedVars } from '@grafana/data';
  3. import { TemplateSrv } from '@grafana/runtime';
  4. export default class MySQLQueryModel {
  5. target: any;
  6. templateSrv: any;
  7. scopedVars: any;
  8. /** @ngInject */
  9. constructor(target: any, templateSrv?: TemplateSrv, scopedVars?: ScopedVars) {
  10. this.target = target;
  11. this.templateSrv = templateSrv;
  12. this.scopedVars = scopedVars;
  13. target.format = target.format || 'time_series';
  14. target.timeColumn = target.timeColumn || 'time';
  15. target.metricColumn = target.metricColumn || 'none';
  16. target.group = target.group || [];
  17. target.where = target.where || [{ type: 'macro', name: '$__timeFilter', params: [] }];
  18. target.select = target.select || [[{ type: 'column', params: ['value'] }]];
  19. // handle pre query gui panels gracefully
  20. if (!('rawQuery' in this.target)) {
  21. if ('rawSql' in target) {
  22. // pre query gui panel
  23. target.rawQuery = true;
  24. } else {
  25. // new panel
  26. target.rawQuery = false;
  27. }
  28. }
  29. // give interpolateQueryStr access to this
  30. this.interpolateQueryStr = this.interpolateQueryStr.bind(this);
  31. }
  32. // remove identifier quoting from identifier to use in metadata queries
  33. unquoteIdentifier(value: string) {
  34. if (value[0] === '"' && value[value.length - 1] === '"') {
  35. return value.substring(1, value.length - 1).replace(/""/g, '"');
  36. } else {
  37. return value;
  38. }
  39. }
  40. quoteIdentifier(value: string) {
  41. return '"' + value.replace(/"/g, '""') + '"';
  42. }
  43. quoteLiteral(value: string) {
  44. return "'" + value.replace(/'/g, "''") + "'";
  45. }
  46. escapeLiteral(value: any) {
  47. return String(value).replace(/'/g, "''");
  48. }
  49. hasTimeGroup() {
  50. return find(this.target.group, (g: any) => g.type === 'time');
  51. }
  52. hasMetricColumn() {
  53. return this.target.metricColumn !== 'none';
  54. }
  55. interpolateQueryStr(value: string, variable: { multi: any; includeAll: any }, defaultFormatFn: any) {
  56. // if no multi or include all do not regexEscape
  57. if (!variable.multi && !variable.includeAll) {
  58. return this.escapeLiteral(value);
  59. }
  60. if (typeof value === 'string') {
  61. return this.quoteLiteral(value);
  62. }
  63. const escapedValues = map(value, this.quoteLiteral);
  64. return escapedValues.join(',');
  65. }
  66. render(interpolate?: boolean) {
  67. const target = this.target;
  68. // new query with no table set yet
  69. if (!this.target.rawQuery && !('table' in this.target)) {
  70. return '';
  71. }
  72. if (!target.rawQuery) {
  73. target.rawSql = this.buildQuery();
  74. }
  75. if (interpolate) {
  76. return this.templateSrv.replace(target.rawSql, this.scopedVars, this.interpolateQueryStr);
  77. } else {
  78. return target.rawSql;
  79. }
  80. }
  81. hasUnixEpochTimecolumn() {
  82. return ['int', 'bigint', 'double'].indexOf(this.target.timeColumnType) > -1;
  83. }
  84. buildTimeColumn(alias = true) {
  85. const timeGroup = this.hasTimeGroup();
  86. let query;
  87. let macro = '$__timeGroup';
  88. if (timeGroup) {
  89. let args;
  90. if (timeGroup.params.length > 1 && timeGroup.params[1] !== 'none') {
  91. args = timeGroup.params.join(',');
  92. } else {
  93. args = timeGroup.params[0];
  94. }
  95. if (this.hasUnixEpochTimecolumn()) {
  96. macro = '$__unixEpochGroup';
  97. }
  98. if (alias) {
  99. macro += 'Alias';
  100. }
  101. query = macro + '(' + this.target.timeColumn + ',' + args + ')';
  102. } else {
  103. query = this.target.timeColumn;
  104. if (alias) {
  105. query += ' AS "time"';
  106. }
  107. }
  108. return query;
  109. }
  110. buildMetricColumn() {
  111. if (this.hasMetricColumn()) {
  112. return this.target.metricColumn + ' AS metric';
  113. }
  114. return '';
  115. }
  116. buildValueColumns() {
  117. let query = '';
  118. for (const column of this.target.select) {
  119. query += ',\n ' + this.buildValueColumn(column);
  120. }
  121. return query;
  122. }
  123. buildValueColumn(column: any) {
  124. let query = '';
  125. const columnName: any = find(column, (g: any) => g.type === 'column');
  126. query = columnName.params[0];
  127. const aggregate: any = find(column, (g: any) => g.type === 'aggregate');
  128. if (aggregate) {
  129. const func = aggregate.params[0];
  130. query = func + '(' + query + ')';
  131. }
  132. const alias: any = find(column, (g: any) => g.type === 'alias');
  133. if (alias) {
  134. query += ' AS ' + this.quoteIdentifier(alias.params[0]);
  135. }
  136. return query;
  137. }
  138. buildWhereClause() {
  139. let query = '';
  140. const conditions = map(this.target.where, (tag, index) => {
  141. switch (tag.type) {
  142. case 'macro':
  143. return tag.name + '(' + this.target.timeColumn + ')';
  144. break;
  145. case 'expression':
  146. return tag.params.join(' ');
  147. break;
  148. }
  149. });
  150. if (conditions.length > 0) {
  151. query = '\nWHERE\n ' + conditions.join(' AND\n ');
  152. }
  153. return query;
  154. }
  155. buildGroupClause() {
  156. let query = '';
  157. let groupSection = '';
  158. for (let i = 0; i < this.target.group.length; i++) {
  159. const part = this.target.group[i];
  160. if (i > 0) {
  161. groupSection += ', ';
  162. }
  163. if (part.type === 'time') {
  164. groupSection += '1';
  165. } else {
  166. groupSection += part.params[0];
  167. }
  168. }
  169. if (groupSection.length) {
  170. query = '\nGROUP BY ' + groupSection;
  171. if (this.hasMetricColumn()) {
  172. query += ',2';
  173. }
  174. }
  175. return query;
  176. }
  177. buildQuery() {
  178. let query = 'SELECT';
  179. query += '\n ' + this.buildTimeColumn();
  180. if (this.hasMetricColumn()) {
  181. query += ',\n ' + this.buildMetricColumn();
  182. }
  183. query += this.buildValueColumns();
  184. query += '\nFROM ' + this.target.table;
  185. query += this.buildWhereClause();
  186. query += this.buildGroupClause();
  187. query += '\nORDER BY ' + this.buildTimeColumn(false);
  188. return query;
  189. }
  190. }