Skip to content

Commit 1f16c61

Browse files
committed
/*%populate*/ についてのドキュメントを追記
1 parent 418d1a8 commit 1f16c61

File tree

3 files changed

+149
-39
lines changed

3 files changed

+149
-39
lines changed

docs/sources/query/batch-update.rst

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
上記の条件を満たさないない場合、戻り値は各更新処理の更新件数を表す ``int[]`` でなければいけません。
3636

37+
.. _auto-batch-update:
38+
3739
SQLの自動生成によるバッチ更新
3840
=============================
3941

@@ -127,6 +129,40 @@ SQLファイルによるバッチ更新を行うには、
127129
``@BatchUpdate`` の ``sqlFile`` 要素に ``true`` を設定し、
128130
メソッドに対応するSQLファイルを用意します。
129131

132+
.. note::
133+
134+
SQLファイルによるバッチ更新は、 :ref:`populate` の利用有無によりルールが異なります。
135+
136+
更新カラムリスト生成コメントを使用する場合
137+
-------------------------------------------------
138+
139+
.. code-block:: java
140+
141+
@BatchUpdate(sqlFile = true)
142+
int[] update(List<Employee> employees);
143+
144+
@BatchUpdate
145+
BatchResult<ImmutableEmployee> update(List<ImmutableEmployee> employees);
146+
147+
パラメータの型は :doc:`../entity` を要素とする ``java.lang.Iterable`` のサブタイプでなければいけません。
148+
指定できるパラメータの数は1つです。
149+
引数は ``null`` であってはいけません。
150+
戻り値の配列の要素の数はパラメータの ``Iterable`` の要素の数と等しくなります。
151+
配列のそれぞれの要素が更新された件数を返します。
152+
153+
たとえば、上記のメソッドに対応するSQLは次のように記述します。
154+
155+
.. code-block:: sql
156+
157+
update employee set /*%populate*/ id = id where name = /* employees.name */'hoge'
158+
159+
SQLファイル上では、パラメータの名前は ``Iterable`` のサブタイプの要素を指します。
160+
161+
更新対象プロパティの制御に関するルールは、 :ref:`auto-batch-update` と同じです。
162+
163+
更新カラムリスト生成コメントを使用しない場合
164+
-------------------------------------------------
165+
130166
.. code-block:: java
131167
132168
@BatchUpdate(sqlFile = true)
@@ -154,7 +190,7 @@ SQLファイルによるバッチ更新では、バージョン番号の自動
154190
また、 ``@BatchUpdate`` の ``exclude`` 要素、 ``include`` 要素は参照されません。
155191

156192
SQLファイルにおけるバージョン番号と楽観的排他制御
157-
-------------------------------------------------
193+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
158194

159195
次の条件を満たす場合に、楽観的排他制御が行われます。
160196

@@ -184,7 +220,7 @@ WHERE句でバージョンを番号を指定しSET句でバージョン番号を
184220
はスローされず、エンティティのバージョンプロパティの値が1増分されます。
185221

186222
ignoreVersion
187-
~~~~~~~~~~~~~
223+
^^^^^^^^^^^^^
188224

189225
``@BatchUpdate`` の ``ignoreVersion`` 要素が ``true`` の場合、
190226
更新件数が0件または複数件であっても、 ``BatchOptimisticLockException`` はスローされません。
@@ -196,7 +232,7 @@ ignoreVersion
196232
int[] update(List<Employee> employees);
197233
198234
suppressOptimisticLockException
199-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
235+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
200236

201237
``@BatchUpdate`` の ``suppressOptimisticLockException`` 要素が ``true`` の場合、
202238
更新件数が0件または複数件であっても ``BatchOptimisticLockException`` はスローされません。
@@ -251,4 +287,3 @@ SQL のログ出力形式
251287
int[] update(List<Employee> employees);
252288
253289
``SqlLogType.RAW`` はバインドパラメータ(?)付きの SQL をログ出力することを表します。
254-

docs/sources/query/update.rst

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
上記の条件を満たさないない場合、戻り値は更新件数を表す ``int`` でなければいけません。
3636

37+
.. _auto-update:
38+
3739
SQLの自動生成による更新
3840
=======================
3941

@@ -163,6 +165,37 @@ SQLファイルによる更新
163165
SQLファイルによる更新を行うには、 ``@Update`` の ``sqlFile`` 要素に ``true`` を設定し、
164166
メソッドに対応するSQLファイルを用意します。
165167

168+
.. note::
169+
170+
SQLファイルによる更新は、 :ref:`populate` の利用有無によりルールが異なります。
171+
172+
更新カラムリスト生成コメントを使用する場合
173+
-------------------------------------------------
174+
175+
最初のパラメータの型はエンティティクラスでなければいけません。
176+
指定できるパラメータの数に制限はありません。
177+
パラメータの型が基本型もしくはドメインクラスの場合、引数を ``null`` にできます。
178+
それ以外の型の場合、引数は ``null`` であってはいけません。
179+
180+
.. code-block:: java
181+
182+
@Update(sqlFile = true)
183+
int update(Employee employee, BigDecimal salary);
184+
185+
@Update(sqlFile = true)
186+
Result<ImmutableEmployee> update(ImmutableEmployee employee, , BigDecimal salary);
187+
188+
たとえば、上記のメソッドに対応するSQLは次のように記述します。
189+
190+
.. code-block:: sql
191+
192+
update employee set /*%populate*/ id = id where salary > /* salary */0
193+
194+
更新対象プロパティの制御に関するルールは、 :ref:`auto-update` と同じです。
195+
196+
更新カラムリスト生成コメントを使用しない場合
197+
-------------------------------------------------
198+
166199
パラメータには任意の型が使用できます。
167200
指定できるパラメータの数に制限はありません。
168201
パラメータの型が基本型もしくはドメインクラスの場合、引数を ``null`` にできます。
@@ -188,7 +221,7 @@ SQLファイルによる更新では、
188221
``includeUnchanged`` 要素は参照されません。
189222

190223
SQLファイルにおけるバージョン番号と楽観的排他制御
191-
-------------------------------------------------
224+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
192225

193226
次の条件を満たす場合に、楽観的排他制御が行われます。
194227

@@ -211,7 +244,7 @@ WHERE句でバージョンを番号を指定しSET句でバージョン番号を
211244
エンティティのバージョンプロパティの値が1増分されます。
212245

213246
ignoreVersion
214-
~~~~~~~~~~~~~
247+
^^^^^^^^^^^^^
215248

216249
``@Update`` の ``ignoreVersion`` 要素が ``true`` の場合、
217250
更新件数が0件であっても、 ``OptimisticLockException`` はスローされません。
@@ -223,7 +256,7 @@ ignoreVersion
223256
int update(Employee employee);
224257
225258
suppressOptimisticLockException
226-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
259+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
227260

228261
``@Update`` の ``suppressOptimisticLockException`` 要素が ``true`` の場合、
229262
更新件数が0件であっても、 ``OptimisticLockException`` はスローされません。

docs/sources/sql.rst

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ Dao インタフェースのメソッドと対応する SQL の例は次のと
208208
209209
select * from employee
210210
where
211-
employee_name = /* dto.employeeName */'abc'
211+
employee_name = /* dto.employeeName */'abc'
212212
and
213213
salary = /* dto.salary */1234
214214
@@ -279,7 +279,7 @@ ifとend
279279

280280
.. code-block:: sql
281281
282-
select * from employee where
282+
select * from employee where
283283
/*%if employeeId != null */
284284
employee_id = /* employeeId */99
285285
/*%end*/
@@ -307,7 +307,7 @@ ifとend
307307

308308
.. code-block:: sql
309309
310-
select * from employee where
310+
select * from employee where
311311
/*%if employeeId != null */
312312
employee_id = /* employeeId */99
313313
/*%end*/
@@ -327,7 +327,7 @@ ifとend
327327

328328
.. code-block:: sql
329329
330-
select * from employee where
330+
select * from employee where
331331
/*%if employeeId != null */
332332
employee_id = /* employeeId */99
333333
/*%end*/
@@ -351,14 +351,14 @@ elseifとelse
351351

352352
.. code-block:: sql
353353
354-
select
355-
*
354+
select
355+
*
356356
from
357-
employee
358-
where
357+
employee
358+
where
359359
/*%if employeeId != null */
360360
employee_id = /* employeeId */9999
361-
/*%elseif department_id != null */
361+
/*%elseif department_id != null */
362362
and
363363
department_id = /* departmentId */99
364364
/*%else*/
@@ -370,35 +370,35 @@ elseifとelse
370370

371371
.. code-block:: sql
372372
373-
select
374-
*
373+
select
374+
*
375375
from
376-
employee
377-
where
376+
employee
377+
where
378378
employee_id = ?
379379
380380
``employeeId == null && department_id != null`` が成立するとき、実際は次の SQL に変換されます。
381381
``department_id`` の直前の ``AND`` は自動で除去されるため出力されません。
382382

383383
.. code-block:: sql
384384
385-
select
386-
*
385+
select
386+
*
387387
from
388-
employee
389-
where
388+
employee
389+
where
390390
department_id = ?
391391
392392
``employeeId == null && department_id == null`` が成立するとき、実際は次の SQL に変換されます。
393393
``department_id`` の直前の ``AND`` は自動で除去されるため出力されません。
394394

395395
.. code-block:: sql
396396
397-
select
398-
*
397+
select
398+
*
399399
from
400-
employee
401-
where
400+
employee
401+
where
402402
department_id is null
403403
404404
ネストした条件コメント
@@ -408,10 +408,10 @@ elseifとelse
408408

409409
.. code-block:: sql
410410
411-
select * from employee where
411+
select * from employee where
412412
/*%if employeeId != null */
413413
employee_id = /* employeeId */99
414-
/*%if employeeName != null */
414+
/*%if employeeName != null */
415415
and
416416
employee_name = /* employeeName */'hoge'
417417
/*%else*/
@@ -429,7 +429,7 @@ elseifとelse
429429

430430
.. code-block:: sql
431431
432-
select * from employee /*%if employeeId != null */
432+
select * from employee /*%if employeeId != null */
433433
where employee_id = /* employeeId */99 /*%end*/
434434
435435
また、 ``if`` と ``end`` は同じレベルの文に含まれなければいけません。
@@ -472,7 +472,7 @@ forとend
472472
.. code-block:: sql
473473
474474
select * from employee where
475-
employee_name like ?
475+
employee_name like ?
476476
or
477477
employee_name like ?
478478
or
@@ -502,7 +502,7 @@ item_has_nextとitem_index
502502

503503
.. code-block:: sql
504504
505-
select * from employee where
505+
select * from employee where
506506
/*%for name : names */
507507
employee_name like /* name */'hoge'
508508
/*%if name_has_next */
@@ -525,7 +525,7 @@ item_has_nextとitem_index
525525

526526
.. code-block:: sql
527527
528-
select * from employee where
528+
select * from employee where
529529
/*%for name : names */
530530
employee_name like /* name */'hoge'
531531
/*%if name_has_next */
@@ -550,14 +550,14 @@ item_has_nextとitem_index
550550
また、 ``for`` と ``end`` は同じレベルの文に含まれなければいけません。
551551
つまり、括弧の外で ``for`` 、括弧の内側で ``end`` という記述は認められません。
552552

553-
カラムリスト展開コメント
554-
------------------------
553+
選択カラムリスト展開コメント
554+
----------------------------
555555

556556
expand
557557
~~~~~~
558558

559559
SELECT節のアスタリスク ``*`` を :doc:`entity` の定義を
560-
参照して自動でカラムのリストに展開する式をカラムリスト展開コメントと呼びます
560+
参照して自動でカラムのリストに展開する式を選択カラムリスト展開コメントと呼びます
561561
構文は次のとおりです。
562562

563563
::
@@ -593,7 +593,7 @@ SELECT節のアスタリスク ``*`` を :doc:`entity` の定義を
593593
select id, name, age from employee
594594
595595
SQL 上でテーブルにエイリアスを指定する場合、
596-
カラムリスト展開コメントにも同じエイリアスを指定してください
596+
選択カラムリスト展開コメントにも同じエイリアスを指定してください
597597

598598
.. code-block:: sql
599599
@@ -605,6 +605,49 @@ SQL 上でテーブルにエイリアスを指定する場合、
605605
606606
select e.id, e.name, e.age from employee e
607607
608+
.. _populate:
609+
610+
更新カラムリスト生成コメント
611+
-----------------------------
612+
613+
populate
614+
~~~~~~~~
615+
616+
UPDATE文のSET節 を :doc:`entity` の定義を
617+
参照して自動で生成する式を更新カラムリスト生成コメントと呼びます。
618+
構文は次のとおりです。
619+
620+
::
621+
622+
/*%populate*/
623+
624+
625+
例を示します。
626+
627+
.. code-block:: sql
628+
629+
update employee set /*%populate*/ id = id where age < 30
630+
631+
上記のSQL文への入力が次のような :doc:`entity` にマッピングされているものとします。
632+
633+
.. code-block:: java
634+
635+
@Entity
636+
public class Employee {
637+
Integer id;
638+
String name;
639+
Integer age;
640+
}
641+
642+
このとき、 SQL は以下のように変換されます。
643+
644+
.. code-block:: sql
645+
646+
update employee set id = ?, name = ?, age = ? where age < 30
647+
648+
更新カラムリスト生成コメントは、 ``/*%populate*/`` からWHERE句までをカラムリストで置き換えます。
649+
つまり、元のSQLにあった ``id = id`` の記述は最終的なSQLからは削除されます。
650+
608651
通常のブロックコメント
609652
----------------------
610653

@@ -649,4 +692,3 @@ SQL 上でテーブルにエイリアスを指定する場合、
649692
``--`` は通常の行コメントだとみなされます。
650693

651694
Domaでは行コメントを特別に解釈することはありません。
652-

0 commit comments

Comments
 (0)