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

    <resultMap type="com.salpa.subject.domain.MonitorDataSource" id="MonitorDatabaseTypeResult">
        <id     property="id"            column="id"             />
        <id     property="projectId"            column="project_id"             />
        <result property="databaseName"          column="database_name"           />
        <result property="schemaName"   column="schema_name"    />
        <result property="databaseType"              column="database_type"                />
        <result property="url"             column="url"              />
        <result property="username"             column="username"              />
        <result property="password"             column="password"              />
        <result property="updateAt"             column="update_at"              />
        <result property="createAt"             column="create_at"              />
    </resultMap>

    <select id="selectDataSourceById" resultMap="MonitorDatabaseTypeResult">
        SELECT id,database_name,schema_name,database_type,url,username,password
        FROM monitor_data_source WHERE project_id = #{id}
    </select>

    <select id="selectByDatabaseName" resultMap="MonitorDatabaseTypeResult">
        SELECT id,database_name,schema_name,database_type,url,username,password
        FROM monitor_data_source WHERE database_name = #{databaseName}
    </select>

    <select id="selectDataSourceList" resultMap="MonitorDatabaseTypeResult">
        SELECT id,database_name,schema_name,database_type,url,username,password
        FROM monitor_data_source
    </select>

    <insert id="addDatabaseSource" parameterType="MonitorDataSource" useGeneratedKeys="true" keyProperty="id">
        insert into monitor_data_source(
        <if test="projectId != null">project_id,</if>
        <if test="databaseName != null and databaseName != ''">database_name,</if>
        <if test="schemaName != null and schemaName != ''">schema_name,</if>
        <if test="databaseType != null and databaseType != ''">database_type,</if>
        <if test="url != null and url != ''">url,</if>
        <if test="password != null and password != ''">password,</if>
        <if test="username != null and username != ''">username</if>
        )values(
        <if test="projectId != null">#{projectId},</if>
        <if test="databaseName != null and databaseName != ''">#{databaseName},</if>
        <if test="schemaName != null and schemaName != ''">#{schemaName},</if>
        <if test="databaseType != null and databaseType != ''">#{databaseType},</if>
        <if test="url != null and url != ''">#{url},</if>
        <if test="password != null and password != ''">#{password},</if>
        <if test="username != null and username != ''">#{username}</if>
        )
    </insert>

    <update id="updateDateSource">
        update monitor_data_source
        <set>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="databaseName != null and databaseName != ''">database_name = #{databaseName},</if>
            <if test="schemaName != null and schemaName != ''">schema_name = #{schemaName},</if>
            <if test="databaseType != null and databaseType != ''">database_type = #{databaseType},</if>
            <if test="url != null and url != ''">url = #{url},</if>
            <if test="password != null and password != ''">password = #{password},</if>
            <if test="username != null and username != ''">username = #{username}</if>
        </set>
        where id = #{id}
    </update>

    <delete id="deleteDataSource">
        DELETE FROM monitor_data_source WHERE project_id = #{projectId}
    </delete>
</mapper>