<?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.lframework.xingyun.template.gen.mappers.GenSimpleTableColumnMapper">

    <resultMap id="OriSimpleTableColumnDto" type="com.lframework.xingyun.template.gen.dto.simpledb.OriSimpleTableColumnDto">
        <id column="id" property="id"/>
        <result column="table_id" property="tableId"/>
        <result column="column_name" property="columnName"/>
        <result column="data_type" property="dataType"/>
        <result column="is_nullable" property="isNullable"/>
        <result column="is_key" property="isKey"/>
        <result column="column_default" property="columnDefault"/>
        <result column="ordinal_position" property="ordinalPosition"/>
        <result column="column_comment" property="columnComment"/>
        <result column="len" property="len"/>
        <result column="decimals" property="decimals"/>
    </resultMap>

    <select id="query" resultMap="OriSimpleTableColumnDto">
        SELECT
            c.COLUMN_NAME AS column_name,
            c.DATA_TYPE AS data_type,
            c.IS_NULLABLE = 'YES' AS is_nullable,
            c.COLUMN_DEFAULT AS column_default,
            c.ORDINAL_POSITION AS ordinal_position,
            u.CONSTRAINT_NAME IS NOT NULL AS is_key,
            c.COLUMN_COMMENT AS column_comment,
            (CASE
            WHEN c.CHARACTER_MAXIMUM_LENGTH IS NULL THEN c.NUMERIC_PRECISION
            ELSE c.CHARACTER_MAXIMUM_LENGTH END) AS len,
            (CASE
            WHEN c.CHARACTER_MAXIMUM_LENGTH IS NULL THEN c.NUMERIC_SCALE
            ELSE c.NUMERIC_SCALE END) AS decimals
        FROM information_schema.COLUMNS AS c
        LEFT JOIN information_schema.KEY_COLUMN_USAGE AS u ON u.TABLE_SCHEMA = c.TABLE_SCHEMA AND u.TABLE_NAME = c.TABLE_NAME AND u.COLUMN_NAME = c.COLUMN_NAME AND u.CONSTRAINT_NAME = 'PRIMARY'
        <where>
            <if test="tableSchema != null and tableSchema != ''">
                AND c.TABLE_SCHEMA = #{tableSchema}
            </if>
            AND c.TABLE_NAME = #{tableName}
        </where>
        ORDER BY c.ORDINAL_POSITION
    </select>
</mapper>
