kyrias / sqlx-conditional-queries

Conditional compile-time verified queries with SQLx
Apache License 2.0
30 stars 4 forks source link

expected string literal #18

Open paulzhang5511 opened 1 month ago

paulzhang5511 commented 1 month ago

image

async fn query_conditional(db: &MySqlPool) -> Result<(), sqlx::Error> {
    let limit = Some(10);
    conditional_query_as!(
        OutputType,
        r#"
        select id, name from users
        {#limit}
    "#,
    #limit = match limit {
        Some(_) => "limit {limit}",
        None => ""
    },
    )
    .fetch_all(db)
    .await?;
kyrias commented 1 month ago

And what is the actual error you get when you compile it? Pretty sure this is just rust-analyzer weirdness.

paulzhang5511 commented 1 month ago

@kyrias It can compile and run normally, with the output being correct, but Rust Analyzer issues an error.

table: users

image

code:

#[derive(Debug)]
struct OutputType {
    id: i32,
    name: String,
}

async fn query_conditional(db: &MySqlPool) -> Result<(), sqlx::Error> {
    let limit = Some(5);
    let rows: Vec<OutputType> = conditional_query_as!(
        OutputType,
        r#"
        select id, name from users
        {#limit}
    "#,
    #limit = match limit {
        Some(_) => "limit {limit}",
        None => ""
    },
    )
    .fetch_all(db)
    .await?;

    for row in rows {
        debug!("-------------------");
        debug!("{}", row.id);
        debug!("{}", row.name);
    }

    Ok(())
}

The output result is:

2024-07-26T01:00:28.686493Z DEBUG sqlx_mysql::connection::tls: not performing TLS upgrade: TLS support not compiled in
2024-07-26T01:00:28.688584Z DEBUG sqlx::query: summary="SET sql_mode=(SELECT CONCAT(@@sql_mode, ',PIPES_AS_CONCAT,NO_ENGINE_SUBSTITUTION')),time_zone='+00:00',NAMES …" db.statement="\n\nSET\n  sql_mode =(\n    SELECT\n      CONCAT(\n        @ @sql_mode,\n        ',PIPES_AS_CONCAT,NO_ENGINE_SUBSTITUTION'\n      )\n  ),\n  time_zone = '+00:00',\n  NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;\n" rows_affected=0 rows_returned=0 elapsed=290.1µs elapsed_secs=0.0002901
2024-07-26T01:00:28.690331Z DEBUG sqlx::query: summary="select id, name from …" db.statement="\n\nselect\n  id,\n  name\nfrom\n  users\nlimit\n  ?\n" rows_affected=0 rows_returned=5 elapsed=572µs elapsed_secs=0.000572
2024-07-26T01:00:28.690700Z DEBUG sqlxdemo01: -------------------
2024-07-26T01:00:28.690855Z DEBUG sqlxdemo01: 0
2024-07-26T01:00:28.691044Z DEBUG sqlxdemo01: test0
2024-07-26T01:00:28.691215Z DEBUG sqlxdemo01: -------------------
2024-07-26T01:00:28.691406Z DEBUG sqlxdemo01: 1
2024-07-26T01:00:28.691530Z DEBUG sqlxdemo01: test
2024-07-26T01:00:28.691653Z DEBUG sqlxdemo01: -------------------
2024-07-26T01:00:28.691779Z DEBUG sqlxdemo01: 2
2024-07-26T01:00:28.691912Z DEBUG sqlxdemo01: test2
2024-07-26T01:00:28.692048Z DEBUG sqlxdemo01: -------------------
2024-07-26T01:00:28.692161Z DEBUG sqlxdemo01: 3
2024-07-26T01:00:28.692293Z DEBUG sqlxdemo01: test3
2024-07-26T01:00:28.692473Z DEBUG sqlxdemo01: -------------------
2024-07-26T01:00:28.692588Z DEBUG sqlxdemo01: 4
2024-07-26T01:00:28.692701Z DEBUG sqlxdemo01: test4

ISSUE expected string literal

image

paulzhang5511 commented 1 month ago

@kyrias When you have time, could you please resolve it as soon as possible?

kyrias commented 1 month ago

It's a rust-analyzer bug where it appears to now be expanding macros differently from how the Rust compiler does, so there's nothing I can do here other than try to report it to them when I have the time.

paulzhang5511 commented 1 month ago

@kyrias Thanks.