<?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.cm.common.plugin.dao.dynamic.IDynamicTableDao">

    <resultMap id="tableDTO" type="com.cm.common.plugin.pojo.dtos.database.table.TableDTO">
        <result column="tableSchema" property="tableSchema"/>
        <result column="tableName" property="tableName"/>
    </resultMap>

    <resultMap id="tableColumnDTO" type="com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO">
        <result column="columnName" property="columnName"/>
        <result column="columnDefault" property="columnDefault"/>
        <result column="isNullable" property="isNullable"/>
        <result column="dataType" property="dataType"/>
        <result column="characterMaximumLength" property="characterMaximumLength"/>
        <result column="numericPrecision" property="numericPrecision"/>
        <result column="numericScale" property="numericScale"/>
        <result column="datetimePrecision" property="datetimePrecision"/>
        <result column="columnType" property="columnType"/>
        <result column="columnKey" property="columnKey"/>
        <result column="columnComment" property="columnComment"/>
        <result column="extra" property="extra"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable" parameterType="string">
        ${_parameter}
    </update>

    <!-- 加列 -->
    <update id="saveTableColumn" parameterType="string">
        ${_parameter}
    </update>

    <!-- 修改列属性 -->
    <update id="updateTableColumnType" parameterType="string">
        ${_parameter}
    </update>

    <!-- 删列 -->
    <update id="deleteTableColumnType" parameterType="string">
        ${_parameter}
    </update>

    <!-- 通过表名获取表 -->
    <select id="getTableByTableName" parameterType="string" resultMap="tableDTO">
        SELECT
            TABLE_SCHEMA tableSchema,
            TABLE_NAME tableName
        FROM
            `information_schema`.`TABLES`
        WHERE
            `TABLE_SCHEMA` = (SELECT database())
        AND
            `TABLE_NAME` = #{_parameter}
    </select>

    <!-- 表详情 -->
    <select id="listTableColumns" parameterType="string" resultMap="tableColumnDTO">
        SELECT
            COLUMN_NAME columnName,
            COLUMN_DEFAULT columnDefault,
            IS_NULLABLE isNullable,
            DATA_TYPE dataType,
            CHARACTER_MAXIMUM_LENGTH characterMaximumLength,
            NUMERIC_PRECISION numericPrecision,
            NUMERIC_SCALE numericScale,
            DATETIME_PRECISION datetimePrecision,
            COLUMN_TYPE columnType,
            COLUMN_KEY columnKey,
            COLUMN_COMMENT columnComment,
            EXTRA extra
        FROM
            `information_schema`.`COLUMNS`
        WHERE
            `TABLE_SCHEMA`= (SELECT database())
        AND
            `TABLE_NAME`= #{_parameter}
    </select>

</mapper>