ababo
ababo

Reputation: 1632

How to return stream with non-static Query?

I need to write a function that returns a BoxStream of records from a dynamically built SQL (e.g. using QueryBuilder). The problem is that the stream references the SQL which causes "cannot return value referencing local variable" error. AFAIK there's no owned Query in sqlx, so I don't see a way to return stream other than to implement my own wrapper. Is there more elegant way?

Upvotes: 1

Views: 160

Answers (1)

Carlos Verdes
Carlos Verdes

Reputation: 3147

The only way I could make my code to compile is wrapping also:

async_stream::stream! {
    let mut query_builder: QueryBuilder<Postgres> = QueryBuilder::new(
        "SELECT ...",
    );
    
            
    
    query_builder.push("...");
    
    let mut rows = query_builder
        .build()
        .fetch(&self.pool)
        .map_err(YourError::from)
        .map(|result| result.and_then(YourModel::try_from));

        while let Some(row) = rows.try_next().await? {
            yield Ok(row);
        }
    }.boxed()
}

Upvotes: 0

Related Questions