schemas) {
59 | this.schemas = schemas;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/ddal-ddr/src/main/java/org/hellojavaer/ddal/ddr/utils/DDRStringUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016-2017 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.hellojavaer.ddal.ddr.utils;
17 |
18 | /**
19 | *
20 | * @author Kaiming Zou,created on 19/11/2016.
21 | */
22 | public class DDRStringUtils {
23 |
24 | public static String trimToNull(String str) {
25 | if (str == null) {
26 | return null;
27 | } else {
28 | str = str.trim();
29 | if (str.length() == 0) {
30 | return null;
31 | } else {
32 | return str;
33 | }
34 | }
35 | }
36 |
37 | public static String toLowerCase(String str) {
38 | if (str == null) {
39 | return null;
40 | } else {
41 | str = str.trim();
42 | if (str.length() == 0) {
43 | return null;
44 | } else {
45 | return str.toLowerCase();
46 | }
47 | }
48 | }
49 |
50 | public static boolean equals(String s0, String s1) {
51 | if (s0 == null && s1 == null) {
52 | return true;
53 | } else if (s0 != null) {
54 | return s0.equals(s1);
55 | } else if (s1 != null) {
56 | return s1.equals(s0);
57 | } else {
58 | return false;
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/ddal-server/ddal-server-datasource/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | 4.0.0
7 |
8 | org.hellojavaer.ddal
9 | ddal-server-datasource
10 | 1.0.0-SNAPSHOT
11 |
12 | ddal-server-datasource
13 | https://github.com/hellojavaer/ddal
14 |
15 |
16 |
17 | hellojavaer
18 | Kaiming Zou
19 | hellojavaer@gmail.com
20 |
21 | owner
22 | developer
23 |
24 | 8
25 |
26 |
27 |
28 | https://github.com/hellojavaer/ddal.git
29 | scm:git:https://github.com/hellojavaer/ddal.git
30 |
31 |
32 |
33 | Apache 2
34 | http://www.apache.org/licenses/LICENSE-2.0.txt
35 | repo
36 | A business-friendly OSS license
37 |
38 |
39 |
40 |
41 |
42 |
43 | org.apache.maven.plugins
44 | maven-deploy-plugin
45 | 2.8.2
46 |
47 | true
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/ddal-example/ddal-example-example1/src/main/java/org/hellojavaer/ddal/example/example1/dao/impl/RoleDaoImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2017-2017 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.hellojavaer.ddal.example.example1.dao.impl;
17 |
18 | import org.hellojavaer.ddal.example.example1.dao.RoleDao;
19 | import org.hellojavaer.ddal.example.example1.entitry.RoleEntity;
20 | import org.mybatis.spring.SqlSessionTemplate;
21 | import org.springframework.beans.factory.annotation.Autowired;
22 | import org.springframework.stereotype.Repository;
23 |
24 | /**
25 | *
26 | * @author Kaiming Zou,created on 22/07/2017.
27 | */
28 | @Repository
29 | public class RoleDaoImpl implements RoleDao {
30 |
31 | @Autowired
32 | private SqlSessionTemplate sqlSessionTemplate;
33 |
34 | @Override
35 | public Long add(RoleEntity roleEntity) {
36 | sqlSessionTemplate.insert("role.add", roleEntity);
37 | return roleEntity.getId();
38 | }
39 |
40 | @Override
41 | public int deleteById(Long id) {
42 | return sqlSessionTemplate.delete("role.deleteById", id);
43 | }
44 |
45 | @Override
46 | public int updateById(RoleEntity roleEntity) {
47 | return sqlSessionTemplate.update("role.updateById", roleEntity);
48 | }
49 |
50 | @Override
51 | public RoleEntity getById(Long id) {
52 | return sqlSessionTemplate.selectOne("role.getById", id);
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/ddal-ddr/src/main/java/org/hellojavaer/ddal/ddr/shard/ShardRouteConfig.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016-2017 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.hellojavaer.ddal.ddr.shard;
17 |
18 | import org.hellojavaer.ddal.ddr.utils.DDRToStringBuilder;
19 |
20 | /**
21 | *
22 | * @author Kaiming Zou,created on 20/12/2016.
23 | */
24 | public class ShardRouteConfig {
25 |
26 | private String scName;
27 | private String tbName;
28 | private String sdKey;
29 |
30 | public ShardRouteConfig() {
31 | }
32 |
33 | public ShardRouteConfig(String scName, String tbName, String sdKey) {
34 | this.scName = scName;
35 | this.tbName = tbName;
36 | this.sdKey = sdKey;
37 | }
38 |
39 | public String getScName() {
40 | return scName;
41 | }
42 |
43 | public void setScName(String scName) {
44 | this.scName = scName;
45 | }
46 |
47 | public String getTbName() {
48 | return tbName;
49 | }
50 |
51 | public void setTbName(String tbName) {
52 | this.tbName = tbName;
53 | }
54 |
55 | public String getSdKey() {
56 | return sdKey;
57 | }
58 |
59 | public void setSdKey(String sdKey) {
60 | this.sdKey = sdKey;
61 | }
62 |
63 | @Override
64 | public String toString() {
65 | return new DDRToStringBuilder().append("scName", scName).append("tbName", tbName).append("sdKey", sdKey).toString();
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/ddal-spring/src/test/java/org/hellojavaer/ddal/spring/scan/dbcluster/DBClusterRouteTestComponent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018-2018 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.hellojavaer.ddal.spring.scan.dbcluster;
17 |
18 | import org.hellojavaer.ddal.core.utils.Assert;
19 | import org.hellojavaer.ddal.ddr.cluster.DBClusterRoute;
20 | import org.hellojavaer.ddal.ddr.cluster.DBClusterRouteContext;
21 | import org.hellojavaer.ddal.spring.scan.UserEntity;
22 | import org.springframework.stereotype.Component;
23 |
24 | /**
25 | *
26 | * @author Kaiming Zou,created on 2018/5/28.
27 | */
28 | @Component
29 | public class DBClusterRouteTestComponent {
30 |
31 | @DBClusterRoute(clusterName = "-name-")
32 | public void test00() {
33 | Assert.equals(DBClusterRouteContext.getClusterName(), "-name-");
34 | }
35 |
36 | @DBClusterRoute(clusterName = "-{$0}-")
37 | public void test01(String str) {
38 | Assert.equals(DBClusterRouteContext.getClusterName(), "-" + str + "-");
39 | }
40 |
41 | @DBClusterRoute(clusterName = "{$0}")
42 | public void test02(int id) {
43 | Assert.equals(DBClusterRouteContext.getClusterName(), id + "");
44 | }
45 |
46 | @DBClusterRoute(clusterName = "tb_{format('%04d',$0.id)}")
47 | public void testFunction(UserEntity userEntity) {
48 | Assert.equals(DBClusterRouteContext.getClusterName(), (String.format("tb_%04d", userEntity.getId())));
49 |
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/ddal-ddr/src/main/java/org/hellojavaer/ddal/ddr/shard/ShardRoute.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016-2017 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.hellojavaer.ddal.ddr.shard;
17 |
18 | import java.lang.annotation.*;
19 |
20 | /**
21 | * Setting the default route information for the schema
22 | *
23 | * This configuration is fully equivalent to invoking #{org.hellojavaer.ddal.ddr.shard.ShardRouteContext.setRouteInfo},
24 | * and this default route configuration will take effect only in the following case:
25 | * 1. No 'sdKey' was specified in route rule configuration
26 | * 2. Specified route rule in configuration but no 'sdKey' was found in sql and no associated route information was specified by #{org.hellojavaer.ddal.ddr.shard.ShardRouteContext.setRouteInfo}
27 | *
28 | * 'ShardRoute' is designed base on 'one schema one route rule'.
29 | *
30 | * But if you need to access one more schemas which have different route rule in one sql, you should add this annotation on its sub-invoking method.
31 | *
32 | *
33 | * @author Kaiming Zou,created on 01/01/2017.
34 | */
35 | @Target({ ElementType.METHOD })
36 | @Retention(RetentionPolicy.RUNTIME)
37 | @Documented
38 | public @interface ShardRoute {
39 |
40 | /**
41 | * allow a comma-delimited list of scNames
42 | *
43 | * @return
44 | */
45 | String scName() default "";
46 |
47 | /**
48 | *
49 | * @return
50 | */
51 | String sdValue() default "";
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/ddal-spring/src/test/java/org/hellojavaer/ddal/spring/scan/ShardRouteTestComponent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016-2017 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.hellojavaer.ddal.spring.scan;
17 |
18 | import org.hellojavaer.ddal.core.utils.Assert;
19 | import org.hellojavaer.ddal.ddr.shard.ShardRoute;
20 | import org.hellojavaer.ddal.ddr.shard.ShardRouteContext;
21 | import org.springframework.stereotype.Component;
22 |
23 | /**
24 | *
25 | * @author Kaiming Zou,created on 22/06/2017.
26 | */
27 | @Component
28 | public class ShardRouteTestComponent {
29 |
30 | @ShardRoute(scName = "user,shop", sdValue = "{$0}")
31 | public void routeWithId(Long id) {
32 | Assert.isTrue(ShardRouteContext.getRouteInfo("user") == id);
33 | Assert.isTrue(ShardRouteContext.getRouteInfo("shop") == id);
34 | }
35 |
36 | @ShardRoute(scName = "user,shop", sdValue = "{$0.id}")
37 | public void routeWithEntity(UserEntity userEntity) {
38 | Assert.isTrue(ShardRouteContext.getRouteInfo("user") == userEntity.getId());
39 | Assert.isTrue(ShardRouteContext.getRouteInfo("shop") == userEntity.getId());
40 | }
41 |
42 | @ShardRoute(scName = "user,shop", sdValue = "tb_{format('%04d',$0.id)}")
43 | public void testFunction(UserEntity userEntity) {
44 | Assert.isTrue(ShardRouteContext.getRouteInfo("user").equals(String.format("tb_%04d", userEntity.getId())));
45 | Assert.isTrue(ShardRouteContext.getRouteInfo("shop").equals(String.format("tb_%04d", userEntity.getId())));
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/ddal-ddr/src/main/java/org/hellojavaer/ddal/ddr/expression/range/Token.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2017-2017 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.hellojavaer.ddal.ddr.expression.range;
17 |
18 | /**
19 | *
20 | * @author Kaiming Zou,created on 02/08/2017.
21 | */
22 | class Token {
23 |
24 | private TokenKind kind;
25 |
26 | private String data;
27 |
28 | private int startPos;
29 |
30 | private int endPos;
31 |
32 | public Token(TokenKind tokenKind, int startPos, int endPos) {
33 | this.kind = tokenKind;
34 | this.startPos = startPos;
35 | this.endPos = endPos;
36 | }
37 |
38 | public Token(TokenKind tokenKind, String data, int startPos, int endPos) {
39 | this(tokenKind, startPos, endPos);
40 | this.data = data;
41 | }
42 |
43 | public TokenKind getKind() {
44 | return this.kind;
45 | }
46 |
47 | public String getData() {
48 | return data;
49 | }
50 |
51 | @Override
52 | public String toString() {
53 | StringBuilder s = new StringBuilder();
54 | s.append("[").append(this.kind.toString());
55 | if (this.data != null) {
56 | s.append(":").append(this.data);
57 | }
58 | s.append("]");
59 | s.append("(").append(this.startPos).append(",").append(this.endPos).append(")");
60 | return s.toString();
61 | }
62 |
63 | public int getStartPos() {
64 | return startPos;
65 | }
66 |
67 | public int getEndPos() {
68 | return endPos;
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/ddal-spring/src/test/java/org/hellojavaer/ddal/spring/scan/EnableShardRouteAnnotationTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016-2017 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.hellojavaer.ddal.spring.scan;
17 |
18 | import org.junit.Test;
19 | import org.junit.runner.RunWith;
20 | import org.springframework.beans.factory.annotation.Autowired;
21 | import org.springframework.test.context.ContextConfiguration;
22 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
23 |
24 | /**
25 | *
26 | * @author Kaiming Zou,created on 21/06/2017.
27 | */
28 | @RunWith(SpringJUnit4ClassRunner.class)
29 | @ContextConfiguration(locations = { "classpath:/spring.xml" })
30 | public class EnableShardRouteAnnotationTest {
31 |
32 | @Autowired
33 | private ShardRouteTestComponent shardRouteTestComponent;
34 |
35 | @Test
36 | public void test01() {
37 | for (int i = 0; i < 100; i++) {
38 | shardRouteTestComponent.routeWithId((long) i);
39 | }
40 | }
41 |
42 | @Test
43 | public void test02() {
44 | for (long i = 0; i < 100; i++) {
45 | UserEntity userEntity = new UserEntity();
46 | userEntity.setId(i);
47 | shardRouteTestComponent.routeWithEntity(userEntity);
48 | }
49 | }
50 |
51 | @Test
52 | public void testFormat() {
53 | for (long i = 0; i < 100; i++) {
54 | UserEntity userEntity = new UserEntity();
55 | userEntity.setId(i);
56 | shardRouteTestComponent.testFunction(userEntity);
57 | }
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/ddal-ddr/src/main/java/org/hellojavaer/ddal/ddr/shard/RangeShardValue.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2017-2017 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.hellojavaer.ddal.ddr.shard;
17 |
18 | import java.util.Objects;
19 |
20 | /**
21 | *
22 | * @author Kaiming Zou,created on 16/09/2017.
23 | */
24 | public class RangeShardValue {
25 |
26 | private Long begin;
27 | private Long end;
28 |
29 | public RangeShardValue() {
30 | }
31 |
32 | public RangeShardValue(Long begin, Long end) {
33 | this.begin = begin;
34 | this.end = end;
35 | }
36 |
37 | public Long getBegin() {
38 | return begin;
39 | }
40 |
41 | public void setBegin(Long begin) {
42 | this.begin = begin;
43 | }
44 |
45 | public Long getEnd() {
46 | return end;
47 | }
48 |
49 | public void setEnd(Long end) {
50 | this.end = end;
51 | }
52 |
53 | @Override
54 | public String toString() {
55 | return "{" + "begin:" + begin + ",end:" + end + '}';
56 | }
57 |
58 | @Override
59 | public int hashCode() {
60 | return Objects.hash(begin, end);
61 | }
62 |
63 | @Override
64 | public boolean equals(Object o) {
65 | if (this == o) return true;
66 | if (o == null || getClass() != o.getClass()) return false;
67 |
68 | RangeShardValue that = (RangeShardValue) o;
69 |
70 | if (begin != null ? !begin.equals(that.begin) : that.begin != null) return false;
71 | return end != null ? end.equals(that.end) : that.end == null;
72 |
73 | }
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/ddal-ddr/src/main/java/org/hellojavaer/ddal/ddr/datasource/jdbc/init/UninitializedStatementProcessor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016-2017 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.hellojavaer.ddal.ddr.datasource.jdbc.init;
17 |
18 | import org.hellojavaer.ddal.ddr.datasource.jdbc.property.StatementProperty;
19 |
20 | import java.util.HashMap;
21 | import java.util.Map;
22 |
23 | /**
24 | *
25 | * @author Kaiming Zou,created on 11/12/2016.
26 | */
27 | public class UninitializedStatementProcessor {
28 |
29 | private static Map map = new HashMap();
30 |
31 | public static void setDefaultValue(StatementProperty prop, Object val, boolean isSyncDefaultValue) {
32 | map.put(prop, new InnerBean(val, isSyncDefaultValue));
33 | }
34 |
35 | public static Object getDefaultValue(StatementProperty prop) {
36 | InnerBean obj = map.get(prop);
37 | if (obj != null) {
38 | return obj.getVal();
39 | } else {
40 | return null;
41 | }
42 | }
43 |
44 | public static boolean isSetDefaultValue(StatementProperty prop) {
45 | return map.containsKey(prop);
46 | }
47 |
48 | public static boolean isSyncDefaultValue(StatementProperty prop) {
49 | InnerBean obj = map.get(prop);
50 | if (obj != null) {
51 | return obj.isSyncDefaultValue();
52 | } else {
53 | return false;
54 | }
55 | }
56 |
57 | public static Object removeDefaultValue(StatementProperty prop) {
58 | return map.remove(prop);
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/ddal-ddr/src/test/java/org/hellojavaer/ddal/ddr/expression/ShardRouteRuleExpressionContextTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018-2018 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.hellojavaer.ddal.ddr.expression;
17 |
18 | import org.hellojavaer.ddal.core.utils.Assert;
19 | import org.junit.Test;
20 |
21 | /**
22 | *
23 | * @author Kaiming Zou,created on 2018/5/28.
24 | */
25 | public class ShardRouteRuleExpressionContextTest {
26 |
27 | @Test
28 | public void test00() {
29 | Assert.notNull(ShardRouteRuleExpressionContext.lookupVariable("format"));
30 | Assert.equals(ShardRouteRuleExpressionContext.getVariable("format"), null);
31 | //
32 | ShardRouteRuleExpressionContext.setVariable("a", "1");
33 | Assert.equals(ShardRouteRuleExpressionContext.getVariable("a"), "1");
34 | Assert.equals(ShardRouteRuleExpressionContext.lookupVariable("a"), "1");
35 | //
36 | ShardRouteRuleExpressionContext.pushContext();
37 | Assert.equals(ShardRouteRuleExpressionContext.getVariable("a"), null);
38 | Assert.equals(ShardRouteRuleExpressionContext.lookupVariable("a"), "1");
39 | //
40 | ShardRouteRuleExpressionContext.setVariable("a", "2");
41 | Assert.equals(ShardRouteRuleExpressionContext.getVariable("a"), "2");
42 | Assert.equals(ShardRouteRuleExpressionContext.lookupVariable("a"), "2");
43 | //
44 | ShardRouteRuleExpressionContext.popContext();
45 | Assert.equals(ShardRouteRuleExpressionContext.getVariable("a"), "1");
46 | Assert.equals(ShardRouteRuleExpressionContext.lookupVariable("a"), "1");
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/ddal-ddr/src/main/java/org/hellojavaer/ddal/ddr/datasource/manager/rw/monitor/WriterMethodInvokeResult.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016-2017 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.hellojavaer.ddal.ddr.datasource.manager.rw.monitor;
17 |
18 | import org.hellojavaer.ddal.ddr.utils.DDRToStringBuilder;
19 |
20 | import java.io.Serializable;
21 |
22 | /**
23 | *
24 | * @author Kaiming Zou,created on 22/12/2016.
25 | */
26 | public class WriterMethodInvokeResult implements Serializable {
27 |
28 | private static final long serialVersionUID = 0L;
29 |
30 | public static final int CODE_OF_SUCCESS = 0;
31 | public static final int CODE_OF_ILLEGAL_ARGUMENT = 20; // 格式错误,参数必填项为空
32 | public static final int CODE_OF_DATA_IS_EMPTY = 40; //
33 | // public static final int CODE_OF_OPERATION_NOT_SUPPORTED = 60;
34 |
35 | private Integer code;
36 | private String desc;
37 |
38 | public WriterMethodInvokeResult() {
39 | }
40 |
41 | public WriterMethodInvokeResult(Integer code, String desc) {
42 | this.code = code;
43 | this.desc = desc;
44 | }
45 |
46 | public Integer getCode() {
47 | return code;
48 | }
49 |
50 | public void setCode(Integer code) {
51 | this.code = code;
52 | }
53 |
54 | public String getDesc() {
55 | return desc;
56 | }
57 |
58 | public void setDesc(String desc) {
59 | this.desc = desc;
60 | }
61 |
62 | @Override
63 | public String toString() {
64 | return new DDRToStringBuilder().append("code", code).append("desc", desc).toString();
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/ddal-ddr/src/main/java/org/hellojavaer/ddal/ddr/shard/simple/SimpleShardParser.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016-2017 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.hellojavaer.ddal.ddr.shard.simple;
17 |
18 | import org.hellojavaer.ddal.ddr.sqlparse.SQLParsedResult;
19 | import org.hellojavaer.ddal.ddr.shard.ShardParser;
20 | import org.hellojavaer.ddal.ddr.shard.ShardRouter;
21 | import org.hellojavaer.ddal.ddr.sqlparse.SQLParsedState;
22 | import org.hellojavaer.ddal.ddr.sqlparse.SQLParser;
23 |
24 | import java.util.Map;
25 |
26 | /**
27 | *
28 | * @author Kaiming Zou,created on 15/11/2016.
29 | */
30 | public class SimpleShardParser implements ShardParser {
31 |
32 | private ShardRouter shardRouter;
33 | private SQLParser sqlParser;
34 |
35 | private SimpleShardParser() {
36 | }
37 |
38 | public SimpleShardParser(SQLParser sqlParser, ShardRouter shardRouter) {
39 | setSqlParser(sqlParser);
40 | setShardRouter(shardRouter);
41 | }
42 |
43 | public ShardRouter getShardRouter() {
44 | return shardRouter;
45 | }
46 |
47 | public void setShardRouter(ShardRouter shardRouter) {
48 | this.shardRouter = shardRouter;
49 | }
50 |
51 | public SQLParser getSqlParser() {
52 | return sqlParser;
53 | }
54 |
55 | public void setSqlParser(SQLParser sqlParser) {
56 | this.sqlParser = sqlParser;
57 | }
58 |
59 | @Override
60 | public SQLParsedResult parse(String sql, Map