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 93bf00e  For #11100, add custom properties for `add resource`; (#11101)
93bf00e is described below

commit 93bf00ea4f9e6e192c61bc71627134f156bb5065
Author: Raigor <[email protected]>
AuthorDate: Fri Jul 2 16:26:48 2021 +0800

    For #11100, add custom properties for `add resource`; (#11101)
    
    * Add properties for add resource;
    
    * fix connectionProperty syntax
    
    * add unit test.
---
 .../src/main/antlr4/imports/Keyword.g4             |  4 +++
 .../src/main/antlr4/imports/RDLStatement.g4        | 10 +++++-
 .../resource/ResourceDistSQLStatementVisitor.java  | 14 ++++++++-
 .../api/DistSQLStatementParserEngineTest.java      | 36 ++++++++++++++++++++++
 .../distsql/parser/segment/DataSourceSegment.java  |  4 +++
 .../rdl/impl/AddResourceBackendHandlerTest.java    |  3 +-
 .../AddResourcesStatementConverterTest.java        |  3 +-
 7 files changed, 70 insertions(+), 4 deletions(-)

diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/Keyword.g4
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/Keyword.g4
index 3f7c491..f009912 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/Keyword.g4
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/Keyword.g4
@@ -70,3 +70,7 @@ PASSWORD
 NAME
     : N A M E
     ;
+
+PROPERTIES
+    : P R O P E R T I E S
+    ;
diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
index f073e20..28f607f 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
@@ -28,7 +28,7 @@ dropResource
     ;
 
 dataSource
-    : dataSourceName LP HOST EQ hostName COMMA PORT EQ port COMMA DB EQ dbName 
COMMA USER EQ user (COMMA PASSWORD EQ password)? RP
+    : dataSourceName LP HOST EQ hostName COMMA PORT EQ port COMMA DB EQ dbName 
COMMA USER EQ user (COMMA PASSWORD EQ password)? (COMMA PROPERTIES LP 
connectionProperties? RP)? RP
     ;
 
 dataSourceName
@@ -58,3 +58,11 @@ user
 password
     : IDENTIFIER | INT | STRING
     ;
+
+connectionProperties
+    : connectionProperty (COMMA connectionProperty)*
+    ;
+
+connectionProperty
+    : key=IDENTIFIER EQ value=IDENTIFIER
+    ;
diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/resource/ResourceDistSQLStatementVisitor.java
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/resource/ResourceDistSQLStatementVisitor.java
index 9118aa3..60f54e1 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/resource/ResourceDistSQLStatementVisitor.java
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/resource/ResourceDistSQLStatementVisitor.java
@@ -24,6 +24,8 @@ import 
org.apache.shardingsphere.distsql.parser.autogen.ResourceStatementParser.
 import 
org.apache.shardingsphere.distsql.parser.autogen.ResourceStatementParser.DropResourceContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.ResourceStatementParser.SchemaNameContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.ResourceStatementParser.ShowResourcesContext;
+import 
org.apache.shardingsphere.distsql.parser.autogen.ResourceStatementParser.ConnectionPropertiesContext;
+import 
org.apache.shardingsphere.distsql.parser.autogen.ResourceStatementParser.ConnectionPropertyContext;
 import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.AddResourceStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.DropResourceStatement;
@@ -33,6 +35,7 @@ import 
org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.SchemaSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
 
+import java.util.Properties;
 import java.util.stream.Collectors;
 
 /**
@@ -48,7 +51,16 @@ public final class ResourceDistSQLStatementVisitor extends 
ResourceStatementBase
     @Override
     public ASTNode visitDataSource(final DataSourceContext ctx) {
         return new DataSourceSegment(
-                ctx.dataSourceName().getText(), ctx.hostName().getText(), 
ctx.port().getText(), ctx.dbName().getText(), ctx.user().getText(), null == 
ctx.password() ? "" : ctx.password().getText());
+                ctx.dataSourceName().getText(), ctx.hostName().getText(), 
ctx.port().getText(), ctx.dbName().getText(), ctx.user().getText(), null == 
ctx.password() ? "" : ctx.password().getText(),
+                null == ctx.connectionProperties() ? new Properties() : 
getConnectionProperties(ctx.connectionProperties()));
+    }
+    
+    private Properties getConnectionProperties(final 
ConnectionPropertiesContext ctx) {
+        Properties result = new Properties();
+        for (ConnectionPropertyContext each : ctx.connectionProperty()) {
+            result.setProperty(new 
IdentifierValue(each.key.getText()).getValue(), new 
IdentifierValue(each.value.getText()).getValue());
+        }
+        return result;
     }
     
     @Override
diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/test/java/org/apache/shardingsphere/distsql/parser/api/DistSQLStatementParserEngineTest.java
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/test/java/org/apache/shardingsphere/distsql/parser/api/DistSQLStatementParserEngineTest.java
index f05e644..41d51dc 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/test/java/org/apache/shardingsphere/distsql/parser/api/DistSQLStatementParserEngineTest.java
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/test/java/org/apache/shardingsphere/distsql/parser/api/DistSQLStatementParserEngineTest.java
@@ -41,6 +41,11 @@ public final class DistSQLStatementParserEngineTest {
     private static final String ADD_RESOURCE_MULTIPLE = "ADD RESOURCE 
ds_0(HOST=127.0.0.1,PORT=3306,DB=test0,USER=ROOT,PASSWORD=123456),"
             + 
"ds_1(HOST=127.0.0.1,PORT=3306,DB=test1,USER=ROOT,PASSWORD=123456);";
     
+    private static final String ADD_RESOURCE_SINGLE_WITH_EMPTY_PROPERTIES = 
"ADD RESOURCE ds_0(HOST=127.0.0.1,PORT=3306,DB=test0,USER=ROOT,PROPERTIES());";
+    
+    private static final String ADD_RESOURCE_SINGLE_WITH_PROPERTIES = "ADD 
RESOURCE 
ds_0(HOST=127.0.0.1,PORT=3306,DB=test0,USER=ROOT,PASSWORD=123456,PROPERTIES(" 
+            + "\"useSSL\"=\"false\",\"serverTimezone\"=\"UTC\"));";
+    
     private static final String DROP_RESOURCE = "DROP RESOURCE ds_0,ds_1";
     
     private final DistSQLStatementParserEngine engine = new 
DistSQLStatementParserEngine();
@@ -101,4 +106,35 @@ public final class DistSQLStatementParserEngineTest {
         assertThat(((DropResourceStatement) sqlStatement).getNames().size(), 
is(2));
         assertTrue(((DropResourceStatement) 
sqlStatement).getNames().containsAll(Arrays.asList("ds_0", "ds_1")));
     }
+    
+    @Test
+    public void assertParseAddSingleResourceWithEmptyProperties() {
+        SQLStatement sqlStatement = 
engine.parse(ADD_RESOURCE_SINGLE_WITH_EMPTY_PROPERTIES);
+        assertTrue(sqlStatement instanceof AddResourceStatement);
+        assertThat(((AddResourceStatement) 
sqlStatement).getDataSources().size(), is(1));
+        DataSourceSegment dataSourceSegment = ((AddResourceStatement) 
sqlStatement).getDataSources().iterator().next();
+        assertThat(dataSourceSegment.getName(), is("ds_0"));
+        assertThat(dataSourceSegment.getHostName(), is("127.0.0.1"));
+        assertThat(dataSourceSegment.getPort(), is("3306"));
+        assertThat(dataSourceSegment.getDb(), is("test0"));
+        assertThat(dataSourceSegment.getUser(), is("ROOT"));
+        assertThat(dataSourceSegment.getProperties().size(), is(0));
+    }
+    
+    @Test
+    public void assertParseAddSingleResourceWithProperties() {
+        SQLStatement sqlStatement = 
engine.parse(ADD_RESOURCE_SINGLE_WITH_PROPERTIES);
+        assertTrue(sqlStatement instanceof AddResourceStatement);
+        assertThat(((AddResourceStatement) 
sqlStatement).getDataSources().size(), is(1));
+        DataSourceSegment dataSourceSegment = ((AddResourceStatement) 
sqlStatement).getDataSources().iterator().next();
+        assertThat(dataSourceSegment.getName(), is("ds_0"));
+        assertThat(dataSourceSegment.getHostName(), is("127.0.0.1"));
+        assertThat(dataSourceSegment.getPort(), is("3306"));
+        assertThat(dataSourceSegment.getDb(), is("test0"));
+        assertThat(dataSourceSegment.getUser(), is("ROOT"));
+        assertThat(dataSourceSegment.getPassword(), is("123456"));
+        assertThat(dataSourceSegment.getProperties().size(), is(2));
+        assertThat(dataSourceSegment.getProperties().getProperty("useSSL"), 
is("false"));
+        
assertThat(dataSourceSegment.getProperties().getProperty("serverTimezone"), 
is("UTC"));
+    }
 }
diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/DataSourceSegment.java
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/DataSourceSegment.java
index d1edff2..6166c49 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/DataSourceSegment.java
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/DataSourceSegment.java
@@ -21,6 +21,8 @@ import lombok.Getter;
 import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
 
+import java.util.Properties;
+
 /**
  * Data source segment.
  */
@@ -39,4 +41,6 @@ public final class DataSourceSegment implements ASTNode {
     private final String user;
     
     private final String password;
+    
+    private final Properties properties;
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AddResourceBackendHandlerTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AddResourceBackendHandlerTest.java
index a83cfec..9758384 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AddResourceBackendHandlerTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AddResourceBackendHandlerTest.java
@@ -39,6 +39,7 @@ import org.mockito.junit.MockitoJUnitRunner;
 import java.lang.reflect.Field;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.Properties;
 
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
@@ -92,6 +93,6 @@ public final class AddResourceBackendHandlerTest {
     }
     
     private AddResourceStatement buildAddResourceStatement() {
-        return new AddResourceStatement(Collections.singleton(new 
DataSourceSegment("ds_0", "127.0.0.1", "test0", "3306", "root", "")));
+        return new AddResourceStatement(Collections.singleton(new 
DataSourceSegment("ds_0", "127.0.0.1", "test0", "3306", "root", "", new 
Properties())));
     }
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/converter/AddResourcesStatementConverterTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/converter/AddResourcesStatementConverterTest.java
index 1e8f2fa..0d36e39 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/converter/AddResourcesStatementConverterTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/converter/AddResourcesStatementConverterTest.java
@@ -27,6 +27,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.LinkedList;
 import java.util.Map;
+import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
@@ -45,7 +46,7 @@ public final class AddResourcesStatementConverterTest {
     private Collection<DataSourceSegment> createDataSourceSegments() {
         Collection<DataSourceSegment> result = new LinkedList<>();
         for (int i = 0; i < 2; i++) {
-            result.add(new DataSourceSegment(String.format("ds%s", i), 
"127.0.0.1", "3306", String.format("demo_ds_%s", i), String.format("root%s", 
i), String.format("root%s", i)));
+            result.add(new DataSourceSegment(String.format("ds%s", i), 
"127.0.0.1", "3306", String.format("demo_ds_%s", i), String.format("root%s", 
i), String.format("root%s", i), new Properties()));
         }
         return result;
     }

Reply via email to