Newer Older
1285 lines | 40.293kb
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

            
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
389
=head3 プライマリーキーを利用した行の更新 C<update_at()>
390

            
391
プライマリーを使用して行を更新するにはC<update_at()>を使用します。
392

            
393
    $dbi->update_at(
394
        table => 'book', primary_key => ['id'],
395
        where => ['123'], param => {name => 'Ken'}
396
    );
397

            
398
この例ではidの列が123の行が更新されます。C<where>には、配列の
399
リファレンスを渡す必要があることに注意してください。
400
なおC<param>にプライマリーキーが含まれていた場合は、そのキーが削除さされます。
401

            
402
=head3 プライマリーキーを利用した行の削除 C<delete_at()>
403

            
404
プライマリーを使用して行を削除するにはC<delete_at()>を使用します。
405

            
406
    $dbi->delete_at(table => 'book', primary_key => ['id'], where => ['123']);
407

            
408
この例ではidの列が123の行が削除されます。C<where>には、配列の
409
リファレンスを渡す必要があることに注意してください。
410

            
411
また下のような記述方法も許されています。
412

            
413
    $dbi->delete_at(table => 'book', primary_key => ['id'], param => {id => '123'});
414

            
415
=head3 プライマリーキーを利用した行の選択 C<select_at()>
416

            
417
プライマリーを使用して行を選択するにはC<select_at()>を使用します。
418

            
419
    $dbi->select_at(table => 'book', primary_key => ['id'], where => ['123']);
420

            
421
この例ではidの列が123の行が選択されます。C<where>には、配列の
422
リファレンスを渡す必要があることに注意してください。
423

            
424
また下のような記述方法も許されています。
425

            
426
    $dbi->select_at(table => 'book', primary_key => ['id'], param => {id => '123'});
427

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

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

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

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

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

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

            
updated document
Yuki Kimoto authored on 2011-01-20
441
    while (my $row = $result->fetch) {
updated document
Yuki Kimoto authored on 2011-01-23
442
        my $title  = $row->[0];
443
        my $author = $row->[1];
updated document
Yuki Kimoto authored on 2011-01-20
444
    }
445

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

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

            
451
    my $row = $result->fetch_first;
452

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

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

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

            
updated document
Yuki Kimoto authored on 2011-01-23
461
    while (my $rows = $result->fetch_multi(2)) {
462
        my $title0   = $rows->[0][0];
463
        my $author0  = $rows->[0][1];
464
        
465
        my $title1   = $rows->[1][0];
466
        my $author1  = $rows->[1][1];
updated document
Yuki Kimoto authored on 2011-01-20
467
    }
468

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

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

            
473
    [
474
        ['Perl', 'Ken'],
475
        ['Ruby', 'Mark']
476
    ]
477

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

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

            
483
    my $rows = $result->fetch_all;
484

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

            
487
    [
488
        ['Perl', 'Ken'],
489
        ['Ruby', 'Mark']
490
    ]
491

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

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

            
496
    while (my $row = $result->fetch_hash) {
497
        my $title  = $row->{title};
498
        my $author = $row->{author};
499
    }
500

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

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

            
506
    my $row = $result->fetch_hash_first;
507

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

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

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

            
517
    while (my $rows = $result->fetch_hash_multi(5)) {
updated document
Yuki Kimoto authored on 2011-01-23
518
        my $title0   = $rows->[0]{title};
519
        my $author0  = $rows->[0]{author};
520
        my $title1  = $rows->[1]{title};
521
        my $author1 = $rows->[1]{author};
updated document
Yuki Kimoto authored on 2011-01-20
522
    }
523

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

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

            
528
    [
529
        {title => 'Perl', author => 'Ken'},
530
        {title => 'Ruby', author => 'Mark'}
531
    ]
532

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

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

            
539
    my $rows = $result->fetch_hash_all;
540

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

            
543
    [
544
        {title => 'Perl', author => 'Ken'},
545
        {title => 'Ruby', author => 'Mark'}
546
    ]
547

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

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

            
553
    my $sth = $result->sth;
554

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

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

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

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

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

            
569
    $dbi->register_filter(
570
        # Time::Piece object to DATE format
571
        tp_to_date => sub {
572
            my $date = shift;
573

            
574
            return '0000-00-00' unless $tp;
575
            return $tp->strftime('%Y-%m-%d');
576
        },
577
        
578
        # DATE to Time::Piece object
579
        date_to_tp => sub {
580
            my $date = shift;
581

            
582
            return if $date eq '0000-00-00';
583
            return Time::Piece->strptime($date, '%Y-%m-%d');
584
        },
585
    );
586

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

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

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

            
593
    $dbi->apply_filter('book',
594
        issue_date => {out => 'tp_to_date', in => 'date_to_tp'},
595
        first_issue_date => {out => 'tp_to_date', in => 'date_to_tp'}
596
    );
597

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

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

            
606
    issue_date => {out => sub { ... }, in => sub { ... }}
607

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

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

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

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

            
620
    my $row = $resutl->fetch_hash_first;
621
    my $tp = $row->{issue_date};
622

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

            
625
    $dbi->select(
626
        table => 'book',
627
        where => {'book.title' => 'Perl', 'book.author' => 'Ken'}
628
    );
629

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

            
633
    my $result = $dbi->execute(
634
       "select issue_date as book__issue_date from book");
635

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

            
638
    $dbi->apply_filter('book',
639
        issue_date => {out => 'tp_to_date', in => 'date_to_tp',
640
                       end => 'tp_to_displaydate'},
641
    );
642

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

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

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

            
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
653
    $dbi->insert(
654
        table => 'book',
655
        param => {issue_date => $tp, first_issue_date => $tp},
656
        filter => {issue_date => 'tp_to_date', first_issue_date => 'tp_to_date'}
657
    );
658

            
659
C<execute()>の例を示します。
660

            
661
my $sql = <<"EOS";
662
select YEAR(issue_date) as issue_year
663
from book
664
where YEAR(issue_date) = {? issue_year}
665
EOS
666
   
667
    my $result = $dbi->execute(
668
        $sql,
669
        param => {issue_year => '2010'},
670
        filter => {issue_year => 'tp_to_year'}
671
    );
672

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

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

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

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

            
684
    $result->end_filter(issue_date => sub {
685
        my $tp = shift;
686
        
687
        return '' unless $tp;
688
        return $tp->strftime('%Y/%m/%d %h:%m:%s (%a)');
689
    });
690

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

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

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

            
698
    $dbi->each_column(
699
        sub {
700
            my ($self, $table, $column, $info) = @_;
701
            
702
            my $type = $info->{TYPE_NAME};
703
            
704
            my $filter = $type eq 'DATE'     ? {out => 'tp_to_date', in => 'date_to_tp'}
705
                       : $type eq 'DATETIME' ? {out => 'tp_to_datetime', in => 'datetime_to_tp'}
706
                                             : undef;
707
            
708
            $self->apply_filter($table, $column, $filter)
709
              if $filter;
710
        }
711
    );
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
712

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
755
    $dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'}
756
                  filter => {title => 'to_something');
757

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

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

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

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

            
769
    {table NAME} -> NAME
770

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

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

            
777
    {? NAME}    ->   ?
778

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

            
781
    {= NAME}    ->   NAME = ?
782

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

            
785
    {<> NAME}   ->   NAME <> ?
786

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

            
789
    {< NAME}    ->   NAME < ?
790

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

            
793
    {> NAME}    ->   NAME > ?
794

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

            
797
    {>= NAME}   ->   NAME >= ?
798

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

            
801
    {<= NAME}   ->   NAME <= ?
802

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

            
805
    {like NAME}   ->   NAME like ?
806

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

            
809
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
810

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

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

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

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

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

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

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

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

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

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

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

            
836
    $dbi->register_tag(
update pod
Yuki Kimoto authored on 2011-01-26
837
        '=' => sub {
838
            my $column = shift;
839
            
840
            return ["$column = ?", [$column]];
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
841
        }
842
    );
843

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

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

            
849
C<=>タグの場合は
850

            
851
    {= title}
852

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

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

            
857
    [
858
        展開後の文字列,
859
        [プレースホルダに埋め込みに利用する列名1, 列名2, ...]
860
    ]
861

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

            
864
    'title = ?'
865

            
866
を返す必要があります。
867

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

            
871
    ['title']
872

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

            
876
上記を合わせると
877

            
878
    ['title = ?', ['title']]
879
    
880
を返す必要があるということです。
881

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

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

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

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

            
892
titleの値だけで検索したい場合
893

            
894
    where {= title}
895

            
896
authorの値だけで検索したい場合
897

            
898
    where {= author}
899

            
900
titleとauthorの両方の値で検索したい場合
901

            
902
    where {= title} and {=author}
903

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

            
907
    my $where = $dbi->where;
908

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

            
911
    $where->clause(
912
        ['and', '{= title'}, '{= author}']
913
    );
914

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

            
917
    ['or' あるいは 'and', タグ1, タグ2, タグ3]
918

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

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

            
925
    ['and', 
926
      '{= title}', 
927
      ['or', '{= author}', '{like date}']
928
    ]
929

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

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

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

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

            
941
    my $where_clause = $where->to_string;
942

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

            
945
    where {= title}
946

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

            
951
    my $where_clause = "$where";
952

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

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

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

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

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

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

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

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

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
971
    $where->clause(
972
        ['and', '{> date}', '{< date}']
973
    );
974
    $where->param($p);
975

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

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

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

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

            
985
    my $p = {date => ['2010-11-15']};
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
どちらも存在しない場合は次のようなデータを作成します。
988

            
989
    my $p = {date => []};
990

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

            
993
    my @date;
994
    push @date, exists $param->{start_date} ? $param->{start_date}
995
                                            : $dbi->not_exists;
996
    push @date, $param->{end_date} if exists $param->{end_date};
997
    my $p = {date => \@date};
998

            
999
=head3 C<select()>との連携
1000

            
update pod
Yuki Kimoto authored on 2011-01-26
1001
L<DBIx::Custom::Where>オブジェクトは
1002
C<select()>のC<where>に直接渡すことが
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
1003
できます。
1004
    
1005
    my $where = $dbi->where;
1006
    $where->clause(...);
1007
    $where->param($param);
1008
    my $result = $dbi->select(table => 'book', where => $where);
1009

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

            
1012
=head3 C<execute()>との連携
1013

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

            
1016

            
1017
    my $where = $dbi->where;
1018
    $where->clause(...);
1019
    $where->param($param);
1020

            
update pod
Yuki Kimoto authored on 2011-02-11
1021
    my $sql = <<"EOS";
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
1022
    select * from book;
1023
    $where
1024
    EOS
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1025

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

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

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

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1035
    package MyModel::book;
1036
    use base 'DBIx::Custom::Model';
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1037
    
1038
    sub insert { ... }
1039
    sub list { ... }
update pod
Yuki Kimoto authored on 2011-01-28
1040

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1043
    $dbi->include_model(
1044
        MyModel => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1045
            'book',
1046
        ]
1047
    );
update pod
Yuki Kimoto authored on 2011-01-28
1048

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

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

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

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

            
1060
    # MyModel.pm
1061
    package MyModel;
1062
    
1063
    use base 'DBIx::Custom::Model';
1064

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

            
1067
    MyModel.pm
1068
    MyModel / book.pm
1069
            / company.pm
1070
    
1071
モデルは次のように利用することができます。
1072

            
1073
    my $result = $dbi->model('book')->list;
1074

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

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

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
1082
またモデルクラスでC<primary_key>の設定がなされていれば、
1083
プライマリキーを指定することなしに
1084
C<update_at()>, C<delete_at()>, C<select_at()>のメソッドを
1085
利用できます。
1086

            
1087
    $dbi->model('book')->delete_at(where => 123);
1088

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

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

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

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

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

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

            
update pod
Yuki Kimoto authored on 2011-02-11
1101
    # DBIx::Custom method
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1102
    $model->execute($sql);
update pod
Yuki Kimoto authored on 2011-02-11
1103
    
1104
    # DBI method
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1105
    $model->begin_work;
1106
    $model->commit;
update pod
Yuki Kimoto authored on 2011-01-28
1107

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

            
1110
    my @models = keys %{$self->models};
1111

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

            
1114
   $model->primary_key(['id', 'number_id']);
1115

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

            
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
1119
モデルには列名を設定することもできます。
1120

            
1121
    $model->columns(['id', 'number_id']);
1122

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1123
列名はC<setup_model()>で自動的に設定することができます。
1124
このメソッドはC<include_model()>の後で呼び出してください。
1125

            
1126
    $dbi->setup_model;
1127

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

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

            
1132
    package MyDBI;
1133
    
1134
    use base 'DBIx::Custom';
1135
    
1136
    sub connect {
1137
        my $self = shift->SUPER::connect(@_);
1138
        
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1139
        $self->include_model(
1140
            MyModel => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1141
                'book',
1142
                'company'
1143
            ]
update pod
Yuki Kimoto authored on 2011-01-28
1144
        );
1145
    }
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1146
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1147
    package MyModel::book;
1148
    use base 'DBIx::Custom::Model';
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1149
    
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1150
    __PACKAGE__->attr('primary_key' => sub { ['id'] };
1151
    
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1152
    sub insert { ... }
1153
    sub list { ... }
1154
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1155
    package MyModel::company;
1156
    use base 'DBIx::Custom::Model';
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1157

            
1158
    __PACKAGE__->attr('primary_key' => sub { ['id'] };
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1159
    
1160
    sub insert { ... }
1161
    sub list { ... }
update pod
Yuki Kimoto authored on 2011-01-28
1162

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

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

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

            
update pod
Yuki Kimoto authored on 2011-02-11
1170
    my $params = [
1171
        {title => 'Perl', author => 'Ken'},
1172
        {title => 'Good day', author => 'Tom'}
1173
    ]
1174
    my $query = $dbi->insert(table => 'book', param => $params->[0], query => 1);
update pod
Yuki Kimoto authored on 2011-01-26
1175

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

            
update pod
Yuki Kimoto authored on 2011-02-11
1179
    foreach my $param (@$params) {
1180
        $dbi->execute($query, $param);
1181
    }
1182

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

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

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

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

            
1199

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

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

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

            
update pod
Yuki Kimoto authored on 2011-01-26
1207
    $dbi->method(
updated document
Yuki Kimoto authored on 2011-01-20
1208
        update_or_insert => sub {
1209
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-01-26
1210
            # something
updated document
Yuki Kimoto authored on 2011-01-20
1211
        },
1212
        find_or_create   => sub {
1213
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-01-26
1214
            # something
updated document
Yuki Kimoto authored on 2011-01-20
1215
        }
1216
    );
1217

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

            
1221
    $dbi->update_or_insert;
1222
    $dbi->find_or_create;
1223

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

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

            
1229
    package MyResult;
1230
    use base 'DBIx::Custom::Result';
1231
    
1232
    sub some_method { ... }
updated document
Yuki Kimoto authored on 2011-01-20
1233

            
removed experimental expand
Yuki Kimoto authored on 2011-01-26
1234
    1;
1235
    
1236
    package main;
1237
    
1238
    use MyResult;
1239
    
1240
    my $dbi = DBIx::Custom->connect(...);
1241
    $dbi->result_class('MyResult');
updated document
Yuki Kimoto authored on 2011-01-20
1242

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

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

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

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

            
removed experimental expand
Yuki Kimoto authored on 2011-01-26
1254
    $dbi->cache_method(sub {
1255
        sub {
1256
            my $self = shift;
1257
            
1258
            $self->{_cached} ||= {};
1259
            
1260
            if (@_ > 1) {
update pod
Yuki Kimoto authored on 2011-02-11
1261
                # キャッシュの保存
removed experimental expand
Yuki Kimoto authored on 2011-01-26
1262
                $self->{_cached}{$_[0]} = $_[1] 
1263
            }
1264
            else {
update pod
Yuki Kimoto authored on 2011-02-11
1265
                # キャッシュの取得
removed experimental expand
Yuki Kimoto authored on 2011-01-26
1266
                return $self->{_cached}{$_[0]}
1267
            }
1268
        }
1269
    });
1270
    
1271
第一はL<DBIx::Custom>オブジェクトです。
update pod
Yuki Kimoto authored on 2011-02-11
1272
第二引数はタグが解析される前のSQLです。
1273
第三引数はタグの解析後のSQLの情報です。これはハッシュのリファレンスです。
updated document
Yuki Kimoto authored on 2011-01-20
1274

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

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

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

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

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