<?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.ruoyi.system.mapper.TableInfoMapper">

    <resultMap id="mysqlTableInfoResult" type="com.ruoyi.system.entity.TableInfo">
        <result property="fieldName" column="fieldName"></result>
        <result property="fieldType" column="fieldType"></result>
        <result property="isNull" column="isNull" javaType="boolean"></result>
        <result property="isPrimary" column="isPrimary" javaType="boolean"></result>
        <result property="fieldDescription" column="fieldDescription"></result>
        <result property="fieldLength" column="fieldLength"></result>
        <result property="isAuto" column="isAuto" javaType="boolean"></result>
    </resultMap>

    <select id="tableInfoList" resultType="com.ruoyi.system.entity.vo.TableInfoVO">
        select table_name tableName,create_time createTime,table_comment tableComment from information_schema.tables
        where table_schema=#{databaseName}
        <if test="map.tableName != null and map.tableName != ''">and table_name like concat('%', #{map.tableName},
            '%')
        </if>
        <if test="map.tableComment != null and map.tableComment !=''">and table_comment like concat('%',
            #{map.tableComment}, '%')
        </if>
        order by createTime asc
    </select>

    <select id="sqlServerTableInfoList" resultType="com.ruoyi.system.entity.vo.TableInfoVO">
        select
        a.name AS tableName,
        a.create_date AS createTime,
        CONVERT ( NVARCHAR ( 100 ), isnull( g.[value], '' ) ) AS tableComment
        from ${databaseName}.sys.tables a
        LEFT JOIN sys.extended_properties g ON ( a.object_id = g.major_id AND g.minor_id = 0 )
        where a.type = 'U'
        <if test="map.tableName != null and map.tableName != ''">and a.name like concat('%', #{map.tableName}, '%')</if>
        <if test="map.tableComment != null and map.tableComment !=''">and CONVERT ( NVARCHAR ( 100 ), isnull( g.[value],
            '' ) ) like concat('%', #{map.tableComment}, '%')
        </if>
        order by createTime asc
    </select>


    <select id="dmTableInfoList" resultType="com.ruoyi.system.entity.vo.TableInfoVO">
        select a.object_name as tableName,
        b.comment$ as tableComment,
        a.created as createTime
        from dba_objects a
        left join SYSTABLECOMMENTS b
        on b.tvname = a.object_name and a.owner = b.schname
        where a.object_type = 'TABLE'
        and a.owner = #{databaseName}
        <if test="map.tableName != null and map.tableName != ''">and a.object_name like concat('%', #{map.tableName},
            '%')
        </if>
        <if test="map.tableComment != null and map.tableComment !=''">and b.comment$ like concat('%',
            #{map.tableComment}, '%')

        </if>
        order by createTime asc
    </select>

    <select id="mysqlTableFieldInfo" resultMap="mysqlTableInfoResult">
        SELECT column_name                               fieldName,
               IF
                   (is_nullable = 'NO', TRUE, FALSE)     isNull,
               data_type                                 fieldType,
               character_maximum_length                  fieldLength,
               IF
                   (column_key = 'PRI', TRUE, FALSE)     isPrimary,
               column_comment                            fieldDescription,
               if(extra = 'auto_increment', true, false) isAuto
        FROM information_schema.COLUMNS
        WHERE table_name = #{tableName}
          AND table_schema = #{dataBaseName}
        ORDER BY ordinal_position asc;

    </select>
    <select id="sqlserverTableFieldInfo" resultMap="mysqlTableInfoResult">
        SELECT fieldName = a.name,
               isAuto = case when COLUMNPROPERTY(a.id, a.name, 'IsIdentity') = 1 then 1 else 0 end,
               isPrimary = case
                               when exists(SELECT 1
                                           FROM sysobjects
                                           where xtype = 'PK'
                                             and parent_obj = a.id
                                             and name in (SELECT name
                                                          FROM sysindexes
                                                          WHERE indid in
                                                                (SELECT indid FROM sysindexkeys WHERE id = a.id AND colid = a.colid)))
                                   then 1
                               else 0 end,
               fieldType = b.name,
               fieldLength = COLUMNPROPERTY(a.id, a.name, 'PRECISION'),
               isNull = case when a.isnullable = 1 then 0 else 1 end,

               fieldDescription = isnull(g.[value], '')
        FROM syscolumns a
                 left join
             systypes b
             on
                 a.xusertype = b.xusertype
                 inner join
             sysobjects d
             on
                 a.id = d.id and d.xtype = 'U' and d.name<![CDATA[ <>]]> 'dtproperties'
                 left join
             syscomments e
             on
                 a.cdefault = e.id
                 left join
             sys.extended_properties g
             on
                 a.id = G.major_id and a.colid = g.minor_id
                 left join
             sys.extended_properties f
             on
                 d.id = f.major_id and f.minor_id = 0
        where d.name = #{tableName}
        order by a.id, a.colorder
    </select>


    <select id="dmTableFieldInfo" resultMap="mysqlTableInfoResult">
        SELECT a.table_name                                        As tableName
             , c.comments                                          As tableComment
             , a.column_name                                       As fieldName
             , b.comments                                          As fieldDescription
             , a.data_type                                         As fieldType
             , a.data_length                                       As fieldLength
             , (case when d.column_position = 1 then 1 else 0 end) As isPrimary
             , (case when (e.nullable = 'N') then 1 else 0 end)    as isNull
             , (case when f.info2 = 1 then 1 else 0 end)           as isAuto
        FROM all_tab_cols a
                 LEFT JOIN all_col_comments b
                           ON b.table_name = a.table_name AND b.column_name = a.column_name AND a.owner = b.schema_name
                 LEFT JOIN all_tab_comments c ON c.table_name = b.table_name AND c.owner = b.owner
                 LEFT JOIN (SELECT dcc.table_name,
                                   dcc.column_name,
                                   dcc.column_position,
                                   dcc.index_owner,
                                   dc.constraint_name
                            FROM all_ind_columns dcc
                                     JOIN all_constraints dc
                                          ON dcc.index_name = dc.index_name AND dc.constraint_type = UPPER('p') AND
                                             dcc.index_owner = dc.owner) d
                           ON d.table_name = a.table_name AND d.column_name = a.column_name AND d.index_owner = a.owner
                 LEFT JOIN all_tab_columns e
                           ON a.table_name = e.table_name AND a.owner = e.owner and a.column_name = e.column_name
                 LEFT JOIN (SELECT f1.owner, f1.object_name As table_name, f0.name, f0.info2
                            FROM syscolumns f0
                                     INNER JOIN dba_objects f1
                                                ON f1.object_type = 'TABLE' AND info2 = 1 AND f1.object_id = f0.id) f
                           ON f.name = a.column_name AND f.table_name = a.table_name AND f.owner = a.owner
        WHERE a.owner = UPPER(#{dataBaseName})
          and a.table_name = #{tableName}
        ORDER BY a.table_name ASC, a.column_id ASC;
    </select>

</mapper>
