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

removing sqlCommentParseEnabled config in SQLParser rule #29575

Merged
merged 5 commits into from
Dec 28, 2023
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 @@ -37,11 +37,11 @@ public final class MySQLComStmtPreparePacket extends MySQLCommandPacket implemen
@Getter
private final HintValueContext hintValueContext;

public MySQLComStmtPreparePacket(final MySQLPacketPayload payload, final boolean sqlCommentParseEnabled) {
public MySQLComStmtPreparePacket(final MySQLPacketPayload payload) {
super(MySQLCommandPacketType.COM_STMT_PREPARE);
String originSQL = payload.readStringEOF();
hintValueContext = sqlCommentParseEnabled ? new HintValueContext() : SQLHintUtils.extractHint(originSQL).orElseGet(HintValueContext::new);
sql = sqlCommentParseEnabled ? originSQL : SQLHintUtils.removeHint(originSQL);
hintValueContext = SQLHintUtils.extractHint(originSQL).orElseGet(HintValueContext::new);
sql = SQLHintUtils.removeHint(originSQL);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ public final class MySQLComQueryPacket extends MySQLCommandPacket implements SQL
@Getter
private final HintValueContext hintValueContext;

public MySQLComQueryPacket(final String sql, final boolean sqlCommentParseEnabled) {
public MySQLComQueryPacket(final String sql) {
super(MySQLCommandPacketType.COM_QUERY);
hintValueContext = sqlCommentParseEnabled ? new HintValueContext() : SQLHintUtils.extractHint(sql).orElseGet(HintValueContext::new);
this.sql = sqlCommentParseEnabled ? sql : SQLHintUtils.removeHint(sql);
hintValueContext = SQLHintUtils.extractHint(sql).orElseGet(HintValueContext::new);
this.sql = SQLHintUtils.removeHint(sql);
}

public MySQLComQueryPacket(final MySQLPacketPayload payload, final boolean sqlCommentParseEnabled) {
public MySQLComQueryPacket(final MySQLPacketPayload payload) {
super(MySQLCommandPacketType.COM_QUERY);
String originSQL = payload.readStringEOF();
hintValueContext = sqlCommentParseEnabled ? new HintValueContext() : SQLHintUtils.extractHint(originSQL).orElseGet(HintValueContext::new);
sql = sqlCommentParseEnabled ? originSQL : SQLHintUtils.removeHint(originSQL);
hintValueContext = SQLHintUtils.extractHint(originSQL).orElseGet(HintValueContext::new);
sql = SQLHintUtils.removeHint(originSQL);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ class MySQLComStmtPreparePacketTest {
@Test
void assertNew() {
when(payload.readStringEOF()).thenReturn("SELECT id FROM tbl WHERE id=?");
MySQLComStmtPreparePacket actual = new MySQLComStmtPreparePacket(payload, false);
MySQLComStmtPreparePacket actual = new MySQLComStmtPreparePacket(payload);
assertThat(actual.getSQL(), is("SELECT id FROM tbl WHERE id=?"));
}

@Test
void assertWrite() {
when(payload.readStringEOF()).thenReturn("SELECT id FROM tbl WHERE id=?");
MySQLComStmtPreparePacket actual = new MySQLComStmtPreparePacket(payload, false);
MySQLComStmtPreparePacket actual = new MySQLComStmtPreparePacket(payload);
actual.write(payload);
verify(payload).writeStringEOF("SELECT id FROM tbl WHERE id=?");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ class MySQLComQueryPacketTest {
@Test
void assertNew() {
when(payload.readStringEOF()).thenReturn("SELECT id FROM tbl");
MySQLComQueryPacket actual = new MySQLComQueryPacket(payload, false);
MySQLComQueryPacket actual = new MySQLComQueryPacket(payload);
assertThat(actual.getSQL(), is("SELECT id FROM tbl"));
}

@Test
void assertWrite() {
when(payload.readStringEOF()).thenReturn("SELECT id FROM tbl");
MySQLComQueryPacket actual = new MySQLComQueryPacket(payload, false);
MySQLComQueryPacket actual = new MySQLComQueryPacket(payload);
actual.write(payload);
verify(payload).writeInt1(MySQLCommandPacketType.COM_QUERY.getValue());
verify(payload).writeStringEOF("SELECT id FROM tbl");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,32 @@ public final class OpenGaussCommandPacketFactory {
*
* @param commandPacketType command packet type for PostgreSQL/openGauss
* @param payload packet payload for PostgreSQL
* @param sqlCommentParseEnabled sql comment parse enabled
* @return created instance
*/
public static PostgreSQLCommandPacket newInstance(final CommandPacketType commandPacketType, final PostgreSQLPacketPayload payload, final boolean sqlCommentParseEnabled) {
public static PostgreSQLCommandPacket newInstance(final CommandPacketType commandPacketType, final PostgreSQLPacketPayload payload) {
if (!OpenGaussCommandPacketType.isExtendedProtocolPacketType(commandPacketType)) {
payload.getByteBuf().skipBytes(1);
return getCommandPacket(commandPacketType, payload, sqlCommentParseEnabled);
return getCommandPacket(commandPacketType, payload);
}
List<PostgreSQLCommandPacket> result = new ArrayList<>();
while (payload.hasCompletePacket()) {
CommandPacketType type = OpenGaussCommandPacketType.valueOf(payload.readInt1());
int length = payload.getByteBuf().getInt(payload.getByteBuf().readerIndex());
PostgreSQLPacketPayload slicedPayload = new PostgreSQLPacketPayload(payload.getByteBuf().readSlice(length), payload.getCharset());
result.add(getCommandPacket(type, slicedPayload, sqlCommentParseEnabled));
result.add(getCommandPacket(type, slicedPayload));
}
return new PostgreSQLAggregatedCommandPacket(result);
}

private static PostgreSQLCommandPacket getCommandPacket(final CommandPacketType commandPacketType, final PostgreSQLPacketPayload payload, final boolean sqlCommentParseEnabled) {
private static PostgreSQLCommandPacket getCommandPacket(final CommandPacketType commandPacketType, final PostgreSQLPacketPayload payload) {
if (OpenGaussCommandPacketType.BATCH_BIND_COMMAND == commandPacketType) {
return new OpenGaussComBatchBindPacket(payload);
}
switch ((PostgreSQLCommandPacketType) commandPacketType) {
case SIMPLE_QUERY:
return new PostgreSQLComQueryPacket(payload, sqlCommentParseEnabled);
return new PostgreSQLComQueryPacket(payload);
case PARSE_COMMAND:
return new PostgreSQLComParsePacket(payload, sqlCommentParseEnabled);
return new PostgreSQLComParsePacket(payload);
case BIND_COMMAND:
return new PostgreSQLComBindPacket(payload);
case DESCRIBE_COMMAND:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ class OpenGaussCommandPacketFactoryTest {
@Test
void assertNewOpenGaussComBatchBindPacket() {
when(payload.getByteBuf()).thenReturn(mock(ByteBuf.class));
assertThat(OpenGaussCommandPacketFactory.newInstance(OpenGaussCommandPacketType.BATCH_BIND_COMMAND, payload, false), instanceOf(PostgreSQLAggregatedCommandPacket.class));
assertThat(OpenGaussCommandPacketFactory.newInstance(OpenGaussCommandPacketType.BATCH_BIND_COMMAND, payload), instanceOf(PostgreSQLAggregatedCommandPacket.class));
}

@Test
void assertNewPostgreSQLPacket() {
assertThat(OpenGaussCommandPacketFactory.newInstance(mock(PostgreSQLCommandPacketType.class), payload, false), instanceOf(PostgreSQLCommandPacket.class));
assertThat(OpenGaussCommandPacketFactory.newInstance(mock(PostgreSQLCommandPacketType.class), payload), instanceOf(PostgreSQLCommandPacket.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,29 @@ public final class PostgreSQLCommandPacketFactory {
*
* @param commandPacketType command packet type for PostgreSQL
* @param payload packet payload for PostgreSQL
* @param sqlCommentParseEnabled SQL comment parse enabled
* @return created instance
*/
public static PostgreSQLCommandPacket newInstance(final PostgreSQLCommandPacketType commandPacketType, final PostgreSQLPacketPayload payload, final boolean sqlCommentParseEnabled) {
public static PostgreSQLCommandPacket newInstance(final PostgreSQLCommandPacketType commandPacketType, final PostgreSQLPacketPayload payload) {
if (!PostgreSQLCommandPacketType.isExtendedProtocolPacketType(commandPacketType)) {
payload.getByteBuf().skipBytes(1);
return getPostgreSQLCommandPacket(commandPacketType, payload, sqlCommentParseEnabled);
return getPostgreSQLCommandPacket(commandPacketType, payload);
}
List<PostgreSQLCommandPacket> result = new ArrayList<>();
while (payload.hasCompletePacket()) {
PostgreSQLCommandPacketType type = PostgreSQLCommandPacketType.valueOf(payload.readInt1());
int length = payload.getByteBuf().getInt(payload.getByteBuf().readerIndex());
PostgreSQLPacketPayload slicedPayload = new PostgreSQLPacketPayload(payload.getByteBuf().readSlice(length), payload.getCharset());
result.add(getPostgreSQLCommandPacket(type, slicedPayload, sqlCommentParseEnabled));
result.add(getPostgreSQLCommandPacket(type, slicedPayload));
}
return new PostgreSQLAggregatedCommandPacket(result);
}

private static PostgreSQLCommandPacket getPostgreSQLCommandPacket(final PostgreSQLCommandPacketType commandPacketType, final PostgreSQLPacketPayload payload,
final boolean sqlCommentParseEnabled) {
private static PostgreSQLCommandPacket getPostgreSQLCommandPacket(final PostgreSQLCommandPacketType commandPacketType, final PostgreSQLPacketPayload payload) {
switch (commandPacketType) {
case SIMPLE_QUERY:
return new PostgreSQLComQueryPacket(payload, sqlCommentParseEnabled);
return new PostgreSQLComQueryPacket(payload);
case PARSE_COMMAND:
return new PostgreSQLComParsePacket(payload, sqlCommentParseEnabled);
return new PostgreSQLComParsePacket(payload);
case BIND_COMMAND:
return new PostgreSQLComBindPacket(payload);
case DESCRIBE_COMMAND:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public final class PostgreSQLComParsePacket extends PostgreSQLCommandPacket impl

private final HintValueContext hintValueContext;

public PostgreSQLComParsePacket(final PostgreSQLPacketPayload payload, final boolean sqlCommentParseEnabled) {
public PostgreSQLComParsePacket(final PostgreSQLPacketPayload payload) {
this.payload = payload;
payload.readInt4();
statementId = payload.readStringNul();
String originSQL = payload.readStringNul();
hintValueContext = sqlCommentParseEnabled ? new HintValueContext() : SQLHintUtils.extractHint(originSQL).orElseGet(HintValueContext::new);
sql = sqlCommentParseEnabled ? originSQL : SQLHintUtils.removeHint(originSQL);
hintValueContext = SQLHintUtils.extractHint(originSQL).orElseGet(HintValueContext::new);
sql = SQLHintUtils.removeHint(originSQL);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public final class PostgreSQLComQueryPacket extends PostgreSQLCommandPacket impl
@Getter
private final HintValueContext hintValueContext;

public PostgreSQLComQueryPacket(final PostgreSQLPacketPayload payload, final boolean sqlCommentParseEnabled) {
public PostgreSQLComQueryPacket(final PostgreSQLPacketPayload payload) {
payload.readInt4();
String originSQL = payload.readStringNul();
hintValueContext = sqlCommentParseEnabled ? new HintValueContext() : SQLHintUtils.extractHint(originSQL).orElseGet(HintValueContext::new);
sql = sqlCommentParseEnabled ? originSQL : SQLHintUtils.removeHint(originSQL);
hintValueContext = SQLHintUtils.extractHint(originSQL).orElseGet(HintValueContext::new);
sql = SQLHintUtils.removeHint(originSQL);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,47 +41,47 @@ class PostgreSQLCommandPacketFactoryTest {
@Test
void assertNewInstanceWithQueryComPacket() {
when(payload.getByteBuf()).thenReturn(mock(ByteBuf.class));
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.SIMPLE_QUERY, payload, false), instanceOf(PostgreSQLComQueryPacket.class));
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.SIMPLE_QUERY, payload), instanceOf(PostgreSQLComQueryPacket.class));
}

@Test
void assertNewInstanceWithParseComPacket() {
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.PARSE_COMMAND, payload, false), instanceOf(PostgreSQLAggregatedCommandPacket.class));
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.PARSE_COMMAND, payload), instanceOf(PostgreSQLAggregatedCommandPacket.class));
}

@Test
void assertNewInstanceWithBindComPacket() {
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.BIND_COMMAND, payload, false), instanceOf(PostgreSQLAggregatedCommandPacket.class));
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.BIND_COMMAND, payload), instanceOf(PostgreSQLAggregatedCommandPacket.class));
}

@Test
void assertNewInstanceWithDescribeComPacket() {
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.DESCRIBE_COMMAND, payload, false), instanceOf(PostgreSQLAggregatedCommandPacket.class));
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.DESCRIBE_COMMAND, payload), instanceOf(PostgreSQLAggregatedCommandPacket.class));
}

@Test
void assertNewInstanceWithExecuteComPacket() {
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.EXECUTE_COMMAND, payload, false), instanceOf(PostgreSQLAggregatedCommandPacket.class));
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.EXECUTE_COMMAND, payload), instanceOf(PostgreSQLAggregatedCommandPacket.class));
}

@Test
void assertNewInstanceWithSyncComPacket() {
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.SYNC_COMMAND, payload, false), instanceOf(PostgreSQLAggregatedCommandPacket.class));
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.SYNC_COMMAND, payload), instanceOf(PostgreSQLAggregatedCommandPacket.class));
}

@Test
void assertNewInstanceWithCloseComPacket() {
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.CLOSE_COMMAND, payload, false), instanceOf(PostgreSQLAggregatedCommandPacket.class));
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.CLOSE_COMMAND, payload), instanceOf(PostgreSQLAggregatedCommandPacket.class));
}

@Test
void assertNewInstanceWithFlushComPacket() {
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.FLUSH_COMMAND, payload, false), instanceOf(PostgreSQLAggregatedCommandPacket.class));
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.FLUSH_COMMAND, payload), instanceOf(PostgreSQLAggregatedCommandPacket.class));
}

@Test
void assertNewInstanceWithTerminationComPacket() {
when(payload.getByteBuf()).thenReturn(mock(ByteBuf.class));
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.TERMINATE, payload, false), instanceOf(PostgreSQLComTerminationPacket.class));
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.TERMINATE, payload), instanceOf(PostgreSQLComTerminationPacket.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void assertNewInstance() {
when(payload.readInt2()).thenReturn(1);
when(payload.readInt4()).thenReturn(0);
when(payload.readStringNul()).thenReturn("sql");
PostgreSQLComParsePacket actual = new PostgreSQLComParsePacket(payload, false);
PostgreSQLComParsePacket actual = new PostgreSQLComParsePacket(payload);
actual.write(payload);
assertThat(actual.getIdentifier(), is(PostgreSQLCommandPacketType.PARSE_COMMAND));
assertThat(actual.getSQL(), is("sql"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PostgreSQLComQueryPacketTest {
@Test
void assertNewInstance() {
when(payload.readStringNul()).thenReturn("sql");
PostgreSQLComQueryPacket actual = new PostgreSQLComQueryPacket(payload, false);
PostgreSQLComQueryPacket actual = new PostgreSQLComQueryPacket(payload);
actual.write(payload);
verify(payload).readInt4();
assertThat(actual.getSQL(), is("sql"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphere

private SQLStatement parse(final String sql) {
CacheOption cacheOption = new CacheOption(0, 0);
return new SQLStatementParserEngine(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL"), cacheOption, cacheOption, false).parse(sql, false);
return new SQLStatementParserEngine(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL"), cacheOption, cacheOption).parse(sql, false);
}

private static class TestCaseArgumentsProvider implements ArgumentsProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static RouteContext assertRoute(final String sql, final List<Object> para
Map<String, ShardingSphereSchema> schemas = buildSchemas();
ConfigurationProperties props = new ConfigurationProperties(new Properties());
SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(databaseType,
new CacheOption(2000, 65535L), new CacheOption(128, 1024L), false);
new CacheOption(2000, 65535L), new CacheOption(128, 1024L));
RuleMetaData ruleMetaData = new RuleMetaData(Arrays.asList(shardingRule, singleRule, timestampServiceRule));
ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, databaseType, mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), ruleMetaData, schemas);
SQLStatementContext sqlStatementContext =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public final class ShardingSphereSQLParserEngine implements SQLParserEngine {

private final DistSQLStatementParserEngine distSQLStatementParserEngine;

public ShardingSphereSQLParserEngine(final DatabaseType databaseType, final CacheOption sqlStatementCacheOption, final CacheOption parseTreeCacheOption, final boolean isParseComment) {
sqlStatementParserEngine = SQLStatementParserEngineFactory.getSQLStatementParserEngine(databaseType, sqlStatementCacheOption, parseTreeCacheOption, isParseComment);
public ShardingSphereSQLParserEngine(final DatabaseType databaseType, final CacheOption sqlStatementCacheOption, final CacheOption parseTreeCacheOption) {
sqlStatementParserEngine = SQLStatementParserEngineFactory.getSQLStatementParserEngine(databaseType, sqlStatementCacheOption, parseTreeCacheOption);
distSQLStatementParserEngine = new DistSQLStatementParserEngine();
}

Expand Down
Loading