<?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="com.usky.demo.mapper.QueryTdengineDataMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.usky.demo.domain.QueryTdengineData">
        <result column="id" property="id" />
        <result column="tdengine_name" property="tdengineName" />
    </resultMap>

    <update id="createSuperTableAndColumn"
            parameterType="com.usky.demo.service.vo.SuperTableVO">
        create stable if not exists ${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}
                <!-- 当dataType.quoted为true时添加size -->
                <if test="item.dataType.quoted == true and item.size != null">
                    (${item.size})
                </if>
            </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}
                <!-- 当dataType.quoted为true时添加size -->
                <if test="item.dataType.quoted == true and item.size != null">
                    (${item.size})
                </if>
            </if>
        </foreach>
    </update>

    <update id="addSuperTableColumn">
        ALTER
        STABLE
        ${superTableName}
        ADD
        COLUMN
        <if test="fields.fieldName != null || fields.fieldName != ''">
            ${fields.fieldName}
        </if>
        <if test="fields.dataType != null || fields.dataType != ''">
            ${fields.dataType}
            <!-- 当dataType.quoted为true时添加size -->
            <if test="fields.dataType.quoted == true and fields.size != null">
                (${fields.size})
            </if>
        </if>
    </update>

    <update id="addSuperTableTag">
        ALTER
        STABLE
        ${superTableName}
        ADD
        TAG
        <if test="fields.fieldName != null || fields.fieldName != ''">
            ${fields.fieldName}
        </if>
        <if test="fields.dataType != null || fields.dataType != ''">
            ${fields.dataType}
            <!-- 当dataType.quoted为true时添加size -->
            <if test="fields.dataType.quoted == true and fields.size != null">
                (${fields.size})
            </if>
        </if>
    </update>

    <delete id="dropSuperTableColumn">
        ALTER
        STABLE
        ${superTableName}
        DROP
        COLUMN
        <if test="fields.fieldName != null || fields.fieldName != ''">
            ${fields.fieldName}
        </if>
    </delete>

    <delete id="dropSuperTableTag">
        ALTER
        STABLE
        ${superTableName}
        DROP
        TAG
        <if test="fields.fieldName != null || fields.fieldName != ''">
            ${fields.fieldName}
        </if>
    </delete>

    <select id="describeSuperOrSubTable" resultType="com.usky.demo.domain.SuperTableDescribeVO">
        DESCRIBE ${tableName};
    </select>

    <select id="getAllDeviceRealTime" resultType="com.usky.demo.domain.DeviceRealDataVO">
        select ts,device_uuid,realtime,device_id from datarealtime;
    </select>


</mapper>
