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

Allow raw identifiers for SqlIdentifier (column-name) #4030

Merged
merged 4 commits into from
May 24, 2024

Conversation

z33ky
Copy link
Contributor

@z33ky z33ky commented May 18, 2024

This allows using the r#identifier syntax for SqlIdentifier (column-name), which is used in derive macros.

table! {
    things {
        id -> Integer,
        r#identifier -> Integer,
    }
}

#[derive(diesel::Queryable)]
struct Thing {
    id: i32,
    r#identifier: i32
}

Previously the derive macros would panic when encountering such an identifier:

`"r#identifier"` is not a valid identifier

@weiznich weiznich requested a review from a team May 18, 2024 17:43
Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

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

Thanks for opening this PR. I think the change itself looks fine, but we want to have some tests that show that everything is parsed correctly.

@z33ky z33ky force-pushed the raw-sqlidentifier branch from 2b99860 to 39cc604 Compare May 20, 2024 15:36
@z33ky
Copy link
Contributor Author

z33ky commented May 20, 2024

Sure. I added it for the Queryable test, since that probably the simplest. Is that sufficient?
Trying to run the tests locally didn't work, I'm getting lots of compiler errors (also without my change). Here's to hoping it runs just like that :D

@z33ky z33ky force-pushed the raw-sqlidentifier branch 3 times, most recently from 3240f29 to 5822c30 Compare May 20, 2024 15:54
@weiznich
Copy link
Member

I think I would prefer to have these kind of test for Insertable, AsChangeset, Selectable and QueryableByName as well.

@z33ky z33ky force-pushed the raw-sqlidentifier branch 6 times, most recently from e925fd9 to bd2a6eb Compare May 20, 2024 17:26
@z33ky
Copy link
Contributor Author

z33ky commented May 20, 2024

Alright, I added raw identifiers to the respective test cases.
Sorry for the force-push spam due to my local setup being unable to execute the tests locally.
The spell-check complains about the Spanish "tipe", though Spanish is also used for the other names of that struct. I'm not sure what's up with that or how I would go about solving this.

@weiznich
Copy link
Member

No worries about the force pushes, that's fine.

For the spell checker: You can just add an exception here: https://github.com/diesel-rs/diesel/blob/master/.typos.toml#L8

As for running the tests: There is a bash script in bin/test that should allow to execute all tests as long as you've installed the build dependencies and configured the required database urls.

@z33ky z33ky force-pushed the raw-sqlidentifier branch from bd2a6eb to ed69337 Compare May 20, 2024 18:36
@z33ky
Copy link
Contributor Author

z33ky commented May 20, 2024

As for running the tests: There is a bash script in bin/test that should allow to execute all tests as long as you've installed the build dependencies and configured the required database urls.

Ah thanks, that works.

}

let conn = &mut connection();
let data = sql_query("SELECT 1 AS foo, 2 AS bar").get_result(conn);
assert_eq!(Ok(MyStruct { foo: 1, bar: 2 }), data);
let data = sql_query("SELECT 1 AS foo, 2 AS bar, 3 AS `r#type`").get_result(conn);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

QueryableByName perhaps poses an interesting question. Should the query indeed contain `r#type` to match or should the SQL query contain type and map to the raw identifier r#type?

Copy link
Member

Choose a reason for hiding this comment

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

I would expect type there.

@z33ky z33ky force-pushed the raw-sqlidentifier branch 3 times, most recently from dcef510 to 65a1dcb Compare May 20, 2024 19:32
diesel_derives/tests/schema.rs Show resolved Hide resolved
let conn = &mut connection();
let data = sql_query("SELECT 1 AS foo, 2 AS bar").get_result(conn);
let data = sql_query("SELECT 1 AS foo, 2 AS bar, 3 AS \"r#type\"").get_result(conn);
Copy link
Member

Choose a reason for hiding this comment

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

I feel like the above QueryableByName should try to read type, not r#type.

);

let conn = &mut connection();
let data = sql_query("SELECT 1 AS foo, 2 AS bar").get_result(conn);
assert_eq!(Ok(MyStruct(1, 2)), data);
let data = sql_query("SELECT 1 AS foo, 2 AS bar, 3 AS \"r#type\"").get_result(conn);
Copy link
Member

Choose a reason for hiding this comment

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

I think to be consistent with previous comment we probably should search for type as name if passing the r#type ident

and ideally also allow passing column_name = "some arbitrary string" where we would then parse 3 as "some arbitrary string" even if some arbitrary string is not an ident to support other weird things users might want. (I'm not sure if that is already supported and it's not a blocker here)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have lifted the restriction on column_name insofar, that a raw identifier is now allowed, however it must still correspond to a valid Rust identifier.
The previous error-message for invalid names is retained, though it now emits an unhelpful

error: Expected valid identifier, found `ty pe`. Diesel automatically renames invalid identifiers, perhaps you meant to write `ty pe_`?
   --> diesel_derives/tests/as_changeset.rs:287:32
    |
287 |         #[diesel(column_name = "ty pe")]
    |                                ^^^^^^^

The error-message should probably use inference::rust_name_for_sql_name()?

@z33ky z33ky force-pushed the raw-sqlidentifier branch 3 times, most recently from 113b6b5 to 365660d Compare May 22, 2024 14:02
@weiznich weiznich enabled auto-merge May 24, 2024 06:25
This allows using the `r#identifier` syntax for SqlIdentifier
(column-name), which is used in derive macros.
Previously the derive macros would panic when encountering such an
identifier:
    `"r#identifier"` is not a valid identifier
@weiznich weiznich force-pushed the raw-sqlidentifier branch from e846ad4 to 5a044a6 Compare May 24, 2024 08:42
@weiznich weiznich added this pull request to the merge queue May 24, 2024
Merged via the queue into diesel-rs:master with commit 3db4ed2 May 24, 2024
49 checks passed
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.

3 participants