spring-data-relational icon indicating copy to clipboard operation
spring-data-relational copied to clipboard

Use force-quoting in `R2dbcMappingContext` by default

Open ShenFeng312 opened this issue 11 months ago • 10 comments

I have a table where the field is desc. Since it's a reserved keyword in PostgreSQL, I had to use the annotation @Column("\"desc\"") to allow it to be written properly. However, when reading the data, I found that it cannot be read correctly because in org.springframework.data.relational.core.conversion.MappingRelationalConverter#read, the org.springframework.data.relational.core.conversion.RowDocumentAccessor#hasValue method doesn't recognize the field. I believe some logic should be added here to solve this issue.

ShenFeng312 avatar Feb 14 '25 05:02 ShenFeng312

can we add a annotation like @ResultSetKey("desc") or other way to support it @mp911de @schauder

ShenFeng312 avatar Feb 17 '25 09:02 ShenFeng312

With @Column("\"desc\"") you make the double quotes part of the column name. Is that intended? SQL identifier in @Column, @Table and similar annotations are used exactly as they are given, and properly quoted to avoid interpretation as key words, or automatic changes to upper or lower case.

Therefore I guess you really want to use @Column("desc").

If that still doesn't work, please provide a full reproducer, preferably base on https://github.com/schauder/issue-jdbc-1993

schauder avatar Feb 17 '25 12:02 schauder

@schauder Thank you for your help, and sorry I forgot to mention that this bug occurs under r2dbc. I tried using the demo provided in your repository for testing, but I’m not sure how to use TestcontainersConfiguration. I believe when using r2dbc, saving data causes an error. The logs I printed indicate that it is not treating field and table names as keywords.

ShenFeng312 avatar Feb 18 '25 05:02 ShenFeng312

The cause of the bug seems to be that the default values provided by org.springframework.data.r2dbc.mapping.R2dbcMappingContext are inconsistent with JDBC. Should I submit a PR to fix this issue?

	public R2dbcMappingContext(NamingStrategy namingStrategy) {
		super(namingStrategy);
// use setForceQuote(true) to support keyword fields
		setForceQuote(false);
	}

ShenFeng312 avatar Feb 18 '25 06:02 ShenFeng312

We initially didn't want to enable forceQuote for R2DBC to retain compatibility. It would make sense to align for the next major revision, so feel free to submit a pull request. I assume that updating all tests will be the major effort here.

mp911de avatar Feb 18 '25 07:02 mp911de

@mp911de @schauder After changing the default values and modifying some test cases, I found that besides the changes to the default values, places like DefaultStatementMapper, UpdateMapper, and QueryMapper are all using SqlIdentifier.unquoted(xxx) by default. This might mean that more changes are required to fully meet our expectations.

ShenFeng312 avatar Feb 26 '25 01:02 ShenFeng312

have any other feedback?@mp911de

ShenFeng312 avatar Mar 14 '25 06:03 ShenFeng312

I looked at a couple of these cases, and those looked fine to me.

If you have any special cases in mind that seem suspicious to you let me know.

As part of the review I'll take a look at the unquoted calls within R2DBC and check if I think there are further things to change.

schauder avatar Mar 14 '25 11:03 schauder

Sorry for the delayed response to this issue. Below is my personal repository where I made temporary changes: https://github.com/ShenFeng312/spring-data-relational. I have also fixed some test cases. When I realized how many changes were required to support this, I started to feel that my approach might be flawed. Given that I don't fully understand the design, I felt that I shouldn't make such extensive modifications without fully understanding it. I hope the community can support this feature soon. Thank you very much. @schauder

ShenFeng312 avatar Apr 23 '25 01:04 ShenFeng312

Could you make a PR anyway? This way this doesn't get lost, which it very likely will when it just sits here as a comment.

schauder avatar Apr 23 '25 05:04 schauder