Newer Older
1226 lines | 37.919kb
added experimental expand me...
yuki-kimoto authored on 2010-10-20
1
=encoding utf8
2

            
updated document
yuki-kimoto authored on 2010-10-22
3
=head1 NAME
added experimental expand me...
yuki-kimoto authored on 2010-10-20
4

            
update pod
Yuki Kimoto authored on 2011-01-28
5
DBIx::Custom::Guide::Ja - DBIx::Customのガイドブック
added experimental expand me...
yuki-kimoto authored on 2010-10-20
6

            
7
=head1 ガイド
8

            
update pod
Yuki Kimoto authored on 2011-01-28
9
L<DBIx::Custom>はSQLの実行を簡単に行うためのクラスです。
10
L<DBIx::Class>やL<DBIx::Simple>と同じように
updated document
Yuki Kimoto authored on 2011-01-20
11
L<DBI>のラッパクラスになっています。L<DBIx::Class>よりも簡単に、
12
L<DBIx::Simple>よりもはるかに柔軟なことを行うことができます。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
13

            
14
L<DBIx::Custom>はO/Rマッパーではありません。O/Rマッパーは
15
便利ですが、O/Rマッパのたくさんの文法を覚える必要があります。
updated document
Yuki Kimoto authored on 2011-01-20
16
また、O/Rマッパによって生成されたSQLは非効率なことがありますし、
17
複雑なSQLを生成することができないので、
update pod
Yuki Kimoto authored on 2011-01-31
18
生のSQLを実行しなければならない場合がたくさんあります。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
19

            
updated document
Yuki Kimoto authored on 2011-01-20
20
L<DBIx::Custom>はO/Rマッパとは対照的な設計が行われています。
updated document
Yuki Kimoto authored on 2011-01-20
21
L<DBIx::Custom>の主な目的は、SQLを尊重しつつ、L<DBI>だけでは
updated document
Yuki Kimoto authored on 2011-01-20
22
とてもめんどうな作業を簡単にすることです。もしSQLについて
23
多くの知識を持っているならば、L<DBIx::Custom>でそのまま
updated document
Yuki Kimoto authored on 2011-01-20
24
活用することができます。
updated document
Yuki Kimoto authored on 2011-01-20
25

            
update pod
Yuki Kimoto authored on 2011-01-28
26
L<DBIx::Custom>の仕組みを少しだけ説明しておきます。
updated document
Yuki Kimoto authored on 2011-01-20
27
L<DBIx::Custom>では、タグと呼ばれるものを
28
SQLの中に埋め込むことができます。
29

            
30
    select * from book where {= title} and {=author};
31

            
32
{}で囲まれた部分がタグです。このSQLは実際に実行されるときには
33
次のようにプレースホルダに展開されます。
34

            
updated document
Yuki Kimoto authored on 2011-01-20
35
    select * from book where title = ? and author = ?;
updated document
Yuki Kimoto authored on 2011-01-20
36

            
37
これらの展開にはどのような意味があるのでしょうかと質問
update pod
Yuki Kimoto authored on 2011-01-28
38
されるかもしれません。この簡単な仕組みの上に
39
便利な機能が実装されます。それは以下のようなものです。
updated document
Yuki Kimoto authored on 2011-01-20
40

            
updated document
Yuki Kimoto authored on 2011-01-20
41
=over 4
updated document
Yuki Kimoto authored on 2011-01-20
42

            
update pod
Yuki Kimoto authored on 2011-01-28
43
=item 1. プレースホルダにバインドする値をハッシュリファレンスで指定
updated document
Yuki Kimoto authored on 2011-01-20
44

            
update pod
Yuki Kimoto authored on 2011-01-28
45
L<DBI>を使うのであればプレースホルダにバインドする値は配列
updated document
Yuki Kimoto authored on 2011-01-20
46
で指定する必要があります。
updated document
Yuki Kimoto authored on 2011-01-20
47

            
updated document
Yuki Kimoto authored on 2011-01-20
48
    $sth->execute(@bind);
49

            
50
L<DBIx::Custom>を利用するのであればハッシュリファレンスで指定すること
51
できます。
52
    
53
    my $param = {title => 'Perl', author => 'Ken'};
54
    $dbi->execute($sql, $param);
55

            
update pod
Yuki Kimoto authored on 2011-01-28
56
=item 2. 値のフィルタリング
updated document
Yuki Kimoto authored on 2011-01-20
57

            
update pod
Yuki Kimoto authored on 2011-01-28
58
L<DBIx::Custom>はフィルタリングの機能を提供します。
updated document
Yuki Kimoto authored on 2011-01-20
59
たとえば、日付の列は、Perlで扱うときにはC<Time::Piece>などの日付オブジェクト
60
で扱い、データベースに格納するときはデータベースの日付型に変換したい
61
と思うのではないでしょうか。またデータベースから取り出すときは
62
データベースの日付型から日付オブジェクトに変換したと思うのでは
63
ないでしょうか。
64

            
65
このようなときはフィルタ機能を使うことができます。
66

            
67
まずフィルタを登録します。
68

            
69
    $dbi->register_filter(
70
        tp_to_date => sub {
71
            ...
72
        },
73
        date_to_tp => sub {
74
            ...
75
        }
76
    );
77

            
78
次にテーブルの各列にこのフィルタを適用します。
79

            
80
    $dbi->apply_filter('book',
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
81
        'issue_date' => {out => 'tp_to_date', in => 'date_to_tp'}
updated document
Yuki Kimoto authored on 2011-01-20
82
    );
83

            
84
outはPerlからデータベースに保存する方向、inはデータベースからPerlに取得する方向です。
85

            
update pod
Yuki Kimoto authored on 2011-01-28
86
多くのメソッドで自動的にこのフィルタが有効になります。
updated document
Yuki Kimoto authored on 2011-01-20
87

            
update pod
Yuki Kimoto authored on 2011-01-28
88
    $dbi->insert(table => 'book', param => {issue_date => $tp});
updated document
Yuki Kimoto authored on 2011-01-20
89

            
90
=item 3. 選択的な検索条件
91

            
update pod
Yuki Kimoto authored on 2011-01-28
92
L<DBI>では選択的に検索条件を作成することは難しいです。
updated document
Yuki Kimoto authored on 2011-01-20
93

            
94
たとえば、検索条件にtitleとauthorが指定された場合は次のSQLを
95

            
96
    select * from book where title = ? and author = ?;
97

            
98
titleだけの場合は次のSQLを
99

            
100
    select * from book where title = ?;
101
    
update pod
Yuki Kimoto authored on 2011-01-31
102
authorだけの場合は次のSQLを実行した場合を考えましょう。
updated document
Yuki Kimoto authored on 2011-01-20
103

            
104
    select * from book where author = ?;
105

            
106
これはとても大変な作業なので、通常はL<SQL::Abstract>を動的に生成してくれる
107
モジュールを利用することになります。
108

            
109
L<DBIx::Custom>はさらに簡単で便利な方法を用意しています。
110

            
update pod
Yuki Kimoto authored on 2011-01-28
111
    # Whereオブジェクト
updated document
Yuki Kimoto authored on 2011-01-20
112
    my $where = $dbi->where;
update pod
Yuki Kimoto authored on 2011-01-28
113
    
114
    # 検索条件
updated document
Yuki Kimoto authored on 2011-01-20
115
    $where->clause(
116
        ['and', '{= title}', {'= author'}]
117
    );
update pod
Yuki Kimoto authored on 2011-01-28
118
    
119
    # 必要な列を自動的に選択するための設定
120
    $where->param({title => 'Perl'});
updated document
Yuki Kimoto authored on 2011-01-20
121

            
update pod
Yuki Kimoto authored on 2011-01-28
122
    # SQLへのWhere句の埋め込み
updated document
Yuki Kimoto authored on 2011-01-20
123
    my $sql = "select * from book $where";
124

            
125
詳しい説明は後ほど行いますが、上記のように記述すれば、
126
L<DBIx::Custom>では選択的な検索条件を持つWhere句を生成することができます。
127
検索条件が入れ子になった構造やorについても対応しています。
128

            
129
=item 4. 挿入、更新、削除、選択を行うためのメソッド
130

            
update pod
Yuki Kimoto authored on 2011-01-30
131
L<DBIx::Custom>では挿入、更新、削除、選択を行うための
132
メソッドを提供しています。
133
C<insert()>, C<update()>, C<delete()>,C<select()>などがあります。
updated document
Yuki Kimoto authored on 2011-01-20
134

            
135
    my $param = {title => 'Perl', author => 'Ken'};
136
    $dbi->insert(table => 'book', param => $param);
137

            
update pod
Yuki Kimoto authored on 2011-01-30
138
=item 5. テーブルのためのメソッドの登録
updated document
Yuki Kimoto authored on 2011-01-20
139

            
update pod
Yuki Kimoto authored on 2011-01-30
140
テーブルのためにメソッドを登録することができます。
updated document
Yuki Kimoto authored on 2011-01-20
141

            
update pod
Yuki Kimoto authored on 2011-01-30
142
    $dbi->table('book')->method(
updated document
Yuki Kimoto authored on 2011-01-20
143
        list => sub {
144
            ...
145
        },
update pod
Yuki Kimoto authored on 2011-01-30
146
        something => sub {
147
            ...
updated document
Yuki Kimoto authored on 2011-01-20
148
        }
149
    );
150

            
update pod
Yuki Kimoto authored on 2011-01-30
151
メソッドの利用です。
updated document
Yuki Kimoto authored on 2011-01-20
152

            
153
    $dbi->table('book')->list;
added experimental expand me...
yuki-kimoto authored on 2010-10-20
154

            
update pod
Yuki Kimoto authored on 2011-01-30
155
多くのO/Rマッパではテーブルのためのクラスを作成する必要がありますが、
156
L<DBIx::Custom>では簡単です。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
157

            
updated document
Yuki Kimoto authored on 2011-01-20
158
=back
159

            
update pod
Yuki Kimoto authored on 2011-01-30
160
L<DBIx::Custom>はとても便利です。
161
興味をもたれた方は、続きをご覧になってみてください。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
162

            
163
=head2 1. データベースへの接続
164

            
update pod
Yuki Kimoto authored on 2011-01-30
165
L<DBIx::Custom>を読み込みます。
updated document
Yuki Kimoto authored on 2011-01-23
166

            
167
    use DBIx::Custom;
168

            
update pod
Yuki Kimoto authored on 2011-01-30
169
データベースに接続するにはC<connect()>メソッドを使用します。
170
戻り値はL<DBIx::Custom>オブジェクトです。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
171

            
updated document
Yuki Kimoto authored on 2011-01-23
172
    my $dbi = DBIx::Custom->connect(
update pod
Yuki Kimoto authored on 2011-01-30
173
        data_source => "dbi:mysql:database=bookstore",
updated document
Yuki Kimoto authored on 2011-01-23
174
        user => 'ken',
175
        password => '!LFKD%$&',
176
        dbi_options => {mysql_enable_utf8 => 1}
177
    );
added experimental expand me...
yuki-kimoto authored on 2010-10-20
178

            
update pod
Yuki Kimoto authored on 2011-01-30
179
C<data_source>はデータベースシステムに応じたものである必要があります。
180
以下はデータソースのサンプルです。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
181

            
updated document
Yuki Kimoto authored on 2011-01-23
182
B<MySQL>
added experimental expand me...
yuki-kimoto authored on 2010-10-20
183

            
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
184
    "dbi:mysql:database=$database"
185
    "dbi:mysql:database=$database;host=$hostname;port=$port"
added experimental expand me...
yuki-kimoto authored on 2010-10-20
186

            
updated document
Yuki Kimoto authored on 2011-01-23
187
B<SQLite>
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
188

            
189
    "dbi:SQLite:dbname=$database"
190
    "dbi:SQLite:dbname=:memory:"
191

            
updated document
Yuki Kimoto authored on 2011-01-23
192
B<PostgreSQL>
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
193

            
194
    "dbi:Pg:dbname=$dbname"
195

            
updated document
Yuki Kimoto authored on 2011-01-23
196
B<Oracle>
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
197

            
198
    "dbi:Oracle:$dbname"
199
    "dbi:Oracle:host=$host;sid=$sid"
200

            
updated document
Yuki Kimoto authored on 2011-01-23
201
B<ODBC(Microsoft Access)>
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
202

            
203
    "dbi:ODBC:driver=Microsoft Access Driver (*.mdb);dbq=hoge.mdb"
204

            
updated document
Yuki Kimoto authored on 2011-01-23
205
B<ODBC(SQL Server)>
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
206

            
207
   "dbi:ODBC:driver={SQL Server};Server=(local);database=test;Trusted_Connection=yes;AutoTranslate=No;"
added experimental expand me...
yuki-kimoto authored on 2010-10-20
208

            
update pod
Yuki Kimoto authored on 2011-01-30
209
認証が必要な場合は、C<user>とC<password>を指定できます。
updated document
Yuki Kimoto authored on 2011-01-23
210

            
update pod
Yuki Kimoto authored on 2011-01-30
211
L<DBIx::Custom>はL<DBI>のラッパークラスです。
212
L<DBI>のデータベースハンドルは取得するにあhC<dbh()>を使用します。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
213

            
214
    my $dbh = $dbi->dbh;
215

            
updated document
Yuki Kimoto authored on 2011-01-23
216
L<DBIx::Custom>ではデータベースハンドル属性にはデフォルトで次のものが設定されます。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
217
    
218
    $dbi->dbh->{RaiseError} = 1;
219
    $dbi->dbh->{PrintError} = 0;
220
    $dbi->dbh->{AutoCommit} = 1;
221

            
update pod
Yuki Kimoto authored on 2011-01-30
222
致命的なエラーが起こるとプログラムは終了します。
223
SQLが実行されると自動的にコミットされます。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
224

            
updated document
Yuki Kimoto authored on 2011-01-23
225
=head2 2. 挿入、更新、削除、選択のためのメソッド
added experimental expand me...
yuki-kimoto authored on 2010-10-20
226

            
update pod
Yuki Kimoto authored on 2011-01-31
227
下記のメソッドがあります。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
228

            
update pod
Yuki Kimoto authored on 2011-01-31
229
=head3 行の挿入 C<insert()>
added experimental expand me...
yuki-kimoto authored on 2010-10-20
230

            
update pod
Yuki Kimoto authored on 2011-01-31
231
データベースに行を挿入するにはC<insert()>を使用します。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
232

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
233
    $dbi->insert(table  => 'book',
added experimental expand me...
yuki-kimoto authored on 2010-10-20
234
                 param  => {title => 'Perl', author => 'Ken'});
235

            
update pod
Yuki Kimoto authored on 2011-01-31
236
C<table>はテーブル名、C<param>は挿入する行のデータです。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
237

            
update pod
Yuki Kimoto authored on 2011-01-31
238
次のSQLが実行されます。
updated document
Yuki Kimoto authored on 2011-01-23
239

            
240
    insert into (title, author) values (?, ?);
added experimental expand me...
yuki-kimoto authored on 2010-10-20
241

            
update pod
Yuki Kimoto authored on 2011-01-26
242
=head3 データの更新 C<update()>
added experimental expand me...
yuki-kimoto authored on 2010-10-20
243

            
update pod
Yuki Kimoto authored on 2011-01-31
244
データベースの行を更新するには、C<update()>を使用します。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
245

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
246
    $dbi->update(table  => 'book', 
added experimental expand me...
yuki-kimoto authored on 2010-10-20
247
                 param  => {title => 'Perl', author => 'Ken'}, 
248
                 where  => {id => 5});
249

            
update pod
Yuki Kimoto authored on 2011-01-31
250
C<table>はテーブル名、C<param>は更新データ、C<where>は
251
条件です。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
252

            
update pod
Yuki Kimoto authored on 2011-01-31
253
次のSQLが実行されます。
updated document
Yuki Kimoto authored on 2011-01-23
254

            
255
    update book set title = ?, author = ?;
added experimental expand me...
yuki-kimoto authored on 2010-10-20
256

            
update pod
Yuki Kimoto authored on 2011-01-31
257
安全のためC<where>のないupdate()を実効することはできません。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
258
もしすべての行を更新したい場合は
updated document
Yuki Kimoto authored on 2011-01-23
259
C<update_all()>を使用してください。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
260

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
261
    $dbi->update_all(table  => 'book', 
added experimental expand me...
yuki-kimoto authored on 2010-10-20
262
                     param  => {title => 'Perl', author => 'Ken'});
263

            
update pod
Yuki Kimoto authored on 2011-01-26
264
=head3 データの削除 C<delete()>
added experimental expand me...
yuki-kimoto authored on 2010-10-20
265

            
update pod
Yuki Kimoto authored on 2011-01-31
266
データベースの行を1件削除するには、C<delete()>を使用します。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
267

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
268
    $dbi->delete(table  => 'book',
added experimental expand me...
yuki-kimoto authored on 2010-10-20
269
                 where  => {author => 'Ken'});
270

            
update pod
Yuki Kimoto authored on 2011-01-31
271
C<table>はテーブル名、C<where>は条件です。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
272

            
update pod
Yuki Kimoto authored on 2011-01-31
273
次のSQLが実行されます。
updated document
Yuki Kimoto authored on 2011-01-23
274

            
275
    delete from book where id = ?;
added experimental expand me...
yuki-kimoto authored on 2010-10-20
276

            
update pod
Yuki Kimoto authored on 2011-01-31
277
安全のためC<where>のないC<delete()>を実効することはできません。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
278
もしすべての行を削除したい場合は
updated document
Yuki Kimoto authored on 2011-01-23
279
C<delete_all()>を使用してください。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
280

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
281
    $dbi->delete_all(table  => 'book');
added experimental expand me...
yuki-kimoto authored on 2010-10-20
282

            
update pod
Yuki Kimoto authored on 2011-01-26
283
=head3 データの選択 C<select()>
added experimental expand me...
yuki-kimoto authored on 2010-10-20
284

            
updated document
Yuki Kimoto authored on 2011-01-23
285
行を選択するにはC<select()>を使用します。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
286

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
287
    my $result = $dbi->select(table => 'book');
added experimental expand me...
yuki-kimoto authored on 2010-10-20
288

            
update pod
Yuki Kimoto authored on 2011-01-31
289
次のSQLが実行されます。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
290

            
updated document
Yuki Kimoto authored on 2011-01-23
291
    select * from book;
added experimental expand me...
yuki-kimoto authored on 2010-10-20
292

            
update pod
Yuki Kimoto authored on 2011-01-31
293
戻り値はL<DBIx::Custom::Result>
updated document
Yuki Kimoto authored on 2011-01-23
294
オブジェクトです。行をフェッチするにはC<fetch()>を使用します。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
295

            
296
    while (my $row = $result->fetch) {
297
        my $title  = $row->[0];
298
        my $author = $row->[1];
299
    }
300

            
update pod
Yuki Kimoto authored on 2011-01-31
301
L<DBIx::Custom::Result>についてはL<3. 行のフェッチ/"3. 行のフェッチ">を見てください。
updated document
Yuki Kimoto authored on 2011-01-23
302

            
update pod
Yuki Kimoto authored on 2011-01-31
303
サンプルを続けます。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
304

            
305
    my $result = $dbi->select(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
306
        table  => 'book',
updated document
Yuki Kimoto authored on 2011-01-23
307
        column => ['author',  'title'],
added experimental expand me...
yuki-kimoto authored on 2010-10-20
308
        where  => {author => 'Ken'}
309
    );
310

            
update pod
Yuki Kimoto authored on 2011-01-31
311
C<column>は列名、C<where>は条件です。
312

            
313
次のSQLが実行されます。
updated document
Yuki Kimoto authored on 2011-01-23
314

            
315
    select author, title from book where author = ?;
added experimental expand me...
yuki-kimoto authored on 2010-10-20
316

            
update pod
Yuki Kimoto authored on 2011-01-31
317
次のサンプルです。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
318

            
319
    my $result = $dbi->select(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
320
        table    => ['book', 'rental'],
updated document
Yuki Kimoto authored on 2011-01-23
321
        where    => {book.name => 'Perl'},
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
322
        relation => {'book.id' => 'rental.book_id'}
added experimental expand me...
yuki-kimoto authored on 2010-10-20
323
    );
324

            
update pod
Yuki Kimoto authored on 2011-01-31
325
C<relation>テーブル間の関係です。これは内部結合です。
326

            
327
次のSQLが実行されます。
328

            
updated document
Yuki Kimoto authored on 2011-01-23
329
bookテーブルのid列とrentalテーブルのbook_idが関連付けられます。
update pod
Yuki Kimoto authored on 2011-01-31
330
次のSQLが実行されます。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
331

            
updated document
Yuki Kimoto authored on 2011-01-23
332
    select * from book, rental where book.name = ? and book.id = rental.book_id;
added experimental expand me...
yuki-kimoto authored on 2010-10-20
333

            
update pod
Yuki Kimoto authored on 2011-01-31
334
次のサンプルです。
added experimental expand me...
yuki-kimoto authored on 2010-10-20
335

            
336
    my $result = $dbi->select(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
337
        table  => 'book',
added experimental expand me...
yuki-kimoto authored on 2010-10-20
338
        where  => {author => 'Ken'},
updated document
Yuki Kimoto authored on 2011-01-23
339
        append => 'for update',
added experimental expand me...
yuki-kimoto authored on 2010-10-20
340
    );
341

            
update pod
Yuki Kimoto authored on 2011-01-31
342
C<append>はSQLの末尾に追加される文字列です。
343

            
344
次のSQLが実行されます。
updated document
Yuki Kimoto authored on 2011-01-20
345

            
updated document
Yuki Kimoto authored on 2011-01-23
346
    select * book where author = ? for update;
updated document
Yuki Kimoto authored on 2011-01-20
347

            
updated document
Yuki Kimoto authored on 2011-01-23
348
またC<append>は、C<select>だけでなくC<insert()>、C<update()>、C<update_all()>
349
C<delete()>、C<delete_all()>、C<select()>で使用することもできます。
updated document
Yuki Kimoto authored on 2011-01-20
350

            
update pod
Yuki Kimoto authored on 2011-02-11
351
C<column>とC<table>を使用する代わりに、C<selection>を使用することも
352
できます。列名とテーブル名をまとめて指定する場合に利用します。
353
    
354
    my $selection = <<"EOS";
355
    title, author, company_name
356
    from book inner join company on book.company_id = company.id
357
    EOS
358

            
359
    $dbi->select(selection => $selection);
360

            
361
C<selection>においてはwhere句を利用できないということに注意してください。
362
"inner join"などの句を利用してください。
363

            
update pod
Yuki Kimoto authored on 2011-01-26
364
=head3 SQLの実行 C<execute()>
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
365

            
update pod
Yuki Kimoto authored on 2011-01-31
366
SQLを実行するにはC<execute()>を使用します。
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
367

            
368
    $dbi->execute("select * from book;");
369

            
update pod
Yuki Kimoto authored on 2011-01-31
370
タグを処理してSQLを実行します。
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
371

            
372
    $dbi->execute(
373
        "select * from book {= title} and {= author};"
374
        param => {title => 'Perl', author => 'Ken'}
375
    );
376

            
update pod
Yuki Kimoto authored on 2011-01-31
377
次のSQLが実行されます。
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
378

            
379
    select * from book title = ? and author = ?;
380

            
update pod
Yuki Kimoto authored on 2011-01-31
381
プレースホルダにtitleとauthorの値が埋め込まれます。
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
382

            
update pod
Yuki Kimoto authored on 2011-01-31
383
タグについてはL<5. タグ/"5. タグ">を見てください。
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
384

            
update pod
Yuki Kimoto authored on 2011-01-31
385
またC<execute()>のSQLの末尾にはセミコロンを置く必要はありません。
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
386

            
387
    $dbi->execute('select * from book');
388

            
updated document
Yuki Kimoto authored on 2011-01-20
389
=head2 3. 行のフェッチ
390

            
updated document
Yuki Kimoto authored on 2011-01-23
391
C<select()>メソッドの戻り値はL<DBIx::Custom::Result>オブジェクトです。
update pod
Yuki Kimoto authored on 2011-02-04
392
行をフェッチするためのさまざまなメソッドがあります。
updated document
Yuki Kimoto authored on 2011-01-20
393

            
update pod
Yuki Kimoto authored on 2011-01-26
394
=head3 1行づつフェッチ(配列) C<fetch()>
updated document
Yuki Kimoto authored on 2011-01-20
395

            
updated document
Yuki Kimoto authored on 2011-01-23
396
一行フェッチして配列のリファレンスに格納するにはC<fetch()>を使用します。
updated document
Yuki Kimoto authored on 2011-01-20
397

            
update pod
Yuki Kimoto authored on 2011-02-04
398
    my $row = $result->fetch;
399

            
400
以下のようにすべての行を取得することができます。
401

            
updated document
Yuki Kimoto authored on 2011-01-20
402
    while (my $row = $result->fetch) {
updated document
Yuki Kimoto authored on 2011-01-23
403
        my $title  = $row->[0];
404
        my $author = $row->[1];
updated document
Yuki Kimoto authored on 2011-01-20
405
    }
406

            
update pod
Yuki Kimoto authored on 2011-01-26
407
=head3 最初の行だけフェッチ(配列) C<fetch_first()>
updated document
Yuki Kimoto authored on 2011-01-20
408

            
updated document
Yuki Kimoto authored on 2011-01-23
409
一行だけフェッチして配列のリファレンスに格納するにはC<fetch_first()>
410
を使用します。
updated document
Yuki Kimoto authored on 2011-01-20
411

            
412
    my $row = $result->fetch_first;
413

            
update pod
Yuki Kimoto authored on 2011-02-04
414
ステートメントハンドルのC<finish()>が実行される
415
ので残りの行をフェッチできません。
updated document
Yuki Kimoto authored on 2011-01-20
416

            
update pod
Yuki Kimoto authored on 2011-01-26
417
=head3 複数行を順にフェッチ(配列) C<fetch_multi()>
updated document
Yuki Kimoto authored on 2011-01-20
418

            
updated document
Yuki Kimoto authored on 2011-01-23
419
複数行をフェッチして配列のリファレンスを要素に持つ
420
配列のリファレンスに格納するにはC<fetch_multi()>を使用します。
updated document
Yuki Kimoto authored on 2011-01-20
421

            
updated document
Yuki Kimoto authored on 2011-01-23
422
    while (my $rows = $result->fetch_multi(2)) {
423
        my $title0   = $rows->[0][0];
424
        my $author0  = $rows->[0][1];
425
        
426
        my $title1   = $rows->[1][0];
427
        my $author1  = $rows->[1][1];
updated document
Yuki Kimoto authored on 2011-01-20
428
    }
429

            
update pod
Yuki Kimoto authored on 2011-02-04
430
取り出したい行数を引数に指定します。
updated document
Yuki Kimoto authored on 2011-01-23
431

            
update pod
Yuki Kimoto authored on 2011-02-04
432
次のようなデータを取得できます。
updated document
Yuki Kimoto authored on 2011-01-23
433

            
434
    [
435
        ['Perl', 'Ken'],
436
        ['Ruby', 'Mark']
437
    ]
438

            
update pod
Yuki Kimoto authored on 2011-01-26
439
=head3 すべての行をフェッチ(配列) C<fetch_all>
updated document
Yuki Kimoto authored on 2011-01-20
440

            
updated document
Yuki Kimoto authored on 2011-01-23
441
すべての行をフェッチして配列のリファレンスを要素に持つ
442
配列のリファレンスに格納するにはC<fetch_all()>を使用します。
updated document
Yuki Kimoto authored on 2011-01-20
443

            
444
    my $rows = $result->fetch_all;
445

            
updated document
Yuki Kimoto authored on 2011-01-23
446
すべての行を格納した次のようなデータを取得できます。
447

            
448
    [
449
        ['Perl', 'Ken'],
450
        ['Ruby', 'Mark']
451
    ]
452

            
update pod
Yuki Kimoto authored on 2011-01-26
453
=head3 1行づつフェッチ(ハッシュ) C<fetch_hash()>
updated document
Yuki Kimoto authored on 2011-01-20
454

            
updated document
Yuki Kimoto authored on 2011-01-23
455
一行フェッチしてハッシュのリファレンスに格納するにはC<fetch_hash()>を使用します。
updated document
Yuki Kimoto authored on 2011-01-20
456

            
457
    while (my $row = $result->fetch_hash) {
458
        my $title  = $row->{title};
459
        my $author = $row->{author};
460
    }
461

            
update pod
Yuki Kimoto authored on 2011-01-26
462
=head3 最初の行だけフェッチ(ハッシュ) C<fetch_hash_first()>
updated document
Yuki Kimoto authored on 2011-01-20
463

            
updated document
Yuki Kimoto authored on 2011-01-23
464
一行だけフェッチしてハッシュのリファレンスに格納するには
465
C<fetch_hash_first()>を使用します。
updated document
Yuki Kimoto authored on 2011-01-20
466

            
467
    my $row = $result->fetch_hash_first;
468

            
update pod
Yuki Kimoto authored on 2011-02-04
469
ステートメントハンドルのC<finish()>が実行される
470
ので残りの行をフェッチできません。
updated document
Yuki Kimoto authored on 2011-01-20
471

            
update pod
Yuki Kimoto authored on 2011-02-04
472
=head3 複数行をフェッチ(ハッシュ) C<fetch_hash_multi()>
updated document
Yuki Kimoto authored on 2011-01-20
473

            
updated document
Yuki Kimoto authored on 2011-01-23
474
複数行をフェッチしてハッシュのリファレンスを要素に持つ
475
配列のリファレンスに格納するにはC<fetch_hash_multi()>
476
を使用します。
updated document
Yuki Kimoto authored on 2011-01-20
477

            
478
    while (my $rows = $result->fetch_hash_multi(5)) {
updated document
Yuki Kimoto authored on 2011-01-23
479
        my $title0   = $rows->[0]{title};
480
        my $author0  = $rows->[0]{author};
481
        my $title1  = $rows->[1]{title};
482
        my $author1 = $rows->[1]{author};
updated document
Yuki Kimoto authored on 2011-01-20
483
    }
484

            
updated document
Yuki Kimoto authored on 2011-01-23
485
引数には取り出したい行数を指定します。
486

            
update pod
Yuki Kimoto authored on 2011-02-04
487
次のようなデータを取得できます。
updated document
Yuki Kimoto authored on 2011-01-23
488

            
489
    [
490
        {title => 'Perl', author => 'Ken'},
491
        {title => 'Ruby', author => 'Mark'}
492
    ]
493

            
update pod
Yuki Kimoto authored on 2011-01-26
494
=head3 すべての行をフェッチ(ハッシュ) C<fetch_hash_all()>
updated document
Yuki Kimoto authored on 2011-01-20
495

            
updated document
Yuki Kimoto authored on 2011-01-23
496
すべての行をフェッチしてハッシュのリファレンスを要素に持つ
497
配列のリファレンスに格納するにはC<fetch_hash_all()>
498
を使用します。
updated document
Yuki Kimoto authored on 2011-01-20
499

            
500
    my $rows = $result->fetch_hash_all;
501

            
update pod
Yuki Kimoto authored on 2011-02-04
502
次のようなデータを取得できます。
updated document
Yuki Kimoto authored on 2011-01-23
503

            
504
    [
505
        {title => 'Perl', author => 'Ken'},
506
        {title => 'Ruby', author => 'Mark'}
507
    ]
508

            
update pod
Yuki Kimoto authored on 2011-01-26
509
=head3 ステートメントハンドル C<sth()>
updated document
Yuki Kimoto authored on 2011-01-23
510

            
update pod
Yuki Kimoto authored on 2011-02-04
511
ステートメントハンドル取得したい場合は
512
<sth()>を使用します。
updated document
Yuki Kimoto authored on 2011-01-20
513

            
514
    my $sth = $result->sth;
515

            
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
516
=head2 4. フィルタリング
517

            
update pod
Yuki Kimoto authored on 2011-02-07
518
L<DBIx::Custom>は値のフィルタリング機能を提供します。
519

            
520
たとえば、データをデータベースに登録するときは
521
L<Time::Piece>オブジェクトからデータベースの日付のフォーマットに、
522
データベースからデータを取得するときは、
523
データベースの日付のフォーマットからL<Time::Piece>オブジェクト
524
に変換を行いたいと思うことでしょう。
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
525

            
update pod
Yuki Kimoto authored on 2011-01-26
526
=head3 フィルタの登録 C<register_filter()>
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
527

            
528
フィルタを登録するにはC<register_filter()>を使用します。
529

            
530
    $dbi->register_filter(
531
        # Time::Piece object to DATE format
532
        tp_to_date => sub {
533
            my $date = shift;
534

            
535
            return '0000-00-00' unless $tp;
536
            return $tp->strftime('%Y-%m-%d');
537
        },
538
        
539
        # DATE to Time::Piece object
540
        date_to_tp => sub {
541
            my $date = shift;
542

            
543
            return if $date eq '0000-00-00';
544
            return Time::Piece->strptime($date, '%Y-%m-%d');
545
        },
546
    );
547

            
548
登録したフィルタはC<apply_filter()>などで利用することができます。
549

            
update pod
Yuki Kimoto authored on 2011-01-26
550
=head3 フィルタの適用 C<apply_filter()>
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
551

            
552
作成したフィルタを適用するには、C<apply_filter()>を使用します。
553

            
554
    $dbi->apply_filter('book',
555
        issue_date => {out => 'tp_to_date', in => 'date_to_tp'},
556
        first_issue_date => {out => 'tp_to_date', in => 'date_to_tp'}
557
    );
558

            
update pod
Yuki Kimoto authored on 2011-02-07
559
第一引数はテーブル名です。第1引数より後の引数は、列名とフィルタルールのペアを記述します。
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
560
フィルタルールのoutには、データベースにデータを送信するときに適用するフィルタを、
561
フィルタルールのinには、データベースからデータを取得するときに適用するフィルタを
update pod
Yuki Kimoto authored on 2011-02-07
562
記述します。
563

            
564
フィルタとしてコードリファレンスを
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
565
指定することもできます。
566

            
567
    issue_date => {out => sub { ... }, in => sub { ... }}
568

            
569
適用されたフィルタはC<insert()>、C<update()>、C<update_all()>、C<delete()>、
570
C<delete_all()>、C<select()>で有効になります。
571

            
572
    my $tp = Time::Piece->strptime('2010/10/14', '%Y/%m/%d');
573
    my $result = $dbi->select(table => 'book', where => {issu_date => $tp});
574

            
575
データベースにデータが送信されるときに、L<Time::Piece>オブジェクトは
576
データベースの日付のフォーマット「2010-10-14」に変換されます。
577

            
update pod
Yuki Kimoto authored on 2011-02-07
578
データをフェッチするときには、データベースの日付のフォーマットは
579
L<Time::Piece>オブジェクトに変換されます。
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
580

            
581
    my $row = $resutl->fetch_hash_first;
582
    my $tp = $row->{issue_date};
583

            
update pod
Yuki Kimoto authored on 2011-02-07
584
テーブル名を含む列名を使用することもできます。
removed experimental expand
Yuki Kimoto authored on 2011-01-26
585

            
586
    $dbi->select(
587
        table => 'book',
588
        where => {'book.title' => 'Perl', 'book.author' => 'Ken'}
589
    );
590

            
update pod
Yuki Kimoto authored on 2011-02-11
591
フェッチを行う場合に"TABLE__COLUMN"という名前を使用した場合もフィルタは
592
有効になります。
593

            
594
    my $result = $dbi->execute(
595
       "select issue_date as book__issue_date from book");
596

            
597
C<in>フィルタの後に実行されるC<end>フィルタを適用することもできます。
598

            
599
    $dbi->apply_filter('book',
600
        issue_date => {out => 'tp_to_date', in => 'date_to_tp',
601
                       end => 'tp_to_displaydate'},
602
    );
603

            
update pod
Yuki Kimoto authored on 2011-02-07
604
=head3 個別のフィルタ C<filter>
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
605

            
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
606
個別にフィルタを適用することもできます。
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
607
個別のフィルタはC<apply_filter()>で適用したフィルタを上書きます。
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
608

            
update pod
Yuki Kimoto authored on 2011-02-07
609
データを送信する場合に個別のフィルタを適用するには、C<filter>オプションを使用します。
610
このオプションはC<insert()>、C<update()>、
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
611
C<update_all()>、C<delete()>、C<delete_all()>、C<select()>、C<execute()>
612
で使用することができます。
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
613

            
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
614
    $dbi->insert(
615
        table => 'book',
616
        param => {issue_date => $tp, first_issue_date => $tp},
617
        filter => {issue_date => 'tp_to_date', first_issue_date => 'tp_to_date'}
618
    );
619

            
620
C<execute()>の例を示します。
621

            
622
my $sql = <<"EOS";
623
select YEAR(issue_date) as issue_year
624
from book
625
where YEAR(issue_date) = {? issue_year}
626
EOS
627
   
628
    my $result = $dbi->execute(
629
        $sql,
630
        param => {issue_year => '2010'},
631
        filter => {issue_year => 'tp_to_year'}
632
    );
633

            
update pod
Yuki Kimoto authored on 2011-02-07
634
行をフェッチするときにも個別のフィルタを適用することができます。
635
C<DBIx::Custom::Result>のC<filter()>を使用します。
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
636

            
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
637
    $result->filter(issue_year => 'year_to_tp');
638

            
update pod
Yuki Kimoto authored on 2011-02-07
639
=head3 最後のフィルタリング : C<end_filter()>
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
640

            
update pod
Yuki Kimoto authored on 2011-02-07
641
最後にもうひとつフィルタを追加することができます。
642
最終的な出力を作成する場合に便利です。
643
最後のフィルタを登録するにはC<end_filter()>を使用します。
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
644

            
645
    $result->end_filter(issue_date => sub {
646
        my $tp = shift;
647
        
648
        return '' unless $tp;
649
        return $tp->strftime('%Y/%m/%d %h:%m:%s (%a)');
650
    });
651

            
update pod
Yuki Kimoto authored on 2011-02-07
652
この例ではL<Time::Piece>オブジェクトを読みやすい書式に変換しています。
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
653

            
update pod
Yuki Kimoto authored on 2011-02-07
654
=head3 フィルタの適用の自動化 C<each_column()>
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
655

            
update pod
Yuki Kimoto authored on 2011-02-07
656
日付型の列は自動的にフィルタを適用できると便利です。
657
列のすべての情報を処理するためのC<each_column()>を利用することができます。
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
658

            
659
    $dbi->each_column(
660
        sub {
661
            my ($self, $table, $column, $info) = @_;
662
            
663
            my $type = $info->{TYPE_NAME};
664
            
665
            my $filter = $type eq 'DATE'     ? {out => 'tp_to_date', in => 'date_to_tp'}
666
                       : $type eq 'DATETIME' ? {out => 'tp_to_datetime', in => 'datetime_to_tp'}
667
                                             : undef;
668
            
669
            $self->apply_filter($table, $column, $filter)
670
              if $filter;
671
        }
672
    );
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
673

            
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
674
each_columnはコールバックを受け取ります。コールバックの引数は
update pod
Yuki Kimoto authored on 2011-02-07
675
L<DBIx::Custom>オブジェクト、テーブル名、列名、列の情報です。
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
676
列の型名の情報をもとに自動的に、フィルタを適用しています。
677

            
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
678
=head2 5. タグ
updated document
Yuki Kimoto authored on 2011-01-20
679

            
update pod
Yuki Kimoto authored on 2011-02-07
680
=head3 タグの基本
updated document
Yuki Kimoto authored on 2011-01-20
681

            
update pod
Yuki Kimoto authored on 2011-02-07
682
SQLの中にタグを埋め込むことができます。
updated document
Yuki Kimoto authored on 2011-01-20
683

            
update pod
Yuki Kimoto authored on 2011-01-26
684
    select * from book where {= title} and {like author};
updated document
Yuki Kimoto authored on 2011-01-20
685

            
update pod
Yuki Kimoto authored on 2011-01-26
686
{= title}と{like author}の部分がタグです。タグは次のような形式
687
を持ちます。
updated document
Yuki Kimoto authored on 2011-01-20
688

            
update pod
Yuki Kimoto authored on 2011-01-26
689
    {タグ名 引数1 引数2 ...}
690
    
691
タグはC<{>で始まり、C<}>で終わります。最初のC<{>とタグ名の間
692
には空白を挿入しないよう注意してください。
updated document
Yuki Kimoto authored on 2011-01-20
693

            
update pod
Yuki Kimoto authored on 2011-02-07
694
C<{>とC<}>は予約語になっています。
695
もし利用したい場合は\でエスケープを行う必要があります。
updated document
Yuki Kimoto authored on 2011-01-20
696

            
update pod
Yuki Kimoto authored on 2011-01-26
697
    select from book \\{ ... \\}
698

            
699
C<\>自体がPerlのエスケープ文字ですので、
update pod
Yuki Kimoto authored on 2011-02-07
700
C<\>は二つ必要になります。
update pod
Yuki Kimoto authored on 2011-01-26
701

            
update pod
Yuki Kimoto authored on 2011-02-07
702
タグはSQLが実行される前に展開されます。
update pod
Yuki Kimoto authored on 2011-01-26
703

            
704
    select * from book where title = ? and author like ?;
updated document
Yuki Kimoto authored on 2011-01-20
705

            
update pod
Yuki Kimoto authored on 2011-01-26
706
タグを含むSQLを実行するにはC<execute()>を使用します。
updated document
Yuki Kimoto authored on 2011-01-20
707

            
update pod
Yuki Kimoto authored on 2011-01-26
708
    my $sql = "select * from book where {= author} and {like title};"
709
    $dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'});
updated document
Yuki Kimoto authored on 2011-01-20
710

            
update pod
Yuki Kimoto authored on 2011-01-26
711
C<param>オプションを使って、プレースホルダに埋め込みたい値を
712
ハッシュリファレンスで指定することができます。
updated document
Yuki Kimoto authored on 2011-01-20
713

            
update pod
Yuki Kimoto authored on 2011-02-07
714
C<execute()>においてもC<filter>を指定することができます。
update pod
Yuki Kimoto authored on 2011-01-26
715

            
716
    $dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'}
717
                  filter => {title => 'to_something');
718

            
update pod
Yuki Kimoto authored on 2011-02-07
719
C<execute>ではC<apply_filter()>で適用されたフィルタ
720
は有効ではないということに注意してください。
update pod
Yuki Kimoto authored on 2011-01-26
721
C<apply_filter()>で適用されたフィルタを有効にするには、
update pod
Yuki Kimoto authored on 2011-02-11
722
C<table>タグを利用します。
update pod
Yuki Kimoto authored on 2011-01-26
723

            
update pod
Yuki Kimoto authored on 2011-02-11
724
    my $sql = "select * from {table book} where {= author} and {like title};"
update pod
Yuki Kimoto authored on 2011-01-26
725

            
update pod
Yuki Kimoto authored on 2011-01-31
726
=head3 タグ一覧
update pod
Yuki Kimoto authored on 2011-01-26
727

            
update pod
Yuki Kimoto authored on 2011-02-11
728
=head4 C<table>
729

            
730
    {table NAME} -> NAME
731

            
732
これはSQLの中でテーブル名を指定する場合に利用します。
733
テーブル名を指定することによって、C<apply_filter()>
734
によるフィルタリングが有効になります。
735

            
update pod
Yuki Kimoto authored on 2011-01-31
736
=head4 C<?>
update pod
Yuki Kimoto authored on 2011-01-26
737

            
738
    {? NAME}    ->   ?
739

            
update pod
Yuki Kimoto authored on 2011-01-31
740
=head4 C<=>
update pod
Yuki Kimoto authored on 2011-01-26
741

            
742
    {= NAME}    ->   NAME = ?
743

            
update pod
Yuki Kimoto authored on 2011-01-31
744
=head4 C<E<lt>E<gt>>
update pod
Yuki Kimoto authored on 2011-01-26
745

            
746
    {<> NAME}   ->   NAME <> ?
747

            
update pod
Yuki Kimoto authored on 2011-01-31
748
=head4 C<E<lt>>
update pod
Yuki Kimoto authored on 2011-01-26
749

            
750
    {< NAME}    ->   NAME < ?
751

            
update pod
Yuki Kimoto authored on 2011-01-31
752
=head4 C<E<gt>>
update pod
Yuki Kimoto authored on 2011-01-26
753

            
754
    {> NAME}    ->   NAME > ?
755

            
update pod
Yuki Kimoto authored on 2011-01-31
756
=head4 C<E<gt>=>
update pod
Yuki Kimoto authored on 2011-01-26
757

            
758
    {>= NAME}   ->   NAME >= ?
759

            
update pod
Yuki Kimoto authored on 2011-01-31
760
=head4 C<E<lt>=>
update pod
Yuki Kimoto authored on 2011-01-26
761

            
762
    {<= NAME}   ->   NAME <= ?
763

            
update pod
Yuki Kimoto authored on 2011-01-31
764
=head4 C<like>
update pod
Yuki Kimoto authored on 2011-01-26
765

            
766
    {like NAME}   ->   NAME like ?
767

            
update pod
Yuki Kimoto authored on 2011-01-31
768
=head4 C<in>
update pod
Yuki Kimoto authored on 2011-01-26
769

            
770
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
771

            
update pod
Yuki Kimoto authored on 2011-01-31
772
=head4 C<insert_param>
update pod
Yuki Kimoto authored on 2011-01-26
773

            
updated document
Yuki Kimoto authored on 2011-01-20
774
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
update pod
Yuki Kimoto authored on 2011-01-26
775

            
update pod
Yuki Kimoto authored on 2011-01-31
776
=head4 C<update_param>
update pod
Yuki Kimoto authored on 2011-01-26
777

            
updated document
Yuki Kimoto authored on 2011-01-20
778
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
779

            
update pod
Yuki Kimoto authored on 2011-01-31
780
=head3 同名の列の扱い
updated document
Yuki Kimoto authored on 2011-01-20
781

            
update pod
Yuki Kimoto authored on 2011-02-07
782
同名の列を含むタグがある場合でも大丈夫です。
783
二つの日付で比較しなければならない場合を
update pod
Yuki Kimoto authored on 2011-01-26
784
考えて見ましょう。
updated document
Yuki Kimoto authored on 2011-01-20
785

            
update pod
Yuki Kimoto authored on 2011-01-26
786
    my $sql = "select * from table where {> date} and {< date};";
updated document
Yuki Kimoto authored on 2011-01-20
787

            
update pod
Yuki Kimoto authored on 2011-02-07
788
このような場合はパラメータの値を配列のリファレンスで指定します。
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
789

            
update pod
Yuki Kimoto authored on 2011-01-26
790
    my $dbi->execute($sql, param => {date => ['2010-10-01', '2012-02-10']});
791

            
update pod
Yuki Kimoto authored on 2011-02-07
792
=head3 タグの登録 C<register_tag()>
update pod
Yuki Kimoto authored on 2011-01-26
793

            
update pod
Yuki Kimoto authored on 2011-02-07
794
独自のタグを登録することができます。
update pod
Yuki Kimoto authored on 2011-01-26
795
タグを追加するにはC<register_tag()>を使用します。
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
796

            
797
    $dbi->register_tag(
update pod
Yuki Kimoto authored on 2011-01-26
798
        '=' => sub {
799
            my $column = shift;
800
            
801
            return ["$column = ?", [$column]];
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
802
        }
803
    );
804

            
update pod
Yuki Kimoto authored on 2011-01-26
805
ここではデフォルトのC<=>タグがどのように実装されているかを示しています。
update pod
Yuki Kimoto authored on 2011-02-11
806
タグの形式は次のようになっています。
update pod
Yuki Kimoto authored on 2011-01-26
807

            
update pod
Yuki Kimoto authored on 2011-02-11
808
    {タグ名 引数1 引数2 ...}
update pod
Yuki Kimoto authored on 2011-01-26
809

            
810
C<=>タグの場合は
811

            
812
    {= title}
813

            
814
という形式ですから、サブルーチンにはtitleというひとつの列名がわたってきます。
815

            
816
サブルーチンの戻り値には次の形式の配列のリファレンスを返す必要があります。
817

            
818
    [
819
        展開後の文字列,
820
        [プレースホルダに埋め込みに利用する列名1, 列名2, ...]
821
    ]
822

            
823
一つ目の要素は展開後の文字列です。この例では
824

            
825
    'title = ?'
826

            
827
を返す必要があります。
828

            
829
二つ目の要素はプレースホルダに埋め込みに利用する列名を含む配列の
830
リファレンスです。今回の例では
831

            
832
    ['title']
833

            
834
を返す必要があります。複数のプレースホルダを含む場合は、この部分が
update pod
Yuki Kimoto authored on 2011-02-11
835
複数になります。
update pod
Yuki Kimoto authored on 2011-01-26
836

            
837
上記を合わせると
838

            
839
    ['title = ?', ['title']]
840
    
841
を返す必要があるということです。
842

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
843
タグの実装の他のサンプルはL<DBIx::Custom::Tag>のソースコード
844
をご覧になってみてください。
845

            
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
846
=head2 6. Where句の動的な生成
847

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
848
=head3 Where句の動的な生成 where()
849

            
850
複数の検索条件を指定して、検索を行いたい場合があります。
851
次の3つのケースのwhere句を考えてみましょう。
852

            
853
titleの値だけで検索したい場合
854

            
855
    where {= title}
856

            
857
authorの値だけで検索したい場合
858

            
859
    where {= author}
860

            
861
titleとauthorの両方の値で検索したい場合
862

            
863
    where {= title} and {=author}
864

            
865
L<DBIx::Custom>では動的なWhere句の生成をサポートしています。
866
まずC<where()>でL<DBIx::Custom::Where>オブジェクトを生成します。
867

            
868
    my $where = $dbi->where;
869

            
870
次にC<clause()>を使用してwhere句を記述します。
871

            
872
    $where->clause(
873
        ['and', '{= title'}, '{= author}']
874
    );
875

            
876
clauseの指定方法は次のようになります。
877

            
878
    ['or' あるいは 'and', タグ1, タグ2, タグ3]
879

            
880
第一引数にはorあるいはandを指定します。第二引数以降には
881
検索条件をタグを使って記述します。
882

            
883
C<clause>の指定は入れ子にすることもでき、さらに複雑な条件
884
を記述することもできます。
885

            
886
    ['and', 
887
      '{= title}', 
888
      ['or', '{= author}', '{like date}']
889
    ]
890

            
update pod
Yuki Kimoto authored on 2011-02-11
891
これは "{=title} and ( {=author} or {like date} )" 意味しています。
892

            
893
C<clause>を設定した後にC<param>にパラメータを指定します。
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
894
    
update pod
Yuki Kimoto authored on 2011-02-11
895
    $where->param({title => 'Perl'});
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
896

            
897
この例ではtitleだけがパラメータに含まれています。
898

            
899
この後C<to_string()>を実行すると$paramに含まれるパラメータを満たす
900
where句を生成することができます。
901

            
902
    my $where_clause = $where->to_string;
903

            
904
パラメータはtitleだけですので、次のようなwhere句が生成されます。
905

            
906
    where {= title}
907

            
cleanup
Yuki Kimoto authored on 2011-01-26
908
またL<DBIx::Custom>は文字列の評価をオーバーロードして、C<to_string()>
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
909
を呼び出すようにしていますので、次のようにしてwhere句を生成することも
910
できます。
911

            
912
    my $where_clause = "$where";
913

            
914
これはSQLの中にwhere句を埋め込むときにとても役立つ機能です。
915

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
916
=head3 同一の列名を含む場合
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
917

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
918
タグの中に同一の名前を持つものが存在した場合でも動的に
919
where句を作成することができます。
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
920

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
921
たとえば、パラメータとして開始日付と終了日付を受け取ったことを
922
考えてみてください。
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
923

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
924
    my $param = {start_date => '2010-11-15', end_date => '2011-11-21'};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
925

            
update pod
Yuki Kimoto authored on 2011-02-11
926
この場合はパラメータの値を配列のリファレンスにしてください。
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
927

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
928
    my $p = {date => ['2010-11-15', '2011-11-21']};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
929

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
930
同名の列を含むタグに順番に埋め込むことができます。
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
931

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
932
    $where->clause(
933
        ['and', '{> date}', '{< date}']
934
    );
935
    $where->param($p);
936

            
937
また開始日付が存在しない場合は次のようなデータを作成します。
938

            
939
    my $p = {date => [$dbi->not_exists, '2011-11-21']};
940

            
update pod
Yuki Kimoto authored on 2011-02-11
941
C<not_exists()>でDBIx::Custom::NotExistsオブジェクトを
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
942
取得できます。これは対応する値が存在しないことを示すためのものです。
943

            
944
また終了日付が存在しない場合は次のようなデータを作成します。
945

            
946
    my $p = {date => ['2010-11-15']};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
947

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
948
どちらも存在しない場合は次のようなデータを作成します。
949

            
950
    my $p = {date => []};
951

            
952
少し難しいので一番簡単に作成できるロジックを示しておきます。
953

            
954
    my @date;
955
    push @date, exists $param->{start_date} ? $param->{start_date}
956
                                            : $dbi->not_exists;
957
    push @date, $param->{end_date} if exists $param->{end_date};
958
    my $p = {date => \@date};
959

            
960
=head3 C<select()>との連携
961

            
update pod
Yuki Kimoto authored on 2011-01-26
962
L<DBIx::Custom::Where>オブジェクトは
963
C<select()>のC<where>に直接渡すことが
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
964
できます。
965
    
966
    my $where = $dbi->where;
967
    $where->clause(...);
968
    $where->param($param);
969
    my $result = $dbi->select(table => 'book', where => $where);
970

            
update pod
Yuki Kimoto authored on 2011-01-26
971
あるいはC<update()>、C<delete()>のwhereに指定することも可能です。
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
972

            
973
=head3 C<execute()>との連携
974

            
975
C<execute()>との連携です。SQLを作成するときに埋め込むことができます。
976

            
977

            
978
    my $where = $dbi->where;
979
    $where->clause(...);
980
    $where->param($param);
981

            
update pod
Yuki Kimoto authored on 2011-02-11
982
    my $sql = <<"EOS";
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
983
    select * from book;
984
    $where
985
    EOS
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
986

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
987
    $dbi->execute($sql, param => $param);
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
988

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
989
=head2 7. モデル
update pod
Yuki Kimoto authored on 2011-01-28
990

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
991
=head3 モデル
update pod
Yuki Kimoto authored on 2011-01-28
992

            
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
993
ソースコードの見通しをよくするために、
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
994
L<DBIx::Custom::Model>を継承してモデルを作成することができます。
update pod
Yuki Kimoto authored on 2011-01-28
995

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
996
    package MyModel::book;
997
    use base 'DBIx::Custom::Model';
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
998
    
999
    sub insert { ... }
1000
    sub list { ... }
update pod
Yuki Kimoto authored on 2011-01-28
1001

            
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1002
このクラスを取り込んで、インスタンス化することができます。
update pod
Yuki Kimoto authored on 2011-01-28
1003

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1004
    $dbi->include_model(
1005
        MyModel => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1006
            'book',
1007
        ]
1008
    );
update pod
Yuki Kimoto authored on 2011-01-28
1009

            
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1010
最初の引数は名前空間です。第二引数は、クラスの末尾の名前を含む
1011
配列のリファレンスです。
update pod
Yuki Kimoto authored on 2011-01-28
1012

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1013
第二引数を指定しなかった場合は、名前空間以下にあるすべてのモデル
1014
が取り込まれます。
update pod
Yuki Kimoto authored on 2011-01-28
1015

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1016
    $dbi->include_model('MyModel');
update pod
Yuki Kimoto authored on 2011-01-28
1017

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1018
ただし、名前空間名そのものを表すモジュールが
1019
存在していることが必要なことに注意してください。
1020

            
1021
    # MyModel.pm
1022
    package MyModel;
1023
    
1024
    use base 'DBIx::Custom::Model';
1025

            
1026
次のようなモジュールの配置が必要です。
1027

            
1028
    MyModel.pm
1029
    MyModel / book.pm
1030
            / company.pm
1031
    
1032
モデルは次のように利用することができます。
1033

            
1034
    my $result = $dbi->model('book')->list;
1035

            
1036
モデルではテーブル名を指定することなしに
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1037
C<insert()>, C<update()>, C<update_all()>,
1038
C<delete()>, C<delete_all()>, C<select()>などのメソッドを
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1039
利用できます。
update pod
Yuki Kimoto authored on 2011-01-28
1040

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1041
    $dbi->model('book')->insert(param => $param);
update pod
Yuki Kimoto authored on 2011-01-28
1042

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1043
モデルはL<DBIx::Custom::Model>です。
update pod
Yuki Kimoto authored on 2011-01-28
1044

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1045
必要であれば、C<table()>でテーブル名を取得することができます。
update pod
Yuki Kimoto authored on 2011-01-28
1046

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1047
    my $table = $model->table;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1048

            
1049
L<DBIx::Custom>オブジェクトを取得することもできます。
1050

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1051
    my $dbi = $model->dbi;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1052

            
1053
L<DBIx::Custom>とL<DBI>のすべてのメソッドを呼び出すこともできます。
update pod
Yuki Kimoto authored on 2011-01-28
1054

            
update pod
Yuki Kimoto authored on 2011-02-11
1055
    # DBIx::Custom method
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1056
    $model->execute($sql);
update pod
Yuki Kimoto authored on 2011-02-11
1057
    
1058
    # DBI method
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1059
    $model->begin_work;
1060
    $model->commit;
update pod
Yuki Kimoto authored on 2011-01-28
1061

            
add models() attribute
Yuki Kimoto authored on 2011-02-21
1062
すべてのモデル名を取得したい場合はC<models()>のキーを取得してください。
1063

            
1064
    my @models = keys %{$self->models};
1065

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
1066
モデルにはプライマリーキーを設定することもできます。
1067

            
1068
   $model->primary_key(['id', 'number_id']);
1069

            
1070
ここで設定したプライマリーキーはC<update_at()>, C<delete_at()>,
1071
C<select_at()>で利用されます。
1072

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1073
=head2 モデルのサンプル
update pod
Yuki Kimoto authored on 2011-02-11
1074

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1075
モデルのサンプルです。
update pod
Yuki Kimoto authored on 2011-01-28
1076

            
1077
    package MyDBI;
1078
    
1079
    use base 'DBIx::Custom';
1080
    
1081
    sub connect {
1082
        my $self = shift->SUPER::connect(@_);
1083
        
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1084
        $self->include_model(
1085
            MyModel => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1086
                'book',
1087
                'company'
1088
            ]
update pod
Yuki Kimoto authored on 2011-01-28
1089
        );
1090
    }
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1091
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1092
    package MyModel::book;
1093
    use base 'DBIx::Custom::Model';
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1094
    
1095
    sub insert { ... }
1096
    sub list { ... }
1097
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1098
    package MyModel::company;
1099
    use base 'DBIx::Custom::Model';
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1100
    
1101
    sub insert { ... }
1102
    sub list { ... }
update pod
Yuki Kimoto authored on 2011-01-28
1103

            
1104
=head2 8. パフォーマンスの改善
updated document
Yuki Kimoto authored on 2011-01-20
1105

            
update pod
Yuki Kimoto authored on 2011-01-26
1106
=head3 クエリの作成
updated document
Yuki Kimoto authored on 2011-01-20
1107

            
update pod
Yuki Kimoto authored on 2011-02-11
1108
パフォーマンスが得られない場合はC<query>オプションを使って
1109
クエリを作成してみてください。
update pod
Yuki Kimoto authored on 2011-01-26
1110

            
update pod
Yuki Kimoto authored on 2011-02-11
1111
    my $params = [
1112
        {title => 'Perl', author => 'Ken'},
1113
        {title => 'Good day', author => 'Tom'}
1114
    ]
1115
    my $query = $dbi->insert(table => 'book', param => $params->[0], query => 1);
update pod
Yuki Kimoto authored on 2011-01-26
1116

            
update pod
Yuki Kimoto authored on 2011-02-11
1117
戻り値はL<DBIx::Custom::Query>オブジェクトです。
1118
作成したクエリはC<execute()>で実行することができます。
update pod
Yuki Kimoto authored on 2011-01-26
1119

            
update pod
Yuki Kimoto authored on 2011-02-11
1120
    foreach my $param (@$params) {
1121
        $dbi->execute($query, $param);
1122
    }
1123

            
1124
ステートメントハンドルが再利用されるので、パフォーマンスが
1125
改善されます。
1126
C<query>オプションはC<insert()>, C<update()>, C<update_all()>,
1127
C<delete()>, C<delete_all()>で利用することができます.
1128

            
1129
クエリを作成するメソッドに渡すパラメータと
1130
C<execute()>に渡すパラメータの個数は同じでなければならない
1131
ことに注意してください。
1132

            
1133
C<create_query()>を使って任意のSQLのクエリを作成
update pod
Yuki Kimoto authored on 2011-01-26
1134
することもできます。
updated document
Yuki Kimoto authored on 2011-01-20
1135

            
1136
    my $query = $dbi->create_query(
update pod
Yuki Kimoto authored on 2011-01-26
1137
        "insert into book {insert_param title author};";
updated document
Yuki Kimoto authored on 2011-01-20
1138
    );
1139

            
1140

            
update pod
Yuki Kimoto authored on 2011-01-28
1141
=head2 9. その他の機能
updated document
Yuki Kimoto authored on 2011-01-20
1142

            
update pod
Yuki Kimoto authored on 2011-01-26
1143
=head3 メソッドの登録
updated document
Yuki Kimoto authored on 2011-01-20
1144

            
update pod
Yuki Kimoto authored on 2011-02-11
1145
L<DBIx::Custom>オブジェクトにメソッドを追加することができます。
update pod
Yuki Kimoto authored on 2011-02-11
1146
C<method()>を使用します。
updated document
Yuki Kimoto authored on 2011-01-20
1147

            
update pod
Yuki Kimoto authored on 2011-01-26
1148
    $dbi->method(
updated document
Yuki Kimoto authored on 2011-01-20
1149
        update_or_insert => sub {
1150
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-01-26
1151
            # something
updated document
Yuki Kimoto authored on 2011-01-20
1152
        },
1153
        find_or_create   => sub {
1154
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-01-26
1155
            # something
updated document
Yuki Kimoto authored on 2011-01-20
1156
        }
1157
    );
1158

            
update pod
Yuki Kimoto authored on 2011-02-11
1159
これらのメソッドは
1160
L<DBIx::Custom>オブジェクトから呼び出すことができます。
updated document
Yuki Kimoto authored on 2011-01-20
1161

            
1162
    $dbi->update_or_insert;
1163
    $dbi->find_or_create;
1164

            
removed experimental expand
Yuki Kimoto authored on 2011-01-26
1165
=head3 結果クラスの変更
1166

            
update pod
Yuki Kimoto authored on 2011-02-11
1167
結果クラスを変更することができます。
1168
デフォルトはL<DBIx::Custom::Result>です。
removed experimental expand
Yuki Kimoto authored on 2011-01-26
1169

            
1170
    package MyResult;
1171
    use base 'DBIx::Custom::Result';
1172
    
1173
    sub some_method { ... }
updated document
Yuki Kimoto authored on 2011-01-20
1174

            
removed experimental expand
Yuki Kimoto authored on 2011-01-26
1175
    1;
1176
    
1177
    package main;
1178
    
1179
    use MyResult;
1180
    
1181
    my $dbi = DBIx::Custom->connect(...);
1182
    $dbi->result_class('MyResult');
updated document
Yuki Kimoto authored on 2011-01-20
1183

            
removed experimental expand
Yuki Kimoto authored on 2011-01-26
1184
=head3 キャッシング
updated document
Yuki Kimoto authored on 2011-01-20
1185

            
update pod
Yuki Kimoto authored on 2011-02-11
1186
タグの解析後のSQLはパフォーマンスの理由のためにキャッシュされます。
removed experimental expand
Yuki Kimoto authored on 2011-01-26
1187
これはC<chace>で設定でき、デフォルトではキャッシュを行う設定です。
updated document
Yuki Kimoto authored on 2011-01-20
1188

            
removed experimental expand
Yuki Kimoto authored on 2011-01-26
1189
    $dbi->cache(1);
updated document
Yuki Kimoto authored on 2011-01-20
1190

            
update pod
Yuki Kimoto authored on 2011-02-11
1191
キャッシュ方法はC<cache_method>で変更することができます。
1192
デフォルトのメソッドは以下のようになっていて、
1193
メモリ上にキャッシュが保存されます。
updated document
Yuki Kimoto authored on 2011-01-20
1194

            
removed experimental expand
Yuki Kimoto authored on 2011-01-26
1195
    $dbi->cache_method(sub {
1196
        sub {
1197
            my $self = shift;
1198
            
1199
            $self->{_cached} ||= {};
1200
            
1201
            if (@_ > 1) {
update pod
Yuki Kimoto authored on 2011-02-11
1202
                # キャッシュの保存
removed experimental expand
Yuki Kimoto authored on 2011-01-26
1203
                $self->{_cached}{$_[0]} = $_[1] 
1204
            }
1205
            else {
update pod
Yuki Kimoto authored on 2011-02-11
1206
                # キャッシュの取得
removed experimental expand
Yuki Kimoto authored on 2011-01-26
1207
                return $self->{_cached}{$_[0]}
1208
            }
1209
        }
1210
    });
1211
    
1212
第一はL<DBIx::Custom>オブジェクトです。
update pod
Yuki Kimoto authored on 2011-02-11
1213
第二引数はタグが解析される前のSQLです。
1214
第三引数はタグの解析後のSQLの情報です。これはハッシュのリファレンスです。
updated document
Yuki Kimoto authored on 2011-01-20
1215

            
update pod
Yuki Kimoto authored on 2011-02-11
1216
第三引数が存在した場合はキャッシュを設定し、
removed experimental expand
Yuki Kimoto authored on 2011-01-26
1217
存在しなかった場合はキャッシュを取得する実装に
1218
してください。
updated document
Yuki Kimoto authored on 2011-01-20
1219

            
update pod
Yuki Kimoto authored on 2011-02-11
1220
=head1 サンプル
1221

            
1222
以下のWikiでサンプルを見ることができます。
1223

            
1224
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki>
1225

            
updated document
Yuki Kimoto authored on 2011-01-20
1226
=cut