use of io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement 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);
}
use of io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement in project sharding-jdbc by shardingjdbc.
the class ParsingSQLRouter method route.
@Override
public SQLRouteResult route(final String logicSQL, final List<Object> parameters, final SQLStatement sqlStatement) {
SQLRouteResult result = new SQLRouteResult(sqlStatement);
if (sqlStatement instanceof InsertStatement && null != ((InsertStatement) sqlStatement).getGeneratedKey()) {
processGeneratedKey(parameters, (InsertStatement) sqlStatement, result);
}
RoutingResult routingResult = route(parameters, sqlStatement);
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, logicSQL, databaseType, sqlStatement);
boolean isSingleRouting = routingResult.isSingleRouting();
if (sqlStatement instanceof SelectStatement && null != ((SelectStatement) sqlStatement).getLimit()) {
processLimit(parameters, (SelectStatement) sqlStatement, isSingleRouting);
}
SQLBuilder sqlBuilder = rewriteEngine.rewrite(!isSingleRouting);
if (routingResult instanceof CartesianRoutingResult) {
for (CartesianDataSource cartesianDataSource : ((CartesianRoutingResult) routingResult).getRoutingDataSources()) {
for (CartesianTableReference cartesianTableReference : cartesianDataSource.getRoutingTableReferences()) {
result.getExecutionUnits().add(new SQLExecutionUnit(cartesianDataSource.getDataSource(), rewriteEngine.generateSQL(cartesianTableReference, sqlBuilder)));
}
}
} else {
for (TableUnit each : routingResult.getTableUnits().getTableUnits()) {
result.getExecutionUnits().add(new SQLExecutionUnit(each.getDataSourceName(), rewriteEngine.generateSQL(each, sqlBuilder)));
}
}
if (showSQL) {
SQLLogger.logSQL(logicSQL, sqlStatement, result.getExecutionUnits(), parameters);
}
return result;
}
use of io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement in project sharding-jdbc by shardingjdbc.
the class InsertStatementParserTest method assertParseWithGenerateKeyColumnsWithoutParameter.
@Test
public void assertParseWithGenerateKeyColumnsWithoutParameter() {
ShardingRule shardingRule = createShardingRuleWithGenerateKeyColumns();
SQLParsingEngine statementParser = new SQLParsingEngine(DatabaseType.MySQL, "INSERT INTO `TABLE_XXX` (`field1`) VALUES (10)", shardingRule);
InsertStatement insertStatement = (InsertStatement) statementParser.parse();
assertInsertStatementWithoutParameter(insertStatement);
}
use of io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement 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()));
}
}
Aggregations