Search in sources :

Example 1 with SQLParsingEngine

use of io.shardingjdbc.core.parsing.SQLParsingEngine in project sharding-jdbc by shardingjdbc.

the class ShowCreateTableMergedResult method init.

private Iterator<MemoryQueryResultRow> init(final List<QueryResult> queryResults) throws SQLException {
    List<MemoryQueryResultRow> result = new LinkedList<>();
    for (QueryResult each : queryResults) {
        while (each.next()) {
            MemoryQueryResultRow memoryResultSetRow = new MemoryQueryResultRow(each);
            String tableName = memoryResultSetRow.getCell(1).toString();
            Optional<TableRule> tableRule = shardingRule.tryFindTableRuleByActualTable(tableName);
            if (tableRule.isPresent()) {
                String logicTableName = tableRule.get().getLogicTable();
                memoryResultSetRow.setCell(1, logicTableName);
                String createTableDDL = memoryResultSetRow.getCell(2).toString();
                SQLParsingEngine sqlParsingEngine = new SQLParsingEngine(DatabaseType.MySQL, createTableDDL, shardingRule);
                String actualTableName = sqlParsingEngine.parse().getTables().getSingleTableName();
                if (actualTableName.startsWith("`")) {
                    logicTableName = "`" + logicTableName + "`";
                }
                memoryResultSetRow.setCell(2, createTableDDL.replaceFirst(actualTableName, logicTableName));
                result.add(memoryResultSetRow);
            }
        }
    }
    if (!result.isEmpty()) {
        setCurrentResultSetRow(result.get(0));
    }
    return result.iterator();
}
Also used : TableRule(io.shardingjdbc.core.rule.TableRule) QueryResult(io.shardingjdbc.core.merger.QueryResult) MemoryQueryResultRow(io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) LinkedList(java.util.LinkedList)

Example 2 with SQLParsingEngine

use of io.shardingjdbc.core.parsing.SQLParsingEngine in project sharding-jdbc by shardingjdbc.

the class ParsingSQLRouter method parse.

@Override
public SQLStatement parse(final String logicSQL) {
    SQLParsingEngine parsingEngine = new SQLParsingEngine(databaseType, logicSQL, shardingRule);
    SQLStatement result = parsingEngine.parse();
    if (result instanceof InsertStatement) {
        ((InsertStatement) result).appendGenerateKeyToken(shardingRule);
    }
    return result;
}
Also used : SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) SQLStatement(io.shardingjdbc.core.parsing.parser.sql.SQLStatement) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)

Example 3 with SQLParsingEngine

use of io.shardingjdbc.core.parsing.SQLParsingEngine in project sharding-jdbc by shardingjdbc.

the class ComStmtPreparePacket method execute.

@Override
public List<DatabaseProtocolPacket> execute() {
    log.debug("COM_STMT_PREPARE received for Sharding-Proxy: {}", sql);
    List<DatabaseProtocolPacket> result = new LinkedList<>();
    int currentSequenceId = 0;
    SQLStatement sqlStatement = new SQLParsingEngine(DatabaseType.MySQL, sql, ShardingRuleRegistry.getInstance().getShardingRule()).parse();
    result.add(new ComStmtPrepareOKPacket(++currentSequenceId, PreparedStatementRegistry.getInstance().register(sql), getNumColumns(sqlStatement), sqlStatement.getParametersIndex(), 0));
    for (int i = 0; i < sqlStatement.getParametersIndex(); i++) {
        // TODO add column name
        result.add(new ColumnDefinition41Packet(++currentSequenceId, ShardingConstant.LOGIC_SCHEMA_NAME, sqlStatement.getTables().isSingleTable() ? sqlStatement.getTables().getSingleTableName() : "", "", "", "", 100, ColumnType.MYSQL_TYPE_VARCHAR, 0));
    }
    if (sqlStatement.getParametersIndex() > 0) {
        result.add(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue()));
    }
    // TODO add If numColumns > 0
    return result;
}
Also used : ColumnDefinition41Packet(io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet) DatabaseProtocolPacket(io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) SQLStatement(io.shardingjdbc.core.parsing.parser.sql.SQLStatement) EofPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket) LinkedList(java.util.LinkedList)

Example 4 with SQLParsingEngine

use of io.shardingjdbc.core.parsing.SQLParsingEngine in project sharding-jdbc by shardingjdbc.

the class UpdateStatementParserTest method parseWithOr.

@Test(expected = SQLParsingUnsupportedException.class)
public void parseWithOr() {
    ShardingRule shardingRule = createShardingRule();
    new SQLParsingEngine(DatabaseType.Oracle, "UPDATE TABLE_XXX SET field1=1 WHERE field1<1 AND (field1 >2 OR field2 =1)", shardingRule).parse();
}
Also used : ShardingRule(io.shardingjdbc.core.rule.ShardingRule) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) Test(org.junit.Test)

Example 5 with SQLParsingEngine

use of io.shardingjdbc.core.parsing.SQLParsingEngine in project sharding-jdbc by shardingjdbc.

the class UpdateStatementParserTest method parseWithSpecialSyntax.

private void parseWithSpecialSyntax(final DatabaseType dbType, final String actualSQL) {
    ShardingRule shardingRule = createShardingRule();
    DMLStatement updateStatement = (DMLStatement) new SQLParsingEngine(dbType, actualSQL, shardingRule).parse();
    assertThat(updateStatement.getTables().find("TABLE_XXX").get().getName(), is("TABLE_XXX"));
    assertFalse(updateStatement.getTables().find("TABLE_XXX").get().getAlias().isPresent());
    Condition condition = updateStatement.getConditions().find(new Column("field1", "TABLE_XXX")).get();
    assertThat(condition.getOperator(), is(ShardingOperator.EQUAL));
    assertThat(((ListShardingValue<? extends Comparable>) condition.getShardingValue(Collections.emptyList())).getValues().iterator().next(), is((Object) 1));
}
Also used : Condition(io.shardingjdbc.core.parsing.parser.context.condition.Condition) Column(io.shardingjdbc.core.parsing.parser.context.condition.Column) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) DMLStatement(io.shardingjdbc.core.parsing.parser.sql.dml.DMLStatement) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine)

Aggregations

SQLParsingEngine (io.shardingjdbc.core.parsing.SQLParsingEngine)23 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)19 Test (org.junit.Test)17 DMLStatement (io.shardingjdbc.core.parsing.parser.sql.dml.DMLStatement)8 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)6 Column (io.shardingjdbc.core.parsing.parser.context.condition.Column)3 Condition (io.shardingjdbc.core.parsing.parser.context.condition.Condition)3 SQLStatement (io.shardingjdbc.core.parsing.parser.sql.SQLStatement)2 LinkedList (java.util.LinkedList)2 QueryResult (io.shardingjdbc.core.merger.QueryResult)1 MemoryQueryResultRow (io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow)1 SQLStatementAssert (io.shardingjdbc.core.parsing.integrate.asserts.SQLStatementAssert)1 TableRule (io.shardingjdbc.core.rule.TableRule)1 DatabaseProtocolPacket (io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket)1 ColumnDefinition41Packet (io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet)1 EofPacket (io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket)1