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

  <resultMap
    type="org.apache.ibatis.submitted.discriminator.Vehicle"
    id="vehicleResult">
    <id property="id" column="id" />
    <result property="maker" column="maker" />
    <discriminator javaType="int" column="vehicle_type">
      <case value="1"
        resultType="org.apache.ibatis.submitted.discriminator.Car">
        <result property="doorCount" column="door_count" />
      </case>
      <case value="2"
        resultType="org.apache.ibatis.submitted.discriminator.Truck">
        <result property="carryingCapacity"
          column="carrying_capacity" />
      </case>
    </discriminator>
  </resultMap>

  <select id="selectVehicles" resultMap="vehicleResult"><![CDATA[
    select * from vehicle order by id
  ]]></select>

  <resultMap
    type="org.apache.ibatis.submitted.discriminator.Owner"
    id="ownerResult">
    <id property="id" column="id" />
    <result property="name" column="name" />
    <discriminator javaType="string" column="vehicle_type">
      <case value="car">
        <association property="vehicle" column="vehicle_id"
          select="selectCar" />
      </case>
      <case value="truck">
        <association property="vehicle" column="vehicle_id"
          select="selectTruck" />
      </case>
    </discriminator>
  </resultMap>

  <select id="selectOwnersWithAVehicle" resultMap="ownerResult"><![CDATA[
    select id, name, vehicle_type, vehicle_id
    from owner
  ]]></select>

  <select id="selectCar"
    resultType="org.apache.ibatis.submitted.discriminator.Car"><![CDATA[
    select id, maker, door_count doorCount
    from vehicle
    where id = #{id}
  ]]></select>

  <select id="selectTruck"
    resultType="org.apache.ibatis.submitted.discriminator.Truck"><![CDATA[
    select id, maker, door_count carryingCapacity
    from vehicle
    where id = #{id}
  ]]></select>

</mapper>
