ShardingCTLSetBackendHandler implements TextProtocolBackendHandler { @Override public BackendResponse execute() { Optional<ShardingCTLSetStatement> shardingTCLStatement = new ShardingCTLSetParser(sql).doParse(); if (!shardingTCLStatement.isPresent()) { throw new InvalidShardingCTLFormatException(sql); } if ("TRANSACTION_TYPE".equals(shardingTCLStatement.get().getKey())) { if (null == backendConnection.getSchemaName()) { throw new ShardingSphereException("Please select database, then switch transaction type."); } try { backendConnection.getTransactionStatus().setTransactionType(TransactionType.valueOf(shardingTCLStatement.get().getValue())); } catch (final IllegalArgumentException ex) { throw new UnsupportedShardingCTLTypeException(sql); } } else { throw new UnsupportedShardingCTLTypeException(sql); } return new UpdateResponse(); } ShardingCTLSetBackendHandler(final String sql, final BackendConnection backendConnection); @Override BackendResponse execute(); @Override boolean next(); @Override QueryData getQueryData();  }
@Test public void assertSwitchTransactionTypeXA() { backendConnection.setCurrentSchema(String.format(SCHEMA_PATTERN, 0)); ShardingCTLSetBackendHandler shardingCTLBackendHandler = new ShardingCTLSetBackendHandler("sctl:set transaction_type=XA", backendConnection); BackendResponse actual = shardingCTLBackendHandler.execute(); assertThat(actual, instanceOf(UpdateResponse.class)); assertThat(backendConnection.getTransactionStatus().getTransactionType(), is(TransactionType.XA)); }
@Test public void assertSwitchTransactionTypeBASE() { backendConnection.setCurrentSchema(String.format(SCHEMA_PATTERN, 0)); ShardingCTLSetBackendHandler shardingCTLBackendHandler = new ShardingCTLSetBackendHandler("sctl:set transaction_type=BASE", backendConnection); BackendResponse actual = shardingCTLBackendHandler.execute(); assertThat(actual, instanceOf(UpdateResponse.class)); assertThat(backendConnection.getTransactionStatus().getTransactionType(), is(TransactionType.BASE)); }
@Test public void assertSwitchTransactionTypeLOCAL() { backendConnection.setCurrentSchema(String.format(SCHEMA_PATTERN, 0)); ShardingCTLSetBackendHandler shardingCTLBackendHandler = new ShardingCTLSetBackendHandler("sctl:set transaction_type=LOCAL", backendConnection); BackendResponse actual = shardingCTLBackendHandler.execute(); assertThat(actual, instanceOf(UpdateResponse.class)); assertThat(backendConnection.getTransactionStatus().getTransactionType(), is(TransactionType.LOCAL)); }
@Test(expected = UnsupportedShardingCTLTypeException.class) public void assertSwitchTransactionTypeFailed() { backendConnection.setCurrentSchema(String.format(SCHEMA_PATTERN, 0)); new ShardingCTLSetBackendHandler("sctl:set transaction_type=XXX", backendConnection).execute(); }
@Test(expected = UnsupportedShardingCTLTypeException.class) public void assertNotSupportedSCTL() { new ShardingCTLSetBackendHandler("sctl:set @@session=XXX", backendConnection).execute(); }
@Test(expected = InvalidShardingCTLFormatException.class) public void assertFormatErrorSCTL() { new ShardingCTLSetBackendHandler("sctl:set yyyyy", backendConnection).execute(); }