diff --git a/Cargo.lock b/Cargo.lock index 3e722ed..3ec3bdc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -129,6 +129,7 @@ dependencies = [ "eui48 1.1.0", "geo-types 0.7.13", "postgres", + "postgres-derive", "postgres-types", "postgresql_embedded", "refinery", @@ -148,6 +149,7 @@ dependencies = [ "cidr", "eui48 1.1.0", "geo-types 0.7.13", + "postgres-derive", "postgres-types", "postgresql_embedded", "refinery", @@ -171,6 +173,7 @@ dependencies = [ "deadpool-postgres", "eui48 1.1.0", "geo-types 0.7.13", + "postgres-derive", "postgres-types", "postgresql_embedded", "refinery", @@ -1622,6 +1625,18 @@ dependencies = [ "tokio-postgres", ] +[[package]] +name = "postgres-derive" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69700ea4603c5ef32d447708e6a19cd3e8ac197a000842e97f527daea5e4175f" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "postgres-protocol" version = "0.6.7" @@ -1831,7 +1846,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e9552f850d5f0964a4e4d0bf306459ac29323ddfbae05e35a7c0d35cb0803cc5" dependencies = [ "anyhow", - "itertools 0.10.5", + "itertools 0.13.0", "proc-macro2", "quote", "syn 2.0.77", @@ -2416,7 +2431,7 @@ dependencies = [ [[package]] name = "sqlc-gen" -version = "0.2.2" +version = "0.2.3" dependencies = [ "base64", "bytes", diff --git a/examples/authors-async/Cargo.toml b/examples/authors-async/Cargo.toml index 1190bd3..3199c04 100644 --- a/examples/authors-async/Cargo.toml +++ b/examples/authors-async/Cargo.toml @@ -8,6 +8,7 @@ bit-vec = { version = "0.6", features = ["serde"] } cidr = { version = "0.2", features = ["serde"] } eui48 = { version = "1.1.0", features = ["serde"] } geo-types = { version = "0.7", features = ["serde"] } +postgres-derive = "0.4.6" postgres-types = "0.2.8" postgresql_embedded = { version = "0.16.3", features = ["blocking", "bundled"] } refinery = { version = "0.8.14", features = ["tokio-postgres"] } diff --git a/examples/authors-async/postgresql/migrations/V00001__initial.sql b/examples/authors-async/postgresql/migrations/V00001__initial.sql index eb9016d..458c204 100644 --- a/examples/authors-async/postgresql/migrations/V00001__initial.sql +++ b/examples/authors-async/postgresql/migrations/V00001__initial.sql @@ -1,7 +1,16 @@ +DROP TYPE IF EXISTS type_genre; +CREATE TYPE type_genre as ENUM ( + 'history', + 'Children', + 'cLaSSic', + 'ADVENTURE' +); + CREATE TABLE authors ( id BIGSERIAL PRIMARY KEY, uuid uuid DEFAULT gen_random_uuid(), name text NOT NULL, + genre type_genre NOT NULL DEFAULT 'ADVENTURE', bio text, data json, attrs hstore, diff --git a/examples/authors-async/postgresql/query.sql b/examples/authors-async/postgresql/query.sql index 1b58f4b..18fa3c0 100644 --- a/examples/authors-async/postgresql/query.sql +++ b/examples/authors-async/postgresql/query.sql @@ -24,6 +24,7 @@ INSERT INTO authors ( name, bio, data, + genre, attrs, ip_inet, ip_cidr, @@ -36,7 +37,7 @@ INSERT INTO authors ( created_at, updated_at ) VALUES ( - $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14 + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15 ) RETURNING *; diff --git a/examples/authors-async/sqlc.yaml b/examples/authors-async/sqlc.yaml index 5e032af..7c4ffdf 100644 --- a/examples/authors-async/sqlc.yaml +++ b/examples/authors-async/sqlc.yaml @@ -6,7 +6,7 @@ plugins: - RUST_LOG wasm: url: file://./../../target/wasm32-wasip1/release/sqlc-gen.wasm - sha256: 787417eebd532829912991ba4f7052371bc9ef35e8b557440b8dc895a0df954e + sha256: b6328c9d2112dbe6c8cfa489c2f1f12439702ee35fdc67b840e087c43814e54c # - name: js # process: # cmd: sqlc-gen-json diff --git a/examples/authors-async/src/db/gen.rs b/examples/authors-async/src/db/gen.rs index 74cf199..9610d8b 100644 --- a/examples/authors-async/src/db/gen.rs +++ b/examples/authors-async/src/db/gen.rs @@ -1,13 +1,13 @@ /// @generated by the sqlc-gen-rust on sqlc-generate using sqlc.yaml /// DO NOT EDIT. const GET_AUTHOR: &str = r#" -select id, uuid, name, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at +select id, uuid, name, genre, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at from authors where id = $1 limit 1 "#; const LIST_AUTHORS: &str = r#" -select id, uuid, name, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at +select id, uuid, name, genre, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at from authors order by name "#; @@ -17,13 +17,14 @@ INSERT INTO authors ( ) VALUES ( $1, $2 ) -RETURNING id, uuid, name, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at +RETURNING id, uuid, name, genre, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at "#; const CREATE_AUTHOR_FULL: &str = r#" INSERT INTO authors ( name, bio, data, + genre, attrs, ip_inet, ip_cidr, @@ -36,20 +37,38 @@ INSERT INTO authors ( created_at, updated_at ) VALUES ( - $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14 + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15 ) -RETURNING id, uuid, name, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at +RETURNING id, uuid, name, genre, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at "#; const DELETE_AUTHOR: &str = r#" delete from authors where id = $1 "#; +#[derive(Clone, Debug, PartialEq, postgres_derive::ToSql, postgres_derive::FromSql)] +#[cfg_attr(feature = "serde_support", derive(serde::Serialize, serde::Deserialize))] +#[postgres(name = "type_genre")] +pub enum TypeGenre { + #[postgres(name = "history")] + #[cfg_attr(feature = "serde_support", serde(rename = "history"))] + History, + #[postgres(name = "Children")] + #[cfg_attr(feature = "serde_support", serde(rename = "Children"))] + Children, + #[postgres(name = "cLaSSic")] + #[cfg_attr(feature = "serde_support", serde(rename = "cLaSSic"))] + CLaSSic, + #[postgres(name = "ADVENTURE")] + #[cfg_attr(feature = "serde_support", serde(rename = "ADVENTURE"))] + Adventure, +} #[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)] #[cfg_attr(feature = "serde_support", derive(serde::Serialize, serde::Deserialize))] pub(crate) struct Author { pub id: i64, pub uuid: Option, pub name: String, + pub genre: TypeGenre, pub bio: Option, pub data: Option, pub attrs: Option>>, @@ -70,6 +89,7 @@ pub(crate) struct CreateAuthorFullParams { pub name: String, pub bio: Option, pub data: Option, + pub genre: TypeGenre, pub attrs: Option>>, pub ip_inet: cidr::IpInet, pub ip_cidr: cidr::IpCidr, @@ -114,6 +134,7 @@ impl Queries { &arg.name, &arg.bio, &arg.data, + &arg.genre, &arg.attrs, &arg.ip_inet, &arg.ip_cidr, diff --git a/examples/authors-async/src/main.rs b/examples/authors-async/src/main.rs index 1fce558..fb40e88 100644 --- a/examples/authors-async/src/main.rs +++ b/examples/authors-async/src/main.rs @@ -63,6 +63,7 @@ async fn main() -> Result<()> { "age": 50, "gender": "male", })), + genre: db::TypeGenre::CLaSSic, attrs: Some( [ ("attr_1".to_string(), Some("attr1 value".to_string())), @@ -102,6 +103,7 @@ async fn main() -> Result<()> { assert_eq!(author_full_res.bio, author_full_req.bio); assert_ne!(author_full_res.uuid, None); assert_eq!(author_full_res.data, author_full_req.data); + assert_eq!(author_full_res.genre, author_full_req.genre); assert_eq!(author_full_res.attrs, author_full_req.attrs); assert_eq!(author_full_res.ip_inet, author_full_req.ip_inet); assert_eq!(author_full_res.ip_cidr, author_full_req.ip_cidr); diff --git a/examples/authors-deadpool/Cargo.toml b/examples/authors-deadpool/Cargo.toml index bf488e7..dd9104e 100644 --- a/examples/authors-deadpool/Cargo.toml +++ b/examples/authors-deadpool/Cargo.toml @@ -21,6 +21,7 @@ time = { version = "0.3.36", features = ["local-offset", "serde"] } tokio = { version = "1.40.0", features = ["rt", "rt-multi-thread", "macros", "signal"] } tokio-postgres = { version = "0.7.12", features = ["with-uuid-1", "with-time-0_3", "array-impls"] } uuid = { version = "1.10.0", features = ["serde"] } +postgres-derive = "0.4.6" [features] default = ["serde_support"] diff --git a/examples/authors-deadpool/postgresql/migrations/V00001__initial.sql b/examples/authors-deadpool/postgresql/migrations/V00001__initial.sql index eb9016d..458c204 100644 --- a/examples/authors-deadpool/postgresql/migrations/V00001__initial.sql +++ b/examples/authors-deadpool/postgresql/migrations/V00001__initial.sql @@ -1,7 +1,16 @@ +DROP TYPE IF EXISTS type_genre; +CREATE TYPE type_genre as ENUM ( + 'history', + 'Children', + 'cLaSSic', + 'ADVENTURE' +); + CREATE TABLE authors ( id BIGSERIAL PRIMARY KEY, uuid uuid DEFAULT gen_random_uuid(), name text NOT NULL, + genre type_genre NOT NULL DEFAULT 'ADVENTURE', bio text, data json, attrs hstore, diff --git a/examples/authors-deadpool/postgresql/query.sql b/examples/authors-deadpool/postgresql/query.sql index 1b58f4b..18fa3c0 100644 --- a/examples/authors-deadpool/postgresql/query.sql +++ b/examples/authors-deadpool/postgresql/query.sql @@ -24,6 +24,7 @@ INSERT INTO authors ( name, bio, data, + genre, attrs, ip_inet, ip_cidr, @@ -36,7 +37,7 @@ INSERT INTO authors ( created_at, updated_at ) VALUES ( - $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14 + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15 ) RETURNING *; diff --git a/examples/authors-deadpool/sqlc.yaml b/examples/authors-deadpool/sqlc.yaml index 0304cdf..8323d87 100644 --- a/examples/authors-deadpool/sqlc.yaml +++ b/examples/authors-deadpool/sqlc.yaml @@ -6,7 +6,7 @@ plugins: - RUST_LOG wasm: url: file://./../../target/wasm32-wasip1/release/sqlc-gen.wasm - sha256: 787417eebd532829912991ba4f7052371bc9ef35e8b557440b8dc895a0df954e + sha256: b6328c9d2112dbe6c8cfa489c2f1f12439702ee35fdc67b840e087c43814e54c # - name: js # process: # cmd: sqlc-gen-json diff --git a/examples/authors-deadpool/src/db/gen.rs b/examples/authors-deadpool/src/db/gen.rs index 8ee5e7f..3545011 100644 --- a/examples/authors-deadpool/src/db/gen.rs +++ b/examples/authors-deadpool/src/db/gen.rs @@ -1,13 +1,13 @@ /// @generated by the sqlc-gen-rust on sqlc-generate using sqlc.yaml /// DO NOT EDIT. const GET_AUTHOR: &str = r#" -select id, uuid, name, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at +select id, uuid, name, genre, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at from authors where id = $1 limit 1 "#; const LIST_AUTHORS: &str = r#" -select id, uuid, name, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at +select id, uuid, name, genre, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at from authors order by name "#; @@ -17,13 +17,14 @@ INSERT INTO authors ( ) VALUES ( $1, $2 ) -RETURNING id, uuid, name, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at +RETURNING id, uuid, name, genre, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at "#; const CREATE_AUTHOR_FULL: &str = r#" INSERT INTO authors ( name, bio, data, + genre, attrs, ip_inet, ip_cidr, @@ -36,20 +37,38 @@ INSERT INTO authors ( created_at, updated_at ) VALUES ( - $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14 + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15 ) -RETURNING id, uuid, name, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at +RETURNING id, uuid, name, genre, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at "#; const DELETE_AUTHOR: &str = r#" delete from authors where id = $1 "#; +#[derive(Clone, Debug, PartialEq, postgres_derive::ToSql, postgres_derive::FromSql)] +#[cfg_attr(feature = "serde_support", derive(serde::Serialize, serde::Deserialize))] +#[postgres(name = "type_genre")] +pub enum TypeGenre { + #[postgres(name = "history")] + #[cfg_attr(feature = "serde_support", serde(rename = "history"))] + History, + #[postgres(name = "Children")] + #[cfg_attr(feature = "serde_support", serde(rename = "Children"))] + Children, + #[postgres(name = "cLaSSic")] + #[cfg_attr(feature = "serde_support", serde(rename = "cLaSSic"))] + CLaSSic, + #[postgres(name = "ADVENTURE")] + #[cfg_attr(feature = "serde_support", serde(rename = "ADVENTURE"))] + Adventure, +} #[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)] #[cfg_attr(feature = "serde_support", derive(serde::Serialize, serde::Deserialize))] pub(crate) struct Author { pub id: i64, pub uuid: Option, pub name: String, + pub genre: TypeGenre, pub bio: Option, pub data: Option, pub attrs: Option>>, @@ -70,6 +89,7 @@ pub(crate) struct CreateAuthorFullParams { pub name: String, pub bio: Option, pub data: Option, + pub genre: TypeGenre, pub attrs: Option>>, pub ip_inet: cidr::IpInet, pub ip_cidr: cidr::IpCidr, @@ -123,6 +143,7 @@ impl Queries { &arg.name, &arg.bio, &arg.data, + &arg.genre, &arg.attrs, &arg.ip_inet, &arg.ip_cidr, diff --git a/examples/authors-deadpool/src/main.rs b/examples/authors-deadpool/src/main.rs index 59526a5..c62f4dd 100644 --- a/examples/authors-deadpool/src/main.rs +++ b/examples/authors-deadpool/src/main.rs @@ -64,6 +64,7 @@ async fn main() -> Result<()> { "age": 50, "gender": "male", })), + genre: db::TypeGenre::CLaSSic, attrs: Some( [ ("attr_1".to_string(), Some("attr1 value".to_string())), @@ -103,6 +104,7 @@ async fn main() -> Result<()> { assert_eq!(author_full_res.bio, author_full_req.bio); assert_ne!(author_full_res.uuid, None); assert_eq!(author_full_res.data, author_full_req.data); + assert_eq!(author_full_res.genre, author_full_req.genre); assert_eq!(author_full_res.attrs, author_full_req.attrs); assert_eq!(author_full_res.ip_inet, author_full_req.ip_inet); assert_eq!(author_full_res.ip_cidr, author_full_req.ip_cidr); diff --git a/examples/authors/Cargo.toml b/examples/authors/Cargo.toml index 280fc8a..29d1cc8 100644 --- a/examples/authors/Cargo.toml +++ b/examples/authors/Cargo.toml @@ -9,6 +9,7 @@ cidr = { version = "0.2", features = ["serde"] } eui48 = { version = "1.1.0", features = ["serde"] } geo-types = { version = "0.7", features = ["serde"] } postgres = "0.19.9" +postgres-derive = "0.4.6" postgres-types = "0.2.8" postgresql_embedded = { version = "0.16.3", features = ["blocking", "bundled"] } refinery = { version = "0.8.14", features = ["postgres"] } diff --git a/examples/authors/postgresql/migrations/V00001__initial.sql b/examples/authors/postgresql/migrations/V00001__initial.sql index eb9016d..458c204 100644 --- a/examples/authors/postgresql/migrations/V00001__initial.sql +++ b/examples/authors/postgresql/migrations/V00001__initial.sql @@ -1,7 +1,16 @@ +DROP TYPE IF EXISTS type_genre; +CREATE TYPE type_genre as ENUM ( + 'history', + 'Children', + 'cLaSSic', + 'ADVENTURE' +); + CREATE TABLE authors ( id BIGSERIAL PRIMARY KEY, uuid uuid DEFAULT gen_random_uuid(), name text NOT NULL, + genre type_genre NOT NULL DEFAULT 'ADVENTURE', bio text, data json, attrs hstore, diff --git a/examples/authors/postgresql/query.sql b/examples/authors/postgresql/query.sql index 1b58f4b..18fa3c0 100644 --- a/examples/authors/postgresql/query.sql +++ b/examples/authors/postgresql/query.sql @@ -24,6 +24,7 @@ INSERT INTO authors ( name, bio, data, + genre, attrs, ip_inet, ip_cidr, @@ -36,7 +37,7 @@ INSERT INTO authors ( created_at, updated_at ) VALUES ( - $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14 + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15 ) RETURNING *; diff --git a/examples/authors/sqlc.yaml b/examples/authors/sqlc.yaml index 175d549..a6706c2 100644 --- a/examples/authors/sqlc.yaml +++ b/examples/authors/sqlc.yaml @@ -6,7 +6,7 @@ plugins: - RUST_LOG wasm: url: file://./../../target/wasm32-wasip1/release/sqlc-gen.wasm - sha256: 787417eebd532829912991ba4f7052371bc9ef35e8b557440b8dc895a0df954e + sha256: b6328c9d2112dbe6c8cfa489c2f1f12439702ee35fdc67b840e087c43814e54c # - name: js # process: # cmd: sqlc-gen-json diff --git a/examples/authors/src/db/gen.rs b/examples/authors/src/db/gen.rs index 5bdd9e3..04903a5 100644 --- a/examples/authors/src/db/gen.rs +++ b/examples/authors/src/db/gen.rs @@ -1,13 +1,13 @@ /// @generated by the sqlc-gen-rust on sqlc-generate using sqlc.yaml /// DO NOT EDIT. const GET_AUTHOR: &str = r#" -select id, uuid, name, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at +select id, uuid, name, genre, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at from authors where id = $1 limit 1 "#; const LIST_AUTHORS: &str = r#" -select id, uuid, name, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at +select id, uuid, name, genre, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at from authors order by name "#; @@ -17,13 +17,14 @@ INSERT INTO authors ( ) VALUES ( $1, $2 ) -RETURNING id, uuid, name, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at +RETURNING id, uuid, name, genre, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at "#; const CREATE_AUTHOR_FULL: &str = r#" INSERT INTO authors ( name, bio, data, + genre, attrs, ip_inet, ip_cidr, @@ -36,20 +37,38 @@ INSERT INTO authors ( created_at, updated_at ) VALUES ( - $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14 + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15 ) -RETURNING id, uuid, name, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at +RETURNING id, uuid, name, genre, bio, data, attrs, ip_inet, ip_cidr, mac_address, geo_point, geo_rect, geo_path, bit_a, varbit_a, created_at, updated_at "#; const DELETE_AUTHOR: &str = r#" delete from authors where id = $1 "#; +#[derive(Clone, Debug, PartialEq, postgres_derive::ToSql, postgres_derive::FromSql)] +#[cfg_attr(feature = "serde_support", derive(serde::Serialize, serde::Deserialize))] +#[postgres(name = "type_genre")] +pub enum TypeGenre { + #[postgres(name = "history")] + #[cfg_attr(feature = "serde_support", serde(rename = "history"))] + History, + #[postgres(name = "Children")] + #[cfg_attr(feature = "serde_support", serde(rename = "Children"))] + Children, + #[postgres(name = "cLaSSic")] + #[cfg_attr(feature = "serde_support", serde(rename = "cLaSSic"))] + CLaSSic, + #[postgres(name = "ADVENTURE")] + #[cfg_attr(feature = "serde_support", serde(rename = "ADVENTURE"))] + Adventure, +} #[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)] #[cfg_attr(feature = "serde_support", derive(serde::Serialize, serde::Deserialize))] pub(crate) struct Author { pub id: i64, pub uuid: Option, pub name: String, + pub genre: TypeGenre, pub bio: Option, pub data: Option, pub attrs: Option>>, @@ -70,6 +89,7 @@ pub(crate) struct CreateAuthorFullParams { pub name: String, pub bio: Option, pub data: Option, + pub genre: TypeGenre, pub attrs: Option>>, pub ip_inet: cidr::IpInet, pub ip_cidr: cidr::IpCidr, @@ -114,6 +134,7 @@ impl Queries { &arg.name, &arg.bio, &arg.data, + &arg.genre, &arg.attrs, &arg.ip_inet, &arg.ip_cidr, diff --git a/examples/authors/src/main.rs b/examples/authors/src/main.rs index c490f4d..d915627 100644 --- a/examples/authors/src/main.rs +++ b/examples/authors/src/main.rs @@ -51,6 +51,7 @@ fn main() -> Result<()> { let author_full_req = db::CreateAuthorFullParams { name: "Author Full".to_string(), bio: None, + genre: db::TypeGenre::CLaSSic, data: Some(serde_json::json!({ "age": 50, "gender": "male", @@ -86,11 +87,13 @@ fn main() -> Result<()> { created_at: time::OffsetDateTime::now_utc(), updated_at: time::OffsetDateTime::now_utc(), }; + let author_full_res = queries.create_author_full(author_full_req.clone()).unwrap(); assert_eq!(author_full_res.name, author_full_req.name); assert_eq!(author_full_res.bio, author_full_req.bio); assert_ne!(author_full_res.uuid, None); assert_eq!(author_full_res.data, author_full_req.data); + assert_eq!(author_full_res.genre, author_full_req.genre); assert_eq!(author_full_res.attrs, author_full_req.attrs); assert_eq!(author_full_res.ip_inet, author_full_req.ip_inet); assert_eq!(author_full_res.ip_cidr, author_full_req.ip_cidr); diff --git a/sqlc-gen/Cargo.toml b/sqlc-gen/Cargo.toml index ecf88ec..7fe76c5 100644 --- a/sqlc-gen/Cargo.toml +++ b/sqlc-gen/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sqlc-gen" -version = "0.2.2" +version = "0.2.3" edition = "2018" [profile.release] diff --git a/sqlc-gen/examples/authors/sqlc.yaml b/sqlc-gen/examples/authors/sqlc.yaml index c8c080e..5438965 100644 --- a/sqlc-gen/examples/authors/sqlc.yaml +++ b/sqlc-gen/examples/authors/sqlc.yaml @@ -6,7 +6,7 @@ plugins: - RUST_LOG wasm: url: file://./../../../target/wasm32-wasip1/release/sqlc-gen.wasm - sha256: 787417eebd532829912991ba4f7052371bc9ef35e8b557440b8dc895a0df954e + sha256: b6328c9d2112dbe6c8cfa489c2f1f12439702ee35fdc67b840e087c43814e54c # # - name: js # process: diff --git a/sqlc-gen/examples/batch/postgresql/gen.rs b/sqlc-gen/examples/batch/postgresql/gen.rs index e2eed69..1078f0e 100644 --- a/sqlc-gen/examples/batch/postgresql/gen.rs +++ b/sqlc-gen/examples/batch/postgresql/gen.rs @@ -6,10 +6,14 @@ from authors where author_id = $1 "#; #[derive(Clone, Debug, PartialEq, postgres_derive::ToSql, postgres_derive::FromSql)] +#[cfg_attr(feature = "serde_support", derive(serde::Serialize, serde::Deserialize))] +#[postgres(name = "book_type")] pub enum BookType { #[postgres(name = "FICTION")] + #[cfg_attr(feature = "serde_support", serde(rename = "FICTION"))] Fiction, #[postgres(name = "NONFICTION")] + #[cfg_attr(feature = "serde_support", serde(rename = "NONFICTION"))] Nonfiction, } #[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)] diff --git a/sqlc-gen/examples/batch/sqlc.json b/sqlc-gen/examples/batch/sqlc.json index 187ada2..be345f4 100644 --- a/sqlc-gen/examples/batch/sqlc.json +++ b/sqlc-gen/examples/batch/sqlc.json @@ -9,7 +9,7 @@ ], "wasm": { "url": "file://./../../../target/wasm32-wasip1/release/sqlc-gen.wasm", - "sha256": "787417eebd532829912991ba4f7052371bc9ef35e8b557440b8dc895a0df954e" + "sha256": "b6328c9d2112dbe6c8cfa489c2f1f12439702ee35fdc67b840e087c43814e54c" } } ], diff --git a/sqlc-gen/examples/booktest/gen/gen.rs b/sqlc-gen/examples/booktest/gen/gen.rs index 7d26f5f..d21d011 100644 --- a/sqlc-gen/examples/booktest/gen/gen.rs +++ b/sqlc-gen/examples/booktest/gen/gen.rs @@ -64,10 +64,14 @@ select say_hello from say_hello($1) "#; #[derive(Clone, Debug, PartialEq, postgres_derive::ToSql, postgres_derive::FromSql)] +#[cfg_attr(feature = "serde_support", derive(serde::Serialize, serde::Deserialize))] +#[postgres(name = "book_type")] pub enum BookType { #[postgres(name = "FICTION")] + #[cfg_attr(feature = "serde_support", serde(rename = "FICTION"))] Fiction, #[postgres(name = "NONFICTION")] + #[cfg_attr(feature = "serde_support", serde(rename = "NONFICTION"))] Nonfiction, } #[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)] diff --git a/sqlc-gen/examples/booktest/sqlc.yaml b/sqlc-gen/examples/booktest/sqlc.yaml index a91284a..5bb88f5 100644 --- a/sqlc-gen/examples/booktest/sqlc.yaml +++ b/sqlc-gen/examples/booktest/sqlc.yaml @@ -6,7 +6,7 @@ plugins: - RUST_LOG wasm: url: file://./../../../target/wasm32-wasip1/release/sqlc-gen.wasm - sha256: 787417eebd532829912991ba4f7052371bc9ef35e8b557440b8dc895a0df954e + sha256: b6328c9d2112dbe6c8cfa489c2f1f12439702ee35fdc67b840e087c43814e54c # # - name: js # process: diff --git a/sqlc-gen/examples/jets/sqlc.json b/sqlc-gen/examples/jets/sqlc.json index f2747c3..0492060 100644 --- a/sqlc-gen/examples/jets/sqlc.json +++ b/sqlc-gen/examples/jets/sqlc.json @@ -9,7 +9,7 @@ ], "wasm": { "url": "file://./../../../target/wasm32-wasip1/release/sqlc-gen.wasm", - "sha256": "787417eebd532829912991ba4f7052371bc9ef35e8b557440b8dc895a0df954e" + "sha256": "b6328c9d2112dbe6c8cfa489c2f1f12439702ee35fdc67b840e087c43814e54c" } } ], diff --git a/sqlc-gen/examples/ondeck/postgresql/gen.rs b/sqlc-gen/examples/ondeck/postgresql/gen.rs index d093791..764c4e6 100644 --- a/sqlc-gen/examples/ondeck/postgresql/gen.rs +++ b/sqlc-gen/examples/ondeck/postgresql/gen.rs @@ -75,10 +75,14 @@ GROUP BY 1 ORDER BY 1 "#; #[derive(Clone, Debug, PartialEq, postgres_derive::ToSql, postgres_derive::FromSql)] +#[cfg_attr(feature = "serde_support", derive(serde::Serialize, serde::Deserialize))] +#[postgres(name = "status")] pub enum Status { #[postgres(name = "op!en")] + #[cfg_attr(feature = "serde_support", serde(rename = "op!en"))] Open, #[postgres(name = "clo@sed")] + #[cfg_attr(feature = "serde_support", serde(rename = "clo@sed"))] Closed, } #[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)] diff --git a/sqlc-gen/examples/ondeck/sqlc.json b/sqlc-gen/examples/ondeck/sqlc.json index 0182ebb..f473437 100644 --- a/sqlc-gen/examples/ondeck/sqlc.json +++ b/sqlc-gen/examples/ondeck/sqlc.json @@ -9,7 +9,7 @@ ], "wasm": { "url": "file://./../../../target/wasm32-wasip1/release/sqlc-gen.wasm", - "sha256": "787417eebd532829912991ba4f7052371bc9ef35e8b557440b8dc895a0df954e" + "sha256": "b6328c9d2112dbe6c8cfa489c2f1f12439702ee35fdc67b840e087c43814e54c" } } ], diff --git a/sqlc-gen/src/codegen/type_enum.rs b/sqlc-gen/src/codegen/type_enum.rs index 46ac4a1..0abb726 100644 --- a/sqlc-gen/src/codegen/type_enum.rs +++ b/sqlc-gen/src/codegen/type_enum.rs @@ -32,6 +32,7 @@ fn generate_enum_variant(i: usize, val: String, seen: &mut HashSet) -> T let ident_variant = get_ident(&value.to_case(Case::Pascal)); quote! { #[postgres(name=#val)] + #[cfg_attr(feature = "serde_support", serde(rename=#val))] #ident_variant } } @@ -56,6 +57,7 @@ impl TypeEnum { fn generate_code(&self) -> TokenStream { let ident_enum_name = get_ident(&self.name()); + let type_name = self.name().to_case(Case::Snake); let mut seen = HashSet::new(); let variants = self .values @@ -67,6 +69,8 @@ impl TypeEnum { quote! { #[derive(Clone, Debug, PartialEq, postgres_derive::ToSql, postgres_derive::FromSql)] + #[cfg_attr(feature = "serde_support", derive(serde::Serialize, serde::Deserialize))] + #[postgres(name=#type_name)] pub enum #ident_enum_name { #(#variants),* }