<?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="org.activiti.engine.impl.persistence.entity.ByteArrayEntity">

  <!-- BYTE ARRAY INSERT -->

  <insert id="insertByteArray" parameterType="org.activiti.engine.impl.persistence.entity.ByteArrayEntity">
    insert into ${prefix}ACT_GE_BYTEARRAY(ID_, REV_, NAME_, BYTES_, DEPLOYMENT_ID_)
    values (
      #{id, jdbcType=VARCHAR},
      1, 
      #{name, jdbcType=VARCHAR}, 
      #{bytes, jdbcType=BLOB}, 
      #{deploymentId, jdbcType=VARCHAR}
    )  
  </insert>
  
  <!-- BYTE ARRAY UPDATE -->

  <update id="updateByteArray" parameterType="org.activiti.engine.impl.persistence.entity.ByteArrayEntity">
    update ${prefix}ACT_GE_BYTEARRAY 
    set
      REV_ = #{revisionNext, jdbcType=INTEGER},
      BYTES_ = #{bytes, jdbcType=BLOB}
    where ID_ = #{id}
      and REV_ = #{revision, jdbcType=INTEGER}
  </update>
  
  <!-- BYTE ARRAY DELETE -->

  <select id="selectBytesOfByteArray" parameterType="string" resultType="hashmap">
    select BYTES_ from ${prefix}ACT_GE_BYTEARRAY where ID_ = #{id} and REV_ = #{revision}
  </select>
  
  <delete id="deleteByteArraysForDeployment" parameterType="org.activiti.engine.impl.persistence.entity.ByteArrayEntity">
    delete from ${prefix}ACT_GE_BYTEARRAY where DEPLOYMENT_ID_ = #{id} and REV_ = #{revision}
  </delete>
  
  <delete id="deleteByteArray" parameterType="org.activiti.engine.impl.persistence.entity.ByteArrayEntity">
    delete from ${prefix}ACT_GE_BYTEARRAY where ID_ = #{id} and REV_ = #{revision}
  </delete>
  
  <delete id="deleteByteArrayNoRevisionCheck" parameterType="string">
    delete from ${prefix}ACT_GE_BYTEARRAY where ID_ = #{id}
  </delete>

  <!-- BYTE ARRAY RESULTMAP -->
  
  <resultMap id="byteArrayResultMap" type="org.activiti.engine.impl.persistence.entity.ByteArrayEntity">
    <id property="id" column="ID_" jdbcType="VARCHAR" />
    <result property="revision" column="REV_" jdbcType="INTEGER"/>
    <result property="name" column="NAME_" jdbcType="VARCHAR"/>
    <result property="bytes" column="BYTES_" jdbcType="BLOB"/>  
  </resultMap>

  <!-- BYTE ARRAY SELECT -->
  
  <select id="selectByteArray" parameterType="string" resultMap="byteArrayResultMap">
   select * from ${prefix}ACT_GE_BYTEARRAY where ID_ = #{id}
  </select>
  
<!-- Postgresql specific configuration -->
  <resultMap id="byteArrayResultMap_postgres" type="org.activiti.engine.impl.persistence.entity.ByteArrayEntity">
    <id property="id" column="ID_" jdbcType="VARCHAR" />
    <result property="revision" column="REV_" jdbcType="INTEGER"/>
    <result property="name" column="NAME_" jdbcType="VARCHAR"/>
    <result property="bytes" column="BYTES_" jdbcType="BINARY"/>  
  </resultMap>
  
  <select id="selectByteArray_postgres" parameterType="string" resultMap="byteArrayResultMap_postgres">
   select * from ${prefix}ACT_GE_BYTEARRAY where ID_ = #{id}
  </select>

  <update id="updateByteArray_postgres" parameterType="org.activiti.engine.impl.persistence.entity.ByteArrayEntity">
    update ${prefix}ACT_GE_BYTEARRAY 
    set
      REV_ = #{revisionNext, jdbcType=INTEGER},
      BYTES_ = #{bytes, jdbcType=BINARY}
    where ID_ = #{id}
      and REV_ = #{revision, jdbcType=INTEGER}
  </update>  
  
    <insert id="insertByteArray_postgres" parameterType="org.activiti.engine.impl.persistence.entity.ByteArrayEntity">
    insert into ${prefix}ACT_GE_BYTEARRAY(ID_, REV_, NAME_, BYTES_, DEPLOYMENT_ID_)
    values (
      #{id, jdbcType=VARCHAR},
      1, 
      #{name, jdbcType=VARCHAR}, 
      #{bytes, jdbcType=BINARY}, 
      #{deploymentId, jdbcType=VARCHAR}
    )  
  </insert>
</mapper>