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

       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.complex_column.PersonMapper">

    <resultMap id="personMap" type="Person">
        <id property="id" column="id"/>
        <result property="firstName" column="firstName"/>
        <result property="lastName" column="lastName"/>
        <association property="parent" column="parent_id" select="getParentWithoutComplex"/>
    </resultMap>

    <resultMap id="personMapComplex" type="Person">
        <id property="id" column="id"/>
        <result property="firstName" column="firstName"/>
        <result property="lastName" column="lastName"/>
        <association property="parent" column="{firstName=parent_firstName,lastName=parent_lastName}" select="getParentWithComplex"/>
    </resultMap>


    <select id="getWithoutComplex" resultMap="personMap" parameterType="long">
        SELECT id, firstName, lastName, parent_id, parent_firstName, parent_lastName
        FROM Person
        WHERE id = #{id,jdbcType=INTEGER}
    </select>
    <!-- parameterType was removed because it failed when using RAW.
    the DynamicSqlSource calculates the parameter out of the actual parameter used in the call
    the RawSqlSource calculates it during startup.
    This paremter is a Person when called directly but an Integer when calling from the ResultMap (nested select)
    so it fails -->
    <select id="getParentWithoutComplex" resultMap="personMap">
        SELECT id, firstName, lastName, parent_id, parent_firstName, parent_lastName
        FROM Person
        WHERE id = #{id,jdbcType=INTEGER}
    </select>
    <select id="getWithComplex" resultMap="personMapComplex" parameterType="long">
        SELECT id, firstName, lastName, parent_id, parent_firstName, parent_lastName
        FROM Person
        WHERE id = #{id,jdbcType=INTEGER}
    </select>
    <select id="getParentWithComplex" resultMap="personMapComplex">
        SELECT id, firstName, lastName, parent_id, parent_firstName, parent_lastName
        FROM Person
        WHERE firstName = #{firstName,jdbcType=VARCHAR}
        AND lastName = #{lastName,jdbcType=VARCHAR}
        LIMIT 1
    </select>

</mapper>
