Skip to content

Commit

Permalink
Reactor code in datasource-plugin (alibaba#10791)
Browse files Browse the repository at this point in the history
* Reactor code in datasource-plugin

* Fix Abstract Mapper Test Case

* Add Empty Check

* Fix Checkstyle

* fix checkstyle

* fix check style

* fix check style

* Fix CheckStyle

* Fix SQL Blank
  • Loading branch information
ZhangShenao authored Jul 25, 2023
1 parent 149c50f commit 25ef67d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public final class DiskUtils {

private static final String NO_SPACE_EN = "No space left on device";

private static final String DISK_QUATA_CN = "超出磁盘限额";
private static final String DISK_QUOTA_CN = "超出磁盘限额";

private static final String DISK_QUATA_EN = "Disk quota exceeded";
private static final String DISK_QUOTA_EN = "Disk quota exceeded";

private static final Charset CHARSET = StandardCharsets.UTF_8;

Expand Down Expand Up @@ -237,8 +237,8 @@ public static boolean writeFile(File file, byte[] content, boolean append) {
} catch (IOException ioe) {
if (ioe.getMessage() != null) {
String errMsg = ioe.getMessage();
if (NO_SPACE_CN.equals(errMsg) || NO_SPACE_EN.equals(errMsg) || errMsg.contains(DISK_QUATA_CN) || errMsg
.contains(DISK_QUATA_EN)) {
if (NO_SPACE_CN.equals(errMsg) || NO_SPACE_EN.equals(errMsg) || errMsg.contains(DISK_QUOTA_CN) || errMsg
.contains(DISK_QUOTA_EN)) {
LOGGER.warn("磁盘满,自杀退出");
System.exit(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public class ConfigInfoAggrMapperByMySql extends AbstractMapper implements Confi

@Override
public MapperResult findConfigInfoAggrByPageFetchRows(MapperContext context) {
Integer startRow = context.getStartRow();
Integer pageSize = context.getPageSize();
int startRow = context.getStartRow();
int pageSize = context.getPageSize();
String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID);
String groupId = (String) context.getWhereParameter(FieldConstant.GROUP_ID);
String tenantId = (String) context.getWhereParameter(FieldConstant.TENANT_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.alibaba.nacos.plugin.datasource.mapper;

import com.alibaba.nacos.common.utils.CollectionUtils;

import java.util.List;

/**
Expand Down Expand Up @@ -43,17 +45,11 @@ public String select(List<String> columns, List<String> where) {
sql.append(getTableName());
sql.append(" ");

if (where.size() == 0) {
if (CollectionUtils.isEmpty(where)) {
return sql.toString();
}

sql.append("WHERE ");
for (int i = 0; i < where.size(); i++) {
sql.append(where.get(i)).append(" = ").append("?");
if (i != where.size() - 1) {
sql.append(" AND ");
}
}
appendWhereClause(where, sql);
return sql.toString();
}

Expand Down Expand Up @@ -100,18 +96,13 @@ public String update(List<String> columns, List<String> where) {
}
}

if (where.size() == 0) {
if (CollectionUtils.isEmpty(where)) {
return sql.toString();
}

sql.append(" WHERE ");
sql.append(" ");
appendWhereClause(where, sql);

for (int i = 0; i < where.size(); i++) {
sql.append(where.get(i)).append(" = ").append("?");
if (i != where.size() - 1) {
sql.append(" AND ");
}
}
return sql.toString();
}

Expand Down Expand Up @@ -143,18 +134,23 @@ public String count(List<String> where) {
return sql.toString();
}

appendWhereClause(where, sql);

return sql.toString();
}

@Override
public String[] getPrimaryKeyGeneratedKeys() {
return new String[] {"id"};
}

private void appendWhereClause(List<String> where, StringBuilder sql) {
sql.append("WHERE ");
for (int i = 0; i < where.size(); i++) {
sql.append(where.get(i)).append(" = ").append("?");
if (i != where.size() - 1) {
sql.append(" AND ");
}
}
return sql.toString();
}

@Override
public String[] getPrimaryKeyGeneratedKeys() {
return new String[]{"id"};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@ public void testGetPrimaryKeyGeneratedKeys() {
String[] keys = abstractMapper.getPrimaryKeyGeneratedKeys();
Assert.assertEquals(keys[0], "id");
}

@Test
public void testSelectAll() {
String sql = abstractMapper.select(Arrays.asList("id", "name"), null);
Assert.assertEquals(sql, "SELECT id,name FROM tenant_info ");
}

@Test
public void testCountAll() {
String sql = abstractMapper.count(null);
Assert.assertEquals(sql, "SELECT COUNT(*) FROM tenant_info ");
}
}

0 comments on commit 25ef67d

Please sign in to comment.