<?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.binding.BoundBlogMapper">

    <select id="selectRandom" resultType="int">
    select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1
  </select>

    <select id="selectBlogsFromXML" resultType="org.apache.ibatis.domain.blog.Blog">
    SELECT * FROM blog
  </select>

    <resultMap id="blogWithPosts" type="Blog">
        <id property="id" column="id"/>
        <result property="title" column="title"/>
        <association property="author" column="author_id"
                     select="selectAuthorWithInlineParams"/>
        <collection property="posts" column="id" select="selectPostsForBlog"/>
    </resultMap>

    <resultMap id="blogUsingConstructor" type="Blog">
        <constructor>
            <idArg column="id" javaType="_int"/>
            <arg column="title" javaType="java.lang.String"/>
            <arg column="author_id" javaType="org.apache.ibatis.domain.blog.Author"
                 select="org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor"/>
            <arg column="id" javaType="java.util.List" select="selectPostsForBlog"/>
        </constructor>
    </resultMap>

    <resultMap id="blogUsingConstructorWithResultMap" type="Blog">
        <constructor>
            <idArg column="id" javaType="_int"/>
            <arg column="title" javaType="java.lang.String"/>
            <arg javaType="org.apache.ibatis.domain.blog.Author" resultMap="org.apache.ibatis.binding.BoundAuthorMapper.authorResultMap"/>
            <arg column="id" javaType="java.util.List" select="selectPostsForBlog"/>
        </constructor>
    </resultMap>

    <resultMap id="blogUsingConstructorWithResultMapAndProperties" type="Blog">
        <constructor>
            <idArg column="id" javaType="_int"/>
            <arg column="title" javaType="java.lang.String"/>
            <arg javaType="org.apache.ibatis.domain.blog.Author" resultMap="org.apache.ibatis.binding.BoundAuthorMapper.authorResultMapWithProperties"/>
            <arg column="id" javaType="java.util.List" select="selectPostsForBlog"/>
        </constructor>
    </resultMap>

    <resultMap id="blogUsingConstructorWithResultMapCollection" type="Blog">
        <constructor>
            <idArg column="id" javaType="_int"/>
            <arg column="title" javaType="java.lang.String"/>
            <arg javaType="org.apache.ibatis.domain.blog.Author" resultMap="org.apache.ibatis.binding.BoundAuthorMapper.authorResultMap"/>
            <arg javaType="java.util.List" resultMap="blogWithPosts"/>
        </constructor>
    </resultMap>

  <select id="selectBlogWithPostsUsingSubSelect" parameterType="int" resultMap="blogWithPosts">
    select * from Blog where id = #{id}
  </select>

  <select id="selectAuthorWithInlineParams"
            parameterType="int"
            resultType="org.apache.ibatis.domain.blog.Author">
    select * from author where id = #{id}
  </select>

  <select id="selectPostsForBlog" parameterType="int" resultType="Post">
    select * from Post where blog_id = #{blog_id}
  </select>

  <select id="selectBlogByIdUsingConstructor" parameterType="int" resultMap="blogUsingConstructor">
    select * from Blog where id = #{id}
  </select>

    <select id="selectBlogUsingConstructorWithResultMap" parameterType="int"
            resultMap="blogUsingConstructorWithResultMap">
      select b.*,
      	a.id as author_id,
      	a.username as author_username,
      	a.password as author_password,
      	a.email as author_email,
      	a.bio as author_bio,
      	a.favourite_section
      from Blog b join Author a
      on b.author_id = a.id
      where b.id = #{id}
    </select>

    <select id="selectBlogUsingConstructorWithResultMapAndProperties" parameterType="int"
            resultMap="blogUsingConstructorWithResultMapAndProperties">
      select b.*,
      	a.id as author_id,
      	a.username as author_username,
      	a.password as author_password,
      	a.email as author_email,
      	a.bio as author_bio,
      	a.favourite_section
      from Blog b join Author a
      on b.author_id = a.id
      where b.id = #{id}
    </select>

    <select id="selectBlogUsingConstructorWithResultMapCollection" parameterType="int"
            resultMap="blogUsingConstructorWithResultMapCollection">
      select b.*, p.*,
      	a.id as author_id,
      	a.username as author_username,
      	a.password as author_password,
      	a.email as author_email,
      	a.bio as author_bio,
      	a.favourite_section
      from Blog b
          join Author a on b.author_id = a.id
          join Post p on b.id = p.blog_id
      where b.id = #{id}
    </select>

</mapper>
