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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.sf.jsqlparser.parser.feature.FeatureConfiguration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
Expand Down Expand Up @@ -61,8 +62,9 @@ public enum Dialect {
this(AdjacentStringLiterals.OFF, lexerFeatures);
}

/** Returns the immutable lexer defaults shared by this dialect preset. */
public Set<Feature> getLexerFeatures() {
return lexerFeatures;
return Collections.unmodifiableSet(lexerFeatures);
}

public AdjacentStringLiterals getAdjacentStringLiterals() {
Expand Down Expand Up @@ -111,6 +113,10 @@ public P withTimeOut(long timeOutMillSeconds) {
return withFeature(Feature.timeOut, timeOutMillSeconds);
}

/**
* Applies this dialect's enabled lexer defaults to the current configuration. Existing options
* remain set; apply explicit overrides after this call, or use a new parser for a fresh preset.
*/
public P withDialect(Dialect dialect) {
withFeature(Feature.dialect, dialect.name());
if (dialect.getAdjacentStringLiterals() != AdjacentStringLiterals.OFF) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.parser;

import static org.junit.jupiter.api.Assertions.*;
import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect;
import net.sf.jsqlparser.parser.feature.Feature;
import org.junit.jupiter.api.Test;

class DialectPresetIsolationTest {
@Test
void callersCannotMutateSharedPresets() {
for (Dialect dialect : Dialect.values()) {
assertThrows(UnsupportedOperationException.class,
() -> dialect.getLexerFeatures().add(Feature.allowHashLineComments));
assertThrows(UnsupportedOperationException.class,
() -> dialect.getLexerFeatures().clear());
}
}

@Test
void explicitParserOverridesAreLocalAndDoNotModifyPresetDefaults() {
CCJSqlParser first = CCJSqlParserUtil.newParser("SELECT 1").withDialect(Dialect.MYSQL)
.withHashLineComments(false);
CCJSqlParser second = CCJSqlParserUtil.newParser("SELECT 1").withDialect(Dialect.MYSQL);
assertFalse(first.getAsBoolean(Feature.allowHashLineComments));
assertTrue(second.getAsBoolean(Feature.allowHashLineComments));
assertTrue(Dialect.MYSQL.getLexerFeatures().contains(Feature.allowHashLineComments));
}

@Test
void preservesAdditivePresetApplicationAndExplicitOverrideOrder() {
CCJSqlParser parser = CCJSqlParserUtil.newParser("SELECT 1").withDialect(Dialect.SQLSERVER)
.withDialect(Dialect.POSTGRESQL);
assertTrue(parser.getAsBoolean(Feature.allowSquareBracketQuotation));
parser.withSquareBracketQuotation(false);
assertFalse(parser.getAsBoolean(Feature.allowSquareBracketQuotation));
assertFalse(CCJSqlParserUtil.newParser("SELECT 1").withDialect(Dialect.POSTGRESQL)
.getAsBoolean(Feature.allowSquareBracketQuotation));
}
}
Loading