| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <?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">
- <update id="createDatabase" parameterType="String">
- create database if not exists #{dataBaseName}
- </update>
- <update id="createSuperTable">
- create table if not exists #{dataBaseName}.#{superTableName}
- <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
- </when>
- <when test="item.dataType == 'tinyint'">
- tinyint
- </when>
- <when test="item.dataType == 'smallint'">
- smallint
- </when>
- <when test="item.dataType == 'int'">
- int
- </when>
- <when test="item.dataType == 'bigint'">
- bigint
- </when>
- <when test="item.dataType == 'float'">
- float
- </when>
- <when test="item.dataType == 'double'">
- double
- </when>
- <when test="item.dataType == 'binary'">
- binary
- </when>
- <when test="item.dataType == 'nchar'">
- nchar
- </when>
- <when test="item.dataType == 'bool'">
- bool
- </when>
- <when test="item.dataType == 'json'">
- json
- </when>
- </choose>
- </if>
- <if test="item.size != null">
- (#{item.size})
- </if>
- </foreach>
- tags
- <!--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
- </when>
- <when test="item.dataType == 'tinyint'">
- tinyint
- </when>
- <when test="item.dataType == 'smallint'">
- smallint
- </when>
- <when test="item.dataType == 'int'">
- int
- </when>
- <when test="item.dataType == 'bigint'">
- bigint
- </when>
- <when test="item.dataType == 'float'">
- float
- </when>
- <when test="item.dataType == 'double'">
- double
- </when>
- <when test="item.dataType == 'binary'">
- binary
- </when>
- <when test="item.dataType == 'nchar'">
- nchar
- </when>
- <when test="item.dataType == 'bool'">
- bool
- </when>
- <when test="item.dataType == 'json'">
- json
- </when>
- </choose>
- </if>
- <if test="item.size != null">
- (#{item.size})
- </if>
- </foreach>
- </update>
- <update id="createTable">
- create table
- if not exists #{dataBaseName}.#{tableName}
- using #{dataBaseName}.#{superTableName}
- tags
- <foreach item="item" collection="tagsFieldValues" separator=","
- open="(" close=")" index="">
- #{item.fieldValue}
- </foreach>
- </update>
- <insert id="insertData">
- insert into #{dataBaseName}.#{tableName}
- <foreach item="item" collection="schemaFieldValues" separator=","
- open="(" close=")" index="">
- #{item.fieldName}
- </foreach>
- using #{dataBaseName}.#{superTableName}
- tags
- <foreach item="item" collection="tagsFieldValues" separator=","
- open="(" close=")" index="">
- #{item.fieldValue}
- </foreach>
- values
- <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
- #{superTableName}
- ADD
- COLUMN
- <if test="fieldsVo.fieldName != null || fieldsVo.fieldName != ''">
- #{fieldsVo.fieldName}
- </if>
- <if test="fieldsVo.dataType != null || fieldsVo.dataType != ''">
- <choose>
- <when test="fieldsVo.dataType == 'timestamp'">
- timestamp
- </when>
- <when test="fieldsVo.dataType == 'tinyint'">
- tinyint
- </when>
- <when test="fieldsVo.dataType == 'smallint'">
- smallint
- </when>
- <when test="fieldsVo.dataType == 'int'">
- int
- </when>
- <when test="fieldsVo.dataType == 'bigint'">
- bigint
- </when>
- <when test="fieldsVo.dataType == 'float'">
- float
- </when>
- <when test="fieldsVo.dataType == 'double'">
- double
- </when>
- <when test="fieldsVo.dataType == 'binary'">
- binary
- </when>
- <when test="fieldsVo.dataType == 'nchar'">
- nchar
- </when>
- <when test="fieldsVo.dataType == 'bool'">
- bool
- </when>
- <when test="fieldsVo.dataType == 'json'">
- json
- </when>
- </choose>
- </if>
- <if test="fieldsVo.size != null">
- (
- #{fieldsVo.size}
- )
- </if>
- </update>
- <update id="dropColumnForSuperTable">
- ALTER
- STABLE
- #{superTableName}
- DROP
- COLUMN
- <if test="fieldsVo.fieldName != null || fieldsVo.fieldName != ''">
- #{fieldsVo.fieldName}
- </if>
- </update>
- <update id="addTagForSuperTable">
- ALTER
- STABLE
- #{superTableName}
- ADD
- TAG
- <if test="fieldsVo.fieldName != null || fieldsVo.fieldName != ''">
- #{fieldsVo.fieldName}
- </if>
- <if test="fieldsVo.dataType != null || fieldsVo.dataType != ''">
- <choose>
- <when test="fieldsVo.dataType == 'timestamp'">
- timestamp
- </when>
- <when test="fieldsVo.dataType == 'tinyint'">
- tinyint
- </when>
- <when test="fieldsVo.dataType == 'smallint'">
- smallint
- </when>
- <when test="fieldsVo.dataType == 'int'">
- int
- </when>
- <when test="fieldsVo.dataType == 'bigint'">
- bigint
- </when>
- <when test="fieldsVo.dataType == 'float'">
- float
- </when>
- <when test="fieldsVo.dataType == 'double'">
- double
- </when>
- <when test="fieldsVo.dataType == 'binary'">
- binary
- </when>
- <when test="fieldsVo.dataType == 'nchar'">
- nchar
- </when>
- <when test="fieldsVo.dataType == 'bool'">
- bool
- </when>
- <when test="fieldsVo.dataType == 'json'">
- json
- </when>
- </choose>
- </if>
- <if test="fieldsVo.size != null">
- (
- #{fieldsVo.size}
- )
- </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
- FROM #{dataBaseName}.#{tableName} WHERE ${fieldName} BETWEEN #{startTime} AND #{endTime}
- </select>
- <select id="checkTableExists" resultType="java.lang.Integer">
- SELECT COUNT(0)
- FROM information_schema.ins_tables
- WHERE db_name = #{dataBaseName}
- AND table_name = #{tableName}
- </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(*)
- from #{dataBaseName}.#{stableName} group by ${tagsName}
- </select>
- <select id="getHistoryData" resultType="java.util.Map"
- parameterType="cn.iocoder.yudao.module.iot.domain.visual.SelectVisualDto">
- SELECT #{fieldName}, ts
- FROM #{dataBaseName}.#{tableName} WHERE ts BETWEEN #{startTime} AND #{endTime}
- LIMIT #{num}
- </select>
- <select id="getRealtimeData" resultType="java.util.Map"
- parameterType="cn.iocoder.yudao.module.iot.domain.visual.SelectVisualDto">
- SELECT #{fieldName}, ts
- FROM #{dataBaseName}.#{tableName} LIMIT #{num}
- </select>
- <select id="getAggregateData" resultType="java.util.Map"
- parameterType="cn.iocoder.yudao.module.iot.domain.visual.SelectVisualDto">
- SELECT #{aggregate}(${fieldName})
- FROM #{dataBaseName}.#{tableName} WHERE ts BETWEEN #{startTime} AND #{endTime} interval (${interval})
- LIMIT #{num}
- </select>
- <insert id="createSuperTableDevice">
- ${sql}
- </insert>
- </mapper>
|