DBIx-Custom / DBIx-Custom-0.1711 / blib / man3 / DBIx::Custom::Guide.3pm /
Newer Older
924 lines | 23.687kb
added common test executing ...
Yuki Kimoto authored on 2011-08-07
1
.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
2
.\"
3
.\" Standard preamble:
4
.\" ========================================================================
5
.de Sh \" Subsection heading
6
.br
7
.if t .Sp
8
.ne 5
9
.PP
10
\fB\\$1\fR
11
.PP
12
..
13
.de Sp \" Vertical space (when we can't use .PP)
14
.if t .sp .5v
15
.if n .sp
16
..
17
.de Vb \" Begin verbatim text
18
.ft CW
19
.nf
20
.ne \\$1
21
..
22
.de Ve \" End verbatim text
23
.ft R
24
.fi
25
..
26
.\" Set up some character translations and predefined strings.  \*(-- will
27
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28
.\" double quote, and \*(R" will give a right double quote.  | will give a
29
.\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used to
30
.\" do unbreakable dashes and therefore won't be available.  \*(C` and \*(C'
31
.\" expand to `' in nroff, nothing in troff, for use with C<>.
32
.tr \(*W-|\(bv\*(Tr
33
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34
.ie n \{\
35
.    ds -- \(*W-
36
.    ds PI pi
37
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
39
.    ds L" ""
40
.    ds R" ""
41
.    ds C` ""
42
.    ds C' ""
43
'br\}
44
.el\{\
45
.    ds -- \|\(em\|
46
.    ds PI \(*p
47
.    ds L" ``
48
.    ds R" ''
49
'br\}
50
.\"
51
.\" If the F register is turned on, we'll generate index entries on stderr for
52
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53
.\" entries marked with X<> in POD.  Of course, you'll have to process the
54
.\" output yourself in some meaningful fashion.
55
.if \nF \{\
56
.    de IX
57
.    tm Index:\\$1\t\\n%\t"\\$2"
58
..
59
.    nr % 0
60
.    rr F
61
.\}
62
.\"
63
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
64
.\" way too many mistakes in technical documents.
65
.hy 0
66
.if n .na
67
.\"
68
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69
.\" Fear.  Run.  Save yourself.  No user-serviceable parts.
70
.    \" fudge factors for nroff and troff
71
.if n \{\
72
.    ds #H 0
73
.    ds #V .8m
74
.    ds #F .3m
75
.    ds #[ \f1
76
.    ds #] \fP
77
.\}
78
.if t \{\
79
.    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80
.    ds #V .6m
81
.    ds #F 0
82
.    ds #[ \&
83
.    ds #] \&
84
.\}
85
.    \" simple accents for nroff and troff
86
.if n \{\
87
.    ds ' \&
88
.    ds ` \&
89
.    ds ^ \&
90
.    ds , \&
91
.    ds ~ ~
92
.    ds /
93
.\}
94
.if t \{\
95
.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96
.    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97
.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98
.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99
.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100
.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101
.\}
102
.    \" troff and (daisy-wheel) nroff accents
103
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110
.ds ae a\h'-(\w'a'u*4/10)'e
111
.ds Ae A\h'-(\w'A'u*4/10)'E
112
.    \" corrections for vroff
113
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115
.    \" for low resolution devices (crt and lpr)
116
.if \n(.H>23 .if \n(.V>19 \
117
\{\
118
.    ds : e
119
.    ds 8 ss
120
.    ds o a
121
.    ds d- d\h'-1'\(ga
122
.    ds D- D\h'-1'\(hy
123
.    ds th \o'bp'
124
.    ds Th \o'LP'
125
.    ds ae ae
126
.    ds Ae AE
127
.\}
128
.rm #[ #] #H #V #F C
129
.\" ========================================================================
130
.\"
131
.IX Title "DBIx::Custom::Guide 3"
132
.TH DBIx::Custom::Guide 3 "2011-07-30" "perl v5.8.8" "User Contributed Perl Documentation"
133
.SH "NAME"
134
DBIx::Custom::Guide \- DBIx::Custom Guide
135
.SH "FEATURES"
136
.IX Header "FEATURES"
137
DBIx::Custom is the wrapper class of \s-1DBI\s0 to execute \s-1SQL\s0 easily.
138
This module have the following features.
139
.IP "* Execute \s-1INSERT\s0, \s-1UPDATE\s0, \s-1DELETE\s0, \s-1SELECT\s0 statement easily" 4
140
.IX Item "Execute INSERT, UPDATE, DELETE, SELECT statement easily"
141
.PD 0
142
.IP "* You can specify bind values by hash reference" 4
143
.IX Item "You can specify bind values by hash reference"
144
.IP "* Filtering by data type. and you can set filter to any column" 4
145
.IX Item "Filtering by data type. and you can set filter to any column"
146
.IP "* Creating where clause and order by clause flexibly" 4
147
.IX Item "Creating where clause and order by clause flexibly"
148
.IP "* Support model" 4
149
.IX Item "Support model"
150
.PD
151
.SH "GUIDE"
152
.IX Header "GUIDE"
153
.Sh "Connect to database"
154
.IX Subsection "Connect to database"
155
.Vb 7
156
\&    use DBIx::Custom;
157
\&    my $dbi = DBIx::Custom->connect(
158
\&        dsn => "dbi:mysql:database=bookshop",
159
\&        user => 'ken',
160
\&        password => '!LFKD%$&',
161
\&        dbi_option => {mysql_enable_utf8 => 1}
162
\&    );
163
.Ve
164
.PP
165
You can connect to database by \f(CW\*(C`connect\*(C'\fR method.
166
\&\f(CW\*(C`dsn\*(C'\fR is data source name, \f(CW\*(C`user\*(C'\fR is user name, \f(CW\*(C`password\*(C'\fR is password.
167
.PP
168
\&\f(CW\*(C`dbi_option\*(C'\fR is \s-1DBI\s0 option.
169
By default, the following option is set.
170
Exeption is thrown when fatal error occur and commit mode is auto commit.
171
.PP
172
.Vb 5
173
\&    {
174
\&        RaiseError  =>  1
175
\&        PrintError  =>  0
176
\&        AutoCommit  =>  1
177
\&    }
178
.Ve
179
.Sh "Execute query"
180
.IX Subsection "Execute query"
181
\fIInsert Statement : \f(CI\*(C`insert\*(C'\fI\fR
182
.IX Subsection "Insert Statement : insert"
183
.PP
184
If you want to execute insert statement, use \f(CW\*(C`insert\*(C'\fR method.
185
.PP
186
.Vb 1
187
\&    $dbi->insert({title => 'Perl', author => 'Ken'}, table  => 'book');
188
.Ve
189
.PP
190
First argument is insert row data, \f(CW\*(C`table\*(C'\fR  is table name.
191
.PP
192
\fIUpdate Statement : \f(CI\*(C`update\*(C'\fI\fR
193
.IX Subsection "Update Statement : update"
194
.PP
195
If you want to execute update stateimuse, use \f(CW\*(C`update\*(C'\fR method.
196
.PP
197
.Vb 5
198
\&    $dbi->update(
199
\&        {title => 'Perl', author => 'Ken'},
200
\&        table  => 'book', 
201
\&        where  => {id => 5}
202
\&    );
203
.Ve
204
.PP
205
First argument is update row data, \f(CW\*(C`table\*(C'\fR is table name, \f(CW\*(C`where\*(C'\fR is condition.
206
.PP
207
Note that you can't execute \f(CW\*(C`update\*(C'\fR method without \f(CW\*(C`where\*(C'\fR.
208
If you want to update all rows, use update_all.
209
.PP
210
.Vb 1
211
\&    $dbi->update_all({title => 'Perl', author => 'Ken'}, table  => 'book');
212
.Ve
213
.PP
214
\fIDelete Statement : \f(CI\*(C`delete\*(C'\fI\fR
215
.IX Subsection "Delete Statement : delete"
216
.PP
217
If you want to execute delete statement, use \f(CW\*(C`delete\*(C'\fR method.
218
.PP
219
.Vb 1
220
\&    $dbi->delete(table  => 'book', where  => {author => 'Ken'});
221
.Ve
222
.PP
223
\&\f(CW\*(C`table\*(C'\fR is table name, \f(CW\*(C`where\*(C'\fR is condition.
224
.PP
225
Note that you can't execute \f(CW\*(C`delete\*(C'\fR method without \f(CW\*(C`where\*(C'\fR.
226
If you want to delete all rows, use \f(CW\*(C`delete_all\*(C'\fR method.
227
.PP
228
.Vb 1
229
\&    $dbi->delete_all(table  => 'book');
230
.Ve
231
.PP
232
\fISelect Statement : \f(CI\*(C`select\*(C'\fI\fR
233
.IX Subsection "Select Statement : select"
234
.PP
235
If you want to execute select statement, use \f(CW\*(C`select\*(C'\fR method.
236
.PP
237
.Vb 1
238
\&    my $result = $dbi->select(table => 'book');
239
.Ve
240
.PP
241
Return value is DBIx::Custom::Result object.
242
You can fetch rows by \f(CW\*(C`fetch\*(C'\fR method.
243
.PP
244
.Vb 4
245
\&    while (my $row = $result->fetch) {
246
\&        my $title  = $row->[0];
247
\&        my $author = $row->[1];
248
\&    }
249
.Ve
250
.PP
251
See also \*(L"Fetch row\*(R" in Fetch row about DBIx::Custom::Result.
252
.PP
253
You can specify column names by \f(CW\*(C`column\*(C'\fR option
254
and condition by \f(CW\*(C`where\*(C'\fR option.
255
.PP
256
.Vb 5
257
\&    my $result = $dbi->select(
258
\&        table  => 'book',
259
\&        column => ['author',  'title'],
260
\&        where  => {author => 'Ken'}
261
\&    );
262
.Ve
263
.PP
264
You can specify join clause by \f(CW\*(C`join\*(C'\fR option.
265
.PP
266
.Vb 6
267
\&    my $result = $dbi->select(
268
\&        table  => 'book',
269
\&        column => ['company.name as company_name']
270
\&        where  => {'book.name' => 'Perl'},
271
\&        join   => ['left outer join company on book.company_id = company.id]
272
\&    );
273
.Ve
274
.PP
275
Note that join clause is joined only when \f(CW\*(C`where\*(C'\fR or \f(CW\*(C`column\*(C'\fR option contains table name,
276
such as book.name.
277
.PP
278
You can append statement to the end of whole statement by \f(CW\*(C`append\*(C'\fR option.
279
.PP
280
.Vb 5
281
\&    my $result = $dbi->select(
282
\&        table  => 'book',
283
\&        where  => {author => 'Ken'},
284
\&        append => 'for update',
285
\&    );
286
.Ve
287
.PP
288
\fI\f(CI\*(C`execute\*(C'\fI\fR
289
.IX Subsection "execute"
290
.PP
291
If you want to execute \s-1SQL\s0, use \f(CW\*(C`execute\*(C'\fR method.
292
.PP
293
.Vb 1
294
\&    $dbi->execute("select * from book;");
295
.Ve
296
.PP
297
You can specify named placeholder.
298
.PP
299
.Vb 4
300
\&    $dbi->execute(
301
\&        "select * from book title = :title and author = :author;"
302
\&        {title => 'Perl', author => 'Ken'}
303
\&    );
304
.Ve
305
.PP
306
:title and :author is named placeholder, which is replaced to placeholers.
307
.PP
308
.Vb 1
309
\&    select * from book title = ? and author = ?;
310
.Ve
311
.PP
312
\fI\f(CI\*(C`dbh\*(C'\fI\fR
313
.IX Subsection "dbh"
314
.PP
315
.Vb 1
316
\&    my $dbh = $dbi->dbh;
317
.Ve
318
.PP
319
Get get database handle object of \s-1DBI\s0.
320
.PP
321
\fI\f(CI\*(C`DBI\*(C'\fI methods\fR
322
.IX Subsection "DBI methods"
323
.PP
324
.Vb 2
325
\&    $dbi->do(...);
326
\&    $dbi->begin_work;
327
.Ve
328
.PP
329
You can call all methods of \s-1DBI\s0 from DBIx::Custom object.
330
.Sh "Fetch Rows"
331
.IX Subsection "Fetch Rows"
332
\&\f(CW\*(C`select\*(C'\fR method return value is DBIx::Custom::Result object.
333
You can fetch a row or rows by various methods.
334
.PP
335
\fIFetch a row (array) : \f(CI\*(C`fetch\*(C'\fI\fR
336
.IX Subsection "Fetch a row (array) : fetch"
337
.PP
338
.Vb 1
339
\&    my $row = $result->fetch;
340
.Ve
341
.PP
342
\&\f(CW\*(C`fetch\*(C'\fR method fetch a row and put it into array reference.
343
You can continue to fetch 
344
.PP
345
.Vb 4
346
\&    while (my $row = $result->fetch) {
347
\&        my $title  = $row->[0];
348
\&        my $author = $row->[1];
349
\&    }
350
.Ve
351
.PP
352
\fIFetch only first row (array) : \f(CI\*(C`fetch_first\*(C'\fI\fR
353
.IX Subsection "Fetch only first row (array) : fetch_first"
354
.PP
355
.Vb 1
356
\&    my $row = $result->fetch_first;
357
.Ve
358
.PP
359
\&\f(CW\*(C`fetch_first\*(C'\fR fetch a only first row and finish statment handle,
360
and put it into array refrence.
361
.PP
362
\fIFetch all rows (array) : \f(CI\*(C`fetch_all\*(C'\fI\fR
363
.IX Subsection "Fetch all rows (array) : fetch_all"
364
.PP
365
.Vb 1
366
\&    my $rows = $result->fetch_all;
367
.Ve
368
.PP
369
\&\f(CW\*(C`fetch_all\*(C'\fR fetch all rows and put them into array of array reference.
370
.PP
371
\fIFetch a row (hash) : \f(CI\*(C`fetch_hash\*(C'\fI\fR
372
.IX Subsection "Fetch a row (hash) : fetch_hash"
373
.PP
374
.Vb 1
375
\&    my $row = $result->fetch_hash;
376
.Ve
377
.PP
378
\&\f(CW\*(C`fetch_hash\*(C'\fR fetch a row and put it into hash reference.
379
You can fetch a row while row exists.
380
.PP
381
.Vb 4
382
\&    while (my $row = $result->fetch_hash) {
383
\&        my $title  = $row->{title};
384
\&        my $author = $row->{author};
385
\&    }
386
.Ve
387
.PP
388
\fIFetch only a first row (hash) : \f(CI\*(C`fetch_hash_first\*(C'\fI\fR
389
.IX Subsection "Fetch only a first row (hash) : fetch_hash_first"
390
.PP
391
.Vb 1
392
\&    my $row = $result->fetch_hash_first;
393
.Ve
394
.PP
395
\&\f(CW\*(C`fetch_hash_first\*(C'\fR fetch only a first row and finish statement handle,
396
and put them into hash refrence.
397
.PP
398
\&\f(CW\*(C`one\*(C'\fR is \f(CW\*(C`fetch_hash_first\*(C'\fR synonym to save word typing.
399
.PP
400
.Vb 1
401
\&    my $row = $result->one;
402
.Ve
403
.PP
404
\fIFetch all rows (hash) : \f(CI\*(C`fetch_hash_all\*(C'\fI\fR
405
.IX Subsection "Fetch all rows (hash) : fetch_hash_all"
406
.PP
407
.Vb 1
408
\&    my $rows = $result->fetch_hash_all;
409
.Ve
410
.PP
411
\&\f(CW\*(C`fetch_hash_all\*(C'\fR fetch all rows and put them into array of hash reference.
412
.PP
413
\fIStatement Handle : \f(CI\*(C`sth\*(C'\fI\fR
414
.IX Subsection "Statement Handle : sth"
415
.PP
416
.Vb 1
417
\&    my $sth = $result->sth;
418
.Ve
419
.PP
420
If you want to get statment handle, use <sth> method.
421
.Sh "Named placeholder"
422
.IX Subsection "Named placeholder"
423
\fIBasic of Parameter\fR
424
.IX Subsection "Basic of Parameter"
425
.PP
426
You can embedd named placeholder into \s-1SQL\s0.
427
.PP
428
.Vb 1
429
\&    select * from book where title = :title and author like :author;
430
.Ve
431
.PP
432
:title and :author is named placeholder
433
.PP
434
Named placeholder is replaced by place holder.
435
.PP
436
.Vb 1
437
\&    select * from book where title = ? and author like ?;
438
.Ve
439
.PP
440
use \f(CW\*(C`execute\*(C'\fR to execute \s-1SQL\s0.
441
.PP
442
.Vb 2
443
\&    my $sql = "select * from book where title = :title and author like :author;"
444
\&    $dbi->execute($sql, {title => 'Perl', author => '%Ken%'});
445
.Ve
446
.PP
447
You can specify \f(CW\*(C`filter\*(C'\fR at \f(CW\*(C`execute\*(C'\fR.
448
.PP
449
.Vb 2
450
\&    $dbi->execute($sql, {title => 'Perl', author => '%Ken%'}
451
\&                  filter => {title => 'to_something');
452
.Ve
453
.PP
454
\fIManipulate same name's columns\fR
455
.IX Subsection "Manipulate same name's columns"
456
.PP
457
It is ok if there are same name's columns.
458
Let's think two date comparison.
459
.PP
460
.Vb 1
461
\&    my $sql = "select * from table where date > :date and date < :date;";
462
.Ve
463
.PP
464
In this case, You specify parameter values as array reference.
465
.PP
466
.Vb 1
467
\&    my $dbi->execute($sql, {date => ['2010-10-01', '2012-02-10']});
468
.Ve
469
.Sh "Create where clause"
470
.IX Subsection "Create where clause"
471
\fIDinamically create where clause : where\fR
472
.IX Subsection "Dinamically create where clause : where"
473
.PP
474
You want to search multiple conditions in many times.
475
Let's think the following three cases.
476
.PP
477
Case1: Search only \f(CW\*(C`title\*(C'\fR
478
.PP
479
.Vb 1
480
\&    where title = :title
481
.Ve
482
.PP
483
Case2: Search only \f(CW\*(C`author\*(C'\fR
484
.PP
485
.Vb 1
486
\&    where author = :author
487
.Ve
488
.PP
489
Case3: Search \f(CW\*(C`title\*(C'\fR and \f(CW\*(C`author\*(C'\fR
490
.PP
491
.Vb 1
492
\&    where title = :title and author = :author
493
.Ve
494
.PP
495
DBIx::Custom support dinamic where clause creating.
496
At first, create DBIx::Custom::Where object by \f(CW\*(C`where\*(C'\fR.
497
.PP
498
.Vb 1
499
\&    my $where = $dbi->where;
500
.Ve
501
.PP
502
Set clause by \f(CW\*(C`clause\*(C'\fR
503
.PP
504
.Vb 3
505
\&    $where->clause(
506
\&        ['and', 'title = :title, 'author = :author']
507
\&    );
508
.Ve
509
.PP
510
\&\f(CW\*(C`clause\*(C'\fR is the following format.
511
.PP
512
.Vb 1
513
\&    ['or' or 'and', PART1, PART1, PART1]
514
.Ve
515
.PP
516
First argument is 'or' or 'and'.
517
Later than first argument are part which contains named placeholder.
518
.PP
519
You can write more complex format.
520
.PP
521
.Vb 4
522
\&    ['and', 
523
\&      'title = :title', 
524
\&      ['or', 'author = :author', 'date like :date']
525
\&    ]
526
.Ve
527
.PP
528
This mean \*(L"title = :title and ( author = :author or date like :date )\*(R".
529
.PP
530
After setting \f(CW\*(C`clause\*(C'\fR, set \f(CW\*(C`param\*(C'\fR.
531
.PP
532
.Vb 1
533
\&    $where->param({title => 'Perl'});
534
.Ve
535
.PP
536
In this example, parameter contains only title.
537
.PP
538
If you execute \f(CW\*(C`string_to\*(C'\fR, you can get where clause
539
which contain only named placeholder.
540
.PP
541
.Vb 1
542
\&    my $where_clause = $where->to_string;
543
.Ve
544
.PP
545
Parameter name is only title, the following where clause is created.
546
.PP
547
.Vb 1
548
\&    where title = :title
549
.Ve
550
.PP
551
You can also create where clause by stringification.
552
.PP
553
.Vb 1
554
\&    my $where_clause = "$where";
555
.Ve
556
.PP
557
This is useful to embbed it into \s-1SQL\s0. 
558
.PP
559
\fIIn case where clause contains same name columns\fR
560
.IX Subsection "In case where clause contains same name columns"
561
.PP
562
Even if same name parameters exists, you can create where clause.
563
Let's think that there are starting date and ending date.
564
.PP
565
.Vb 1
566
\&    my $param = {start_date => '2010-11-15', end_date => '2011-11-21'};
567
.Ve
568
.PP
569
In this case, you set parameter value as array reference.
570
.PP
571
.Vb 1
572
\&    my $p = {date => ['2010-11-15', '2011-11-21']};
573
.Ve
574
.PP
575
You can embbed these values into same name parameters.
576
.PP
577
.Vb 4
578
\&    $where->clause(
579
\&        ['and', 'date > :date', 'date < :date']
580
\&    );
581
\&    $where->param($p);
582
.Ve
583
.PP
584
If starting date isn't exists, create the following parameter.
585
.PP
586
.Vb 1
587
\&    my $p = {date => [$dbi->not_exists, '2011-11-21']};
588
.Ve
589
.PP
590
You can get DBIx::Custom::NotExists object by \f(CW\*(C`not_exists\*(C'\fR
591
This mean correnspondinf value isn't exists.
592
.PP
593
If ending date isn't exists, create the following parameter.
594
.PP
595
.Vb 1
596
\&    my $p = {date => ['2010-11-15']};
597
.Ve
598
.PP
599
If both date isn't exists, create the following parameter.
600
.PP
601
.Vb 1
602
\&    my $p = {date => []};
603
.Ve
604
.PP
605
This logic is a little difficut. See the following ones.
606
.PP
607
.Vb 5
608
\&    my @date;
609
\&    push @date, exists $param->{start_date} ? $param->{start_date}
610
\&                                            : $dbi->not_exists;
611
\&    push @date, $param->{end_date} if exists $param->{end_date};
612
\&    my $p = {date => \e@date};
613
.Ve
614
.PP
615
\fIWith \f(CI\*(C`select\*(C'\fI\fR
616
.IX Subsection "With select"
617
.PP
618
You can pass DBIx::Custom::Where object to \f(CW\*(C`where\*(C'\fR of \f(CW\*(C`select\*(C'\fR.
619
.PP
620
.Vb 4
621
\&    my $where = $dbi->where;
622
\&    $where->clause(['and', 'title = :title', 'author = :author']);
623
\&    $where->param({title => 'Perl'});
624
\&    my $result = $dbi->select(table => 'book', where => $where);
625
.Ve
626
.PP
627
You can also pass it to \f(CW\*(C`where\*(C'\fR of \f(CW\*(C`update\*(C'\fRA\f(CW\*(C`delete\*(C'\fR
628
.PP
629
\fIWith \f(CI\*(C`execute\*(C'\fI\fR
630
.IX Subsection "With execute"
631
.PP
632
DBIx::Custom::Where object is embedded into \s-1SQL\s0.
633
.PP
634
.Vb 3
635
\&    my $where = $dbi->where;
636
\&    $where->clause(['and', 'title = :title', 'author = :author']);
637
\&    $where->param({title => 'Perl'});
638
.Ve
639
.PP
640
.Vb 4
641
\&    my $sql = <<"EOS";
642
\&    select * from book;
643
\&    $where
644
\&    EOS
645
.Ve
646
.PP
647
.Vb 1
648
\&    $dbi->execute($sql, $param, table => 'book');
649
.Ve
650
.Sh "Filtering"
651
.IX Subsection "Filtering"
652
\fIRegister filter : \f(CI\*(C`register_filter\*(C'\fI\fR
653
.IX Subsection "Register filter : register_filter"
654
.PP
655
If you want to register filter, use \f(CW\*(C`register_filter\*(C'\fR.
656
.PP
657
.Vb 6
658
\&    $dbi->register_filter(
659
\&        # Time::Piece object to DATE format
660
\&        tp_to_date => sub {
661
\&            my $date = shift;
662
\&            return $tp->strftime('%Y-%m-%d');
663
\&        },
664
.Ve
665
.PP
666
.Vb 6
667
\&        # DATE to Time::Piece object
668
\&        date_to_tp => sub {
669
\&            my $date = shift;
670
\&            return Time::Piece->strptime($date, '%Y-%m-%d');
671
\&        },
672
\&    );
673
.Ve
674
.PP
675
\fIFilter before sending data into database : \f(CI\*(C`filter\*(C'\fI option\fR
676
.IX Subsection "Filter before sending data into database : filter option"
677
.PP
678
If you filter sending data, use \f(CW\*(C`filter\*(C'\fR option.
679
.PP
680
.Vb 5
681
\&    $dbi->execute(
682
\&        'insert into book (date) values (:date)',
683
\&        {date => $tp},
684
\&        filter => {date => 'tp_to_date'}
685
\&    );
686
.Ve
687
.PP
688
You can use \f(CW\*(C`filter\*(C'\fR option in \f(CW\*(C`insert\*(C'\fR, \f(CW\*(C`update\*(C'\fR, \f(CW\*(C`delete\*(C'\fR, \f(CW\*(C`select\*(C'\fR method.
689
.PP
690
.Vb 5
691
\&    $dbi->insert(
692
\&        {date => $tp},
693
\&        table => 'book',
694
\&        filter => {date => 'tp_to_date'}
695
\&    );
696
.Ve
697
.PP
698
\fIFilter after fetching data from database.\fR
699
.IX Subsection "Filter after fetching data from database."
700
.PP
701
If you filter fetch data, use DBIx::Custom::Result's \f(CW\*(C`filter\*(C'\fR method.
702
.PP
703
.Vb 3
704
\&    my $result = $dbi->select(column => 'date', table => 'book');
705
\&    $result->filter(date => 'date_to_tp');
706
\&    my $row = $result->one;
707
.Ve
708
.Sh "7. Model"
709
.IX Subsection "7. Model"
710
\fIModel\fR
711
.IX Subsection "Model"
712
.PP
713
you can define model extending DBIx::Custom::Model
714
to improve source code view.
715
.PP
716
At first, you create basic model class extending <DBIx::Custom::Model>.
717
Each DBIx::Custom class inherit Object::Simple.
718
so you can inherit the following way.
719
.PP
720
.Vb 2
721
\&    package MyModel;
722
\&    use DBIx::Custom::Model -base;
723
.Ve
724
.PP
725
Next, you create each model classes.
726
.PP
727
MyModel::book
728
.PP
729
.Vb 2
730
\&    package MyModel::book;
731
\&    use MyModel -base;
732
.Ve
733
.PP
734
.Vb 2
735
\&    sub insert { ... }
736
\&    sub list { ... }
737
.Ve
738
.PP
739
MyModel::company
740
.PP
741
.Vb 2
742
\&    package MyModel::company;
743
\&    use MyModel -base;
744
.Ve
745
.PP
746
.Vb 2
747
\&    sub insert { ... }
748
\&    sub list { ... }
749
.Ve
750
.PP
751
The follwoing modules location is needed.
752
.PP
753
.Vb 3
754
\&    MyModel.pm
755
\&    MyModel / book.pm
756
\&            / company.pm
757
.Ve
758
.PP
759
You can include these models by \f(CW\*(C`include_model\*(C'\fR
760
.PP
761
.Vb 1
762
\&    $dbi->include_model('MyModel');
763
.Ve
764
.PP
765
First argument is name space of model.
766
.PP
767
You can use model like this.
768
.PP
769
.Vb 1
770
\&    my $result = $dbi->model('book')->list;
771
.Ve
772
.PP
773
In mode, You can use such as methods,
774
\&\f(CW\*(C`insert\*(C'\fR, \f(CW\*(C`update\*(C'\fR, \f(CW\*(C`update_all\*(C'\fR,
775
\&\f(CW\*(C`delete\*(C'\fR, \f(CW\*(C`delete_all\*(C'\fR, \f(CW\*(C`select\*(C'\fR
776
without \f(CW\*(C`table\*(C'\fR option.
777
.PP
778
.Vb 1
779
\&    $dbi->model('book')->insert($param);
780
.Ve
781
.PP
782
Model is DBIx::Custom::Model.
783
.PP
784
If you need table nameAyou can get it by \f(CW\*(C`table\*(C'\fR.
785
.PP
786
.Vb 1
787
\&    my $table = $model->table;
788
.Ve
789
.PP
790
You can get DBIx::Custom.
791
.PP
792
.Vb 1
793
\&    my $dbi = $model->dbi;
794
.Ve
795
.PP
796
You can also call all methods of DBIx::Custom and \s-1DBI\s0. 
797
.PP
798
.Vb 2
799
\&    # DBIx::Custom method
800
\&    $model->execute($sql);
801
.Ve
802
.PP
803
.Vb 3
804
\&    # DBI method
805
\&    $model->begin_work;
806
\&    $model->commit;
807
.Ve
808
.PP
809
If you want to get all models, you can get them by keys of \f(CW\*(C`models\*(C'\fR.
810
.PP
811
.Vb 1
812
\&    my @models = keys %{$self->models};
813
.Ve
814
.PP
815
You can set primary key to model.
816
.PP
817
.Vb 1
818
\&   $model->primary_key(['id', 'number_id']);
819
.Ve
820
.PP
821
Primary key is used by \f(CW\*(C`insert\*(C'\fR, \f(CW\*(C`update\*(C'\fR, \f(CW\*(C`delete\*(C'\fR,
822
and \f(CW\*(C`select\*(C'\fR methods.
823
.PP
824
You can set column names
825
.PP
826
.Vb 1
827
\&    $model->columns(['id', 'number_id']);
828
.Ve
829
.PP
830
Column names is automarically set by \f(CW\*(C`setup_model\*(C'\fR.
831
This method is needed to be call after \f(CW\*(C`include_model\*(C'\fR.
832
.PP
833
.Vb 1
834
\&    $dbi->setup_model;
835
.Ve
836
.PP
837
You can set \f(CW\*(C`join\*(C'\fR
838
.PP
839
.Vb 1
840
\&    $model->join(['left outer join company on book.company_id = company.id']);
841
.Ve
842
.PP
843
\&\f(CW\*(C`join\*(C'\fR is used by \f(CW\*(C`select\*(C'\fR method.
844
.Sh "Create column clause automatically : mycolumn, column"
845
.IX Subsection "Create column clause automatically : mycolumn, column"
846
To create column clause automatically, use \f(CW\*(C`mycolumn\*(C'\fR.
847
Valude of \f(CW\*(C`table\*(C'\fR and \f(CW\*(C`columns\*(C'\fR is used.
848
.PP
849
.Vb 1
850
\&    my $mycolumns = $model->mycolumn;
851
.Ve
852
.PP
853
If \f(CW\*(C`table\*(C'\fR is 'book'A\f(CW\*(C`column\*(C'\fR is ['id', 'name'],
854
the following clause is created.
855
.PP
856
.Vb 1
857
\&    book.id as id, book.name as name
858
.Ve
859
.PP
860
These column name is for removing column name ambiguities.
861
.PP
862
You can create column clause from columns of other table.
863
.PP
864
.Vb 1
865
\&    my $columns = $model->column('company');
866
.Ve
867
.PP
868
If \f(CW\*(C`table\*(C'\fR is \*(L"company\*(R", \f(CW\*(C`column\*(C'\fR return ['id', 'name'],
869
the following clause is created.
870
.PP
871
.Vb 1
872
\&    company.id as "company.id", company.name as "company.name"
873
.Ve
874
.Sh "Model Examples"
875
.IX Subsection "Model Examples"
876
Model examples
877
.PP
878
.Vb 2
879
\&    package MyDBI;
880
\&    use DBIx::Custom -base;
881
.Ve
882
.PP
883
.Vb 2
884
\&    sub connect {
885
\&        my $self = shift->SUPER::connect(@_);
886
.Ve
887
.PP
888
.Vb 7
889
\&        $self->include_model(
890
\&            MyModel => [
891
\&                'book',
892
\&                'company'
893
\&            ]
894
\&        );
895
\&    }
896
.Ve
897
.PP
898
.Vb 2
899
\&    package MyModel::book;
900
\&    use DBIx::Custom::Model -base;
901
.Ve
902
.PP
903
.Vb 1
904
\&    has primary_key => sub { ['id'] };
905
.Ve
906
.PP
907
.Vb 2
908
\&    sub insert { ... }
909
\&    sub list { ... }
910
.Ve
911
.PP
912
.Vb 2
913
\&    package MyModel::company;
914
\&    use DBIx::Custom::Model -base;
915
.Ve
916
.PP
917
.Vb 1
918
\&    has primary_key => sub { ['id'] };
919
.Ve
920
.PP
921
.Vb 2
922
\&    sub insert { ... }
923
\&    sub list { ... }
924
.Ve