Skip to content

Commit 0e30f41

Browse files
authored
Merge pull request alibaba#14041 from WangzJi/feature/unified-datasource-dialect
feat: Merge nacos-plugin PostgreSQL & Base Implementation
2 parents e275d6a + 5bf23da commit 0e30f41

35 files changed

Lines changed: 2062 additions & 2 deletions
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 1999-2022 Alibaba Group Holding Ltd.
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+
17+
package com.alibaba.nacos.plugin.datasource.constants;
18+
19+
/**
20+
* DatabaseType Constant.
21+
*
22+
* @author Long Yu
23+
**/
24+
public class DatabaseTypeConstant {
25+
26+
public static final String POSTGRESQL = "postgresql";
27+
28+
public static final String GUASSDB = "gaussdb";
29+
30+
public static final String MYSQL = "mysql";
31+
32+
public static final String ORACLE = "oracle";
33+
34+
public static final String DM = "dm";
35+
36+
public static final String SQLSERVER = "sqlserver";
37+
38+
public static final String KINGBASE = "kingbase";
39+
40+
public static final String YASDB = "yasdb";
41+
42+
public static final String DERBY = "derby";
43+
44+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 1999-2022 Alibaba Group Holding Ltd.
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+
17+
package com.alibaba.nacos.plugin.datasource.constants;
18+
19+
/**
20+
* PrimaryKeyConstant.
21+
*
22+
* @author Long Yu
23+
*/
24+
public class PrimaryKeyConstant {
25+
26+
/**
27+
* replace lower Statement.RETURN_GENERATED_KEYS into id key.
28+
*/
29+
public static final String[] LOWER_RETURN_PRIMARY_KEYS = new String[]{"id"};
30+
31+
/**
32+
* upper replace Statement.RETURN_GENERATED_KEYS into id key.
33+
* using dameng database.
34+
*/
35+
public static final String[] UPPER_RETURN_PRIMARY_KEYS = new String[]{"ID"};
36+
37+
}

plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/constants/TableConstant.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ public class TableConstant {
4343
public static final String TENANT_INFO = "tenant_info";
4444

4545
public static final String MIGRATE_CONFIG = "migrate_config";
46+
47+
public static final String CONFIG_INFO_AGGR = "config_info_aggr";
4648
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 1999-2022 Alibaba Group Holding Ltd.
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+
17+
package com.alibaba.nacos.plugin.datasource.dialect;
18+
19+
import com.alibaba.nacos.plugin.datasource.constants.PrimaryKeyConstant;
20+
21+
/**
22+
* Abstract DatabaseDialect.
23+
* Default limit for mysql,postgresql
24+
* @author Long Yu
25+
*/
26+
public abstract class AbstractDatabaseDialect implements DatabaseDialect {
27+
28+
@Override
29+
public int getPagePrevNum(int page, int pageSize) {
30+
return (page - 1) * pageSize;
31+
}
32+
33+
@Override
34+
public int getPageLastNum(int page, int pageSize) {
35+
return pageSize;
36+
}
37+
38+
@Override
39+
public String getLimitTopSqlWithMark(String sql) {
40+
return sql + " LIMIT ? ";
41+
}
42+
43+
@Override
44+
public String getLimitPageSqlWithMark(String sql) {
45+
return sql + " LIMIT ?,? ";
46+
}
47+
48+
@Override
49+
public String getLimitPageSql(String sql, int pageNo, int pageSize) {
50+
return sql + " LIMIT " + getPagePrevNum(pageNo, pageSize) + " , " + pageSize;
51+
}
52+
53+
@Override
54+
public String getLimitPageSqlWithOffset(String sql, int startOffset, int pageSize) {
55+
return sql + " LIMIT " + startOffset + " , " + pageSize;
56+
}
57+
58+
@Override
59+
public String[] getReturnPrimaryKeys() {
60+
return PrimaryKeyConstant.LOWER_RETURN_PRIMARY_KEYS;
61+
}
62+
63+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 1999-2022 Alibaba Group Holding Ltd.
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+
17+
package com.alibaba.nacos.plugin.datasource.dialect;
18+
19+
/**
20+
* DatabaseDialect interface.
21+
* @author Long Yu
22+
*/
23+
public interface DatabaseDialect {
24+
25+
/**
26+
* get database type.
27+
* @return return database type name
28+
*/
29+
public String getType();
30+
31+
/**
32+
* get frist index page param.
33+
* @param page current pageNo
34+
* @param pageSize current pageSize
35+
* @return offset val or maxRange
36+
*/
37+
public int getPagePrevNum(int page, int pageSize);
38+
39+
/**
40+
* get second index page param.
41+
* @param page current pageNo
42+
* @param pageSize current pageSize
43+
* @return limit val or minRange
44+
*/
45+
public int getPageLastNum(int page, int pageSize);
46+
47+
/**
48+
* get page limit top data sql,contain placeholder.
49+
* @param sql orign sql
50+
* @return append limit sql
51+
*/
52+
public String getLimitTopSqlWithMark(String sql);
53+
54+
/**
55+
* get page limit page data sql,contain placeholder.
56+
* @param sql orign sql
57+
* @return append limit sql
58+
*/
59+
public String getLimitPageSqlWithMark(String sql);
60+
61+
/**
62+
* get page limit page data sql,using number.
63+
* @param sql orign sql
64+
* @param pageNo current pageNo
65+
* @param pageSize current pageSize
66+
* @return contain page number param sql
67+
*/
68+
public String getLimitPageSql(String sql, int pageNo, int pageSize);
69+
70+
/**
71+
* get page limit page data sql,using offset.
72+
* @param sql orign sql
73+
* @param startOffset current offset row
74+
* @param pageSize current pageSize
75+
* @return contain page number param sql
76+
*/
77+
public String getLimitPageSqlWithOffset(String sql, int startOffset, int pageSize);
78+
79+
/**
80+
* get database return primary keys.
81+
* @return
82+
*/
83+
public String[] getReturnPrimaryKeys();
84+
85+
/**
86+
* Get the function corresponding to the dialect according to the function name
87+
* @author Mr.Muzhi
88+
* @since 2025/1/7 16:30
89+
* @param functionName functionName
90+
* @return function
91+
*/
92+
String getFunction(String functionName);
93+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 1999-2022 Alibaba Group Holding Ltd.
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+
17+
package com.alibaba.nacos.plugin.datasource.dialect;
18+
19+
import com.alibaba.nacos.plugin.datasource.constants.DatabaseTypeConstant;
20+
import com.alibaba.nacos.plugin.datasource.enums.mysql.TrustedMysqlFunctionEnum;
21+
22+
/**
23+
* defauLT database dialect.
24+
* @author Long Yu
25+
*/
26+
public class DefaultDatabaseDialect extends AbstractDatabaseDialect {
27+
28+
@Override
29+
public String getType() {
30+
return DatabaseTypeConstant.MYSQL;
31+
}
32+
33+
@Override
34+
public String getFunction(String functionName) {
35+
return TrustedMysqlFunctionEnum.getFunctionByName(functionName);
36+
}
37+
38+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 1999-2022 Alibaba Group Holding Ltd.
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+
17+
package com.alibaba.nacos.plugin.datasource.dialect;
18+
19+
import com.alibaba.nacos.plugin.datasource.constants.DatabaseTypeConstant;
20+
import com.alibaba.nacos.plugin.datasource.enums.derby.TrustedDerbyFunctionEnum;
21+
22+
/**
23+
* Derby database dialect.
24+
*
25+
* @author xiweng.yy
26+
*/
27+
public class DerbyDatabaseDialect extends AbstractDatabaseDialect {
28+
29+
@Override
30+
public String getType() {
31+
return DatabaseTypeConstant.DERBY;
32+
}
33+
34+
@Override
35+
public String getLimitTopSqlWithMark(String sql) {
36+
return sql + " OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY";
37+
}
38+
39+
@Override
40+
public String getLimitPageSqlWithMark(String sql) {
41+
return sql + " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
42+
}
43+
44+
@Override
45+
public String getLimitPageSql(String sql, int pageNo, int pageSize) {
46+
return sql + " OFFSET " + getPagePrevNum(pageNo, pageSize) + " ROWS FETCH NEXT " + pageSize + " ROWS ONLY";
47+
}
48+
49+
@Override
50+
public String getLimitPageSqlWithOffset(String sql, int startOffset, int pageSize) {
51+
return sql + " OFFSET " + startOffset + " ROWS FETCH NEXT " + pageSize + " ROWS ONLY";
52+
}
53+
54+
@Override
55+
public String getFunction(String functionName) {
56+
return TrustedDerbyFunctionEnum.getFunctionByName(functionName);
57+
}
58+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 1999-2022 Alibaba Group Holding Ltd.
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+
17+
package com.alibaba.nacos.plugin.datasource.dialect;
18+
19+
import com.alibaba.nacos.plugin.datasource.constants.DatabaseTypeConstant;
20+
import com.alibaba.nacos.plugin.datasource.enums.mysql.TrustedMysqlFunctionEnum;
21+
22+
/**
23+
* MySQL database dialect.
24+
*
25+
* @author xiweng.yy
26+
*/
27+
public class MysqlDatabaseDialect extends AbstractDatabaseDialect {
28+
29+
@Override
30+
public String getType() {
31+
return DatabaseTypeConstant.MYSQL;
32+
}
33+
34+
@Override
35+
public String getFunction(String functionName) {
36+
return TrustedMysqlFunctionEnum.getFunctionByName(functionName);
37+
}
38+
}

0 commit comments

Comments
 (0)