Skip to content

Commit

Permalink
Merge pull request #7 from easeq/for-enum
Browse files Browse the repository at this point in the history
Enum related issues and improvements
  • Loading branch information
easeq authored Oct 19, 2024
2 parents 65013c1 + 2f4e714 commit 7245b7f
Show file tree
Hide file tree
Showing 29 changed files with 163 additions and 29 deletions.
19 changes: 17 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/authors-async/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 2 additions & 1 deletion examples/authors-async/postgresql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ INSERT INTO authors (
name,
bio,
data,
genre,
attrs,
ip_inet,
ip_cidr,
Expand All @@ -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 *;

Expand Down
2 changes: 1 addition & 1 deletion examples/authors-async/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 26 additions & 5 deletions examples/authors-async/src/db/gen.rs
Original file line number Diff line number Diff line change
@@ -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
"#;
Expand All @@ -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,
Expand All @@ -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<uuid::Uuid>,
pub name: String,
pub genre: TypeGenre,
pub bio: Option<String>,
pub data: Option<serde_json::Value>,
pub attrs: Option<std::collections::HashMap<String, Option<String>>>,
Expand All @@ -70,6 +89,7 @@ pub(crate) struct CreateAuthorFullParams {
pub name: String,
pub bio: Option<String>,
pub data: Option<serde_json::Value>,
pub genre: TypeGenre,
pub attrs: Option<std::collections::HashMap<String, Option<String>>>,
pub ip_inet: cidr::IpInet,
pub ip_cidr: cidr::IpCidr,
Expand Down Expand Up @@ -114,6 +134,7 @@ impl Queries {
&arg.name,
&arg.bio,
&arg.data,
&arg.genre,
&arg.attrs,
&arg.ip_inet,
&arg.ip_cidr,
Expand Down
2 changes: 2 additions & 0 deletions examples/authors-async/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions examples/authors-deadpool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 2 additions & 1 deletion examples/authors-deadpool/postgresql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ INSERT INTO authors (
name,
bio,
data,
genre,
attrs,
ip_inet,
ip_cidr,
Expand All @@ -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 *;

Expand Down
2 changes: 1 addition & 1 deletion examples/authors-deadpool/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 26 additions & 5 deletions examples/authors-deadpool/src/db/gen.rs
Original file line number Diff line number Diff line change
@@ -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
"#;
Expand All @@ -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,
Expand All @@ -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<uuid::Uuid>,
pub name: String,
pub genre: TypeGenre,
pub bio: Option<String>,
pub data: Option<serde_json::Value>,
pub attrs: Option<std::collections::HashMap<String, Option<String>>>,
Expand All @@ -70,6 +89,7 @@ pub(crate) struct CreateAuthorFullParams {
pub name: String,
pub bio: Option<String>,
pub data: Option<serde_json::Value>,
pub genre: TypeGenre,
pub attrs: Option<std::collections::HashMap<String, Option<String>>>,
pub ip_inet: cidr::IpInet,
pub ip_cidr: cidr::IpCidr,
Expand Down Expand Up @@ -123,6 +143,7 @@ impl Queries {
&arg.name,
&arg.bio,
&arg.data,
&arg.genre,
&arg.attrs,
&arg.ip_inet,
&arg.ip_cidr,
Expand Down
2 changes: 2 additions & 0 deletions examples/authors-deadpool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions examples/authors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
9 changes: 9 additions & 0 deletions examples/authors/postgresql/migrations/V00001__initial.sql
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Loading

0 comments on commit 7245b7f

Please sign in to comment.