<?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.salpa.subject.mapper.TableForeignKeyDocumentMapper">

    <resultMap type="com.salpa.subject.domain.MonitorTableForeignKeyDocument" id="MonitorTableForeignKeyDocumentResult">
        <id     property="id"            column="id"             />
        <id     property="tableDocumentId"            column="table_document_id"             />
        <result property="databaseDocumentId"          column="database_document_id"           />
        <result property="keySeq"   column="key_seq"    />
        <result property="fkName"              column="fk_name"                />
        <result property="fkTableName"             column="fk_table_name"              />
        <result property="fkColumnName"             column="fk_column_name"              />
        <result property="pkName"             column="pk_name"              />
        <result property="pkTableName"             column="pk_table_name"              />
        <result property="pkColumnName"             column="pk_column_name"              />
        <result property="updateRule"             column="update_rule"              />
        <result property="deleteRule"             column="delete_rule"              />
        <result property="createAt"             column="create_at"              />
    </resultMap>

    <select id="getTableForeignKey" resultMap="MonitorTableForeignKeyDocumentResult">
        SELECT id,database_document_id,table_document_id,key_seq,fk_name,fk_table_name,fk_column_name,pk_name,pk_table_name,pk_column_name,update_rule,delete_rule,create_at
        FROM monitor_table_foreign_key_document WHERE database_document_id = #{id}
    </select>

    <select id="getTableForeignKeyByTableId" resultMap="MonitorTableForeignKeyDocumentResult">
        SELECT id,database_document_id,table_document_id,key_seq,fk_name,fk_table_name,fk_column_name,pk_name,pk_table_name,pk_column_name,update_rule,delete_rule,create_at
        FROM monitor_table_foreign_key_document WHERE table_document_id = #{id}
    </select>

    <insert id="insertTableForeignKey">
        insert into monitor_table_foreign_key_document(
        <if test="tableDocumentId != null">table_document_id,</if>
        <if test="keySeq != null and keySeq != ''">key_seq,</if>
        <if test="fkName != null and fkName != ''">fk_name,</if>
        <if test="fkTableName != null and fkTableName != ''">fk_table_name,</if>
        <if test="fkColumnName != null and fkColumnName != ''">fk_column_name,</if>
        <if test="pkName != null and pkName != ''">pk_name,</if>
        <if test="pkTableName != null and pkTableName != ''">pk_table_name,</if>
        <if test="pkColumnName != null and pkColumnName != ''">pk_column_name,</if>
        <if test="updateRule != null and updateRule != ''">update_rule,</if>
        <if test="deleteRule != null and deleteRule != ''">delete_rule,</if>
        <if test="databaseDocumentId != null">database_document_id</if>
        )values(
        <if test="tableDocumentId != null">#{tableDocumentId},</if>
        <if test="keySeq != null and keySeq != ''">#{keySeq},</if>
        <if test="fkName != null and fkName != ''">#{fkName},</if>
        <if test="fkTableName != null and fkTableName != ''">#{fkTableName},</if>
        <if test="fkColumnName != null and fkColumnName != ''">#{fkColumnName},</if>
        <if test="pkName != null and pkName != ''">#{pkName},</if>
        <if test="pkTableName != null and pkTableName != ''">#{pkTableName},</if>
        <if test="pkColumnName != null and pkColumnName != ''">#{pkColumnName},</if>
        <if test="updateRule != null and updateRule != ''">#{updateRule},</if>
        <if test="deleteRule != null and deleteRule != ''">#{deleteRule},</if>
        <if test="databaseDocumentId != null">#{databaseDocumentId}</if>
        )
    </insert>
</mapper>