sqlc-gen-sqlx 0.1.1

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")]

const LIST_AUTHORS: &str = "SELECT id, name, bio FROM authors";
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct ListAuthorsRow {
    pub id: i64,
    pub name: String,
    pub bio: Option<String>,
}
pub trait AsExecutor {
    type Exec<'c>: sqlx::Executor<'c, Database = sqlx::Postgres> where Self: 'c;
    fn as_executor(&mut self) -> Self::Exec<'_>;
}
impl AsExecutor for sqlx::PgPool {
    type Exec<'c> = &'c sqlx::PgPool where Self: 'c;
    fn as_executor(&mut self) -> Self::Exec<'_> {
        &*self
    }
}
impl AsExecutor for &sqlx::PgPool {
    type Exec<'c> = &'c sqlx::PgPool where Self: 'c;
    fn as_executor(&mut self) -> Self::Exec<'_> {
        *self
    }
}
impl AsExecutor for sqlx::PgConnection {
    type Exec<'c> = &'c mut sqlx::PgConnection where Self: 'c;
    fn as_executor(&mut self) -> Self::Exec<'_> {
        self
    }
}
impl AsExecutor for sqlx::Transaction<'_, sqlx::Postgres> {
    type Exec<'c> = &'c mut sqlx::PgConnection where Self: 'c;
    fn as_executor(&mut self) -> Self::Exec<'_> {
        &mut **self
    }
}
impl AsExecutor for sqlx::pool::PoolConnection<sqlx::Postgres> {
    type Exec<'c> = &'c mut sqlx::PgConnection where Self: 'c;
    fn as_executor(&mut self) -> Self::Exec<'_> {
        &mut **self
    }
}
impl<T: AsExecutor + ?Sized> AsExecutor for &mut T {
    type Exec<'c> = T::Exec<'c> where Self: 'c;
    fn as_executor(&mut self) -> Self::Exec<'_> {
        (**self).as_executor()
    }
}
pub struct Queries<E> {
    db: E,
}
impl<E> Queries<E> {
    pub fn new(db: E) -> Self {
        Self { db }
    }
}
impl<E: AsExecutor> Queries<E> {
    pub async fn list_authors(&mut self) -> Result<Vec<ListAuthorsRow>, sqlx::Error> {
        sqlx::query_as::<_, ListAuthorsRow>(LIST_AUTHORS)
            .fetch_all(self.db.as_executor())
            .await
    }
}