Newer Older
1204 lines | 29.52kb
update pod
Yuki Kimoto authored on 2011-01-28
1
=encoding utf8
2

            
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
3
=head1 NAME
4

            
update pod
Yuki Kimoto authored on 2011-01-28
5
DBIx::Custom::Guide - DBIx::Custom Guide
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
6

            
pod fix
Yuki Kimoto authored on 2011-01-21
7
=head1 GUIDE
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
8

            
update pod
Yuki Kimoto authored on 2011-01-28
9
B<This guide is now writing.>
update pod
Yuki Kimoto authored on 2011-01-26
10

            
update pod
Yuki Kimoto authored on 2011-01-28
11
L<DBIx::Custom> is the class to make easy to execute SQL.
12
This is L<DBI> wrapper class like L<DBIx::Class> or L<DBIx::Simple>.
13
You can do thing more easy than L<DBIx::Class>, more flexible
14
than L<DBIx::Simple>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
15

            
update pod
Yuki Kimoto authored on 2011-01-28
16
L<DBIx::Custom> is B<not< O/R mapper, O/R mapper is usefule, but
17
you must learn many things. Created SQL is sometimes inefficient,
18
and in many cases you create raw SQL because
19
O/R mapper can't make complex SQL
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
20

            
update pod
Yuki Kimoto authored on 2011-01-28
21
L<DBIx::Custom> is opposit of O/R mapper.
22
The main purpose is that we respect SQL
23
and make easy difficult works if you use only L<DBI>.
24
If you already learn SQL, it is easy to use L<DBIx::Custom>.
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
25

            
update pod
Yuki Kimoto authored on 2011-01-28
26
I explain L<DBIx::Custom> a little in this section.
27
In L<DBIx::Custom>, you embbed tag in SQL.
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
28

            
update pod
Yuki Kimoto authored on 2011-01-28
29
    select * from book where {= title} and {=author};
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
30

            
update pod
Yuki Kimoto authored on 2011-01-28
31
The part arround {} is tag.
32
This SQL is converted to the one which contains place holder.
33

            
34
    select * from book where title = ? and author = ?;
35

            
36
Maybe you ask me that this conversion is meaningful.
37
On the top of this, usuful features is implemented.
38
See the following descriptions.
39

            
40
=over 4
41

            
42
=item 1. Specify place holder binding value as hash refernce
43

            
44
If you use L<DBI>, you must specify place holder binding value
45
as array.
46

            
47
    $sth->execute(@bind);
48

            
49
If you use L<DBIx::Custom>, you specify it as hash reference.
50
    
51
    my $param = {title => 'Perl', author => 'Ken'};
52
    $dbi->execute($sql, $param);
53

            
54
=item 2. Filtering
55

            
56
L<DBIx::Custom> provides filtering system.
57
For example, You think that about date value you want to 
58
manipulate it as date object like L<Time::Piece> in Perl,
59
and want to convert it to database DATE format.
60
and want to do reverse.
61

            
62
You can use filtering system.
63

            
64
At first, register filter.
65

            
66
    $dbi->register_filter(
67
        tp_to_date => sub {
68
            ...
69
        },
70
        date_to_tp => sub {
71
            ...
72
        }
73
    );
74

            
75
next, apply this filter to each column.
76

            
77
    $dbi->apply_filter('book',
78
        'issue_date' => {out => 'tp_to_date', in => 'date_to_tp'}
79
    );
80

            
81
C<out> is perl-to-database way. C<in> is perl-from-database way.
82

            
83
This filter is automatically enabled in many method.
84

            
85
    $dbi->insert(table => 'book', param => {issue_date => $tp});
86

            
87

            
88
=item 3. Selective search condition
89

            
90
It is difficult to create selective where clause in L<DBI>.
91
For example, If C<title> and C<author> is specified, we create 
92
the following SQL.
93

            
94
    select * from book where title = ? and author = ?;
95

            
96
If only C<title> is specified, the following one
97

            
98
    select * from book where title = ?;
99

            
100
If only C<author> is specified, the following one,
101

            
102
    select * from book where author = ?;
103

            
104
This is hard work. Generally we use modules like L<SQL::Abstract>.
105
L<DBIx::Custom> prepare the way to make it easy.
106

            
107
    # Where object
108
    my $where = $dbi->where;
109
    
110
    # Search condition
111
    $where->clause(
112
        ['and', '{= title}', {'= author'}]
113
    );
114
    
115
    # Setting to automatically select needed column
116
    $where->param({title => 'Perl'});
117

            
118
    # Embbed where clause to SQL
119
    my $sql = "select * from book $where";
120

            
121
You can create where clause which has selected search condition.
122
You can write nesting of where clause and C<or> condition
123

            
update pod
Yuki Kimoto authored on 2011-01-30
124
=item 4. Methods for insert, update, delete, select
update pod
Yuki Kimoto authored on 2011-01-28
125

            
update pod
Yuki Kimoto authored on 2011-01-30
126
L<DBIx::Custom> provides methods for insert, update, delete, select
127
There are C<insert()>, C<update()>, C<delete()>,C<select()>.
update pod
Yuki Kimoto authored on 2011-01-28
128

            
129
    my $param = {title => 'Perl', author => 'Ken'};
130
    $dbi->insert(table => 'book', param => $param);
131

            
update pod
Yuki Kimoto authored on 2011-01-30
132
=item 5. Register method for table.
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
133

            
update pod
Yuki Kimoto authored on 2011-01-30
134
You can register method for table.
update pod
Yuki Kimoto authored on 2011-01-28
135

            
update pod
Yuki Kimoto authored on 2011-01-30
136
    $dbi->table('book')->method(
update pod
Yuki Kimoto authored on 2011-01-28
137
        list => sub {
138
            ...
139
        },
update pod
Yuki Kimoto authored on 2011-01-30
140
        something => sub {
141
            ...
update pod
Yuki Kimoto authored on 2011-01-28
142
        }
143
    );
144

            
update pod
Yuki Kimoto authored on 2011-01-30
145
use the mehtod.
update pod
Yuki Kimoto authored on 2011-01-28
146

            
147
    $dbi->table('book')->list;
148

            
update pod
Yuki Kimoto authored on 2011-01-30
149
Many O/R mapper must create class for table,
150
but L<DBIx::Custom> make it easy.
update pod
Yuki Kimoto authored on 2011-01-28
151

            
152
=back
153

            
update pod
Yuki Kimoto authored on 2011-01-30
154
L<DBIx::Custom> is very useful.
155
See the following if you are interested in it.
update pod
Yuki Kimoto authored on 2011-01-28
156

            
update pod
Yuki Kimoto authored on 2011-01-30
157
=head2 1. Connect to database
update pod
Yuki Kimoto authored on 2011-01-28
158

            
update pod
Yuki Kimoto authored on 2011-01-30
159
Load L<DBIx::Custom>.
update pod
Yuki Kimoto authored on 2011-01-28
160

            
161
    use DBIx::Custom;
162

            
update pod
Yuki Kimoto authored on 2011-01-30
163
use C<connect()> to connect to database.
164
Return value is L<DBIx::Custom> object.
update pod
Yuki Kimoto authored on 2011-01-28
165

            
166
    my $dbi = DBIx::Custom->connect(
update pod
Yuki Kimoto authored on 2011-01-30
167
        data_source => "dbi:mysql:database=bookstore",
update pod
Yuki Kimoto authored on 2011-01-28
168
        user => 'ken',
169
        password => '!LFKD%$&',
170
        dbi_options => {mysql_enable_utf8 => 1}
171
    );
172

            
update pod
Yuki Kimoto authored on 2011-01-30
173
C<data_source> must be one corresponding to the database system.
174
The following ones are data source example.
update pod
Yuki Kimoto authored on 2011-01-28
175

            
176
B<MySQL>
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
177

            
178
    "dbi:mysql:database=$database"
179
    "dbi:mysql:database=$database;host=$hostname;port=$port"
180

            
update pod
Yuki Kimoto authored on 2011-01-28
181
B<SQLite>
182

            
183
    "dbi:SQLite:dbname=$database"
184
    "dbi:SQLite:dbname=:memory:"
185

            
186
B<PostgreSQL>
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
187

            
188
    "dbi:Pg:dbname=$dbname"
189

            
update pod
Yuki Kimoto authored on 2011-01-28
190
B<Oracle>
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
191

            
192
    "dbi:Oracle:$dbname"
193
    "dbi:Oracle:host=$host;sid=$sid"
194

            
update pod
Yuki Kimoto authored on 2011-01-28
195
B<ODBC(Microsoft Access)>
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
196

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

            
update pod
Yuki Kimoto authored on 2011-01-28
199
B<ODBC(SQL Server)>
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
200

            
201
   "dbi:ODBC:driver={SQL Server};Server=(local);database=test;Trusted_Connection=yes;AutoTranslate=No;"
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
202

            
update pod
Yuki Kimoto authored on 2011-01-30
203
If authentication is needed, you can specify C<user> and C<password>
204

            
205
L<DBIx::Custom> is wrapper class of L<DBI>.
206
You can use all methods of L<DBI> from L<DBIx::Custom> object.
207

            
208
    $dbi->do(...);
209
    $dbi->begin_work;
update pod
Yuki Kimoto authored on 2011-01-28
210

            
update pod
Yuki Kimoto authored on 2011-01-30
211
use C<dhb()> to get database handle of L<DBI>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
212

            
update pod
Yuki Kimoto authored on 2011-01-28
213
    my $dbh = $dbi->dbh;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
214

            
update pod
Yuki Kimoto authored on 2011-01-30
215
By default, the following ones is set to database handle attributes.
216

            
217
    RaiseError  ->  1
218
    PrintError  ->  0
219
    AutoCommit  ->  1
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
220

            
update pod
Yuki Kimoto authored on 2011-01-30
221
If fatal error occuer, program terminate.
222
If SQL is executed, commit is executed automatically.
update pod
Yuki Kimoto authored on 2011-01-28
223

            
update pod
Yuki Kimoto authored on 2011-01-31
224
=head2 2. Methods for insert, update, delete, or insert
update pod
Yuki Kimoto authored on 2011-01-28
225

            
update pod
Yuki Kimoto authored on 2011-01-31
226
There are following methods.
update pod
Yuki Kimoto authored on 2011-01-28
227

            
update pod
Yuki Kimoto authored on 2011-01-31
228
=head3 C<insert()>
update pod
Yuki Kimoto authored on 2011-01-28
229

            
update pod
Yuki Kimoto authored on 2011-01-31
230
use C<insert()> to insert row into database
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
231

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

            
update pod
Yuki Kimoto authored on 2011-01-31
235
C<table> is table name, C<param> is insert data.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
236

            
update pod
Yuki Kimoto authored on 2011-01-31
237
Following SQL is executed.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
238

            
update pod
Yuki Kimoto authored on 2011-01-28
239
    insert into (title, author) values (?, ?);
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
240

            
update pod
Yuki Kimoto authored on 2011-01-31
241
=head3 C<update()>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
242

            
update pod
Yuki Kimoto authored on 2011-01-31
243
use C<update()> to update row in database.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
244

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

            
update pod
Yuki Kimoto authored on 2011-01-31
249
C<table> is table name, C<param> is update data, C<where> is condition.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
250

            
update pod
Yuki Kimoto authored on 2011-01-31
251
Following SQL is executed.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
252

            
update pod
Yuki Kimoto authored on 2011-01-28
253
    update book set title = ?, author = ?;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
254

            
update pod
Yuki Kimoto authored on 2011-01-31
255
You can't execute C<update()> without C<where> for safety.
256
use C<update_all()> if you want to update all rows.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
257

            
update pod
Yuki Kimoto authored on 2011-01-28
258
    $dbi->update_all(table  => 'book', 
259
                     param  => {title => 'Perl', author => 'Ken'});
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
260

            
update pod
Yuki Kimoto authored on 2011-01-31
261
=head3 C<delete()>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
262

            
update pod
Yuki Kimoto authored on 2011-01-31
263
use C<delete()> to delete rows from database.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
264

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
265
    $dbi->delete(table  => 'book',
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
266
                 where  => {author => 'Ken'});
267

            
update pod
Yuki Kimoto authored on 2011-01-31
268
C<table> is table name, C<where> is condition.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
269

            
update pod
Yuki Kimoto authored on 2011-01-31
270
Following SQL is executed.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
271

            
update pod
Yuki Kimoto authored on 2011-01-28
272
    delete from book where id = ?;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
273

            
update pod
Yuki Kimoto authored on 2011-01-31
274
You can't execute C<delete()> without C<where> for safety.
275
use C<delete_all()> if you want to delete all rows.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
276

            
update pod
Yuki Kimoto authored on 2011-01-28
277
    $dbi->delete_all(table  => 'book');
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
278

            
update pod
Yuki Kimoto authored on 2011-01-31
279
=head3 C<select()>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
280

            
update pod
Yuki Kimoto authored on 2011-01-31
281
use C<select()> to select rows from database
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
282

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
283
    my $result = $dbi->select(table => 'book');
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
284

            
update pod
Yuki Kimoto authored on 2011-01-31
285
Following SQL is executed.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
286

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
287
    select * from book;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
288

            
update pod
Yuki Kimoto authored on 2011-01-31
289
Return value is L<DBIx::Custom::Result> object.
290
use C<fetch()> to fetch row.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
291

            
292
    while (my $row = $result->fetch) {
293
        my $title  = $row->[0];
294
        my $author = $row->[1];
295
    }
296

            
update pod
Yuki Kimoto authored on 2011-01-31
297
See L<3. Fetch row/"3. Fetch row"> about L<DBIx::Custom::Result>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
298

            
update pod
Yuki Kimoto authored on 2011-01-31
299
Continue more examples.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
300

            
301
    my $result = $dbi->select(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
302
        table  => 'book',
update pod
Yuki Kimoto authored on 2011-01-28
303
        column => ['author',  'title'],
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
304
        where  => {author => 'Ken'}
305
    );
306

            
update pod
Yuki Kimoto authored on 2011-01-31
307
C<column> is column names, C<where> is condition.
308

            
309
Following SQL is executed.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
310

            
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
311
    select author, title from book where author = ?;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
312

            
update pod
Yuki Kimoto authored on 2011-01-31
313
Next example.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
314

            
315
    my $result = $dbi->select(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
316
        table    => ['book', 'rental'],
update pod
Yuki Kimoto authored on 2011-01-31
317
        where    => {'book.name' => 'Perl'},
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
318
        relation => {'book.id' => 'rental.book_id'}
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
319
    );
320

            
update pod
Yuki Kimoto authored on 2011-01-31
321
C<relation> is relation of tables. This is inner join.
322

            
323
Following SQL is executed.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
324

            
update pod
Yuki Kimoto authored on 2011-01-28
325
    select * from book, rental where book.name = ? and book.id = rental.book_id;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
326

            
update pod
Yuki Kimoto authored on 2011-01-31
327
Next example.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
328

            
329
    my $result = $dbi->select(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
330
        table  => 'book',
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
331
        where  => {author => 'Ken'},
update pod
Yuki Kimoto authored on 2011-01-28
332
        append => 'for update',
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
333
    );
334

            
update pod
Yuki Kimoto authored on 2011-01-31
335
C<append> is string appending to end of SQL.
336

            
337
Following SQL is executed.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
338

            
update pod
Yuki Kimoto authored on 2011-01-28
339
    select * book where author = ? for update;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
340

            
update pod
Yuki Kimoto authored on 2011-01-31
341
C<appned> is also used at C<insert()>, C<update()>, C<update_all()>
342
C<delete()>, C<delete_all()>, and C<select()>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
343

            
update pod
Yuki Kimoto authored on 2011-02-11
344
Instead of C<column> and C<table>,
345
you can use C<selection>.
346
This is used to specify column names and table names at once
347

            
348
    my $selection = <<"EOS";
349
    title, author, company_name
350
    from book inner join company on book.company_id = company.id
351
    EOS
352

            
353
    $dbi->select(selection => $selection);
354

            
355
Note that you can't use where clause in C<selection>.
356
use clause like "inner join".
357

            
update pod
Yuki Kimoto authored on 2011-01-31
358
=head3 C<execute()> SQL
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
359

            
update pod
Yuki Kimoto authored on 2011-01-31
360
use C<execute()> to execute SQL
update pod
Yuki Kimoto authored on 2011-01-28
361

            
362
    $dbi->execute("select * from book;");
363

            
update pod
Yuki Kimoto authored on 2011-01-31
364
Process tag and execute SQL.
update pod
Yuki Kimoto authored on 2011-01-28
365

            
366
    $dbi->execute(
367
        "select * from book {= title} and {= author};"
368
        param => {title => 'Perl', author => 'Ken'}
369
    );
370

            
update pod
Yuki Kimoto authored on 2011-01-31
371
Following SQL is executed.
update pod
Yuki Kimoto authored on 2011-01-28
372

            
373
    select * from book title = ? and author = ?;
374

            
update pod
Yuki Kimoto authored on 2011-01-31
375
Values of title and author is embbdeded into placeholder.
update pod
Yuki Kimoto authored on 2011-01-28
376

            
update pod
Yuki Kimoto authored on 2011-01-31
377
See L<5. Tag/"5. Tag"> about tag.
update pod
Yuki Kimoto authored on 2011-01-28
378

            
update pod
Yuki Kimoto authored on 2011-01-31
379
You don't have to wirte last semicolon in C<execute()>. 
update pod
Yuki Kimoto authored on 2011-01-28
380

            
381
    $dbi->execute('select * from book');
382

            
update pod
Yuki Kimoto authored on 2011-02-04
383
=head2 3. Fetch row
update pod
Yuki Kimoto authored on 2011-01-28
384

            
update pod
Yuki Kimoto authored on 2011-02-04
385
Return value of C<select()> is L<DBIx::Custom::Result> object.
386
There are many methods to fetch row.
update pod
Yuki Kimoto authored on 2011-01-28
387

            
update pod
Yuki Kimoto authored on 2011-02-04
388
=head3 Fetch a row (array) C<fetch()>
update pod
Yuki Kimoto authored on 2011-01-28
389

            
update pod
Yuki Kimoto authored on 2011-02-04
390
use C<fetch()> to fetch a row and assign it into array reference.
391

            
392
    my $row = $result->fetch;
393

            
394
You can get all rows.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
395

            
396
    while (my $row = $result->fetch) {
update pod
Yuki Kimoto authored on 2011-01-28
397
        my $title  = $row->[0];
398
        my $author = $row->[1];
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
399
    }
400

            
update pod
Yuki Kimoto authored on 2011-02-04
401
=head3 Fetch only first row (array) C<fetch_first()>
update pod
Yuki Kimoto authored on 2011-01-28
402

            
update pod
Yuki Kimoto authored on 2011-02-04
403
use C<fetch_first()> to fetch only first row.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
404

            
405
    my $row = $result->fetch_first;
406

            
update pod
Yuki Kimoto authored on 2011-02-04
407
You can't fetch rest rows
408
because statement handle C<finish()> is executed.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
409

            
update pod
Yuki Kimoto authored on 2011-02-04
410
=head3 Fetch rows (array) C<fetch_multi()>
update pod
Yuki Kimoto authored on 2011-01-28
411

            
update pod
Yuki Kimoto authored on 2011-02-04
412
use C<fetch_multi()> to fetch rows and assign it into
413
array reference which has array references as element.
update pod
Yuki Kimoto authored on 2011-01-28
414

            
415
    while (my $rows = $result->fetch_multi(2)) {
416
        my $title0   = $rows->[0][0];
417
        my $author0  = $rows->[0][1];
418
        
419
        my $title1   = $rows->[1][0];
420
        my $author1  = $rows->[1][1];
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
421
    }
update pod
Yuki Kimoto authored on 2011-01-28
422

            
update pod
Yuki Kimoto authored on 2011-02-04
423
Specify row count as argument.
update pod
Yuki Kimoto authored on 2011-01-28
424

            
update pod
Yuki Kimoto authored on 2011-02-04
425
You can get the following data.
update pod
Yuki Kimoto authored on 2011-01-28
426

            
427
    [
428
        ['Perl', 'Ken'],
429
        ['Ruby', 'Mark']
430
    ]
431

            
update pod
Yuki Kimoto authored on 2011-02-04
432
=head3 Fetch all rows (array) C<fetch_all>
update pod
Yuki Kimoto authored on 2011-01-28
433

            
update pod
Yuki Kimoto authored on 2011-02-04
434
use C<fetch_all()> to fetch all rows and assign it into
435
array reference which has array reference as element.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
436

            
437
    my $rows = $result->fetch_all;
438

            
update pod
Yuki Kimoto authored on 2011-02-04
439
You can get the following data.
update pod
Yuki Kimoto authored on 2011-01-28
440

            
441
    [
442
        ['Perl', 'Ken'],
443
        ['Ruby', 'Mark']
444
    ]
445

            
update pod
Yuki Kimoto authored on 2011-02-04
446
=head3 Fetch a row (hash) C<fetch_hash()>
update pod
Yuki Kimoto authored on 2011-01-28
447

            
update pod
Yuki Kimoto authored on 2011-02-04
448
use C<fetch_hash()> to fetch a row and assign it into hash reference.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
449

            
450
    while (my $row = $result->fetch_hash) {
451
        my $title  = $row->{title};
452
        my $author = $row->{author};
453
    }
454

            
update pod
Yuki Kimoto authored on 2011-02-04
455
=head3 Fetch only first row (hash) C<fetch_hash_first()>
update pod
Yuki Kimoto authored on 2011-01-28
456

            
update pod
Yuki Kimoto authored on 2011-02-04
457
use C<fetch_hash_first()> to fetch only first row
458
and assign it into hash reference.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
459

            
460
    my $row = $result->fetch_hash_first;
update pod
Yuki Kimoto authored on 2011-01-28
461

            
update pod
Yuki Kimoto authored on 2011-02-04
462
You can't fetch rest rows
463
because statement handle C<finish()> is executed.
update pod
Yuki Kimoto authored on 2011-01-28
464

            
update pod
Yuki Kimoto authored on 2011-02-04
465
=head3 Fetch rows (hash) C<fetch_hash_multi()>
update pod
Yuki Kimoto authored on 2011-01-28
466

            
update pod
Yuki Kimoto authored on 2011-02-04
467
use C<fetch_hash_multi()> to fetch rows and
468
assign it into array reference which has hash references as element.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
469

            
470
    while (my $rows = $result->fetch_hash_multi(5)) {
update pod
Yuki Kimoto authored on 2011-01-28
471
        my $title0   = $rows->[0]{title};
472
        my $author0  = $rows->[0]{author};
473
        my $title1  = $rows->[1]{title};
474
        my $author1 = $rows->[1]{author};
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
475
    }
update pod
Yuki Kimoto authored on 2011-01-28
476

            
update pod
Yuki Kimoto authored on 2011-02-04
477
Specify row count as argument.
update pod
Yuki Kimoto authored on 2011-01-28
478

            
update pod
Yuki Kimoto authored on 2011-02-04
479
You can get the following data.
update pod
Yuki Kimoto authored on 2011-01-28
480

            
481
    [
482
        {title => 'Perl', author => 'Ken'},
483
        {title => 'Ruby', author => 'Mark'}
484
    ]
485

            
update pod
Yuki Kimoto authored on 2011-02-04
486
=head3 Fetch all rows (hash) C<fetch_hash_all()>
update pod
Yuki Kimoto authored on 2011-01-28
487

            
update pod
Yuki Kimoto authored on 2011-02-04
488
use C<fetch_hash_all()> to fetch all rows and
489
assign it into array reference which has hash 
490
references as element.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
491

            
492
    my $rows = $result->fetch_hash_all;
493

            
update pod
Yuki Kimoto authored on 2011-02-04
494
You can get the following data.
update pod
Yuki Kimoto authored on 2011-01-28
495

            
496
    [
497
        {title => 'Perl', author => 'Ken'},
498
        {title => 'Ruby', author => 'Mark'}
499
    ]
500

            
update pod
Yuki Kimoto authored on 2011-02-04
501
=head3 Statement handle C<sth()>
update pod
Yuki Kimoto authored on 2011-01-28
502

            
update pod
Yuki Kimoto authored on 2011-02-04
503
use <sth()> to get statement handle.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
504

            
505
    my $sth = $result->sth;
506

            
update pod
Yuki Kimoto authored on 2011-02-07
507
=head2 4. Filtering
update pod
Yuki Kimoto authored on 2011-01-28
508

            
update pod
Yuki Kimoto authored on 2011-02-07
509
L<DBIx::Custom> provide value filtering. 
510
For example, You maybe want to convert L<Time::Piece> object to
511
database date format when register data into database.
512
and convert database date fromat to L<Time::Piece> object
513
when get data from database.
update pod
Yuki Kimoto authored on 2011-01-28
514

            
update pod
Yuki Kimoto authored on 2011-02-07
515
=head3 Register filter C<register_filter()>
update pod
Yuki Kimoto authored on 2011-01-28
516

            
update pod
Yuki Kimoto authored on 2011-02-07
517
use C<register_filter() to register filter.
update pod
Yuki Kimoto authored on 2011-01-28
518

            
519
    $dbi->register_filter(
520
        # Time::Piece object to DATE format
521
        tp_to_date => sub {
522
            my $date = shift;
523

            
524
            return '0000-00-00' unless $tp;
525
            return $tp->strftime('%Y-%m-%d');
526
        },
527
        
528
        # DATE to Time::Piece object
529
        date_to_tp => sub {
530
            my $date = shift;
531

            
532
            return if $date eq '0000-00-00';
533
            return Time::Piece->strptime($date, '%Y-%m-%d');
534
        },
535
    );
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
536

            
update pod
Yuki Kimoto authored on 2011-02-07
537
Registered filter is used by C<apply_filter()> or etc.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
538

            
update pod
Yuki Kimoto authored on 2011-02-07
539
=head3 Apply filter C<apply_filter()>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
540

            
update pod
Yuki Kimoto authored on 2011-02-07
541
use C<apply_filter()> to apply registered filter.
update pod
Yuki Kimoto authored on 2011-01-28
542

            
543
    $dbi->apply_filter('book',
544
        issue_date => {out => 'tp_to_date', in => 'date_to_tp'},
545
        first_issue_date => {out => 'tp_to_date', in => 'date_to_tp'}
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
546
    );
547

            
update pod
Yuki Kimoto authored on 2011-02-07
548
First argument is table name. Arguments after first argument are pairs of column
549
name and fitering rule. C<out> of filtering rule is filter which is used when data
550
is send to database. C<in> of filtering rule is filter which is used when data
551
is got from database. 
552

            
553
You can specify code reference as filter.
update pod
Yuki Kimoto authored on 2011-01-28
554

            
555
    issue_date => {out => sub { ... }, in => sub { ... }}
556

            
update pod
Yuki Kimoto authored on 2011-02-07
557
Applied filter become effective at insert()>, C<update()>, C<update_all()>,
558
C<delete()>, C<delete_all()>, C<select()>.
update pod
Yuki Kimoto authored on 2011-01-28
559

            
560
    my $tp = Time::Piece->strptime('2010/10/14', '%Y/%m/%d');
update pod
Yuki Kimoto authored on 2011-02-07
561
    my $result = $dbi->select(table => 'book', where => {issue_date => $tp});
update pod
Yuki Kimoto authored on 2011-01-28
562

            
update pod
Yuki Kimoto authored on 2011-02-07
563
When data is send to database, L<Time::Piece> object is converted
564
to database date format "2010-10-14"
update pod
Yuki Kimoto authored on 2011-01-28
565

            
update pod
Yuki Kimoto authored on 2011-02-07
566
When data is fetched, database date format is
567
converted to L<Time::Piece> object.
update pod
Yuki Kimoto authored on 2011-01-28
568

            
569
    my $row = $resutl->fetch_hash_first;
570
    my $tp = $row->{issue_date};
571

            
update pod
Yuki Kimoto authored on 2011-02-07
572
You can also use column name which contains table name.
update pod
Yuki Kimoto authored on 2011-01-28
573

            
574
    $dbi->select(
575
        table => 'book',
update pod
Yuki Kimoto authored on 2011-02-07
576
        where => {'book.issue_date' => $tp}
update pod
Yuki Kimoto authored on 2011-01-28
577
    );
578

            
update pod
Yuki Kimoto authored on 2011-02-11
579
In fetching, Filter is effective if you use "TABLE__COLUMN" as column name.
580

            
581
    my $result = $dbi->execute(
582
       "select issue_date as book__issue_date from book");
583

            
584
You can apply C<end> filter execute after C<in> filter.
585

            
586
    $dbi->apply_filter('book',
587
        issue_date => {out => 'tp_to_date', in => 'date_to_tp',
588
                       end => 'tp_to_displaydate'},
589
    );
590

            
update pod
Yuki Kimoto authored on 2011-02-07
591
=head3 Individual filter C<filter>
update pod
Yuki Kimoto authored on 2011-01-28
592

            
update pod
Yuki Kimoto authored on 2011-02-07
593
You can apply individual filter .
594
This filter overwrite the filter by C<apply_filter()>
update pod
Yuki Kimoto authored on 2011-01-28
595

            
update pod
Yuki Kimoto authored on 2011-02-07
596
use C<filter> option to apply individual filter
597
when data is send to database.
598
This option is used at C<insert()>, C<update()>,
599
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>,
600
C<execute()>.
update pod
Yuki Kimoto authored on 2011-01-28
601

            
update pod
Yuki Kimoto authored on 2011-02-07
602
C<insert()> example:
update pod
Yuki Kimoto authored on 2011-01-28
603

            
604
    $dbi->insert(
605
        table => 'book',
606
        param => {issue_date => $tp, first_issue_date => $tp},
607
        filter => {issue_date => 'tp_to_date', first_issue_date => 'tp_to_date'}
608
    );
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
609

            
update pod
Yuki Kimoto authored on 2011-02-07
610
C<execute()> example:
update pod
Yuki Kimoto authored on 2011-01-28
611

            
612
my $sql = <<"EOS";
613
select YEAR(issue_date) as issue_year
614
from book
615
where YEAR(issue_date) = {? issue_year}
616
EOS
617
   
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
618
    my $result = $dbi->execute(
update pod
Yuki Kimoto authored on 2011-01-28
619
        $sql,
620
        param => {issue_year => '2010'},
621
        filter => {issue_year => 'tp_to_year'}
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
622
    );
623

            
update pod
Yuki Kimoto authored on 2011-02-07
624
You can also apply indivisual filter when you fetch row.
625
use C<DBIx::Custom::Result>'s C<filter()>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
626

            
update pod
Yuki Kimoto authored on 2011-01-28
627
    $result->filter(issue_year => 'year_to_tp');
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
628

            
update pod
Yuki Kimoto authored on 2011-02-07
629
=head3 End filtering : C<end_filter()>
update pod
Yuki Kimoto authored on 2011-01-28
630

            
update pod
Yuki Kimoto authored on 2011-02-07
631
You can add filter at end.
632
It is useful to create last output.
633
use C<end_filter()> to add end filter.
update pod
Yuki Kimoto authored on 2011-01-28
634

            
635
    $result->end_filter(issue_date => sub {
636
        my $tp = shift;
637
        
638
        return '' unless $tp;
639
        return $tp->strftime('%Y/%m/%d %h:%m:%s (%a)');
640
    });
641

            
update pod
Yuki Kimoto authored on 2011-02-07
642
In this example, L<Time::Piece> object is converted to readable format.
update pod
Yuki Kimoto authored on 2011-01-28
643

            
update pod
Yuki Kimoto authored on 2011-02-07
644
=head3 Automate applying filter C<each_column()>
update pod
Yuki Kimoto authored on 2011-01-28
645

            
update pod
Yuki Kimoto authored on 2011-02-07
646
It is useful to apply filter automatically at date type columns.
647
You can use C<each_column()> to process all column infos.
update pod
Yuki Kimoto authored on 2011-01-28
648

            
649
    $dbi->each_column(
650
        sub {
651
            my ($self, $table, $column, $info) = @_;
652
            
653
            my $type = $info->{TYPE_NAME};
654
            
655
            my $filter = $type eq 'DATE'     ? {out => 'tp_to_date', in => 'date_to_tp'}
656
                       : $type eq 'DATETIME' ? {out => 'tp_to_datetime', in => 'datetime_to_tp'}
657
                                             : undef;
658
            
659
            $self->apply_filter($table, $column, $filter)
660
              if $filter;
661
        }
662
    );
663

            
update pod
Yuki Kimoto authored on 2011-02-07
664
C<each_column() receive callback.
665
callback arguments are L<DBIx::Custom> object, table name, column name, column information.
666
Filter is applied automatically by column type.
update pod
Yuki Kimoto authored on 2011-01-28
667

            
update pod
Yuki Kimoto authored on 2011-02-07
668
=head2 5. Tag
update pod
Yuki Kimoto authored on 2011-01-28
669

            
update pod
Yuki Kimoto authored on 2011-02-07
670
=head3 Basic of Tag
update pod
Yuki Kimoto authored on 2011-01-28
671

            
update pod
Yuki Kimoto authored on 2011-02-07
672
You can embedd tag into SQL.
update pod
Yuki Kimoto authored on 2011-01-28
673

            
674
    select * from book where {= title} and {like author};
675

            
update pod
Yuki Kimoto authored on 2011-02-07
676
{= title} and {like author} are tag. Tag has the folloring format.
update pod
Yuki Kimoto authored on 2011-01-28
677

            
update pod
Yuki Kimoto authored on 2011-02-07
678
    {TAG_NAME ARG1 ARG2 ...}
update pod
Yuki Kimoto authored on 2011-01-28
679

            
update pod
Yuki Kimoto authored on 2011-02-07
680
Tag start C<{> and end C<}>. 
681
Don't insert space between C<{} and tag name.
682

            
683
C<{> and C<}> are reserved word.
684
If you want to use these, escape it by '\';
update pod
Yuki Kimoto authored on 2011-01-28
685

            
686
    select from book \\{ ... \\}
687

            
update pod
Yuki Kimoto authored on 2011-02-07
688
\ is perl's escape character, you need two \.
689

            
690
Tag is expanded before executing SQL.
update pod
Yuki Kimoto authored on 2011-01-28
691

            
692
    select * from book where title = ? and author like ?;
693

            
update pod
Yuki Kimoto authored on 2011-02-07
694
use C<execute()> to execute SQL which contains tag
update pod
Yuki Kimoto authored on 2011-01-28
695

            
696
    my $sql = "select * from book where {= author} and {like title};"
697
    $dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'});
698

            
update pod
Yuki Kimoto authored on 2011-02-07
699
You can specify values embedded into place holder as hash reference using
700
C<param> option.
update pod
Yuki Kimoto authored on 2011-01-28
701

            
update pod
Yuki Kimoto authored on 2011-02-07
702
You can specify C<filter()> at C<execute()>.
update pod
Yuki Kimoto authored on 2011-01-28
703

            
704
    $dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'}
705
                  filter => {title => 'to_something');
706

            
update pod
Yuki Kimoto authored on 2011-02-07
707
Note that at C<execute()> the filter applied by C<apply_filter()>
708
don't has effective to columns.
update pod
Yuki Kimoto authored on 2011-02-11
709
You have to use C<table> tag in SQL
update pod
Yuki Kimoto authored on 2011-01-28
710

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

            
update pod
Yuki Kimoto authored on 2011-02-07
713
=head3 Tag list
update pod
Yuki Kimoto authored on 2011-01-28
714

            
715
L<DBIx::Custom>�ł͎��̃^�O���g�p�”\�ł��B
716

            
update pod
Yuki Kimoto authored on 2011-02-07
717
The following tag is available.
update pod
Yuki Kimoto authored on 2011-01-28
718

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

            
721
    {table NAME} -> NAME
722

            
723
This is used to specify table name in SQL.
724
If you specify table name, Filtering by 
725
C<apply_filter()> is effective.
726

            
update pod
Yuki Kimoto authored on 2011-01-31
727
=head4 C<?>
update pod
Yuki Kimoto authored on 2011-01-28
728

            
729
    {? NAME}    ->   ?
730

            
update pod
Yuki Kimoto authored on 2011-01-31
731
=head4 C<=>
update pod
Yuki Kimoto authored on 2011-01-28
732

            
733
    {= NAME}    ->   NAME = ?
734

            
update pod
Yuki Kimoto authored on 2011-01-31
735
=head4 C<E<lt>E<gt>>
update pod
Yuki Kimoto authored on 2011-01-28
736

            
737
    {<> NAME}   ->   NAME <> ?
738

            
update pod
Yuki Kimoto authored on 2011-01-31
739
=head4 C<E<lt>>
update pod
Yuki Kimoto authored on 2011-01-28
740

            
741
    {< NAME}    ->   NAME < ?
742

            
update pod
Yuki Kimoto authored on 2011-01-31
743
=head4 C<E<gt>>
update pod
Yuki Kimoto authored on 2011-01-28
744

            
745
    {> NAME}    ->   NAME > ?
746

            
update pod
Yuki Kimoto authored on 2011-01-31
747
=head4 C<E<gt>=>
update pod
Yuki Kimoto authored on 2011-01-28
748

            
749
    {>= NAME}   ->   NAME >= ?
750

            
update pod
Yuki Kimoto authored on 2011-01-31
751
=head4 C<E<lt>=>
update pod
Yuki Kimoto authored on 2011-01-28
752

            
753
    {<= NAME}   ->   NAME <= ?
754

            
update pod
Yuki Kimoto authored on 2011-01-31
755
=head4 C<like>
update pod
Yuki Kimoto authored on 2011-01-28
756

            
757
    {like NAME}   ->   NAME like ?
758

            
update pod
Yuki Kimoto authored on 2011-01-31
759
=head4 C<in>
update pod
Yuki Kimoto authored on 2011-01-28
760

            
761
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
762

            
update pod
Yuki Kimoto authored on 2011-01-31
763
=head4 C<insert_param>
update pod
Yuki Kimoto authored on 2011-01-28
764

            
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
765
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
update pod
Yuki Kimoto authored on 2011-01-28
766

            
update pod
Yuki Kimoto authored on 2011-01-31
767
=head4 C<update_param>
update pod
Yuki Kimoto authored on 2011-01-28
768

            
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
769
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
770

            
update pod
Yuki Kimoto authored on 2011-02-07
771
=head3 Manipulate same name's columns
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
772

            
update pod
Yuki Kimoto authored on 2011-02-07
773
It is ok if there are same name's columns.
774
Let's think two date comparison.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
775

            
update pod
Yuki Kimoto authored on 2011-01-28
776
    my $sql = "select * from table where {> date} and {< date};";
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
777

            
update pod
Yuki Kimoto authored on 2011-02-07
778
In this case, You specify paramter values as array reference.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
779

            
update pod
Yuki Kimoto authored on 2011-01-28
780
    my $dbi->execute($sql, param => {date => ['2010-10-01', '2012-02-10']});
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
781

            
update pod
Yuki Kimoto authored on 2011-02-07
782
=head3 Register Tag C<register_tag()>
update pod
Yuki Kimoto authored on 2011-01-28
783

            
update pod
Yuki Kimoto authored on 2011-02-07
784
You can register your tag.
785
use C<register_tag()> to register tag.
update pod
Yuki Kimoto authored on 2011-01-28
786

            
787
    $dbi->register_tag(
788
        '=' => sub {
789
            my $column = shift;
790
            
791
            return ["$column = ?", [$column]];
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
792
        }
793
    );
794

            
update pod
Yuki Kimoto authored on 2011-01-28
795
�����ł̓f�t�H���g��C<=>�^�O���ǂ̂悤�Ɏ������Ă��邩����Ă��܂��B
796
�^�O��o�^����֐��̈�̓^�O�̒��ɏ����ꂽ��ɂȂ�܂��B
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
797

            
update pod
Yuki Kimoto authored on 2011-01-28
798
    {�^�O�� ��1 ��2}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
799

            
update pod
Yuki Kimoto authored on 2011-01-28
800
C<=>�^�O�̏ꍇ��
cleanup
Yuki Kimoto authored on 2011-01-12
801

            
update pod
Yuki Kimoto authored on 2011-01-28
802
    {= title}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
803

            
update pod
Yuki Kimoto authored on 2011-01-28
804
�Ƃ����`���ł�����A�T�u���[�`���ɂ�title�Ƃ����ЂƂ‚̗񖼂��킽��Ă��܂��B
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
805

            
update pod
Yuki Kimoto authored on 2011-01-28
806
�T�u���[�`���̖߂�l�ɂ͎��̌`���̔z��̃��t�@�����X��Ԃ��K�v������܂��B
807

            
808
    [
809
        �W�J��̕�����,
810
        [�v���[�X�z���_�ɖ��ߍ��݂ɗ��p�����1, ��2, ...]
811
    ]
812

            
813
��–ڂ̗v�f�͓W�J��̕�����ł��B���̗�ł�
814

            
815
    'title = ?'
816

            
817
��Ԃ��K�v������܂��B
818

            
819
��–ڂ̗v�f�̓v���[�X�z���_�ɖ��ߍ��݂ɗ��p����񖼂�܂ޔz���
820
���t�@�����X�ł��B����̗�ł�
821

            
822
    ['title']
823

            
824
��Ԃ��K�v������܂��B�����̃v���[�X�z���_��܂ޏꍇ�́A���̕�����
825
�����ɂȂ�܂��BC<insert_param>�^�O��C<update_param>�^�O��
826
���̕�������ە����ɂȂ�Ă��܂��B
827

            
828
��L��킹���
829

            
830
    ['title = ?', ['title']]
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
831
    
update pod
Yuki Kimoto authored on 2011-01-28
832
��Ԃ��K�v������Ƃ������Ƃł��B
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
833

            
update pod
Yuki Kimoto authored on 2011-01-28
834
�^�O�̎���̑��̃T���v����L<DBIx::Custom::Tag>�̃\�[�X�R�[�h
835
����ɂȂ�Ă݂Ă��������B
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
836

            
update pod
Yuki Kimoto authored on 2011-01-28
837
=head2 6. Where��̓��I�Ȑ���
838

            
839
=head3 Where��̓��I�Ȑ��� where()
840

            
841
�����̌�����w�肵�āA�����s�������ꍇ������܂��B
842
����3�‚̃P�[�X��where���l���Ă݂܂��傤�B
843
���L�̂悤��where�傪�K�v�ɂȂ�܂��B
844

            
845
title�̒l�����Ō�������ꍇ
846

            
847
    where {= title}
848

            
849
author�̒l�����Ō�������ꍇ
850

            
851
    where {= author}
852

            
853
title��author�̗���̒l�Ō�������ꍇ
854

            
855
    where {= title} and {=author}
856

            
857
L<DBIx::Custom>�ł͓��I��Where��̐�����T�|�[�g���Ă��܂��B
858
�܂�C<where()>��L<DBIx::Custom::Where>�I�u�W�F�N�g�𐶐����܂��B
859

            
860
    my $where = $dbi->where;
861

            
862
����C<clause()>��g�p����where���L�q���܂��B
863

            
864
    $where->clause(
865
        ['and', '{= title'}, '{= author}']
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
866
    );
867

            
update pod
Yuki Kimoto authored on 2011-01-28
868
clause�̎w���@�͎��̂悤�ɂȂ�܂��B
869

            
870
    ['or' ���邢�� 'and', �^�O1, �^�O2, �^�O3]
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
871

            
update pod
Yuki Kimoto authored on 2011-01-28
872
����ɂ�or���邢��and��w�肵�܂��B����ȍ~�ɂ�
873
������^�O��g��ċL�q���܂��B
874

            
875
C<clause>�̎w��͓��q�ɂ��邱�Ƃ�ł��A����ɕ��G�ȏ�
876
��L�q���邱�Ƃ�ł��܂��B
877

            
878
    ['and', 
879
      '{= title}', 
880
      ['or', '{= author}', '{like date}']
881
    ]
882

            
883
���̂悤��C<clause>��ݒ肵�����C<param>�Ƀp�����[�^��w�肵�܂��B
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
884
    
update pod
Yuki Kimoto authored on 2011-01-28
885
    my $param => {title => 'Perl'};
886
    $where->param($param);
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
887

            
update pod
Yuki Kimoto authored on 2011-01-28
888
���̗�ł�title�������p�����[�^�Ɋ܂܂�Ă��܂��B
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
889

            
update pod
Yuki Kimoto authored on 2011-01-28
890
���̌�C<to_string()>���s�����$param�Ɋ܂܂��p�����[�^�𖞂���
891
where��𐶐����邱�Ƃ��ł��܂��B
892

            
893
    my $where_clause = $where->to_string;
894

            
895
�p�����[�^��title�����ł��̂ŁA���̂悤��where�傪��������܂��B
896

            
897
    where {= title}
898

            
899
�܂�L<DBIx::Custom>�͕�����̕]����I�[�o�[���[�h���āAC<to_string()>
900
��Ăяo���悤�ɂ��Ă��܂��̂ŁA���̂悤�ɂ���where��𐶐����邱�Ƃ�
901
�ł��܂��B
902

            
903
    my $where_clause = "$where";
904

            
905
�����SQL�̒���where��𖄂ߍ��ނƂ��ɂƂĂ�𗧂‹@�\�ł��B
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
906

            
update pod
Yuki Kimoto authored on 2011-01-28
907
=head3 ����̗񖼂�܂ޏꍇ
check arguments of connect m...
Yuki Kimoto authored on 2010-12-20
908

            
update pod
Yuki Kimoto authored on 2011-01-28
909
�^�O�̒��ɓ���̖��O��‚�̂����݂����ꍇ�ł��I��
910
where���쐬���邱�Ƃ��ł��܂��B
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
911

            
update pod
Yuki Kimoto authored on 2011-01-28
912
���Ƃ��΁A�p�����[�^�Ƃ��ĊJ�n��t�ƏI����t��󂯎������Ƃ�
913
�l���Ă݂Ă��������B
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
914

            
update pod
Yuki Kimoto authored on 2011-01-28
915
    my $param = {start_date => '2010-11-15', end_date => '2011-11-21'};
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
916

            
update pod
Yuki Kimoto authored on 2011-01-28
917
�܂��J�n��t�ƏI����t�̕Е���A�ǂ����󂯎��Ȃ��ꍇ���邩����܂���B
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
918

            
update pod
Yuki Kimoto authored on 2011-01-28
919
���̏ꍇ�͎��̂悤�ȃp�����[�^�ɕϊ����邱�ƂőΉ����邱�Ƃ��ł��܂��B
920

            
921
    my $p = {date => ['2010-11-15', '2011-11-21']};
922

            
923
�l���z��̃��t�@�����X�ɂȂ�Ă��邱�Ƃɒ��ڂ��Ă��������B���̂悤�ɂ����
924
�����̗��܂ރ^�O�ɏ��Ԃɖ��ߍ��ނ��Ƃ��ł��܂��B
925

            
926
    $where->clause(
927
        ['and', '{> date}', '{< date}']
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
928
    );
update pod
Yuki Kimoto authored on 2011-01-28
929
    $where->param($p);
930

            
931
�܂��J�n��t�����݂��Ȃ��ꍇ�͎��̂悤�ȃf�[�^��쐬���܂��B
932

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

            
935
L<DBIx::Custom>��C<not_exists>��DBIx::Custom::NotExists�I�u�W�F�N�g��
936
�擾�ł��܂��B����͑Ή�����l�����݂��Ȃ����Ƃ�����߂̂�̂ł��B
937

            
938
�܂��I����t�����݂��Ȃ��ꍇ�͎��̂悤�ȃf�[�^��쐬���܂��B
939

            
940
    my $p = {date => ['2010-11-15']};
941

            
942
�ǂ�����݂��Ȃ��ꍇ�͎��̂悤�ȃf�[�^��쐬���܂��B
943

            
944
    my $p = {date => []};
945

            
946
��������̂ň�ԊȒP�ɍ쐬�ł��郍�W�b�N����Ă����܂��B
947

            
948
    my @date;
949
    push @date, exists $param->{start_date} ? $param->{start_date}
950
                                            : $dbi->not_exists;
951
    push @date, $param->{end_date} if exists $param->{end_date};
952
    my $p = {date => \@date};
953

            
954
=head3 C<select()>�Ƃ̘A�g
955

            
956
L<DBIx::Custom::Where>�I�u�W�F�N�g��
957
C<select()>��C<where>�ɒ��ړn�����Ƃ�
958
�ł��܂��B
959
    
960
    my $where = $dbi->where;
961
    $where->clause(...);
962
    $where->param($param);
963
    my $result = $dbi->select(table => 'book', where => $where);
964

            
965
���邢��C<update()>�AC<delete()>��where�Ɏw�肷�邱�Ƃ�”\�ł��B
966

            
967
=head3 C<execute()>�Ƃ̘A�g
968

            
969
C<execute()>�Ƃ̘A�g�ł��BSQL��쐬����Ƃ��ɖ��ߍ��ނ��Ƃ��ł��܂��B
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
970

            
971

            
update pod
Yuki Kimoto authored on 2011-01-28
972
    my $where = $dbi->where;
973
    $where->clause(...);
974
    $where->param($param);
975

            
976
    my $sql = <<"EOS"
977
    select * from book;
978
    $where
979
    EOS
980

            
981
    $dbi->execute($sql, param => $param);
982

            
update pod
Yuki Kimoto authored on 2011-02-11
983
=head2 7. Table object
update pod
Yuki Kimoto authored on 2011-01-28
984

            
update pod
Yuki Kimoto authored on 2011-02-11
985
=head3 Create table object C<table()>
update pod
Yuki Kimoto authored on 2011-01-28
986

            
update pod
Yuki Kimoto authored on 2011-02-11
987
You can create table object to access table in database.
988
use C<tabel()> to create table object.
update pod
Yuki Kimoto authored on 2011-01-28
989

            
990
    my $table = $dbi->table('book');
991

            
update pod
Yuki Kimoto authored on 2011-02-11
992
Return value is L<DBIx::Custom::Table>.
993
From table object, you can call C<insert()>, C<update()>, C<update_all()>�A
994
C<delete()>, C<delete_all()>, C<select()>.
995
You don't have to specify table name.
update pod
Yuki Kimoto authored on 2011-01-28
996

            
997
    $table->insert(param => $param);
998

            
update pod
Yuki Kimoto authored on 2011-02-11
999
You can add any method to table object.
update pod
Yuki Kimoto authored on 2011-01-28
1000

            
1001
    $table->method(
1002
        register => sub {
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
1003
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-01-28
1004
            my $table_name = $self->name;
update pod
Yuki Kimoto authored on 2011-02-11
1005
            # ...
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
1006
        },
update pod
Yuki Kimoto authored on 2011-02-11
1007
        list => sub { ... }
update pod
Yuki Kimoto authored on 2011-01-28
1008
    );
1009

            
update pod
Yuki Kimoto authored on 2011-02-11
1010
You can get table name by C<name()>.
update pod
Yuki Kimoto authored on 2011-01-28
1011

            
update pod
Yuki Kimoto authored on 2011-02-11
1012
You can call these method from table object.
update pod
Yuki Kimoto authored on 2011-01-28
1013

            
1014
    $table->register(...);
1015
    $table->list(...);
1016

            
update pod
Yuki Kimoto authored on 2011-02-11
1017
=head2 Use L<DBIx::Custom> and <DBI> methods
update pod
Yuki Kimoto authored on 2011-01-28
1018

            
update pod
Yuki Kimoto authored on 2011-02-11
1019
You can call all methods of L<DBIx::Custom> and L<DBI>
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
1020

            
update pod
Yuki Kimoto authored on 2011-02-11
1021
    # DBIx::Custom method
1022
    $table->execute($sql);
1023
    
1024
    # DBI method
1025
    $table->begin_work;
1026
    $table->commit;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
1027

            
update pod
Yuki Kimoto authored on 2011-02-11
1028
=head2 Add table shared method
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
1029

            
update pod
Yuki Kimoto authored on 2011-02-11
1030
To share methods in all tables,
1031
add method to base table.
1032
You can get base table by C<base_table()>.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
1033

            
update pod
Yuki Kimoto authored on 2011-01-28
1034
    $dbi->base_table->method(
1035
        count => sub {
1036
            my $self = shift;
1037
            return $self->select(column => ['count(*)']);
1038
        }
1039
    );
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1040

            
update pod
Yuki Kimoto authored on 2011-02-11
1041
This method is used by all talbes.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1042

            
update pod
Yuki Kimoto authored on 2011-02-11
1043
    $dbi->table('book')->count(...);
update pod
Yuki Kimoto authored on 2011-01-28
1044

            
update pod
Yuki Kimoto authored on 2011-02-11
1045
Even if same method name is added to table,
1046
You can use base table method by C<base_METHOD()>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1047

            
update pod
Yuki Kimoto authored on 2011-02-11
1048
    $table->method(
1049
        count => sub {
1050
            my $self = shift;
1051
            
1052
            $self->base_count(...);
1053
            
1054
            # ...
1055
        }
1056
    );
1057

            
1058
=head2 Model example
1059

            
1060
Generally, it is good to create model in constructor
1061
in the class extending L<DBIx::Custom>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1062

            
update pod
Yuki Kimoto authored on 2011-01-28
1063
    package MyDBI;
1064
    
1065
    use base 'DBIx::Custom';
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1066
    
update pod
Yuki Kimoto authored on 2011-01-28
1067
    sub connect {
1068
        my $self = shift->SUPER::connect(@_);
1069
        
1070
        $self->base_table->method(
update pod
Yuki Kimoto authored on 2011-02-11
1071
            delete_multi => sub { ... }
update pod
Yuki Kimoto authored on 2011-01-28
1072
        );
1073
        
1074
        $self->table('book')->method(
update pod
Yuki Kimoto authored on 2011-02-11
1075
            register => sub { ... },
1076
            remove   => sub { ... },
update pod
Yuki Kimoto authored on 2011-01-28
1077
        );
1078
        
1079
        $self->table('company')->method(
update pod
Yuki Kimoto authored on 2011-02-11
1080
            register => sub { ... },
1081
            remove   => sub { ... },
update pod
Yuki Kimoto authored on 2011-01-28
1082
        );
1083
    }
1084

            
update pod
Yuki Kimoto authored on 2011-02-11
1085
You can use this class in the following way.
update pod
Yuki Kimoto authored on 2011-01-28
1086

            
1087
    my $dbi = MyDBI->connect(...);
update pod
Yuki Kimoto authored on 2011-02-11
1088
    $dbi->table('book')->delete_multi(...);
update pod
Yuki Kimoto authored on 2011-01-28
1089

            
update pod
Yuki Kimoto authored on 2011-02-11
1090
=head2 8. Improve performance
update pod
Yuki Kimoto authored on 2011-01-28
1091

            
update pod
Yuki Kimoto authored on 2011-02-11
1092
=head3 Create query
update pod
Yuki Kimoto authored on 2011-01-28
1093

            
update pod
Yuki Kimoto authored on 2011-02-11
1094
If you can't get performance, create query by C<query> option.
1095
For example, many insert is needed.
update pod
Yuki Kimoto authored on 2011-01-28
1096

            
update pod
Yuki Kimoto authored on 2011-02-11
1097
    my $params = [
1098
        {title => 'Perl', author => 'Ken'},
1099
        {title => 'Good day', author => 'Tom'}
1100
    ]
1101
    my $query = $dbi->insert(table => 'book', param => $params->[0], query => 1);
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1102

            
update pod
Yuki Kimoto authored on 2011-02-11
1103
Return value is L<DBIx::Custom::Query> object.
1104
This query is executed by C<execute()>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1105

            
update pod
Yuki Kimoto authored on 2011-02-11
1106
    foreach my $param (@$params) {
1107
        $dbi->execute($query, $param);
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1108
    }
1109

            
update pod
Yuki Kimoto authored on 2011-02-11
1110
Performance is improved because statement handle is reused
1111
C<query> option is used in C<insert()>, C<update()>, C<update_all()>,
1112
C<delete()>, C<delete_all()>.
1113

            
1114
Note that parameters count is same as method for creating query and C<execute()>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1115

            
update pod
Yuki Kimoto authored on 2011-02-11
1116
You can create query from any SQL by C<create_query()>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1117

            
update pod
Yuki Kimoto authored on 2011-02-11
1118
    my $query = $dbi->create_query(
1119
        "insert into book {insert_param title author};";
1120
    );
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1121

            
update pod
Yuki Kimoto authored on 2011-02-11
1122
=head2 9. Other features
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1123

            
update pod
Yuki Kimoto authored on 2011-02-11
1124
=head3 Add method
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1125

            
update pod
Yuki Kimoto authored on 2011-02-11
1126
You can add method to L<DBIx::Custom> object.
1127
use C<method().
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1128

            
update pod
Yuki Kimoto authored on 2011-01-28
1129
    $dbi->method(
1130
        update_or_insert => sub {
1131
            my $self = shift;
1132
            # something
1133
        },
1134
        find_or_create   => sub {
1135
            my $self = shift;
1136
            # something
1137
        }
1138
    );
1139

            
update pod
Yuki Kimoto authored on 2011-02-11
1140
You can call these methods from L<DBIx::Custom> object.
update pod
Yuki Kimoto authored on 2011-01-28
1141

            
1142
    $dbi->update_or_insert;
1143
    $dbi->find_or_create;
1144

            
update pod
Yuki Kimoto authored on 2011-02-11
1145
=head3 Change result class
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1146

            
update pod
Yuki Kimoto authored on 2011-02-11
1147
You can change result class. By default it is L<DBIx::Custom::Result>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1148

            
update pod
Yuki Kimoto authored on 2011-01-28
1149
    package MyResult;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1150
    use base 'DBIx::Custom::Result';
1151
    
1152
    sub some_method { ... }
1153

            
1154
    1;
1155
    
1156
    package main;
1157
    
update pod
Yuki Kimoto authored on 2011-01-28
1158
    use MyResult;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1159
    
1160
    my $dbi = DBIx::Custom->connect(...);
update pod
Yuki Kimoto authored on 2011-01-28
1161
    $dbi->result_class('MyResult');
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1162

            
update pod
Yuki Kimoto authored on 2011-01-28
1163
=head3 �L���b�V���O
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1164

            
update pod
Yuki Kimoto authored on 2011-02-11
1165
SQL after parsing tag is cached for performance.
1166
You can set C<cache()>. By default, chaching is true.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1167

            
update pod
Yuki Kimoto authored on 2011-01-28
1168
    $dbi->cache(1);
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1169

            
update pod
Yuki Kimoto authored on 2011-02-11
1170
The way to cache is changed by C<cache_method()>.
1171
Default method is the following one.
1172
Cache is saved to memory.
update pod
Yuki Kimoto authored on 2011-01-28
1173

            
1174
    $dbi->cache_method(sub {
1175
        sub {
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1176
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-01-28
1177
            
1178
            $self->{_cached} ||= {};
1179
            
1180
            if (@_ > 1) {
update pod
Yuki Kimoto authored on 2011-02-11
1181
                # Save cache
update pod
Yuki Kimoto authored on 2011-01-28
1182
                $self->{_cached}{$_[0]} = $_[1] 
1183
            }
1184
            else {
update pod
Yuki Kimoto authored on 2011-02-11
1185
                # Get cache
update pod
Yuki Kimoto authored on 2011-01-28
1186
                return $self->{_cached}{$_[0]}
1187
            }
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1188
        }
update pod
Yuki Kimoto authored on 2011-01-28
1189
    });
1190
    
update pod
Yuki Kimoto authored on 2011-02-11
1191
First argument is L<DBIx::Custom> object.
1192
Second argument is SQL before parsing.
1193
Third argument is SQL information after parsing. This is hash reference.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1194

            
update pod
Yuki Kimoto authored on 2011-02-11
1195
If third argument exists, you save cache,
1196
and if third argument isn't exists, you get chace.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1197

            
pod fix
Yuki Kimoto authored on 2011-01-21
1198
=head1 EXAMPLES
1199

            
update pod
Yuki Kimoto authored on 2011-02-11
1200
You can see exsamples in the following wiki.
1201

            
pod fix
Yuki Kimoto authored on 2011-01-21
1202
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki> - Many useful examples
add examples
Yuki Kimoto authored on 2011-01-07
1203

            
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1204
=cut