<?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="egovframework.com.mtdata.database.mapper.DatabaseMapper">

	<resultMap id="databaseInfoMap" type="egovframework.com.mtdata.database.vo.DatabaseInfoVO">
		<result property="sysId" column="SYS_ID"/>
		<result property="dbConnId" column="DB_CONN_ID"/>
		<result property="dbType" column="DB_TYPE"/>
		<result property="dbConnUser" column="DB_CONN_USER"/>
		<result property="dbConnPw" column="DB_CONN_PW"/>
		<result property="dbConnIp" column="DB_CONN_IP"/>
		<result property="dbConnPort" column="DB_CONN_PORT"/>
		<result property="registerId" column="REGISTER_ID"/>
		<result property="dbNm" column="DB_NM"/>
		<result property="useYn" column="USE_YN"/>
	</resultMap>

	<insert id="insertDbInfo" parameterType="DatabaseInfoVO">
		<selectKey resultType="int" keyProperty="dbLinkId" order="BEFORE">
			SELECT nextval('inst_db_info_db_link_id_seq') as dbLinkId
		</selectKey>
		INSERT INTO INST_DB_INFO
		(DB_LINK_ID,
		DB_CONN_ID,
		DB_NM)
		VALUES
		(#{dbLinkId},
		#{dbConnId},
		#{dbNm})
	</insert>

	<insert id="insertDbConnInfo" parameterType="DatabaseInfoVO">
		INSERT INTO INST_DB_CONN_INFO
			(SYS_ID,
			DB_TYPE,
			DB_CONN_USER,
			DB_CONN_PW,
			DB_CONN_PORT,
			DB_CONN_IP,
			DB_CONN_DATABASE,
			REGISTER_ID)
		VALUES
			(#{sysId},
			#{dbType},
			#{dbConnUser},
			#{dbConnPw},
			#{dbConnPort},
			#{dbConnIp},
			#{dbConnDatabase},
			#{registerId})
	</insert>

	<select id="selectSysDb" resultMap="databaseInfoMap">
		SELECT B.*
		FROM (SELECT ROW_NUMBER() OVER() rn, A.*
			FROM (SELECT SYS_ID,
					DB_CONN_ID ,
					DB_CONN_USER ,
					DB_CONN_PW ,
					DB_CONN_PORT ,
					DB_CONN_IP ,
					DB_CONN_DATABASE,
					DB_TYPE ,
					REGIST_DATE
				FROM INST_DB_CONN_INFO
				WHERE SYS_ID = #{sysId}
				AND USE_YN ='Y'
				ORDER BY REGIST_DATE DESC) A
			) B
		WHERE rn = 1;
	</select>

	<select id="selectSysDbConnList" resultMap="databaseInfoMap">
		SELECT SYS_ID, DB_CONN_USER, DB_CONN_PW, DB_CONN_PORT, DB_CONN_IP, B.*
		<if test='dbType != null and(dbType eq "mysql".toString())'>
		, (SELECT CONCAT(DB_TYPE||'_'||B.DB_CONN_ID) AS pgSourceNm )
		</if>
		FROM INST_DB_CONN_INFO A
		LEFT OUTER JOIN (
			SELECT DB_CONN_ID, STRING_AGG(DB_NM, ', ' ) DBNMLIST
			FROM INST_DB_INFO
			WHERE USE_YN = 'Y'
			GROUP BY DB_CONN_ID
		) B
		ON (A.DB_CONN_ID = B.DB_CONN_ID)
		WHERE DB_TYPE = #{dbType}
		ORDER BY SYS_ID , A.DB_CONN_ID
	</select>

	<select id="checkDbConnInfo" parameterType="DatabaseInfoVO" resultType="java.lang.Integer">
		SELECT COUNT(*)
		FROM INST_DB_CONN_INFO
		WHERE SYS_ID = #{sysId}
		AND DB_CONN_IP = #{dbConnIp}
	</select>

	<insert id="insertDbTblInfo" parameterType="TableInfoVO">
		<selectKey resultType="int" keyProperty="tableId" order="BEFORE">
			SELECT nextval('inst_db_tbl_info_seq') as tableId
		</selectKey>
		INSERT INTO INST_DB_TBL_INFO
		( TABLE_ID, DB_LINK_ID, TABLE_NM, TABLE_DC, REGIST_DATE, ENGINE )
		VALUES
		( #{tableId}, #{dbLinkId}, #{tableNm}, #{tableDc}, #{registDate}, #{engine} )
	</insert>

	<insert id="insertTblColInfo" parameterType="ColumnInfoVO">
		INSERT INTO inst_tbl_col_info (table_id, column_nm, column_type, is_nullable, is_pkey, is_fkey, column_dc, default_value)
		VALUES (#{tableId}, #{columnNm}, #{columnType}, #{isNullable}, #{isPkey}, #{isFkey}, #{columnDc}, #{defaultValue})
	</insert>


	<select id="selectRegisterdDBInfoList" parameterType="DatabaseInfoVO" resultType="DatabaseInfoVO">
		SELECT
			row_number() over () as row,
				a.db_link_id,
			a.db_nm,
			b.db_conn_id,
			b.sys_id,
			c.sys_nm,
			b.db_conn_ip,
			b.db_conn_port,
			b.db_conn_user,
			repeat('*', length(b.db_conn_pw)) as db_conn_pw,
			b.sys_id,
			b.db_type
		FROM inst_db_info a
		 LEFT JOIN inst_db_conn_info b ON a.db_conn_id = b.db_conn_id AND b.use_yn = 'Y'
		 LEFT JOIN inst_sys_info c ON b.sys_id = c.sys_id
		WHERE b.register_id =  #{registerId}
		AND a.use_yn = 'Y'
	</select>

	<sql id="selectInstDbConnInfo">
		SELECT
			sys_id,
			use_yn,
			db_type,
			db_conn_user,
			db_conn_pw,
			db_conn_port,
			db_conn_ip,
			register_id,
			regist_date
		FROM inst_db_conn_info
		WHERE 1=1
	</sql>

	<select id="selectInstDbConnInfoList" parameterType="DBConnInfoVO" resultType="DBConnInfoVO">
		<include refid="selectInstDbConnInfo" />
		<if test="registerId != null and registerId != ''">
			AND register_id = #{registerId}
		</if>
	</select>

	<select id="selectInstDbConnInfo" parameterType="DBConnInfoVO" resultType="DBConnInfoVO">
		<include refid="selectInstDbConnInfo" />
		AND db_conn_id = #{dbConnId}
		<if test="registerId != null and registerId != ''">
			AND register_id = #{registerId}
		</if>
	</select>

	<insert id="insertInstDbConnInfo" parameterType="DBConnInfoVO">
		INSERT INTO inst_db_conn_info (
			sys_id,
			db_type,
			db_conn_user,
			db_conn_pw,
			db_conn_port,
			db_conn_ip,
			register_id
		) VALUES (
		 #{sysId},
		 #{dbType},
		 #{dbConnUser},
		 #{dbConnPw},
		 #{dbConnPort},
		 #{dbConnIp},
		 #{registerId}
	 )
		<selectKey resultType="int" keyProperty="dbConnId" order="AFTER">
			SELECT LAST_INSERT_ID()
		</selectKey>
	</insert>

	<update id="updateInstDbConnInfo" parameterType="DBConnInfoVO">
		UPDATE INST_DB_CONN_INFO
		SET register_id = #{registerId}
		<if test="useYn != null and useYn != ''">
			, USE_YN = #{useYn}
		</if>
		<if test="dbType != null and dbType != ''">
			, DB_TYPE = #{dbType}
		</if>
		<if test="dbConnUser != null and dbConnUser != ''">
			, DB_CONN_USER = #{dbConnUser}
		</if>
		<if test="dbConnPw != null and dbConnPw != ''">
			, DB_CONN_PW = #{dbConnPw}
		</if>
		<if test="dbConnIp != null and dbConnIp != ''">
			, DB_CONN_IP = #{dbConnIp}
		</if>
		WHERE DB_CONN_ID = #{dbConnId}
	</update>

	<sql id="selectInstDbInfo">
		SELECT
			db_conn_id,
			db_link_id,
			db_nm,
			db_cmment,
			regist_date,
			use_yn
		FROM inst_db_info
		WHERE 1=1
	</sql>

	<select id="selectInstDbInfoList" parameterType="DBInfoVO" resultType="DBInfoVO">
		<include refid="selectInstDbInfo" />
		<if test="dbConnId != null and dbConnId != ''">
			AND db_conn_id = #{dbConnId}
		</if>
	</select>

	<select id="selectInstDbInfo" parameterType="DBInfoVO" resultType="DBInfoVO">
		<include refid="selectInstDbInfo" />
		AND db_link_id = #{dbLinkId}
		<if test="dbConnId != null and dbConnId != ''">
			AND db_conn_id = #{dbConnId}
		</if>
	</select>

	<insert id="insertInstDbInfo" parameterType="DBInfoVO">
		INSERT INTO inst_db_info (
			db_conn_id,
			db_nm,
			db_cmment,
		) VALUES (
		 #{dbConnId},
		 #{dbNm},
		 #{dbCmment}
	 )
	</insert>

	<update id="updateInstDbInfo" parameterType="DBInfoVO">
		UPDATE inst_db_info
		SET regist_date = current_time
		<if test="dbNm != null and dbNm != ''">
			,db_nm = #{dbNm}
		</if>
		<if test="dbCmment != null and dbCmment != ''">
			,db_cmment = #{dbCmment}
		</if>
		<if test="useYn != null and useYn != ''">
			,use_yn = #{useYn}
		</if>
		WHERE db_link_id = #{dbLinkId}
	</update>


	<sql id="selectInstDbTblInfo">
		SELECT
			table_id
			db_link_id,
			table_nm,
			table_dc,
			regist_date,
			engine
		FROM inst_db_tbl_info
		WHERE 1=1
	</sql>

	<select id="selectInstDbTblInfoList" parameterType="TableInfoVO" resultType="TableInfoVO">
		<include refid="selectInstDbTblInfo" />
		<if test="dbLinkId != null and dbLinkId != ''">
			AND db_link_id = #{dbLinkId}
		</if>
	</select>

	<select id="selectInstDbTblInfo" parameterType="TableInfoVO" resultType="TableInfoVO">
		<include refid="selectInstDbTblInfo" />
		AND table_id = #{tableId}
		<if test="dbLinkId != null and dbLinkId != ''">
			AND db_link_id = #{dbLinkId}
		</if>
	</select>

	<insert id="insertInstDbTblInfo" parameterType="TableInfoVO">
		INSERT INTO inst_db_tbl_info (
			db_link_id,
			table_nm,
			table_dc,
			engine
		) VALUES (
		 #{dbLinkId},
		 #{tableNm},
		 #{tableDc},
		 #{engine}
	 )
	</insert>

	<update id="updateInstDbTblInfo" parameterType="TableInfoVO">
		UPDATE inst_db_tbl_info
		<set>
			<if test="tableNm != null and tableNm != ''">
				table_nm = #{tableNm},
			</if>
			<if test="tableDc != null and tableDc != ''">
				table_dc = #{tableDc},
			</if>
			<if test="useYn != null and useYn != ''">
				use_yn = #{useYn},
			</if>
		</set>
		WHERE table_id = #{tableId}
	</update>

	<sql id="selectInstDbTblColInfo">
		SELECT
			column_id,
			table_id,
			column_nm,
			column_type,
			is_nullable,
			is_pkey,
			is_fkey,
			column_dc,
			default_value
		FROM inst_tbl_col_info
		WHERE 1=1
	</sql>

	<select id="selectInstDbTblColInfoList" parameterType="ColumnInfoVO" resultType="ColumnInfoVO">
		<include refid="selectInstDbTblInfo" />
		AND table_id = #{tableId}
	</select>

	<select id="selectInstDbTblColInfo" parameterType="ColumnInfoVO" resultType="ColumnInfoVO">
		<include refid="selectInstDbTblInfo" />
		AND table_id = #{tableId}
		AND column_id = #{columnId}
	</select>

	<insert id="insertInstDbTblColInfo" parameterType="ColumnInfoVO">
		INSERT INTO inst_tbl_col_info (
			table_id,
			column_nm,
			column_type,
			is_nullable,
			is_pkey,
			is_fkey,
			column_dc,
			default_value
		) VALUES (
		 #{tableId},
		 #{columnNm},
		 #{columnType},
		 #{isNullable},
		 #{isPkey},
		 #{isFkey},
		 #{columnDc},
		 #{defaultValue}
	 )
		<selectKey resultType="int" keyProperty="dbConnId" order="AFTER">
			SELECT LAST_INSERT_ID()
		</selectKey>
	</insert>

	<update id="updateInstDbTblColInfo" parameterType="ColumnInfoVO">
		UPDATE inst_tbl_col_info
		<set>
			<if test="columnNm != null and columnNm != ''">
				column_nm = #{columnNm},
			</if>
			<if test="columnType != null and columnType != ''">
				column_type = #{columnType},
			</if>
			<if test="isNullable != null and isNullable != ''">
				is_nullable = #{isNullable},
			</if>
			<if test="isPkey != null and isPkey != ''">
				is_pkey = #{isPkey},
			</if>
			<if test="isFkey != null and isFkey != ''">
				is_fkey = #{isFkey},
			</if>
			<if test="columnDc != null and columnDc != ''">
				column_dc = #{columnDc},
			</if>
			<if test="defaultValue != null and defaultValue != ''">
				default_value = #{defaultValue},
			</if>
		</set>
		WHERE table_id = #{tableId}
		AND column_id = #{columnId}
	</update>

</mapper>
