Skip to content

Commit

Permalink
add visitor for setVariable of mysql (#4228)
Browse files Browse the repository at this point in the history
* add visitor for setvariable

* fix for code style
  • Loading branch information
jingshanglu committed Feb 11, 2020
1 parent 74edb78 commit 655d4a0
Show file tree
Hide file tree
Showing 17 changed files with 291 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
-->

<sql-cases>
<sql-case id="set_parameter_equal" value="SET configuration_parameter = 'value'" db-types="PostgreSQL" />
<sql-case id="set_parameter_equal" value="SET configuration_parameter = 'value'" db-types="PostgreSQL, MySQL" />
<sql-case id="set_parameter_to" value="SET configuration_parameter TO 'value'" db-types="PostgreSQL" />
<sql-case id="set_parameter_for_session_scope" value="SET SESSION configuration_parameter TO 'value'" db-types="PostgreSQL" />
<sql-case id="set_parameter_for_local_scope" value="SET LOCAL configuration_parameter TO 'value'" db-types="PostgreSQL" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<sql-case id="show_index_with_table_back_quotes" value="SHOW INDEXES FROM `t_order` FROM sharding_db" db-types="MySQL" />
<sql-case id="show_index_with_database_back_quotes" value="SHOW INDEXES FROM t_order FROM `sharding_db`" db-types="MySQL" />
<sql-case id="show_index_with_back_quotes" value="SHOW KEYS FROM `sharding_db`.`t_order`" db-types="MySQL" />
<sql-case id="show_columns_from_table" value="SHOW COLUMNS FROM `t_order`" db-types="MySQL" />
<sql-case id="show_create_table" value="SHOW CREATE TABLE `t_order`" db-types="MySQL" />
<sql-case id="show_all" value="SHOW ALL" db-types="PostgreSQL" />
<sql-case id="show_server_version" value="SHOW SERVER_VERSION" db-types="PostgreSQL" />
<sql-case id="show_transaction_isolation_level" value="SHOW TRANSACTION ISOLATION LEVEL" db-types="PostgreSQL" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.sql.parser.sql.statement.dal.dialect.mysql;

import org.apache.shardingsphere.sql.parser.sql.statement.dal.DALStatement;

/**
* Set variable statement.
*
* @author lujingshang
*/
public final class SetStatement extends DALStatement {
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ showProfileType
;

setVariable
: SET variable_?
: SET variableExpr+
;

variableExpr
: variable_ EQ_ expr
;

showBinaryLogs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ public class FromTableSegment implements SQLSegment, TableAvailable {

private int stopIndex;

private TableSegment pattern;
private TableSegment table;

@Override
public String getTableName() {
return pattern.getTableName();
return table.getTableName();
}

@Override
public QuoteCharacter getTableQuoteCharacter() {
return QuoteCharacter.NONE;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.sql.parser.sql.segment.dal;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.sql.parser.sql.segment.SQLSegment;

/**
* Variable expr segment.
*
* @author lujingshang
*/
@RequiredArgsConstructor
@Getter
public final class VariableExpressionSegment implements SQLSegment {

private final int startIndex;

private final int stopIndex;

private final String variable;

private final String expression;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
package org.apache.shardingsphere.sql.parser.visitor;

import org.apache.shardingsphere.sql.parser.MySQLVisitor;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.VariableExprContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SetVariableContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ShowOtherContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.DescContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.FromSchemaContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.FromTableContext;
Expand All @@ -34,8 +37,10 @@
import org.apache.shardingsphere.sql.parser.sql.segment.dal.FromSchemaSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dal.FromTableSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dal.ShowLikeSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dal.VariableExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.generic.SchemaSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.generic.TableSegment;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.SetStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.DescribeStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowColumnsStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowCreateTableStatement;
Expand All @@ -44,9 +49,12 @@
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowTableStatusStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowTablesStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.UseStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowOtherStatement;
import org.apache.shardingsphere.sql.parser.sql.value.identifier.IdentifierValue;
import org.apache.shardingsphere.sql.parser.sql.value.literal.impl.StringLiteralValue;

import java.util.List;

/**
* MySQL DAL visitor.
*
Expand Down Expand Up @@ -119,7 +127,7 @@ public ASTNode visitShowColumns(final ShowColumnsContext ctx) {
FromSchemaContext fromSchemaContext = ctx.fromSchema();
if (null != fromTableContext) {
FromTableSegment fromTableSegment = (FromTableSegment) visit(fromTableContext);
result.setTable(fromTableSegment.getPattern());
result.setTable(fromTableSegment.getTable());
result.getAllSQLSegments().add(fromTableSegment);
}
if (null != fromSchemaContext) {
Expand All @@ -142,7 +150,7 @@ public ASTNode visitShowIndex(final ShowIndexContext ctx) {
}
if (null != fromTableContext) {
FromTableSegment fromTableSegment = (FromTableSegment) visitFromTable(fromTableContext);
TableSegment tableSegment = fromTableSegment.getPattern();
TableSegment tableSegment = fromTableSegment.getTable();
result.setTable(tableSegment);
result.getAllSQLSegments().add(tableSegment);
}
Expand All @@ -161,9 +169,27 @@ public ASTNode visitShowCreateTable(final ShowCreateTableContext ctx) {
public ASTNode visitFromTable(final FromTableContext ctx) {
FromTableSegment fromTableSegment = new FromTableSegment();
TableSegment tableSegment = (TableSegment) visit(ctx.tableName());
fromTableSegment.setPattern(tableSegment);
fromTableSegment.setTable(tableSegment);
return fromTableSegment;
}

@Override
public ASTNode visitShowOther(final ShowOtherContext ctx) {
return new ShowOtherStatement();
}

@Override
public ASTNode visitSetVariable(final SetVariableContext ctx) {
List<VariableExprContext> variableExprs = ctx.variableExpr();
SetStatement result = new SetStatement();
for (VariableExprContext vectx: variableExprs) {
String variable = vectx.variable_().getText();
String expr = vectx.expr().getText();
VariableExpressionSegment variableExpressionSegment = new VariableExpressionSegment(vectx.start.getStartIndex(), vectx.stop.getStopIndex(), variable, expr);
result.getAllSQLSegments().add(variableExpressionSegment);
}
return result;
}

@Override
public ASTNode visitFromSchema(final FromSchemaContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.dal.impl.ShowTableStatusStatementAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.dal.impl.ShowTablesStatementAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.dal.impl.UseStatementAssert;
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.dal.impl.SetVariableStatementAssert;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.SQLParserTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.dal.DescribeStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.dal.ShowColumnsStatementTestCase;
Expand All @@ -39,6 +40,7 @@
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.dal.ShowTableStatusStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.dal.ShowTablesStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.dal.UseStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.dal.SetVariableStatementTestCase;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.DALStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.DescribeStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowColumnsStatement;
Expand All @@ -48,6 +50,7 @@
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowTableStatusStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.ShowTablesStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.UseStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.SetStatement;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.postgresql.ShowStatement;

/**
Expand Down Expand Up @@ -85,6 +88,8 @@ public static void assertIs(final SQLCaseAssertContext assertContext, final DALS
ShowIndexStatementAssert.assertIs(assertContext, (ShowIndexStatement) actual, (ShowIndexStatementTestCase) expected);
} else if (actual instanceof ShowStatement) {
ShowStatementAssert.assertIs(assertContext, (ShowStatement) actual, (ShowStatementTestCase) expected);
} else if (actual instanceof SetStatement) {
SetVariableStatementAssert.assertIs(assertContext, (SetStatement) actual, (SetVariableStatementTestCase) expected);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.sql.parser.integrate.asserts.statement.dal.impl;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.sql.parser.integrate.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.dal.SetVariableStatementTestCase;
import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.SetStatement;

/**
* Set variable statement assert.
*
* @author lujingshang
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SetVariableStatementAssert {

/**
* Assert set variable statement is correct with expected parser result.
*
* @param assertContext assert context
* @param actual actual set Variable statement
* @param expected expected describe statement test case
*/
public static void assertIs(final SQLCaseAssertContext assertContext, final SetStatement actual, final SetVariableStatementTestCase expected) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.dal.ShowTableStatusStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.dal.ShowTablesStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.dal.UseStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.dal.ShowColumnsStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.dal.SetVariableStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.dcl.AlterLoginStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.dcl.AlterRoleStatementTestCase;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.dcl.AlterUserStatementTestCase;
Expand Down Expand Up @@ -193,7 +195,7 @@ public final class SQLParserTestCases {
private final List<ShowTablesStatementTestCase> showTablesTestCases = new LinkedList<>();

@XmlElement(name = "show-columns")
private final List<ShowTablesStatementTestCase> showColumnsTestCases = new LinkedList<>();
private final List<ShowColumnsStatementTestCase> showColumnsTestCases = new LinkedList<>();

@XmlElement(name = "show-create-table")
private final List<ShowCreateTableStatementTestCase> showCreateTableTestCases = new LinkedList<>();
Expand All @@ -203,10 +205,13 @@ public final class SQLParserTestCases {

@XmlElement(name = "show-index")
private final List<ShowIndexStatementTestCase> showIndexTestCases = new LinkedList<>();

@XmlElement(name = "show")
private final List<ShowStatementTestCase> showTestCases = new LinkedList<>();


@XmlElement(name = "set-variable")
private final List<SetVariableStatementTestCase> setVariableTestCases = new LinkedList<>();

@XmlElement(name = "common")
private final List<CommonStatementTestCase> commonTestCases = new LinkedList<>();

Expand Down Expand Up @@ -258,6 +263,7 @@ public Map<String, SQLParserTestCase> getAllSQLParserTestCases() {
putAll(showCreateTableTestCases, result);
putAll(showTableStatusTestCases, result);
putAll(showIndexTestCases, result);
putAll(setVariableTestCases, result);
putAll(showTestCases, result);
putAll(commonTestCases, result);
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.sql.parser.integrate.jaxb.domain.segment.impl.set;

import lombok.Getter;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.segment.AbstractExpectedSQLSegment;

import javax.xml.bind.annotation.XmlElement;

/**
* Expected variable expression.
*
* @author lujingshang
*/
@Getter
public final class ExpectedVariableExpression extends AbstractExpectedSQLSegment {

@XmlElement(name = "variable")
private String variable;

@XmlElement(name = "expression")
private String expression;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.sql.parser.integrate.jaxb.domain.statement.dal;

import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.segment.impl.set.ExpectedVariableExpression;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.SQLParserTestCase;

import javax.xml.bind.annotation.XmlElement;
import java.util.LinkedList;
import java.util.List;

/**
* set variable statement test case.
*
* @author lujingshang
*/
@Getter
@Setter
public final class SetVariableStatementTestCase extends SQLParserTestCase {

@XmlElement(name = "variable-expression")
private final List<ExpectedVariableExpression> variableExpressions = new LinkedList<>();
}
Loading

0 comments on commit 655d4a0

Please sign in to comment.