2024-10-23 23:23:40 +08:00
|
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
|
|
|
|
|
|
|
|
|
<mapper namespace="cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineMapper">
|
2024-10-27 21:26:35 +08:00
|
|
|
|
<!-- TODO 对 $ 符号有安全要求的话,后期改为接口方式 -->
|
2024-10-23 23:23:40 +08:00
|
|
|
|
|
|
|
|
|
<update id="createDatabase" parameterType="String">
|
2024-10-27 21:26:35 +08:00
|
|
|
|
CREATE DATABASE IF NOT EXISTS ${dataBaseName}
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</update>
|
|
|
|
|
|
|
|
|
|
<update id="createSuperTable">
|
2024-10-26 23:15:31 +08:00
|
|
|
|
CREATE STABLE IF NOT EXISTS ${dataBaseName}.${superTableName}
|
2024-10-23 23:23:40 +08:00
|
|
|
|
<foreach item="item" collection="schemaFields" separator=","
|
|
|
|
|
open="(" close=")" index="">
|
|
|
|
|
<if test="item.fieldName != null || item.fieldName != ''">
|
|
|
|
|
${item.fieldName}
|
|
|
|
|
</if>
|
|
|
|
|
<if test="item.dataType != null || item.dataType != ''">
|
|
|
|
|
<choose>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'TIMESTAMP'">
|
|
|
|
|
TIMESTAMP
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'TINYINT'">
|
|
|
|
|
TINYINT
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'SMALLINT'">
|
|
|
|
|
SMALLINT
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'INT'">
|
|
|
|
|
INT
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'BIGINT'">
|
|
|
|
|
BIGINT
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'FLOAT'">
|
|
|
|
|
FLOAT
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'DOUBLE'">
|
|
|
|
|
DOUBLE
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'BINARY'">
|
|
|
|
|
BINARY
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'NCHAR'">
|
|
|
|
|
NCHAR
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'BOOL'">
|
|
|
|
|
BOOL
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'JSON'">
|
|
|
|
|
JSON
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
|
|
|
|
</choose>
|
|
|
|
|
</if>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<if test="item.dataLength > 0">
|
|
|
|
|
(
|
|
|
|
|
${item.dataLength}
|
|
|
|
|
)
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</if>
|
|
|
|
|
</foreach>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
TAGS
|
2024-10-23 23:23:40 +08:00
|
|
|
|
<!--tdEngine不支持动态tags里的数据类型,只能使用choose标签比对-->
|
|
|
|
|
<foreach item="item" collection="tagsFields" separator=","
|
|
|
|
|
open="(" close=")" index="">
|
|
|
|
|
<if test="item.fieldName != null || item.fieldName != ''">
|
|
|
|
|
${item.fieldName}
|
|
|
|
|
</if>
|
|
|
|
|
<if test="item.dataType != null || item.dataType != ''">
|
|
|
|
|
<choose>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'TIMESTAMP'">
|
|
|
|
|
TIMESTAMP
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'TINYINT'">
|
|
|
|
|
TINYINT
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'SMALLINT'">
|
|
|
|
|
SMALLINT
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'INT'">
|
|
|
|
|
INT
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'BIGINT'">
|
|
|
|
|
BIGINT
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'FLOAT'">
|
|
|
|
|
FLOAT
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'DOUBLE'">
|
|
|
|
|
DOUBLE
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'BINARY'">
|
|
|
|
|
BINARY
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'NCHAR'">
|
|
|
|
|
NCHAR
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'BOOL'">
|
|
|
|
|
BOOL
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="item.dataType == 'JSON'">
|
|
|
|
|
JSON
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
|
|
|
|
</choose>
|
|
|
|
|
</if>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<if test="item.dataLength > 0">
|
|
|
|
|
(
|
|
|
|
|
${item.dataLength}
|
|
|
|
|
)
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</if>
|
|
|
|
|
</foreach>
|
|
|
|
|
</update>
|
|
|
|
|
|
2024-10-31 21:47:54 +08:00
|
|
|
|
<!-- CREATE TABLE [IF NOT EXISTS] tb_name USING stb_name TAGS (tag_value1, ...);-->
|
2024-10-23 23:23:40 +08:00
|
|
|
|
<update id="createTable">
|
2024-10-31 21:47:54 +08:00
|
|
|
|
CREATE TABLE IF NOT EXISTS ${dataBaseName}.${tableName}
|
|
|
|
|
USING ${dataBaseName}.${superTableName}
|
|
|
|
|
<foreach item="item" collection="tagsFieldValues" separator="," open="(" close=")">
|
|
|
|
|
${item.fieldName}
|
|
|
|
|
</foreach>
|
|
|
|
|
TAGS
|
|
|
|
|
<foreach item="item" collection="tagsFieldValues" separator="," open="(" close=")">
|
2024-10-23 23:23:40 +08:00
|
|
|
|
#{item.fieldValue}
|
|
|
|
|
</foreach>
|
|
|
|
|
</update>
|
|
|
|
|
|
2024-10-31 21:47:54 +08:00
|
|
|
|
<!-- insert into d1004 (ts, voltage, phase) values("2018-10-04 14:38:06", 223, 0.29) -->
|
2024-10-23 23:23:40 +08:00
|
|
|
|
<insert id="insertData">
|
2024-10-31 21:47:54 +08:00
|
|
|
|
INSERT INTO ${dataBaseName}.${tableName}
|
2024-10-23 23:23:40 +08:00
|
|
|
|
<foreach item="item" collection="schemaFieldValues" separator=","
|
|
|
|
|
open="(" close=")" index="">
|
2024-10-31 21:47:54 +08:00
|
|
|
|
${item.fieldName}
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</foreach>
|
2024-10-31 21:47:54 +08:00
|
|
|
|
VALUES
|
2024-10-23 23:23:40 +08:00
|
|
|
|
<foreach item="item" collection="schemaFieldValues" separator=","
|
|
|
|
|
open="(" close=")" index="">
|
|
|
|
|
#{item.fieldValue}
|
|
|
|
|
</foreach>
|
|
|
|
|
</insert>
|
|
|
|
|
|
|
|
|
|
<select id="selectByTimestamp" parameterType="cn.iocoder.yudao.module.iot.domain.SelectDto"
|
|
|
|
|
resultType="Map">
|
|
|
|
|
select * from #{dataBaseName}.#{tableName}
|
|
|
|
|
<!--查询这里不能使用#{}占位符的方式,使用这种方式,tdEngine不识别为列名,只能使用${}占位的方式-->
|
|
|
|
|
<!--因为tdEngine引擎一次只执行一条sql,所以有效预防了sql的注入,且该服务该接口为内部调用,所以可以使用${}-->
|
|
|
|
|
where ${fieldName}
|
|
|
|
|
between #{startTime} and #{endTime}
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<update id="addColumnForSuperTable">
|
2024-10-26 23:15:31 +08:00
|
|
|
|
ALTER STABLE ${dataBaseName}.${superTableName} ADD COLUMN
|
|
|
|
|
<if test="field.fieldName != null || field.fieldName != ''">
|
|
|
|
|
#{field.fieldName}
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</if>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<if test="field.dataType != null || field.dataType != ''">
|
2024-10-23 23:23:40 +08:00
|
|
|
|
<choose>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="field.dataType == 'TIMESTAMP'">
|
|
|
|
|
TIMESTAMP
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="field.dataType == 'TINYINT'">
|
|
|
|
|
TINYINT
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="field.dataType == 'SMALLINT'">
|
|
|
|
|
SMALLINT
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="field.dataType == 'INT'">
|
|
|
|
|
INT
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="field.dataType == 'BIGINT'">
|
|
|
|
|
BIGINT
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="field.dataType == 'FLOAT'">
|
|
|
|
|
FLOAT
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="field.dataType == 'DOUBLE'">
|
|
|
|
|
DOUBLE
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="field.dataType == 'BINARY'">
|
|
|
|
|
BINARY
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="field.dataType == 'NCHAR'">
|
|
|
|
|
NCHAR
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="field.dataType == 'BOOL'">
|
|
|
|
|
BOOL
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="field.dataType == 'JSON'">
|
|
|
|
|
JSON
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</when>
|
|
|
|
|
</choose>
|
|
|
|
|
</if>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<if test="field.dataLength > 0">
|
2024-10-23 23:23:40 +08:00
|
|
|
|
(
|
2024-10-26 23:15:31 +08:00
|
|
|
|
#{field.dataLength}
|
2024-10-23 23:23:40 +08:00
|
|
|
|
)
|
|
|
|
|
</if>
|
|
|
|
|
</update>
|
|
|
|
|
|
|
|
|
|
<update id="dropColumnForSuperTable">
|
2024-10-26 23:15:31 +08:00
|
|
|
|
ALTER STABLE ${dataBaseName}.${superTableName} DROP COLUMN
|
|
|
|
|
<if test="field.fieldName != null || field.fieldName != ''">
|
|
|
|
|
#{field.fieldName}
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</if>
|
|
|
|
|
</update>
|
|
|
|
|
|
|
|
|
|
<update id="addTagForSuperTable">
|
|
|
|
|
ALTER
|
|
|
|
|
STABLE
|
|
|
|
|
#{superTableName}
|
|
|
|
|
ADD
|
|
|
|
|
TAG
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<if test="field.fieldName != null || fieldDO.fieldName != ''">
|
|
|
|
|
#{fieldDO.fieldName}
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</if>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<if test="fieldDO.dataType != null || fieldDO.dataType != ''">
|
2024-10-23 23:23:40 +08:00
|
|
|
|
<choose>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="fieldDO.dataType == 'timestamp'">
|
2024-10-23 23:23:40 +08:00
|
|
|
|
timestamp
|
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="fieldDO.dataType == 'tinyint'">
|
2024-10-23 23:23:40 +08:00
|
|
|
|
tinyint
|
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="fieldDO.dataType == 'smallint'">
|
2024-10-23 23:23:40 +08:00
|
|
|
|
smallint
|
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="fieldDO.dataType == 'int'">
|
2024-10-23 23:23:40 +08:00
|
|
|
|
int
|
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="fieldDO.dataType == 'bigint'">
|
2024-10-23 23:23:40 +08:00
|
|
|
|
bigint
|
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="fieldDO.dataType == 'float'">
|
2024-10-23 23:23:40 +08:00
|
|
|
|
float
|
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="fieldDO.dataType == 'double'">
|
2024-10-23 23:23:40 +08:00
|
|
|
|
double
|
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="fieldDO.dataType == 'binary'">
|
2024-10-23 23:23:40 +08:00
|
|
|
|
binary
|
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="fieldDO.dataType == 'nchar'">
|
2024-10-23 23:23:40 +08:00
|
|
|
|
nchar
|
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="fieldDO.dataType == 'bool'">
|
2024-10-23 23:23:40 +08:00
|
|
|
|
bool
|
|
|
|
|
</when>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<when test="fieldDO.dataType == 'json'">
|
2024-10-23 23:23:40 +08:00
|
|
|
|
json
|
|
|
|
|
</when>
|
|
|
|
|
</choose>
|
|
|
|
|
</if>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<if test="fieldDO.dataLength > 0">
|
2024-10-23 23:23:40 +08:00
|
|
|
|
(
|
2024-10-26 23:15:31 +08:00
|
|
|
|
#{fieldDO.dataLength}
|
2024-10-23 23:23:40 +08:00
|
|
|
|
)
|
|
|
|
|
</if>
|
|
|
|
|
</update>
|
|
|
|
|
|
|
|
|
|
<update id="dropTagForSuperTable">
|
|
|
|
|
ALTER
|
|
|
|
|
STABLE
|
|
|
|
|
#{superTableName}
|
|
|
|
|
DROP
|
|
|
|
|
TAG
|
|
|
|
|
<if test="fieldsVo.fieldName != null || fieldsVo.fieldName != ''">
|
|
|
|
|
#{fieldsVo.fieldName}
|
|
|
|
|
</if>
|
|
|
|
|
</update>
|
|
|
|
|
|
|
|
|
|
<select id="getCountByTimestamp" parameterType="cn.iocoder.yudao.module.iot.domain.SelectDto"
|
|
|
|
|
resultType="java.util.Map">
|
|
|
|
|
SELECT count(0) AS count
|
2024-10-27 21:26:35 +08:00
|
|
|
|
FROM #{dataBaseName}.#{tableName}
|
|
|
|
|
WHERE ${fieldName} BETWEEN #{startTime} AND #{endTime}
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</select>
|
|
|
|
|
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<select id="showSuperTables" resultType="java.util.Map">
|
|
|
|
|
SHOW ${dataBaseName}.STABLES LIKE '${superTableName}'
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<select id="getLastData" resultType="java.util.Map">
|
|
|
|
|
select last(time), *
|
|
|
|
|
from #{tableName}
|
|
|
|
|
where device_id = #{deviceId}
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<select id="getLastDataByTags" parameterType="cn.iocoder.yudao.module.iot.domain.TagsSelectDao"
|
|
|
|
|
resultType="Map">
|
|
|
|
|
select last(*)
|
2024-10-27 21:26:35 +08:00
|
|
|
|
from #{dataBaseName}.#{stableName}
|
|
|
|
|
group by ${tagsName}
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<select id="getHistoryData" resultType="java.util.Map"
|
|
|
|
|
parameterType="cn.iocoder.yudao.module.iot.domain.visual.SelectVisualDto">
|
2024-11-05 23:26:34 +08:00
|
|
|
|
SELECT ${fieldName} as data, time
|
|
|
|
|
FROM ${dataBaseName}.${tableName}
|
|
|
|
|
WHERE time BETWEEN #{startTime} AND #{endTime}
|
|
|
|
|
AND ${fieldName} IS NOT NULL
|
|
|
|
|
ORDER BY time DESC
|
|
|
|
|
LIMIT #{params.rows} offset #{params.page}
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</select>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
|
2024-10-23 23:23:40 +08:00
|
|
|
|
<select id="getRealtimeData" resultType="java.util.Map"
|
|
|
|
|
parameterType="cn.iocoder.yudao.module.iot.domain.visual.SelectVisualDto">
|
2024-11-05 23:26:34 +08:00
|
|
|
|
SELECT #{fieldName}, time
|
2024-10-27 21:26:35 +08:00
|
|
|
|
FROM #{dataBaseName}.#{tableName}
|
2024-10-23 23:23:40 +08:00
|
|
|
|
</select>
|
2024-10-26 23:15:31 +08:00
|
|
|
|
|
2024-10-23 23:23:40 +08:00
|
|
|
|
<select id="getAggregateData" resultType="java.util.Map"
|
|
|
|
|
parameterType="cn.iocoder.yudao.module.iot.domain.visual.SelectVisualDto">
|
|
|
|
|
SELECT #{aggregate}(${fieldName})
|
2024-10-27 21:26:35 +08:00
|
|
|
|
FROM #{dataBaseName}.#{tableName}
|
|
|
|
|
WHERE ts BETWEEN #{startTime} AND #{endTime} interval (${interval})
|
2024-10-23 23:23:40 +08:00
|
|
|
|
LIMIT #{num}
|
|
|
|
|
</select>
|
|
|
|
|
|
2024-10-26 23:15:31 +08:00
|
|
|
|
<select id="describeSuperTable" resultType="java.util.Map">
|
|
|
|
|
DESCRIBE ${dataBaseName}.${superTableName}
|
|
|
|
|
</select>
|
2024-11-05 23:26:34 +08:00
|
|
|
|
<select id="getHistoryCount" resultType="java.lang.Long">
|
|
|
|
|
SELECT count(time)
|
|
|
|
|
FROM ${dataBaseName}.${tableName}
|
|
|
|
|
WHERE time BETWEEN #{startTime} AND #{endTime}
|
|
|
|
|
AND ${fieldName} IS NOT NULL
|
|
|
|
|
</select>
|
2024-10-23 23:23:40 +08:00
|
|
|
|
|
|
|
|
|
</mapper>
|