Skip to content

Question - does sql in query_as has to have the lifetime of the returned QueryAs ?  #1428

Closed
@jeremychone

Description

@jeremychone

Any reason why the query_as function has the sql &str with the same lifetime as the returned QueryAs object?

pub fn query_as<'q, DB, O>(sql: &'q str) -> QueryAs<'q, ....

I might be missing something, but it makes it hard to do some query "building / execution" composition, as it seems to force query_as building and execution to be in the same function. For example, if I want to have a function that just builds and binds a query_as, not sure how I can return it to be used elsewhere.

pub fn sql_insert_with_return_id(....) -> QueryAs<'q, sqlx::Postgres, (i64,), PgArguments> {
   let mut sql = String::from(....);
   // .. continue to build string
   let mut query = sqlx::query_as::<sqlx::Postgres, (i64,)>(&sql);

  query // <-- Error here since query_as require sql to have same lifetime as the Query
}

Note: I thought I found a workaround, but I am stuck. It seems we have to execute the query in the same function we created it. Am I missing something?

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