Skip to content

[aot repositories] use text block when converting spring data jdbc query strings #1726

Closed

Description

@martinlippert

When converting generated queries for Spring Data JDBC repositories, the query can contain a lot of strings in quotes (and therefore escaped quote characters). A better way of writing those queries is to put them into a text block, which then avoid having to escape the quotes.

Activity

  1. added this to the 5.1.0.RELEASE milestone on Dec 4, 2025
  2. added
    for: eclipse
    for: vscode
    on Dec 4, 2025
  3. catturtle123 commented on Jan 19, 2026

    @catturtle123
    Contributor

    Hi @BoykoAlex,

    I noticed this issue is currently assigned to you for the 5.1.0.RELEASE milestone.

    Could you let me know if you have already started working on this? If not, would it be okay for me to pick this up and submit a PR?

    If I may contribute, here is my proposed approach:

    1. Implementation Plan
    I plan to modify DataRepositoryAotMetadataCodeLensProvider.

    • I will introduce logic to check if the query string contains quotes.
    • If it does, the generator will switch to using Java Text Blocks (""") to avoid excessive escaping.
    • If not, it will fall back to the existing standard string behavior.

    2. Testing Plan
    I plan to add a new test case to DataRepositoryAotMetadataCodeLensProviderJdbcTest.

    • This will verify that the generated @Query annotation correctly uses the text block syntax and preserves the raw SQL structure.

    Please feel free to let me know if I can pick this up and if the proposed direction looks good to you.

  4. BoykoAlex commented on Jan 19, 2026

    @BoykoAlex
    Contributor

    @catturtle123 please go ahead since I haven't started this at all. Thanks very much! I guess only 2 additional things came to my mind:

    1. I'd have this logic for all repositories not just jdbc (don't see a reason why not)
    2. I'd check what the java version on the project. if 15 or above then text blocks otherwise the current single line string. The IJavaProject should have info about the java version.
  5. catturtle123 commented on Jan 20, 2026

    @catturtle123
    Contributor

    That sounds good to me.

    Originally, since the issue was raised in the context of JDBC, I was thinking of keeping the change minimal and scoped just to JDBC repositories.
    But I agree there’s no strong reason to limit it, and applying this to all repository types makes sense and keeps the behavior consistent.

    Regarding the Java version, I agree with this approach and will follow it when deciding whether to use text blocks.

    I’ll rely on the Java version information from IJavaProject and reflect this logic in the implementation and accompanying tests when preparing the PR.

  6. catturtle123 commented on Jan 20, 2026

    @catturtle123
    Contributor

    I've submitted a PR to address this issue.

    PR: #1762

  7. martinlippert commented on Jan 22, 2026

    @martinlippert
    MemberAuthor

    Thanks a lot @catturtle123 for jumping on this and working on a PR. That is awesome.

    So, looks like the first step here is done and @BoykoAlex merged the PR #1762. Now, I think we need to go one step further... :-)

    Now the query param is using a text block, but the result is missing important things:

    Image
    • the indentation of the text block is not correct
    • the content of the text block is always a single line (which makes using a text block more or less useless)

    Instead of turning this into a single line text block, the text block should be split into multiple lines. The line break should be done at keyword boundaries, so that instead of generating:

        @Query("SELECT u FROM users u WHERE u.username = :username")
    

    the code lens action should generate:

        @Query("""
                SELECT u FROM users u
                WHERE u.username = :username
                """)
    

    Ideally the decision whether to turn this into a single line query or a multiline text block query should also get a preference setting, so that users can decide which style they prefer.

  8. BoykoAlex commented on Jan 23, 2026

    @BoykoAlex
    Contributor

    @catturtle123 Splitting the query properly on multiple lines needs a bit of investigation work. You'd need to find the proper parser for the query and split it somehow around parser tree nodes, i.e. "select" clause one line, "where" clause second line and so on. Probably for each query type you'd have a "formatter" class which would share common interface. Give formatter a string query, the formatter uinder the hood parser the query and splits it over multiple lines a line per clause... Anyway, this would my rough idea for it.

  9. catturtle123 commented on Jan 23, 2026

    @catturtle123
    Contributor

    @martinlippert and @BoykoAlex, thank you for the great feedback!

    Regarding the design, I'm planning to handle this via a global preference in the user settings so users can toggle their preferred query style.

    As suggested by Alex, I'll introduce a common formatting interface and create separate implementation classes for each specific query type to keep the logic modular and extensible. For JPQL and SQL specifically, I’ve encapsulated the formatting logic using the existing JpqlLexer inside a dedicated JpqlQueryFormatter class.

    Also, do you have a preference for the default style? Should it stay as the standard single-line string to maintain current behavior, or would you prefer the new multiline text block to be the default?

    Does this design and the use of the lexer sound like the right direction to you?

  10. martinlippert commented on Jan 23, 2026

    @martinlippert
    MemberAuthor

    My preference would be:

    • the single line string to be the default in the preferences
    • the indentation of the text block to be correct (as mentioned above)

    I don't know whether you need a parser/lexer to split the string or if a simple keyword matching would be enough, but that is up to you. @BoykoAlex has a much deeper insight into this, so I would follow his advice.

    As a side note: I would prefer to keep all this in a branch for now and not yet merge things into main to avoid a situation where we would have to revert changes before shipping a release due to this not being fully done.

  11. catturtle123 commented on Jan 23, 2026

    @catturtle123
    Contributor

    @martinlippert

    Thanks for the guidance.

    I’ll continue working on this in a separate branch on my fork for now.
    Once the feature is ready, would you prefer me to:

    • open a Draft PR for discussion,
    • open a regular PR (and if so, is there a specific branch you’d like me to target, or should it go against main),
    • or keep the changes on my fork and check back before opening a PR?

    Happy to follow whichever workflow you prefer.

  12. martinlippert commented on Jan 23, 2026

    @martinlippert
    MemberAuthor

    I think you can have a branch in your fork and create a regular draft PR from that, and then update the PR with new commits on your branch for further improvements. Then, if we decide to merge the PR, we can squash the PR into one commit and merge. Would that work for you?

  13. catturtle123 commented on Jan 23, 2026

    @catturtle123
    Contributor

    That works for me, thanks.

    I’ll open a draft PR from my fork shortly and continue to push updates there. Squash-and-merge on completion sounds good to me.

  14. catturtle123 commented on Jan 23, 2026

    @catturtle123
    Contributor

    A draft PR for this has been opened for discussion: #1763.

  15. BoykoAlex commented on Jan 23, 2026

    @BoykoAlex
    Contributor

    @catturtle123 I saw the daft PR for the formatting. Wow! That was fast!
    There is another idea i had in mind for the formatting... I'd try select this annotation with the query and use VScode command to format this code manually in the editor. If the results are okay perhaps we don't need to add all this support on the LS side to format the query and instead just use a "format" command that would perform the formatting on the client (add the command to the list on the server side). Therefore I'd check how the format works in VSCode java editor for the query. Then check the same in Eclipse...
    I'm not convinced that IDE formatter would have better results but I'd give it atry before proceeding with the LS side solution.

  16. martinlippert commented on Jan 23, 2026

    @martinlippert
    MemberAuthor

    @catturtle123 Two additional comments here:

    • I would prefer the format action as well, it makes things a lot easier. At the moment, you insert 8 spaces, but the code around the query might have a different style (tabs) and/or a different indentation. So using the format action from the surrounding IDE would solve those issues automatically for you. Otherwise, you would need to take those things into account yourself. I think you could take the indentation of the annotated method into account for this, for example.

    • you check a preference and decide which way to go, but haven't seen the other code that a new preference requires (default settings, data transfer, vscode definition, etc.). If you want to know more details about this, you can find other commits in the history that added preferences where you could take a look at the exact changes for this.

    Hope this helps!

  17. catturtle123 commented on Jan 24, 2026

    @catturtle123
    Contributor

    @BoykoAlex @martinlippert Thanks for the feedback! I didn't think about the indentation style issues (tabs/spaces), so using the IDE's format action makes a lot of sense. I'll give it a try in both editors and see how it works. I'll also make sure to follow the history for adding preferences to ensure nothing is missed.

  18. BoykoAlex commented on Jan 25, 2026

    @BoykoAlex
    Contributor

    @catturtle123 we may need to combine both: ls side formatting and then apply client formatting to adopt to the proper identation etc.

  19. catturtle123 commented on Jan 29, 2026

    @catturtle123
    Contributor

    @BoykoAlex Thanks for the great suggestion

    I've updated the implementation to combine LS-side query formatting with the IDE's built-in formatting. The custom formatter (JpqlQueryFormatter, MongoQueryFormatter) splits the query into meaningful multiline segments, and then the IDE's native format action is triggered after inserting the Query annotation to handle indentation and styling according to the user's workspace settings.

    This approach is implemented for the "Add Query" code action. Note that while the "Turn into @query" CodeLens isn't explicitly blocked, the IDE formatting may not work properly there.

    I'm currently verifying this behavior in Eclipse and have pushed the changes to the draft PR for review.

  20. BoykoAlex commented on Mar 2, 2026

    @BoykoAlex
    Contributor

    Number of commits went in to fix this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

for: eclipsefor: vscodetheme: spring-data-supporttype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions