statementPosition.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { LinkedToken } from '../../monarch/LinkedToken';
  2. import { StatementPosition } from '../../monarch/types';
  3. import { AND, ASC, BY, DESC, EQUALS, FROM, GROUP, NOT_EQUALS, ORDER, SCHEMA, SELECT, WHERE } from '../language';
  4. import { SQLTokenTypes } from './types';
  5. export function getStatementPosition(currentToken: LinkedToken | null): StatementPosition {
  6. const previousNonWhiteSpace = currentToken?.getPreviousNonWhiteSpaceToken();
  7. const previousKeyword = currentToken?.getPreviousKeyword();
  8. const previousIsSlash = currentToken?.getPreviousNonWhiteSpaceToken()?.is(SQLTokenTypes.Operator, '/');
  9. if (
  10. currentToken === null ||
  11. (currentToken.isWhiteSpace() && currentToken.previous === null) ||
  12. (currentToken.is(SQLTokenTypes.Keyword, SELECT) && currentToken.previous === null) ||
  13. previousIsSlash ||
  14. (currentToken.isIdentifier() && (previousIsSlash || currentToken?.previous === null))
  15. ) {
  16. return StatementPosition.SelectKeyword;
  17. }
  18. if (previousNonWhiteSpace?.value === SELECT) {
  19. return StatementPosition.AfterSelectKeyword;
  20. }
  21. if (
  22. (previousNonWhiteSpace?.is(SQLTokenTypes.Parenthesis, '(') || currentToken?.is(SQLTokenTypes.Parenthesis, '()')) &&
  23. previousKeyword?.value === SELECT
  24. ) {
  25. return StatementPosition.AfterSelectFuncFirstArgument;
  26. }
  27. if (previousKeyword?.value === SELECT && previousNonWhiteSpace?.isParenthesis()) {
  28. return StatementPosition.FromKeyword;
  29. }
  30. if (previousNonWhiteSpace?.value === FROM) {
  31. return StatementPosition.AfterFromKeyword;
  32. }
  33. if (
  34. (previousNonWhiteSpace?.is(SQLTokenTypes.Parenthesis, '(') || currentToken?.is(SQLTokenTypes.Parenthesis, '()')) &&
  35. previousKeyword?.value === SCHEMA
  36. ) {
  37. return StatementPosition.SchemaFuncFirstArgument;
  38. }
  39. if (previousKeyword?.value === SCHEMA && previousNonWhiteSpace?.is(SQLTokenTypes.Delimiter, ',')) {
  40. return StatementPosition.SchemaFuncExtraArgument;
  41. }
  42. if (
  43. (previousKeyword?.value === FROM && previousNonWhiteSpace?.isDoubleQuotedString()) ||
  44. (previousKeyword?.value === FROM && previousNonWhiteSpace?.isVariable()) ||
  45. (previousKeyword?.value === SCHEMA && previousNonWhiteSpace?.is(SQLTokenTypes.Parenthesis, ')'))
  46. ) {
  47. return StatementPosition.AfterFrom;
  48. }
  49. if (
  50. previousKeyword?.value === WHERE &&
  51. (previousNonWhiteSpace?.isKeyword() ||
  52. previousNonWhiteSpace?.is(SQLTokenTypes.Parenthesis, '(') ||
  53. previousNonWhiteSpace?.is(SQLTokenTypes.Operator, AND))
  54. ) {
  55. return StatementPosition.WhereKey;
  56. }
  57. if (
  58. previousKeyword?.value === WHERE &&
  59. (previousNonWhiteSpace?.isIdentifier() || previousNonWhiteSpace?.isDoubleQuotedString())
  60. ) {
  61. return StatementPosition.WhereComparisonOperator;
  62. }
  63. if (
  64. previousKeyword?.value === WHERE &&
  65. (previousNonWhiteSpace?.is(SQLTokenTypes.Operator, EQUALS) ||
  66. previousNonWhiteSpace?.is(SQLTokenTypes.Operator, NOT_EQUALS))
  67. ) {
  68. return StatementPosition.WhereValue;
  69. }
  70. if (
  71. previousKeyword?.value === WHERE &&
  72. (previousNonWhiteSpace?.isString() || previousNonWhiteSpace?.is(SQLTokenTypes.Parenthesis, ')'))
  73. ) {
  74. return StatementPosition.AfterWhereValue;
  75. }
  76. if (
  77. previousKeyword?.is(SQLTokenTypes.Keyword, BY) &&
  78. previousKeyword?.getPreviousKeyword()?.is(SQLTokenTypes.Keyword, GROUP) &&
  79. (previousNonWhiteSpace?.is(SQLTokenTypes.Keyword, BY) || previousNonWhiteSpace?.is(SQLTokenTypes.Delimiter, ','))
  80. ) {
  81. return StatementPosition.AfterGroupByKeywords;
  82. }
  83. if (
  84. previousKeyword?.is(SQLTokenTypes.Keyword, BY) &&
  85. previousKeyword?.getPreviousKeyword()?.is(SQLTokenTypes.Keyword, GROUP) &&
  86. (previousNonWhiteSpace?.isIdentifier() || previousNonWhiteSpace?.isDoubleQuotedString())
  87. ) {
  88. return StatementPosition.AfterGroupBy;
  89. }
  90. if (
  91. previousNonWhiteSpace?.is(SQLTokenTypes.Keyword, BY) &&
  92. previousNonWhiteSpace?.getPreviousKeyword()?.is(SQLTokenTypes.Keyword, ORDER)
  93. ) {
  94. return StatementPosition.AfterOrderByKeywords;
  95. }
  96. if (
  97. previousKeyword?.is(SQLTokenTypes.Keyword, BY) &&
  98. previousKeyword?.getPreviousKeyword()?.is(SQLTokenTypes.Keyword, ORDER) &&
  99. previousNonWhiteSpace?.is(SQLTokenTypes.Parenthesis) &&
  100. previousNonWhiteSpace?.getPreviousNonWhiteSpaceToken()?.is(SQLTokenTypes.Function)
  101. ) {
  102. return StatementPosition.AfterOrderByFunction;
  103. }
  104. if (previousKeyword?.is(SQLTokenTypes.Keyword, DESC) || previousKeyword?.is(SQLTokenTypes.Keyword, ASC)) {
  105. return StatementPosition.AfterOrderByDirection;
  106. }
  107. return StatementPosition.Unknown;
  108. }