提交 064adfab 编写于 作者: T tristaZero

Avoid second sql parsing

上级 2e5789e7
......@@ -54,27 +54,27 @@ public final class DatabaseCommunicationEngineFactory {
/**
* Create new instance of text protocol backend handler.
*
* @param sqlStatement sql statement
* @param sql SQL to be executed
* @param backendConnection backend connection
* @return instance of text protocol backend handler
*/
public DatabaseCommunicationEngine newTextProtocolInstance(final String sql, final BackendConnection backendConnection) {
public DatabaseCommunicationEngine newTextProtocolInstance(final SQLStatement sqlStatement, final String sql, final BackendConnection backendConnection) {
SchemaContext schema = backendConnection.getSchema();
SQLStatement sqlStatement = schema.getRuntimeContext().getSqlParserEngine().parse(sql, false);
return new JDBCDatabaseCommunicationEngine(sql, backendConnection, createSQLExecuteEngine(schema, sqlStatement, backendConnection, new StatementExecutorWrapper(schema, sqlStatement)));
}
/**
* Create new instance of text protocol backend handler.
*
* @param sqlStatement sql statement
* @param sql SQL to be executed
* @param parameters SQL parameters
* @param backendConnection backend connection
* @return instance of text protocol backend handler
*/
public DatabaseCommunicationEngine newBinaryProtocolInstance(final String sql, final List<Object> parameters, final BackendConnection backendConnection) {
public DatabaseCommunicationEngine newBinaryProtocolInstance(final SQLStatement sqlStatement, final String sql, final List<Object> parameters, final BackendConnection backendConnection) {
SchemaContext schema = backendConnection.getSchema();
SQLStatement sqlStatement = schema.getRuntimeContext().getSqlParserEngine().parse(sql, true);
return new JDBCDatabaseCommunicationEngine(sql,
backendConnection, createSQLExecuteEngine(schema, sqlStatement, backendConnection, new PreparedStatementExecutorWrapper(schema, sqlStatement, parameters)));
}
......
......@@ -71,9 +71,9 @@ public final class TextProtocolBackendHandlerFactory {
return createTCLBackendHandler(sql, (TCLStatement) sqlStatement, backendConnection);
}
if (sqlStatement instanceof DALStatement) {
return createDALBackendHandler((DALStatement) sqlStatement, sql, backendConnection);
return createDALBackendHandler(sql, (DALStatement) sqlStatement, backendConnection);
}
return new QueryBackendHandler(sql, backendConnection);
return new QueryBackendHandler(sql, sqlStatement, backendConnection);
}
private static TextProtocolBackendHandler createTCLBackendHandler(final String sql, final TCLStatement tclStatement, final BackendConnection backendConnection) {
......@@ -92,10 +92,10 @@ public final class TextProtocolBackendHandlerFactory {
if (tclStatement instanceof RollbackStatement) {
return new TransactionBackendHandler(TransactionOperationType.ROLLBACK, backendConnection);
}
return new BroadcastBackendHandler(sql, backendConnection);
return new BroadcastBackendHandler(sql, tclStatement, backendConnection);
}
private static TextProtocolBackendHandler createDALBackendHandler(final DALStatement dalStatement, final String sql, final BackendConnection backendConnection) {
private static TextProtocolBackendHandler createDALBackendHandler(final String sql, final DALStatement dalStatement, final BackendConnection backendConnection) {
if (dalStatement instanceof UseStatement) {
return new UseDatabaseBackendHandler((UseStatement) dalStatement, backendConnection);
}
......@@ -104,8 +104,8 @@ public final class TextProtocolBackendHandlerFactory {
}
// FIXME: There are three SetStatement classes.
if (dalStatement instanceof SetStatement) {
return new BroadcastBackendHandler(sql, backendConnection);
return new BroadcastBackendHandler(sql, dalStatement, backendConnection);
}
return new UnicastBackendHandler(sql, backendConnection);
return new UnicastBackendHandler(sql, dalStatement, backendConnection);
}
}
......@@ -26,6 +26,7 @@ import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
import java.util.Collection;
import java.util.LinkedList;
......@@ -40,6 +41,8 @@ public final class BroadcastBackendHandler implements TextProtocolBackendHandler
private final String sql;
private final SQLStatement sqlStatement;
private final BackendConnection backendConnection;
@Override
......@@ -48,7 +51,7 @@ public final class BroadcastBackendHandler implements TextProtocolBackendHandler
String originalSchema = backendConnection.getSchema().getName();
for (String each : ProxySchemaContexts.getInstance().getSchemaNames()) {
backendConnection.setCurrentSchema(each);
responses.add(databaseCommunicationEngineFactory.newTextProtocolInstance(sql, backendConnection).execute());
responses.add(databaseCommunicationEngineFactory.newTextProtocolInstance(sqlStatement, sql, backendConnection).execute());
}
backendConnection.setCurrentSchema(originalSchema);
for (BackendResponse each : responses) {
......
......@@ -26,6 +26,7 @@ import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
import java.sql.SQLException;
......@@ -39,6 +40,8 @@ public final class UnicastBackendHandler implements TextProtocolBackendHandler {
private final String sql;
private final SQLStatement sqlStatement;
private final BackendConnection backendConnection;
private DatabaseCommunicationEngine databaseCommunicationEngine;
......@@ -49,7 +52,7 @@ public final class UnicastBackendHandler implements TextProtocolBackendHandler {
if (null == backendConnection.getSchema()) {
return new ErrorResponse(new NoDatabaseSelectedException());
}
databaseCommunicationEngine = databaseCommunicationEngineFactory.newTextProtocolInstance(sql, backendConnection);
databaseCommunicationEngine = databaseCommunicationEngineFactory.newTextProtocolInstance(sqlStatement, sql, backendConnection);
return databaseCommunicationEngine.execute();
}
......
......@@ -26,6 +26,7 @@ import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
import java.sql.SQLException;
......@@ -39,6 +40,8 @@ public final class QueryBackendHandler implements TextProtocolBackendHandler {
private final String sql;
private final SQLStatement sqlStatement;
private final BackendConnection backendConnection;
private DatabaseCommunicationEngine databaseCommunicationEngine;
......@@ -48,7 +51,7 @@ public final class QueryBackendHandler implements TextProtocolBackendHandler {
if (null == backendConnection.getSchema()) {
return new ErrorResponse(new NoDatabaseSelectedException());
}
databaseCommunicationEngine = databaseCommunicationEngineFactory.newTextProtocolInstance(sql, backendConnection);
databaseCommunicationEngine = databaseCommunicationEngineFactory.newTextProtocolInstance(sqlStatement, sql, backendConnection);
return databaseCommunicationEngine.execute();
}
......
......@@ -59,7 +59,8 @@ public final class DatabaseCommunicationEngineFactoryTest {
public void assertNewTextProtocolInstance() {
BackendConnection backendConnection = mock(BackendConnection.class);
when(backendConnection.getSchema()).thenReturn(schemaContext);
DatabaseCommunicationEngine engine = DatabaseCommunicationEngineFactory.getInstance().newTextProtocolInstance("schemaName", backendConnection);
DatabaseCommunicationEngine engine =
DatabaseCommunicationEngineFactory.getInstance().newTextProtocolInstance(mock(SQLStatement.class), "schemaName", backendConnection);
assertNotNull(engine);
assertThat(engine, instanceOf(JDBCDatabaseCommunicationEngine.class));
}
......@@ -68,7 +69,8 @@ public final class DatabaseCommunicationEngineFactoryTest {
public void assertNewBinaryProtocolInstance() {
BackendConnection backendConnection = mock(BackendConnection.class);
when(backendConnection.getSchema()).thenReturn(schemaContext);
DatabaseCommunicationEngine engine = DatabaseCommunicationEngineFactory.getInstance().newBinaryProtocolInstance("schemaName", Collections.emptyList(), backendConnection);
DatabaseCommunicationEngine engine =
DatabaseCommunicationEngineFactory.getInstance().newBinaryProtocolInstance(mock(SQLStatement.class), "schemaName", Collections.emptyList(), backendConnection);
assertNotNull(engine);
assertThat(engine, instanceOf(JDBCDatabaseCommunicationEngine.class));
}
......
......@@ -29,6 +29,7 @@ import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts;
import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -75,7 +76,7 @@ public final class BroadcastBackendHandlerTest {
@Test
public void assertExecuteSuccess() {
mockDatabaseCommunicationEngine(new UpdateResponse());
BroadcastBackendHandler broadcastBackendHandler = new BroadcastBackendHandler("SET timeout = 1000", backendConnection);
BroadcastBackendHandler broadcastBackendHandler = new BroadcastBackendHandler("SET timeout = 1000", mock(SQLStatement.class), backendConnection);
setBackendHandlerFactory(broadcastBackendHandler);
BackendResponse actual = broadcastBackendHandler.execute();
assertThat(actual, instanceOf(UpdateResponse.class));
......@@ -96,7 +97,7 @@ public final class BroadcastBackendHandlerTest {
public void assertExecuteFailure() {
ErrorResponse errorResponse = new ErrorResponse(new SQLException("no reason", "X999", -1));
mockDatabaseCommunicationEngine(errorResponse);
BroadcastBackendHandler broadcastBackendHandler = new BroadcastBackendHandler("SET timeout = 1000", backendConnection);
BroadcastBackendHandler broadcastBackendHandler = new BroadcastBackendHandler("SET timeout = 1000", mock(SQLStatement.class), backendConnection);
setBackendHandlerFactory(broadcastBackendHandler);
assertThat(broadcastBackendHandler.execute(), instanceOf(ErrorResponse.class));
verify(databaseCommunicationEngine, times(10)).execute();
......@@ -104,7 +105,7 @@ public final class BroadcastBackendHandlerTest {
private void mockDatabaseCommunicationEngine(final BackendResponse backendResponse) {
when(databaseCommunicationEngine.execute()).thenReturn(backendResponse);
when(databaseCommunicationEngineFactory.newTextProtocolInstance(anyString(), any())).thenReturn(databaseCommunicationEngine);
when(databaseCommunicationEngineFactory.newTextProtocolInstance(any(), anyString(), any())).thenReturn(databaseCommunicationEngine);
}
@SneakyThrows(ReflectiveOperationException.class)
......
......@@ -28,6 +28,7 @@ import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.Bac
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts;
import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.junit.Before;
import org.junit.Test;
......@@ -76,7 +77,7 @@ public final class UnicastBackendHandlerTest {
@Test
public void assertExecuteWhileSchemaIsNull() {
UnicastBackendHandler backendHandler = new UnicastBackendHandler("show variable like %s", backendConnection);
UnicastBackendHandler backendHandler = new UnicastBackendHandler("show variable like %s", mock(SQLStatement.class), backendConnection);
backendConnection.setCurrentSchema("schema_8");
setDatabaseCommunicationEngine(backendHandler);
BackendResponse actual = backendHandler.execute();
......@@ -87,7 +88,7 @@ public final class UnicastBackendHandlerTest {
@Test
public void assertExecuteWhileSchemaNotNull() {
backendConnection.setCurrentSchema("schema_0");
UnicastBackendHandler backendHandler = new UnicastBackendHandler("show variable like %s", backendConnection);
UnicastBackendHandler backendHandler = new UnicastBackendHandler("show variable like %s", mock(SQLStatement.class), backendConnection);
setDatabaseCommunicationEngine(backendHandler);
BackendResponse actual = backendHandler.execute();
assertThat(actual, instanceOf(UpdateResponse.class));
......@@ -97,7 +98,7 @@ public final class UnicastBackendHandlerTest {
private void setUnderlyingHandler(final BackendResponse backendResponse) {
DatabaseCommunicationEngine databaseCommunicationEngine = mock(DatabaseCommunicationEngine.class);
when(databaseCommunicationEngine.execute()).thenReturn(backendResponse);
when(databaseCommunicationEngineFactory.newTextProtocolInstance(anyString(), any())).thenReturn(databaseCommunicationEngine);
when(databaseCommunicationEngineFactory.newTextProtocolInstance(any(), anyString(), any())).thenReturn(databaseCommunicationEngine);
}
@SneakyThrows(ReflectiveOperationException.class)
......
......@@ -41,6 +41,7 @@ import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts;
import org.apache.shardingsphere.proxy.frontend.api.QueryCommandExecutor;
import org.apache.shardingsphere.proxy.frontend.mysql.MySQLErrPacketFactory;
import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
import java.sql.SQLException;
import java.util.ArrayList;
......@@ -67,8 +68,9 @@ public final class MySQLComStmtExecuteExecutor implements QueryCommandExecutor {
private int currentSequenceId;
public MySQLComStmtExecuteExecutor(final MySQLComStmtExecutePacket comStmtExecutePacket, final BackendConnection backendConnection) {
databaseCommunicationEngine =
DatabaseCommunicationEngineFactory.getInstance().newBinaryProtocolInstance(comStmtExecutePacket.getSql(), comStmtExecutePacket.getParameters(), backendConnection);
SQLStatement sqlStatement = backendConnection.getSchema().getRuntimeContext().getSqlParserEngine().parse(comStmtExecutePacket.getSql(), true);
databaseCommunicationEngine = DatabaseCommunicationEngineFactory.getInstance().newBinaryProtocolInstance(sqlStatement,
comStmtExecutePacket.getSql(), comStmtExecutePacket.getParameters(), backendConnection);
}
@Override
......
......@@ -29,6 +29,7 @@ import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.frontend.api.CommandExecutor;
import org.apache.shardingsphere.proxy.frontend.mysql.MySQLErrPacketFactory;
import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
import java.sql.SQLException;
import java.util.Collection;
......@@ -51,7 +52,9 @@ public final class MySQLComFieldListPacketExecutor implements CommandExecutor {
public MySQLComFieldListPacketExecutor(final MySQLComFieldListPacket packet, final BackendConnection backendConnection) {
this.packet = packet;
schemaName = backendConnection.getSchema().getName();
databaseCommunicationEngine = DatabaseCommunicationEngineFactory.getInstance().newTextProtocolInstance(getShowColumnsSQL(), backendConnection);
String sql = getShowColumnsSQL();
SQLStatement sqlStatement = backendConnection.getSchema().getRuntimeContext().getSqlParserEngine().parse(sql, false);
databaseCommunicationEngine = DatabaseCommunicationEngineFactory.getInstance().newTextProtocolInstance(sqlStatement, sql, backendConnection);
}
@Override
......
......@@ -42,6 +42,7 @@ import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts;
import org.apache.shardingsphere.proxy.frontend.api.QueryCommandExecutor;
import org.apache.shardingsphere.proxy.frontend.postgresql.PostgreSQLErrPacketFactory;
import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
......@@ -71,8 +72,13 @@ public final class PostgreSQLComBindExecutor implements QueryCommandExecutor {
public PostgreSQLComBindExecutor(final PostgreSQLComBindPacket packet, final BackendConnection backendConnection) {
this.packet = packet;
databaseCommunicationEngine = null == packet.getSql()
? null : DatabaseCommunicationEngineFactory.getInstance().newBinaryProtocolInstance(packet.getSql(), packet.getParameters(), backendConnection);
if (null != packet.getSql()) {
SQLStatement sqlStatement = backendConnection.getSchema().getRuntimeContext().getSqlParserEngine().parse(packet.getSql(), true);
databaseCommunicationEngine =
DatabaseCommunicationEngineFactory.getInstance().newBinaryProtocolInstance(sqlStatement, packet.getSql(), packet.getParameters(), backendConnection);
} else {
databaseCommunicationEngine = null;
}
}
@Override
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册