若依微服务1.05版本,tdengine服务完善

1、增加了修改tag值的方法
2、在获取历史时修改了order by,防止防注入
dev 1.0.5
xins 1 year ago
parent 08c2a39100
commit 50ea2fc814

@ -0,0 +1,34 @@
package com.ruoyi.tdengine.api.domain;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* @Description: tag
* @ClassName: AlterTagVo
* @Author : xins
* @Date :2023-08-30 10:56
* @Version :1.0
*/
@Data
public class AlterTagVo {
//数据库名称
@NotBlank(message="databaseName cannot be empty")
private String databaseName;
//子表名称
@NotBlank(message="tableName cannot be empty")
private String tableName;
@NotBlank(message="tagName cannot be empty")
private String tagName;
@NotNull(message="tagValue cannot be empty")
private Object tagValue;
}

@ -36,5 +36,5 @@ public class TdHistorySelectDto {
private String orderByFieldName;
//排序方式
private String orderByMode;
private String sort;
}

@ -39,7 +39,9 @@ public class HwSceneController extends BaseController
@RequestMapping("getLanDemo")
public String getLanDemo(HttpServletResponse response) {
Locale locale = LocaleContextHolder.getLocale();
System.out.println(locale.getLanguage()+"---"+locale.getCountry());
System.out.printf("%s---%s%n",
locale.getLanguage(),
locale.getCountry());
return MessageUtils.getMessage("user.login.username");
}

@ -45,8 +45,8 @@ public class TdEngineController {
try {
//调用创建数据库方法
this.tdEngineService.createDatabase(databaseName);
log.info("success for create database " + databaseName);
return R.ok();
log.info("successfully created database " + databaseName);
return R.ok("successfully created database " + databaseName);
} catch (UncategorizedSQLException e) {
String message = e.getCause().getMessage();
try {
@ -85,8 +85,8 @@ public class TdEngineController {
List<TdFieldVo> schemaFieldsVos = TdFieldVo.batchConvertFields(schemaFields);
List<TdFieldVo> tagsFieldsVos = TdFieldVo.batchConvertFields(tagsFields);
this.tdEngineService.createSuperTable(databaseName, superTableName, firstFieldName, schemaFieldsVos, tagsFieldsVos);
log.info("success for create superTable " + superTableName);
return R.ok("created superTable '" + superTableName + "' success");
log.info("successfully created superTable " + superTableName);
return R.ok("successfully created superTable " + superTableName);
} catch (UncategorizedSQLException e) {
String message = e.getCause().getMessage();
try {
@ -116,8 +116,8 @@ public class TdEngineController {
String tableName = tdTableVo.getTableName();
List<TdField> tagsFieldValues = tdTableVo.getTagsFieldValues();
this.tdEngineService.createTable(databaseName, superTableName, tableName, tagsFieldValues);
log.info("success for create table " + tableName);
return R.ok("created table '" + tableName + "' success");
log.info("successfully created table " + tableName);
return R.ok("successfully created table " + tableName);
} catch (Exception e) {
String message = e.getCause().getMessage();
try {
@ -146,8 +146,8 @@ public class TdEngineController {
try {
TdFieldVo addFieldVo = TdFieldVo.convertField(addTdField);
this.tdEngineService.addSuperTableColumn(databaseName, superTableName, addFieldVo);
log.info("success for add column for superTable " + superTableName + "");
return R.ok();
log.info("successfully added column for superTable " + superTableName);
return R.ok("successfully added column for superTable " + superTableName);
} catch (UncategorizedSQLException e) {
String message = e.getCause().getMessage();
try {
@ -178,8 +178,40 @@ public class TdEngineController {
try {
TdFieldVo dropFieldVo = TdFieldVo.convertField(dropField);
this.tdEngineService.dropSuperTableColumn(databaseName, superTableName, dropFieldVo);
log.info("successful operation: drop column for superTable '" + superTableName + "' success");
return R.ok();
log.info("successfully droped column of superTable " + superTableName);
return R.ok("successfully droped column of superTable " + superTableName);
} catch (UncategorizedSQLException e) {
String message = e.getCause().getMessage();
try {
message = message.substring(message.lastIndexOf("invalid operation"));
} catch (Exception ex) {
}
log.error(message);
return R.fail(message);
} catch (Exception e) {
log.error(e.getMessage());
return R.fail(e.getMessage());
}
}
/**
* @param: alterTagVo
* @description tag
* @author xins
* @date 2023-08-30 11:17
* @return R<?>
*/
@PostMapping("/alterTableTag")
public R<?> alterTableTag(@Validated @RequestBody AlterTagVo alterTagVo) {
String databaseName = alterTagVo.getDatabaseName();
String tableName = alterTagVo.getTableName();
String tagName = alterTagVo.getTagName();
Object tagValue = alterTagVo.getTagValue();
try {
this.tdEngineService.alterTableTag(databaseName,tableName,tagName,tagValue);
log.info("successfully altered tag "+tagName+" value to "+tagValue+" of tableName");
return R.ok("successfully altered tag "+tagName+" value to "+tagValue+" of tableName");
} catch (UncategorizedSQLException e) {
String message = e.getCause().getMessage();
try {

@ -1,9 +1,6 @@
package com.ruoyi.tdengine.mapper;
import com.ruoyi.tdengine.api.domain.TdField;
import com.ruoyi.tdengine.api.domain.TdFieldVo;
import com.ruoyi.tdengine.api.domain.TdHistorySelectDto;
import com.ruoyi.tdengine.api.domain.TdSelectDto;
import com.ruoyi.tdengine.api.domain.*;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@ -83,6 +80,22 @@ public interface TdEngineMapper {
@Param("dropFieldsVo") TdFieldVo dropFieldsVo);
/**
* @param: databaseName
* @param: tableName
* @param: tagName
* @param: tagValue
* @description tagValue
* @author xins
* @date 2023-08-30 11:02
*/
void alterTableTag(@Param("databaseName") String databaseName,
@Param("tableName") String tableName,
@Param("tagName") String tagName,
@Param("tagValue") Object tagValue
);
/**
* @param: databaseName
* @param: tableName
@ -128,8 +141,6 @@ public interface TdEngineMapper {
Map<String, Long> getCountOfHistoryData(TdHistorySelectDto tdHistorySelectDto);
// /**
// * 检查表是否存在
// * @param dataBaseName

@ -69,6 +69,16 @@ public interface ITdEngineService {
*/
public void dropSuperTableColumn(String databaseName,String superTableName, TdFieldVo dropFieldsVo) throws Exception;
/**
* @param: databaseName
* @param: tableName
* @param: tagName
* @param: tagValue
* @description tagValue
* @author xins
* @date 2023-08-30 11:03
*/
public void alterTableTag(String databaseName,String tableName,String tagName,Object tagValue);
/**
* @param: databaseName
* @param: tableName

@ -104,6 +104,19 @@ public class TdEngineServiceImpl implements ITdEngineService {
this.tdEngineMapper.dropSuperTableColumn(databaseName, superTableName, dropFieldsVo);
}
/**
* @param: databaseName
* @param: tableName
* @param: tagName
* @param: tagValue
* @description tagValue
* @author xins
* @date 2023-08-30 11:03
*/
@Override
public void alterTableTag(String databaseName,String tableName,String tagName,Object tagValue){
this.tdEngineMapper.alterTableTag(databaseName,tableName,tagName,tagValue);
}
/**
* @param: databaseName
@ -182,12 +195,12 @@ public class TdEngineServiceImpl implements ITdEngineService {
*/
@Override
public List<Map<String, Object>> getHistoryData(TdHistorySelectDto tdHistorySelectDto) {
if (StringUtils.isBlank(tdHistorySelectDto.getOrderByFieldName())) {
tdHistorySelectDto.setOrderByFieldName(TdEngineConstants.DEFAULT_FIRST_FIELD_NAME);
}
if (StringUtils.isBlank(tdHistorySelectDto.getOrderByMode())) {
tdHistorySelectDto.setOrderByMode(TdEngineConstants.DEFAULT_ORDER_BY_MODE);
}
// if (StringUtils.isBlank(tdHistorySelectDto.getOrderByFieldName())) {
// tdHistorySelectDto.setOrderByFieldName(TdEngineConstants.DEFAULT_FIRST_FIELD_NAME);
// }
// if (StringUtils.isBlank(tdHistorySelectDto.getOrderByMode())) {
// tdHistorySelectDto.setOrderByMode(TdEngineConstants.DEFAULT_ORDER_BY_MODE);
// }
List<Map<String, Object>> historyDataMaps = this.tdEngineMapper.getHistoryData(tdHistorySelectDto);
return historyDataMaps;

@ -173,6 +173,10 @@
</if>
</update>
<update id="alterTableTag">
ALTER TABLE #{databaseName}.#{tableName} SET TAG #{tagName}=#{tagValue};
</update>
<!-- 需要提前建表。如果用插入记录时自动建表则会有概率出现问题:设备上报的属性值跟定义的属性值不同的情况-->
<insert id="insertTable">
@ -212,10 +216,15 @@
</if>
</where>
order by ${orderByFieldName}
<if test="#{orderByMode} != null and #{orderByMode} != ''">
${orderByMode}
</if>
<!--由于此处orderby是用${orderByFieldName},而不是用#${orderByFieldName}这样就会用sql注入风险所以加上choose和判断条件-->
<choose>
<when test="(orderByFieldName=='ts') and (sort=='desc' or sort=='asc')">
order by ${orderByFieldName} ${sort}
</when>
</choose>
<if test="offset != 0 and limit != 0 ">
LIMIT #{offset},#{limit}
</if>
@ -235,6 +244,8 @@
<!--
<select id="checkTableExists" resultType="java.lang.Integer">
SELECT COUNT(0) FROM #{dataBaseName}.#{tableName}

Loading…
Cancel
Save