Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLX unwraps internally when there are comments in the query #231

Closed
PonasKovas opened this issue Apr 5, 2020 · 6 comments
Closed

SQLX unwraps internally when there are comments in the query #231

PonasKovas opened this issue Apr 5, 2020 · 6 comments
Labels
bug db:sqlite Related to SQLite

Comments

@PonasKovas
Copy link

Hello, my program panics because sqlx unwraps on sqlx-core/src/sqlite/statement.rs:97:43. No error messages or anything.

I'll try to isolate the code that causes this right now, any idea what this could be?

@PonasKovas
Copy link
Author

Isolated:

use sqlx::{Connect, Executor};
use sqlx::sqlite::SqliteConnection;

#[tokio::main]
async fn main() {
    let mut conn = SqliteConnection::connect("sqlite://test.db").await.unwrap();
    
    conn.execute("-- comment").await.unwrap();
}

Apparently I can't have comments in my queries.

@PonasKovas PonasKovas changed the title SQLX unwraps internally SQLX unwraps internally when there are comments in the query Apr 5, 2020
@PonasKovas
Copy link
Author

And this is not because there is no actual query (just a comment), as the same error happens when there are comments alongside normal queries.

@mehcode
Copy link
Member

mehcode commented Apr 5, 2020

Interesting edge case. I would expect this to just work.

@mehcode mehcode added bug db:sqlite Related to SQLite labels Apr 5, 2020
@izik1
Copy link
Contributor

izik1 commented Apr 8, 2020

Hi! This seems to occur when the last statement of a query is empty, as conn.execute("") triggers this, however, conn.execute("select 1 -- comment") does not.
Looking at the sqlite docs: (https://www.sqlite.org/c3ref/prepare.html) it appears that we aren't handling the cases where the query is empty or a complete comment.

summary:

  • successfully reproduced your example
  • reproduced "comments in normal query" via:
select 1; -- comment
  • failed to reproduce "comments in normal query" via:
-- comment
select 1
select 1;
-- comment
select 1;
  • found a new case:
use sqlx::{Connect, Executor};
use sqlx::sqlite::SqliteConnection;

#[async_std::main]
async fn main() {
    let mut conn = SqliteConnection::connect("sqlite::memory:").await.unwrap();
    conn.execute("").await.unwrap();
}
  • completed diagnosis of bug

conclusion:
As a temporary workaround while we work on fixing this, you can write your queries such that:

  1. They aren't just a comment
  2. There is no ; after the final statement in a query OR there is no comment after the final statement in a query (statement -- comment is fine, statement; is fine, statement; -- comment is bugged).

@mehcode
Copy link
Member

mehcode commented Apr 8, 2020

@izik1 Thanks for the detailed investigation.


@PonasKovas This should be fixed on master. If you don't mind trying it out when you have a moment to see if it does indeed fix your case; that would help validate the fix.

@PonasKovas
Copy link
Author

@mehcode Yep, everything woks now, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug db:sqlite Related to SQLite
Projects
None yet
Development

No branches or pull requests

3 participants