highlightSql.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import styled from '@emotion/styled';
  2. import {space} from 'sentry/styles/space';
  3. export const highlightSql = (
  4. description: string,
  5. queryDetail: {action: string; domain: string}
  6. ) => {
  7. let acc = '';
  8. return description.split('').map((token, i) => {
  9. acc += token;
  10. let final: string | React.ReactElement | null = null;
  11. if (acc === queryDetail.action) {
  12. final = <Operation key={i}>{queryDetail.action} </Operation>;
  13. } else if (acc === queryDetail.domain) {
  14. final = <Domain key={i}>{queryDetail.domain} </Domain>;
  15. } else if (KEYWORDS.has(acc)) {
  16. final = <Keyword key={i}>{acc}</Keyword>;
  17. } else if (['(', ')'].includes(acc)) {
  18. final = <Bracket key={i}>{acc}</Bracket>;
  19. } else if (token === ' ' || token === '\n' || description[i + 1] === ')') {
  20. final = acc;
  21. } else if (acc === '%s') {
  22. final = <Parameter>{acc}</Parameter>;
  23. } else if (i === description.length - 1) {
  24. final = acc;
  25. }
  26. if (final) {
  27. acc = '';
  28. const result = final;
  29. final = null;
  30. return result;
  31. }
  32. return null;
  33. });
  34. };
  35. const KEYWORDS = new Set([
  36. 'ADD',
  37. 'ALL',
  38. 'ALTER',
  39. 'COLUMN',
  40. 'TABLE',
  41. 'AND',
  42. 'ANY',
  43. 'AS',
  44. 'ASC',
  45. 'BACKUP',
  46. 'DATABASE',
  47. 'BETWEEN',
  48. 'CASE',
  49. 'CHECK',
  50. 'CONSTRAINT',
  51. 'CREATE',
  52. 'DATABASE',
  53. 'DEFAULT',
  54. 'DELETE',
  55. 'DESC',
  56. 'DISTINCT',
  57. 'DROP',
  58. 'EXEC',
  59. 'EXISTS',
  60. 'FOREIGN',
  61. 'KEY',
  62. 'FROM',
  63. 'FULL',
  64. 'OUTER',
  65. 'INNER',
  66. 'LEFT',
  67. 'RIGHT',
  68. 'ORDER',
  69. 'ON',
  70. 'LIMIT',
  71. 'WHERE',
  72. 'COUNT',
  73. 'JOIN',
  74. 'GROUP',
  75. 'BY',
  76. 'HAVING',
  77. 'UPDATE',
  78. ]);
  79. const Operation = styled('b')`
  80. color: ${p => p.theme.blue400};
  81. `;
  82. const Domain = styled('b')`
  83. color: ${p => p.theme.green400};
  84. margin-right: -${space(0.5)};
  85. `;
  86. const Parameter = styled('b')`
  87. color: ${p => p.theme.red400};
  88. margin-right: -${space(0.5)};
  89. `;
  90. const Keyword = styled('b')`
  91. color: ${p => p.theme.yellow400};
  92. `;
  93. const Bracket = styled('b')`
  94. color: ${p => p.theme.pink400};
  95. `;