<?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="flowExternalDbConnection">

  <!--
    flow_external_db_connection 은 시스템 전역 테이블 (company_code 없음)
    map-underscore-to-camel-case: true → snake_case 컬럼이 자동으로 camelCase 키로 매핑됨
  -->

  <select id="selectAll" parameterType="map" resultType="map">
    SELECT
        ID
        , NAME
        , DESCRIPTION
        , DB_TYPE
        , HOST
        , PORT
        , DATABASE_NAME
        , USERNAME
        , PASSWORD_ENCRYPTED
        , SSL_ENABLED
        , CONNECTION_OPTIONS
        , IS_ACTIVE
        , CREATED_BY
        , UPDATED_BY
        , CREATED_DATE
        , UPDATED_DATE
    FROM FLOW_EXTERNAL_DB_CONNECTION
    <where>
      <if test="active_only == true">
        IS_ACTIVE = TRUE
      </if>
    </where>
    ORDER BY NAME ASC
  </select>

  <select id="selectById" parameterType="map" resultType="map">
    SELECT
        ID
        , NAME
        , DESCRIPTION
        , DB_TYPE
        , HOST
        , PORT
        , DATABASE_NAME
        , USERNAME
        , PASSWORD_ENCRYPTED
        , SSL_ENABLED
        , CONNECTION_OPTIONS
        , IS_ACTIVE
        , CREATED_BY
        , UPDATED_BY
        , CREATED_DATE
        , UPDATED_DATE
    FROM FLOW_EXTERNAL_DB_CONNECTION
    WHERE ID = #{id}
  </select>

  <insert id="insert" parameterType="map" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO FLOW_EXTERNAL_DB_CONNECTION (
        NAME
        , DESCRIPTION
        , DB_TYPE
        , HOST
        , PORT
        , DATABASE_NAME
        , USERNAME
        , PASSWORD_ENCRYPTED
        , SSL_ENABLED
        , CONNECTION_OPTIONS
        , CREATED_BY
        , UPDATED_BY
    ) VALUES (
        #{name}
        , #{description}
        , #{db_type}
        , #{host}
        , #{port}
        , #{database_name}
        , #{username}
        , #{password_encrypted}
        , #{ssl_enabled}
        , #{connection_options}::JSONB
        , #{created_by}
        , #{updated_by}
    )
  </insert>

  <!-- 동적 UPDATE: 요청에 포함된 필드만 수정 -->
  <update id="update" parameterType="map">
    UPDATE FLOW_EXTERNAL_DB_CONNECTION
    <set>
      <if test="name != null">NAME = #{name},</if>
      <if test="description != null">DESCRIPTION = #{description},</if>
      <if test="host != null">HOST = #{host},</if>
      <if test="port != null">PORT = #{port},</if>
      <if test="database_name != null">DATABASE_NAME = #{database_name},</if>
      <if test="username != null">USERNAME = #{username},</if>
      <if test="password_encrypted != null">PASSWORD_ENCRYPTED = #{password_encrypted},</if>
      <if test="ssl_enabled != null">SSL_ENABLED = #{ssl_enabled},</if>
      <if test="connection_options != null">CONNECTION_OPTIONS = #{connection_options}::JSONB,</if>
      <if test="is_active != null">IS_ACTIVE = #{is_active},</if>
      UPDATED_BY = #{updated_by},
      UPDATED_DATE = NOW()
    </set>
    WHERE ID = #{id}
  </update>

  <delete id="delete" parameterType="map">
    DELETE FROM FLOW_EXTERNAL_DB_CONNECTION
    WHERE ID = #{id}
  </delete>

</mapper>
