TdEngineMapper.xml 11.3 KB
<?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 STABLE 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.dataLength > 0">
                (
                ${item.dataLength}
                )
            </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.dataLength > 0">
                (
                ${item.dataLength}
                )
            </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 ${dataBaseName}.${superTableName} ADD COLUMN
        <if test="field.fieldName != null || field.fieldName != ''">
            #{field.fieldName}
        </if>
        <if test="field.dataType != null || field.dataType != ''">
            <choose>
                <when test="field.dataType == 'TIMESTAMP'">
                    TIMESTAMP
                </when>
                <when test="field.dataType == 'TINYINT'">
                    TINYINT
                </when>
                <when test="field.dataType == 'SMALLINT'">
                    SMALLINT
                </when>
                <when test="field.dataType == 'INT'">
                    INT
                </when>
                <when test="field.dataType == 'BIGINT'">
                    BIGINT
                </when>
                <when test="field.dataType == 'FLOAT'">
                    FLOAT
                </when>
                <when test="field.dataType == 'DOUBLE'">
                    DOUBLE
                </when>
                <when test="field.dataType == 'BINARY'">
                    BINARY
                </when>
                <when test="field.dataType == 'NCHAR'">
                    NCHAR
                </when>
                <when test="field.dataType == 'BOOL'">
                    BOOL
                </when>
                <when test="field.dataType == 'JSON'">
                    JSON
                </when>
            </choose>
        </if>
        <if test="field.dataLength > 0">
            (
            #{field.dataLength}
            )
        </if>
    </update>

    <update id="dropColumnForSuperTable">
        ALTER STABLE ${dataBaseName}.${superTableName} DROP COLUMN
        <if test="field.fieldName != null || field.fieldName != ''">
            #{field.fieldName}
        </if>
    </update>

    <update id="addTagForSuperTable">
        ALTER
        STABLE
        #{superTableName}
        ADD
        TAG
        <if test="field.fieldName != null || fieldDO.fieldName != ''">
            #{fieldDO.fieldName}
        </if>
        <if test="fieldDO.dataType != null || fieldDO.dataType != ''">
            <choose>
                <when test="fieldDO.dataType == 'timestamp'">
                    timestamp
                </when>
                <when test="fieldDO.dataType == 'tinyint'">
                    tinyint
                </when>
                <when test="fieldDO.dataType == 'smallint'">
                    smallint
                </when>
                <when test="fieldDO.dataType == 'int'">
                    int
                </when>
                <when test="fieldDO.dataType == 'bigint'">
                    bigint
                </when>
                <when test="fieldDO.dataType == 'float'">
                    float
                </when>
                <when test="fieldDO.dataType == 'double'">
                    double
                </when>
                <when test="fieldDO.dataType == 'binary'">
                    binary
                </when>
                <when test="fieldDO.dataType == 'nchar'">
                    nchar
                </when>
                <when test="fieldDO.dataType == 'bool'">
                    bool
                </when>
                <when test="fieldDO.dataType == 'json'">
                    json
                </when>
            </choose>
        </if>
        <if test="fieldDO.dataLength > 0">
            (
            #{fieldDO.dataLength}
            )
        </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="showSuperTables" resultType="java.util.Map">
        SHOW ${dataBaseName}.STABLES LIKE '${superTableName}'
    </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>

    <select id="describeSuperTable" resultType="java.util.Map">
        DESCRIBE ${dataBaseName}.${superTableName}
    </select>

</mapper>