Search in sources :

Example 1 with SchemaToken

use of io.shardingjdbc.core.parsing.parser.token.SchemaToken in project sharding-jdbc by shardingjdbc.

the class SQLRewriteEngine method rewrite.

/**
 * rewrite SQL.
 *
 * @param isRewriteLimit is rewrite limit
 * @return SQL builder
 */
public SQLBuilder rewrite(final boolean isRewriteLimit) {
    SQLBuilder result = new SQLBuilder();
    if (sqlTokens.isEmpty()) {
        result.appendLiterals(originalSQL);
        return result;
    }
    int count = 0;
    sortByBeginPosition();
    for (SQLToken each : sqlTokens) {
        if (0 == count) {
            result.appendLiterals(originalSQL.substring(0, each.getBeginPosition()));
        }
        if (each instanceof TableToken) {
            appendTablePlaceholder(result, (TableToken) each, count, sqlTokens);
        } else if (each instanceof SchemaToken) {
            appendSchemaPlaceholder(result, (SchemaToken) each, count, sqlTokens);
        } else if (each instanceof IndexToken) {
            appendIndexPlaceholder(result, (IndexToken) each, count, sqlTokens);
        } else if (each instanceof ItemsToken) {
            appendItemsToken(result, (ItemsToken) each, count, sqlTokens);
        } else if (each instanceof RowCountToken) {
            appendLimitRowCount(result, (RowCountToken) each, count, sqlTokens, isRewriteLimit);
        } else if (each instanceof OffsetToken) {
            appendLimitOffsetToken(result, (OffsetToken) each, count, sqlTokens, isRewriteLimit);
        } else if (each instanceof OrderByToken) {
            appendOrderByToken(result, count, sqlTokens);
        }
        count++;
    }
    return result;
}
Also used : TableToken(io.shardingjdbc.core.parsing.parser.token.TableToken) SQLToken(io.shardingjdbc.core.parsing.parser.token.SQLToken) SchemaToken(io.shardingjdbc.core.parsing.parser.token.SchemaToken) IndexToken(io.shardingjdbc.core.parsing.parser.token.IndexToken) OffsetToken(io.shardingjdbc.core.parsing.parser.token.OffsetToken) ItemsToken(io.shardingjdbc.core.parsing.parser.token.ItemsToken) RowCountToken(io.shardingjdbc.core.parsing.parser.token.RowCountToken) OrderByToken(io.shardingjdbc.core.parsing.parser.token.OrderByToken)

Example 2 with SchemaToken

use of io.shardingjdbc.core.parsing.parser.token.SchemaToken in project sharding-jdbc by shardingjdbc.

the class MySQLShowParser method parse.

@Override
public DALStatement parse() {
    lexerEngine.nextToken();
    lexerEngine.skipIfEqual(DefaultKeyword.FULL);
    if (lexerEngine.equalAny(MySQLKeyword.DATABASES)) {
        return new ShowDatabasesStatement();
    }
    if (lexerEngine.skipIfEqual(MySQLKeyword.TABLES)) {
        DALStatement result = new ShowTablesStatement();
        if (lexerEngine.equalAny(DefaultKeyword.FROM, DefaultKeyword.IN)) {
            int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length();
            lexerEngine.nextToken();
            lexerEngine.nextToken();
            result.getSqlTokens().add(new RemoveToken(beginPosition, lexerEngine.getCurrentToken().getEndPosition()));
        }
        return result;
    }
    if (lexerEngine.skipIfEqual(MySQLKeyword.COLUMNS, MySQLKeyword.FIELDS)) {
        DALStatement result = new ShowColumnsStatement();
        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;
    }
    if (lexerEngine.skipIfEqual(DefaultKeyword.CREATE) && lexerEngine.skipIfEqual(DefaultKeyword.TABLE)) {
        DALStatement result = new ShowCreateTableStatement();
        tableReferencesClauseParser.parseSingleTableWithoutAlias(result);
        return result;
    }
    return new ShowOtherStatement();
}
Also used : ShowCreateTableStatement(io.shardingjdbc.core.parsing.parser.dialect.mysql.statement.ShowCreateTableStatement) ShowOtherStatement(io.shardingjdbc.core.parsing.parser.dialect.mysql.statement.ShowOtherStatement) ShowTablesStatement(io.shardingjdbc.core.parsing.parser.dialect.mysql.statement.ShowTablesStatement) ShowColumnsStatement(io.shardingjdbc.core.parsing.parser.dialect.mysql.statement.ShowColumnsStatement) ShowDatabasesStatement(io.shardingjdbc.core.parsing.parser.dialect.mysql.statement.ShowDatabasesStatement) DALStatement(io.shardingjdbc.core.parsing.parser.sql.dal.DALStatement) RemoveToken(io.shardingjdbc.core.parsing.parser.token.RemoveToken) SchemaToken(io.shardingjdbc.core.parsing.parser.token.SchemaToken)

Aggregations

SchemaToken (io.shardingjdbc.core.parsing.parser.token.SchemaToken)2 ShowColumnsStatement (io.shardingjdbc.core.parsing.parser.dialect.mysql.statement.ShowColumnsStatement)1 ShowCreateTableStatement (io.shardingjdbc.core.parsing.parser.dialect.mysql.statement.ShowCreateTableStatement)1 ShowDatabasesStatement (io.shardingjdbc.core.parsing.parser.dialect.mysql.statement.ShowDatabasesStatement)1 ShowOtherStatement (io.shardingjdbc.core.parsing.parser.dialect.mysql.statement.ShowOtherStatement)1 ShowTablesStatement (io.shardingjdbc.core.parsing.parser.dialect.mysql.statement.ShowTablesStatement)1 DALStatement (io.shardingjdbc.core.parsing.parser.sql.dal.DALStatement)1 IndexToken (io.shardingjdbc.core.parsing.parser.token.IndexToken)1 ItemsToken (io.shardingjdbc.core.parsing.parser.token.ItemsToken)1 OffsetToken (io.shardingjdbc.core.parsing.parser.token.OffsetToken)1 OrderByToken (io.shardingjdbc.core.parsing.parser.token.OrderByToken)1 RemoveToken (io.shardingjdbc.core.parsing.parser.token.RemoveToken)1 RowCountToken (io.shardingjdbc.core.parsing.parser.token.RowCountToken)1 SQLToken (io.shardingjdbc.core.parsing.parser.token.SQLToken)1 TableToken (io.shardingjdbc.core.parsing.parser.token.TableToken)1