Unverified Commit d8a13eff authored by 张亮's avatar 张亮 Committed by GitHub
Browse files

Merge pull request #1043 from tuohai666/dev

for #891, support SHOW INDEX syntax
parents 31caa58f f95e6fb7
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -96,5 +96,7 @@ public enum MySQLKeyword implements Keyword {
    ROLLUP,
    RESTRICT,
    STRAIGHT_JOIN, 
    REGEXP
    REGEXP,
    INDEXES,
    KEYS
}
+11 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import io.shardingsphere.core.parsing.parser.clause.TableReferencesClauseParser;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowColumnsStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowCreateTableStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowDatabasesStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowIndexStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowOtherStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowTablesStatement;
import io.shardingsphere.core.parsing.parser.sql.dal.DALStatement;
@@ -82,6 +83,16 @@ public final class MySQLShowParser extends AbstractShowParser {
            tableReferencesClauseParser.parseSingleTableWithoutAlias(result);
            return result;
        }
        if (lexerEngine.skipIfEqual(DefaultKeyword.INDEX, MySQLKeyword.INDEXES, MySQLKeyword.KEYS)) {
            DALStatement result = new ShowIndexStatement();
            lexerEngine.skipIfEqual(DefaultKeyword.FROM, DefaultKeyword.IN);
            tableReferencesClauseParser.parseSingleTableWithoutAlias(result);
            if (lexerEngine.skipIfEqual(DefaultKeyword.FROM, DefaultKeyword.IN)) {
                int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length();
                result.getSqlTokens().add(new SchemaToken(beginPosition, lexerEngine.getCurrentToken().getLiterals(), result.getTables().getSingleTableName()));
            }
            return result;
        }
        return new ShowOtherStatement();
    }
}
+28 −0
Original line number Diff line number Diff line
/*
 * 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.parser.dialect.mysql.statement;

import io.shardingsphere.core.parsing.parser.sql.dal.DALStatement;

/**
 * Show columns statement.
 *
 * @author zhangyonglun
 */
public final class ShowIndexStatement extends DALStatement {
}
+68 −0
Original line number Diff line number Diff line
/*
 * 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.integrate.asserts.token;

import io.shardingsphere.core.parsing.integrate.asserts.SQLStatementAssertMessage;
import io.shardingsphere.core.parsing.integrate.jaxb.token.ExpectedSchemaToken;
import io.shardingsphere.core.parsing.integrate.jaxb.token.ExpectedTokens;
import io.shardingsphere.core.parsing.parser.token.SQLToken;
import io.shardingsphere.core.parsing.parser.token.SchemaToken;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
 * Schema token assert.
 *
 * @author zhangyonglun
 */
@RequiredArgsConstructor
final class SchemaTokenAssert {
    
    private final SQLStatementAssertMessage assertMessage;
    
    void assertSchemaTokens(final List<SQLToken> actual, final ExpectedTokens expected) {
        List<SchemaToken> schemaTokens = getSchemaTokens(actual);
        assertThat(assertMessage.getFullAssertMessage("Schema tokens size error: "), schemaTokens.size(), is(expected.getSchemaTokens().size()));
        int count = 0;
        for (ExpectedSchemaToken each : expected.getSchemaTokens()) {
            assertSchemaToken(schemaTokens.get(count), each);
            count++;
        }
    }
    
    private void assertSchemaToken(final SchemaToken actual, final ExpectedSchemaToken expected) {
        assertThat(assertMessage.getFullAssertMessage("Schema tokens begin position assertion error: "), actual.getBeginPosition(), is(expected.getBeginPosition()));
        assertThat(assertMessage.getFullAssertMessage("Schema tokens original literals assertion error: "), actual.getOriginalLiterals(), is(expected.getOriginalLiterals()));
        assertThat(assertMessage.getFullAssertMessage("Schema tokens table name assertion error: "), actual.getTableName(), is(expected.getTableName()));
    }
    
    private List<SchemaToken> getSchemaTokens(final List<SQLToken> actual) {
        List<SchemaToken> result = new ArrayList<>(actual.size());
        for (SQLToken each : actual) {
            if (each instanceof SchemaToken) {
                result.add((SchemaToken) each);
            }
        }
        return result;
    }
}
+4 −0
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@ public final class TokenAssert {
    
    private final TableTokenAssert tableTokenAssert;
    
    private final SchemaTokenAssert schemaTokenAssert;
    
    private final IndexTokenAssert indexTokenAssert;
    
    private final ItemsTokenAssert itemsTokenAssert;
@@ -49,6 +51,7 @@ public final class TokenAssert {
    
    public TokenAssert(final SQLCaseType sqlCaseType, final SQLStatementAssertMessage assertMessage) {
        tableTokenAssert = new TableTokenAssert(assertMessage);
        schemaTokenAssert = new SchemaTokenAssert(assertMessage);
        indexTokenAssert = new IndexTokenAssert(assertMessage);
        itemsTokenAssert = new ItemsTokenAssert(assertMessage);
        generatedKeyTokenAssert = new GeneratedKeyTokenAssert(sqlCaseType, assertMessage);
@@ -66,6 +69,7 @@ public final class TokenAssert {
     */
    public void assertTokens(final List<SQLToken> actual, final ExpectedTokens expected) {
        tableTokenAssert.assertTableTokens(actual, expected);
        schemaTokenAssert.assertSchemaTokens(actual, expected);
        indexTokenAssert.assertIndexToken(actual, expected);
        itemsTokenAssert.assertItemsToken(actual, expected);
        generatedKeyTokenAssert.assertGeneratedKeyToken(actual, expected);
Loading