<?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.gen.mapper.GenTableColumnMapper">

	<resultMap type="com.ruoyi.gen.domain.GenTableColumn" id="GenTableColumnResult">
		<id     property="columnId"       column="column_id"      />
		<result property="tableId"        column="table_id"       />
		<result property="columnName"     column="column_name"    />
		<result property="columnComment"  column="column_comment" />
		<result property="columnType"     column="column_type"    />
		<result property="javaType"       column="java_type"      />
		<result property="javaField"      column="java_field"     />
		<result property="isPk"           column="is_pk"          />
		<result property="isIncrement"    column="is_increment"   />
		<result property="isRequired"     column="is_required"    />
		<result property="isInsert"       column="is_insert"      />
		<result property="isEdit"         column="is_edit"        />
		<result property="isList"         column="is_list"        />
		<result property="isQuery"        column="is_query"       />
		<result property="queryType"      column="query_type"     />
		<result property="htmlType"       column="html_type"      />
		<result property="dictType"       column="dict_type"      />
		<result property="sort"           column="sort"           />
		<result property="createBy"       column="create_by"      />
		<result property="createTime"     column="create_time"    />
		<result property="updateBy"       column="update_by"      />
		<result property="updateTime"     column="update_time"    />
	</resultMap>

	<select id="selectTableColumnListByTableId" resultMap="GenTableColumnResult">
		SELECT column_id, table_id, column_name, column_comment, column_type, java_type, java_field, is_pk, is_increment, is_required, is_insert, is_edit, is_list, is_query, query_type, html_type, dict_type, sort, create_by, create_time, update_by, update_time
        FROM gen_table_column
		WHERE del_flag = #{delFlag}
		AND table_id = #{tableId}
		ORDER BY sort
	</select>

	<select id="selectTableColumnsByName" parameterType="string" resultMap="GenTableColumnResult">
		SELECT
			column_name,
			(CASE WHEN (is_nullable = 'no' <![CDATA[ && ]]> column_key != 'PRI') THEN '1' ELSE null END) AS is_required,
			(CASE WHEN column_key = 'PRI' THEN '1' ELSE '0' END) AS is_pk,
			ordinal_position AS sort,
			column_comment,
			(CASE WHEN extra = 'auto_increment' THEN '1' ELSE '0' END) AS is_increment,
			column_type
		FROM information_schema.columns
		WHERE table_schema = (SELECT database())
		AND table_name = (#{tableName})
		ORDER BY ordinal_position
	</select>

	<delete id="deleteGenTableColumnByIds">
		DELETE FROM gen_table_column
		WHERE table_id IN
		<foreach collection="array" item="tableId" open="(" separator="," close=")">
			#{tableId}
		</foreach>
	</delete>

	<delete id="deleteGenTableColumns">
		DELETE FROM gen_table_column
		WHERE column_id IN
		<foreach collection="list" item="item" open="(" separator="," close=")">
			#{item.columnId}
		</foreach>
	</delete>

</mapper>