GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix bug in splitSqlScript by inponomarev · Pull Request #7646 · testcontainers/testcontainers-java

Conversation

@inponomarev

@inponomarev inponomarev commented Oct 10, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Fix #3316
Fix #4441

Problem

Current version of ScriptUtils.splitSqlScript tries to take into account BEGIN...END blocks. However, it treats any END as an end of the block, even if it's END IF or END LOOP, so that the following one-statement script

BEGIN
     rec_loop: LOOP
         FETCH blah;
         IF something_wrong THEN LEAVE rec_loop; END IF;
         do_something_else;
     END LOOP;
END;

Is incorrectly split into following "statements":

BEGIN
    rec_loop: LOOP
        FETCH blah;
        IF something_wrong THEN LEAVE rec_loop; END IF
-----------------------------------------------------------------
       do_something_else
-----------------------------------------------------------------
   END LOOP
-----------------------------------------------------------------
END

This is also broken for the case when a special statement separator is used (like GO in MSSQL tradition, / in Oracle tradition or @ in DB2).

The following statement is not split over @ sign as when it comes to the separator, the algorithm counted more ENDs than BEGINs and and since the balance is not zero, it is not splitting the code:

BEGIN
    rec_loop: LOOP
        FETCH blah;
        IF something_wrong THEN LEAVE rec_loop; END IF;
        do_something_else;
    END LOOP;
END;
@
CALL something();
@

Solution

Apparently, the "true end of block" must be END immediately followed by a separator (with probably whitespace and/or comments preceding the separator).

The current implementation of splitSqlScript is poorly written and it's difficult to extend "syntax rules". So I have completely rewritten it in a style of a standard parser:

1 "Lexical analysis" is perfromed in ScriptScanner class. This class retrieves next id, next comment, next string literal etc.
2 "Syntactic rules" are implemented in ScriptSplitter whis is just a recursive procedural parser.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@inponomarev
fix splitSqlScript
029d79d
@inponomarev
inponomarev requested a review from a team October 10, 2023 20:28
@eddumelendez eddumelendez mentioned this pull request Oct 10, 2023
[Bug]: ScriptUtils.splitSqlScript works incorrectly with statements containing IFs and LOOPs #7647
Closed
inponomarev added 3 commits October 10, 2023 23:36
@inponomarev
refactor: inline single-line single-use methods
65d13db
@inponomarev
public --> package-private
4a6c599
@inponomarev
use ScriptScanner for containsSqlScriptDelimiters
a370105
@kiview

kiview commented Oct 11, 2023

Copy link
Copy Markdown
Member

Hey @inponomarev, thanks for your PR, but unfortunately we will have to close it.

ScriptUtils has been mostly taken from the Spring-JDBC project as a convenient feature and we agree that it can't support all use cases and all SQL syntax variants. We also agree with your assessment regarding the difficulty to extend and maintain it. But that's precisely the reason why we don't want to invest further in this feature or refactoring it extensively.

Instead, we recommend to use the mechanisms provided by the vendor specific images, which mostly means using withCopyFileToContainer() and a specific path for init scripts. I am pretty sure it should also work for the use case you have in mind.

In the future, we'd suggest to reach out to us first via Slack or GH discussions, to avoid unnecessary work on features that won't get merged.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kiview kiview closed this Oct 11, 2023
@inponomarev
Test for 3316
b71000c
@kiview kiview reopened this Oct 13, 2023
@inponomarev
support dollar-quoted strings
ed8f305
@kiview

kiview commented Oct 13, 2023

Copy link
Copy Markdown
Member

Re-opened after private discussion in Slack.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eddumelendez
Fix checkstyle
90bce80
@eddumelendez eddumelendez added this to the next milestone Oct 13, 2023
@eddumelendez eddumelendez added the type/enhancement label Oct 13, 2023
kiview
kiview previously approved these changes Oct 23, 2023
View reviewed changes

@kiview kiview left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Can you PTAL @eddumelendez?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread modules/database-commons/src/main/java/org/testcontainers/ext/ScriptScanner.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@eddumelendez @kiview
Update modules/database-commons/src/main/java/org/testcontainers/ext/…
473237f
…ScriptScanner.java

Co-authored-by: Kevin Wittek <[email protected]>
@eddumelendez
eddumelendez dismissed kiview’s stale review via 473237f October 30, 2023 18:56
eddumelendez
eddumelendez approved these changes Oct 30, 2023
View reviewed changes
@eddumelendez
eddumelendez merged commit 9cfa166 into testcontainers:main Oct 30, 2023
@eddumelendez

eddumelendez commented Oct 30, 2023

Copy link
Copy Markdown
Member

Thanks for your contribution, @inponomarev !

inponomarev reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

inponomarev added a commit to inponomarev/testcontainers-java that referenced this pull request Nov 16, 2023
@inponomarev
fix bugs introduced by testcontainers#7646
46f03d2
@inponomarev inponomarev mentioned this pull request Nov 16, 2023
Fix bugs in SQLScriptScanner with big String literals and PostgreSQL identifiers (as introduced by #7646) #7818
Merged
eddumelendez added a commit that referenced this pull request Nov 17, 2023
@inponomarev @eddumelendez
Fix bugs in SQLScriptScanner with big String literals and PostgreSQ…
b59888a
…L identifiers (as introduced by #7646) (#7818)

Co-authored-by: Eddú Meléndez Gonzales <[email protected]>
@inponomarev inponomarev mentioned this pull request Jan 12, 2024
Fix SQLUtils.splitSqlScript: keep semicolons after END when semicolon is not the statement separator #8108
Merged
fokion pushed a commit to fokion/testcontainers-java that referenced this pull request Jan 18, 2025
@inponomarev @eddumelendez
@kiview @fokion
Enhance ScriptUtils: fix parser (testcontainers#7646)
b84130d
Co-authored-by: Eddú Meléndez <[email protected]>
Co-authored-by: Kevin Wittek <[email protected]>
fokion pushed a commit to fokion/testcontainers-java that referenced this pull request Jan 18, 2025
@inponomarev @eddumelendez @fokion
Fix bugs in SQLScriptScanner with big String literals and PostgreSQ…
f7fff1d
…L identifiers (as introduced by testcontainers#7646) (testcontainers#7818)

Co-authored-by: Eddú Meléndez Gonzales <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@eddumelendez eddumelendez eddumelendez approved these changes
@kiview kiview kiview left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

type/enhancement

Projects

None yet

Milestone

1.19.2

Development

Successfully merging this pull request may close these issues.

TC_INITSCRIPT can't handle PostgreSQL dollar quoted strings MySQL Script execution failed if LOOP statement at PROCEDURE used

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@inponomarev @kiview @eddumelendez
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.