Skip to content

Commit d518b6f

Browse files
authored
Merge pull request #181 from domaframework/sql-processor
SQLテンプレートで組み立てられたSQLを扱うための `@SqlProcessor` を追加
2 parents a5498d8 + 9c0c615 commit d518b6f

File tree

141 files changed

+2329
-795
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+2329
-795
lines changed

docs/sources/query/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
procedure
1717
factory
1818
script
19+
sql-processor
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
==================
2+
SQLプロセッサ
3+
==================
4+
5+
.. contents:: 目次
6+
:depth: 3
7+
8+
SQLテンプレートで組み立てられたSQLをアプリケーションで扱うには、 ``@SqlProcessor`` をDaoのメソッドに注釈します。
9+
10+
.. code-block:: java
11+
12+
@Config(config = AppConfig.class)
13+
public interface EmployeeDao {
14+
@SqlProcessor
15+
<R> R process(Integer id, BiFunction<Config, PreparedSql, R> handler);
16+
...
17+
}
18+
19+
メソッドに対応する :doc:`../sql` が必須です。
20+
21+
.. warning::
22+
23+
SQLプロセッサを使ってSQLを組み立て実行する場合、潜在的には常にSQLインジェクションのリスクがあります。
24+
まずは、他のクエリもしくはクエリビルダを使う方法を検討してください。
25+
また、SQLプロセッサでは信頼できない値をSQLの組み立てに使わないように注意してください。
26+
27+
戻り値
28+
==================
29+
30+
メソッドの戻り値は任意の型にできます。
31+
ただし、 ``BiFunction`` 型のパラメータの3番目の型パラメータと合わせる必要があります。
32+
33+
なお、戻り値の型を ``void`` にする場合、 ``BiFunction`` 型のパラメータの3番目の型パラメータには ``Void`` を指定します。
34+
35+
.. code-block:: java
36+
37+
@SqlProcessor
38+
void process(Integer id, BiFunction<Config, PreparedSql, Void> handler);
39+
40+
パラメータ
41+
==================
42+
43+
``BiFunction`` 型のパラメータを1つのみ含める必要があります。
44+
``BiFunction`` 型のパラメータはSQLテンプレート処理後のSQLを処理するために使われます。
45+
46+
その他のパラメータはSQLテンプレートで参照できます。
47+
基本的には、 :doc:`select` の問い合わせ条件に指定できるのと同じ型を使用できます。
48+
49+
利用例
50+
==================
51+
52+
例えば、SQLテンプレートで処理したSQLをさらに変形し直接実行することができます。(この例では例外処理を省略しています。)
53+
54+
.. code-block:: java
55+
56+
EmployeeDao dao = ...
57+
dao.process(1, (config, preparedSql) -> {
58+
String sql = preparedSql.getRawSql();
59+
String anotherSql = createAnotherSql(sql);
60+
DataSource dataSource = config.getDataSource()
61+
Connection connection = dataSource.getConnection();
62+
PreparedStatement statement = connection.prepareStatement(anotherSql);
63+
return statement.execute();
64+
});
65+
66+
.. code-block:: sql
67+
68+
select * from employee where id = /*^ id */0
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2004-2010 the Seasar Foundation and the Others.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
* either express or implied. See the License for the specific language
14+
* governing permissions and limitations under the License.
15+
*/
16+
package org.seasar.doma;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* SQLテンプレートで組み立てられたSQLを扱うことを示します。
25+
* <p>
26+
* このアノテーションが注釈されるメソッドは、Daoインタフェースのメンバでなければいけません。
27+
*
28+
* <h3>例:</h3>
29+
*
30+
* <pre>
31+
* &#064;Dao(config = AppConfig.class)
32+
* public interface EmployeeDao {
33+
* &#64;SqlProcessor
34+
* &lt;R&gt; R process(Integer id, BiFunction&lt;Config, PreparedSql, R&gt; handler);
35+
* }
36+
* </pre>
37+
*
38+
* @author nakamura
39+
* @since 2.14.0
40+
*/
41+
@Target(ElementType.METHOD)
42+
@Retention(RetentionPolicy.RUNTIME)
43+
@DaoMethod
44+
public @interface SqlProcessor {
45+
46+
}

0 commit comments

Comments
 (0)