You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2021/04/13 12:46:28 UTC

[shardingsphere] branch master updated: add combinational sql token and owner token (#10071)

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 57954ac  add combinational sql token and owner token (#10071)
57954ac is described below

commit 57954ac3824aff61ba758debb805c55baaaa764b
Author: huanghao495430759 <34...@users.noreply.github.com>
AuthorDate: Tue Apr 13 07:45:50 2021 -0500

    add combinational sql token and owner token (#10071)
    
    * add OwnerToken.
    
    * add CombinationalSQLToken.
    
    * add CombinationalSQLTokenTest and OwnerTokenTest.
    
    * fix OwnerTokenTest.
    
    Co-authored-by: huanghao <hu...@360shuke.com>
---
 .../token/pojo/generic/CombinationalSQLToken.java  | 60 ++++++++++++++++
 .../rewrite/sql/token/pojo/generic/OwnerToken.java | 79 ++++++++++++++++++++++
 .../pojo/generic/CombinationalSQLTokenTest.java    | 43 ++++++++++++
 .../sql/token/pojo/generic/OwnerTokenTest.java     | 75 ++++++++++++++++++++
 4 files changed, 257 insertions(+)

diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/pojo/generic/CombinationalSQLToken.java b/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/pojo/generic/CombinationalSQLToken.java
new file mode 100644
index 0000000..4929c2e
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/pojo/generic/CombinationalSQLToken.java
@@ -0,0 +1,60 @@
+/*
+ * 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.infra.rewrite.sql.token.pojo.generic;
+
+import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.SQLToken;
+import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.Substitutable;
+
+import java.util.Collection;
+import java.util.LinkedList;
+
+/**
+ * Combinational sql token.
+ */
+public final class CombinationalSQLToken extends SQLToken implements Substitutable {
+
+    private final Collection<SQLToken> sqlTokens = new LinkedList<>();
+
+    private final int stopIndex;
+
+    public CombinationalSQLToken(final int startIndex, final int stopIndex) {
+        super(startIndex);
+        this.stopIndex = stopIndex;
+    }
+
+    /**
+     * add sql token.
+     * @param sqlToken sql token
+     */
+    public void addSQLToken(final SQLToken sqlToken) {
+        this.sqlTokens.add(sqlToken);
+    }
+
+    /**
+     * get sql tokens.
+     * @return sql tokens
+     */
+    public Collection<SQLToken> getSQLTokens() {
+        return sqlTokens;
+    }
+
+    @Override
+    public int getStopIndex() {
+        return stopIndex;
+    }
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/pojo/generic/OwnerToken.java b/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/pojo/generic/OwnerToken.java
new file mode 100644
index 0000000..638c48e
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/pojo/generic/OwnerToken.java
@@ -0,0 +1,79 @@
+/*
+ * 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.infra.rewrite.sql.token.pojo.generic;
+
+import lombok.Getter;
+import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.RouteUnitAware;
+import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.SQLToken;
+import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.Substitutable;
+import org.apache.shardingsphere.infra.route.context.RouteUnit;
+import org.apache.shardingsphere.sql.parser.sql.common.constant.QuoteCharacter;
+
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Owner token.
+ */
+public final class OwnerToken extends SQLToken implements Substitutable, RouteUnitAware {
+
+    @Getter
+    private final int stopIndex;
+
+    private final String ownerName;
+
+    private final String tableName;
+
+    private final QuoteCharacter quoteCharacter;
+
+    public OwnerToken(final int startIndex, final int stopIndex, final String ownerName, final String tableName, final QuoteCharacter quoteCharacter) {
+        super(startIndex);
+        this.stopIndex = stopIndex;
+        this.ownerName = ownerName;
+        this.tableName = tableName;
+        this.quoteCharacter = quoteCharacter;
+    }
+
+    @Override
+    public String toString(final RouteUnit routeUnit) {
+        if (Objects.nonNull(ownerName) && tableName.equals(ownerName)) {
+            Set<String> actualTableNames = routeUnit.getActualTableNames(tableName);
+            String actualTableName = actualTableNames.isEmpty() ? tableName.toLowerCase() : actualTableNames.iterator().next();
+            return getQuoteCharacter().wrap(actualTableName) + ".";
+        }
+        return toString();
+    }
+
+    @Override
+    public String toString() {
+        return Objects.isNull(ownerName) ? "" : getQuoteCharacter().wrap(ownerName) + ".";
+    }
+
+    @Override
+    public int getStopIndex() {
+        return stopIndex;
+    }
+
+    /**
+     * get QuoteCharacter.
+     * @return column QuoteCharacter
+     */
+    public QuoteCharacter getQuoteCharacter() {
+        return Objects.nonNull(quoteCharacter) ? quoteCharacter : QuoteCharacter.NONE;
+    }
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/test/java/org/apache/shardingsphere/infra/rewrite/sql/token/pojo/generic/CombinationalSQLTokenTest.java b/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/test/java/org/apache/shardingsphere/infra/rewrite/sql/token/pojo/generic/CombinationalSQLTokenTest.java
new file mode 100644
index 0000000..b13e64a
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/test/java/org/apache/shardingsphere/infra/rewrite/sql/token/pojo/generic/CombinationalSQLTokenTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.infra.rewrite.sql.token.pojo.generic;
+
+import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.SQLToken;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class CombinationalSQLTokenTest {
+
+    @Test
+    public void assertCombinationalSQLToken() {
+        CombinationalSQLToken combinationalSQLToken = new CombinationalSQLToken(0, 1);
+        SQLToken sqlToken = mock(SQLToken.class);
+        when(sqlToken.getStartIndex()).thenReturn(2);
+        when(sqlToken.compareTo(any(SQLToken.class))).thenReturn(0);
+        combinationalSQLToken.addSQLToken(sqlToken);
+        assertThat(combinationalSQLToken.getStartIndex(), is(0));
+        assertThat(combinationalSQLToken.getStopIndex(), is(1));
+        assertThat(combinationalSQLToken.getSQLTokens().size(), is(1));
+        assertThat(combinationalSQLToken.getSQLTokens().iterator().next().compareTo(sqlToken), is(0));
+    }
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/test/java/org/apache/shardingsphere/infra/rewrite/sql/token/pojo/generic/OwnerTokenTest.java b/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/test/java/org/apache/shardingsphere/infra/rewrite/sql/token/pojo/generic/OwnerTokenTest.java
new file mode 100644
index 0000000..66289bf
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/test/java/org/apache/shardingsphere/infra/rewrite/sql/token/pojo/generic/OwnerTokenTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.infra.rewrite.sql.token.pojo.generic;
+
+import org.apache.shardingsphere.infra.route.context.RouteMapper;
+import org.apache.shardingsphere.infra.route.context.RouteUnit;
+import org.apache.shardingsphere.sql.parser.sql.common.constant.QuoteCharacter;
+import org.junit.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class OwnerTokenTest {
+
+    @Test
+    public void assertOwnerTokenWithOwnerNameEqualsTableName() {
+        OwnerToken ownerToken = new OwnerToken(0, 1, "t_user", "t_user", QuoteCharacter.NONE);
+        assertThat(ownerToken.toString(buildRouteUnit()), is("t_user_0."));
+        assertTokenGrid(ownerToken);
+    }
+
+    @Test
+    public void assertOwnerTokenWithOwnerNameNotEqualsTableName() {
+        OwnerToken ownerToken = new OwnerToken(0, 1, "u", "t_user", QuoteCharacter.NONE);
+        assertThat(ownerToken.toString(buildRouteUnit()), is("u."));
+        assertTokenGrid(ownerToken);
+    }
+
+    @Test
+    public void assertOwnerTokenWithNoRouteUnitAndOwnerNameEqualsTableName() {
+        OwnerToken ownerToken = new OwnerToken(0, 1, "t_user_detail", "t_user_detail", QuoteCharacter.NONE);
+        assertThat(ownerToken.toString(), is("t_user_detail."));
+        assertTokenGrid(ownerToken);
+    }
+
+    @Test
+    public void assertOwnerTokenWithNoRouteUnitAndOwnerNameNotEqualsTableName() {
+        OwnerToken ownerToken = new OwnerToken(0, 1, "ud", "t_user_detail", QuoteCharacter.NONE);
+        assertThat(ownerToken.toString(), is("ud."));
+        assertTokenGrid(ownerToken);
+    }
+
+    @Test
+    public void assertOwnerTokenWithNoRouteUnitAndOwnerNameIsEmpty() {
+        OwnerToken ownerToken = new OwnerToken(0, 1, null, "t_user_detail", QuoteCharacter.NONE);
+        assertThat(ownerToken.toString(), is(""));
+        assertTokenGrid(ownerToken);
+    }
+
+    private void assertTokenGrid(final OwnerToken ownerToken) {
+        assertThat(ownerToken.getStartIndex(), is(0));
+        assertThat(ownerToken.getStopIndex(), is(1));
+    }
+
+    private RouteUnit buildRouteUnit() {
+        return new RouteUnit(new RouteMapper("logic_db", "logic_db"), Collections.singletonList(new RouteMapper("t_user", "t_user_0")));
+    }
+}