<?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.zhengqing.modules.code.mapper.DatabaseMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.zhengqing.modules.code.entity.Database">
        <id column="id" property="id"/>
        <result column="project_id" property="projectId"/>
        <result column="name" property="name"/>
        <result column="url" property="url"/>
        <result column="user" property="user"/>
        <result column="password" property="password"/>
        <result column="db_schema" property="dbSchema"/>
        <result column="db_type" property="dbType"/>
        <result column="driver" property="driver"/>
        <result column="gmt_create" property="gmtCreate" />
        <result column="gmt_modified" property="gmtModified" />
    </resultMap>

    <resultMap id="DatabaseView" type="com.zhengqing.modules.code.dto.output.DatabaseView" extends="BaseResultMap">
        <result column="project" property="project"/>
    </resultMap>

    <select id="selectDatabases" resultMap="DatabaseView">
        SELECT d.*,
        p.name AS 'project'
        FROM t_code_database d
        LEFT JOIN t_code_project p ON d.project_id = p.id
        WHERE 1 = 1
        <if test="filter.id!=null">
            AND d.id = #{filter.id}
        </if>
        <if test="filter.name!=null and filter.name!=''">
            AND d.name = #{filter.name}
        </if>
        <if test="filter.projectId!=null">
            AND d.project_id = #{filter.projectId}
        </if>
        <if test="filter.userId!=null and filter.userId!=''">
            AND	p.user_id = #{filter.userId}
        </if>
        ORDER BY d.id DESC
    </select>

    <select id="selectByName" resultMap="BaseResultMap">
        SELECT *
        FROM t_code_database
        WHERE name = #{dbName}
    </select>
</mapper>
