This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new d5101bfe3d5 Implements show variable executor for PostgreSQL (#19837)
d5101bfe3d5 is described below

commit d5101bfe3d57e99e7131eb1c71fd81d45d7eae81
Author: 吴伟杰 <[email protected]>
AuthorDate: Wed Aug 3 21:03:29 2022 +0800

    Implements show variable executor for PostgreSQL (#19837)
    
    * Implements show variable executor for PostgreSQL
    
    * Complete PostgreSQLAdminExecutorCreatorTest
    
    * Remove unused PostgreSQLAdminExecutorFactoryTest
    
    * Remove unused SQLException
    
    * Add PostgreSQLShowVariableExecutorTest
    
    * Revise initial map size in PostgreSQLShowVariableExecutor
    
    * Add null check to UnicastDatabaseBackendHandler
---
 .../postgresql/PostgreSQLAdminExecutorCreator.java |   5 +
 .../postgresql/PostgreSQLShowVariableExecutor.java |  96 +++++++++++++++++
 .../data/impl/UnicastDatabaseBackendHandler.java   |   4 +-
 .../PostgreSQLAdminExecutorCreatorTest.java        |  13 ++-
 .../PostgreSQLAdminExecutorFactoryTest.java        | 114 ---------------------
 .../PostgreSQLShowVariableExecutorTest.java        |  80 +++++++++++++++
 6 files changed, 195 insertions(+), 117 deletions(-)

diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLAdminExecutorCreator.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLAdminExecutorCreator.java
index 77df8fc5cb9..7c6c7f54d09 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLAdminExecutorCreator.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLAdminExecutorCreator.java
@@ -30,6 +30,7 @@ import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.Tab
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dal.ResetParameterStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dal.SetStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dal.ShowStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
 
 import java.util.ArrayList;
@@ -57,6 +58,10 @@ public final class PostgreSQLAdminExecutorCreator implements 
DatabaseAdminExecut
     
     @Override
     public Optional<DatabaseAdminExecutor> create(final SQLStatementContext<?> 
sqlStatementContext) {
+        SQLStatement sqlStatement = sqlStatementContext.getSqlStatement();
+        if (sqlStatement instanceof ShowStatement) {
+            return Optional.of(new 
PostgreSQLShowVariableExecutor((ShowStatement) sqlStatement));
+        }
         return Optional.empty();
     }
     
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLShowVariableExecutor.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLShowVariableExecutor.java
new file mode 100644
index 00000000000..8d913ef8821
--- /dev/null
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLShowVariableExecutor.java
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.backend.handler.admin.postgresql;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
+import 
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResultMetaData;
+import 
org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.metadata.RawQueryResultColumnMetaData;
+import 
org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.metadata.RawQueryResultMetaData;
+import org.apache.shardingsphere.infra.merge.result.MergedResult;
+import 
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataMergedResult;
+import 
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
+import 
org.apache.shardingsphere.proxy.backend.handler.admin.executor.DatabaseAdminQueryExecutor;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dal.ShowStatement;
+
+import java.sql.Types;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Executor for PostgreSQL show statement.
+ */
+@RequiredArgsConstructor
+public final class PostgreSQLShowVariableExecutor implements 
DatabaseAdminQueryExecutor {
+    
+    private static final Map<String, VariableRowDataGenerator> 
VARIABLE_ROW_DATA_GENERATORS = new LinkedHashMap<>(7, 1);
+    
+    static {
+        VARIABLE_ROW_DATA_GENERATORS.put("application_name", connectionSession 
-> new String[]{"application_name", "PostgreSQL", "Sets the application name to 
be reported in statistics and logs."});
+        VARIABLE_ROW_DATA_GENERATORS.put("client_encoding", connectionSession 
-> new String[]{"client_encoding", "UTF8", "Sets the client's character set 
encoding."});
+        VARIABLE_ROW_DATA_GENERATORS.put("integer_datetimes", 
connectionSession -> new String[]{"integer_datetimes", "on", "Shows whether 
datetimes are integer based."});
+        VARIABLE_ROW_DATA_GENERATORS.put("timezone", connectionSession -> new 
String[]{"TimeZone", "Etc/UTC", "Sets the time zone for displaying and 
interpreting time stamps."});
+        VARIABLE_ROW_DATA_GENERATORS.put("transaction_isolation", 
connectionSession -> {
+            String result = null == connectionSession.getIsolationLevel() ? 
"read committed" : 
connectionSession.getIsolationLevel().getIsolationLevel().replace("-", " 
").toLowerCase(Locale.ROOT);
+            return new String[]{"transaction_isolation", result, "Sets the 
current transaction's isolation level"};
+        });
+        VARIABLE_ROW_DATA_GENERATORS.put("transaction_read_only",
+                connectionSession -> new String[]{"transaction_read_only", 
connectionSession.isReadOnly() ? "on" : "off", "Sets the current transaction's 
read-only status."});
+        VARIABLE_ROW_DATA_GENERATORS.put("server_version", connectionSession 
-> new String[]{"server_version", ShardingSphereVersion.VERSION, "Shows the 
server version."});
+    }
+    
+    private final ShowStatement showStatement;
+    
+    @Getter
+    private QueryResultMetaData queryResultMetaData;
+    
+    @Getter
+    private MergedResult mergedResult;
+    
+    @Override
+    public void execute(final ConnectionSession connectionSession) {
+        String name = showStatement.getName().toLowerCase(Locale.ROOT);
+        if ("ALL".equalsIgnoreCase(name)) {
+            executeShowAll(connectionSession);
+            return;
+        }
+        queryResultMetaData = new 
RawQueryResultMetaData(Collections.singletonList(new 
RawQueryResultColumnMetaData("", "", name, Types.VARCHAR, "VARCHAR", -1, 0)));
+        VariableRowDataGenerator variableRowDataGenerator = 
VARIABLE_ROW_DATA_GENERATORS.getOrDefault(name, unused -> new String[]{"", "", 
""});
+        mergedResult = new LocalDataMergedResult(Collections.singletonList(new 
LocalDataQueryResultRow(variableRowDataGenerator.getVariable(connectionSession)[1])));
+    }
+    
+    private void executeShowAll(final ConnectionSession connectionSession) {
+        queryResultMetaData = new RawQueryResultMetaData(Arrays.asList(
+                new RawQueryResultColumnMetaData("", "", "name", 
Types.VARCHAR, "VARCHAR", -1, 0),
+                new RawQueryResultColumnMetaData("", "", "setting", 
Types.VARCHAR, "VARCHAR", -1, 0),
+                new RawQueryResultColumnMetaData("", "", "description", 
Types.VARCHAR, "VARCHAR", -1, 0)));
+        mergedResult = new 
LocalDataMergedResult(VARIABLE_ROW_DATA_GENERATORS.values().stream().map(each 
-> new LocalDataQueryResultRow(each.getVariable(connectionSession)))
+                .collect(Collectors.toList()));
+    }
+    
+    private interface VariableRowDataGenerator {
+        
+        Object[] getVariable(ConnectionSession connectionSession);
+    }
+}
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/data/impl/UnicastDatabaseBackendHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/data/impl/UnicastDatabaseBackendHandler.java
index f666074c012..29e6691a28e 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/data/impl/UnicastDatabaseBackendHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/data/impl/UnicastDatabaseBackendHandler.java
@@ -103,6 +103,8 @@ public final class UnicastDatabaseBackendHandler implements 
DatabaseBackendHandl
     
     @Override
     public void close() throws SQLException {
-        databaseCommunicationEngine.close();
+        if (null != databaseCommunicationEngine) {
+            databaseCommunicationEngine.close();
+        }
     }
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLAdminExecutorCreatorTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLAdminExecutorCreatorTest.java
index 8c8143b5f9e..6db4fc8f981 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLAdminExecutorCreatorTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLAdminExecutorCreatorTest.java
@@ -31,7 +31,9 @@ import 
org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dal.PostgreSQLResetParameterStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dal.PostgreSQLSetStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dal.PostgreSQLShowStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dml.PostgreSQLDeleteStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dml.PostgreSQLInsertStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dml.PostgreSQLSelectStatement;
 import org.junit.Test;
 
@@ -64,8 +66,15 @@ public final class PostgreSQLAdminExecutorCreatorTest {
     private static final String SELECT_PG_CATALOG_WITH_SUBQUERY = "select * 
from (select * from pg_catalog.pg_namespace) t;";
     
     @Test
-    public void assertCreateWithSQLStatementContextOnly() {
-        assertThat(new PostgreSQLAdminExecutorCreator().create(null), 
is(Optional.empty()));
+    public void assertCreateWithOtherSQLStatementContextOnly() {
+        assertThat(new PostgreSQLAdminExecutorCreator().create(new 
CommonSQLStatementContext<>(new PostgreSQLInsertStatement())), 
is(Optional.empty()));
+    }
+    
+    @Test
+    public void assertCreateWithShowSQLStatement() {
+        Optional<DatabaseAdminExecutor> actual = new 
PostgreSQLAdminExecutorCreator().create(new CommonSQLStatementContext<>(new 
PostgreSQLShowStatement("client_encoding")));
+        assertTrue(actual.isPresent());
+        assertThat(actual.get(), 
instanceOf(PostgreSQLShowVariableExecutor.class));
     }
     
     @Test
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLAdminExecutorFactoryTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLAdminExecutorFactoryTest.java
deleted file mode 100644
index 80f26bff8bc..00000000000
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLAdminExecutorFactoryTest.java
+++ /dev/null
@@ -1,114 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.proxy.backend.handler.admin.postgresql;
-
-import 
org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
-import 
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
-import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
-import 
org.apache.shardingsphere.proxy.backend.handler.admin.executor.DatabaseAdminExecutor;
-import 
org.apache.shardingsphere.proxy.backend.handler.admin.postgresql.executor.SelectDatabaseExecutor;
-import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dal.VariableAssignSegment;
-import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dal.VariableSegment;
-import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
-import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
-import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dal.SetStatement;
-import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
-import 
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
-import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dal.PostgreSQLSetStatement;
-import org.junit.Test;
-
-import java.sql.SQLException;
-import java.util.Optional;
-
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verifyNoInteractions;
-import static org.mockito.Mockito.when;
-
-public final class PostgreSQLAdminExecutorFactoryTest {
-    
-    private final PostgreSQLAdminExecutorCreator 
postgreSQLAdminExecutorFactory = new PostgreSQLAdminExecutorCreator();
-    
-    @Test
-    public void assertNewInstanceWithPostgreSQLSelectPgDatabaseStatement() {
-        SelectStatement statement = mock(SelectStatement.class);
-        SimpleTableSegment tableSegment = mock(SimpleTableSegment.class);
-        when(tableSegment.getTableName()).thenReturn(new TableNameSegment(0, 
0, new IdentifierValue("pg_database")));
-        when(statement.getFrom()).thenReturn(tableSegment);
-        SelectStatementContext statementContext = 
mock(SelectStatementContext.class);
-        when(statementContext.getSqlStatement()).thenReturn(statement);
-        Optional<DatabaseAdminExecutor> executorOptional = 
postgreSQLAdminExecutorFactory.create(statementContext, "", null);
-        assertTrue(executorOptional.isPresent());
-        assertThat(executorOptional.get(), 
instanceOf(SelectDatabaseExecutor.class));
-    }
-    
-    @Test
-    public void assertNewInstanceWithSQLStatementOnly() {
-        assertFalse(postgreSQLAdminExecutorFactory.create(null).isPresent());
-    }
-    
-    @Test
-    public void assertNewInstanceWithUnknownStatement() {
-        
assertFalse(postgreSQLAdminExecutorFactory.create(mock(SQLStatementContext.class),
 null, null).isPresent());
-    }
-    
-    @Test
-    public void assertNewInstanceWithSetClientEncoding() {
-        SetStatement setStatement = createSetStatement("client_encoding");
-        CommonSQLStatementContext<SetStatement> statementContext = new 
CommonSQLStatementContext<>(setStatement);
-        Optional<DatabaseAdminExecutor> actual = 
postgreSQLAdminExecutorFactory.create(statementContext, null, null);
-        assertTrue(actual.isPresent());
-        assertThat(actual.get(), 
instanceOf(PostgreSQLSetVariableAdminExecutor.class));
-    }
-    
-    @Test
-    public void assertNewInstanceWithSetExtraFloatDigits() throws SQLException 
{
-        SetStatement setStatement = createSetStatement("extra_float_digits");
-        CommonSQLStatementContext<SetStatement> statementContext = new 
CommonSQLStatementContext<>(setStatement);
-        Optional<DatabaseAdminExecutor> actual = 
postgreSQLAdminExecutorFactory.create(statementContext, null, null);
-        assertTrue(actual.isPresent());
-        ConnectionSession connectionSession = mock(ConnectionSession.class);
-        actual.get().execute(connectionSession);
-        verifyNoInteractions(connectionSession);
-    }
-    
-    @Test
-    public void assertNewInstanceWithSetApplicationName() throws SQLException {
-        SetStatement setStatement = createSetStatement("application_name");
-        CommonSQLStatementContext<SetStatement> statementContext = new 
CommonSQLStatementContext<>(setStatement);
-        Optional<DatabaseAdminExecutor> actual = 
postgreSQLAdminExecutorFactory.create(statementContext, null, null);
-        assertTrue(actual.isPresent());
-        ConnectionSession connectionSession = mock(ConnectionSession.class);
-        actual.get().execute(connectionSession);
-        verifyNoInteractions(connectionSession);
-    }
-    
-    private SetStatement createSetStatement(final String 
configurationParameter) {
-        VariableSegment variableSegment = new VariableSegment();
-        variableSegment.setVariable(configurationParameter);
-        VariableAssignSegment variableAssignSegment = new 
VariableAssignSegment();
-        variableAssignSegment.setVariable(variableSegment);
-        SetStatement result = new PostgreSQLSetStatement();
-        result.getVariableAssigns().add(variableAssignSegment);
-        return result;
-    }
-}
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLShowVariableExecutorTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLShowVariableExecutorTest.java
new file mode 100644
index 00000000000..9816f6cf317
--- /dev/null
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/postgresql/PostgreSQLShowVariableExecutorTest.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.backend.handler.admin.postgresql;
+
+import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
+import 
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResultMetaData;
+import org.apache.shardingsphere.infra.merge.result.MergedResult;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dal.PostgreSQLShowStatement;
+import org.junit.Test;
+
+import java.sql.SQLException;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+public final class PostgreSQLShowVariableExecutorTest {
+    
+    @Test
+    public void assertExecuteShowAll() throws SQLException {
+        ConnectionSession connectionSession = mock(ConnectionSession.class);
+        PostgreSQLShowVariableExecutor executor = new 
PostgreSQLShowVariableExecutor(new PostgreSQLShowStatement("ALL"));
+        executor.execute(connectionSession);
+        QueryResultMetaData actualMetaData = executor.getQueryResultMetaData();
+        assertThat(actualMetaData.getColumnCount(), is(3));
+        assertThat(actualMetaData.getColumnLabel(1), is("name"));
+        assertThat(actualMetaData.getColumnLabel(2), is("setting"));
+        assertThat(actualMetaData.getColumnLabel(3), is("description"));
+        MergedResult actualResult = executor.getMergedResult();
+        Map<String, String> expected = new LinkedHashMap<>();
+        expected.put("application_name", "PostgreSQL");
+        expected.put("client_encoding", "UTF8");
+        expected.put("integer_datetimes", "on");
+        expected.put("TimeZone", "Etc/UTC");
+        expected.put("transaction_isolation", "read committed");
+        expected.put("transaction_read_only", "off");
+        expected.put("server_version", ShardingSphereVersion.VERSION);
+        for (Entry<String, String> entry : expected.entrySet()) {
+            assertTrue(actualResult.next());
+            assertThat(actualResult.getValue(1, String.class), 
is(entry.getKey()));
+            assertThat(actualResult.getValue(2, String.class), 
is(entry.getValue()));
+        }
+        assertFalse(actualResult.next());
+    }
+    
+    @Test
+    public void assertExecuteShowOne() throws SQLException {
+        ConnectionSession connectionSession = mock(ConnectionSession.class);
+        PostgreSQLShowVariableExecutor executor = new 
PostgreSQLShowVariableExecutor(new PostgreSQLShowStatement("client_encoding"));
+        executor.execute(connectionSession);
+        QueryResultMetaData actualMetaData = executor.getQueryResultMetaData();
+        assertThat(actualMetaData.getColumnCount(), is(1));
+        assertThat(actualMetaData.getColumnLabel(1), is("client_encoding"));
+        MergedResult actualResult = executor.getMergedResult();
+        assertTrue(actualResult.next());
+        assertThat(actualResult.getValue(1, String.class), is("UTF8"));
+        assertFalse(actualResult.next());
+    }
+}

Reply via email to