<?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.DatabaseTypeMapper">

    <resultMap type="com.salpa.subject.domain.MonitorDatabaseType" id="MonitorDatabaseTypeResult">
        <id     property="id"            column="id"             />
        <result property="databaseType"          column="database_type"           />
        <result property="icon"   column="icon"    />
        <result property="description"              column="description"                />
        <result property="jdbcDriverFileUrl"             column="jdbc_driver_file_url"              />
        <result property="jdbcDriverFilePath"             column="jdbc_driver_file_path"              />
        <result property="jdbcDriverClassName"             column="jdbc_driver_class_name"              />
        <result property="jdbcProtocol"             column="jdbc_protocol"              />
        <result property="urlPattern"             column="url_pattern"              />
        <result property="deleted"             column="deleted"              />
        <result property="deletedToken"             column="deleted_token"              />
        <result property="updateAt"             column="update_at"              />
        <result property="createAt"             column="create_at"              />
    </resultMap>

    <select id="getDatabaseTypeAll" resultMap="MonitorDatabaseTypeResult">
        SELECT database_type,icon
        FROM monitor_database_type WHERE deleted = 0
    </select>

    <select id="getList" resultMap="MonitorDatabaseTypeResult">
        SELECT id,database_type,icon,description,jdbc_driver_file_url,jdbc_driver_file_path,jdbc_driver_class_name,jdbc_protocol,url_pattern,deleted,deleted_token,update_at,create_at
        FROM monitor_database_type WHERE deleted = 0
        <if test="databaseType != null and databaseType != ''">
            AND database_type like concat('%', #{databaseType}, '%')
        </if>
    </select>

    <select id="getById" resultMap="MonitorDatabaseTypeResult">
        SELECT id,database_type,icon,description,jdbc_driver_file_url,jdbc_driver_file_path,jdbc_driver_class_name,jdbc_protocol,url_pattern,deleted,deleted_token,update_at,create_at
        FROM monitor_database_type WHERE deleted = 0 AND id = #{id}
    </select>

    <select id="getTypeByName" resultMap="MonitorDatabaseTypeResult">
        SELECT id,database_type,icon,description,jdbc_driver_file_url,jdbc_driver_file_path,jdbc_driver_class_name,jdbc_protocol,url_pattern,deleted,deleted_token,update_at,create_at
        FROM monitor_database_type WHERE database_type = #{databaseType} AND deleted = 0
    </select>

    <select id="checkDatabaseTypeUnique" parameterType="String" resultType="int">
        SELECT count(1) from monitor_database_type where database_type = #{databaseType} and deleted = 0 limit 1
    </select>

    <select id="countDataSource" resultType="int">
        SELECT count(1) from monitor_data_source WHERE database_type = #{databaseType}
    </select>

    <insert id="addDatabaseType" parameterType="MonitorDatabaseType">
        insert into monitor_database_type(
        <if test="databaseType != null and databaseType != ''">database_type,</if>
        <if test="icon != null and icon != ''">icon,</if>
        <if test="description != null and description != ''">description,</if>
        <if test="jdbcDriverFileUrl != null and jdbcDriverFileUrl != ''">jdbc_driver_file_url,</if>
        <if test="jdbcDriverFilePath != null and jdbcDriverFilePath != ''">jdbc_driver_file_path,</if>
        <if test="jdbcDriverClassName != null and jdbcDriverClassName != ''">jdbc_driver_class_name,</if>
        <if test="jdbcProtocol != null and jdbcProtocol != ''">jdbc_protocol,</if>
        <if test="urlPattern != null and urlPattern != ''">url_pattern</if>
        )values(
        <if test="databaseType != null and databaseType != ''">#{databaseType},</if>
        <if test="icon != null and icon != ''">#{icon},</if>
        <if test="description != null and description != ''">#{description},</if>
        <if test="jdbcDriverFileUrl != null and jdbcDriverFileUrl != ''">#{jdbcDriverFileUrl},</if>
        <if test="jdbcDriverFilePath != null and jdbcDriverFilePath != ''">#{jdbcDriverFilePath},</if>
        <if test="jdbcDriverClassName != null and jdbcDriverClassName != ''">#{jdbcDriverClassName},</if>
        <if test="jdbcProtocol != null and jdbcProtocol != ''">#{jdbcProtocol},</if>
        <if test="urlPattern != null and urlPattern != ''">#{urlPattern}</if>
        )
    </insert>

    <update id="updateDatabaseType">
        UPDATE monitor_database_type
        <set>
            <if test="databaseType != null and databaseType != ''">database_type = #{databaseType},</if>
            icon = #{icon},
            <if test="description != null and description != ''">description = #{description},</if>
            jdbc_driver_file_url = #{jdbcDriverFileUrl},
            jdbc_driver_file_path = #{jdbcDriverFilePath},
            <if test="jdbcDriverClassName != null and jdbcDriverClassName != ''">jdbc_driver_class_name = #{jdbcDriverClassName},</if>
            <if test="jdbcProtocol != null and jdbcProtocol != ''">jdbc_protocol = #{jdbcProtocol},</if>
            <if test="urlPattern != null and urlPattern != ''">url_pattern = #{urlPattern}</if>
        </set>
        WHERE id = #{id}
    </update>

    <update id="deleteDatabaseType">
        UPDATE monitor_database_type SET deleted = 1 WHERE id = #{id}
    </update>
</mapper>