328 lines
12 KiB
XML
Raw Normal View History

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">
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>
<when test="item.dataType == 'TIMESTAMP'">
TIMESTAMP
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'TINYINT'">
TINYINT
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'SMALLINT'">
SMALLINT
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'INT'">
INT
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'BIGINT'">
BIGINT
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'FLOAT'">
FLOAT
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'DOUBLE'">
DOUBLE
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'BINARY'">
BINARY
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'NCHAR'">
NCHAR
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'BOOL'">
BOOL
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'JSON'">
JSON
2024-10-23 23:23:40 +08:00
</when>
</choose>
</if>
<if test="item.dataLength > 0">
(
${item.dataLength}
)
2024-10-23 23:23:40 +08:00
</if>
</foreach>
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>
<when test="item.dataType == 'TIMESTAMP'">
TIMESTAMP
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'TINYINT'">
TINYINT
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'SMALLINT'">
SMALLINT
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'INT'">
INT
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'BIGINT'">
BIGINT
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'FLOAT'">
FLOAT
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'DOUBLE'">
DOUBLE
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'BINARY'">
BINARY
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'NCHAR'">
NCHAR
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'BOOL'">
BOOL
2024-10-23 23:23:40 +08:00
</when>
<when test="item.dataType == 'JSON'">
JSON
2024-10-23 23:23:40 +08:00
</when>
</choose>
</if>
<if test="item.dataLength > 0">
(
${item.dataLength}
)
2024-10-23 23:23:40 +08:00
</if>
</foreach>
</update>
<!-- CREATE TABLE [IF NOT EXISTS] tb_name USING stb_name TAGS (tag_value1, ...);-->
2024-10-23 23:23:40 +08:00
<update id="createTable">
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>
<!-- 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">
INSERT INTO ${dataBaseName}.${tableName}
2024-10-23 23:23:40 +08:00
<foreach item="item" collection="schemaFieldValues" separator=","
open="(" close=")" index="">
${item.fieldName}
2024-10-23 23:23:40 +08:00
</foreach>
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">
ALTER STABLE ${dataBaseName}.${superTableName} ADD COLUMN
<if test="field.fieldName != null || field.fieldName != ''">
#{field.fieldName}
2024-10-23 23:23:40 +08:00
</if>
<if test="field.dataType != null || field.dataType != ''">
2024-10-23 23:23:40 +08:00
<choose>
<when test="field.dataType == 'TIMESTAMP'">
TIMESTAMP
2024-10-23 23:23:40 +08:00
</when>
<when test="field.dataType == 'TINYINT'">
TINYINT
2024-10-23 23:23:40 +08:00
</when>
<when test="field.dataType == 'SMALLINT'">
SMALLINT
2024-10-23 23:23:40 +08:00
</when>
<when test="field.dataType == 'INT'">
INT
2024-10-23 23:23:40 +08:00
</when>
<when test="field.dataType == 'BIGINT'">
BIGINT
2024-10-23 23:23:40 +08:00
</when>
<when test="field.dataType == 'FLOAT'">
FLOAT
2024-10-23 23:23:40 +08:00
</when>
<when test="field.dataType == 'DOUBLE'">
DOUBLE
2024-10-23 23:23:40 +08:00
</when>
<when test="field.dataType == 'BINARY'">
BINARY
2024-10-23 23:23:40 +08:00
</when>
<when test="field.dataType == 'NCHAR'">
NCHAR
2024-10-23 23:23:40 +08:00
</when>
<when test="field.dataType == 'BOOL'">
BOOL
2024-10-23 23:23:40 +08:00
</when>
<when test="field.dataType == 'JSON'">
JSON
2024-10-23 23:23:40 +08:00
</when>
</choose>
</if>
<if test="field.dataLength > 0">
2024-10-23 23:23:40 +08:00
(
#{field.dataLength}
2024-10-23 23:23:40 +08:00
)
</if>
</update>
<update id="dropColumnForSuperTable">
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
<if test="field.fieldName != null || fieldDO.fieldName != ''">
#{fieldDO.fieldName}
2024-10-23 23:23:40 +08:00
</if>
<if test="fieldDO.dataType != null || fieldDO.dataType != ''">
2024-10-23 23:23:40 +08:00
<choose>
<when test="fieldDO.dataType == 'timestamp'">
2024-10-23 23:23:40 +08:00
timestamp
</when>
<when test="fieldDO.dataType == 'tinyint'">
2024-10-23 23:23:40 +08:00
tinyint
</when>
<when test="fieldDO.dataType == 'smallint'">
2024-10-23 23:23:40 +08:00
smallint
</when>
<when test="fieldDO.dataType == 'int'">
2024-10-23 23:23:40 +08:00
int
</when>
<when test="fieldDO.dataType == 'bigint'">
2024-10-23 23:23:40 +08:00
bigint
</when>
<when test="fieldDO.dataType == 'float'">
2024-10-23 23:23:40 +08:00
float
</when>
<when test="fieldDO.dataType == 'double'">
2024-10-23 23:23:40 +08:00
double
</when>
<when test="fieldDO.dataType == 'binary'">
2024-10-23 23:23:40 +08:00
binary
</when>
<when test="fieldDO.dataType == 'nchar'">
2024-10-23 23:23:40 +08:00
nchar
</when>
<when test="fieldDO.dataType == 'bool'">
2024-10-23 23:23:40 +08:00
bool
</when>
<when test="fieldDO.dataType == 'json'">
2024-10-23 23:23:40 +08:00
json
</when>
</choose>
</if>
<if test="fieldDO.dataLength > 0">
2024-10-23 23:23:40 +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>
<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">
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-23 23:23:40 +08:00
<select id="getRealtimeData" resultType="java.util.Map"
parameterType="cn.iocoder.yudao.module.iot.domain.visual.SelectVisualDto">
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-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>
<select id="describeSuperTable" resultType="java.util.Map">
DESCRIBE ${dataBaseName}.${superTableName}
</select>
<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>