<?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.qiaoba.module.generator.mapper.GeneratorTableMapper">

    <resultMap type="com.qiaoba.module.generator.entity.vo.DbTableVo" id="DbTableVoResult">
        <result property="tableName" column="table_name"/>
        <result property="tableComment" column="table_comment"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <select id="selectPageDbTableList" resultMap="DbTableVoResult">
        <if test="dto.dbType=='MySQL'.toString()">
            select table_name, table_comment, create_time, update_time from information_schema.tables
            where table_schema = (select database())
            AND table_name NOT LIKE 'qrtz_%' AND table_name NOT LIKE 'generator_%'
            AND table_name NOT IN (select table_name from generator_table)
            <if test="dto.tableName != null and dto.tableName != ''">
                AND lower(table_name) like lower(concat('%', #{dto.tableName}, '%'))
            </if>
            <if test="dto.tableComment != null and dto.tableComment != ''">
                AND lower(table_comment) like lower(concat('%', #{dto.tableComment}, '%'))
            </if>
            order by create_time desc
        </if>

        <if test="dto.dbType=='Oracle'.toString()">
            select lower(dt.table_name) as table_name, dtc.comments as table_comment, uo.created as create_time,
            uo.last_ddl_time as update_time
            from user_tables dt, user_tab_comments dtc, user_objects uo
            where dt.table_name = dtc.table_name
            and dt.table_name = uo.object_name
            and uo.object_type = 'TABLE'
            AND dt.table_name NOT LIKE 'XXL_JOB_%' AND dt.table_name NOT LIKE 'GEN_%'
            AND dt.table_name NOT IN (select table_name from generator_table)
            <if test="dto.tableName != null and dto.tableName != ''">
                AND lower(dt.table_name) like lower(concat(concat('%', #{dto.tableName}), '%'))
            </if>
            <if test="dto.tableComment != null and dto.tableComment != ''">
                AND lower(dtc.comments) like lower(concat(concat('%', #{dto.tableComment}), '%'))
            </if>
            order by create_time desc
        </if>

        <if test="dto.dbType=='PostgreSQL'.toString()">
            select table_name, table_comment, create_time, update_time
            from (
            SELECT c.relname AS table_name,
            obj_description(c.oid) AS table_comment,
            CURRENT_TIMESTAMP AS create_time,
            CURRENT_TIMESTAMP AS update_time
            FROM pg_class c
            LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
            WHERE (c.relkind = ANY (ARRAY ['r'::"char", 'p'::"char"]))
            AND c.relname != 'spatial_%'::text
            AND n.nspname = #{dto.schema}::name
            AND n.nspname <![CDATA[ <> ]]> ''::name
            ) list_table
            where table_name NOT LIKE 'xxl_job_%' AND table_name NOT LIKE 'gen_%'
            AND table_name NOT IN (select table_name from generator_table)
            <if test="dto.tableName != null and dto.tableName != ''">
                AND lower(table_name) like lower(concat('%', #{dto.tableName}, '%'))
            </if>
            <if test="dto.tableComment != null and dto.tableComment != ''">
                AND lower(table_comment) like lower(concat('%', #{dto.tableComment}, '%'))
            </if>
            order by create_time desc
        </if>
    </select>

</mapper>
