<?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="cokr.xit.ens.modules.common.ctgy.sys.mng.mapper.IKeySequenceMapper">

    <select id="selectKeySequence" parameterType="string" resultType="long">
        /** iup-keyseq-mapper|selectKeySequence-key sequence 조회|julim  */
        SELECT next_val + 1
          FROM ens_seq_generator
         WHERE seq_name = #{seqName}
           FOR UPDATE
    </select>

    <insert id="saveKeySequence" parameterType="map">
        /** iup-keyseq-mapper|saveKeySequence-key sequence 저장|julim  */
        MERGE
         INTO ens_seq_generator tgt
        USING (SELECT #{seqName} AS seq_name, #{nextVal} AS next_val FROM dual) src
           ON (tgt.seq_name = src.seq_name)
         WHEN MATCHED THEN
            UPDATE SET tgt.next_val = src.next_val
         WHEN NOT MATCHED THEN
            INSERT (seq_name, next_val)
            VALUES (src.seq_name, src.next_val)
    </insert>

</mapper>
