Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into improve
Browse files Browse the repository at this point in the history
  • Loading branch information
azexcy committed Nov 20, 2023
2 parents 6b02ace + dc9d8be commit 3b6e23d
Show file tree
Hide file tree
Showing 194 changed files with 2,800 additions and 1,030 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ weight = 12
{{% tab name="语法" %}}
```sql
ExportDatabaseConfiguration ::=
'IMPORT' 'DATABASE' 'CONFIGURATION' 'FROM' 'FILE' filePath ('TO' databaseName)?

databaseName ::=
identifier
'IMPORT' 'DATABASE' 'CONFIGURATION' 'FROM' 'FILE' filePath

filePath ::=
string
Expand All @@ -38,7 +35,7 @@ filePath ::=
-`YAML` 中的配置导入到指定逻辑库中

```sql
IMPORT DATABASE CONFIGURATION FROM FILE "/xxx/config_sharding_db.yaml" TO sharding_db;
IMPORT DATABASE CONFIGURATION FROM FILE "/xxx/config_sharding_db.yaml";
```

-`YAML` 中的配置导入到当前逻辑库中
Expand All @@ -49,8 +46,8 @@ IMPORT DATABASE CONFIGURATION FROM FILE "/xxx/config_sharding_db.yaml";

### 保留字

`IMPORT``DATABASE``CONFIGURATION``FROM``FILE``TO`
`IMPORT``DATABASE``CONFIGURATION``FROM``FILE`

### 相关链接

- [保留字](/cn/user-manual/shardingsphere-proxy/distsql/syntax/reserved-word/)
- [保留字](/cn/user-manual/shardingsphere-proxy/distsql/syntax/reserved-word/)
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ The `IMPORT DATABASE CONFIGURATION` syntax is used to import `YAML` configuratio
{{% tab name="Grammar" %}}
```sql
ExportDatabaseConfiguration ::=
'IMPORT' 'DATABASE' 'CONFIGURATION' 'FROM' 'FILE' filePath ('TO' databaseName)?

databaseName ::=
identifier
'IMPORT' 'DATABASE' 'CONFIGURATION' 'FROM' 'FILE' filePath

filePath ::=
string
Expand All @@ -38,7 +35,7 @@ filePath ::=
- Import the configuration in `YAML` into the specified database

```sql
IMPORT DATABASE CONFIGURATION FROM FILE "/xxx/config_sharding_db.yaml" TO sharding_db;
IMPORT DATABASE CONFIGURATION FROM FILE "/xxx/config_sharding_db.yaml";
```

- Import the configuration in `YAML` into the current database
Expand All @@ -49,7 +46,7 @@ IMPORT DATABASE CONFIGURATION FROM FILE "/xxx/config_sharding_db.yaml";

### Reserved word

`IMPORT`, `DATABASE`, `CONFIGURATION`, `FROM`, `FILE`, `TO`
`IMPORT`, `DATABASE`, `CONFIGURATION`, `FROM`, `FILE`

### Related links

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void assertGetRowData() {
assertThat(row.getCell(4), is("user_assisted"));
assertThat(row.getCell(5), is("user_like"));
assertThat(row.getCell(6), is("md5"));
assertThat(row.getCell(7), is("{}"));
assertThat(row.getCell(7), is(""));
assertThat(row.getCell(8), is(""));
assertThat(row.getCell(9), is(""));
assertThat(row.getCell(10), is(""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void assertGetRowData() {
assertThat(row.getCell(1), is("t_mask"));
assertThat(row.getCell(2), is("user_id"));
assertThat(row.getCell(3), is("md5"));
assertThat(row.getCell(4), is("{}"));
assertThat(row.getCell(4), is(""));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ void assertGetRowData() {
assertThat(row.getCell(3), is("source"));
assertThat(row.getCell(4), is("shadow"));
assertThat(row.getCell(5), is("REGEX_MATCH"));
assertThat(row.getCell(6), is("{}"));
assertThat(row.getCell(6), is(""));
row = iterator.next();
assertThat(row.getCell(1), is("t_order_item"));
assertThat(row.getCell(2), is("shadow_rule"));
assertThat(row.getCell(3), is("source"));
assertThat(row.getCell(4), is("shadow"));
assertThat(row.getCell(5), is("REGEX_MATCH"));
assertThat(row.getCell(6), is("{}"));
assertThat(row.getCell(6), is(""));
}

@Test
Expand Down
10 changes: 10 additions & 0 deletions features/sharding/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@
<artifactId>shardingsphere-infra-expr-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-infra-key-generator-uuid</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-infra-key-generator-snowflake</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.shardingsphere</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import lombok.Setter;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContext;
import org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContextDecorator;
Expand All @@ -39,15 +40,16 @@ public final class ShardingSQLRewriteContextDecorator implements SQLRewriteConte

@Override
public void decorate(final ShardingRule shardingRule, final ConfigurationProperties props, final SQLRewriteContext sqlRewriteContext, final RouteContext routeContext) {
if (!containsShardingTable(shardingRule, sqlRewriteContext.getSqlStatementContext())) {
SQLStatementContext sqlStatementContext = sqlRewriteContext.getSqlStatementContext();
if (sqlStatementContext instanceof InsertStatementContext && !containsShardingTable(shardingRule, sqlStatementContext)) {
return;
}
if (!sqlRewriteContext.getParameters().isEmpty()) {
Collection<ParameterRewriter> parameterRewriters =
new ShardingParameterRewriterBuilder(shardingRule, routeContext, sqlRewriteContext.getDatabase().getSchemas(), sqlRewriteContext.getSqlStatementContext()).getParameterRewriters();
new ShardingParameterRewriterBuilder(shardingRule, routeContext, sqlRewriteContext.getDatabase().getSchemas(), sqlStatementContext).getParameterRewriters();
rewriteParameters(sqlRewriteContext, parameterRewriters);
}
sqlRewriteContext.addSQLTokenGenerators(new ShardingTokenGenerateBuilder(shardingRule, routeContext, sqlRewriteContext.getSqlStatementContext()).getSQLTokenGenerators());
sqlRewriteContext.addSQLTokenGenerators(new ShardingTokenGenerateBuilder(shardingRule, routeContext, sqlStatementContext).getSQLTokenGenerators());
}

private boolean containsShardingTable(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.apache.shardingsphere.infra.rule.identifier.type.TableNamesMapper;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.keygen.core.algorithm.KeyGenerateAlgorithm;
import org.apache.shardingsphere.keygen.core.exception.algorithm.GenerateKeyStrategyNotFoundException;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableReferenceRuleConfiguration;
Expand All @@ -48,11 +50,9 @@
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm;
import org.apache.shardingsphere.sharding.cache.ShardingCache;
import org.apache.shardingsphere.sharding.exception.algorithm.keygen.GenerateKeyStrategyNotFoundException;
import org.apache.shardingsphere.sharding.exception.algorithm.sharding.ShardingAlgorithmInitializationException;
import org.apache.shardingsphere.sharding.exception.metadata.InvalidBindingTablesException;
import org.apache.shardingsphere.sharding.exception.metadata.ShardingTableRuleNotFoundException;
import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import org.apache.shardingsphere.sharding.spi.ShardingAuditAlgorithm;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.shardingsphere.sharding.algorithm.keygen.fixture;

import lombok.Getter;
import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
import org.apache.shardingsphere.keygen.core.algorithm.KeyGenerateAlgorithm;

import java.util.concurrent.atomic.AtomicInteger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shardingsphere.sharding.rewrite.context;

import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContext;
Expand All @@ -26,6 +27,7 @@
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
Expand All @@ -43,4 +45,16 @@ void assertDecorate() {
new ShardingSQLRewriteContextDecorator().decorate(mock(ShardingRule.class), mock(ConfigurationProperties.class), sqlRewriteContext, mock(RouteContext.class));
assertTrue(sqlRewriteContext.getSqlTokens().isEmpty());
}

@Test
void assertDecorateWhenInsertStatementNotContainsShardingTable() {
SQLRewriteContext sqlRewriteContext = mock(SQLRewriteContext.class);
InsertStatementContext insertStatementContext = mock(InsertStatementContext.class, RETURNS_DEEP_STUBS);
when(insertStatementContext.getTablesContext().getTableNames()).thenReturn(Collections.singleton("t_order"));
when(sqlRewriteContext.getSqlStatementContext()).thenReturn(insertStatementContext);
ShardingRule shardingRule = mock(ShardingRule.class);
when(shardingRule.findTableRule("t_order")).thenReturn(Optional.empty());
new ShardingSQLRewriteContextDecorator().decorate(shardingRule, mock(ConfigurationProperties.class), sqlRewriteContext, mock(RouteContext.class));
assertTrue(sqlRewriteContext.getSqlTokens().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.keygen.core.exception.algorithm.GenerateKeyStrategyNotFoundException;
import org.apache.shardingsphere.keygen.snowflake.algorithm.SnowflakeKeyGenerateAlgorithm;
import org.apache.shardingsphere.keygen.uuid.algorithm.UUIDKeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.algorithm.audit.DMLShardingConditionsShardingAuditAlgorithm;
import org.apache.shardingsphere.sharding.algorithm.keygen.SnowflakeKeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.algorithm.keygen.UUIDKeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableReferenceRuleConfiguration;
Expand All @@ -42,7 +43,6 @@
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.exception.algorithm.keygen.GenerateKeyStrategyNotFoundException;
import org.apache.shardingsphere.sharding.exception.algorithm.sharding.ShardingAlgorithmInitializationException;
import org.apache.shardingsphere.sharding.exception.metadata.InvalidBindingTablesException;
import org.apache.shardingsphere.sharding.exception.metadata.ShardingTableRuleNotFoundException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.keygen.core.algorithm.KeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableReferenceRuleConfiguration;
Expand All @@ -56,7 +57,6 @@
import org.apache.shardingsphere.sharding.exception.strategy.InvalidShardingStrategyConfigurationException;
import org.apache.shardingsphere.sharding.rule.BindingTableCheckedConfiguration;
import org.apache.shardingsphere.sharding.rule.TableRule;
import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import org.apache.shardingsphere.sharding.spi.ShardingAuditAlgorithm;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.shardingsphere.sharding.distsql.fixture.keygen;

import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
import org.apache.shardingsphere.keygen.core.algorithm.KeyGenerateAlgorithm;

public final class DistSQLKeyGenerateAlgorithmFixture implements KeyGenerateAlgorithm {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void assertGetRowData() {
assertThat(row.getCell(11), is("{\"algorithm-expression\":\"t_order_${order_id % 2}\"}"));
assertThat(row.getCell(12), is("order_id"));
assertThat(row.getCell(13), is("SNOWFLAKE"));
assertThat(row.getCell(14), is("{}"));
assertThat(row.getCell(14), is(""));
assertThat(row.getCell(15), is("DML_SHARDING_CONDITIONS"));
assertThat(row.getCell(16), is("true"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void assertGetRowData() {
LocalDataQueryResultRow row = iterator.next();
assertThat(row.getCell(1), is("uuid_key_generator"));
assertThat(row.getCell(2), is("UUID"));
assertThat(row.getCell(3), is("{}"));
assertThat(row.getCell(3), is(""));
}

@Test
Expand Down
43 changes: 43 additions & 0 deletions infra/algorithm/key-generator/core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-infra-key-generator</artifactId>
<version>5.4.2-SNAPSHOT</version>
</parent>
<artifactId>shardingsphere-infra-key-generator-core</artifactId>
<name>${project.artifactId}</name>

<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-infra-common</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-test-util</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sharding.spi;
package org.apache.shardingsphere.keygen.core.algorithm;

import org.apache.shardingsphere.infra.algorithm.ShardingSphereAlgorithm;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.keygen.core.exception;

import org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.SQLState;
import org.apache.shardingsphere.infra.exception.core.external.sql.type.feature.FeatureSQLException;

/**
* Key generate SQL exception.
*/
public abstract class KeyGenerateSQLException extends FeatureSQLException {

private static final int FEATURE_CODE = 11;

private static final long serialVersionUID = 3124409584064186239L;

protected KeyGenerateSQLException(final SQLState sqlState, final int errorCode, final String reason, final Object... messageArgs) {
super(sqlState, FEATURE_CODE, errorCode, reason, messageArgs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sharding.exception.algorithm.keygen;
package org.apache.shardingsphere.keygen.core.exception.algorithm;

import org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState;
import org.apache.shardingsphere.sharding.exception.ShardingSQLException;
import org.apache.shardingsphere.keygen.core.exception.KeyGenerateSQLException;

/**
* Generate key strategy not found exception.
*/
public final class GenerateKeyStrategyNotFoundException extends ShardingSQLException {
public final class GenerateKeyStrategyNotFoundException extends KeyGenerateSQLException {

private static final long serialVersionUID = 7456922260524630374L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
* limitations under the License.
*/

package org.apache.shardingsphere.sharding.exception.algorithm.keygen;
package org.apache.shardingsphere.keygen.core.exception.algorithm;

import org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState;
import org.apache.shardingsphere.sharding.exception.ShardingSQLException;
import org.apache.shardingsphere.keygen.core.exception.KeyGenerateSQLException;

/**
* Key generate algorithm initialization exception.
*/
public final class KeyGenerateAlgorithmInitializationException extends ShardingSQLException {
public final class KeyGenerateAlgorithmInitializationException extends KeyGenerateSQLException {

private static final long serialVersionUID = -9046956561006694072L;
private static final long serialVersionUID = 4137100879778822323L;

public KeyGenerateAlgorithmInitializationException(final String keyGenerateType, final String reason) {
super(XOpenSQLState.GENERAL_ERROR, 91, "Key generate algorithm `%s` initialization failed, reason is: %s.", keyGenerateType, reason);
Expand Down
Loading

0 comments on commit 3b6e23d

Please sign in to comment.