<?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.OracleDynamicTableMapper">

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

    <select id="selectList" resultMap="cn.fateverse.code.mapper.dynamic.DynamicTableMapper.DynamicTableResult">
        select ut.table_name,utc.comments as table_comment,ut.last_analyzed as update_time
        from user_tables ut
        left join user_tab_comments utc on ut.table_name = utc.table_name
        where rownum between #{page.startNum} and #{page.endNum}
        <if test="table.tableName != null and table.tableName != ''">
            and ut.table_name like concat(concat('%', #{table.tableName}), '%')
        </if>
        <if test="table.tableComment != null and table.tableComment != ''">
            and utc.comments like concat(concat('%', #{table.tableComment}), '%')
        </if>
        <if test="table.startTime != null and table.endTime != null"><!-- 开始时间检索 -->
            and update_time between #{table.startTime} and #{table.endTime}
        </if>
        <if test="list !=null and list.size!=0">
            and ut.table_name not in
            <foreach collection="list" item="name" separator="," open="(" close=")" >
                #{name}
            </foreach>
        </if>

    </select>

    <select id="selectListCount" resultType="java.lang.Long">
        select count(1)
        from user_tables ut
        left join user_tab_comments utc on ut.table_name = utc.table_name
        <where>
            <if test="table.tableName != null and table.tableName != ''">
                and ut.table_name like concat(concat('%', #{table.tableName}), '%')
            </if>
            <if test="table.tableComment != null and table.tableComment != ''">
                and utc.comments like concat(concat('%', #{table.tableComment}), '%')
            </if>
            <if test="table.startTime != null and table.endTime != null"><!-- 开始时间检索 -->
                and ut.last_analyzed between #{table.startTime} and #{table.endTime}
            </if>
            <if test="list !=null and list.size!=0">
                and ut.table_name not in
                <foreach collection="list" item="name" separator="," open="(" close=")" >
                    #{name}
                </foreach>
            </if>
        </where>
    </select>

    <!-- and table_name NOT LIKE 'gen_%' -->
    <select id="selectListByNameList" resultMap="cn.fateverse.code.mapper.dynamic.DynamicTableMapper.TableResult">
        select ut.table_name,utc.comments as table_comment,ut.last_analyzed as update_time,'ORACLE' as data_source_type
        from user_tables ut
        left join user_tab_comments utc on ut.table_name = utc.table_name
        where ut.table_name in
        <foreach collection="list" item="name" separator="," open="(" close=")" >
            #{name}
        </foreach>
    </select>

    <select id="selectColumnsByNameList" resultMap="cn.fateverse.code.mapper.dynamic.DynamicTableMapper.TableColumnResult">
        select distinct utc.column_name,
            utc.table_name as dict_type,
            utc.data_type as column_type,
            utc.data_length as column_length,
            utc.data_scale as column_scale,
            (case when utc.nullable = 'N' then '1' else '0' end) as is_required ,
            utc.column_id as sort,
            (case when uc.constraint_type = 'P' then '1' else '0' end) as is_pk ,
            ucc.comments as column_comment
        from user_tab_columns utc
                 inner join user_col_comments ucc on ucc.column_name = utc.column_name and ucc.table_name = utc.table_name
                 left join user_cons_columns uccs on uccs.column_name = utc.column_name
                 left join user_constraints uc on uc.constraint_name = uccs.constraint_name
        where uc.search_condition is null
          and utc.table_name in
        <foreach collection="list" item="name" open="(" separator="," close=")">
            #{name}
        </foreach>
        order by utc.column_id
    </select>
</mapper>
