Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/main/java/net/sf/jsqlparser/parser/AbstractJSqlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ public enum Dialect {
Feature.allowSquareBracketQuotation), POSTGRESQL(
AdjacentStringLiterals.NEWLINE,
Feature.allowDollarQuotedStringTags,
Feature.allowDigitSeparators), H2, EXASOL, BIGQUERY(
AdjacentStringLiterals.WHITESPACE,
Feature.allowDoubleQuotedStrings,
Feature.allowHashLineComments,
Feature.allowBackslashEscapeCharacter), DATABRICKS(
Feature.allowDigitSeparators), H2(
Feature.allowDoubleSlashLineComments), EXASOL, BIGQUERY(
AdjacentStringLiterals.WHITESPACE,
Feature.allowDoubleQuotedStrings,
Feature.allowBackslashEscapeCharacter), SNOWFLAKE(
Feature.allowBackslashEscapeCharacter), INFORMIX, SPANNER, DORIS, COCKROACHDB, TERADATA, SQLITE;
Feature.allowHashLineComments,
Feature.allowBackslashEscapeCharacter), DATABRICKS(
AdjacentStringLiterals.WHITESPACE,
Feature.allowDoubleQuotedStrings,
Feature.allowBackslashEscapeCharacter), SNOWFLAKE(
Feature.allowBackslashEscapeCharacter,
Feature.allowDoubleSlashLineComments), INFORMIX, SPANNER, DORIS, COCKROACHDB, TERADATA, SQLITE;

private final Set<Feature> lexerFeatures;
private final AdjacentStringLiterals adjacentStringLiterals;
Expand Down Expand Up @@ -197,6 +199,14 @@ public P withHashLineComments(boolean allowHashLineComments) {
return withFeature(Feature.allowHashLineComments, allowHashLineComments);
}

public P withDoubleSlashLineComments() {
return withFeature(Feature.allowDoubleSlashLineComments, true);
}

public P withDoubleSlashLineComments(boolean allowDoubleSlashLineComments) {
return withFeature(Feature.allowDoubleSlashLineComments, allowDoubleSlashLineComments);
}

public P withBlankLinesAsSeparator(boolean allowBlankLinesAsStatementSeparator) {
return withFeature(Feature.allowBlankLinesAsStatementSeparator,
allowBlankLinesAsStatementSeparator);
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/net/sf/jsqlparser/parser/SimpleCharStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
/* Generated By:JavaCC: Do not edit this line by hand. SimpleCharStream.java Version 8.0.0 */
/*
* Warning: this file is maintained by hand on top of the JavaCC template. Among other manual
* adjustments it carries the in-buffer '#' and blank line rewrites in BeginToken(), which
* Feature.allowHashLineComments and Feature.allowBlankLinesAsStatementSeparator depend on. Never
* overwrite it with a freshly generated SimpleCharStream, that would silently drop the rewrites and
* break both features.
* adjustments it carries the in-buffer '#', '//' and blank line rewrites in BeginToken(), which
* Feature.allowHashLineComments, Feature.allowDoubleSlashLineComments and
* Feature.allowBlankLinesAsStatementSeparator depend on. Never overwrite it with a freshly
* generated SimpleCharStream, that would silently drop the rewrites and break these features.
*/
package net.sf.jsqlparser.parser;

Expand Down Expand Up @@ -66,6 +66,10 @@ public class SimpleCharStream {
static final char HASH_SUBSTITUTION = '\u0001';
static final char MYSQL_MINUS_SUBSTITUTION = '\u0002';
static final char MYSQL_COMMENT_ERROR = '\u0003';
// H2 and Snowflake `//` line comments: the same rewrite for the first `/`
// of a token-start `//` under Feature.allowDoubleSlashLineComments. Without
// it no rule matches `//`, so `//*` is a division and a block comment.
static final char DOUBLE_SLASH_SUBSTITUTION = '\u0004';
FeatureConfiguration featureConfiguration;
private boolean mySqlExecutableComment;
String mySqlCommentError;
Expand Down Expand Up @@ -241,6 +245,11 @@ public final char BeginToken() throws java.io.IOException {
&& featureConfiguration.getAsBoolean(Feature.allowHashLineComments)) {
buffer[bufpos] = HASH_SUBSTITUTION;
c = HASH_SUBSTITUTION;
} else if (c == '/' && featureConfiguration != null
&& featureConfiguration.getAsBoolean(Feature.allowDoubleSlashLineComments)
&& peekCharacters(1).equals("/")) {
buffer[bufpos] = DOUBLE_SLASH_SUBSTITUTION;
c = DOUBLE_SLASH_SUBSTITUTION;
} else if ((c == '-' || c == '/' || c == '*') && featureConfiguration != null
&& AbstractJSqlParser.Dialect.MYSQL.name()
.equals(featureConfiguration.getValue(Feature.dialect))) {
Expand Down Expand Up @@ -526,6 +535,8 @@ public String GetImage() {
image = "#" + image.substring(1);
} else if (!image.isEmpty() && image.charAt(0) == MYSQL_MINUS_SUBSTITUTION) {
image = "-" + image.substring(1);
} else if (!image.isEmpty() && image.charAt(0) == DOUBLE_SLASH_SUBSTITUTION) {
image = "/" + image.substring(1);
}
return image;
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/sf/jsqlparser/parser/feature/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,12 @@ public enum Feature {
*/
allowHashLineComments(false),

/**
* allows H2 and Snowflake `//` line comments; disabled by default, where `//*` is a division
* followed by a block comment as in MySQL, PostgreSQL, SQL Server and SQLite
*/
allowDoubleSlashLineComments(false),

/**
* treats three consecutive newlines as a statement separator (enabled by default). When
* disabled, blank lines are plain whitespace, so a statement containing them is parsed whole
Expand Down
6 changes: 5 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -6855,9 +6855,13 @@ TOKEN : /* Numeric Constants */
| < #HEX_DIGIT: ["0"-"9","A"-"F"] >
}

// `//` starts a line comment only under Feature.allowDoubleSlashLineComments
// (H2, Snowflake), where the stream rewrites its first `/` to
// SimpleCharStream.DOUBLE_SLASH_SUBSTITUTION as for `#` below. Elsewhere
// `//*` is a division followed by a block comment.
SPECIAL_TOKEN:
{
< LINE_COMMENT: ("--" | "//") (~["\r","\n"])*>
< LINE_COMMENT: ("--" | "\u0004/") (~["\r","\n"])*>
}

// MySQL `#` line comment (#2499), flag-gated. Under
Expand Down
6 changes: 5 additions & 1 deletion src/site/sphinx/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,9 @@ One grammar covers every supported RDBMS, but a few pieces of syntax mean differ
* - ``DATABRICKS``
- ``withDoubleQuotedStrings``, ``withBackslashEscapeCharacter``, any-whitespace rule for adjacent string literals
* - ``SNOWFLAKE``
- ``withBackslashEscapeCharacter`` only, double quotes stay quoted identifiers
- ``withBackslashEscapeCharacter`` and ``withDoubleSlashLineComments``, double quotes stay quoted identifiers
* - ``H2``
- ``withDoubleSlashLineComments``
* - ``INFORMIX``
- Informix ``ALTER TABLE ... ADD CONSTRAINT`` definitions with optional trailing constraint names
* - ``SPANNER``
Expand Down Expand Up @@ -2097,6 +2099,8 @@ The individual features
- ``".."`` reads as a string literal instead of a quoted identifier (BigQuery, Spark/Databricks, MySQL default ``sql_mode``); combined with ``withBackslashEscapeCharacter``, ``\\"`` does not end the literal, as in MySQL ``"it\\"s"``
* - ``withHashLineComments``
- ``#`` starts a line comment
* - ``withDoubleSlashLineComments``
- ``//`` starts a line comment (H2, Snowflake); otherwise ``//*`` is a division followed by a block comment
* - ``withBlankLinesAsSeparator``
- ``false`` stops two empty lines (``\n\n\n``) ending a statement, so they read as plain whitespace
* - ``withAdjacentStringLiterals``
Expand Down
43 changes: 42 additions & 1 deletion src/test/java/net/sf/jsqlparser/parser/CCJSqlParserUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ public void testNestingDepth() throws Exception {
@Test
public void testParseStatementIssue1250() throws Exception {
Statement result = CCJSqlParserUtil.parse(
"Select test.* from (Select * from sch.PERSON_TABLE // root test\n) as test");
"Select test.* from (Select * from sch.PERSON_TABLE // root test\n) as test",
parser -> parser.withDoubleSlashLineComments());
assertEquals("SELECT test.* FROM (SELECT * FROM sch.PERSON_TABLE) AS test",
result.toString());
}
Expand Down Expand Up @@ -668,6 +669,46 @@ public void testHashLineCommentsMySQLStatementSemantics() throws Exception {
CCJSqlParserUtil.parse("SELECT 1 # comment").toString());
}

@Test
public void testDoubleSlashLineCommentsFeature() throws Exception {
// default (flag off): `//*` is a division followed by a block comment, as on MySQL,
// MariaDB, PostgreSQL, SQL Server and SQLite, which all return the subquery here
String sql = "SELECT 6 //*x*/ 2, (SELECT max(pw) FROM secrets)";
assertEquals("SELECT 6 / 2, (SELECT max(pw) FROM secrets)",
CCJSqlParserUtil.parse(sql).toString());
for (AbstractJSqlParser.Dialect dialect : new AbstractJSqlParser.Dialect[] {
AbstractJSqlParser.Dialect.MYSQL, AbstractJSqlParser.Dialect.POSTGRESQL,
AbstractJSqlParser.Dialect.SQLSERVER, AbstractJSqlParser.Dialect.SQLITE}) {
assertEquals("SELECT 6 / 2, (SELECT max(pw) FROM secrets)",
CCJSqlParserUtil.parse(sql, p -> p.withDialect(dialect)).toString());
}
assertEquals(2,
CCJSqlParserUtil.parseStatements("SELECT 1 //**/ 1; DROP TABLE users").size());
assertThrows(JSQLParserException.class,
() -> CCJSqlParserUtil.parse("SELECT 1 // comment"));

// with the flag or the H2 and SNOWFLAKE presets: `//` to the end of the line, `//*`
// included, as H2 and Snowflake read it
for (Consumer<CCJSqlParser> consumer : List.<Consumer<CCJSqlParser>>of(
p -> p.withDoubleSlashLineComments(),
p -> p.withDialect(AbstractJSqlParser.Dialect.H2),
p -> p.withDialect(AbstractJSqlParser.Dialect.SNOWFLAKE))) {
assertEquals("SELECT 6", CCJSqlParserUtil.parse(sql, consumer).toString());
assertEquals("SELECT 1 FROM t",
CCJSqlParserUtil.parse("SELECT 1 // comment\nFROM t", consumer).toString());
assertEquals("SELECT 'a//b'",
CCJSqlParserUtil.parse("SELECT 'a//b'", consumer).toString());
}
assertEquals(List.of("// leading"),
((PlainSelect) CCJSqlParserUtil.parse("// leading\nSELECT 1",
p -> p.withDoubleSlashLineComments())).getLeadingComments());
// an explicit switch after the preset wins
assertEquals("SELECT 6 / 2", CCJSqlParserUtil.parse("SELECT 6 //*x*/ 2",
p -> p.withDialect(AbstractJSqlParser.Dialect.H2)
.withDoubleSlashLineComments(false))
.toString());
}

@Test
public void testDialectPresets() throws Exception {
// MYSQL and MARIADB: backslash escapes and # line comments (MySQL 8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public void lineCommentsExcludeLineTerminators(String lineTerminator)
@Test
public void mixedCommentsRetainExactImagesAndSourceOrder() throws JSQLParserException {
Select select = (Select) CCJSqlParserUtil
.parse("/* a */ -- b\r\n// c\r/* d */ SELECT 1");
.parse("/* a */ -- b\r\n// c\r/* d */ SELECT 1",
parser -> parser.withDoubleSlashLineComments());
assertEquals(List.of("/* a */", "-- b", "// c", "/* d */"), select.getLeadingComments());
}

Expand Down
Loading