sqlc-gen-sqlx 0.2.2

A sqlc plugin that generates type-safe sqlx Rust code from SQL queries.
Documentation
---
source: tests/codegen.rs
expression: code
---
// Code generated by sqlc-gen-sqlx v[VERSION]. DO NOT EDIT.
// sqlc version: [VERSION]

#![allow(
    dead_code,
    reason = "generated queries may expose items a caller does not use"
)]

pub trait AsExecutor {
    fn as_executor(&mut self) -> impl sqlx::Executor<'_, Database = sqlx::Postgres>;
}
impl AsExecutor for sqlx::PgPool {
    fn as_executor(&mut self) -> impl sqlx::Executor<'_, Database = sqlx::Postgres> {
        &*self
    }
}
impl AsExecutor for &sqlx::PgPool {
    fn as_executor(&mut self) -> impl sqlx::Executor<'_, Database = sqlx::Postgres> {
        *self
    }
}
impl AsExecutor for sqlx::PgConnection {
    fn as_executor(&mut self) -> impl sqlx::Executor<'_, Database = sqlx::Postgres> {
        &mut *self
    }
}
impl AsExecutor for sqlx::Transaction<'_, sqlx::Postgres> {
    fn as_executor(&mut self) -> impl sqlx::Executor<'_, Database = sqlx::Postgres> {
        &mut **self
    }
}
impl AsExecutor for sqlx::pool::PoolConnection<sqlx::Postgres> {
    fn as_executor(&mut self) -> impl sqlx::Executor<'_, Database = sqlx::Postgres> {
        &mut **self
    }
}
impl<T: AsExecutor + ?Sized> AsExecutor for &mut T {
    fn as_executor(&mut self) -> impl sqlx::Executor<'_, Database = sqlx::Postgres> {
        (**self).as_executor()
    }
}
pub const BATCH_LIST_AUTHORS_BY_DYNAMIC_IDS: &str = "SELECT id, name, bio FROM authors WHERE id IN ($1)";
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct BatchListAuthorsByDynamicIdsRow {
    pub id: i64,
    pub name: String,
    pub bio: Option<String>,
}
pub fn batch_list_authors_by_dynamic_ids<'a, E, I>(
    db: E,
    items: I,
) -> impl futures_core::stream::Stream<
    Item = Result<Vec<BatchListAuthorsByDynamicIdsRow>, sqlx::Error>,
> + 'a
where
    E: AsExecutor + 'a,
    I: IntoIterator<Item = Vec<i64>> + 'a,
    I::IntoIter: 'a,
{
    futures_util::stream::try_unfold(
        (db, items.into_iter()),
        |(mut db, mut items)| async move {
            let Some(item) = items.next() else {
                return Ok(None);
            };
            let ids = item;
            let mut sql = BATCH_LIST_AUTHORS_BY_DYNAMIC_IDS.to_string();
            let next_placeholder = 1usize;
            let placeholder_1 = next_placeholder;
            let slice_len = (ids).len();
            let replacement = if slice_len == 0 {
                "NULL".to_string()
            } else {
                (placeholder_1..(placeholder_1 + slice_len))
                    .map(|n| format!("${}", n))
                    .collect::<Vec<_>>()
                    .join(", ")
            };
            if sql.contains("/*SLICE:ids*/?") {
                sql = sql.replace("/*SLICE:ids*/?", &replacement);
            } else {
                if sql.contains("/*SLICE:ids*/$1") {
                    sql = sql.replace("/*SLICE:ids*/$1", &replacement);
                } else {
                    sql = sql.replace("$1", &replacement);
                }
            }
            let mut query = sqlx::query_as::<_, BatchListAuthorsByDynamicIdsRow>(&sql);
            for value in ids {
                query = query.bind(value);
            }
            let rows = query.fetch_all(db.as_executor()).await?;
            Ok(Some((rows, (db, items))))
        },
    )
}