Search in sources :

Example 1 with SQLStatement

use of io.shardingjdbc.core.parsing.parser.sql.SQLStatement 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 2 with SQLStatement

use of io.shardingjdbc.core.parsing.parser.sql.SQLStatement in project sharding-jdbc by shardingjdbc.

the class MasterSlaveStatement method executeUpdate.

@Override
public int executeUpdate(final String sql, final String[] columnNames) throws SQLException {
    int result = 0;
    SQLStatement sqlStatement = new SQLJudgeEngine(sql).judge();
    for (Connection each : connection.getConnections(sqlStatement.getType())) {
        Statement statement = each.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
        routedStatements.add(statement);
        result += statement.executeUpdate(sql, columnNames);
    }
    return result;
}
Also used : SQLJudgeEngine(io.shardingjdbc.core.parsing.SQLJudgeEngine) Statement(java.sql.Statement) SQLStatement(io.shardingjdbc.core.parsing.parser.sql.SQLStatement) Connection(java.sql.Connection) MasterSlaveConnection(io.shardingjdbc.core.jdbc.core.connection.MasterSlaveConnection) SQLStatement(io.shardingjdbc.core.parsing.parser.sql.SQLStatement)

Example 3 with SQLStatement

use of io.shardingjdbc.core.parsing.parser.sql.SQLStatement in project sharding-jdbc by shardingjdbc.

the class MasterSlaveStatement method execute.

@Override
public boolean execute(final String sql, final int[] columnIndexes) throws SQLException {
    boolean result = false;
    SQLStatement sqlStatement = new SQLJudgeEngine(sql).judge();
    for (Connection each : connection.getConnections(sqlStatement.getType())) {
        Statement statement = each.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
        routedStatements.add(statement);
        result = statement.execute(sql, columnIndexes);
    }
    return result;
}
Also used : SQLJudgeEngine(io.shardingjdbc.core.parsing.SQLJudgeEngine) Statement(java.sql.Statement) SQLStatement(io.shardingjdbc.core.parsing.parser.sql.SQLStatement) Connection(java.sql.Connection) MasterSlaveConnection(io.shardingjdbc.core.jdbc.core.connection.MasterSlaveConnection) SQLStatement(io.shardingjdbc.core.parsing.parser.sql.SQLStatement)

Example 4 with SQLStatement

use of io.shardingjdbc.core.parsing.parser.sql.SQLStatement 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 5 with SQLStatement

use of io.shardingjdbc.core.parsing.parser.sql.SQLStatement in project sharding-jdbc by shardingjdbc.

the class MergeEngineFactoryTest method assertNewInstanceWithOtherStatement.

@Test(expected = UnsupportedOperationException.class)
public void assertNewInstanceWithOtherStatement() throws SQLException {
    SQLStatement insertStatement = new InsertStatement();
    MergeEngineFactory.newInstance(null, queryResults, insertStatement);
}
Also used : SQLStatement(io.shardingjdbc.core.parsing.parser.sql.SQLStatement) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) Test(org.junit.Test)

Aggregations

SQLStatement (io.shardingjdbc.core.parsing.parser.sql.SQLStatement)16 Connection (java.sql.Connection)10 Statement (java.sql.Statement)10 MasterSlaveConnection (io.shardingjdbc.core.jdbc.core.connection.MasterSlaveConnection)9 SQLJudgeEngine (io.shardingjdbc.core.parsing.SQLJudgeEngine)9 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)3 Test (org.junit.Test)3 SQLParsingEngine (io.shardingjdbc.core.parsing.SQLParsingEngine)2 DatabaseProtocolPacket (io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket)2 DALMergeEngine (io.shardingjdbc.core.merger.dal.DALMergeEngine)1 DQLMergeEngine (io.shardingjdbc.core.merger.dql.DQLMergeEngine)1 LexerEngine (io.shardingjdbc.core.parsing.lexer.LexerEngine)1 DALStatement (io.shardingjdbc.core.parsing.parser.sql.dal.DALStatement)1 SelectStatement (io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement)1 ColumnDefinition41Packet (io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet)1 EofPacket (io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket)1 ErrPacket (io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket)1 OKPacket (io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket)1 SQLException (java.sql.SQLException)1 LinkedList (java.util.LinkedList)1