Skip to content

Commit

Permalink
for #1205, refactor SQLExecutionEvent, use SQLExecutionUnit instead o…
Browse files Browse the repository at this point in the history
…f dataSource & sqlUnit
  • Loading branch information
terrymanu committed Sep 1, 2018
1 parent 73896c7 commit 1636e7b
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package io.shardingsphere.core.event.executor.sql;

import io.shardingsphere.core.routing.SQLUnit;
import io.shardingsphere.core.routing.SQLExecutionUnit;

import java.util.List;

Expand All @@ -29,7 +29,7 @@
*/
public final class DMLExecutionEvent extends SQLExecutionEvent {

public DMLExecutionEvent(final String dataSource, final SQLUnit sqlUnit, final List<Object> parameters) {
super(dataSource, sqlUnit, parameters);
public DMLExecutionEvent(final SQLExecutionUnit sqlExecutionUnit, final List<Object> parameters) {
super(sqlExecutionUnit, parameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package io.shardingsphere.core.event.executor.sql;

import io.shardingsphere.core.routing.SQLUnit;
import io.shardingsphere.core.routing.SQLExecutionUnit;

import java.util.List;

Expand All @@ -29,7 +29,7 @@
*/
public final class DQLExecutionEvent extends SQLExecutionEvent {

public DQLExecutionEvent(final String dataSource, final SQLUnit sqlUnit, final List<Object> parameters) {
super(dataSource, sqlUnit, parameters);
public DQLExecutionEvent(final SQLExecutionUnit sqlExecutionUnit, final List<Object> parameters) {
super(sqlExecutionUnit, parameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package io.shardingsphere.core.event.executor.sql;

import io.shardingsphere.core.event.ShardingEvent;
import io.shardingsphere.core.routing.SQLUnit;
import io.shardingsphere.core.routing.SQLExecutionUnit;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

Expand All @@ -34,9 +34,7 @@
@Getter
public class SQLExecutionEvent extends ShardingEvent {

private final String dataSource;

private final SQLUnit sqlUnit;
private final SQLExecutionUnit sqlExecutionUnit;

private final List<Object> parameters;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public final class SQLExecutionEventFactory {
*/
public static SQLExecutionEvent createEvent(final SQLType sqlType, final StatementExecuteUnit executeUnit, final List<Object> parameters) {
if (SQLType.DQL == sqlType) {
return new DQLExecutionEvent(executeUnit.getSqlExecutionUnit().getDataSource(), executeUnit.getSqlExecutionUnit().getSqlUnit(), parameters);
return new DQLExecutionEvent(executeUnit.getSqlExecutionUnit(), parameters);
}
if (SQLType.DML == sqlType) {
return new DMLExecutionEvent(executeUnit.getSqlExecutionUnit().getDataSource(), executeUnit.getSqlExecutionUnit().getSqlUnit(), parameters);
return new DMLExecutionEvent(executeUnit.getSqlExecutionUnit(), parameters);
}
return new SQLExecutionEvent(executeUnit.getSqlExecutionUnit().getDataSource(), executeUnit.getSqlExecutionUnit().getSqlUnit(), parameters);
return new SQLExecutionEvent(executeUnit.getSqlExecutionUnit(), parameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public final class SQLExecutePrepareTemplate {
*
* @param sqlExecutionUnits units execution SQL units
* @param callback SQL execute prepare callback
* @return key is data source name, value is statement execute unit groups
* @return statement execute unit groups
* @throws SQLException SQL exception
*/
public Collection<ShardingExecuteGroup<StatementExecuteUnit>> getStatementExecuteUnitGroups(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void listen(final DMLExecutionEvent event) {
case BEFORE_EXECUTE:
//TODO for batch SQL need split to 2-level records
transactionLogStorage.add(new TransactionLog(event.getId(), bedSoftTransaction.getTransactionId(), bedSoftTransaction.getTransactionType(),
event.getDataSource(), event.getSqlUnit().getSql(), event.getParameters(), System.currentTimeMillis(), 0));
event.getSqlExecutionUnit().getDataSource(), event.getSqlExecutionUnit().getSqlUnit().getSql(), event.getParameters(), System.currentTimeMillis(), 0));
return;
case EXECUTE_SUCCESS:
transactionLogStorage.remove(event.getId());
Expand All @@ -76,13 +76,13 @@ public void listen(final DMLExecutionEvent event) {
Connection conn = null;
PreparedStatement preparedStatement = null;
try {
conn = bedSoftTransaction.getConnection().getConnection(event.getDataSource());
conn = bedSoftTransaction.getConnection().getConnection(event.getSqlExecutionUnit().getDataSource());
if (!isValidConnection(conn)) {
bedSoftTransaction.getConnection().release(conn);
conn = bedSoftTransaction.getConnection().getConnection(event.getDataSource());
conn = bedSoftTransaction.getConnection().getConnection(event.getSqlExecutionUnit().getDataSource());
isNewConnection = true;
}
preparedStatement = conn.prepareStatement(event.getSqlUnit().getSql());
preparedStatement = conn.prepareStatement(event.getSqlExecutionUnit().getSqlUnit().getSql());
//TODO for batch event need split to 2-level records
for (int parameterIndex = 0; parameterIndex < event.getParameters().size(); parameterIndex++) {
preparedStatement.setObject(parameterIndex + 1, event.getParameters().get(parameterIndex));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public final class ExecutorTestUtil {
* @param event SQL execution event
*/
public static void listen(final EventCaller eventCaller, final SQLExecutionEvent event) {
eventCaller.verifyDataSource(event.getDataSource());
eventCaller.verifySQL(event.getSqlUnit().getSql());
eventCaller.verifyDataSource(event.getSqlExecutionUnit().getDataSource());
eventCaller.verifySQL(event.getSqlExecutionUnit().getSqlUnit().getSql());
eventCaller.verifyParameters(event.getParameters());
eventCaller.verifyEventExecutionType(event.getEventType());
if (ShardingEventType.EXECUTE_FAILURE == event.getEventType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ protected void beforeExecute(final SQLExecutionEvent event) {
}
if (null == branchSpan.get()) {
branchSpan.set(ShardingTracer.get().buildSpan(OPERATION_NAME_PREFIX).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
.withTag(Tags.PEER_HOSTNAME.getKey(), event.getDataSource()).withTag(Tags.COMPONENT.getKey(), ShardingTags.COMPONENT_NAME)
.withTag(Tags.DB_INSTANCE.getKey(), event.getDataSource()).withTag(Tags.DB_TYPE.getKey(), "sql")
.withTag(Tags.PEER_HOSTNAME.getKey(), event.getSqlExecutionUnit().getDataSource()).withTag(Tags.COMPONENT.getKey(), ShardingTags.COMPONENT_NAME)
.withTag(Tags.DB_INSTANCE.getKey(), event.getSqlExecutionUnit().getDataSource()).withTag(Tags.DB_TYPE.getKey(), "sql")
.withTag(ShardingTags.DB_BIND_VARIABLES.getKey(), event.getParameters().isEmpty() ? "" : Joiner.on(",").join(event.getParameters()))
.withTag(Tags.DB_STATEMENT.getKey(), event.getSqlUnit().getSql()).startManual());
.withTag(Tags.DB_STATEMENT.getKey(), event.getSqlExecutionUnit().getSqlUnit().getSql()).startManual());
}
}

Expand Down

0 comments on commit 1636e7b

Please sign in to comment.