Skip to content

Commit

Permalink
Fix status line display of the executed SQL command, part 2
Browse files Browse the repository at this point in the history
After recent improvements, SQL command _arguments_ came through
verbatim:

   DENY DQL, DML, DDL, AL ON SCHEMA sys TO test;
   DENY DQL, DML, DDL, AL OK, 1 row affected (0.082 sec)

This fixes it to just display the effective SQL command, without any
arguments:

   DENY OK, 1 row affected (0.082 sec)
  • Loading branch information
amotl committed Mar 19, 2024
1 parent 03c2ab9 commit 3582f12
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 4 additions & 1 deletion crate/crash/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import logging
import os
import re
import sys
from argparse import ArgumentParser, ArgumentTypeError
from collections import namedtuple
Expand Down Expand Up @@ -486,7 +487,9 @@ def _exec_and_print(self, expression: Union[str, sqlparse.sql.Statement]) -> boo
def stmt_type(expression: Union[str, sqlparse.sql.Statement]):
"""Extract type of statement, e.g. SELECT, INSERT, UPDATE, DELETE, ..."""
statement = to_statement(expression)
return str(statement.token_first(skip_ws=True, skip_cm=True)).upper()
command_with_args = str(statement.token_first(skip_ws=True, skip_cm=True))
effective_command = re.findall(r'[\w]+', command_with_args)[0]
return effective_command.upper()


def to_statement(expression: Union[str, sqlparse.sql.Statement]) -> sqlparse.sql.Statement:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ def test_stmt_type(self):
self.assertEqual(stmt_type('SELECT 1; /* foo */'), 'SELECT')
self.assertEqual(stmt_type('-- foo \n SELECT 1;'), 'SELECT')
self.assertEqual(stmt_type('SELECT 1; -- foo'), 'SELECT')
# statements with arguments as part of the command
self.assertEqual(stmt_type('/* foo */ DENY DQL, DML, DDL, AL ON SCHEMA sys TO test;'), 'DENY')

def test_decode_timeout_success(self):
self.assertEqual(_decode_timeout(None), None)
Expand Down

0 comments on commit 3582f12

Please sign in to comment.