---
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 CREATE_AUTHOR: &str = "INSERT INTO authors (name) VALUES ($1) RETURNING id";
pub async fn create_author<E: AsExecutor>(
mut db: E,
name: String,
) -> Result<i64, sqlx::Error> {
let (_row,): (i64,) = sqlx::query_as(CREATE_AUTHOR)
.bind(name)
.fetch_one(db.as_executor())
.await?;
Ok(_row)
}