diff --git a/hibernate-core/src/main/antlr/sql-script.g b/hibernate-core/src/main/antlr/sql-script.g index 3a26ed6cfd..2a2607c027 100644 --- a/hibernate-core/src/main/antlr/sql-script.g +++ b/hibernate-core/src/main/antlr/sql-script.g @@ -42,6 +42,11 @@ options { protected void statementEnded() { // by default, nothing to do } + + protected void skip() { + // by default, nothing to do + } + } @@ -52,11 +57,11 @@ options { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ script - : (statement)+ EOF + : (newLineToSkip)* (statement)+ EOF ; statement - : { statementStarted(); } (statementPart)* DELIMITER (WS_CHAR)* { statementEnded(); } + : { statementStarted(); } (statementPart (afterStatementPartNewline)*)* DELIMITER (newLineToSkip)* { statementEnded(); } ; statementPart @@ -70,12 +75,22 @@ quotedString } ; +afterStatementPartNewline + : NEWLINE { + out(" "); + } + ; + +newLineToSkip + : NEWLINE { + skip(); + } + ; + + nonSkippedChar - : w:WS_CHAR { - out( w ); - } - | o:OTHER_CHAR { - out( o ); + : c:CHAR { + out( c ); } ; @@ -108,13 +123,14 @@ QUOTED_TEXT protected ESCqs : '\'' '\'' ; -WS_CHAR - : ' ' - | '\t' - | ( "\r\n" | '\r' | '\n' ) {newline();} +CHAR + : ( ' ' | '\t' ) => ( ' ' | '\t' ) + | ~( ';' | '\n' | '\r' ) ; -OTHER_CHAR : ~( ';' | ' ' | '\t' | '\n' | '\r' ); +NEWLINE + : ( '\r' | '\n' | '\r''\n' ) + ; 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 abd3f8a0e9..07329692d9 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 @@ -33,9 +33,9 @@ public class MultiLineImportExtractorTest { public void testExtraction() throws IOException { final ClassLoader classLoader = ClassLoader.getSystemClassLoader(); - try ( final InputStream stream = classLoader.getResourceAsStream( IMPORT_FILE ) ) { + try (final InputStream stream = classLoader.getResourceAsStream( IMPORT_FILE )) { assertThat( stream, notNullValue() ); - try ( final InputStreamReader reader = new InputStreamReader( stream ) ) { + try (final InputStreamReader reader = new InputStreamReader( stream )) { final String[] commands = extractor.extractCommands( reader ); assertThat( commands, notNullValue() ); assertThat( commands.length, is( 6 ) ); @@ -46,7 +46,7 @@ public class MultiLineImportExtractorTest { assertThat( commands[1], is( "INSERT INTO test_data VALUES (1, 'sample')" ) ); - assertThat( commands[2], is( "DELETE" + System.lineSeparator() + " FROM test_data" ) ); + assertThat( commands[2], is( "DELETE FROM test_data" ) ); assertThat( commands[3], startsWith( "INSERT INTO test_data VALUES (2," ) ); assertThat( commands[3], containsString( "-- line 2" ) ); @@ -54,7 +54,7 @@ public class MultiLineImportExtractorTest { assertThat( commands[4], startsWith( "INSERT INTO test_data VALUES (3" ) ); assertThat( commands[4], not( containsString( "third record" ) ) ); - assertThat( commands[5], startsWith( "INSERT INTO test_data" + System.lineSeparator() + "VALUES" + System.lineSeparator() ) ); + assertThat( commands[5].replace( "\t", "" ), is( "INSERT INTO test_data VALUES ( 4 , NULL )" ) ); } } }