<?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.query.mapper.dynamic.PostgresDynamicDataSourceMapper">


    <sql id="uniQueryWhere">
        <where>
            1 = 1
            <foreach collection="select.uniCons" item="uniCon">
                <if test="uniCon.ucMock != null and uniCon.ucMock != '' and uniCon.ucType == 'input'">
                    <if test="uniCon.ucCon == 'EQ'">and ${uniCon.ucKey} = #{uniCon.ucMock}</if>
                    <if test="uniCon.ucCon == 'NE'">and ${uniCon.ucKey} != #{uniCon.ucMock}</if>
                    <if test="uniCon.ucCon == 'GT'">and ${uniCon.ucKey} > #{uniCon.ucMock}</if>
                    <if test="uniCon.ucCon == 'GTE'">and ${uniCon.ucKey} >= #{uniCon.ucMock}</if>
                    <if test="uniCon.ucCon == 'LT'">and ${uniCon.ucKey} &lt; #{uniCon.ucMock}</if>
                    <if test="uniCon.ucCon == 'LTE'">and ${uniCon.ucKey} &lt;= #{uniCon.ucMock}</if>
                    <if test="uniCon.ucCon == 'LIKE'">and ${uniCon.ucKey} like concat('%', #{uniCon.ucMock}, '%')</if>
                </if>
                <if test="uniCon.ucMock != null and uniCon.ucType != 'input'">
                    <if test="uniCon.ucCon == 'EQ'">and ${uniCon.ucKey} = #{uniCon.ucMock}</if>
                    <if test="uniCon.ucCon == 'NE'">and ${uniCon.ucKey} != #{uniCon.ucMock}</if>
                    <if test="uniCon.ucCon == 'GT'">and ${uniCon.ucKey} > #{uniCon.ucMock}</if>
                    <if test="uniCon.ucCon == 'GTE'">and ${uniCon.ucKey} >= #{uniCon.ucMock}</if>
                    <if test="uniCon.ucCon == 'LT'">and ${uniCon.ucKey} &lt; #{uniCon.ucMock}</if>
                    <if test="uniCon.ucCon == 'LTE'">and ${uniCon.ucKey} &lt;= #{uniCon.ucMock}</if>
                    <if test="uniCon.ucCon == 'LIKE'">and ${uniCon.ucKey} like concat('%', #{uniCon.ucMock}, '%')</if>
                    <if test="uniCon.ucCon == 'BETWEEN'">and ${uniCon.ucKey} between #{uniCon.begin} and #{uniCon.end}
                    </if>
                </if>
            </foreach>
        </where>
    </sql>

    <select id="checkSource" resultType="java.lang.Integer">
        select 1 as num;
    </select>

    <select id="uniSingleQueryTopology" resultType="java.util.Map">
        select ${select.selectKey} from ${select.tableInfo}
        <include refid="uniQueryWhere"/>
        limit #{page.startNum} offset #{page.endNum}
    </select>

    <select id="uniSingleQueryTopologyCount" resultType="java.lang.Long">
        select count(1) from ${select.tableInfo}
        <include refid="uniQueryWhere"/>
    </select>

    <select id="uniMultiListQueryTopology" resultType="java.util.Map">
        select ${select.selectKey} from ${select.tableInfo}
        <foreach collection="select.relevance" item="item">
            ${item}
        </foreach>
        <include refid="uniQueryWhere"/>
        limit #{page.startNum} offset #{page.endNum}
    </select>

    <select id="uniMultiListQueryTopologyCount" resultType="java.lang.Long">
        select count(1) from ${select.tableInfo}
        <foreach collection="select.relevance" item="item">
            ${item}
        </foreach>
        <include refid="uniQueryWhere"/>
    </select>


    <select id="selectTableList"
            resultMap="cn.fateverse.query.mapper.DynamicDataSourceMapper.DynamicTableResult">
        select
            relname as table_name,
            obj_description(oid) as table_comment
        from pg_class
        where relkind = 'r'
            and obj_description(relnamespace) like concat('%',#{params.namespace},'%')
        <if test="table.tableName != null and table.tableName != ''">
            and  relname like  concat('%', #{table.tableName}, '%')
        </if>
        <if test="table.tableComment != null and table.tableComment != ''">
            and  obj_description(oid) like  concat('%', #{table.tableComment}, '%')
        </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} offset #{page.endNum}
    </select>

    <select id="selectTableListCount" resultType="java.lang.Long">
        select count(1)
        from pg_class
        where relkind = 'r'
        and obj_description(relnamespace) like concat('%',#{params.namespace},'%')
        <if test="table.tableName != null and table.tableName != ''">
            and  relname) like  concat('%', #{table.tableName}, '%')
        </if>
        <if test="table.tableComment != null and table.tableComment != ''">
            and  obj_description(oid) like  concat('%', #{table.tableComment}, '%')
        </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>

    <select id="selectDynamicTableListByNameList"
            resultMap="cn.fateverse.query.mapper.DynamicDataSourceMapper.TableResult">
        select
            relname as table_name,
            obj_description(oid) as table_comment,
            'POSTGRES' as data_source_type
        from pg_class
        where relkind = 'r'
            and obj_description(relnamespace) like concat('%',#{params.namespace},'%')
            and relname in
        <foreach collection="list" item="name" open="(" separator="," close=")">
            #{name}
        </foreach>
    </select>

    <select id="selectDynamicTableColumnsByNameList"
            resultMap="cn.fateverse.query.mapper.DynamicDataSourceMapper.TableColumnResult">
        select
            c.relname as remark,
            a.attname as column_name,
            d.description as column_comment,
            concat_ws ('', t.typname,SUBSTRING (format_type (a.atttypid,a.atttypmod) from'\(.*\)')) as column_type,
            a.attnum as sort,
            (case when typstorage = 'p' then 1 else 0 end ) as pk
        from
            pg_class c,
            pg_attribute a,
            pg_type t,
            pg_description d
        where a.attnum > 0
            and a.attrelid = c.oid
            and a.atttypid = t.oid
            and d.objoid = a.attrelid
            and d.objsubid = a.attnum
            and obj_description(relnamespace) like concat('%',#{params.namespace},'%') and c.relname in
        <foreach collection="list" item="name" open="(" separator="," close=")">
            #{name}
        </foreach>
        order by sort
    </select>

</mapper>