Commit ca3f02b4 authored by haocao's avatar haocao
Browse files

For #475 2th.

parent 8f06e81f
Loading
Loading
Loading
Loading
+19 −4
Original line number Diff line number Diff line
@@ -20,9 +20,12 @@ package io.shardingjdbc.core.parsing.parser.sql.ddl.create;
import io.shardingjdbc.core.parsing.lexer.LexerEngine;
import io.shardingjdbc.core.parsing.lexer.token.DefaultKeyword;
import io.shardingjdbc.core.parsing.lexer.token.Keyword;
import io.shardingjdbc.core.parsing.lexer.token.Token;
import io.shardingjdbc.core.parsing.parser.clause.TableReferencesClauseParser;
import io.shardingjdbc.core.parsing.parser.exception.SQLParsingException;
import io.shardingjdbc.core.parsing.parser.sql.SQLParser;
import io.shardingjdbc.core.parsing.parser.sql.ddl.DDLStatement;
import io.shardingjdbc.core.parsing.parser.token.IndexToken;
import io.shardingjdbc.core.rule.ShardingRule;
import lombok.AccessLevel;
import lombok.Getter;
@@ -52,18 +55,30 @@ public abstract class AbstractCreateParser implements SQLParser {
        lexerEngine.nextToken();
        lexerEngine.skipAll(getSkippedKeywordsBetweenCreateIndexAndKeyword());
        lexerEngine.skipAll(getSkippedKeywordsBetweenCreateAndKeyword());
        DDLStatement result = new DDLStatement();
        if (lexerEngine.equalAny(DefaultKeyword.INDEX)) {
            lexerEngine.skipUntil(DefaultKeyword.ON);
            parseIndex(result);
        } else if (lexerEngine.equalAny(DefaultKeyword.TABLE)) {
            lexerEngine.nextToken();
        } else {
            lexerEngine.unsupportedIfNotSkip(DefaultKeyword.TABLE);
            lexerEngine.skipAll(getSkippedKeywordsBetweenCreateTableAndTableName());
        } else {
            throw new SQLParsingException("Can't support other CREATE grammar unless CREATE TABLE, CREATE INDEX.");
        }
        DDLStatement result = new DDLStatement();
        tableReferencesClauseParser.parse(result, true);
        return result;
    }
    
    private void parseIndex(final DDLStatement ddlStatement) {
        lexerEngine.nextToken();
        Token currentToken = lexerEngine.getCurrentToken();
        int beginPosition = currentToken.getEndPosition() - currentToken.getLiterals().length();
        String literals = currentToken.getLiterals();
        lexerEngine.skipUntil(DefaultKeyword.ON);
        lexerEngine.nextToken();
        String tableName = lexerEngine.getCurrentToken().getLiterals();
        ddlStatement.getSqlTokens().add(new IndexToken(beginPosition, literals, tableName));
    }
    
    protected abstract Keyword[] getSkippedKeywordsBetweenCreateAndKeyword();
    
    protected abstract Keyword[] getSkippedKeywordsBetweenCreateTableAndTableName();
+59 −0
Original line number Diff line number Diff line
/*
 * Copyright 1999-2015 dangdang.com.
 * <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.shardingjdbc.core.parsing.parser.token;

import io.shardingjdbc.core.util.SQLUtil;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

/**
 * Index token.
 *
 * @author caohao
 */
@RequiredArgsConstructor
@ToString
public final class IndexToken implements SQLToken {
    
    @Getter
    private final int beginPosition;
    
    @Getter
    private final String originalLiterals;
    
    private final String tableName;
    
    /**
     * Get index name.
     * 
     * @return index name
     */
    public String getIndexName() {
        return SQLUtil.getExactlyValue(originalLiterals).toLowerCase();
    }
    
    /**
     * Get table name.
     *
     * @return table name
     */
    public String getTableName() {
        return SQLUtil.getExactlyValue(tableName).toLowerCase();
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -97,7 +97,7 @@ public abstract class AbstractShardingTableOnlyTest extends AbstractSQLAssertTes
    
    @Before
    public void initDDLTables() throws SQLException {
        if (getSql().startsWith("ALTER") || getSql().startsWith("TRUNCATE") || getSql().startsWith("DROP")) {
        if (getSql().startsWith("ALTER") || getSql().startsWith("TRUNCATE") || getSql().startsWith("DROP TABLE")) {
            if (getSql().contains("TEMP")) {
                executeSql("CREATE TEMPORARY TABLE t_temp_log(id int, status varchar(10))");
            } else {
@@ -108,7 +108,7 @@ public abstract class AbstractShardingTableOnlyTest extends AbstractSQLAssertTes
    
    @After
    public void cleanupDdlTables() throws SQLException {
        if (getSql().startsWith("CREATE") || getSql().startsWith("ALTER") || getSql().startsWith("TRUNCATE")) {
        if (getSql().startsWith("CREATE TABLE") || getSql().startsWith("ALTER") || getSql().startsWith("TRUNCATE")) {
            if (getSql().contains("TEMP")) {
                executeSql("DROP TABLE t_temp_log");
            } else {
+6 −0
Original line number Diff line number Diff line
@@ -50,6 +50,9 @@ public final class Assert {
    @XmlElement(name = "table-token")
    private List<TableToken> tableTokens;
    
    @XmlElement(name = "index-token")
    private IndexToken indexToken;
    
    @XmlElement(name = "items-token")
    private ItemsToken itemsToken;
    
@@ -88,6 +91,9 @@ public final class Assert {
        if (null != tableTokens) {
            result.addAll(tableTokens);
        }
        if (null != indexToken) {
            result.add(indexToken);
        }
        if (null != offsetToken) {
            result.add(offsetToken);
        }
+37 −0
Original line number Diff line number Diff line
/*
 * Copyright 1999-2015 dangdang.com.
 * <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.shardingjdbc.core.parsing.parser.jaxb;

import lombok.Getter;
import lombok.Setter;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;

@Getter
@Setter
@XmlAccessorType(XmlAccessType.FIELD)
public class IndexToken extends SQLToken {
    
    @XmlAttribute(name = "original-literals")
    private String originalLiterals;
    
    @XmlAttribute(name = "table-name")
    private String tableName;
}
Loading