Skip to content

Commit

Permalink
ADD springboot-starter for SQL TRANSLATOR (#19882)
Browse files Browse the repository at this point in the history
* ADD springboot-starter for SQL TRANSLATOR

* Remove properties file name
  • Loading branch information
zhaojinchao95 committed Aug 5, 2022
1 parent 108f455 commit 0ede0bd
Show file tree
Hide file tree
Showing 14 changed files with 406 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
<artifactId>shardingsphere-parser-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-sql-translator-spring-boot-starter</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 @@ -75,10 +75,6 @@
{
"name": "spring.shardingsphere.schema.name",
"type": "org.apache.shardingsphere.spring.boot.ShardingSphereAutoConfiguration"
},
{
"name": "spring.shardingsphere.rules.sql-translator",
"type": "org.apache.shardingsphere.sqltranslator.api.config.SQLTranslatorRuleConfiguration"
}
],
"hints": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
<module>shardingsphere-sql-translator-api</module>
<module>shardingsphere-sql-translator-core</module>
<module>shardingsphere-sql-translator-provider</module>
<module>shardingsphere-sql-translator-spring</module>
</modules>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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-sql-translator</artifactId>
<version>5.1.3-SNAPSHOT</version>
</parent>
<artifactId>shardingsphere-sql-translator-spring</artifactId>
<packaging>pom</packaging>
<name>${project.artifactId}</name>

<modules>
<module>shardingsphere-sql-translator-spring-boot-starter</module>
</modules>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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-sql-translator-spring</artifactId>
<version>5.1.3-SNAPSHOT</version>
</parent>
<artifactId>shardingsphere-sql-translator-spring-boot-starter</artifactId>
<name>${project.artifactId}</name>

<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-sql-translator-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-spring-boot-starter-infra</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.sqltranslator.spring.boot;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.sqltranslator.spring.boot.condition.SQLTranslatorSpringBootCondition;
import org.apache.shardingsphere.sqltranslator.spring.boot.rule.YamlSQLTranslatorRuleSpringBootConfiguration;
import org.apache.shardingsphere.sqltranslator.yaml.config.YamlSQLTranslatorRuleConfiguration;
import org.apache.shardingsphere.sqltranslator.yaml.swapper.YamlSQLTranslatorRuleConfigurationSwapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

/**
* SQL translator rule configuration for spring boot.
*/
@Configuration
@EnableConfigurationProperties(YamlSQLTranslatorRuleSpringBootConfiguration.class)
@ConditionalOnClass(YamlSQLTranslatorRuleConfiguration.class)
@Conditional(SQLTranslatorSpringBootCondition.class)
@RequiredArgsConstructor
public class SQLTranslatorRuleSpringBootConfiguration {

private final YamlSQLTranslatorRuleConfigurationSwapper swapper = new YamlSQLTranslatorRuleConfigurationSwapper();

private final YamlSQLTranslatorRuleSpringBootConfiguration yamlConfig;

/**
* Create SQL translator rule configuration bean.
*
* @return SQL translator rule configuration
*/
@Bean
public RuleConfiguration sqlTranslatorRuleConfiguration() {
return swapper.swapToObject(yamlConfig.getSqlTranslator());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.sqltranslator.spring.boot.condition;

import org.apache.shardingsphere.spring.boot.util.PropertyUtil;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
* SQL translator spring boot condition.
*/
public final class SQLTranslatorSpringBootCondition extends SpringBootCondition {

private static final String SQL_TRANSLATOR_PREFIX = "spring.shardingsphere.rules.sql-translator";

@Override
public ConditionOutcome getMatchOutcome(final ConditionContext conditionContext, final AnnotatedTypeMetadata annotatedTypeMetadata) {
return PropertyUtil.containPropertyPrefix(conditionContext.getEnvironment(), SQL_TRANSLATOR_PREFIX)
? ConditionOutcome.match()
: ConditionOutcome.noMatch("Can't find ShardingSphere sql-translator rule configuration in local file.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.sqltranslator.spring.boot.rule;

import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.sqltranslator.yaml.config.YamlSQLTranslatorRuleConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* YAML SQL translator rule spring boot configuration.
*/
@ConfigurationProperties(prefix = "spring.shardingsphere.rules")
@Getter
@Setter
public final class YamlSQLTranslatorRuleSpringBootConfiguration {

private YamlSQLTranslatorRuleConfiguration sqlTranslator;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.
*/

{
"groups": [
{
"name": "spring.shardingsphere.rules.sql-translator",
"type": "org.apache.shardingsphere.sqltranslator.yaml.config.YamlSQLTranslatorRuleConfiguration",
"sourceType": "org.apache.shardingsphere.sqltranslator.spring.boot.rule.YamlSQLTranslatorRuleSpringBootConfiguration"
}
],
"properties": [
{
"name": "spring.shardingsphere.rules.sql-translator.type",
"type": "java.lang.String",
"sourceType": "org.apache.shardingsphere.sqltranslator.yaml.config.YamlSQLTranslatorRuleConfiguration"
},
{
"name": "spring.shardingsphere.rules.sql-translator.use-original-sql-when-translating-failed",
"type": "java.lang.Boolean",
"sourceType": "org.apache.shardingsphere.sqltranslator.yaml.config.YamlSQLTranslatorRuleConfiguration"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# 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.
#

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.apache.shardingsphere.sqltranslator.spring.boot.SQLTranslatorRuleSpringBootConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

provides: shardingsphere-sql-translator-spring-boot-starter
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.sqltranslator.spring.boot;

import org.apache.shardingsphere.sqltranslator.api.config.SQLTranslatorRuleConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SQLTranslatorSpringBootStarterTest.class)
@SpringBootApplication
public class SQLTranslatorSpringBootStarterTest {

@Resource
private SQLTranslatorRuleConfiguration sqlTranslatorRuleConfiguration;

@Test
public void assertSQLParserRule() {
assertThat(sqlTranslatorRuleConfiguration.getType(), is("MySQL"));
assertTrue(sqlTranslatorRuleConfiguration.isUseOriginalSQLWhenTranslatingFailed());
}
}

0 comments on commit 0ede0bd

Please sign in to comment.