<?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.fateverse.code.mapper.dynamic.MySqlDynamicTableMapper">

    <select id="checkSource" resultType="java.lang.Integer">
        select 1 from dual
    </select>

    <select id="selectList" resultMap="cn.fateverse.code.mapper.dynamic.DynamicTableMapper.DynamicTableResult">
        select table_name, table_comment, create_time, update_time
        from information_schema.tables
        where table_schema = (select database())
        and table_name NOT LIKE 'gen_%'
        <if test="table.tableName != null and table.tableName != ''">
            and  table_name like  concat('%', #{table.tableName}, '%')
        </if>
        <if test="table.tableComment != null and table.tableComment != ''">
            and  table_comment like  concat('%', #{table.tableComment}, '%')
        </if>
        <if test="table.startTime != null and table.endTime != null"><!-- 开始时间检索 -->
            and create_time between #{table.startTime} and #{table.endTime}
        </if>
        <if test="list !=null and list.size!=0">
            and  table_name not in
            <foreach collection="list" item="name" separator="," open="(" close=")" >
                 #{name}
            </foreach>
        </if>
        limit #{page.startNum},#{page.endNum}
    </select>

    <select id="selectListCount" resultType="java.lang.Long">
        select count(1)
        from information_schema.tables
        where table_schema = (select database())
        and table_name NOT LIKE 'gen_%'
        <if test="table.tableName != null and table.tableName != ''">
            and  table_name like  concat('%', #{table.tableName}, '%')
        </if>
        <if test="table.tableComment != null and table.tableComment != ''">
            and  table_comment like  concat('%', #{table.tableComment}, '%')
        </if>
        <if test="table.startTime != null and table.endTime != null"><!-- 开始时间检索 -->
            and create_time between #{table.startTime} and #{table.endTime}
        </if>
        <if test="list !=null and list.size!=0">
            and  table_name not in
            <foreach collection="list" item="name" separator="," open="(" close=")" >
                 #{name}
            </foreach>
        </if>
    </select>

    <!-- and table_name NOT LIKE 'gen_%' -->
    <select id="selectListByNameList" resultMap="cn.fateverse.code.mapper.dynamic.DynamicTableMapper.TableResult">
        select table_name, table_comment, create_time, update_time, 'MYSQL' as data_source_type
        from information_schema.tables
        where table_name not like 'gen_%'
        and table_schema = (select database())
        and table_name in
        <foreach collection="list" item="name" open="(" separator="," close=")">
            #{name}
        </foreach>
    </select>

    <select id="selectColumnsByNameList" resultMap="cn.fateverse.code.mapper.dynamic.DynamicTableMapper.TableColumnResult">
        select table_name as dict_type,column_name, (case when (is_nullable = 'no' <![CDATA[ && ]]> column_key != 'PRI') then '1' else null end) as is_required, (case when column_key = 'PRI' then '1' else '0' end) as is_pk, ordinal_position as sort, column_comment, (case when extra = 'auto_increment' then '1' else '0' end) as is_increment, column_type
        from information_schema.columns
        where table_schema = (select database()) and table_name in
        <foreach collection="list" item="name" open="(" separator="," close=")">
            #{name}
        </foreach>
        order by ordinal_position
    </select>


</mapper>
