<?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.submitted.ancestor_ref.Mapper">

  <resultMap type="org.apache.ibatis.submitted.ancestor_ref.User"
    id="userMapAssociation">
    <id property="id" column="id" />
    <result property="name" column="name" />
    <association property="friend" resultMap="userMapAssociation"
      columnPrefix="friend_" />
  </resultMap>

  <select id="getUserAssociation" resultMap="userMapAssociation">
    select u.id, u.name, uf.id friend_id, uf.name friend_name
    from users u
    left join friend f on f.user_id = u.id
    left join users uf on uf.id = f.friend_id
    where u.id = #{id}
  </select>

  <resultMap type="org.apache.ibatis.submitted.ancestor_ref.User"
    id="userMapCollection">
    <id property="id" column="id" />
    <result property="name" column="name" />
    <collection property="friends" resultMap="userMapCollection"
      columnPrefix="friend_" />
  </resultMap>

  <select id="getUserCollection" resultMap="userMapCollection">
    select u.id, u.name, uf.id friend_id, uf.name friend_name
    from users u
    left join friend f on f.user_id = u.id
    left join users uf on uf.id = f.friend_id
    where u.id = #{id}
    order by uf.id
  </select>

  <resultMap type="org.apache.ibatis.submitted.ancestor_ref.Blog"
    id="blogResult">
    <id property="id" column="id" />
    <result property="title" column="title" />
    <association property="author" resultMap="authorResult"
      columnPrefix="author_" />
    <association property="coAuthor" resultMap="authorResult"
      columnPrefix="co_author_" />
  </resultMap>

  <resultMap type="org.apache.ibatis.submitted.ancestor_ref.Author"
    id="authorResult">
    <id property="id" column="id" />
    <result property="name" column="name" />
    <association property="blog" resultMap="blogResult" />
    <association property="reputation" resultMap="reputationResult" />
  </resultMap>

  <resultMap type="org.apache.ibatis.submitted.ancestor_ref.Reputation"
    id="reputationResult">
    <id property="value" column="reputation" />
    <association property="author" resultMap="authorResult" />
  </resultMap>

  <select id="selectBlog" resultMap="blogResult"><![CDATA[
    select id, title, a.id author_id, a.name author_name, a.reputation author_reputation,
      ca.id co_author_id, ca.name co_author_name, ca.reputation co_author_reputation
    from blog b
    left join author a on a.id = b.author_id
    left join author ca on ca.id = b.co_author_id
    where b.id = #{id}
  ]]></select>

</mapper>
