<?xml version="1.0" encoding="UTF-8" ?>
<!--

       Copyright ${license.git.copyrightYears} the original author or authors.

       Licensed under the Apache License, Version 2.0 (the "License");
       you may not use this file except in compliance with the License.
       You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing, software
       distributed under the License is distributed on an "AS IS" BASIS,
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       See the License for the specific language governing permissions and
       limitations under the License.

-->
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="org.apache.ibatis.domain.blog.mappers.PostMapper">
  <resultMap id="postLiteIdMap" type="org.apache.ibatis.domain.blog.PostLiteId">
      <constructor>
          <idArg javaType="_int" column="id"/>
      </constructor>
  </resultMap>

  <resultMap id="postLiteMap" type="org.apache.ibatis.domain.blog.PostLite">
      <constructor>
          <arg javaType="org.apache.ibatis.domain.blog.PostLiteId" resultMap="postLiteIdMap"/>
          <arg javaType="_int" column="blog_id"/>
      </constructor>
  </resultMap>

  <resultMap id="postLiteMap2NestedWithSelect" type="org.apache.ibatis.domain.blog.BlogLite">
    <id column="blog_id" property="id" />
    <collection property="posts" ofType="org.apache.ibatis.domain.blog.PostLite">
      <constructor>
          <arg javaType="org.apache.ibatis.domain.blog.PostLiteId" column="{id=id}" select="selectPostLiteId" />
          <arg javaType="_int" column="blog_id"/>
      </constructor>
    </collection>
  </resultMap>

  <resultMap id="postLiteMap2NestedWithoutSelect" type="org.apache.ibatis.domain.blog.BlogLite">
    <id column="blog_id" property="id" />
    <collection property="posts" ofType="org.apache.ibatis.domain.blog.PostLite">
      <constructor>
          <arg javaType="org.apache.ibatis.domain.blog.PostLiteId" resultMap="postLiteIdMap" />
          <arg javaType="_int" column="blog_id"/>
      </constructor>
    </collection>
  </resultMap>

  <resultMap id="mutablePostLiteMap" type="org.apache.ibatis.domain.blog.PostLite">
      <result property="blogId" column="blog_id"/>
      <association property="id" column="id" resultMap="postLiteIdMap"/>
  </resultMap>

  <resultMap id="mutablePostLiteIdMap" type="org.apache.ibatis.domain.blog.PostLiteId">
      <id property="id" column="id"/>
  </resultMap>

  <select id="selectPostLite" resultMap="postLiteMap">
      select id, blog_id from post where blog_id is not null
  </select>

  <select id="selectPostLite2NestedWithSelect" resultMap="postLiteMap2NestedWithSelect">
      select id, 1 as blog_id from post where blog_id is not null
  </select>

  <select id="selectPostLite2NestedWithoutSelect" resultMap="postLiteMap2NestedWithoutSelect">
      select id, 1 as blog_id from post where blog_id is not null
  </select>

  <select id="selectPostLiteId" resultMap="postLiteIdMap">
      select ${id} as id from (values(0)) as t
  </select>

  <select id="selectMutablePostLite" resultMap="mutablePostLiteMap">
      select id, blog_id from post where blog_id is not null
  </select>

  <sql id="byBlogId">
    <if test="blog_id != null">${prefix}_id = #{blog_id}</if>
  </sql>

  <select id="findPost" resultType="org.apache.ibatis.domain.blog.Post">
    SELECT *
    FROM POST P
    <where>
      <choose>
        <when test="id != null">id = #{id}</when>
        <when test="author_id != null">AND author_id = #{author_id}</when>
        <otherwise>
          <if test="ids != null">
            AND id IN
            <foreach item="item_id" index="index" open="(" close=")" separator="," collection="ids">#{ids[${index}]}
            </foreach>
          </if>
          <trim prefix="AND">
            <include refid="byBlogId">
                <property name="prefix" value="blog"/>
            </include>
          </trim>
        </otherwise>
      </choose>
    </where>
  </select>

  <select id="selectPostIn" resultType="org.apache.ibatis.domain.blog.Post">
    SELECT *
    FROM POST P
    WHERE ID in
    <foreach item="item" index="index" collection="list"
             open="(" close=")">
      <if test="index != 0">,</if> #{item}
    </foreach>
  </select>

  <select id="selectOddPostsIn" resultType="org.apache.ibatis.domain.blog.Post">
    SELECT *
    FROM POST P
    WHERE ID in
    <foreach item="item" index="index" collection="list"
             open="(" separator="," close=")">
      <if test="index % 2 != 0">
        #{item}
      </if>
    </foreach>
    ORDER BY P.ID
  </select>

  <select id="selectOddPostsInKeysList" resultType="org.apache.ibatis.domain.blog.Post">
    SELECT *
    FROM POST P
    WHERE ID in
    <foreach item="item" index="index" collection="keys"
             open="(" separator="," close=")">
      <if test="index % 2 != 0">
        #{item}
      </if>
    </foreach>
    ORDER BY P.ID
  </select>
</mapper>