sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

[feature] Get line and column number from sql errors when they are available

Open lovasoa opened this issue 3 years ago • 3 comments

Is your feature request related to a problem? Please describe.

sqlx is difficult to use when developing large SQL queries, because when an error occurs somewhere, its position inside the sql string is not accessible.

Describe the solution you'd like

In sqlite, there is a function sqlite3_error_offset that returns the exact position in the sql query where an error occurred, that could be called to pass that information down to the library user in the rust world. In postgres, error position is also available.

The DatabaseError could be extended to have

fn position(&self) -> Option<usize>

, or in an even more user-friendly manner

struct Position {line: usize, column: usize}

fn position(&self) -> Option<Position>

Describe alternatives you've considered

Currently, I just try to guess where the error actually is, and find it by trial and error.

Additional context Add any other context or screenshots about the feature request here.

lovasoa avatar Aug 21 '22 10:08 lovasoa

This has been something I've wanted to improve for a while. I'm not entirely sure why, but parse errors from SQL are just generally awful, regardless of flavor. I imagine the parser in any database server is going to be one of the oldest (possibly by several decades!) and and most arcane parts of the codebase, so no one really touches it except to add support for new features where necessary.

Providing the position is the first part of improving that, but I also think the Display impl for the DatabaseError type should use the position to generate a context string showing where in the query the error occurred.

abonander avatar Aug 23 '22 22:08 abonander

Would you be opened to a first pr implementing just

fn position(&self) -> Option<usize>

?

lovasoa avatar Aug 24 '22 09:08 lovasoa

Sure, although it should default to returning None if you're adding it as a trait method.

abonander avatar Aug 25 '22 22:08 abonander