Search in sources :

Example 16 with SQLStatement

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

the class SQLExecuteBackendHandler method executeUpdate.

private List<DatabaseProtocolPacket> executeUpdate(final DataSource dataSource, final String sql, final SQLStatement sqlStatement) {
    try (Connection conn = dataSource.getConnection();
        Statement statement = conn.createStatement()) {
        int affectedRows;
        long lastInsertId = 0;
        if (sqlStatement instanceof InsertStatement) {
            affectedRows = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
            lastInsertId = getGeneratedKey(statement);
        } else {
            affectedRows = statement.executeUpdate(sql);
        }
        return Collections.<DatabaseProtocolPacket>singletonList(new OKPacket(1, affectedRows, lastInsertId, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, ""));
    } catch (final SQLException ex) {
        return Collections.<DatabaseProtocolPacket>singletonList(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage()));
    }
}
Also used : SQLException(java.sql.SQLException) SQLStatement(io.shardingjdbc.core.parsing.parser.sql.SQLStatement) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ErrPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket) DatabaseProtocolPacket(io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket) OKPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)

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