Skip to content

Commit

Permalink
Merge pull request #1 from easeq/clean-and-validate-gen-code
Browse files Browse the repository at this point in the history
Cleanup and validate generated code
  • Loading branch information
easeq authored Sep 23, 2024
2 parents 105fcca + 9cb4b6a commit cd364a1
Show file tree
Hide file tree
Showing 24 changed files with 987 additions and 868 deletions.
46 changes: 46 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion examples/authors/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-wasi/release/sqlc-gen.wasm
sha256: 91320118cfc27080bdd42a70be43198d3430fd774aa2ea3920ce1f8b55d444c4
sha256: 7c02055c3eba7bcb913da0e9090541314d14da8c350b34891c4a2e5826c23307

# - name: js
# process:
Expand All @@ -19,6 +19,7 @@ sql:
- out: src/db
plugin: rust-gen
options:
type: async
lang: en-US
# - out: gen
# plugin: js
68 changes: 21 additions & 47 deletions examples/authors/src/db/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,10 @@ const GET_AUTHOR: &str = r#"
SELECT id, name, bio FROM authors
WHERE id = $1 LIMIT 1
"#;
#[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)]
pub(crate) struct GetAuthorParams {
pub id: i64,
}
#[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)]
pub(crate) struct GetAuthorRow {
pub id: i64,
pub name: String,
pub bio: Option<String>,
}
const LIST_AUTHORS: &str = r#"
SELECT id, name, bio FROM authors
ORDER BY name
"#;
#[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)]
pub(crate) struct ListAuthorsRow {
pub id: i64,
pub name: String,
pub bio: Option<String>,
}
const CREATE_AUTHOR: &str = r#"
INSERT INTO authors (
name, bio
Expand All @@ -33,59 +17,49 @@ INSERT INTO authors (
)
RETURNING id, name, bio
"#;
const DELETE_AUTHOR: &str = r#"
DELETE FROM authors
WHERE id = $1
"#;
#[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)]
pub(crate) struct CreateAuthorParams {
pub(crate) struct Author {
pub id: i64,
pub name: String,
pub bio: Option<String>,
}
#[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)]
pub(crate) struct CreateAuthorRow {
pub id: i64,
pub(crate) struct CreateAuthorParams {
pub name: String,
pub bio: Option<String>,
}
const DELETE_AUTHOR: &str = r#"
DELETE FROM authors
WHERE id = $1
"#;
#[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)]
pub(crate) struct DeleteAuthorParams {
pub id: i64,
}
pub struct Queries {
client: postgres::Client,
}
impl Queries {
pub fn new(client: postgres::Client) -> Self {
Self { client }
}
pub(crate) fn get_author(
pub(crate) fn create_author(
&mut self,
params: GetAuthorParams,
) -> anyhow::Result<GetAuthorRow> {
let row = self.client.query_one(GET_AUTHOR, &[&params.id])?;
arg: CreateAuthorParams,
) -> anyhow::Result<Author> {
let row = self.client.query_one(CREATE_AUTHOR, &[&arg.name, &arg.bio])?;
Ok(sqlc_core::FromPostgresRow::from_row(&row)?)
}
pub(crate) fn delete_author(&mut self, id: i64) -> anyhow::Result<()> {
self.client.execute(DELETE_AUTHOR, &[&id])?;
Ok(())
}
pub(crate) fn get_author(&mut self, id: i64) -> anyhow::Result<Author> {
let row = self.client.query_one(GET_AUTHOR, &[&id])?;
Ok(sqlc_core::FromPostgresRow::from_row(&row)?)
}
pub(crate) fn list_authors(&mut self) -> anyhow::Result<Vec<ListAuthorsRow>> {
pub(crate) fn list_authors(&mut self) -> anyhow::Result<Vec<Author>> {
let rows = self.client.query(LIST_AUTHORS, &[])?;
let mut result: Vec<ListAuthorsRow> = vec![];
let mut result: Vec<Author> = vec![];
for row in rows {
result.push(sqlc_core::FromPostgresRow::from_row(&row)?);
}
Ok(result)
}
pub(crate) fn create_author(
&mut self,
params: CreateAuthorParams,
) -> anyhow::Result<CreateAuthorRow> {
let row = self.client.query_one(CREATE_AUTHOR, &[&params.name, &params.bio])?;
Ok(sqlc_core::FromPostgresRow::from_row(&row)?)
}
pub(crate) fn delete_author(
&mut self,
params: DeleteAuthorParams,
) -> anyhow::Result<()> {
self.client.execute(DELETE_AUTHOR, &[&params.id])?;
Ok(())
}
}
28 changes: 7 additions & 21 deletions examples/authors/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ fn main() -> Result<()> {
let authors = queries.list_authors().unwrap();
assert_eq!(authors.len(), 0);

let author_res_err = queries.get_author(db::GetAuthorParams { id: 1 }).is_err();
let author_res_err = queries.get_author(1).is_err();
assert_eq!(author_res_err, true);

let delete_res = queries
.delete_author(db::DeleteAuthorParams { id: 1 })
.is_ok();
let delete_res = queries.delete_author(1).is_ok();
assert_eq!(delete_res, true);

let author1_req = db::CreateAuthorParams {
Expand All @@ -56,11 +54,7 @@ fn main() -> Result<()> {
assert_eq!(author1_res.bio, author1_req.bio.clone());
assert!(author1_res.id > 0);

let mut authors_list_prepared = vec![db::ListAuthorsRow {
id: author1_res.id,
name: author1_res.name.clone(),
bio: author1_res.bio.clone(),
}];
let mut authors_list_prepared = vec![author1_res.clone()];
let authors = queries.list_authors().unwrap();
assert_eq!(authors.len(), 1);
assert_eq!(authors, authors_list_prepared);
Expand All @@ -74,24 +68,16 @@ fn main() -> Result<()> {
assert_eq!(author2_res.bio, author2_req.bio);
assert!(author2_res.id > 1);

authors_list_prepared.push(db::ListAuthorsRow {
id: author2_res.id,
name: author2_res.name,
bio: author2_res.bio,
});
authors_list_prepared.push(author2_res.clone());

let authors = queries.list_authors().unwrap();
assert_eq!(authors.len(), 2);
assert_eq!(authors, authors_list_prepared);

let author = queries.get_author(db::GetAuthorParams { id: 1 }).unwrap();
assert_eq!(author.id, author1_res.id);
assert_eq!(author.name, author1_res.name);
assert_eq!(author.bio, author1_res.bio);
let author = queries.get_author(1).unwrap();
assert_eq!(author, author1_res);

queries
.delete_author(db::DeleteAuthorParams { id: 1 })
.unwrap();
queries.delete_author(1).unwrap();
let authors = queries.list_authors().unwrap();
assert_eq!(authors.len(), 1);
assert_eq!(authors, authors_list_prepared[1..]);
Expand Down
1 change: 1 addition & 0 deletions sqlc-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ edition = "2021"

[dependencies]
postgres = "0.19.9"
postgres-types = "0.2.8"
2 changes: 2 additions & 0 deletions sqlc-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ postgres-types = "0.2.7"
strum = "0.26.3"
strum_macros = "0.26.4"
itertools = "0.13.0"
pluralizer = "0.4.0"
check_keyword = "0.3.1"

[build-dependencies]
prost-build = "0.9.0"
2 changes: 1 addition & 1 deletion sqlc-gen/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ generate-for-example:
file_with_ext=$$(ls sqlc.*) && \
file_ext=$${file_with_ext##*.} && \
ls sqlc.* | yq -iP ".plugins[0].wasm.sha256=\"$$sha_256\", .plugins[0].wasm.url=\"file://./../../../target/wasm32-wasi/release/sqlc-gen.wasm\"" $$file_with_ext -o $$file_ext && \
sqlc generate && \
RUST_LOG=debug sqlc generate && \
cd -

generate:
Expand Down
68 changes: 21 additions & 47 deletions sqlc-gen/examples/authors/postgresql/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,10 @@ const GET_AUTHOR: &str = r#"
SELECT id, name, bio FROM authors
WHERE id = $1 LIMIT 1
"#;
#[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)]
pub(crate) struct GetAuthorParams {
pub id: i64,
}
#[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)]
pub(crate) struct GetAuthorRow {
pub id: i64,
pub name: String,
pub bio: Option<String>,
}
const LIST_AUTHORS: &str = r#"
SELECT id, name, bio FROM authors
ORDER BY name
"#;
#[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)]
pub(crate) struct ListAuthorsRow {
pub id: i64,
pub name: String,
pub bio: Option<String>,
}
const CREATE_AUTHOR: &str = r#"
INSERT INTO authors (
name, bio
Expand All @@ -33,59 +17,49 @@ INSERT INTO authors (
)
RETURNING id, name, bio
"#;
const DELETE_AUTHOR: &str = r#"
DELETE FROM authors
WHERE id = $1
"#;
#[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)]
pub(crate) struct CreateAuthorParams {
pub(crate) struct Author {
pub id: i64,
pub name: String,
pub bio: Option<String>,
}
#[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)]
pub(crate) struct CreateAuthorRow {
pub id: i64,
pub(crate) struct CreateAuthorParams {
pub name: String,
pub bio: Option<String>,
}
const DELETE_AUTHOR: &str = r#"
DELETE FROM authors
WHERE id = $1
"#;
#[derive(Clone, Debug, sqlc_derive::FromPostgresRow, PartialEq)]
pub(crate) struct DeleteAuthorParams {
pub id: i64,
}
pub struct Queries {
client: postgres::Client,
}
impl Queries {
pub fn new(client: postgres::Client) -> Self {
Self { client }
}
pub(crate) fn get_author(
pub(crate) fn create_author(
&mut self,
params: GetAuthorParams,
) -> anyhow::Result<GetAuthorRow> {
let row = self.client.query_one(GET_AUTHOR, &[&params.id])?;
arg: CreateAuthorParams,
) -> anyhow::Result<Author> {
let row = self.client.query_one(CREATE_AUTHOR, &[&arg.name, &arg.bio])?;
Ok(sqlc_core::FromPostgresRow::from_row(&row)?)
}
pub(crate) fn delete_author(&mut self, id: i64) -> anyhow::Result<()> {
self.client.execute(DELETE_AUTHOR, &[&id])?;
Ok(())
}
pub(crate) fn get_author(&mut self, id: i64) -> anyhow::Result<Author> {
let row = self.client.query_one(GET_AUTHOR, &[&id])?;
Ok(sqlc_core::FromPostgresRow::from_row(&row)?)
}
pub(crate) fn list_authors(&mut self) -> anyhow::Result<Vec<ListAuthorsRow>> {
pub(crate) fn list_authors(&mut self) -> anyhow::Result<Vec<Author>> {
let rows = self.client.query(LIST_AUTHORS, &[])?;
let mut result: Vec<ListAuthorsRow> = vec![];
let mut result: Vec<Author> = vec![];
for row in rows {
result.push(sqlc_core::FromPostgresRow::from_row(&row)?);
}
Ok(result)
}
pub(crate) fn create_author(
&mut self,
params: CreateAuthorParams,
) -> anyhow::Result<CreateAuthorRow> {
let row = self.client.query_one(CREATE_AUTHOR, &[&params.name, &params.bio])?;
Ok(sqlc_core::FromPostgresRow::from_row(&row)?)
}
pub(crate) fn delete_author(
&mut self,
params: DeleteAuthorParams,
) -> anyhow::Result<()> {
self.client.execute(DELETE_AUTHOR, &[&params.id])?;
Ok(())
}
}
Loading

0 comments on commit cd364a1

Please sign in to comment.