Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: New Functionalities #13

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

feat: New Functionalities #13

wants to merge 9 commits into from

Conversation

buraktabn
Copy link

closes #12

I wanted to bypass geo types by making it String on the rust side to store the geom from SQL as a string. I've also added new features to integrate my project more.

Added:

  • Rust Enum creation based on SQL Enum types
  • Optional --serde flag if you wanna derive structs/enums with serde
  • Optional --datetime-lib to select time or chrono crate to use. defaults to chrono

Fixed:

  • If the column name is type, it's a reserved keyword in Rust. So, I created a helper function rust_type_fix. (naming could be better)

Notes:

  • I haven't try the migrate command yet, I only needed generate command

@buraktabn buraktabn marked this pull request as draft August 13, 2024 22:03
));
query_code.push('\n');

query_code.push_str(&generate_unique_query_code(table_name, schema_name, rows));
query_code.push('\n');
// query_code.push_str(&generate_unique_query_code(table_name, schema_name, &rows));
Copy link
Author

@buraktabn buraktabn Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This created duplicate function names for some of my tables. I had to comment this out and forget about it. will look into it soon.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's because primary keys are unique - the by_{} one is meant to be for the pk, where as this is meant to be for all other unique keys.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PK query is sort of important for cases where the PK is two or more columns, which may or may not each be unique

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I fixed it by filtering more based on the given table name and unique columns.

Comment on lines +283 to +289
pub(crate) fn rust_type_fix(column_name: &str) -> String {
if column_name == "type" {
String::from("r#type")
} else {
column_name.to_string()
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks for this

src/utils.rs Outdated Show resolved Hide resolved
src/utils.rs Outdated
Comment on lines 115 to 119
let serde_derives = if enable_serde {
", serde::Serialize, serde::Deserialize"
} else {
""
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually wonder whether instead of hardcoding this for serde, it's worth making more extensible by just allowing people to provide extra headers (sort of like how tonic build works for gRPC)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great idea. We could have new args like --struct-derive and --enum-derive that could take multiple values

@@ -543,7 +573,7 @@ fn generate_select_by_fk_query_code(
select_code.push_str(&format!(
" pub async fn all_by_{}_{}<'e, E: PgExecutor<'e>>(executor: E, {}_{}: {}) -> Result<Vec<{}>> {{\n",
to_snake_case(foreign_row_table_name),
foreign_row_column_name,
to_snake_case(column_name),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might need to be foreign_row_column_name still

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, you're right. I've changed this because I was figuring out what was causing the duplicate functions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with foreign_row_column_name:

     pub async fn all_by_profile_id<'e, E: PgExecutor<'e>>(executor: E, profile_id: i64) -> Result<Vec<Friendship>> {
        query_as::<_, Friendship>(r#"SELECT * FROM "friendship" WHERE from_user = $1"#)
            .bind(profile_id)
            .fetch_all(executor)
            .await
    }
    pub async fn all_by_profile_id<'e, E: PgExecutor<'e>>(executor: E, profile_id: i64) -> Result<Vec<Friendship>> {
        query_as::<_, Friendship>(r#"SELECT * FROM "friendship" WHERE to_user = $1"#)
            .bind(profile_id)
            .fetch_all(executor)
            .await
    }

with to_snake_case(column_name):

    pub async fn all_by_profile_from_user<'e, E: PgExecutor<'e>>(executor: E, profile_id: i64) -> Result<Vec<Friendship>> {
        query_as::<_, Friendship>(r#"SELECT * FROM "friendship" WHERE from_user = $1"#)
            .bind(profile_id)
            .fetch_all(executor)
            .await
    }
    pub async fn all_by_profile_to_user<'e, E: PgExecutor<'e>>(executor: E, profile_id: i64) -> Result<Vec<Friendship>> {
        query_as::<_, Friendship>(r#"SELECT * FROM "friendship" WHERE to_user = $1"#)
            .bind(profile_id)
            .fetch_all(executor)
            .await
    }

yeah I remember why I changed it :)

Comment on lines +83 to +92
.arg(
Arg::with_name("exclude")
.short('e')
.long("exclude")
.takes_value(true)
.value_name("SQLGEN_EXCLUDE")
.multiple(true)
.use_delimiter(true)
.help("Specify the excluded table name(s)"),
)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great idea

Cargo.toml Outdated Show resolved Hide resolved
@jayy-lmao
Copy link
Owner

Appreciate the work so far!
Thanks for doing this, I know @yellowHatpro will appreciate the enum generation!

@jayy-lmao jayy-lmao mentioned this pull request Aug 14, 2024
@buraktabn
Copy link
Author

With this library, we could create many more generators for other things as well. For example, I'm working on creating redis json schemas in here: R3-Lab@bdba715. But Since we only focus only on the rust types and helpers for SQL, Redis may not have life in here :) lmk what you think

@buraktabn buraktabn marked this pull request as ready for review August 15, 2024 19:27
@yellowHatpro
Copy link

yellowHatpro commented Aug 16, 2024

Appreciate the work so far! Thanks for doing this, I know @yellowHatpro will appreciate the enum generation!

Thanks a lot for this. I even had a column name type in one of the tables, and I had to make it work by renaming it to r#type.
The new features will definitely be helpful 🤩🤩

@jayy-lmao
Copy link
Owner

With this library, we could create many more generators for other things as well. For example, I'm working on creating redis json schemas in here: R3-Lab@bdba715. But Since we only focus only on the rust types and helpers for SQL, Redis may not have life in here :) lmk what you think

Yeah I think my intention is to maybe expand to support other DBs in future but it probably still would be SQL.
If you split out the Redis part v. happy to mention and link to it in README :)

schemas: Option<Vec<&str>>,
date_time_lib: DateTimeLib,
struct_derives: Vec<String>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ty for doing this 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Panics on unkown extension
3 participants