Skip to main content

sqlc_gen_sqlx/codegen/
mod.rs

1use crate::{
2    catalog, config::Config, emit::FileEmitter, error::Error, plugin::GenerateRequestView,
3    types::TypeMap,
4};
5
6mod batch;
7mod composites;
8mod copyfrom;
9mod enums;
10mod query;
11
12pub fn generate(request: &GenerateRequestView<'_>, config: &Config) -> Result<String, Error> {
13    let mut type_map = TypeMap::new(&config.overrides, &config.copy_cheap_types);
14    let catalog_info = catalog::walk(request, &mut type_map)?;
15    let col_overrides = crate::types::build_column_overrides(&config.overrides);
16    let mut emitter = FileEmitter::new(request.sqlc_version, env!("CARGO_PKG_VERSION"));
17
18    // Emit type definitions before query code.
19    for info in &catalog_info.enums {
20        emitter.push(enums::gen_enum(info, &config.enum_derives)?);
21    }
22    for info in &catalog_info.composites {
23        emitter.push(composites::gen_composite(info, &config.composite_derives)?);
24    }
25
26    let mut module_items: Vec<proc_macro2::TokenStream> = Vec::new();
27    let mut impl_fns: Vec<proc_macro2::TokenStream> = Vec::new();
28
29    for q in request.queries.iter() {
30        let (outer, inner) = match q.cmd {
31            ":exec" => query::gen_exec(q, &type_map, config, &col_overrides)?,
32            ":execrows" => query::gen_execrows(q, &type_map, config, &col_overrides)?,
33            ":execresult" => query::gen_execresult(q, &type_map, config, &col_overrides)?,
34            ":execlastid" => query::gen_execlastid(q, &type_map, config, &col_overrides)?,
35            ":batchexec" => batch::gen_batchexec(q, &type_map, config, &col_overrides)?,
36            ":batchone" => batch::gen_batchone(q, &type_map, config, &col_overrides)?,
37            ":batchmany" => batch::gen_batchmany(q, &type_map, config, &col_overrides)?,
38            ":copyfrom" => copyfrom::gen_copyfrom(q, &type_map, config, &col_overrides)?,
39            ":one" => query::gen_one(q, &type_map, config, &col_overrides)?,
40            ":many" => query::gen_many(q, &type_map, config, &col_overrides)?,
41            cmd => {
42                eprintln!("sqlc-gen-sqlx: skipping unsupported annotation {cmd}");
43                continue;
44            }
45        };
46        module_items.push(outer);
47        impl_fns.push(inner);
48    }
49
50    for item in module_items {
51        emitter.push(item);
52    }
53
54    emitter.push(quote::quote! {
55        pub struct Queries<E> {
56            db: E,
57        }
58
59        impl<E> Queries<E> {
60            pub fn new(db: E) -> Self {
61                Self { db }
62            }
63        }
64    });
65
66    if !impl_fns.is_empty() {
67        emitter.push(quote::quote! {
68            impl<E> Queries<E>
69            where
70                for<'c> &'c mut E: sqlx::Executor<'c, Database = sqlx::Postgres>,
71            {
72                #(#impl_fns)*
73            }
74        });
75    }
76
77    emitter.finish()
78}