Skip to content

Commit 7245b7f

Browse files
authored
Merge pull request #7 from easeq/for-enum
Enum related issues and improvements
2 parents 65013c1 + 2f4e714 commit 7245b7f

File tree

29 files changed

+163
-29
lines changed

29 files changed

+163
-29
lines changed

Cargo.lock

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

examples/authors-async/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ bit-vec = { version = "0.6", features = ["serde"] }
88
cidr = { version = "0.2", features = ["serde"] }
99
eui48 = { version = "1.1.0", features = ["serde"] }
1010
geo-types = { version = "0.7", features = ["serde"] }
11+
postgres-derive = "0.4.6"
1112
postgres-types = "0.2.8"
1213
postgresql_embedded = { version = "0.16.3", features = ["blocking", "bundled"] }
1314
refinery = { version = "0.8.14", features = ["tokio-postgres"] }

examples/authors-async/postgresql/migrations/V00001__initial.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
DROP TYPE IF EXISTS type_genre;
2+
CREATE TYPE type_genre as ENUM (
3+
'history',
4+
'Children',
5+
'cLaSSic',
6+
'ADVENTURE'
7+
);
8+
19
CREATE TABLE authors (
210
id BIGSERIAL PRIMARY KEY,
311
uuid uuid DEFAULT gen_random_uuid(),
412
name text NOT NULL,
13+
genre type_genre NOT NULL DEFAULT 'ADVENTURE',
514
bio text,
615
data json,
716
attrs hstore,

examples/authors-async/postgresql/query.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ INSERT INTO authors (
2424
name,
2525
bio,
2626
data,
27+
genre,
2728
attrs,
2829
ip_inet,
2930
ip_cidr,
@@ -36,7 +37,7 @@ INSERT INTO authors (
3637
created_at,
3738
updated_at
3839
) VALUES (
39-
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
40+
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
4041
)
4142
RETURNING *;
4243

examples/authors-async/sqlc.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins:
66
- RUST_LOG
77
wasm:
88
url: file://./../../target/wasm32-wasip1/release/sqlc-gen.wasm
9-
sha256: 787417eebd532829912991ba4f7052371bc9ef35e8b557440b8dc895a0df954e
9+
sha256: b6328c9d2112dbe6c8cfa489c2f1f12439702ee35fdc67b840e087c43814e54c
1010
# - name: js
1111
# process:
1212
# cmd: sqlc-gen-json

examples/authors-async/src/db/gen.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/// @generated by the sqlc-gen-rust on sqlc-generate using sqlc.yaml
22
/// DO NOT EDIT.
33
const GET_AUTHOR: &str = r#"
4-
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
4+
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
55
from authors
66
where id = $1
77
limit 1
88
"#;
99
const LIST_AUTHORS: &str = r#"
10-
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
10+
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
1111
from authors
1212
order by name
1313
"#;
@@ -17,13 +17,14 @@ INSERT INTO authors (
1717
) VALUES (
1818
$1, $2
1919
)
20-
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
20+
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
2121
"#;
2222
const CREATE_AUTHOR_FULL: &str = r#"
2323
INSERT INTO authors (
2424
name,
2525
bio,
2626
data,
27+
genre,
2728
attrs,
2829
ip_inet,
2930
ip_cidr,
@@ -36,20 +37,38 @@ INSERT INTO authors (
3637
created_at,
3738
updated_at
3839
) VALUES (
39-
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
40+
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
4041
)
41-
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
42+
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
4243
"#;
4344
const DELETE_AUTHOR: &str = r#"
4445
delete from authors
4546
where id = $1
4647
"#;
48+
#[derive(Clone, Debug, PartialEq, postgres_derive::ToSql, postgres_derive::FromSql)]
49+
#[cfg_attr(feature = "serde_support", derive(serde::Serialize, serde::Deserialize))]
50+
#[postgres(name = "type_genre")]
51+
pub enum TypeGenre {
52+
#[postgres(name = "history")]
53+
#[cfg_attr(feature = "serde_support", serde(rename = "history"))]
54+
History,
55+
#[postgres(name = "Children")]
56+
#[cfg_attr(feature = "serde_support", serde(rename = "Children"))]
57+
Children,
58+
#[postgres(name = "cLaSSic")]
59+
#[cfg_attr(feature = "serde_support", serde(rename = "cLaSSic"))]
60+
CLaSSic,
61+
#[postgres(name = "ADVENTURE")]
62+
#[cfg_attr(feature = "serde_support", serde(rename = "ADVENTURE"))]
63+
Adventure,
64+
}
4765
#[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)]
4866
#[cfg_attr(feature = "serde_support", derive(serde::Serialize, serde::Deserialize))]
4967
pub(crate) struct Author {
5068
pub id: i64,
5169
pub uuid: Option<uuid::Uuid>,
5270
pub name: String,
71+
pub genre: TypeGenre,
5372
pub bio: Option<String>,
5473
pub data: Option<serde_json::Value>,
5574
pub attrs: Option<std::collections::HashMap<String, Option<String>>>,
@@ -70,6 +89,7 @@ pub(crate) struct CreateAuthorFullParams {
7089
pub name: String,
7190
pub bio: Option<String>,
7291
pub data: Option<serde_json::Value>,
92+
pub genre: TypeGenre,
7393
pub attrs: Option<std::collections::HashMap<String, Option<String>>>,
7494
pub ip_inet: cidr::IpInet,
7595
pub ip_cidr: cidr::IpCidr,
@@ -114,6 +134,7 @@ impl Queries {
114134
&arg.name,
115135
&arg.bio,
116136
&arg.data,
137+
&arg.genre,
117138
&arg.attrs,
118139
&arg.ip_inet,
119140
&arg.ip_cidr,

examples/authors-async/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ async fn main() -> Result<()> {
6363
"age": 50,
6464
"gender": "male",
6565
})),
66+
genre: db::TypeGenre::CLaSSic,
6667
attrs: Some(
6768
[
6869
("attr_1".to_string(), Some("attr1 value".to_string())),
@@ -102,6 +103,7 @@ async fn main() -> Result<()> {
102103
assert_eq!(author_full_res.bio, author_full_req.bio);
103104
assert_ne!(author_full_res.uuid, None);
104105
assert_eq!(author_full_res.data, author_full_req.data);
106+
assert_eq!(author_full_res.genre, author_full_req.genre);
105107
assert_eq!(author_full_res.attrs, author_full_req.attrs);
106108
assert_eq!(author_full_res.ip_inet, author_full_req.ip_inet);
107109
assert_eq!(author_full_res.ip_cidr, author_full_req.ip_cidr);

examples/authors-deadpool/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ time = { version = "0.3.36", features = ["local-offset", "serde"] }
2121
tokio = { version = "1.40.0", features = ["rt", "rt-multi-thread", "macros", "signal"] }
2222
tokio-postgres = { version = "0.7.12", features = ["with-uuid-1", "with-time-0_3", "array-impls"] }
2323
uuid = { version = "1.10.0", features = ["serde"] }
24+
postgres-derive = "0.4.6"
2425

2526
[features]
2627
default = ["serde_support"]

examples/authors-deadpool/postgresql/migrations/V00001__initial.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
DROP TYPE IF EXISTS type_genre;
2+
CREATE TYPE type_genre as ENUM (
3+
'history',
4+
'Children',
5+
'cLaSSic',
6+
'ADVENTURE'
7+
);
8+
19
CREATE TABLE authors (
210
id BIGSERIAL PRIMARY KEY,
311
uuid uuid DEFAULT gen_random_uuid(),
412
name text NOT NULL,
13+
genre type_genre NOT NULL DEFAULT 'ADVENTURE',
514
bio text,
615
data json,
716
attrs hstore,

examples/authors-deadpool/postgresql/query.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ INSERT INTO authors (
2424
name,
2525
bio,
2626
data,
27+
genre,
2728
attrs,
2829
ip_inet,
2930
ip_cidr,
@@ -36,7 +37,7 @@ INSERT INTO authors (
3637
created_at,
3738
updated_at
3839
) VALUES (
39-
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
40+
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
4041
)
4142
RETURNING *;
4243

examples/authors-deadpool/sqlc.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins:
66
- RUST_LOG
77
wasm:
88
url: file://./../../target/wasm32-wasip1/release/sqlc-gen.wasm
9-
sha256: 787417eebd532829912991ba4f7052371bc9ef35e8b557440b8dc895a0df954e
9+
sha256: b6328c9d2112dbe6c8cfa489c2f1f12439702ee35fdc67b840e087c43814e54c
1010
# - name: js
1111
# process:
1212
# cmd: sqlc-gen-json

examples/authors-deadpool/src/db/gen.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/// @generated by the sqlc-gen-rust on sqlc-generate using sqlc.yaml
22
/// DO NOT EDIT.
33
const GET_AUTHOR: &str = r#"
4-
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
4+
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
55
from authors
66
where id = $1
77
limit 1
88
"#;
99
const LIST_AUTHORS: &str = r#"
10-
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
10+
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
1111
from authors
1212
order by name
1313
"#;
@@ -17,13 +17,14 @@ INSERT INTO authors (
1717
) VALUES (
1818
$1, $2
1919
)
20-
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
20+
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
2121
"#;
2222
const CREATE_AUTHOR_FULL: &str = r#"
2323
INSERT INTO authors (
2424
name,
2525
bio,
2626
data,
27+
genre,
2728
attrs,
2829
ip_inet,
2930
ip_cidr,
@@ -36,20 +37,38 @@ INSERT INTO authors (
3637
created_at,
3738
updated_at
3839
) VALUES (
39-
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
40+
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
4041
)
41-
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
42+
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
4243
"#;
4344
const DELETE_AUTHOR: &str = r#"
4445
delete from authors
4546
where id = $1
4647
"#;
48+
#[derive(Clone, Debug, PartialEq, postgres_derive::ToSql, postgres_derive::FromSql)]
49+
#[cfg_attr(feature = "serde_support", derive(serde::Serialize, serde::Deserialize))]
50+
#[postgres(name = "type_genre")]
51+
pub enum TypeGenre {
52+
#[postgres(name = "history")]
53+
#[cfg_attr(feature = "serde_support", serde(rename = "history"))]
54+
History,
55+
#[postgres(name = "Children")]
56+
#[cfg_attr(feature = "serde_support", serde(rename = "Children"))]
57+
Children,
58+
#[postgres(name = "cLaSSic")]
59+
#[cfg_attr(feature = "serde_support", serde(rename = "cLaSSic"))]
60+
CLaSSic,
61+
#[postgres(name = "ADVENTURE")]
62+
#[cfg_attr(feature = "serde_support", serde(rename = "ADVENTURE"))]
63+
Adventure,
64+
}
4765
#[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)]
4866
#[cfg_attr(feature = "serde_support", derive(serde::Serialize, serde::Deserialize))]
4967
pub(crate) struct Author {
5068
pub id: i64,
5169
pub uuid: Option<uuid::Uuid>,
5270
pub name: String,
71+
pub genre: TypeGenre,
5372
pub bio: Option<String>,
5473
pub data: Option<serde_json::Value>,
5574
pub attrs: Option<std::collections::HashMap<String, Option<String>>>,
@@ -70,6 +89,7 @@ pub(crate) struct CreateAuthorFullParams {
7089
pub name: String,
7190
pub bio: Option<String>,
7291
pub data: Option<serde_json::Value>,
92+
pub genre: TypeGenre,
7393
pub attrs: Option<std::collections::HashMap<String, Option<String>>>,
7494
pub ip_inet: cidr::IpInet,
7595
pub ip_cidr: cidr::IpCidr,
@@ -123,6 +143,7 @@ impl Queries {
123143
&arg.name,
124144
&arg.bio,
125145
&arg.data,
146+
&arg.genre,
126147
&arg.attrs,
127148
&arg.ip_inet,
128149
&arg.ip_cidr,

examples/authors-deadpool/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ async fn main() -> Result<()> {
6464
"age": 50,
6565
"gender": "male",
6666
})),
67+
genre: db::TypeGenre::CLaSSic,
6768
attrs: Some(
6869
[
6970
("attr_1".to_string(), Some("attr1 value".to_string())),
@@ -103,6 +104,7 @@ async fn main() -> Result<()> {
103104
assert_eq!(author_full_res.bio, author_full_req.bio);
104105
assert_ne!(author_full_res.uuid, None);
105106
assert_eq!(author_full_res.data, author_full_req.data);
107+
assert_eq!(author_full_res.genre, author_full_req.genre);
106108
assert_eq!(author_full_res.attrs, author_full_req.attrs);
107109
assert_eq!(author_full_res.ip_inet, author_full_req.ip_inet);
108110
assert_eq!(author_full_res.ip_cidr, author_full_req.ip_cidr);

examples/authors/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ cidr = { version = "0.2", features = ["serde"] }
99
eui48 = { version = "1.1.0", features = ["serde"] }
1010
geo-types = { version = "0.7", features = ["serde"] }
1111
postgres = "0.19.9"
12+
postgres-derive = "0.4.6"
1213
postgres-types = "0.2.8"
1314
postgresql_embedded = { version = "0.16.3", features = ["blocking", "bundled"] }
1415
refinery = { version = "0.8.14", features = ["postgres"] }

examples/authors/postgresql/migrations/V00001__initial.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
DROP TYPE IF EXISTS type_genre;
2+
CREATE TYPE type_genre as ENUM (
3+
'history',
4+
'Children',
5+
'cLaSSic',
6+
'ADVENTURE'
7+
);
8+
19
CREATE TABLE authors (
210
id BIGSERIAL PRIMARY KEY,
311
uuid uuid DEFAULT gen_random_uuid(),
412
name text NOT NULL,
13+
genre type_genre NOT NULL DEFAULT 'ADVENTURE',
514
bio text,
615
data json,
716
attrs hstore,

0 commit comments

Comments
 (0)