<?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.taosdata.taosdemo.mapper.SubTableMapper">

    <!-- 创建子表 -->
    <update id="createUsingSuperTable">
        CREATE table IF NOT EXISTS ${database}.${name} USING ${supertable} TAGS
        <foreach collection="tags" item="tag" index="index" open="(" close=")" separator=",">
            #{tag.value}
        </foreach>
    </update>

    <!-- 插入：向一张表中插入多张表 -->
    <insert id="insertOneTableMultiValues">
        INSERT INTO ${database}.${name}
        VALUES
        <foreach collection="values" item="value">
            <foreach collection="value.fields" item="field" open="(" close=")" separator=",">
                #{field.value}
            </foreach>
        </foreach>
    </insert>

    <!-- 插入：使用自动建表模式，向一张表中插入多条数据 -->
    <insert id="insertOneTableMultiValuesUsingSuperTable">
        INSERT INTO ${database}.${name} USING ${supertable} TAGS
        <foreach collection="tags" item="tag" index="index" open="(" close=")" separator=",">
            #{tag.value}
        </foreach>
        VALUES
        <foreach collection="values" item="value">
            <foreach collection="value.fields" item="field" open="(" close=")" separator=",">
                #{field.value}
            </foreach>
        </foreach>
    </insert>

    <!-- TODO:插入：向一张表中插入多张表, 指定列 -->

    <!-- TODO:插入：向一张表中插入多张表, 自动建表，指定列 -->

    <!-- 插入：向多张表中插入多条数据 -->
    <insert id="insertMultiTableMultiValues">
        INSERT INTO
        <foreach collection="tables" item="table">
            ${table.database}.${table.name}
            VALUES
            <foreach collection="table.values" item="value">
                <foreach collection="value.fields" item="field" open="(" close=")" separator=",">
                    #{field.value}
                </foreach>
            </foreach>
        </foreach>
    </insert>

    <!-- 插入：向多张表中插入多条数据，自动建表 -->
    <insert id="insertMultiTableMultiValuesUsingSuperTable">
        INSERT INTO
        <foreach collection="tables" item="table">
            ${table.database}.${table.name} USING ${table.supertable} TAGS
            <foreach collection="table.tags" item="tag" index="index" open="(" close=")" separator=",">
                #{tag.value}
            </foreach>
            VALUES
            <foreach collection="table.values" item="value">
                <foreach collection="value.fields" item="field" open="(" close=")" separator=",">
                    #{field.value}
                </foreach>
            </foreach>
        </foreach>
    </insert>

    <!-- TODO:插入：向多张表中插入多张表, 指定列 -->

    <!-- TODO:插入：向多张表中插入多张表, 自动建表，指定列 -->

    <!-- TODO:修改子表标签值 alter table ${tablename} set tag tagName=newTagValue -->
    <!-- TODO: -->

</mapper>