5. <select id="selectAllEntries" resultMap="selectEntryMap">
SELECT
en.id AS entry_id
, en.title AS entry_title
, en.contents AS entry_contents
, en.updated AS entry_updated
, tag.id AS tag_id
, tag.tag AS tag_tag
, au.id AS author_id
, au.name AS author_name
FROM
Entry AS en
lEFT OUTER JOIN
Entry2Tag AS e2t
ON en.id = e2t.entry_id
LEFT OUTER JOIN
Tag AS tag
ON tag.id = e2t.tag_id
JOIN
Entry2Author as e2a
ON e2a.entry_id = en.id
JOIN
Author as au
ON au.id = e2a.author_id
ORDER BY
en.id
, au.id
, tag.id
</select>
8. 沿革①
• 2012年春
– Glorpと付き合いたくない
– PragmaにSQLを書けばシンプルでいいのでは?
• VisualWorksでPMapperライブラリ作成開始
selectById: id
<resultType: 'PMapper.PmEntry'>
<selectOne: 'SELECT id, title, contents, updated
FROM PmEntry
WHERE id = :id'>
^self executeQuery
9. 沿革②
• すぐに破綻が見える
– ちょっと大きくなるとシンプルさが消失
selectById: id
<selectOne: ‘
SELECT
id as tag_id,
tag as tag_name
FROM PmTag
WHERE id = :id'>
<resultMap: #(
'PMapper.PmTag'
#(id tag_id)
#(tag tag_name))>
^self executeQuery
29. select②
• 使用例
<resultMap id="selectAuthorMap" type="KbAuthor">
<id property="id" column="author_id" />
<result property="name" column="author_name" />
</resultMap>
<select id="selectAllAuthors" resultType="KbAuthor">
<![CDATA[
SELECT
id as author_id
, name as author_name
FROM
Author
]]>
</select>
CDATAで囲むことも可能
この例では必要ないが、<>等の演算子を含
める場合に必要
テーブルをJOINした場合などにも使いまわ
したいので、columnに別名を付けている
30. select③
<select id="selectAllEntries" resultMap="selectEntryMap">
SELECT
en.id AS entry_id
, en.title AS entry_title
, en.contents AS entry_contents
, en.updated AS entry_updated
, tag.id AS tag_id
, tag.tag AS tag_tag
, au.id AS author_id
, au.name AS author_name
FROM
Entry AS en
lEFT OUTER JOIN
Entry2Tag AS e2t
ON en.id = e2t.entry_id
LEFT OUTER JOIN
Tag AS tag
ON tag.id = e2t.tag_id
JOIN
Entry2Author as e2a
ON e2a.entry_id = en.id
JOIN
Author as au
ON au.id = e2a.author_id
ORDER BY
en.id
, au.id
, tag.id
</select>
33. sql
• コード片の再利用に使用
<sql id="selectAllTagsSql">
SELECT
id
, tag
FROM
Tag
</sql>
<select id="selectAllTags" resultType="KbTag">
<include refid="selectAllTagsSql"/>
</select>
<selectOne id="selectTagById" arguments="id" resultType="KbTag">
<include refid="selectAllTagsSql"/>
WHERE
id = #{id}
</selectOne>