Skip to content

MSSQL: a table named after a reserved word (e.g. User) breaks the overview, tables list and table pages #76

Description

@xavpain

What happens

Connecting to a SQL Server database that has a table called User (or Order, Group, Key, ... any T-SQL reserved word) makes the UI unusable. The connection itself succeeds, but the home page immediately fails:

INFO  sql_studio::mssql: found 190 tables in Server=...;Database=erp;...
ERROR sql_studio::handlers: error while getting database overview: Token error: 'Incorrect syntax near the keyword 'User'.' on server ... executing  on line 1 (code: 156, state: 1, class: 15)

The tables list and the individual table page fail the same way. Only the query editor still works.

Why

In mod mssql table names are interpolated into SQL without quoting:

let sql = format!("SELECT count(*) AS count FROM {}", count.name);

SELECT count(*) FROM User is a syntax error in T-SQL. The paged row fetch also does ORDER BY {first_column} unquoted, so a column named Key, Order, etc. breaks table data too.

Same problem elsewhere

  • mysql: same pattern, FROM {} and ORDER BY {first_column} unquoted. A table called order or group breaks it the same way.
  • sqlite / libsql: table name is quoted but ORDER BY {first_column} is not, so a first column named order, group, key... breaks the table data page.
  • postgres, duckdb, parquet, csv and clickhouse already quote identifiers and are fine.

Fix

Quote identifiers at every interpolation site: [name] for mssql, `name` for mysql, "name" for sqlite/libsql, the same way the ClickHouse module already does with its quote_ident helper. PR ready.

Repro

Any SQL Server database with:

CREATE TABLE [User] (ID int);

then sql-studio mssql "<connection string>" and open the home page.

Version

sql-studio 0.1.52

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions