Skip to content

Commit 6fc8449

Browse files
committed
feat: includes macros as default and enhance logger
1 parent 0958151 commit 6fc8449

File tree

14 files changed

+262
-47
lines changed

14 files changed

+262
-47
lines changed

Cargo.lock

Lines changed: 107 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[workspace]
2+
members = [".", "sqlx_gen_macros"]
3+
14
[package]
25
name = "sqlx-gen"
36
version = "0.4.0"
@@ -9,7 +12,28 @@ readme = "README.md"
912
keywords = ["sqlx", "codegen", "postgres", "mysql", "sqlite"]
1013
categories = ["database", "development-tools"]
1114

15+
[[bin]]
16+
name = "sqlx-gen"
17+
required-features = ["cli"]
18+
19+
[features]
20+
default = ["cli"]
21+
cli = [
22+
"dep:sqlx",
23+
"dep:tokio",
24+
"dep:clap",
25+
"dep:heck",
26+
"dep:thiserror",
27+
"dep:quote",
28+
"dep:proc-macro2",
29+
"dep:syn",
30+
"dep:prettyplease",
31+
"dep:log",
32+
"dep:env_logger",
33+
]
34+
1235
[dependencies]
36+
sqlx-gen-macros = { path = "sqlx_gen_macros", version = "0.4.0" }
1337
sqlx = { version = "0.8", features = [
1438
"runtime-tokio",
1539
"tls-rustls-ring",
@@ -19,15 +43,17 @@ sqlx = { version = "0.8", features = [
1943
"chrono",
2044
"uuid",
2145
"json",
22-
] }
23-
tokio = { version = "1", features = ["full"] }
24-
clap = { version = "4", features = ["derive", "env"] }
25-
heck = "0.5"
26-
thiserror = "2"
27-
quote = "1"
28-
proc-macro2 = "1"
29-
syn = "2"
30-
prettyplease = "0.2"
46+
], optional = true }
47+
tokio = { version = "1", features = ["full"], optional = true }
48+
clap = { version = "4", features = ["derive", "env"], optional = true }
49+
heck = { version = "0.5", optional = true }
50+
thiserror = { version = "2", optional = true }
51+
quote = { version = "1", optional = true }
52+
proc-macro2 = { version = "1", optional = true }
53+
syn = { version = "2", optional = true }
54+
prettyplease = { version = "0.2", optional = true }
55+
log = { version = "0.4", optional = true }
56+
env_logger = { version = "0.11", optional = true }
3157

3258
[dev-dependencies]
3359
pretty_assertions = "1"

sqlx_gen_macros/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "sqlx-gen-macros"
3+
version = "0.4.0"
4+
edition = "2021"
5+
description = "No-op attribute macros for sqlx-gen generated code"
6+
license = "MIT"
7+
repository = "https://github.com/LeadcodeDev/sqlx-gen"
8+
keywords = ["sqlx", "codegen", "macros"]
9+
categories = ["database", "development-tools"]
10+
11+
[lib]
12+
proc-macro = true

sqlx_gen_macros/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use proc_macro::TokenStream;
2+
3+
/// No-op derive macro that registers `sqlx_gen` as a helper attribute.
4+
///
5+
/// This allows `#[sqlx_gen(...)]` to be used on both structs and fields
6+
/// without the compiler rejecting them as unknown attributes.
7+
///
8+
/// # Usage
9+
///
10+
/// Add `SqlxGen` to your derive list:
11+
/// ```ignore
12+
/// #[derive(sqlx::FromRow, SqlxGen)]
13+
/// #[sqlx_gen(kind = "table", table = "users")]
14+
/// pub struct Users {
15+
/// #[sqlx_gen(primary_key)]
16+
/// pub id: i32,
17+
/// }
18+
/// ```
19+
#[proc_macro_derive(SqlxGen, attributes(sqlx_gen))]
20+
pub fn derive_sqlx_gen(_input: TokenStream) -> TokenStream {
21+
TokenStream::new()
22+
}

src/codegen/composite_gen.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub fn generate_composite(
2828
);
2929

3030
imports.insert("use serde::{Serialize, Deserialize};".to_string());
31+
imports.insert("use sqlx_gen::SqlxGen;".to_string());
3132
let mut derive_tokens = vec![
3233
quote! { Debug },
3334
quote! { Clone },
@@ -36,6 +37,7 @@ pub fn generate_composite(
3637
quote! { Serialize },
3738
quote! { Deserialize },
3839
quote! { sqlx::Type },
40+
quote! { SqlxGen },
3941
];
4042
for d in extra_derives {
4143
let ident = format_ident!("{}", d);

src/codegen/crud_gen.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ pub fn generate_crud_from_parsed(
2121

2222
let table_name = &entity.table_name;
2323

24-
// Pool type
24+
// Pool type (used via full path sqlx::PgPool etc., no import needed)
2525
let pool_type = pool_type_tokens(db_kind);
26-
let pool_import = pool_import_str(db_kind);
27-
imports.insert(pool_import);
2826

2927
// Entity import
3028
imports.insert(format!("use {}::{};", entity_module_path, entity.struct_name));
@@ -495,14 +493,6 @@ fn pool_type_tokens(db_kind: DatabaseKind) -> TokenStream {
495493
}
496494
}
497495

498-
fn pool_import_str(db_kind: DatabaseKind) -> String {
499-
match db_kind {
500-
DatabaseKind::Postgres => "use sqlx::PgPool;".to_string(),
501-
DatabaseKind::Mysql => "use sqlx::MySqlPool;".to_string(),
502-
DatabaseKind::Sqlite => "use sqlx::SqlitePool;".to_string(),
503-
}
504-
}
505-
506496
fn placeholder(db_kind: DatabaseKind, index: usize) -> String {
507497
match db_kind {
508498
DatabaseKind::Postgres => format!("${}", index),
@@ -1033,10 +1023,10 @@ mod tests {
10331023
// --- imports ---
10341024

10351025
#[test]
1036-
fn test_imports_contain_pool() {
1026+
fn test_no_pool_import() {
10371027
let skip = Methods::all();
10381028
let (_, imports) = generate_crud_from_parsed(&standard_entity(), DatabaseKind::Postgres, "crate::models::users", &skip, false);
1039-
assert!(imports.iter().any(|i| i.contains("PgPool")));
1029+
assert!(!imports.iter().any(|i| i.contains("PgPool")));
10401030
}
10411031

10421032
#[test]

src/codegen/domain_gen.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ pub fn generate_domain(
4343
quote! { #fallback }
4444
});
4545

46+
let domain_doc = "sqlx_gen:kind=domain";
4647
let tokens = quote! {
4748
#[doc = #doc]
48-
#[sqlx_gen(kind = "domain")]
49+
#[doc = #domain_doc]
4950
pub type #alias_name = #type_tokens;
5051
};
5152

src/codegen/entity_parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct ParsedField {
1515
pub is_nullable: bool,
1616
/// The inner type if nullable, or the full type if not
1717
pub inner_type: String,
18-
/// Whether this field is a primary key (#[sqlx_gen(primary_key)])
18+
/// Whether this field is a primary key (`#[sqlx_gen(primary_key)]`)
1919
pub is_primary_key: bool,
2020
}
2121

src/codegen/enum_gen.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub fn generate_enum(
2222
let doc = format!("Enum: {}.{}", enum_info.schema_name, enum_info.name);
2323

2424
imports.insert("use serde::{Serialize, Deserialize};".to_string());
25+
imports.insert("use sqlx_gen::SqlxGen;".to_string());
2526
let mut derive_tokens = vec![
2627
quote! { Debug },
2728
quote! { Clone },
@@ -30,6 +31,7 @@ pub fn generate_enum(
3031
quote! { Serialize },
3132
quote! { Deserialize },
3233
quote! { sqlx::Type },
34+
quote! { SqlxGen },
3335
];
3436
for d in extra_derives {
3537
let ident = format_ident!("{}", d);

0 commit comments

Comments
 (0)