From 4716262645858c5f28ef236fb1a031dbe9142a92 Mon Sep 17 00:00:00 2001 From: Nathan Xu Date: Tue, 18 Aug 2020 11:26:32 -0400 Subject: [PATCH] HHH-14152 Preserve end-of-line chars in MultiLineImportExtractor --- hibernate-core/src/main/antlr/sql-script.g | 24 +++++++++---------- .../MultiLineImportExtractorTest.java | 4 ++-- .../test/fileimport/multi-line-statements.sql | 18 +++++++------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/hibernate-core/src/main/antlr/sql-script.g b/hibernate-core/src/main/antlr/sql-script.g index 9aba884a02..3a26ed6cfd 100644 --- a/hibernate-core/src/main/antlr/sql-script.g +++ b/hibernate-core/src/main/antlr/sql-script.g @@ -56,7 +56,7 @@ script ; statement - : { statementStarted(); } (statementPart)* DELIMITER { statementEnded(); } + : { statementStarted(); } (statementPart)* DELIMITER (WS_CHAR)* { statementEnded(); } ; statementPart @@ -71,8 +71,11 @@ quotedString ; nonSkippedChar - : c:CHAR { - out( c ); + : w:WS_CHAR { + out( w ); + } + | o:OTHER_CHAR { + out( o ); } ; @@ -105,18 +108,13 @@ QUOTED_TEXT protected ESCqs : '\'' '\'' ; -CHAR - : ( ' ' | '\t' ) => ( ' ' | '\t' ) - | ~( ';' | '\n' | '\r' ) +WS_CHAR + : ' ' + | '\t' + | ( "\r\n" | '\r' | '\n' ) {newline();} ; -NEWLINE - : ( '\r' | '\n' | '\r''\n' ) { - newline(); - // skip the entire match from the lexer stream - $setType( Token.SKIP ); - } - ; +OTHER_CHAR : ~( ';' | ' ' | '\t' | '\n' | '\r' ); LINE_COMMENT // match `//` or `--` followed by anything other than \n or \r until NEWLINE diff --git a/hibernate-core/src/test/java/org/hibernate/test/fileimport/MultiLineImportExtractorTest.java b/hibernate-core/src/test/java/org/hibernate/test/fileimport/MultiLineImportExtractorTest.java index 6df672a746..d1d4aa08a1 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/fileimport/MultiLineImportExtractorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/fileimport/MultiLineImportExtractorTest.java @@ -44,7 +44,7 @@ public class MultiLineImportExtractorTest { assertThat( commands[1], is( "INSERT INTO test_data VALUES (1, 'sample')" ) ); - assertThat( commands[2], is( "DELETE FROM test_data" ) ); + assertThat( commands[2], is( "DELETE\n FROM test_data" ) ); assertThat( commands[3], startsWith( "INSERT INTO test_data VALUES (2," ) ); assertThat( commands[3], containsString( "-- line 2" ) ); @@ -52,7 +52,7 @@ public class MultiLineImportExtractorTest { assertThat( commands[4], startsWith( "INSERT INTO test_data VALUES (3" ) ); assertThat( commands[4], not( containsString( "third record" ) ) ); - assertThat( commands[5], containsString( "INSERT INTO test_data (id, text)" ) ); + assertThat( commands[5], startsWith( "INSERT INTO test_data\nVALUES\n" ) ); } } } diff --git a/hibernate-core/src/test/resources/org/hibernate/test/fileimport/multi-line-statements.sql b/hibernate-core/src/test/resources/org/hibernate/test/fileimport/multi-line-statements.sql index 27a0fabd2f..c569d45b82 100644 --- a/hibernate-core/src/test/resources/org/hibernate/test/fileimport/multi-line-statements.sql +++ b/hibernate-core/src/test/resources/org/hibernate/test/fileimport/multi-line-statements.sql @@ -23,12 +23,14 @@ INSERT INTO test_data VALUES (2, 'Multi-line comment line 1 -- INSERT INTO test_data VALUES (1, NULL); INSERT INTO test_data VALUES (3 /* 'third record' */, NULL /* value */); -- with NULL value -INSERT INTO test_data (id, text) - VALUES - ( - 4 -- another record - , NULL - ); +INSERT INTO test_data +VALUES + ( + 4 -- another record + , NULL + ); +-- comment; +-- comment; + + --- comment; --- comment;