Skip to content

SQL queries with leading comments are incorrectly executed as non-query statements #178

@divingbeetle

Description

@divingbeetle

When executing SQL queries that begin with comments(both block(/* comment */) and line comments(--)),
the query execution handler misclassifies them as non-query statements (EXEC) instead of query statements (SELECT).

This results in a response of "Query OK, 0 row affected" rather than returning the actual result set.

To Reproduce
Steps to reproduce the behavior:

Execute a SELECT query with a leading comment:

/* With Comment */
SELECT * FROM users;

And Without the comment:

SELECT * FROM users;

Tried with local/remote MySQL and PostgreSQL servers.

Expected behavior

Queries with leading comments should be correctly identified as SELECT statements and return the result set, just like queries without comments.

Versions (please complete the following information):

  • OS Version: WSL2 Ubuntu 24.04
  • sqls Version: 0.2.28

Additional context

The issue seems to stem from the way the SQL command is parsed in the executeCommand function.
Which uses QueryExecType function to determine the type of SQL command.

Current Implementation checks for first word after basic whitespace trimming, which fails when comments are present at the start of the query.

Maybe a function like stripLeadingComments could be implemented to remove comments before type detection?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions