提交 b7e01921 编写于 作者: T terrymanu

for #1864, use spi for sql parser

上级 06a9e500
......@@ -33,6 +33,7 @@
<module>sharding-core-parser-postgresql</module>
<module>sharding-core-parser-oracle</module>
<module>sharding-core-parser-sqlserver</module>
<module>sharding-core-parser-spi</module>
</modules>
<dependencies>
......
......@@ -39,6 +39,11 @@
<artifactId>sharding-core-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-core-parser-spi</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-core-parser-mysql</artifactId>
......
......@@ -19,20 +19,10 @@ package org.apache.shardingsphere.core.parsing.antlr.parser.impl;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.TokenStream;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.parsing.antlr.autogen.MySQLStatementLexer;
import org.apache.shardingsphere.core.parsing.antlr.autogen.OracleStatementLexer;
import org.apache.shardingsphere.core.parsing.antlr.autogen.PostgreSQLStatementLexer;
import org.apache.shardingsphere.core.parsing.antlr.autogen.SQLServerStatementLexer;
import org.apache.shardingsphere.core.parsing.antlr.parser.impl.dialect.MySQLParser;
import org.apache.shardingsphere.core.parsing.antlr.parser.impl.dialect.OracleParser;
import org.apache.shardingsphere.core.parsing.antlr.parser.impl.dialect.PostgreSQLParser;
import org.apache.shardingsphere.core.parsing.antlr.parser.impl.dialect.SQLServerParser;
import org.apache.shardingsphere.core.parsing.api.SQLParser;
import org.apache.shardingsphere.core.parsing.spi.ShardingParseEngine;
import org.apache.shardingsphere.core.spi.NewInstanceServiceLoader;
/**
* SQL parser factory.
......@@ -43,6 +33,10 @@ import org.apache.shardingsphere.core.parsing.antlr.parser.impl.dialect.SQLServe
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SQLParserFactory {
static {
NewInstanceServiceLoader.register(ShardingParseEngine.class);
}
/**
* New instance of SQL parser.
*
......@@ -51,40 +45,11 @@ public final class SQLParserFactory {
* @return SQL parser
*/
public static SQLParser newInstance(final DatabaseType databaseType, final String sql) {
return createSQLParser(databaseType, createLexer(databaseType, sql));
}
private static Lexer createLexer(final DatabaseType databaseType, final String sql) {
CharStream sqlCharStream = CharStreams.fromString(sql);
switch (databaseType) {
case H2:
case MySQL:
return new MySQLStatementLexer(sqlCharStream);
case PostgreSQL:
return new PostgreSQLStatementLexer(sqlCharStream);
case SQLServer:
return new SQLServerStatementLexer(sqlCharStream);
case Oracle:
return new OracleStatementLexer(sqlCharStream);
default:
throw new UnsupportedOperationException(String.format("Can not support database type [%s].", databaseType));
}
}
private static SQLParser createSQLParser(final DatabaseType databaseType, final Lexer lexer) {
TokenStream tokenStream = new CommonTokenStream(lexer);
switch (databaseType) {
case H2:
case MySQL:
return new MySQLParser(tokenStream);
case PostgreSQL:
return new PostgreSQLParser(tokenStream);
case SQLServer:
return new SQLServerParser(tokenStream);
case Oracle:
return new OracleParser(tokenStream);
default:
throw new UnsupportedOperationException(String.format("Can not support database type [%s].", databaseType));
for (ShardingParseEngine each : NewInstanceServiceLoader.newServiceInstances(ShardingParseEngine.class)) {
if (DatabaseType.valueOf(each.getDatabaseType()) == databaseType) {
return each.createSQLParser(sql);
}
}
throw new UnsupportedOperationException(String.format("Cannot support database type '%s'", databaseType));
}
}
......@@ -24,7 +24,7 @@ import org.apache.shardingsphere.core.parsing.antlr.autogen.MySQLStatementParser
import org.apache.shardingsphere.core.parsing.antlr.parser.advanced.AdvancedErrorStrategy;
import org.apache.shardingsphere.core.parsing.antlr.parser.advanced.AdvancedMatchHandler;
import org.apache.shardingsphere.core.parsing.antlr.parser.advanced.AdvancedParserATNSimulator;
import org.apache.shardingsphere.core.parsing.antlr.parser.impl.SQLParser;
import org.apache.shardingsphere.core.parsing.api.SQLParser;
/**
* SQL parser for MySQL.
......
......@@ -24,7 +24,7 @@ import org.apache.shardingsphere.core.parsing.antlr.autogen.OracleStatementParse
import org.apache.shardingsphere.core.parsing.antlr.parser.advanced.AdvancedErrorStrategy;
import org.apache.shardingsphere.core.parsing.antlr.parser.advanced.AdvancedMatchHandler;
import org.apache.shardingsphere.core.parsing.antlr.parser.advanced.AdvancedParserATNSimulator;
import org.apache.shardingsphere.core.parsing.antlr.parser.impl.SQLParser;
import org.apache.shardingsphere.core.parsing.api.SQLParser;
/**
* SQL parser for Oracle.
......
......@@ -24,7 +24,7 @@ import org.apache.shardingsphere.core.parsing.antlr.autogen.PostgreSQLStatementP
import org.apache.shardingsphere.core.parsing.antlr.parser.advanced.AdvancedErrorStrategy;
import org.apache.shardingsphere.core.parsing.antlr.parser.advanced.AdvancedMatchHandler;
import org.apache.shardingsphere.core.parsing.antlr.parser.advanced.AdvancedParserATNSimulator;
import org.apache.shardingsphere.core.parsing.antlr.parser.impl.SQLParser;
import org.apache.shardingsphere.core.parsing.api.SQLParser;
/**
* SQL parser for PostgreSQL.
......
......@@ -24,7 +24,7 @@ import org.apache.shardingsphere.core.parsing.antlr.autogen.SQLServerStatementPa
import org.apache.shardingsphere.core.parsing.antlr.parser.advanced.AdvancedErrorStrategy;
import org.apache.shardingsphere.core.parsing.antlr.parser.advanced.AdvancedMatchHandler;
import org.apache.shardingsphere.core.parsing.antlr.parser.advanced.AdvancedParserATNSimulator;
import org.apache.shardingsphere.core.parsing.antlr.parser.impl.SQLParser;
import org.apache.shardingsphere.core.parsing.api.SQLParser;
/**
* SQL parser for SQLServer.
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.core.parsing.antlr.spi;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.parsing.antlr.autogen.MySQLStatementLexer;
import org.apache.shardingsphere.core.parsing.antlr.parser.impl.dialect.MySQLParser;
import org.apache.shardingsphere.core.parsing.spi.ShardingParseEngine;
/**
* Sharding parse engine for H2.
*
* @author zhangliang
*/
public final class H2ShardingParseEngine implements ShardingParseEngine {
@Override
public String getDatabaseType() {
return DatabaseType.H2.name();
}
@Override
public MySQLParser createSQLParser(final String sql) {
return new MySQLParser(new CommonTokenStream(new MySQLStatementLexer(CharStreams.fromString(sql))));
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.core.parsing.antlr.spi;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.parsing.antlr.autogen.MySQLStatementLexer;
import org.apache.shardingsphere.core.parsing.antlr.parser.impl.dialect.MySQLParser;
import org.apache.shardingsphere.core.parsing.spi.ShardingParseEngine;
/**
* Sharding parse engine for MySQL.
*
* @author zhangliang
*/
public final class MySQLShardingParseEngine implements ShardingParseEngine {
@Override
public String getDatabaseType() {
return DatabaseType.MySQL.name();
}
@Override
public MySQLParser createSQLParser(final String sql) {
return new MySQLParser(new CommonTokenStream(new MySQLStatementLexer(CharStreams.fromString(sql))));
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.core.parsing.antlr.spi;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.parsing.antlr.autogen.OracleStatementLexer;
import org.apache.shardingsphere.core.parsing.antlr.parser.impl.dialect.OracleParser;
import org.apache.shardingsphere.core.parsing.spi.ShardingParseEngine;
/**
* Sharding parse engine for Oracle.
*
* @author zhangliang
*/
public final class OracleShardingParseEngine implements ShardingParseEngine {
@Override
public String getDatabaseType() {
return DatabaseType.Oracle.name();
}
@Override
public OracleParser createSQLParser(final String sql) {
return new OracleParser(new CommonTokenStream(new OracleStatementLexer(CharStreams.fromString(sql))));
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.core.parsing.antlr.spi;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.parsing.antlr.autogen.PostgreSQLStatementLexer;
import org.apache.shardingsphere.core.parsing.antlr.parser.impl.dialect.PostgreSQLParser;
import org.apache.shardingsphere.core.parsing.spi.ShardingParseEngine;
/**
* Sharding parse engine for PostgreSQL.
*
* @author zhangliang
*/
public final class PostgreSQLShardingParseEngine implements ShardingParseEngine {
@Override
public String getDatabaseType() {
return DatabaseType.PostgreSQL.name();
}
@Override
public PostgreSQLParser createSQLParser(final String sql) {
return new PostgreSQLParser(new CommonTokenStream(new PostgreSQLStatementLexer(CharStreams.fromString(sql))));
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.core.parsing.antlr.spi;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.parsing.antlr.autogen.SQLServerStatementLexer;
import org.apache.shardingsphere.core.parsing.antlr.parser.impl.dialect.SQLServerParser;
import org.apache.shardingsphere.core.parsing.spi.ShardingParseEngine;
/**
* Sharding parse engine for SQLServer.
*
* @author zhangliang
*/
public final class SQLServerShardingParseEngine implements ShardingParseEngine {
@Override
public String getDatabaseType() {
return DatabaseType.SQLServer.name();
}
@Override
public SQLServerParser createSQLParser(final String sql) {
return new SQLServerParser(new CommonTokenStream(new SQLServerStatementLexer(CharStreams.fromString(sql))));
}
}
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
org.apache.shardingsphere.core.parsing.antlr.spi.MySQLShardingParseEngine
org.apache.shardingsphere.core.parsing.antlr.spi.PostgreSQLShardingParseEngine
org.apache.shardingsphere.core.parsing.antlr.spi.SQLServerShardingParseEngine
org.apache.shardingsphere.core.parsing.antlr.spi.OracleShardingParseEngine
org.apache.shardingsphere.core.parsing.antlr.spi.H2ShardingParseEngine
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>sharding-core-parser</artifactId>
<groupId>org.apache.shardingsphere</groupId>
<version>4.0.0-RC2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sharding-core-parser-spi</artifactId>
<name>${project.artifactId}</name>
</project>
......@@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.apache.shardingsphere.core.parsing.antlr.parser.impl;
package org.apache.shardingsphere.core.parsing.api;
import org.antlr.v4.runtime.ParserRuleContext;
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.core.parsing.spi;
import org.apache.shardingsphere.core.parsing.api.SQLParser;
/**
* Sharding parse engine.
*
* @author zhangliang
*/
public interface ShardingParseEngine {
/**
* Get database type.
*
* @return database type
*/
String getDatabaseType();
/**
* Create SQL parser.
*
* @param sql SQL
* @return instance of SQL parser
*/
SQLParser createSQLParser(String sql);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册