Newer Older
1181 lines | 29.597kb
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
L<DBIx::Custom> is the class to make easy to execute SQL.
10
This is L<DBI> wrapper class like L<DBIx::Class> or L<DBIx::Simple>.
11
You can do thing more easy than L<DBIx::Class>, more flexible
12
than L<DBIx::Simple>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
13

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

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

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

            
updated pod
Yuki Kimoto authored on 2011-06-07
27
    select * from book where title = :title and author = :author;
deprecated DBIx::Custom::MyS...
root authored on 2010-11-26
28

            
updated pod
Yuki Kimoto authored on 2011-06-07
29
The part :title is parameter.
update pod
Yuki Kimoto authored on 2011-01-28
30
This SQL is converted to the one which contains place holder.
31

            
32
    select * from book where title = ? and author = ?;
33

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

            
38
=over 4
39

            
40
=item 1. Specify place holder binding value as hash refernce
41

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

            
45
    $sth->execute(@bind);
46

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

            
52
=item 2. Filtering
53

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

            
60
You can use filtering system.
61

            
62
At first, register filter.
63

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

            
73
next, apply this filter to each column.
74

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

            
79
C<out> is perl-to-database way. C<in> is perl-from-database way.
80

            
81
This filter is automatically enabled in many method.
82

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

            
85

            
86
=item 3. Selective search condition
87

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

            
92
    select * from book where title = ? and author = ?;
93

            
94
If only C<title> is specified, the following one
95

            
96
    select * from book where title = ?;
97

            
98
If only C<author> is specified, the following one,
99

            
100
    select * from book where author = ?;
101

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

            
105
    # Where object
106
    my $where = $dbi->where;
107
    
108
    # Search condition
109
    $where->clause(
updated pod
Yuki Kimoto authored on 2011-06-07
110
        ['and', 'title = :title', 'author = :author']
update pod
Yuki Kimoto authored on 2011-01-28
111
    );
112
    
113
    # Setting to automatically select needed column
114
    $where->param({title => 'Perl'});
115

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

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

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

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

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

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

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

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

            
update pod
Yuki Kimoto authored on 2011-01-30
143
use the mehtod.
update pod
Yuki Kimoto authored on 2011-01-28
144

            
145
    $dbi->table('book')->list;
146

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

            
150
=back
151

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

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

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

            
159
    use DBIx::Custom;
160

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

            
164
    my $dbi = DBIx::Custom->connect(
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
165
        dsn => "dbi:mysql:database=bookshop",
update pod
Yuki Kimoto authored on 2011-01-28
166
        user => 'ken',
167
        password => '!LFKD%$&',
168
        dbi_options => {mysql_enable_utf8 => 1}
169
    );
170

            
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
171
C<dsn> must be one corresponding to the database system.
update pod
Yuki Kimoto authored on 2011-01-30
172
The following ones are data source example.
update pod
Yuki Kimoto authored on 2011-01-28
173

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

            
176
    "dbi:mysql:database=$database"
177
    "dbi:mysql:database=$database;host=$hostname;port=$port"
178

            
update pod
Yuki Kimoto authored on 2011-01-28
179
B<SQLite>
180

            
181
    "dbi:SQLite:dbname=$database"
182
    "dbi:SQLite:dbname=:memory:"
183

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

            
186
    "dbi:Pg:dbname=$dbname"
187

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

            
190
    "dbi:Oracle:$dbname"
191
    "dbi:Oracle:host=$host;sid=$sid"
192

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

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

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

            
199
   "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
200

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
update pod
Yuki Kimoto authored on 2011-01-31
247
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
248

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
313
    my $result = $dbi->select(
cleanup
Yuki Kimoto authored on 2011-03-10
314
        table  => 'book',
315
        column => ['company.name as company__name']
316
        where  => {'book.name' => 'Perl'},
317
        join   => ['left outer join company on book.company_id = company.id]
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
318
    );
319

            
cleanup
Yuki Kimoto authored on 2011-03-10
320
You can join table by C<join>.
update pod
Yuki Kimoto authored on 2011-01-31
321

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
324
    select company.name as company__name
325
    from book
cleanup
Yuki Kimoto authored on 2011-03-10
326
      left outer join company on book.company_id = company.id
cleanup
Yuki Kimoto authored on 2011-03-21
327
    where book.name = ?;
cleanup
Yuki Kimoto authored on 2011-03-10
328

            
329
company_if of book and id of company is left outer joined.
330

            
331
Note that only when C<where> or C<column> contain table name,
332
C<join> is joined.
333
if you specify the following option, C<join> is not joined
334
because C<join> is not needed.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
335

            
cleanup
Yuki Kimoto authored on 2011-03-10
336
    my $result = $dbi->select(
337
        table  => 'book',
338
        where  => {'name' => 'Perl'},
339
        join   => ['left outer join company on book.company_id = company.id]
340
    );
341

            
342
Following SQL is executeed.
343

            
344
    select * from book where book.name = ?;
cleanup
Yuki Kimoto authored on 2011-03-21
345

            
346
You can specify column names easily using C<mycolumn()> and C<column()>.
347

            
348
    my $result = $dbi->select(
349
        table  => 'book',
350
        column => [
351
            $dbi->mycolumn('book' => ['name']),
352
            $dbi->column('company' => ['id', 'name'])
353
        ],
354
        join   => ['left outer join company on book.company_id = company.id]
355
    );
356

            
357
The following SQL is executed.
358

            
359
    select book.name as name,
360
      company.id as comapny__id,
361
      company.name as company__name
362
    from book
363
      left outer join company on book.company_id = company.id
364

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

            
367
    my $result = $dbi->select(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
368
        table  => 'book',
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
369
        where  => {author => 'Ken'},
update pod
Yuki Kimoto authored on 2011-01-28
370
        append => 'for update',
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
371
    );
372

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

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

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

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

            
update pod
Yuki Kimoto authored on 2011-02-11
382
=head3 C<execute()>
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
383

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

            
386
    $dbi->execute("select * from book;");
387

            
updated pod
Yuki Kimoto authored on 2011-06-07
388
Process parameter and execute SQL.
update pod
Yuki Kimoto authored on 2011-01-28
389

            
390
    $dbi->execute(
updated pod
Yuki Kimoto authored on 2011-06-07
391
        "select * from book title = :title and author = :author;"
update pod
Yuki Kimoto authored on 2011-01-28
392
        param => {title => 'Perl', author => 'Ken'}
393
    );
394

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

            
397
    select * from book title = ? and author = ?;
398

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

            
updated pod
Yuki Kimoto authored on 2011-06-07
401
See L<5. Parameter/"5. Parameter"> about parameter.
update pod
Yuki Kimoto authored on 2011-01-28
402

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

            
405
    $dbi->execute('select * from book');
406

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

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

            
update pod
Yuki Kimoto authored on 2011-02-11
412
=head3 Fetch a row (array) : C<fetch()>
update pod
Yuki Kimoto authored on 2011-01-28
413

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

            
416
    my $row = $result->fetch;
417

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

            
420
    while (my $row = $result->fetch) {
update pod
Yuki Kimoto authored on 2011-01-28
421
        my $title  = $row->[0];
422
        my $author = $row->[1];
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
423
    }
424

            
update pod
Yuki Kimoto authored on 2011-02-11
425
=head3 Fetch only first row (array) : C<fetch_first()>
update pod
Yuki Kimoto authored on 2011-01-28
426

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

            
429
    my $row = $result->fetch_first;
430

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

            
update pod
Yuki Kimoto authored on 2011-02-11
434
=head3 Fetch rows (array) : C<fetch_multi()>
update pod
Yuki Kimoto authored on 2011-01-28
435

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

            
439
    while (my $rows = $result->fetch_multi(2)) {
440
        my $title0   = $rows->[0][0];
441
        my $author0  = $rows->[0][1];
442
        
443
        my $title1   = $rows->[1][0];
444
        my $author1  = $rows->[1][1];
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
445
    }
update pod
Yuki Kimoto authored on 2011-01-28
446

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

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

            
451
    [
452
        ['Perl', 'Ken'],
453
        ['Ruby', 'Mark']
454
    ]
455

            
update pod
Yuki Kimoto authored on 2011-02-11
456
=head3 Fetch all rows (array) : C<fetch_all>
update pod
Yuki Kimoto authored on 2011-01-28
457

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

            
461
    my $rows = $result->fetch_all;
462

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

            
465
    [
466
        ['Perl', 'Ken'],
467
        ['Ruby', 'Mark']
468
    ]
469

            
update pod
Yuki Kimoto authored on 2011-02-11
470
=head3 Fetch a row (hash) : C<fetch_hash()>
update pod
Yuki Kimoto authored on 2011-01-28
471

            
update pod
Yuki Kimoto authored on 2011-02-04
472
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
473

            
474
    while (my $row = $result->fetch_hash) {
475
        my $title  = $row->{title};
476
        my $author = $row->{author};
477
    }
478

            
update pod
Yuki Kimoto authored on 2011-02-11
479
=head3 Fetch only first row (hash) : C<fetch_hash_first()>
update pod
Yuki Kimoto authored on 2011-01-28
480

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

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

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

            
update pod
Yuki Kimoto authored on 2011-02-11
489
=head3 Fetch rows (hash) : C<fetch_hash_multi()>
update pod
Yuki Kimoto authored on 2011-01-28
490

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

            
494
    while (my $rows = $result->fetch_hash_multi(5)) {
update pod
Yuki Kimoto authored on 2011-01-28
495
        my $title0   = $rows->[0]{title};
496
        my $author0  = $rows->[0]{author};
497
        my $title1  = $rows->[1]{title};
498
        my $author1 = $rows->[1]{author};
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
499
    }
update pod
Yuki Kimoto authored on 2011-01-28
500

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

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

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

            
update pod
Yuki Kimoto authored on 2011-02-11
510
=head3 Fetch all rows (hash) : C<fetch_hash_all()>
update pod
Yuki Kimoto authored on 2011-01-28
511

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

            
516
    my $rows = $result->fetch_hash_all;
517

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

            
520
    [
521
        {title => 'Perl', author => 'Ken'},
522
        {title => 'Ruby', author => 'Mark'}
523
    ]
524

            
update pod
Yuki Kimoto authored on 2011-02-11
525
=head3 Statement handle : C<sth()>
update pod
Yuki Kimoto authored on 2011-01-28
526

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

            
529
    my $sth = $result->sth;
530

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

            
update pod
Yuki Kimoto authored on 2011-02-07
533
L<DBIx::Custom> provide value filtering. 
534
For example, You maybe want to convert L<Time::Piece> object to
535
database date format when register data into database.
536
and convert database date fromat to L<Time::Piece> object
537
when get data from database.
update pod
Yuki Kimoto authored on 2011-01-28
538

            
update pod
Yuki Kimoto authored on 2011-02-11
539
=head3 Register filter : C<register_filter()>
update pod
Yuki Kimoto authored on 2011-01-28
540

            
update pod
Yuki Kimoto authored on 2011-02-11
541
use C<register_filter()> to register filter.
update pod
Yuki Kimoto authored on 2011-01-28
542

            
543
    $dbi->register_filter(
544
        # Time::Piece object to DATE format
545
        tp_to_date => sub {
546
            my $date = shift;
547

            
548
            return '0000-00-00' unless $tp;
549
            return $tp->strftime('%Y-%m-%d');
550
        },
551
        
552
        # DATE to Time::Piece object
553
        date_to_tp => sub {
554
            my $date = shift;
555

            
556
            return if $date eq '0000-00-00';
557
            return Time::Piece->strptime($date, '%Y-%m-%d');
558
        },
559
    );
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
560

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

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

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

            
567
    $dbi->apply_filter('book',
568
        issue_date => {out => 'tp_to_date', in => 'date_to_tp'},
569
        first_issue_date => {out => 'tp_to_date', in => 'date_to_tp'}
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
570
    );
571

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

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

            
579
    issue_date => {out => sub { ... }, in => sub { ... }}
580

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

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

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

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

            
593
    my $row = $resutl->fetch_hash_first;
594
    my $tp = $row->{issue_date};
595

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

            
598
    $dbi->select(
599
        table => 'book',
update pod
Yuki Kimoto authored on 2011-02-07
600
        where => {'book.issue_date' => $tp}
update pod
Yuki Kimoto authored on 2011-01-28
601
    );
602

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

            
605
    my $result = $dbi->execute(
606
       "select issue_date as book__issue_date from book");
607

            
608
You can apply C<end> filter execute after C<in> filter.
609

            
610
    $dbi->apply_filter('book',
611
        issue_date => {out => 'tp_to_date', in => 'date_to_tp',
612
                       end => 'tp_to_displaydate'},
613
    );
614

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

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

            
update pod
Yuki Kimoto authored on 2011-02-07
620
use C<filter> option to apply individual filter
621
when data is send to database.
622
This option is used at C<insert()>, C<update()>,
623
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>,
624
C<execute()>.
update pod
Yuki Kimoto authored on 2011-01-28
625

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

            
628
    $dbi->insert(
629
        table => 'book',
630
        param => {issue_date => $tp, first_issue_date => $tp},
631
        filter => {issue_date => 'tp_to_date', first_issue_date => 'tp_to_date'}
632
    );
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
633

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

            
636
my $sql = <<"EOS";
637
select YEAR(issue_date) as issue_year
638
from book
639
where YEAR(issue_date) = {? issue_year}
640
EOS
641
   
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
642
    my $result = $dbi->execute(
update pod
Yuki Kimoto authored on 2011-01-28
643
        $sql,
644
        param => {issue_year => '2010'},
645
        filter => {issue_year => 'tp_to_year'}
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
646
    );
647

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

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

            
selection can contain where ...
Yuki Kimoto authored on 2011-03-06
653
You can remove filter by C<remove_filter()>
654

            
655
    $result->remove_filter;
656

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

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

            
663
    $result->end_filter(issue_date => sub {
664
        my $tp = shift;
665
        
666
        return '' unless $tp;
667
        return $tp->strftime('%Y/%m/%d %h:%m:%s (%a)');
668
    });
669

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

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
672
You can remove end_filter by C<end_filter>
673

            
674
    $result->remove_end_filter;
675

            
update pod
Yuki Kimoto authored on 2011-02-11
676
=head3 Automate applying filter : C<each_column()>
update pod
Yuki Kimoto authored on 2011-01-28
677

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

            
681
    $dbi->each_column(
682
        sub {
683
            my ($self, $table, $column, $info) = @_;
684
            
685
            my $type = $info->{TYPE_NAME};
686
            
687
            my $filter = $type eq 'DATE'     ? {out => 'tp_to_date', in => 'date_to_tp'}
688
                       : $type eq 'DATETIME' ? {out => 'tp_to_datetime', in => 'datetime_to_tp'}
689
                                             : undef;
690
            
691
            $self->apply_filter($table, $column, $filter)
692
              if $filter;
693
        }
694
    );
695

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

            
updated pod
Yuki Kimoto authored on 2011-06-07
700
=head2 5. Parameter
update pod
Yuki Kimoto authored on 2011-01-28
701

            
updated pod
Yuki Kimoto authored on 2011-06-07
702
=head3 Basic of Parameter
update pod
Yuki Kimoto authored on 2011-01-28
703

            
updated pod
Yuki Kimoto authored on 2011-06-07
704
You can embedd parameter into SQL.
update pod
Yuki Kimoto authored on 2011-01-28
705

            
updated pod
Yuki Kimoto authored on 2011-06-07
706
    select * from book where title = :title and author like :author;
update pod
Yuki Kimoto authored on 2011-01-28
707

            
updated pod
Yuki Kimoto authored on 2011-06-07
708
:title and :author is parameter
update pod
Yuki Kimoto authored on 2011-01-28
709

            
updated pod
Yuki Kimoto authored on 2011-06-07
710
Parameter is converted to place holder.
update pod
Yuki Kimoto authored on 2011-01-28
711

            
712
    select * from book where title = ? and author like ?;
713

            
updated pod
Yuki Kimoto authored on 2011-06-07
714
use C<execute()> to execute SQL.
update pod
Yuki Kimoto authored on 2011-01-28
715

            
updated pod
Yuki Kimoto authored on 2011-06-07
716
    my $sql = "select * from book where title = :title and author like :author;"
update pod
Yuki Kimoto authored on 2011-01-28
717
    $dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'});
718

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

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

            
724
    $dbi->execute($sql, param => {title => 'Perl', author => '%Ken%'}
725
                  filter => {title => 'to_something');
726

            
update pod
Yuki Kimoto authored on 2011-02-07
727
Note that at C<execute()> the filter applied by C<apply_filter()>
728
don't has effective to columns.
improved table search in col...
Yuki Kimoto authored on 2011-04-12
729
You have to use C<table> option
update pod
Yuki Kimoto authored on 2011-01-28
730

            
improved table search in col...
Yuki Kimoto authored on 2011-04-12
731
    $dbi->execute($sql, table => ['author', 'book']);
update pod
Yuki Kimoto authored on 2011-01-28
732

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

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

            
updated pod
Yuki Kimoto authored on 2011-06-07
738
    my $sql = "select * from table where date > :date and date < :date;";
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
739

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

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

            
update pod
Yuki Kimoto authored on 2011-02-11
744
=head2 6. Dinamically create where clause
update pod
Yuki Kimoto authored on 2011-01-28
745

            
update pod
Yuki Kimoto authored on 2011-02-11
746
=head3 Dinamically create where clause : where()
update pod
Yuki Kimoto authored on 2011-01-28
747

            
update pod
Yuki Kimoto authored on 2011-02-11
748
You want to search multiple conditions in many times.
749
Let's think the following three cases.
update pod
Yuki Kimoto authored on 2011-01-28
750

            
update pod
Yuki Kimoto authored on 2011-02-11
751
Case1: Search only C<title>
update pod
Yuki Kimoto authored on 2011-01-28
752

            
updated pod
Yuki Kimoto authored on 2011-06-07
753
    where title = :title
update pod
Yuki Kimoto authored on 2011-01-28
754

            
update pod
Yuki Kimoto authored on 2011-02-11
755
Case2: Search only C<author>
update pod
Yuki Kimoto authored on 2011-01-28
756

            
updated pod
Yuki Kimoto authored on 2011-06-07
757
    where author = :author
update pod
Yuki Kimoto authored on 2011-01-28
758

            
update pod
Yuki Kimoto authored on 2011-02-11
759
Case3: Search C<title> and C<author>
update pod
Yuki Kimoto authored on 2011-01-28
760

            
updated pod
Yuki Kimoto authored on 2011-06-07
761
    where title = :title and author = :author
update pod
Yuki Kimoto authored on 2011-01-28
762

            
update pod
Yuki Kimoto authored on 2011-02-11
763
L<DBIx::Custom> support dinamic where clause creating.
764
At first, create L<DBIx::Custom::Where> object by C<where()>.
update pod
Yuki Kimoto authored on 2011-01-28
765

            
766
    my $where = $dbi->where;
767

            
update pod
Yuki Kimoto authored on 2011-02-11
768
Set clause by C<clause()>
update pod
Yuki Kimoto authored on 2011-01-28
769

            
770
    $where->clause(
updated pod
Yuki Kimoto authored on 2011-06-07
771
        ['and', 'title = :title, 'author = :author']
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
772
    );
773

            
update pod
Yuki Kimoto authored on 2011-02-11
774
C<clause> is the following format.
update pod
Yuki Kimoto authored on 2011-01-28
775

            
updated pod
Yuki Kimoto authored on 2011-06-07
776
    ['or' or 'and', PART1, PART1, PART1]
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
777

            
update pod
Yuki Kimoto authored on 2011-02-11
778
First argument is 'or' or 'and'.
updated pod
Yuki Kimoto authored on 2011-06-07
779
Later than first argument are part which contains parameter.
update pod
Yuki Kimoto authored on 2011-01-28
780

            
update pod
Yuki Kimoto authored on 2011-02-11
781
You can write more complex format.
update pod
Yuki Kimoto authored on 2011-01-28
782

            
783
    ['and', 
updated pod
Yuki Kimoto authored on 2011-06-07
784
      'title = :title', 
785
      ['or', 'author = :author', 'date like :date']
update pod
Yuki Kimoto authored on 2011-01-28
786
    ]
787

            
updated pod
Yuki Kimoto authored on 2011-06-07
788
This mean "title = :title and ( author = :author or date like :date )".
update pod
Yuki Kimoto authored on 2011-02-11
789

            
790
After setting C<clause>, set C<param>.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
791
    
update pod
Yuki Kimoto authored on 2011-02-11
792
    $where->param({title => 'Perl'});
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
793

            
update pod
Yuki Kimoto authored on 2011-02-11
794
In this example, parameter contains only title.
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
795

            
update pod
Yuki Kimoto authored on 2011-02-11
796
If you execute C<string_to()>, you can get where clause
797
which contain only parameter name.
update pod
Yuki Kimoto authored on 2011-01-28
798

            
799
    my $where_clause = $where->to_string;
800

            
update pod
Yuki Kimoto authored on 2011-02-11
801
Parameter name is only title, the following where clause is created.
update pod
Yuki Kimoto authored on 2011-01-28
802

            
updated pod
Yuki Kimoto authored on 2011-06-07
803
    where title = :title
update pod
Yuki Kimoto authored on 2011-01-28
804

            
update pod
Yuki Kimoto authored on 2011-02-11
805
You can also create where clause by stringification.
update pod
Yuki Kimoto authored on 2011-01-28
806

            
807
    my $where_clause = "$where";
808

            
update pod
Yuki Kimoto authored on 2011-02-11
809
This is useful to embbed it into SQL. 
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
810

            
update pod
Yuki Kimoto authored on 2011-02-11
811
=head3 In case where clause contains same name columns
check arguments of connect m...
Yuki Kimoto authored on 2010-12-20
812

            
updated pod
Yuki Kimoto authored on 2011-06-07
813
Even if same name parameters exists, you can create where clause.
update pod
Yuki Kimoto authored on 2011-02-11
814
Let's think that there are starting date and ending date.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
815

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

            
update pod
Yuki Kimoto authored on 2011-02-11
818
In this case, you set parameter value as array reference.
update pod
Yuki Kimoto authored on 2011-01-28
819

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

            
updated pod
Yuki Kimoto authored on 2011-06-07
822
You can embbed these values into same name parameters.
update pod
Yuki Kimoto authored on 2011-01-28
823

            
824
    $where->clause(
updated pod
Yuki Kimoto authored on 2011-06-07
825
        ['and', 'date > :date', 'date < :date']
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
826
    );
update pod
Yuki Kimoto authored on 2011-01-28
827
    $where->param($p);
828

            
update pod
Yuki Kimoto authored on 2011-02-11
829
If starting date isn't exists, create the following parameter.
update pod
Yuki Kimoto authored on 2011-01-28
830

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

            
update pod
Yuki Kimoto authored on 2011-02-11
833
You can get DBIx::Custom::NotExists object by C<not_exists()>
834
This mean correnspondinf value isn't exists.
update pod
Yuki Kimoto authored on 2011-01-28
835

            
update pod
Yuki Kimoto authored on 2011-02-11
836
If ending date isn't exists, create the following parameter.
update pod
Yuki Kimoto authored on 2011-01-28
837

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

            
update pod
Yuki Kimoto authored on 2011-02-11
840
If both date isn't exists, create the following parameter.
update pod
Yuki Kimoto authored on 2011-01-28
841

            
842
    my $p = {date => []};
843

            
update pod
Yuki Kimoto authored on 2011-02-11
844
This logic is a little difficut. See the following ones.
update pod
Yuki Kimoto authored on 2011-01-28
845

            
846
    my @date;
847
    push @date, exists $param->{start_date} ? $param->{start_date}
848
                                            : $dbi->not_exists;
849
    push @date, $param->{end_date} if exists $param->{end_date};
850
    my $p = {date => \@date};
851

            
update pod
Yuki Kimoto authored on 2011-02-11
852
=head3 With C<select()>
update pod
Yuki Kimoto authored on 2011-01-28
853

            
update pod
Yuki Kimoto authored on 2011-02-11
854
You can pass L<DBIx::Custom::Where> object to C<where> of C<select()>.
update pod
Yuki Kimoto authored on 2011-01-28
855
    
856
    my $where = $dbi->where;
updated pod
Yuki Kimoto authored on 2011-06-07
857
    $where->clause(['and', 'title = :title', 'author = :author']);
update pod
Yuki Kimoto authored on 2011-02-11
858
    $where->param({title => 'Perl'});
update pod
Yuki Kimoto authored on 2011-01-28
859
    my $result = $dbi->select(table => 'book', where => $where);
860

            
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
861
You can also pass it to C<where> of C<update()>AC<delete()>
update pod
Yuki Kimoto authored on 2011-01-28
862

            
update pod
Yuki Kimoto authored on 2011-02-11
863
=head3 With C<execute()>
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
864

            
update pod
Yuki Kimoto authored on 2011-02-11
865
L<DBIx::Custom::Where> object is embedded into SQL.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
866

            
update pod
Yuki Kimoto authored on 2011-01-28
867
    my $where = $dbi->where;
updated pod
Yuki Kimoto authored on 2011-06-07
868
    $where->clause(['and', 'title = :title', 'author = :author']);
update pod
Yuki Kimoto authored on 2011-02-11
869
    $where->param({title => 'Perl'});
update pod
Yuki Kimoto authored on 2011-01-28
870

            
update pod
Yuki Kimoto authored on 2011-02-11
871
    my $sql = <<"EOS";
updated pod
Yuki Kimoto authored on 2011-06-07
872
    select * from book;
update pod
Yuki Kimoto authored on 2011-01-28
873
    $where
874
    EOS
875

            
updated pod
Yuki Kimoto authored on 2011-06-07
876
    $dbi->execute($sql, param => $param, table => 'book');
update pod
Yuki Kimoto authored on 2011-01-28
877

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
878
=head2 7. Model
update pod
Yuki Kimoto authored on 2011-01-28
879

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
880
=head3 Model
update pod
Yuki Kimoto authored on 2011-01-28
881

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
882
you can define model extending L<DBIx::Custom::Model>
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
883
to improve source code view.
update pod
Yuki Kimoto authored on 2011-01-28
884

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
885
At first, you create basic model class extending <DBIx::Custom::Model>.
update pod
Yuki Kimoto authored on 2011-01-28
886

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
887
    package MyModel;
888
    
889
    use base 'DBIx::Custom::Model';
update pod
Yuki Kimoto authored on 2011-01-28
890

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
891
Next, you create each model classes.
update pod
Yuki Kimoto authored on 2011-01-28
892

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
893
MyModel::book
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
894

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
895
    package MyModel::book;
896
    
897
    use base 'MyModel';
898
    
899
    sub insert { ... }
900
    sub list { ... }
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
901

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
902
MyModel::company
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
903

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
904
    package MyModel::company;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
905
    
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
906
    use base 'MyModel';
907
    
908
    sub insert { ... }
909
    sub list { ... }
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
910

            
911
The follwoing modules location is needed.
912

            
913
    MyModel.pm
914
    MyModel / book.pm
915
            / company.pm
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
916

            
917
You can include these models by C<include_model()>
918

            
919
    $dbi->include_model('MyModel');
920

            
921
First argument is name space of model.
922

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
923
You can use model like this.
924

            
925
    my $result = $dbi->model('book')->list;
926

            
927
In mode, You can use such as methods,
928
C<insert()>, C<update()>, C<update_all()>,
929
C<delete()>, C<delete_all()>, C<select()>
930
without C<table> option.
931

            
932
    $dbi->model('book')->insert(param => $param);
update pod
Yuki Kimoto authored on 2011-01-28
933

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
934
Model is L<DBIx::Custom::Model>.
update pod
Yuki Kimoto authored on 2011-01-28
935

            
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
936
If you need table nameAyou can get it by C<table()>.
update pod
Yuki Kimoto authored on 2011-01-28
937

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
938
    my $table = $model->table;
update pod
Yuki Kimoto authored on 2011-01-28
939

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
940
You can get L<DBIx::Custom>.
update pod
Yuki Kimoto authored on 2011-01-28
941

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
944
You can also call all methods of L<DBIx::Custom> and L<DBI>. 
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
945

            
update pod
Yuki Kimoto authored on 2011-02-11
946
    # DBIx::Custom method
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
947
    $model->execute($sql);
update pod
Yuki Kimoto authored on 2011-02-11
948
    
949
    # DBI method
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
950
    $model->begin_work;
951
    $model->commit;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
952

            
add models() attribute
Yuki Kimoto authored on 2011-02-21
953
If you want to get all models, you can get them by keys of C<models()>.
954

            
955
    my @models = keys %{$self->models};
956

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
957
You can set primary key to model.
958

            
959
   $model->primary_key(['id', 'number_id']);
960

            
update pod
Yuki Kimoto authored on 2011-02-28
961
Primary key is used by C<insert_at>, C<update_at()>, C<delete_at()>,
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
962
C<select_at()>.
963

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
964
by C<filter> you can define filters applied by C<apply_filter()>
965

            
966
    $model->filter({
967
        title  => {out => ..., in => ..., end => ...},
968
        author => {out => ..., in => ..., end => ...}
969
    });
970

            
971
This filters is applied when C<include_model()> is called.
972

            
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
973
You can set column names
974

            
975
    $model->columns(['id', 'number_id']);
976

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
977
Column names is automarically set by C<setup_model()>.
978
This method is needed to be call after C<include_model()>.
979

            
980
    $dbi->setup_model;
981

            
cleanup
Yuki Kimoto authored on 2011-03-10
982
You can set C<join>
cleanup
Yuki Kimoto authored on 2011-02-22
983

            
cleanup
Yuki Kimoto authored on 2011-03-10
984
    $model->join(['left outer join company on book.company_id = company.id']);
cleanup
Yuki Kimoto authored on 2011-02-22
985

            
cleanup
Yuki Kimoto authored on 2011-03-10
986
This C<join> is used by C<select()>, C<select_at()>
cleanup
Yuki Kimoto authored on 2011-02-22
987

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
988
=head2 Class name, Model name, Table name
989

            
990
Class name, model name, and table name is a little different.
991
Generally Class name is model name, and table name is model name.
992

            
993
    CLASS        MODEL              TABLE
994
    book         (CLASS) -> book    (MODEL) -> book
995

            
996
You can change model name.
997

            
998
    package MyModel::book;
999
    
1000
    __PACAKGE__->attr(name => 'book_model');
1001

            
1002
    CLASS        MODEL         TABLE
1003
    book         book_model    (MODEL) -> book_model
1004

            
1005
Model name is the name used by L<model()> of L<DBIx::Custom>.
1006

            
1007
    $dbi->model('book_model');
1008

            
1009
You can change table name.
1010

            
1011
    package MyModel::book;
1012
    
1013
    __PACAKGE__->attr(table => 'book_table');
1014

            
1015
    CLASS        MODEL              TABLE
1016
    book         (CLASS) -> book    book_table
1017

            
1018
Table name is the table really accessed.
1019

            
1020
    $dbi->model('book')->insert(...); # access to "book_table"
1021

            
cleanup
Yuki Kimoto authored on 2011-03-21
1022
=head2 Create column clause automatically : mycolumn(), column()
1023

            
1024
To create column clause automatically, use C<mycolumn()>.
1025
Valude of C<table> and C<columns> is used.
1026

            
1027
    my $column_clause = $model->mycolumn;
1028

            
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
1029
If C<table> is 'book'AC<column> is ['id', 'name'],
cleanup
Yuki Kimoto authored on 2011-03-21
1030
the following clause is created.
1031

            
1032
    book.id as id, book.name as name
1033

            
1034
These column name is for removing column name ambiguities.
1035

            
1036
You can create column clause from columns of other table.
1037

            
1038
    my $column_clause = $model->column('company');
1039

            
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
1040
If C<table> is 'company'AC<column> is ['id', 'name'],
cleanup
Yuki Kimoto authored on 2011-03-21
1041
the following clause is created.
1042

            
1043
    company.id as company__id, company.name as company__name
1044

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
1045
=head2 Create column clause automatically : column_clause()
1046

            
1047
To create column clause automatically, use C<column_clause()>.
1048
Valude of C<table> and C<columns> is used.
1049

            
1050
    my $column_clause = $model->column_clause;
1051

            
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
1052
If C<table> is 'book'AC<column> is ['id', 'name'],
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
1053
the following clause is created.
1054

            
1055
    book.id as id, book.name as name
1056

            
1057
These column name is for removing column name ambiguities.
1058

            
1059
If you remove some columns, use C<remove> option.
1060

            
1061
    my $column_clause = $model->column_clause(remove => ['id']);
1062

            
1063
If you add some column, use C<add> option.
1064

            
1065
    my $column_clause = $model->column_clause(add => ['company.id as company__id']);
1066

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1067
=head2 Model Examples
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1068

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1069
Model examples
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1070

            
update pod
Yuki Kimoto authored on 2011-01-28
1071
    package MyDBI;
1072
    
1073
    use base 'DBIx::Custom';
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1074
    
update pod
Yuki Kimoto authored on 2011-01-28
1075
    sub connect {
1076
        my $self = shift->SUPER::connect(@_);
1077
        
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1078
        $self->include_model(
1079
            MyModel => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1080
                'book',
1081
                'company'
1082
            ]
update pod
Yuki Kimoto authored on 2011-01-28
1083
        );
1084
    }
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1085
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1086
    package MyModel::book;
1087
    use base 'DBIx::Custom::Model';
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1088
    
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1089
    __PACKAGE__->attr('primary_key' => sub { ['id'] };
1090
    
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1091
    sub insert { ... }
1092
    sub list { ... }
1093
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1094
    package MyModel::company;
1095
    use base 'DBIx::Custom::Model';
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1096

            
1097
    __PACKAGE__->attr('primary_key' => sub { ['id'] };
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1098
    
1099
    sub insert { ... }
1100
    sub list { ... }
update pod
Yuki Kimoto authored on 2011-01-28
1101

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

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

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

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

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

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

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

            
1126
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
1127

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

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

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

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

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

            
update pod
Yuki Kimoto authored on 2011-01-28
1141
    $dbi->method(
1142
        update_or_insert => sub {
1143
            my $self = shift;
1144
            # something
1145
        },
1146
        find_or_create   => sub {
1147
            my $self = shift;
1148
            # something
1149
        }
1150
    );
1151

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

            
1154
    $dbi->update_or_insert;
1155
    $dbi->find_or_create;
1156

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

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

            
update pod
Yuki Kimoto authored on 2011-01-28
1161
    package MyResult;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1162
    use base 'DBIx::Custom::Result';
1163
    
1164
    sub some_method { ... }
1165

            
1166
    1;
1167
    
1168
    package main;
1169
    
update pod
Yuki Kimoto authored on 2011-01-28
1170
    use MyResult;
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1171
    
1172
    my $dbi = DBIx::Custom->connect(...);
update pod
Yuki Kimoto authored on 2011-01-28
1173
    $dbi->result_class('MyResult');
added DBIx::Custom::Guides
yuki-kimoto authored on 2010-10-17
1174

            
pod fix
Yuki Kimoto authored on 2011-01-21
1175
=head1 EXAMPLES
1176

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

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

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