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

Normalize source string when parsing #609

Merged
merged 2 commits into from
Jun 27, 2021
Merged

Conversation

davidjpfeiffer
Copy link
Contributor

This pull request fixes the bug described by issue #88

This bug can be reproduced by using ResultSet.getDate() to parse a DATETIME created using CURRENT_TIMESTAMP

The exception thrown will look something like this:
java.text.ParseException: Unparseable date: "2021-06-08 02:00:47" does not match (\p{Nd}++)\Q-\E(\p{Nd}++)\Q-\E(\p{Nd}++)\Q \E(\p{Nd}++)\Q:\E(\p{Nd}++)\Q:\E(\p{Nd}++)\Q.\E(\p{Nd}++)

The regex used to parse dates does not account for the fact that new dates created by SQLite's CURRENT_TIMESTAMP do not include millisecond information. The regex used by the date parser tries to find a string matching yyyy-MM-dd HH:mm:ss.SSS but the dates created by SQLite do not include the milliseconds .SSS so the date strings are not matching and this is causing the parser to return null. The solution implemented in this pull request detects this exact case (string length is exactly 19) and then appends .000 to the source string. This causes the regex to match on the string allowing the date string to be parsed and the Date object to be returned. The source object is not mutated, and the extra characters do not change the DATETIME represented by the date string, so I think this is a safe fix.

Please let me know if you have any questions about this.

The regex used to parse dates does not account for the fact that new dates created by SQLite's CURRENT_TIMESTAMP do not include millisecond information. The regex used by the date parser tries to find a string matching "yyyy-MM-dd HH:mm:ss.SSS" but the dates created by SQLite do not include the milliseconds (.SSS) so the date strings are not matching and this is causing the parser to return null. The solution is to detect this exact case (string length is exactly 19) and then append .000 to the source string. This causes the regex to match on the string. allowing the date string to be parsed, and the Date object to be returned. The source object is not mutated, and the extra characters do not change the DATETIME represented by the date string, so I think this is a safe fix.
@bendem
Copy link

bendem commented Jun 8, 2021

No unit test to catch possible future regression?

@davidjpfeiffer
Copy link
Contributor Author

@bendem I'm a little outside my comfort zone here, but I gave it a shot. I added a unit test to the StatementTest class. Let me know what you think.

@bendem
Copy link

bendem commented Jun 9, 2021

Looks good to me, but I'm not maintainer :)

@xerial xerial merged commit 2eeb817 into xerial:master Jun 27, 2021
@xerial
Copy link
Owner

xerial commented Jun 27, 2021

Thanks for the fix. It looks good to me

xerial added a commit that referenced this pull request Jun 27, 2021
xerial added a commit that referenced this pull request Jun 27, 2021
@xerial
Copy link
Owner

xerial commented Jun 27, 2021

@davidjpfeiffer Sorry. It's once merged, but the CI is failing because of this change. Could you submit a change again?

@davidjpfeiffer
Copy link
Contributor Author

@xerial sure, should I create a new PR with the exact same changes?

@xerial
Copy link
Owner

xerial commented Jun 28, 2021

Yes. In another PR, GitHub Action CI will run to find any issue in advance

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