Skip to content

Commit

Permalink
Merge pull request #1543 from loxp/antlr-parser
Browse files Browse the repository at this point in the history
add ANTLR PostgreSQL DAL parser
  • Loading branch information
terrymanu authored Dec 3, 2018
2 parents 0d65e1a + 5813fcd commit 728b328
Show file tree
Hide file tree
Showing 25 changed files with 374 additions and 94 deletions.
24 changes: 20 additions & 4 deletions sharding-core/src/main/antlr4/imports/PostgreSQLDALStatement.g4
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,26 @@ grammar PostgreSQLDALStatement;
import PostgreSQLKeyword, Keyword, BaseRule, DataType, Symbol;

show
: SHOW showParam
: SHOW (ALL | ID)
;

setParam
: SET scope? setClause
;

scope
: SESSION | LOCAL
;

showParam
: ALL
| ID
setClause
: TIME ZONE timeZoneType
| ID (TO | EQ_) (STRING | DEFAULT)
;

timeZoneType
: NUMBER | LOCAL | DEFAULT
;

resetParam
: RESET (ALL | ID)
;
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ execute
| alterRoleResetConfig
| dropRole
| show
| setParam
| resetParam
;
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,7 @@ public SQLStatement parse(final boolean useCache) {
return cachedSQLStatement.get();
}
LexerEngine lexerEngine = LexerEngineFactory.newInstance(dbType, sql);
lexerEngine.nextToken();
Token firstToken = lexerEngine.getCurrentToken();
SQLStatement result;

if (PostgreSQLKeyword.SHOW == lexerEngine.getCurrentToken().getType()) {
result = AntlrParsingEngine.parse(dbType, sql, shardingRule, shardingTableMetaData);
return result;
}

SQLParser sqlParser = SQLParserFactory.newInstance(dbType, lexerEngine.getCurrentToken().getType(), shardingRule, lexerEngine, shardingTableMetaData);
Token currentToken = lexerEngine.getCurrentToken();
if (firstToken != currentToken) {
if (DDLStatement.isDDL(firstToken.getType(), currentToken.getType())) {
result = AntlrParsingEngine.parse(dbType, sql, shardingRule, shardingTableMetaData);
} else {
result = sqlParser.parse();
}
} else if (TCLStatement.isTCL(firstToken.getType())) {
result = AntlrParsingEngine.parse(dbType, sql, shardingRule, shardingTableMetaData);
} else {
result = sqlParser.parse();
}
SQLStatement result = SQLParserFactory.newInstance(dbType, shardingRule, lexerEngine, shardingTableMetaData, sql).parse();
if (useCache) {
ParsingResultCache.getInstance().put(sql, result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,30 @@
import io.shardingsphere.core.parsing.antlr.extractor.statement.SQLStatementExtractorFactory;
import io.shardingsphere.core.parsing.antlr.extractor.statement.SQLStatementType;
import io.shardingsphere.core.parsing.parser.exception.SQLParsingUnsupportedException;
import io.shardingsphere.core.parsing.parser.sql.SQLParser;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import io.shardingsphere.core.rule.ShardingRule;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import org.antlr.v4.runtime.ParserRuleContext;

/**
* Parsing engine for Antlr.
*
* @author duhongjun
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class AntlrParsingEngine {
@AllArgsConstructor
public final class AntlrParsingEngine implements SQLParser {

/**
* Parse SQL.
*
* @param dbType database type
* @param sql SQL
* @param shardingRule sharding rule
* @param shardingTableMetaData sharding table meta data
* @return SQL statement
*/
public static SQLStatement parse(final DatabaseType dbType, final String sql, final ShardingRule shardingRule, final ShardingTableMetaData shardingTableMetaData) {
private DatabaseType dbType;

private String sql;

private ShardingRule shardingRule;

private ShardingTableMetaData shardingTableMetaData;

@Override
public SQLStatement parse() {
ParserRuleContext rootContext = SQLASTParserFactory.newInstance(dbType, sql).execute();
if (null == rootContext) {
throw new SQLParsingUnsupportedException(String.format("Unsupported SQL of `%s`", sql));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package io.shardingsphere.core.parsing.antlr.extractor.segment.engine.dialect.postgresql;

import com.google.common.base.Optional;
Expand All @@ -8,6 +25,11 @@
import io.shardingsphere.core.parsing.antlr.sql.segment.ShowParamSegment;
import org.antlr.v4.runtime.ParserRuleContext;

/**
* PostgreSQL show param extractor.
*
* @author loxp
*/
public class ShowParamExtractor implements OptionalSQLSegmentExtractor {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public enum SQLStatementType {

SET_VARIABLE("SetVariable"),

SET_PARAM("SetParam"), // PostgreSQL SET statement

RESET_PARAM("ResetParam"),

SHOW("Show");

private final String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package io.shardingsphere.core.parsing.antlr.extractor.statement.engine.dal;

import io.shardingsphere.core.parsing.antlr.extractor.statement.engine.AbstractSQLStatementExtractor;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import io.shardingsphere.core.parsing.parser.sql.dal.DALStatement;

/**
* DAL statement extractor.
*
* @author loxp
*/
public abstract class DALStatementExtractor extends AbstractSQLStatementExtractor {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package io.shardingsphere.core.parsing.antlr.extractor.statement.engine.dal.dialect.postgresql;

import io.shardingsphere.core.parsing.antlr.extractor.statement.engine.dal.DALStatementExtractor;
import io.shardingsphere.core.parsing.parser.dialect.postgresql.statement.ResetParamStatement;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;

/**
* PostgreSQL reset param statement extractor.
*
* @author loxp
*/
public class PostgreSQLResetParamExtractor extends DALStatementExtractor {

@Override
protected SQLStatement createStatement() {
return new ResetParamStatement();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package io.shardingsphere.core.parsing.antlr.extractor.statement.engine.dal.dialect.postgresql;

import io.shardingsphere.core.parsing.antlr.extractor.statement.engine.dal.DALStatementExtractor;
import io.shardingsphere.core.parsing.parser.dialect.postgresql.statement.SetParamStatement;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import lombok.NoArgsConstructor;

/**
* PostgreSQL set param statement extractor.
*
* @author loxp
*/
@NoArgsConstructor
public class PostgreSQLSetParamExtractor extends DALStatementExtractor {

@Override
protected SQLStatement createStatement() {
return new SetParamStatement();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package io.shardingsphere.core.parsing.antlr.extractor.statement.engine.dal.dialect.postgresql;

import io.shardingsphere.core.parsing.antlr.extractor.segment.engine.dialect.postgresql.ShowParamExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.statement.engine.dal.DALStatementExtractor;
import io.shardingsphere.core.parsing.parser.dialect.postgresql.statement.ShowStatement;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;

/**
* PostgreSQL show statement extractor.
*
* @author loxp
*/
public final class PostgreSQLShowExtractor extends DALStatementExtractor {

public PostgreSQLShowExtractor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import io.shardingsphere.core.parsing.antlr.extractor.statement.SQLStatementExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.statement.SQLStatementType;
import io.shardingsphere.core.parsing.antlr.extractor.statement.engine.dal.dialect.postgresql.PostgreSQLResetParamExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.statement.engine.dal.dialect.postgresql.PostgreSQLSetParamExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.statement.engine.dal.dialect.postgresql.PostgreSQLShowExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.statement.engine.ddl.CreateIndexExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.statement.engine.ddl.CreateTableExtractor;
Expand Down Expand Up @@ -65,6 +67,8 @@ private static void registerTCL() {

private static void registerDAL() {
EXTRACTORS.put(SQLStatementType.SHOW, new PostgreSQLShowExtractor());
EXTRACTORS.put(SQLStatementType.SET_PARAM, new PostgreSQLSetParamExtractor());
EXTRACTORS.put(SQLStatementType.RESET_PARAM, new PostgreSQLResetParamExtractor());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package io.shardingsphere.core.parsing.antlr.filler.engnie;

import io.shardingsphere.core.metadata.table.ShardingTableMetaData;
Expand All @@ -7,6 +24,11 @@
import io.shardingsphere.core.parsing.parser.dialect.postgresql.statement.ShowStatement;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;

/**
* Show param segment filler.
*
* @author loxp
*/
public class ShowParamSegmentFiller implements SQLSegmentFiller {
@Override
public void fill(SQLSegment sqlSegment, SQLStatement sqlStatement, ShardingTableMetaData shardingTableMetaData) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package io.shardingsphere.core.parsing.antlr.sql.segment;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

/**
* Show param segment.
*
* @author loxp
*/
@RequiredArgsConstructor
@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ public enum PostgreSQLKeyword implements Keyword {
INITIALLY,
DEFERRED,
IMMEDIATE,
EXTRACT
EXTRACT,
CONSTRAINTS
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ public enum DefaultKeyword implements Keyword {
COMMIT,
ROLLBACK,
SAVEPOINT,
BEGIN,
BEGIN,
TRANSACTION,

/*
Other Command
Expand Down
Loading

0 comments on commit 728b328

Please sign in to comment.